This is an automated email from the ASF dual-hosted git repository. zhangliang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push: new 16edb280e37 Refactor ViewMetaDataNodePath (#34566) 16edb280e37 is described below commit 16edb280e37395454fb3eaa99b9cf602b13b4144 Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Thu Feb 6 01:30:47 2025 +0800 Refactor ViewMetaDataNodePath (#34566) --- .../node/path/metadata/ViewMetaDataNodePath.java | 40 +++++++++------------- .../path/metadata/ViewMetaDataNodePathTest.java | 34 +++++++++--------- .../database/metadata/type/ViewChangedHandler.java | 2 +- 3 files changed, 35 insertions(+), 41 deletions(-) diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/ViewMetaDataNodePath.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/ViewMetaDataNodePath.java index 8ff188d8f24..91d467756a8 100644 --- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/ViewMetaDataNodePath.java +++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/ViewMetaDataNodePath.java @@ -30,19 +30,13 @@ import java.util.regex.Pattern; @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class ViewMetaDataNodePath { - private static final String ROOT_NODE = "/metadata"; - private static final String VIEWS_NODE = "views"; private static final String VERSIONS_NODE = "versions"; private static final String ACTIVE_VERSION_NODE = "active_version"; - private static final String VIEWS_PATTERN = "/([\\w\\-]+)/schemas/([\\w\\-]+)/views"; - - private static final String ACTIVE_VERSION_SUFFIX = "/([\\w\\-]+)/active_version"; - - private static final String VIEW_SUFFIX = "/([\\w\\-]+)$"; + private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)"; /** * Get view root path. @@ -105,46 +99,46 @@ public final class ViewMetaDataNodePath { } /** - * Get view name by active version path. + * Get view name. * * @param path path * @return view name */ - public static Optional<String> getViewNameByActiveVersionPath(final String path) { - Pattern pattern = Pattern.compile(ROOT_NODE + VIEWS_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE); + public static Optional<String> findViewName(final String path) { + Pattern pattern = Pattern.compile(getViewPath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + "$", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(path); return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty(); } /** - * Get view name. + * Is view path. * * @param path path - * @return view name + * @return true or false */ - public static Optional<String> findViewName(final String path) { - Pattern pattern = Pattern.compile(ROOT_NODE + VIEWS_PATTERN + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(path); - return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty(); + public static boolean isViewPath(final String path) { + return findViewName(path).isPresent(); } /** - * Is view active version path. + * Find view name by active version path. * * @param path path - * @return true or false + * @return view name */ - public static boolean isViewActiveVersionPath(final String path) { - return Pattern.compile(ROOT_NODE + VIEWS_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); + public static Optional<String> findViewNameByActiveVersionPath(final String path) { + Pattern pattern = Pattern.compile(getViewActiveVersionPath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN, IDENTIFIER_PATTERN), Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(path); + return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty(); } /** - * Is view path. + * Is view active version path. * * @param path path * @return true or false */ - public static boolean isViewPath(final String path) { - return Pattern.compile(ROOT_NODE + VIEWS_PATTERN + VIEW_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find(); + public static boolean isViewActiveVersionPath(final String path) { + return findViewNameByActiveVersionPath(path).isPresent(); } } diff --git a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/metadata/ViewMetaDataNodePathTest.java b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/metadata/ViewMetaDataNodePathTest.java index 0d645bdd97b..de1e92ce0b1 100644 --- a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/metadata/ViewMetaDataNodePathTest.java +++ b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/metadata/ViewMetaDataNodePathTest.java @@ -33,6 +33,11 @@ class ViewMetaDataNodePathTest { assertThat(ViewMetaDataNodePath.getViewRootPath("foo_db", "foo_schema"), is("/metadata/foo_db/schemas/foo_schema/views")); } + @Test + void assertGetViewPath() { + assertThat(ViewMetaDataNodePath.getViewPath("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view")); + } + @Test void assertGetViewActiveVersionPath() { assertThat(ViewMetaDataNodePath.getViewActiveVersionPath("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version")); @@ -49,41 +54,36 @@ class ViewMetaDataNodePathTest { } @Test - void assertGetViewPath() { - assertThat(ViewMetaDataNodePath.getViewPath("foo_db", "foo_schema", "foo_view"), is("/metadata/foo_db/schemas/foo_schema/views/foo_view")); + void assertFindViewName() { + Optional<String> actual = ViewMetaDataNodePath.findViewName("/metadata/foo_db/schemas/foo_schema/views/foo_view"); + assertTrue(actual.isPresent()); + assertThat(actual.get(), is("foo_view")); } @Test - void assertGetTableNameByActiveVersionNode() { - Optional<String> actual = ViewMetaDataNodePath.getViewNameByActiveVersionPath("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version"); - assertTrue(actual.isPresent()); - assertThat(actual.get(), is("foo_view")); + void assertFindViewNameIfNotFound() { + assertFalse(ViewMetaDataNodePath.findViewName("/xxx/foo_db/schemas/foo_schema/views/foo_view").isPresent()); } @Test - void assertGetTableNameByActiveVersionNodeIfNotFound() { - assertFalse(ViewMetaDataNodePath.getViewNameByActiveVersionPath("/xxx/foo_db/schemas/foo_schema/views/foo_view/active_version").isPresent()); + void assertIsViewPath() { + assertTrue(ViewMetaDataNodePath.isViewPath("/metadata/foo_db/schemas/foo_schema/views/foo_view")); } @Test - void assertFindViewName() { - Optional<String> actual = ViewMetaDataNodePath.findViewName("/metadata/foo_db/schemas/foo_schema/views/foo_view"); + 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 assertFindViewNameIfNotFound() { - assertFalse(ViewMetaDataNodePath.findViewName("/xxx/foo_db/schemas/foo_schema/views/foo_view").isPresent()); + 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")); } - - @Test - void assertIsViewPath() { - assertTrue(ViewMetaDataNodePath.isViewPath("/metadata/foo_db/schemas/foo_schema/views/foo_view")); - } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/type/ViewChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/type/ViewChangedHandler.java index 7e74f5be4b2..034676bc49d 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/type/ViewChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/type/ViewChangedHandler.java @@ -46,7 +46,7 @@ public final class ViewChangedHandler { * @param event data changed event */ public void handleCreatedOrAltered(final String databaseName, final String schemaName, final DataChangedEvent event) { - String viewName = ViewMetaDataNodePath.getViewNameByActiveVersionPath(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); + String viewName = ViewMetaDataNodePath.findViewNameByActiveVersionPath(event.getKey()).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);