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 424de8e1f82 Refactor DatabaseListenerCoordinatorNodePath (#34754)
424de8e1f82 is described below
commit 424de8e1f82eaa7fc6e7fa187fa38fafcfc6a06c
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Feb 23 00:59:44 2025 +0800
Refactor DatabaseListenerCoordinatorNodePath (#34754)
* Refactor DatabaseListenerCoordinatorNodePath
* Refactor DatabaseListenerCoordinatorNodePath
---
.../metadata/database/TableMetadataNodePath.java | 6 +--
.../metadata/database/ViewMetadataNodePath.java | 2 +-
.../state/DatabaseListenerCoordinatorNodePath.java | 11 ++++++
.../DatabaseListenerCoordinatorNodePathParser.java | 46 ----------------------
...abaseListenerCoordinatorNodePathParserTest.java | 35 ----------------
.../DatabaseListenerCoordinatorNodePathTest.java | 10 +++++
.../type/DatabaseListenerChangedHandler.java | 10 ++---
7 files changed, 30 insertions(+), 90 deletions(-)
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/database/TableMetadataNodePath.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/database/TableMetadataNodePath.java
index 5a2b88faf47..94f91850a49 100644
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/database/TableMetadataNodePath.java
+++
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/database/TableMetadataNodePath.java
@@ -45,7 +45,7 @@ public final class TableMetadataNodePath implements NodePath {
/**
* Create database search criteria.
*
- * @return created database search criteria
+ * @return created search criteria
*/
public static NodePathSearchCriteria createDatabaseSearchCriteria() {
return new NodePathSearchCriteria(new
TableMetadataNodePath(NodePathPattern.IDENTIFIER, null, null), true, true, 1);
@@ -55,7 +55,7 @@ public final class TableMetadataNodePath implements NodePath {
* Create schema search criteria.
*
* @param containsChildPath contains child path
- * @return created schema search criteria
+ * @return created search criteria
*/
public static NodePathSearchCriteria createSchemaSearchCriteria(final
boolean containsChildPath) {
return new NodePathSearchCriteria(new
TableMetadataNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER,
null), true, containsChildPath, 2);
@@ -64,7 +64,7 @@ public final class TableMetadataNodePath implements NodePath {
/**
* Create table search criteria.
*
- * @return created table search criteria
+ * @return created search criteria
*/
public static NodePathSearchCriteria createTableSearchCriteria() {
return new NodePathSearchCriteria(new TableMetadataNodePath(), false,
false, 3);
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/database/ViewMetadataNodePath.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/database/ViewMetadataNodePath.java
index 273cedbe5ab..260d5d9f02c 100644
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/database/ViewMetadataNodePath.java
+++
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/metadata/database/ViewMetadataNodePath.java
@@ -45,7 +45,7 @@ public final class ViewMetadataNodePath implements NodePath {
/**
* Create view search criteria.
*
- * @return created view search criteria
+ * @return created search criteria
*/
public static NodePathSearchCriteria createViewSearchCriteria() {
return new NodePathSearchCriteria(new ViewMetadataNodePath(), false,
false, 3);
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePath.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePath.java
index 46bc7b664a5..97b5e328c84 100644
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePath.java
+++
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePath.java
@@ -21,6 +21,8 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
+import org.apache.shardingsphere.mode.node.path.NodePathPattern;
+import org.apache.shardingsphere.mode.node.path.NodePathSearchCriteria;
/**
* Database listener coordinator node path.
@@ -31,4 +33,13 @@ import
org.apache.shardingsphere.mode.node.path.NodePathEntity;
public final class DatabaseListenerCoordinatorNodePath implements NodePath {
private final String databaseName;
+
+ /**
+ * Create database search criteria.
+ *
+ * @return created search criteria
+ */
+ public static NodePathSearchCriteria createDatabaseSearchCriteria() {
+ return new NodePathSearchCriteria(new
DatabaseListenerCoordinatorNodePath(NodePathPattern.IDENTIFIER), false, false,
1);
+ }
}
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathParser.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathParser.java
deleted file mode 100644
index f2c03c63d03..00000000000
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathParser.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mode.node.path.state;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
-import org.apache.shardingsphere.mode.node.path.NodePathPattern;
-
-import java.util.Optional;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * Database listener coordinator node path parser.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class DatabaseListenerCoordinatorNodePathParser {
-
- /**
- * Find database name by database listener coordinator node path.
- *
- * @param databaseListenerCoordinatorNodePath database listener
coordinator node path
- * @return found database name
- */
- public static Optional<String> findDatabaseName(final String
databaseListenerCoordinatorNodePath) {
- Pattern pattern = Pattern.compile(NodePathGenerator.toPath(new
DatabaseListenerCoordinatorNodePath(NodePathPattern.IDENTIFIER), false) + "$",
Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(databaseListenerCoordinatorNodePath);
- return matcher.find() ? Optional.of(matcher.group(1)) :
Optional.empty();
- }
-}
diff --git
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathParserTest.java
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathParserTest.java
deleted file mode 100644
index 9052627634c..00000000000
---
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathParserTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mode.node.path.state;
-
-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;
-
-class DatabaseListenerCoordinatorNodePathParserTest {
-
- @Test
- void assertFindDatabaseName() {
-
assertThat(DatabaseListenerCoordinatorNodePathParser.findDatabaseName("/states/database_listener_coordinator/foo_db"),
is(Optional.of("foo_db")));
-
assertFalse(DatabaseListenerCoordinatorNodePathParser.findDatabaseName("/states/database_listener_coordinator").isPresent());
- }
-}
diff --git
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathTest.java
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathTest.java
index 6442c48e3b0..064f7c5e63e 100644
---
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathTest.java
+++
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/state/DatabaseListenerCoordinatorNodePathTest.java
@@ -18,10 +18,14 @@
package org.apache.shardingsphere.mode.node.path.state;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.NodePathSearcher;
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;
class DatabaseListenerCoordinatorNodePathTest {
@@ -30,4 +34,10 @@ class DatabaseListenerCoordinatorNodePathTest {
assertThat(NodePathGenerator.toPath(new
DatabaseListenerCoordinatorNodePath(null), false),
is("/states/database_listener_coordinator"));
assertThat(NodePathGenerator.toPath(new
DatabaseListenerCoordinatorNodePath("foo_db"), false),
is("/states/database_listener_coordinator/foo_db"));
}
+
+ @Test
+ void assertCreateDatabaseSearchCriteria() {
+
assertThat(NodePathSearcher.find("/states/database_listener_coordinator/foo_db",
DatabaseListenerCoordinatorNodePath.createDatabaseSearchCriteria()),
is(Optional.of("foo_db")));
+
assertFalse(NodePathSearcher.find("/states/database_listener_coordinator",
DatabaseListenerCoordinatorNodePath.createDatabaseSearchCriteria()).isPresent());
+ }
}
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/DatabaseListenerChangedHandler.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/DatabaseListenerChangedHandler.java
index 6b4a21721b9..15bba5a4380 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/DatabaseListenerChangedHandler.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/DatabaseListenerChangedHandler.java
@@ -27,9 +27,9 @@ import
org.apache.shardingsphere.mode.manager.cluster.persist.coordinator.databa
import
org.apache.shardingsphere.mode.manager.cluster.persist.coordinator.database.ClusterDatabaseListenerPersistCoordinator;
import
org.apache.shardingsphere.mode.metadata.refresher.statistics.StatisticsRefreshEngine;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.NodePathSearcher;
import
org.apache.shardingsphere.mode.node.path.metadata.database.TableMetadataNodePath;
import
org.apache.shardingsphere.mode.node.path.state.DatabaseListenerCoordinatorNodePath;
-import
org.apache.shardingsphere.mode.node.path.state.DatabaseListenerCoordinatorNodePathParser;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import java.util.Arrays;
@@ -52,16 +52,16 @@ public final class DatabaseListenerChangedHandler
implements GlobalDataChangedEv
@Override
public void handle(final ContextManager contextManager, final
DataChangedEvent event) {
-
DatabaseListenerCoordinatorNodePathParser.findDatabaseName(event.getKey())
+ NodePathSearcher.find(event.getKey(),
DatabaseListenerCoordinatorNodePath.createDatabaseSearchCriteria())
.ifPresent(optional -> handle(contextManager, optional,
ClusterDatabaseListenerCoordinatorType.valueOf(event.getValue())));
}
- private static void handle(final ContextManager contextManager, final
String databaseName, final ClusterDatabaseListenerCoordinatorType
clusterDatabaseListenerCoordinatorType) {
+ private void handle(final ContextManager contextManager, final String
databaseName, final ClusterDatabaseListenerCoordinatorType type) {
ClusterPersistRepository repository = (ClusterPersistRepository)
contextManager.getPersistServiceFacade().getRepository();
- if (ClusterDatabaseListenerCoordinatorType.CREATE ==
clusterDatabaseListenerCoordinatorType) {
+ if (ClusterDatabaseListenerCoordinatorType.CREATE == type) {
repository.watch(NodePathGenerator.toPath(new
TableMetadataNodePath(databaseName, null, null), true), new
DatabaseMetaDataChangedListener(contextManager));
contextManager.getMetaDataContextManager().getDatabaseMetaDataManager().addDatabase(databaseName);
- } else if (ClusterDatabaseListenerCoordinatorType.DROP ==
clusterDatabaseListenerCoordinatorType) {
+ } else if (ClusterDatabaseListenerCoordinatorType.DROP == type) {
repository.removeDataListener(NodePathGenerator.toPath(new
TableMetadataNodePath(databaseName, null, null), true));
contextManager.getMetaDataContextManager().getDatabaseMetaDataManager().dropDatabase(databaseName);
}