Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ShardingSphereDataNodePath #34275

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}

Expand All @@ -88,115 +92,85 @@ 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);
}

/**
* Get table path.
*
* @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);
}

/**
* Get table row path.
*
* @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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,36 @@ public Collection<Type> getSubscribedTypes() {
@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());
}
}

Expand Down Expand Up @@ -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);
}
}
}
Loading