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 001c3006d35 Remove NamedDatabaseRuleItemNode and UniqueDatabaseRuleItemNode (#34782) 001c3006d35 is described below commit 001c3006d3558aec360422d436218c67cc0f80bb Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Tue Feb 25 09:41:02 2025 +0800 Remove NamedDatabaseRuleItemNode and UniqueDatabaseRuleItemNode (#34782) * Refactor NamedDatabaseRuleItemNode and UniqueDatabaseRuleItemNode * Remove NamedDatabaseRuleItemNode and UniqueDatabaseRuleItemNode * Remove NamedDatabaseRuleItemNode and UniqueDatabaseRuleItemNode * Remove NamedDatabaseRuleItemNode and UniqueDatabaseRuleItemNode * Remove NamedDatabaseRuleItemNode and UniqueDatabaseRuleItemNode --- .../nodepath/BroadcastRuleNodeProviderTest.java | 2 +- .../nodepath/EncryptRuleNodeProviderTest.java | 4 +- .../nodepath/MaskRuleNodeProviderTest.java | 4 +- .../ReadwriteSplittingRuleNodeProviderTest.java | 4 +- .../nodepath/ShadowRuleNodeProviderTest.java | 8 ++-- .../nodepath/ShardingRuleNodeProviderTest.java | 4 +- .../nodepath/SingleRuleNodeProviderTest.java | 4 +- .../type/RuleItemAlteredBuildExecutor.java | 21 ++++---- .../type/RuleItemDroppedBuildExecutor.java | 18 +++---- .../type/config/database/DatabaseRuleNode.java | 44 ++--------------- .../database/item/NamedDatabaseRuleItemNode.java | 41 ---------------- .../database/item/UniqueDatabaseRuleItemNode.java | 41 ---------------- .../tuple/YamlRepositoryTupleSwapperEngine.java | 29 ++++++----- .../type/config/database/RuleNodePathTest.java | 56 ---------------------- 14 files changed, 58 insertions(+), 222 deletions(-) diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/metadata/nodepath/BroadcastRuleNodeProviderTest.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/metadata/nodepath/BroadcastRuleNodeProviderTest.java index 8f8065e2543..47b22fdee45 100644 --- a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/metadata/nodepath/BroadcastRuleNodeProviderTest.java +++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/metadata/nodepath/BroadcastRuleNodeProviderTest.java @@ -36,7 +36,7 @@ class BroadcastRuleNodeProviderTest { DatabaseRuleNode actual = provider.getDatabaseRuleNode(); assertTrue(actual.getNamedItems().isEmpty()); assertThat(actual.getUniqueItems().size(), is(1)); - assertTrue(actual.getUniqueItems().containsKey(BroadcastRuleNodeProvider.TABLES)); + assertTrue(actual.getUniqueItems().contains(BroadcastRuleNodeProvider.TABLES)); assertThat(actual.getRuleType(), is(BroadcastRuleNodeProvider.RULE_TYPE)); } } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/metadata/nodepath/EncryptRuleNodeProviderTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/metadata/nodepath/EncryptRuleNodeProviderTest.java index bcc54bf0248..c7d1e52ba6d 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/metadata/nodepath/EncryptRuleNodeProviderTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/metadata/nodepath/EncryptRuleNodeProviderTest.java @@ -35,8 +35,8 @@ class EncryptRuleNodeProviderTest { void assertGetDatabaseRuleNode() { DatabaseRuleNode actual = provider.getDatabaseRuleNode(); assertThat(actual.getNamedItems().size(), is(2)); - assertTrue(actual.getNamedItems().containsKey(EncryptRuleNodeProvider.ENCRYPTORS)); - assertTrue(actual.getNamedItems().containsKey(EncryptRuleNodeProvider.TABLES)); + assertTrue(actual.getNamedItems().contains(EncryptRuleNodeProvider.ENCRYPTORS)); + assertTrue(actual.getNamedItems().contains(EncryptRuleNodeProvider.TABLES)); assertTrue(actual.getUniqueItems().isEmpty()); assertThat(actual.getRuleType(), is(EncryptRuleNodeProvider.RULE_TYPE)); } diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/metadata/nodepath/MaskRuleNodeProviderTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/metadata/nodepath/MaskRuleNodeProviderTest.java index 81ababafef6..8b87bda70ad 100644 --- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/metadata/nodepath/MaskRuleNodeProviderTest.java +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/metadata/nodepath/MaskRuleNodeProviderTest.java @@ -35,8 +35,8 @@ class MaskRuleNodeProviderTest { void assertGetDatabaseRuleNode() { DatabaseRuleNode actual = provider.getDatabaseRuleNode(); assertThat(actual.getNamedItems().size(), is(2)); - assertTrue(actual.getNamedItems().containsKey(MaskRuleNodeProvider.MASK_ALGORITHMS)); - assertTrue(actual.getNamedItems().containsKey(MaskRuleNodeProvider.TABLES)); + assertTrue(actual.getNamedItems().contains(MaskRuleNodeProvider.MASK_ALGORITHMS)); + assertTrue(actual.getNamedItems().contains(MaskRuleNodeProvider.TABLES)); assertTrue(actual.getUniqueItems().isEmpty()); assertThat(actual.getRuleType(), is(MaskRuleNodeProvider.RULE_TYPE)); } diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/metadata/nodepath/ReadwriteSplittingRuleNodeProviderTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/metadata/nodepath/ReadwriteSplittingRuleNodeProviderTest.java index 63a8c0a7692..d3c6c783e62 100644 --- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/metadata/nodepath/ReadwriteSplittingRuleNodeProviderTest.java +++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/metadata/nodepath/ReadwriteSplittingRuleNodeProviderTest.java @@ -35,8 +35,8 @@ class ReadwriteSplittingRuleNodeProviderTest { void assertGetDatabaseRuleNode() { DatabaseRuleNode actual = provider.getDatabaseRuleNode(); assertThat(actual.getNamedItems().size(), is(2)); - assertTrue(actual.getNamedItems().containsKey(ReadwriteSplittingRuleNodeProvider.DATA_SOURCE_GROUPS)); - assertTrue(actual.getNamedItems().containsKey(ReadwriteSplittingRuleNodeProvider.LOAD_BALANCERS)); + assertTrue(actual.getNamedItems().contains(ReadwriteSplittingRuleNodeProvider.DATA_SOURCE_GROUPS)); + assertTrue(actual.getNamedItems().contains(ReadwriteSplittingRuleNodeProvider.LOAD_BALANCERS)); assertTrue(actual.getUniqueItems().isEmpty()); assertThat(actual.getRuleType(), is(ReadwriteSplittingRuleNodeProvider.RULE_TYPE)); } diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/metadata/nodepath/ShadowRuleNodeProviderTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/metadata/nodepath/ShadowRuleNodeProviderTest.java index 7fc88e0c9f7..b9c125152c4 100644 --- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/metadata/nodepath/ShadowRuleNodeProviderTest.java +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/metadata/nodepath/ShadowRuleNodeProviderTest.java @@ -35,11 +35,11 @@ class ShadowRuleNodeProviderTest { void assertGetDatabaseRuleNode() { DatabaseRuleNode actual = provider.getDatabaseRuleNode(); assertThat(actual.getNamedItems().size(), is(3)); - assertTrue(actual.getNamedItems().containsKey(ShadowRuleNodeProvider.SHADOW_ALGORITHMS)); - assertTrue(actual.getNamedItems().containsKey(ShadowRuleNodeProvider.TABLES)); - assertTrue(actual.getNamedItems().containsKey(ShadowRuleNodeProvider.DATA_SOURCES)); + assertTrue(actual.getNamedItems().contains(ShadowRuleNodeProvider.SHADOW_ALGORITHMS)); + assertTrue(actual.getNamedItems().contains(ShadowRuleNodeProvider.TABLES)); + assertTrue(actual.getNamedItems().contains(ShadowRuleNodeProvider.DATA_SOURCES)); assertThat(actual.getUniqueItems().size(), is(1)); - assertTrue(actual.getUniqueItems().containsKey(ShadowRuleNodeProvider.DEFAULT_SHADOW_ALGORITHM_NAME)); + assertTrue(actual.getUniqueItems().contains(ShadowRuleNodeProvider.DEFAULT_SHADOW_ALGORITHM_NAME)); assertThat(actual.getRuleType(), is(ShadowRuleNodeProvider.RULE_TYPE)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/nodepath/ShardingRuleNodeProviderTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/nodepath/ShardingRuleNodeProviderTest.java index 5cdec298a2a..40840be9362 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/nodepath/ShardingRuleNodeProviderTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/nodepath/ShardingRuleNodeProviderTest.java @@ -40,12 +40,12 @@ class ShardingRuleNodeProviderTest { assertThat(actual.getNamedItems().size(), is(6)); List<String> namedRuleItems = Arrays.asList(ShardingRuleNodeProvider.TABLES, ShardingRuleNodeProvider.AUTO_TABLES, ShardingRuleNodeProvider.BINDING_TABLES, ShardingRuleNodeProvider.SHARDING_ALGORITHMS, ShardingRuleNodeProvider.KEY_GENERATORS, ShardingRuleNodeProvider.AUDITORS); - assertThat("Named rule items equality without order", actual.getNamedItems().keySet(), IsIterableContainingInAnyOrder.containsInAnyOrder(namedRuleItems.toArray())); + assertThat("Named rule items equality without order", actual.getNamedItems(), IsIterableContainingInAnyOrder.containsInAnyOrder(namedRuleItems.toArray())); assertThat(actual.getUniqueItems().size(), is(6)); List<String> uniqueRuleItems = Arrays.asList(ShardingRuleNodeProvider.DEFAULT_DATABASE_STRATEGY, ShardingRuleNodeProvider.DEFAULT_TABLE_STRATEGY, ShardingRuleNodeProvider.DEFAULT_KEY_GENERATE_STRATEGY, ShardingRuleNodeProvider.DEFAULT_AUDIT_STRATEGY, ShardingRuleNodeProvider.DEFAULT_SHARDING_COLUMN, ShardingRuleNodeProvider.SHARDING_CACHE); - assertThat("Unique rule items equality without order", actual.getUniqueItems().keySet(), IsIterableContainingInAnyOrder.containsInAnyOrder(uniqueRuleItems.toArray())); + assertThat("Unique rule items equality without order", actual.getUniqueItems(), IsIterableContainingInAnyOrder.containsInAnyOrder(uniqueRuleItems.toArray())); assertThat(actual.getRuleType(), is(ShardingRuleNodeProvider.RULE_TYPE)); } } diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/metadata/nodepath/SingleRuleNodeProviderTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/metadata/nodepath/SingleRuleNodeProviderTest.java index 5287a566a7f..d47e2e0100f 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/metadata/nodepath/SingleRuleNodeProviderTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/metadata/nodepath/SingleRuleNodeProviderTest.java @@ -36,8 +36,8 @@ class SingleRuleNodeProviderTest { DatabaseRuleNode actual = provider.getDatabaseRuleNode(); assertTrue(actual.getNamedItems().isEmpty()); assertThat(actual.getUniqueItems().size(), is(2)); - assertTrue(actual.getUniqueItems().containsKey(SingleRuleNodeProvider.TABLES)); - assertTrue(actual.getUniqueItems().containsKey(SingleRuleNodeProvider.DEFAULT_DATA_SOURCE)); + assertTrue(actual.getUniqueItems().contains(SingleRuleNodeProvider.TABLES)); + assertTrue(actual.getUniqueItems().contains(SingleRuleNodeProvider.DEFAULT_DATA_SOURCE)); assertThat(actual.getRuleType(), is(SingleRuleNodeProvider.RULE_TYPE)); } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemAlteredBuildExecutor.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemAlteredBuildExecutor.java index af44f4363a6..610eadc8fb2 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemAlteredBuildExecutor.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemAlteredBuildExecutor.java @@ -18,14 +18,15 @@ package org.apache.shardingsphere.mode.metadata.changed.executor.type; import org.apache.shardingsphere.mode.metadata.changed.executor.RuleItemChangedBuildExecutor; +import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern; +import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearcher; import org.apache.shardingsphere.mode.node.path.type.config.database.DatabaseRuleNode; -import org.apache.shardingsphere.mode.node.path.type.config.database.item.NamedDatabaseRuleItemNode; -import org.apache.shardingsphere.mode.node.path.type.config.database.item.UniqueDatabaseRuleItemNode; +import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleItem; +import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleNodePath; import org.apache.shardingsphere.mode.spi.rule.item.alter.AlterNamedRuleItem; import org.apache.shardingsphere.mode.spi.rule.item.alter.AlterRuleItem; import org.apache.shardingsphere.mode.spi.rule.item.alter.AlterUniqueRuleItem; -import java.util.Map.Entry; import java.util.Optional; /** @@ -35,15 +36,17 @@ public final class RuleItemAlteredBuildExecutor implements RuleItemChangedBuildE @Override public Optional<AlterRuleItem> build(final DatabaseRuleNode databaseRuleNode, final String databaseName, final String path, final Integer activeVersion) { - for (Entry<String, NamedDatabaseRuleItemNode> entry : databaseRuleNode.getNamedItems().entrySet()) { - Optional<String> itemName = entry.getValue().getVersionNodePathParser().findIdentifierByActiveVersionPath(path, 2); + for (String each : databaseRuleNode.getNamedItems()) { + DatabaseRuleNodePath databaseRuleNodePath = new DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, databaseRuleNode.getRuleType(), new DatabaseRuleItem(each, NodePathPattern.IDENTIFIER)); + Optional<String> itemName = NodePathSearcher.getVersion(databaseRuleNodePath).findIdentifierByActiveVersionPath(path, 2); if (itemName.isPresent()) { - return Optional.of(new AlterNamedRuleItem(databaseName, itemName.get(), path, activeVersion, databaseRuleNode.getRuleType() + "." + entry.getKey())); + return Optional.of(new AlterNamedRuleItem(databaseName, itemName.get(), path, activeVersion, databaseRuleNode.getRuleType() + "." + each)); } } - for (Entry<String, UniqueDatabaseRuleItemNode> entry : databaseRuleNode.getUniqueItems().entrySet()) { - if (entry.getValue().getVersionNodePathParser().isActiveVersionPath(path)) { - return Optional.of(new AlterUniqueRuleItem(databaseName, path, activeVersion, databaseRuleNode.getRuleType() + "." + entry.getKey())); + for (String each : databaseRuleNode.getUniqueItems()) { + DatabaseRuleNodePath databaseRuleNodePath = new DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, databaseRuleNode.getRuleType(), new DatabaseRuleItem(each)); + if (NodePathSearcher.getVersion(databaseRuleNodePath).isActiveVersionPath(path)) { + return Optional.of(new AlterUniqueRuleItem(databaseName, path, activeVersion, databaseRuleNode.getRuleType() + "." + each)); } } return Optional.empty(); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemDroppedBuildExecutor.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemDroppedBuildExecutor.java index 48ee0224a25..e6e89f4a918 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemDroppedBuildExecutor.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/changed/executor/type/RuleItemDroppedBuildExecutor.java @@ -18,16 +18,15 @@ package org.apache.shardingsphere.mode.metadata.changed.executor.type; import org.apache.shardingsphere.mode.metadata.changed.executor.RuleItemChangedBuildExecutor; +import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern; import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearcher; import org.apache.shardingsphere.mode.node.path.type.config.database.DatabaseRuleNode; -import org.apache.shardingsphere.mode.node.path.type.config.database.item.NamedDatabaseRuleItemNode; -import org.apache.shardingsphere.mode.node.path.type.config.database.item.UniqueDatabaseRuleItemNode; +import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleItem; import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleNodePath; import org.apache.shardingsphere.mode.spi.rule.item.drop.DropNamedRuleItem; import org.apache.shardingsphere.mode.spi.rule.item.drop.DropRuleItem; import org.apache.shardingsphere.mode.spi.rule.item.drop.DropUniqueRuleItem; -import java.util.Map.Entry; import java.util.Optional; /** @@ -37,15 +36,16 @@ public final class RuleItemDroppedBuildExecutor implements RuleItemChangedBuildE @Override public Optional<DropRuleItem> build(final DatabaseRuleNode databaseRuleNode, final String databaseName, final String path, final Integer activeVersion) { - for (Entry<String, NamedDatabaseRuleItemNode> entry : databaseRuleNode.getNamedItems().entrySet()) { - Optional<String> itemName = NodePathSearcher.find(path, DatabaseRuleNodePath.createRuleItemNameSearchCriteria(databaseRuleNode.getRuleType(), entry.getValue().getType())); + for (String each : databaseRuleNode.getNamedItems()) { + Optional<String> itemName = NodePathSearcher.find(path, DatabaseRuleNodePath.createRuleItemNameSearchCriteria(databaseRuleNode.getRuleType(), each)); if (itemName.isPresent()) { - return Optional.of(new DropNamedRuleItem(databaseName, itemName.get(), databaseRuleNode.getRuleType() + "." + entry.getKey())); + return Optional.of(new DropNamedRuleItem(databaseName, itemName.get(), databaseRuleNode.getRuleType() + "." + each)); } } - for (Entry<String, UniqueDatabaseRuleItemNode> entry : databaseRuleNode.getUniqueItems().entrySet()) { - if (entry.getValue().getVersionNodePathParser().isActiveVersionPath(path)) { - return Optional.of(new DropUniqueRuleItem(databaseName, databaseRuleNode.getRuleType() + "." + entry.getKey())); + for (String each : databaseRuleNode.getUniqueItems()) { + DatabaseRuleNodePath databaseRuleNodePath = new DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, databaseRuleNode.getRuleType(), new DatabaseRuleItem(each)); + if (NodePathSearcher.getVersion(databaseRuleNodePath).isActiveVersionPath(path)) { + return Optional.of(new DropUniqueRuleItem(databaseName, databaseRuleNode.getRuleType() + "." + each)); } } return Optional.empty(); diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/DatabaseRuleNode.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/DatabaseRuleNode.java index 18973df521f..25aa7bd9c87 100644 --- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/DatabaseRuleNode.java +++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/DatabaseRuleNode.java @@ -18,56 +18,20 @@ package org.apache.shardingsphere.mode.node.path.type.config.database; import lombok.Getter; -import org.apache.shardingsphere.mode.node.path.type.config.database.item.NamedDatabaseRuleItemNode; -import org.apache.shardingsphere.mode.node.path.type.config.database.item.UniqueDatabaseRuleItemNode; +import lombok.RequiredArgsConstructor; import java.util.Collection; -import java.util.Map; -import java.util.stream.Collectors; /** * Database rule node. */ +@RequiredArgsConstructor @Getter public final class DatabaseRuleNode { private final String ruleType; - private final Map<String, NamedDatabaseRuleItemNode> namedItems; + private final Collection<String> namedItems; - private final Map<String, UniqueDatabaseRuleItemNode> uniqueItems; - - public DatabaseRuleNode(final String ruleType, final Collection<String> namedRuleItemNodePathTypes, final Collection<String> uniqueRuleItemNodePathTypes) { - this.ruleType = ruleType; - namedItems = getNamedRuleItemNodePathMap(namedRuleItemNodePathTypes); - uniqueItems = getUniqueRuleItemNodePathMap(uniqueRuleItemNodePathTypes); - } - - private Map<String, NamedDatabaseRuleItemNode> getNamedRuleItemNodePathMap(final Collection<String> namedRuleItemNodePathTypes) { - return namedRuleItemNodePathTypes.stream().collect(Collectors.toMap(each -> each, each -> new NamedDatabaseRuleItemNode(ruleType, each))); - } - - private Map<String, UniqueDatabaseRuleItemNode> getUniqueRuleItemNodePathMap(final Collection<String> uniqueRuleItemNodePathTypes) { - return uniqueRuleItemNodePathTypes.stream().collect(Collectors.toMap(each -> each, each -> new UniqueDatabaseRuleItemNode(ruleType, each))); - } - - /** - * Get named rule item node path. - * - * @param itemType item type - * @return named rule item node path - */ - public NamedDatabaseRuleItemNode getNamedItem(final String itemType) { - return namedItems.get(itemType); - } - - /** - * Get unique rule item node path. - * - * @param itemType item type - * @return unique rule item node path - */ - public UniqueDatabaseRuleItemNode getUniqueItem(final String itemType) { - return uniqueItems.get(itemType); - } + private final Collection<String> uniqueItems; } diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/item/NamedDatabaseRuleItemNode.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/item/NamedDatabaseRuleItemNode.java deleted file mode 100644 index af43e28e6e1..00000000000 --- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/item/NamedDatabaseRuleItemNode.java +++ /dev/null @@ -1,41 +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.type.config.database.item; - -import lombok.Getter; -import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern; -import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearcher; -import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleItem; -import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleNodePath; -import org.apache.shardingsphere.mode.node.path.type.version.VersionNodePathParser; - -/** - * Named database rule item node. - */ -@Getter -public final class NamedDatabaseRuleItemNode { - - private final String type; - - private final VersionNodePathParser versionNodePathParser; - - public NamedDatabaseRuleItemNode(final String ruleType, final String type) { - this.type = type; - versionNodePathParser = NodePathSearcher.getVersion(new DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, ruleType, new DatabaseRuleItem(type, NodePathPattern.IDENTIFIER))); - } -} diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/item/UniqueDatabaseRuleItemNode.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/item/UniqueDatabaseRuleItemNode.java deleted file mode 100644 index b6494cf3977..00000000000 --- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/config/database/item/UniqueDatabaseRuleItemNode.java +++ /dev/null @@ -1,41 +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.type.config.database.item; - -import lombok.Getter; -import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern; -import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearcher; -import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleItem; -import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleNodePath; -import org.apache.shardingsphere.mode.node.path.type.version.VersionNodePathParser; - -/** - * Unique database rule item node. - */ -@Getter -public final class UniqueDatabaseRuleItemNode { - - private final String type; - - private final VersionNodePathParser versionNodePathParser; - - public UniqueDatabaseRuleItemNode(final String ruleType, final String type) { - this.type = type; - versionNodePathParser = NodePathSearcher.getVersion(new DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, ruleType, new DatabaseRuleItem(type))); - } -} diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/tuple/YamlRepositoryTupleSwapperEngine.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/tuple/YamlRepositoryTupleSwapperEngine.java index 43a886424ce..8bc2cd6b91f 100644 --- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/tuple/YamlRepositoryTupleSwapperEngine.java +++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/tuple/YamlRepositoryTupleSwapperEngine.java @@ -29,6 +29,7 @@ import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlGlobalRuleConfi import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper; import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine; +import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern; import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearcher; import org.apache.shardingsphere.mode.node.path.type.config.database.DatabaseRuleNode; import org.apache.shardingsphere.mode.node.path.type.config.global.GlobalRuleNodePath; @@ -96,7 +97,7 @@ public final class YamlRepositoryTupleSwapperEngine { Collection<RepositoryTuple> result = new LinkedList<>(); for (Object value : (Collection) fieldValue) { String tupleKeyName = tupleKeyListNameGenerator.value().getConstructor().newInstance().generate(value); - result.add(new RepositoryTuple(new DatabaseRuleItem(databaseRuleNode.getNamedItem(tupleName).getType(), tupleKeyName).toString(), value.toString())); + result.add(new RepositoryTuple(new DatabaseRuleItem(tupleName, tupleKeyName).toString(), value.toString())); } return result; } @@ -104,27 +105,27 @@ public final class YamlRepositoryTupleSwapperEngine { Collection<RepositoryTuple> result = new LinkedList<>(); for (Object entry : ((Map) fieldValue).entrySet()) { result.add(new RepositoryTuple( - new DatabaseRuleItem(databaseRuleNode.getNamedItem(tupleName).getType(), ((Entry) entry).getKey().toString()).toString(), YamlEngine.marshal(((Entry) entry).getValue()))); + new DatabaseRuleItem(tupleName, ((Entry) entry).getKey().toString()).toString(), YamlEngine.marshal(((Entry) entry).getValue()))); } return result; } if (fieldValue instanceof Collection) { return ((Collection) fieldValue).isEmpty() ? Collections.emptyList() - : Collections.singleton(new RepositoryTuple(databaseRuleNode.getUniqueItem(tupleName).getType(), YamlEngine.marshal(fieldValue))); + : Collections.singleton(new RepositoryTuple(tupleName, YamlEngine.marshal(fieldValue))); } if (fieldValue instanceof String) { return ((String) fieldValue).isEmpty() ? Collections.emptyList() - : Collections.singleton(new RepositoryTuple(databaseRuleNode.getUniqueItem(tupleName).getType(), fieldValue.toString())); + : Collections.singleton(new RepositoryTuple(tupleName, fieldValue.toString())); } if (fieldValue instanceof Boolean || fieldValue instanceof Integer || fieldValue instanceof Long) { - return Collections.singleton(new RepositoryTuple(databaseRuleNode.getUniqueItem(tupleName).getType(), fieldValue.toString())); + return Collections.singleton(new RepositoryTuple(tupleName, fieldValue.toString())); } if (fieldValue instanceof Enum) { - return Collections.singleton(new RepositoryTuple(databaseRuleNode.getUniqueItem(tupleName).getType(), ((Enum) fieldValue).name())); + return Collections.singleton(new RepositoryTuple(tupleName, ((Enum) fieldValue).name())); } - return Collections.singleton(new RepositoryTuple(databaseRuleNode.getUniqueItem(tupleName).getType(), YamlEngine.marshal(fieldValue))); + return Collections.singleton(new RepositoryTuple(tupleName, YamlEngine.marshal(fieldValue))); } private Collection<Field> getFields(final Class<? extends YamlRuleConfiguration> yamlRuleConfigurationClass) { @@ -169,7 +170,8 @@ public final class YamlRepositoryTupleSwapperEngine { DatabaseRuleNode databaseRuleNode = TypedSPILoader.getService(DatabaseRuleNodeProvider.class, yamlRuleConfig.getRuleConfigurationType()).getDatabaseRuleNode(); for (RepositoryTuple each : repositoryTuples.stream() .filter(each -> NodePathSearcher.isMatchedPath(each.getKey(), DatabaseRuleNodePath.createValidRuleTypeSearchCriteria(databaseRuleNode.getRuleType()))).collect(Collectors.toList())) { - if (databaseRuleNode.getUniqueItem(tupleEntity.value()).getVersionNodePathParser().isVersionPath(each.getKey())) { + DatabaseRuleNodePath databaseRuleNodePath = new DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, databaseRuleNode.getRuleType(), new DatabaseRuleItem(tupleEntity.value())); + if (NodePathSearcher.getVersion(databaseRuleNodePath).isVersionPath(each.getKey())) { return Optional.of(YamlEngine.unmarshal(each.getValue(), toBeSwappedType)); } } @@ -211,17 +213,22 @@ public final class YamlRepositoryTupleSwapperEngine { String tupleName = getTupleName(field); RepositoryTupleKeyListNameGenerator tupleKeyListNameGenerator = field.getAnnotation(RepositoryTupleKeyListNameGenerator.class); if (null != tupleKeyListNameGenerator && fieldValue instanceof Collection) { - databaseRuleNode.getNamedItem(tupleName).getVersionNodePathParser() + DatabaseRuleNodePath databaseRuleNodePath = new DatabaseRuleNodePath( + NodePathPattern.IDENTIFIER, databaseRuleNode.getRuleType(), new DatabaseRuleItem(tupleName, NodePathPattern.IDENTIFIER)); + NodePathSearcher.getVersion(databaseRuleNodePath) .findIdentifierByVersionsPath(repositoryTuple.getKey(), 2).ifPresent(optional -> ((Collection) fieldValue).add(repositoryTuple.getValue())); return; } if (fieldValue instanceof Map) { Class<?> valueClass = (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[1]; - databaseRuleNode.getNamedItem(tupleName).getVersionNodePathParser().findIdentifierByVersionsPath(repositoryTuple.getKey(), 2) + DatabaseRuleNodePath databaseRuleNodePath = new DatabaseRuleNodePath( + NodePathPattern.IDENTIFIER, databaseRuleNode.getRuleType(), new DatabaseRuleItem(tupleName, NodePathPattern.IDENTIFIER)); + NodePathSearcher.getVersion(databaseRuleNodePath).findIdentifierByVersionsPath(repositoryTuple.getKey(), 2) .ifPresent(optional -> ((Map) fieldValue).put(optional, YamlEngine.unmarshal(repositoryTuple.getValue(), valueClass))); return; } - if (!databaseRuleNode.getUniqueItem(tupleName).getVersionNodePathParser().isVersionPath(repositoryTuple.getKey())) { + DatabaseRuleNodePath databaseRuleNodePath = new DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, databaseRuleNode.getRuleType(), new DatabaseRuleItem(tupleName)); + if (!NodePathSearcher.getVersion(databaseRuleNodePath).isVersionPath(repositoryTuple.getKey())) { return; } if (fieldValue instanceof Collection) { diff --git a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/type/config/database/RuleNodePathTest.java b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/type/config/database/RuleNodePathTest.java deleted file mode 100644 index 210a0ed8dba..00000000000 --- a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/path/type/config/database/RuleNodePathTest.java +++ /dev/null @@ -1,56 +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.type.config.database; - -import org.apache.shardingsphere.mode.node.path.type.config.database.item.NamedDatabaseRuleItemNode; -import org.apache.shardingsphere.mode.node.path.type.config.database.item.UniqueDatabaseRuleItemNode; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -class RuleNodePathTest { - - private DatabaseRuleNode databaseRuleNode; - - @BeforeEach - void setup() { - List<String> namedRuleItemNodePathTypes = Collections.singletonList("tables"); - List<String> uniqueRuleItemNodePathTypes = Arrays.asList("tables", "tables/type"); - databaseRuleNode = new DatabaseRuleNode("foo", namedRuleItemNodePathTypes, uniqueRuleItemNodePathTypes); - } - - @Test - void assertFindNameByVersion() { - NamedDatabaseRuleItemNode namedRulePath = databaseRuleNode.getNamedItem("tables"); - assertThat(namedRulePath.getType(), is("tables")); - } - - @Test - void assertGetUniqueItem() { - UniqueDatabaseRuleItemNode uniqueRulePath = databaseRuleNode.getUniqueItem("tables"); - assertThat(uniqueRulePath.getType(), is("tables")); - UniqueDatabaseRuleItemNode uniqueRulePathWithType = databaseRuleNode.getUniqueItem("tables/type"); - assertThat(uniqueRulePathWithType.getType(), is("tables/type")); - } -}