Skip to content

Commit

Permalink
Add VersionNodePath.findIdentifierByActiveVersionPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Feb 7, 2025
1 parent 6b26cfb commit 1d5561c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,12 @@ public static boolean isViewPath(final String path) {
}

/**
* Find view name by active version path.
* Get view version pattern node path.
*
* @param path path
* @return view name
* @return view version node path
*/
public static Optional<String> findViewNameByActiveVersionPath(final String path) {
Pattern pattern = Pattern.compile(
getVersionNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER).getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
public static VersionNodePath getVersionPatternNodePath() {
return new VersionNodePath(getViewPath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER));
}

/**
Expand All @@ -111,6 +107,6 @@ public static Optional<String> findViewNameByActiveVersionPath(final String path
* @return true or false
*/
public static boolean isViewActiveVersionPath(final String path) {
return findViewNameByActiveVersionPath(path).isPresent();
return ViewMetaDataNodePath.getVersionPatternNodePath().findIdentifierByActiveVersionPath(path, 3).isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -75,4 +77,17 @@ public boolean isVersionPath(final String path) {
Pattern pattern = Pattern.compile(String.join("/", getVersionsPath(), VERSION_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
return pattern.matcher(path).find();
}

/**
* Find identifier name by active version path.
*
* @param activeVersionPath active version path
* @param identifierGroupIndex identifier group index
* @return found identifier
*/
public Optional<String> findIdentifierByActiveVersionPath(final String activeVersionPath, final int identifierGroupIndex) {
Pattern pattern = Pattern.compile(getActiveVersionPath(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(activeVersionPath);
return matcher.find() ? Optional.of(matcher.group(identifierGroupIndex)) : Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ void assertIsViewPath() {
assertTrue(ViewMetaDataNodePath.isViewPath("/metadata/foo_db/schemas/foo_schema/views/foo_view"));
}

@Test
void assertGetTableNameByActiveVersionNode() {
Optional<String> actual = ViewMetaDataNodePath.findViewNameByActiveVersionPath("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version");
assertTrue(actual.isPresent());
assertThat(actual.get(), is("foo_view"));
}

@Test
void assertGetTableNameByActiveVersionNodeIfNotFound() {
assertFalse(ViewMetaDataNodePath.findViewNameByActiveVersionPath("/xxx/foo_db/schemas/foo_schema/views/foo_view/active_version").isPresent());
}

@Test
void assertIsViewActiveVersionPath() {
assertTrue(ViewMetaDataNodePath.isViewActiveVersionPath("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -49,4 +51,20 @@ void assertIsVersionPath() {
assertFalse(new VersionNodePath(IDENTIFIER_PATTERN).isVersionPath("foo/versions"));
assertFalse(new VersionNodePath(IDENTIFIER_PATTERN).isVersionPath("foo/versions/0/xxx"));
}

@Test
void assertFindIdentifierByActiveVersionPath() {
String path = "/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version";
VersionNodePath versionNodePath = new VersionNodePath("/metadata/([\\w\\-]+)/schemas/([\\w\\-]+)/tables/([\\w\\-]+)");
assertThat(versionNodePath.findIdentifierByActiveVersionPath(path, 1), is(Optional.of("foo_db")));
assertThat(versionNodePath.findIdentifierByActiveVersionPath(path, 2), is(Optional.of("foo_schema")));
assertThat(versionNodePath.findIdentifierByActiveVersionPath(path, 3), is(Optional.of("foo_tbl")));
}

@Test
void assertNotFindIdentifierByActiveVersionPath() {
String path = "/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/versions";
VersionNodePath versionNodePath = new VersionNodePath("/metadata/([\\w\\-]+)/schemas/([\\w\\-]+)/tables/([\\w\\-]+)");
assertFalse(versionNodePath.findIdentifierByActiveVersionPath(path, 1).isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ViewChangedHandler(final ContextManager contextManager) {
* @param event data changed event
*/
public void handleCreatedOrAltered(final String databaseName, final String schemaName, final DataChangedEvent event) {
String viewName = ViewMetaDataNodePath.findViewNameByActiveVersionPath(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found."));
String viewName = ViewMetaDataNodePath.getVersionPatternNodePath().findIdentifierByActiveVersionPath(event.getKey(), 3).orElseThrow(() -> new IllegalStateException("View name not found."));
ActiveVersionChecker.checkActiveVersion(contextManager, event);
ShardingSphereView view = contextManager.getPersistServiceFacade().getMetaDataPersistFacade().getDatabaseMetaDataFacade().getView().load(databaseName, schemaName, viewName);
contextManager.getMetaDataContextManager().getDatabaseMetaDataManager().alterView(databaseName, schemaName, view);
Expand Down

0 comments on commit 1d5561c

Please sign in to comment.