Skip to content

Commit

Permalink
Refactor DatabaseMetaDataNodePath
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 5, 2025
1 parent ee15eee commit 8d63200
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public final class DatabaseMetaDataNodePath {

private static final String VERSIONS = "versions";

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

/**
* Get meta data root path.
*
Expand Down Expand Up @@ -92,13 +94,13 @@ public static String getTablesPath(final String databaseName, final String schem
}

/**
* Get version node by active version path.
* Get version path.
*
* @param rulePath rule path
* @param activeVersion active version
* @return active version path
* @return version path
*/
public static String getVersionNodeByActiveVersionPath(final String rulePath, final String activeVersion) {
public static String getVersionPath(final String rulePath, final String activeVersion) {
return rulePath.replace(ACTIVE_VERSION, VERSIONS) + "/" + activeVersion;
}

Expand All @@ -110,7 +112,8 @@ public static String getVersionNodeByActiveVersionPath(final String rulePath, fi
* @return found database name
*/
public static Optional<String> findDatabaseName(final String path, final boolean containsChildPath) {
Pattern pattern = Pattern.compile(getRootPath() + "/([\\w\\-]+)" + (containsChildPath ? "?" : "$"), Pattern.CASE_INSENSITIVE);
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();
}
Expand All @@ -123,7 +126,8 @@ public static Optional<String> findDatabaseName(final String path, final boolean
* @return found schema name
*/
public static Optional<String> findSchemaName(final String path, final boolean containsChildPath) {
Pattern pattern = Pattern.compile(getRootPath() + "/([\\w\\-]+)/schemas/([\\w\\-]+)" + (containsChildPath ? "?" : "$"), Pattern.CASE_INSENSITIVE);
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String getActiveVersionByFullPath(final String fullPath) {

@Override
public String getVersionPathByActiveVersion(final String path, final String activeVersion) {
return repository.query(DatabaseMetaDataNodePath.getVersionNodeByActiveVersionPath(path, activeVersion));
return repository.query(DatabaseMetaDataNodePath.getVersionPath(path, activeVersion));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ void assertGetTablesPath() {
}

@Test
void assertGetVersionNodeByActiveVersionPath() {
assertThat(DatabaseMetaDataNodePath.getVersionNodeByActiveVersionPath("foo_rule", "1"), is("foo_rule/1"));
void assertGetVersionPath() {
assertThat(DatabaseMetaDataNodePath.getVersionPath("foo_rule", "1"), is("foo_rule/1"));
}

@Test
Expand Down

0 comments on commit 8d63200

Please sign in to comment.