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 ecdd0f50cb70a..0966cbe620915 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 void persist(final ShardingSphereDatabase database, final String schemaNa } 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 b3105b2c2d71b..b1b4762c8d0c5 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 static String getSchemaRootPath(final String databaseName) { * @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 static String getSchemaDataPath(final String databaseName, final String s * @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 static String getTableRootPath(final String databaseName, final String sc * * @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 static String getTablePath(final String databaseName, final String schema * * @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 findDatabaseName(final String configNodeFullPath) { - Pattern pattern = Pattern.compile(getDatabasesRootPath() + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(configNodeFullPath); + public static Optional 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 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 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 getSchemaNameBySchemaPath(final String schemaPath) { - Pattern pattern = Pattern.compile(getDatabasesRootPath() + "/([\\w\\-]+)/schemas/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(schemaPath); + public static Optional 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 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 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 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 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 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 cebc1cf9b6fd8..07019abb407f2 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 @@ void assertGetSchemaRootPath() { } @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 @@ void assertGetTableRowPath() { } @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 b1eff4454a033..a8202a2db5de0 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 Collection getSubscribedTypes() { @Override public void handle(final ContextManager contextManager, final DataChangedEvent event) { ShardingSphereDatabaseDataManager databaseManager = contextManager.getMetaDataContextManager().getDatabaseManager(); - Optional databaseName = ShardingSphereDataNodePath.findDatabaseName(event.getKey()); + Optional 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 schemaName = ShardingSphereDataNodePath.findSchemaName(event.getKey()); + Optional 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 tableName = ShardingSphereDataNodePath.getTableName(event.getKey()); + Optional 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 rowPath = ShardingSphereDataNodePath.getRowUniqueKey(event.getKey()); - if (rowPath.isPresent()) { - handleRowDataChanged(databaseManager, event.getType(), event.getValue(), databaseName.get(), schemaName.get(), tableName.get(), rowPath.get()); + Optional 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 @@ private void handleTableChanged(final ShardingSphereDatabaseDataManager database } 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); } } }