Skip to content

Commit

Permalink
Add NodePathPattern (#34591)
Browse files Browse the repository at this point in the history
* Add DataNodePathPattern

* Add NodePathPattern

* Add NodePathPattern
  • Loading branch information
terrymanu authored Feb 7, 2025
1 parent 72676a5 commit 6b26cfb
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.node.path;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
* Node path pattern.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class NodePathPattern {

/**
* Identifier pattern.
*/
public static final String IDENTIFIER = "([\\w\\-]+)";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;

import java.util.Optional;
Expand All @@ -33,10 +34,6 @@ public final class GlobalRuleNodePath {

private static final String ROOT_NODE = "/rules";

private static final String IDENTIFIER_PATTERN = "(\\w+)";

private static final String VERSION_PATTERN = "(\\d+)";

/**
* Get global rule root path.
*
Expand Down Expand Up @@ -73,7 +70,7 @@ public static VersionNodePath getVersionNodePath(final String ruleTypeName) {
* @return found rule type name
*/
public static Optional<String> findRuleTypeNameFromActiveVersion(final String path) {
Pattern pattern = Pattern.compile(getVersionNodePath(IDENTIFIER_PATTERN).getActiveVersionPath() + "$", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getVersionNodePath(NodePathPattern.IDENTIFIER).getActiveVersionPath() + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.mode.node.path.config.rule.item;

import org.apache.shardingsphere.mode.node.path.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.config.rule.root.RuleRootNodePath;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;

Expand All @@ -29,10 +30,6 @@
*/
public final class NamedRuleItemNodePath {

private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";

private static final String VERSION_PATTERN = "(\\d+)";

private final String type;

private final Pattern namePathPattern;
Expand All @@ -43,9 +40,9 @@ public final class NamedRuleItemNodePath {

public NamedRuleItemNodePath(final RuleRootNodePath rootNodePath, final String type) {
this.type = type;
String pattern = String.join("/", rootNodePath.getNodePrefix() + type, IDENTIFIER_PATTERN);
String pattern = String.join("/", rootNodePath.getNodePrefix() + type, NodePathPattern.IDENTIFIER);
VersionNodePath versionNodePath = new VersionNodePath(pattern);
namePathPattern = Pattern.compile(String.join("/", versionNodePath.getVersionsPath(), VERSION_PATTERN));
namePathPattern = Pattern.compile(String.join("/", versionNodePath.getVersionsPath(), VersionNodePath.VERSION_PATTERN));
activeVersionPathPattern = Pattern.compile(versionNodePath.getActiveVersionPath() + "$");
itemPathPattern = Pattern.compile(pattern + "$");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
*/
public final class UniqueRuleItemNodePath {

private static final String VERSION_PATTERN = "(\\d+)";

private final String parentNode;

private final String type;
Expand All @@ -42,15 +40,15 @@ public UniqueRuleItemNodePath(final RuleRootNodePath ruleRootNodePath, final Str
parentNode = null;
this.type = type;
VersionNodePath versionNodePath = new VersionNodePath(ruleRootNodePath.getNodePrefix() + type);
pathPattern = Pattern.compile(String.join("/", versionNodePath.getVersionsPath(), VERSION_PATTERN));
pathPattern = Pattern.compile(String.join("/", versionNodePath.getVersionsPath(), VersionNodePath.VERSION_PATTERN));
activeVersionPathPattern = Pattern.compile(versionNodePath.getActiveVersionPath() + "$");
}

public UniqueRuleItemNodePath(final RuleRootNodePath ruleRootNodePath, final String parentNode, final String type) {
this.parentNode = parentNode;
this.type = type;
VersionNodePath versionNodePath = new VersionNodePath(ruleRootNodePath.getNodePrefix() + parentNode + "/" + type);
pathPattern = Pattern.compile(String.join("/", versionNodePath.getVersionsPath(), VERSION_PATTERN));
pathPattern = Pattern.compile(String.join("/", versionNodePath.getVersionsPath(), VersionNodePath.VERSION_PATTERN));
activeVersionPathPattern = Pattern.compile(versionNodePath.getActiveVersionPath() + "$");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;

import java.util.Optional;
Expand All @@ -39,8 +40,6 @@ public final class DataSourceMetaDataNodePath {

private static final String UNITS_NODE = "units";

private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";

/**
* Get data source root path.
*
Expand Down Expand Up @@ -122,7 +121,7 @@ public static VersionNodePath getStorageNodeVersionNodePath(final String databas
* @return found storage unit name
*/
public static Optional<String> findStorageUnitNameByActiveVersionPath(final String path) {
Pattern pattern = Pattern.compile(getStorageUnitVersionNodePath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getStorageUnitVersionNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
Expand All @@ -134,7 +133,7 @@ public static Optional<String> findStorageUnitNameByActiveVersionPath(final Stri
* @return found storage unit name
*/
public static Optional<String> findStorageUnitNameByStorageUnitPath(final String path) {
Pattern pattern = Pattern.compile(getStorageUnitPath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getStorageUnitPath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
Expand All @@ -146,7 +145,7 @@ public static Optional<String> findStorageUnitNameByStorageUnitPath(final String
* @return found storage unit name
*/
public static Optional<String> findStorageNodeNameByActiveVersionPath(final String path) {
Pattern pattern = Pattern.compile(getStorageNodeVersionNodePath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getStorageNodeVersionNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
Expand All @@ -158,7 +157,7 @@ public static Optional<String> findStorageNodeNameByActiveVersionPath(final Stri
* @return found storage unit name
*/
public static Optional<String> findStorageNodeNameByStorageNodePath(final String path) {
Pattern pattern = Pattern.compile(getStorageNodePath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getStorageNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
Expand All @@ -170,6 +169,6 @@ public static Optional<String> findStorageNodeNameByStorageNodePath(final String
* @return true or false
*/
public static boolean isDataSourceRootPath(final String path) {
return Pattern.compile(getDataSourceRootPath(IDENTIFIER_PATTERN) + "?", Pattern.CASE_INSENSITIVE).matcher(path).find();
return Pattern.compile(getDataSourceRootPath(NodePathPattern.IDENTIFIER) + "?", Pattern.CASE_INSENSITIVE).matcher(path).find();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;

import java.util.Optional;
import java.util.regex.Matcher;
Expand All @@ -38,8 +39,6 @@ public final class DatabaseMetaDataNodePath {

private static final String ACTIVE_VERSION_NODE = "active_version";

private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";

/**
* Get meta data root path.
*
Expand Down Expand Up @@ -100,7 +99,7 @@ public static String getVersionPath(final String rulePath, final int activeVersi
*/
public static Optional<String> findDatabaseName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(getDatabasePath(IDENTIFIER_PATTERN) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getDatabasePath(NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}
Expand All @@ -114,7 +113,7 @@ public static Optional<String> findDatabaseName(final String path, final boolean
*/
public static Optional<String> findSchemaName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(getSchemaPath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getSchemaPath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;

import java.util.Optional;
import java.util.regex.Matcher;
Expand All @@ -40,8 +41,6 @@ public final class StatisticsNodePath {

private static final String JOB_NODE = "job";

private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";

private static final String UNIQUE_KEY_PATTERN = "(\\w+)";

/**
Expand Down Expand Up @@ -129,7 +128,7 @@ public static String getTableRowPath(final String databaseName, final String sch
*/
public static Optional<String> findDatabaseName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(getDatabasePath(IDENTIFIER_PATTERN) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getDatabasePath(NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}
Expand All @@ -143,7 +142,7 @@ public static Optional<String> findDatabaseName(final String path, final boolean
*/
public static Optional<String> findSchemaName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(getSchemaPath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getSchemaPath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
Expand All @@ -157,7 +156,7 @@ public static Optional<String> findSchemaName(final String path, final boolean c
*/
public static Optional<String> findTableName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(getTablePath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getTablePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
Expand All @@ -169,7 +168,8 @@ public static Optional<String> findTableName(final String path, final boolean co
* @return found row unique key
*/
public static Optional<String> findRowUniqueKey(final String path) {
Pattern pattern = Pattern.compile(getTableRowPath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, UNIQUE_KEY_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(
getTableRowPath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, UNIQUE_KEY_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(4)) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;

import java.util.Optional;
Expand All @@ -33,8 +34,6 @@ public final class TableMetaDataNodePath {

private static final String TABLES_NODE = "tables";

private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";

/**
* Get table root path.
*
Expand Down Expand Up @@ -77,7 +76,7 @@ public static VersionNodePath getVersionNodePath(final String databaseName, fina
* @return found table name
*/
public static Optional<String> findTableName(final String path) {
Pattern pattern = Pattern.compile(getTablePath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getTablePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
Expand All @@ -99,7 +98,8 @@ public static boolean isTablePath(final String path) {
* @return table name
*/
public static Optional<String> findTableNameByActiveVersionPath(final String path) {
Pattern pattern = Pattern.compile(getVersionNodePath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, IDENTIFIER_PATTERN).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(
getVersionNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.version.VersionNodePath;

import java.util.Optional;
Expand All @@ -33,8 +34,6 @@ public final class ViewMetaDataNodePath {

private static final String VIEWS_NODE = "views";

private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";

/**
* Get view root path.
*
Expand Down Expand Up @@ -77,7 +76,7 @@ public static VersionNodePath getVersionNodePath(final String databaseName, fina
* @return view name
*/
public static Optional<String> findViewName(final String path) {
Pattern pattern = Pattern.compile(getViewPath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(getViewPath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
Expand All @@ -99,7 +98,8 @@ public static boolean isViewPath(final String path) {
* @return view name
*/
public static Optional<String> findViewNameByActiveVersionPath(final String path) {
Pattern pattern = Pattern.compile(getVersionNodePath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, IDENTIFIER_PATTERN).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(
getVersionNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Expand All @@ -30,12 +29,12 @@
@Getter
public final class VersionNodePath {

public static final String VERSION_PATTERN = "(\\d+)";

private static final String ACTIVE_VERSION = "active_version";

private static final String VERSIONS = "versions";

private static final String VERSION_PATTERN = "(\\d+)";

private final String path;

/**
Expand Down Expand Up @@ -74,7 +73,6 @@ public String getVersionPath(final int version) {
*/
public boolean isVersionPath(final String path) {
Pattern pattern = Pattern.compile(String.join("/", getVersionsPath(), VERSION_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find();
return pattern.matcher(path).find();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

class VersionNodePathTest {

private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";

@Test
void assertGetActiveVersionPath() {
assertThat(new VersionNodePath("foo").getActiveVersionPath(), is("foo/active_version"));
Expand All @@ -43,8 +45,8 @@ void assertGetVersionPath() {

@Test
void assertIsVersionPath() {
assertTrue(new VersionNodePath("foo").isVersionPath("foo/versions/0"));
assertFalse(new VersionNodePath("foo").isVersionPath("foo/versions"));
assertFalse(new VersionNodePath("foo").isVersionPath("foo/versions/0/xxx"));
assertTrue(new VersionNodePath(IDENTIFIER_PATTERN).isVersionPath("foo/versions/0"));
assertFalse(new VersionNodePath(IDENTIFIER_PATTERN).isVersionPath("foo/versions"));
assertFalse(new VersionNodePath(IDENTIFIER_PATTERN).isVersionPath("foo/versions/0/xxx"));
}
}

0 comments on commit 6b26cfb

Please sign in to comment.