Skip to content

Commit

Permalink
Refactor DatabaseMetaDataNode
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 5, 2025
1 parent ae12535 commit 1823fce
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,53 +83,31 @@ public static String getMetaDataTablesPath(final String databaseName, final Stri
}

/**
* Get database name.
* Find database name.
*
* @param path path
* @return database name
* @param containsChildPath whether contains child path
* @return found database name
*/
public static Optional<String> getDatabaseName(final String path) {
Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
public static Optional<String> findDatabaseName(final String path, final boolean containsChildPath) {
Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)" + (containsChildPath ? "?" : "$"), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}

/**
* Get database name by schema path.
*
* @param schemaPath database path
* @return database name
*/
public static Optional<String> getDatabaseNameBySchemaNode(final String schemaPath) {
Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(schemaPath);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}

/**
* Get schema name.
* Find schema name.
*
* @param path path
* @return schema name
* @param containsChildPath whether contains child path
* @return found schema name
*/
public static Optional<String> getSchemaName(final String path) {
Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/schemas/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
public static Optional<String> findSchemaName(final String path, final boolean containsChildPath) {
Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/schemas/([\\w\\-]+)" + (containsChildPath ? "?" : "$"), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}

/**
* Get schema name by table path.
*
* @param tablePath table path
* @return schema name
*/
public static Optional<String> getSchemaNameByTableNode(final String tablePath) {
Pattern pattern = Pattern.compile(getMetaDataNode() + "/([\\w\\-]+)/schemas/([\\w\\-]+)?", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(tablePath);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}

/**
* Get version node by active version path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,55 @@ void assertGetMetaDataTablesPath() {
}

@Test
void assertGetDatabaseName() {
Optional<String> actual = DatabaseMetaDataNode.getDatabaseName("/metadata/foo_db");
void assertFindDatabaseNameWithNotContainsChildPath() {
Optional<String> actual = DatabaseMetaDataNode.findDatabaseName("/metadata/foo_db", false);
assertTrue(actual.isPresent());
assertThat(actual.get(), is("foo_db"));
}

@Test
void assertGetDatabaseNameIfNotFound() {
assertFalse(DatabaseMetaDataNode.getDatabaseName("/metadata").isPresent());
void assertNotFindDatabaseNameWithNotContainsChildPath() {
Optional<String> actual = DatabaseMetaDataNode.findDatabaseName("/metadata/foo_db/schemas/foo_schema", false);
assertFalse(actual.isPresent());
}

@Test
void assertGetDatabaseNameBySchemaNode() {
Optional<String> actual = DatabaseMetaDataNode.getDatabaseNameBySchemaNode("/metadata/foo_db/schemas/foo_schema");
void assertFindDatabaseNameWithContainsChildPath() {
Optional<String> actual = DatabaseMetaDataNode.findDatabaseName("/metadata/foo_db/schemas/foo_schema", true);
assertTrue(actual.isPresent());
assertThat(actual.get(), is("foo_db"));
}

@Test
void assertGetDatabaseNameBySchemaNodeIfNotFound() {
assertFalse(DatabaseMetaDataNode.getDatabaseNameBySchemaNode("/xxx/foo_db").isPresent());
void assertNotFindDatabaseNameWithContainsChildPath() {
Optional<String> actual = DatabaseMetaDataNode.findDatabaseName("/xxx/foo_db/schemas/foo_schema", true);
assertFalse(actual.isPresent());
}

@Test
void assertGetSchemaName() {
Optional<String> actual = DatabaseMetaDataNode.getSchemaName("/metadata/foo_db/schemas/foo_schema");
void assertFindSchemaNameWithNotContainsChildPath() {
Optional<String> actual = DatabaseMetaDataNode.findSchemaName("/metadata/foo_db/schemas/foo_schema", false);
assertTrue(actual.isPresent());
assertThat(actual.get(), is("foo_schema"));
}

@Test
void assertGetSchemaNameIfNotFound() {
assertFalse(DatabaseMetaDataNode.getSchemaName("/metadata/foo_db/xxx/foo_schema").isPresent());
void assertNotFindSchemaNameWithNotContainsChildPath() {
Optional<String> actual = DatabaseMetaDataNode.findSchemaName("/metadata/foo_db/schemas/foo_schema/tables", false);
assertFalse(actual.isPresent());
}

@Test
void assertGetSchemaNameByTableNode() {
Optional<String> actual = DatabaseMetaDataNode.getSchemaNameByTableNode("/metadata/foo_db/schemas/foo_schema/tables");
void assertFindSchemaNameWithContainsChildPath() {
Optional<String> actual = DatabaseMetaDataNode.findSchemaName("/metadata/foo_db/schemas/foo_schema/tables", true);
assertTrue(actual.isPresent());
assertThat(actual.get(), is("foo_schema"));
}

@Test
void assertGetSchemaNameByTableNodeIfNotFound() {
assertFalse(DatabaseMetaDataNode.getSchemaNameByTableNode("/xxx/foo_db/schemas/foo_schema/tables").isPresent());
void assertNotFindSchemaNameWithContainsChildPath() {
Optional<String> actual = DatabaseMetaDataNode.findSchemaName("/xxx/foo_db/schemas/foo_schema/tables", true);
assertFalse(actual.isPresent());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public MetaDataChangedHandler(final ContextManager contextManager) {
*/
public boolean handle(final String databaseName, final DataChangedEvent event) {
String eventKey = event.getKey();
Optional<String> schemaName = DatabaseMetaDataNode.getSchemaName(eventKey);
Optional<String> schemaName = DatabaseMetaDataNode.findSchemaName(eventKey, false);
if (schemaName.isPresent()) {
handleSchemaChanged(databaseName, schemaName.get(), event);
return true;
}
schemaName = DatabaseMetaDataNode.getSchemaNameByTableNode(eventKey);
schemaName = DatabaseMetaDataNode.findSchemaName(eventKey, true);
if (schemaName.isPresent() && isTableMetaDataChanged(eventKey)) {
handleTableChanged(databaseName, schemaName.get(), event);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class DatabaseMetaDataChangedListener implements DataChangedEventLi

@Override
public void onChange(final DataChangedEvent event) {
Optional<String> databaseName = DatabaseMetaDataNode.getDatabaseNameBySchemaNode(event.getKey());
Optional<String> databaseName = DatabaseMetaDataNode.findDatabaseName(event.getKey(), true);
if (!databaseName.isPresent()) {
return;
}
Expand Down

0 comments on commit 1823fce

Please sign in to comment.