This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 3a085807082 Refactor ShardingSphereDataNodePath (#34275)
3a085807082 is described below
commit 3a085807082cc649ecefe63014efa818c0f3d274
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Jan 7 23:34:30 2025 +0800
Refactor ShardingSphereDataNodePath (#34275)
---
.../data/ShardingSphereDataPersistService.java | 2 +-
.../persist/node/ShardingSphereDataNodePath.java | 102 ++++++++-------------
.../node/ShardingSphereDataNodePathTest.java | 77 ++++++----------
.../global/ShardingSphereDataChangedHandler.java | 22 ++---
4 files changed, 76 insertions(+), 127 deletions(-)
diff --git
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java
index ecdd0f50cb7..0966cbe6209 100644
---
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java
+++
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java
@@ -98,7 +98,7 @@ public final class ShardingSphereDataPersistService {
}
private void persistSchema(final String databaseName, final String
schemaName) {
-
repository.persist(ShardingSphereDataNodePath.getSchemaDataPath(databaseName,
schemaName), "");
+
repository.persist(ShardingSphereDataNodePath.getSchemaPath(databaseName,
schemaName), "");
}
private void persistTableData(final ShardingSphereDatabase database, final
String schemaName, final ShardingSphereSchemaData schemaData) {
diff --git
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/ShardingSphereDataNodePath.java
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/ShardingSphereDataNodePath.java
index b3105b2c2d7..b1b4762c8d0 100644
---
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/ShardingSphereDataNodePath.java
+++
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/ShardingSphereDataNodePath.java
@@ -40,6 +40,10 @@ public final class ShardingSphereDataNodePath {
private static final String JOB_NODE = "job";
+ private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";
+
+ private static final String UNIQUE_KEY_PATTERN = "(\\w+)";
+
/**
* Get database root path.
*
@@ -76,7 +80,7 @@ public final class ShardingSphereDataNodePath {
* @param schemaName schema name
* @return schema path
*/
- public static String getSchemaDataPath(final String databaseName, final
String schemaName) {
+ public static String getSchemaPath(final String databaseName, final String
schemaName) {
return String.join("/", getSchemaRootPath(databaseName), schemaName);
}
@@ -88,7 +92,7 @@ public final class ShardingSphereDataNodePath {
* @return table root path
*/
public static String getTableRootPath(final String databaseName, final
String schemaName) {
- return String.join("/", getSchemaDataPath(databaseName, schemaName),
TABLES_NODE);
+ return String.join("/", getSchemaPath(databaseName, schemaName),
TABLES_NODE);
}
/**
@@ -96,11 +100,11 @@ public final class ShardingSphereDataNodePath {
*
* @param databaseName database name
* @param schemaName schema name
- * @param table table name
+ * @param tableName table name
* @return table path
*/
- public static String getTablePath(final String databaseName, final String
schemaName, final String table) {
- return String.join("/", getTableRootPath(databaseName, schemaName),
table);
+ public static String getTablePath(final String databaseName, final String
schemaName, final String tableName) {
+ return String.join("/", getTableRootPath(databaseName, schemaName),
tableName);
}
/**
@@ -108,95 +112,65 @@ public final class ShardingSphereDataNodePath {
*
* @param databaseName database name
* @param schemaName schema name
- * @param table table name
+ * @param tableName table name
* @param uniqueKey unique key
* @return table row path
*/
- public static String getTableRowPath(final String databaseName, final
String schemaName, final String table, final String uniqueKey) {
- return String.join("/", getTablePath(databaseName, schemaName, table),
uniqueKey);
+ public static String getTableRowPath(final String databaseName, final
String schemaName, final String tableName, final String uniqueKey) {
+ return String.join("/", getTablePath(databaseName, schemaName,
tableName), uniqueKey);
}
/**
* Find database name.
*
- * @param configNodeFullPath config node full path
+ * @param path path
+ * @param containsChildPath whether contains child path
* @return found database name
*/
- public static Optional<String> findDatabaseName(final String
configNodeFullPath) {
- Pattern pattern = Pattern.compile(getDatabasesRootPath() +
"/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(configNodeFullPath);
+ 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);
+ Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) :
Optional.empty();
}
/**
* Find schema name.
*
- * @param configNodeFullPath config node full path
+ * @param path path
+ * @param containsChildPath whether contains child path
* @return found schema name
*/
- public static Optional<String> findSchemaName(final String
configNodeFullPath) {
- Pattern pattern = Pattern.compile(getDatabasesRootPath() +
"/([\\w\\-]+)/schemas/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(configNodeFullPath);
- return matcher.find() ? Optional.of(matcher.group(2)) :
Optional.empty();
- }
-
- /**
- * Get database name by database path.
- *
- * @param databasePath database path
- * @return database name
- */
- public static Optional<String> getDatabaseNameByDatabasePath(final String
databasePath) {
- Pattern pattern = Pattern.compile(getDatabasesRootPath() +
"/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(databasePath);
- return matcher.find() ? Optional.of(matcher.group(1)) :
Optional.empty();
- }
-
- /**
- * Get schema name.
- *
- * @param schemaPath schema path
- * @return schema name
- */
- public static Optional<String> getSchemaNameBySchemaPath(final String
schemaPath) {
- Pattern pattern = Pattern.compile(getDatabasesRootPath() +
"/([\\w\\-]+)/schemas/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(schemaPath);
+ 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);
+ Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) :
Optional.empty();
}
/**
- * Get table data path.
- *
- * @param tableMetaDataPath table data path
- * @return table name
- */
- public static Optional<String> getTableName(final String
tableMetaDataPath) {
- Pattern pattern = Pattern.compile(getDatabasesRootPath() +
"/([\\w\\-]+)/schemas/([\\w\\-]+)/tables" + "/([\\w\\-]+)$",
Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(tableMetaDataPath);
- return matcher.find() ? Optional.of(matcher.group(3)) :
Optional.empty();
- }
-
- /**
- * Get table name by row path.
+ * Find table name.
*
- * @param rowPath row data path
- * @return table name
+ * @param path path
+ * @param containsChildPath whether contains child path
+ * @return found table name
*/
- public static Optional<String> getTableNameByRowPath(final String rowPath)
{
- Pattern pattern = Pattern.compile(getDatabasesRootPath() +
"/([\\w\\-]+)/schemas/([\\w\\-]+)/tables" + "/([\\w\\-]+)?",
Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(rowPath);
+ 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);
+ Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) :
Optional.empty();
}
/**
- * Get row unique key.
+ * Find row unique key.
*
- * @param rowPath row data path
- * @return row unique key
+ * @param path path
+ * @return found row unique key
*/
- public static Optional<String> getRowUniqueKey(final String rowPath) {
- Pattern pattern = Pattern.compile(getDatabasesRootPath() +
"/([\\w\\-]+)/schemas/([\\w\\-]+)/tables" + "/([\\w\\-]+)" + "/(\\w+)$",
Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(rowPath);
+ 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);
+ Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(4)) :
Optional.empty();
}
diff --git
a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/ShardingSphereDataNodePathTest.java
b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/ShardingSphereDataNodePathTest.java
index cebc1cf9b6f..07019abb407 100644
---
a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/ShardingSphereDataNodePathTest.java
+++
b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/ShardingSphereDataNodePathTest.java
@@ -42,8 +42,8 @@ class ShardingSphereDataNodePathTest {
}
@Test
- void assertGetSchemaDataPath() {
- assertThat(ShardingSphereDataNodePath.getSchemaDataPath("foo_db",
"db_schema"), is("/statistics/databases/foo_db/schemas/db_schema"));
+ void assertGetSchemaPath() {
+ assertThat(ShardingSphereDataNodePath.getSchemaPath("foo_db",
"db_schema"), is("/statistics/databases/foo_db/schemas/db_schema"));
}
@Test
@@ -62,73 +62,48 @@ class ShardingSphereDataNodePathTest {
}
@Test
- void assertFindDatabaseNameHappyPath() {
-
assertThat(ShardingSphereDataNodePath.findDatabaseName("/statistics/databases/foo_db"),
is(Optional.of("foo_db")));
+ void assertFindDatabaseNameWithNotContainsChildPath() {
+
assertThat(ShardingSphereDataNodePath.findDatabaseName("/statistics/databases/foo_db",
false), is(Optional.of("foo_db")));
+
assertThat(ShardingSphereDataNodePath.findDatabaseName("/statistics/databases",
false), is(Optional.empty()));
}
@Test
- void assertFindDatabaseNameDbNameNotFoundScenario() {
-
assertThat(ShardingSphereDataNodePath.findDatabaseName("/statistics/databases"),
is(Optional.empty()));
+ void assertFindDatabaseNameWithContainsChildPath() {
+
assertThat(ShardingSphereDataNodePath.findDatabaseName("/statistics/databases/foo_db",
true), is(Optional.of("foo_db")));
+
assertThat(ShardingSphereDataNodePath.findDatabaseName("/statistics/databases/foo_db/schemas/db_schema",
true), is(Optional.of("foo_db")));
+
assertThat(ShardingSphereDataNodePath.findDatabaseName("/statistics/databases",
true), is(Optional.empty()));
}
@Test
- void assertFindSchemaNameHappyPath() {
-
assertThat(ShardingSphereDataNodePath.findSchemaName("/statistics/databases/foo_db/schemas/db_schema"),
is(Optional.of("db_schema")));
+ void assertFindSchemaNameWithNotContainsChildPath() {
+
assertThat(ShardingSphereDataNodePath.findSchemaName("/statistics/databases/foo_db/schemas/foo_schema",
false), is(Optional.of("foo_schema")));
+
assertThat(ShardingSphereDataNodePath.findSchemaName("/statistics/databases/foo_db",
false), is(Optional.empty()));
}
@Test
- void assertFindSchemaNameSchemaNameNotFoundScenario() {
-
assertThat(ShardingSphereDataNodePath.findSchemaName("/statistics/databases/foo_db"),
is(Optional.empty()));
+ void assertFindSchemaNameWithContainsChildPath() {
+
assertThat(ShardingSphereDataNodePath.findSchemaName("/statistics/databases/foo_db/schemas/foo_schema",
true), is(Optional.of("foo_schema")));
+
assertThat(ShardingSphereDataNodePath.findSchemaName("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl",
true), is(Optional.of("foo_schema")));
+
assertThat(ShardingSphereDataNodePath.findSchemaName("/statistics/databases/foo_db",
true), is(Optional.empty()));
}
@Test
- void assertFindDatabaseNameByDatabasePathHappyPath() {
-
assertThat(ShardingSphereDataNodePath.getDatabaseNameByDatabasePath("/statistics/databases/foo_db"),
is(Optional.of("foo_db")));
+ void assertFindTableNameWithNotContainsChildPath() {
+
assertThat(ShardingSphereDataNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name",
false), is(Optional.of("tbl_name")));
+
assertThat(ShardingSphereDataNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema",
false), is(Optional.empty()));
}
@Test
- void assertFindDatabaseNameByDatabasePathDbNameNotFoundScenario() {
-
assertThat(ShardingSphereDataNodePath.getDatabaseNameByDatabasePath("/statistics/databases"),
is(Optional.empty()));
+ void assertFindTableNameWithContainsChildPath() {
+
assertThat(ShardingSphereDataNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name",
true), is(Optional.of("tbl_name")));
+
assertThat(ShardingSphereDataNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name/key",
true), is(Optional.of("tbl_name")));
+
assertThat(ShardingSphereDataNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema/tables",
true), is(Optional.empty()));
}
@Test
- void assertFindSchemaNameBySchemaPathHappyPath() {
-
assertThat(ShardingSphereDataNodePath.getSchemaNameBySchemaPath("/statistics/databases/foo_db/schemas/db_schema"),
is(Optional.of("db_schema")));
- }
-
- @Test
- void assertFindSchemaNameBySchemaPathSchemaNameNotFoundScenario() {
-
assertThat(ShardingSphereDataNodePath.getSchemaNameBySchemaPath("/statistics//databasesdb_name"),
is(Optional.empty()));
- }
-
- @Test
- void assertGetTableNameHappyPath() {
-
assertThat(ShardingSphereDataNodePath.getTableName("/statistics/databases/foo_db/schemas/db_schema/tables/tbl_name"),
is(Optional.of("tbl_name")));
- }
-
- @Test
- void assertGetTableNameTableNameNotFoundScenario() {
-
assertThat(ShardingSphereDataNodePath.getTableName("/statistics/databases/foo_db/schemas/db_schema"),
is(Optional.empty()));
- }
-
- @Test
- void assertGetTableNameByRowPathHappyPath() {
-
assertThat(ShardingSphereDataNodePath.getTableNameByRowPath("/statistics/databases/foo_db/schemas/db_schema/tables/tbl_name"),
is(Optional.of("tbl_name")));
- }
-
- @Test
- void assertGetTableNameByRowPathTableNameNotFoundScenario() {
-
assertThat(ShardingSphereDataNodePath.getTableNameByRowPath("/statistics/databases/foo_db/schemas/db_schema"),
is(Optional.empty()));
- }
-
- @Test
- void assertGetRowUniqueKeyHappyPath() {
-
assertThat(ShardingSphereDataNodePath.getRowUniqueKey("/statistics/databases/foo_db/schemas/db_schema/tables/tbl_name/key"),
is(Optional.of("key")));
- }
-
- @Test
- void assertGetRowUniqueKeyUniqueKeyNotFoundScenario() {
-
assertThat(ShardingSphereDataNodePath.getRowUniqueKey("/statistics/databases/foo_db/schemas/db_schema/tables/tbl_name"),
is(Optional.empty()));
+ void assertFindRowUniqueKey() {
+
assertThat(ShardingSphereDataNodePath.findRowUniqueKey("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name/key"),
is(Optional.of("key")));
+
assertThat(ShardingSphereDataNodePath.findRowUniqueKey("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name"),
is(Optional.empty()));
}
@Test
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ShardingSphereDataChangedHandler.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ShardingSphereDataChangedHandler.java
index b1eff4454a0..a8202a2db5d 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ShardingSphereDataChangedHandler.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ShardingSphereDataChangedHandler.java
@@ -49,36 +49,36 @@ public final class ShardingSphereDataChangedHandler
implements DataChangedEventH
@Override
public void handle(final ContextManager contextManager, final
DataChangedEvent event) {
ShardingSphereDatabaseDataManager databaseManager =
contextManager.getMetaDataContextManager().getDatabaseManager();
- Optional<String> databaseName =
ShardingSphereDataNodePath.findDatabaseName(event.getKey());
+ Optional<String> databaseName =
ShardingSphereDataNodePath.findDatabaseName(event.getKey(), false);
if (databaseName.isPresent()) {
handleDatabaseChanged(databaseManager, event.getType(),
databaseName.get());
return;
}
- databaseName =
ShardingSphereDataNodePath.getDatabaseNameByDatabasePath(event.getKey());
+ databaseName =
ShardingSphereDataNodePath.findDatabaseName(event.getKey(), true);
if (!databaseName.isPresent()) {
return;
}
- Optional<String> schemaName =
ShardingSphereDataNodePath.findSchemaName(event.getKey());
+ Optional<String> schemaName =
ShardingSphereDataNodePath.findSchemaName(event.getKey(), false);
if (schemaName.isPresent()) {
handleSchemaChanged(databaseManager, event.getType(),
databaseName.get(), schemaName.get());
return;
}
- schemaName =
ShardingSphereDataNodePath.getSchemaNameBySchemaPath(event.getKey());
+ schemaName = ShardingSphereDataNodePath.findSchemaName(event.getKey(),
true);
if (!schemaName.isPresent()) {
return;
}
- Optional<String> tableName =
ShardingSphereDataNodePath.getTableName(event.getKey());
+ Optional<String> tableName =
ShardingSphereDataNodePath.findTableName(event.getKey(), false);
if (tableName.isPresent()) {
handleTableChanged(databaseManager, event.getType(),
databaseName.get(), schemaName.get(), tableName.get());
return;
}
- tableName =
ShardingSphereDataNodePath.getTableNameByRowPath(event.getKey());
+ tableName = ShardingSphereDataNodePath.findTableName(event.getKey(),
true);
if (!tableName.isPresent()) {
return;
}
- Optional<String> rowPath =
ShardingSphereDataNodePath.getRowUniqueKey(event.getKey());
- if (rowPath.isPresent()) {
- handleRowDataChanged(databaseManager, event.getType(),
event.getValue(), databaseName.get(), schemaName.get(), tableName.get(),
rowPath.get());
+ Optional<String> uniqueKey =
ShardingSphereDataNodePath.findRowUniqueKey(event.getKey());
+ if (uniqueKey.isPresent()) {
+ handleRowDataChanged(databaseManager, event.getType(),
event.getValue(), databaseName.get(), schemaName.get(), tableName.get(),
uniqueKey.get());
}
}
@@ -122,11 +122,11 @@ public final class ShardingSphereDataChangedHandler
implements DataChangedEventH
}
private void handleRowDataChanged(final ShardingSphereDatabaseDataManager
databaseManager, final Type type, final String eventValue,
- final String databaseName, final String
schemaName, final String tableName, final String rowPath) {
+ final String databaseName, final String
schemaName, final String tableName, final String uniqueKey) {
if ((Type.ADDED == type || Type.UPDATED == type) &&
!Strings.isNullOrEmpty(eventValue)) {
databaseManager.alterShardingSphereRowData(databaseName,
schemaName, tableName, YamlEngine.unmarshal(eventValue,
YamlShardingSphereRowData.class));
} else if (Type.DELETED == type) {
- databaseManager.deleteShardingSphereRowData(databaseName,
schemaName, tableName, rowPath);
+ databaseManager.deleteShardingSphereRowData(databaseName,
schemaName, tableName, uniqueKey);
}
}
}