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 b13244b5cf3 Refactor QualifiedDataSourceNodePath (#34756)
b13244b5cf3 is described below
commit b13244b5cf3149a1446785716c95623adb927341
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Feb 23 13:31:14 2025 +0800
Refactor QualifiedDataSourceNodePath (#34756)
---
.../QualifiedDataSourceStatePersistService.java | 2 +-
.../node/storage/QualifiedDataSourceNodePath.java | 16 ++++++-
.../storage/QualifiedDataSourceNodePathParser.java | 49 ----------------------
.../QualifiedDataSourceNodePathParserTest.java | 46 --------------------
.../storage/QualifiedDataSourceNodePathTest.java | 12 ++++++
.../type/QualifiedDataSourceChangedHandler.java | 6 +--
6 files changed, 31 insertions(+), 100 deletions(-)
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/state/node/QualifiedDataSourceStatePersistService.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/state/node/QualifiedDataSourceStatePersistService.java
index 73715a31ce8..d365027de8b 100644
---
a/mode/core/src/main/java/org/apache/shardingsphere/mode/state/node/QualifiedDataSourceStatePersistService.java
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/state/node/QualifiedDataSourceStatePersistService.java
@@ -47,7 +47,7 @@ public final class QualifiedDataSourceStatePersistService {
* @return loaded qualified data source states
*/
public Map<String, QualifiedDataSourceState> load() {
- Collection<String> qualifiedDataSourceNodes =
repository.getChildrenKeys(NodePathGenerator.toPath(new
QualifiedDataSourceNodePath(null), false));
+ Collection<String> qualifiedDataSourceNodes =
repository.getChildrenKeys(NodePathGenerator.toPath(new
QualifiedDataSourceNodePath((String) null), false));
Map<String, QualifiedDataSourceState> result = new
HashMap<>(qualifiedDataSourceNodes.size(), 1F);
qualifiedDataSourceNodes.forEach(each -> {
String yamlContent = repository.query(NodePathGenerator.toPath(new
QualifiedDataSourceNodePath(new QualifiedDataSource(each)), false));
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePath.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePath.java
index 5be2d739204..e7eb7fa6ddd 100644
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePath.java
+++
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePath.java
@@ -22,6 +22,7 @@ import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
+import org.apache.shardingsphere.mode.node.path.NodePathSearchCriteria;
/**
* Qualified data source node path.
@@ -31,5 +32,18 @@ import
org.apache.shardingsphere.mode.node.path.NodePathEntity;
@Getter
public final class QualifiedDataSourceNodePath implements NodePath {
- private final QualifiedDataSource qualifiedDataSource;
+ private final String qualifiedDataSource;
+
+ public QualifiedDataSourceNodePath(final QualifiedDataSource
qualifiedDataSource) {
+ this(qualifiedDataSource.toString());
+ }
+
+ /**
+ * Create qualified data source search criteria.
+ *
+ * @return created search criteria
+ */
+ public static NodePathSearchCriteria
createQualifiedDataSourceSearchCriteria() {
+ return new NodePathSearchCriteria(new
QualifiedDataSourceNodePath("(\\S+)"), false, false, 1);
+ }
}
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathParser.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathParser.java
deleted file mode 100644
index 1698e7ab0d2..00000000000
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathParser.java
+++ /dev/null
@@ -1,49 +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.node.storage;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import
org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource;
-import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
-
-import java.util.Optional;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * Qualified data source node path parser.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class QualifiedDataSourceNodePathParser {
-
- private static final String QUALIFIED_DATA_SOURCE_PATTERN = "(\\S+)";
-
- /**
- * Find qualified data source.
- *
- * @param qualifiedDataSourcePath qualified data source path
- * @return found qualified data source
- */
- public static Optional<QualifiedDataSource> findQualifiedDataSource(final
String qualifiedDataSourcePath) {
- Pattern pattern = Pattern.compile(
- String.join("/", NodePathGenerator.toPath(new
QualifiedDataSourceNodePath(null), false), QUALIFIED_DATA_SOURCE_PATTERN +
"$"), Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(qualifiedDataSourcePath);
- return matcher.find() ? Optional.of(new
QualifiedDataSource(matcher.group(1))) : Optional.empty();
- }
-}
diff --git
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathParserTest.java
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathParserTest.java
deleted file mode 100644
index b5e21128ac4..00000000000
---
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathParserTest.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.node.storage;
-
-import
org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource;
-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;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-class QualifiedDataSourceNodePathParserTest {
-
- @Test
- void assertFindQualifiedDataSource() {
- Optional<QualifiedDataSource> actual =
QualifiedDataSourceNodePathParser.findQualifiedDataSource("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0");
- assertTrue(actual.isPresent());
- assertThat(actual.get().getDatabaseName(), is("replica_query_db"));
- assertThat(actual.get().getGroupName(), is("readwrite_ds"));
- assertThat(actual.get().getDataSourceName(), is("replica_ds_0"));
- }
-
- @Test
- void assertNotFindQualifiedDataSource() {
- Optional<QualifiedDataSource> actual =
QualifiedDataSourceNodePathParser.findQualifiedDataSource("/nodes/xxx/");
- assertFalse(actual.isPresent());
- }
-}
diff --git
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathTest.java
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathTest.java
index 4f5ac6af152..e03c9c31425 100644
---
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathTest.java
+++
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/node/storage/QualifiedDataSourceNodePathTest.java
@@ -19,16 +19,28 @@ package
org.apache.shardingsphere.mode.node.path.node.storage;
import
org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource;
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 QualifiedDataSourceNodePathTest {
@Test
void assertToPath() {
+ assertThat(NodePathGenerator.toPath(new
QualifiedDataSourceNodePath((String) null), false),
is("/nodes/qualified_data_sources"));
assertThat(NodePathGenerator.toPath(new
QualifiedDataSourceNodePath(new
QualifiedDataSource("foo_db.foo_group.foo_ds")), false),
is("/nodes/qualified_data_sources/foo_db.foo_group.foo_ds"));
}
+
+ @Test
+ void assertCreateQualifiedDataSourceSearchCriteria() {
+
assertThat(NodePathSearcher.find("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0",
QualifiedDataSourceNodePath.createQualifiedDataSourceSearchCriteria()),
+ is(Optional.of("replica_query_db.readwrite_ds.replica_ds_0")));
+ assertFalse(NodePathSearcher.find("/nodes/xxx/",
QualifiedDataSourceNodePath.createQualifiedDataSourceSearchCriteria()).isPresent());
+ }
}
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/QualifiedDataSourceChangedHandler.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/QualifiedDataSourceChangedHandler.java
index 52b39133ab0..1bba5fdb46b 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/QualifiedDataSourceChangedHandler.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/type/QualifiedDataSourceChangedHandler.java
@@ -31,8 +31,8 @@ import
org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global.GlobalDataChangedEventHandler;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
+import org.apache.shardingsphere.mode.node.path.NodePathSearcher;
import
org.apache.shardingsphere.mode.node.path.node.storage.QualifiedDataSourceNodePath;
-import
org.apache.shardingsphere.mode.node.path.node.storage.QualifiedDataSourceNodePathParser;
import java.util.Arrays;
import java.util.Collection;
@@ -45,7 +45,7 @@ public final class QualifiedDataSourceChangedHandler
implements GlobalDataChange
@Override
public String getSubscribedKey() {
- return NodePathGenerator.toPath(new QualifiedDataSourceNodePath(null),
false);
+ return NodePathGenerator.toPath(new
QualifiedDataSourceNodePath((String) null), false);
}
@Override
@@ -58,7 +58,7 @@ public final class QualifiedDataSourceChangedHandler
implements GlobalDataChange
if (Strings.isNullOrEmpty(event.getValue())) {
return;
}
- Optional<QualifiedDataSource> qualifiedDataSource =
QualifiedDataSourceNodePathParser.findQualifiedDataSource(event.getKey());
+ Optional<QualifiedDataSource> qualifiedDataSource =
NodePathSearcher.find(event.getKey(),
QualifiedDataSourceNodePath.createQualifiedDataSourceSearchCriteria()).map(QualifiedDataSource::new);
if (!qualifiedDataSource.isPresent()) {
return;
}