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 1543c0a465d Unify to save znode path fully in RuleRepositoryTuple.key
(#34835)
1543c0a465d is described below
commit 1543c0a465d9802783d5ed2f7709f08fe982181c
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Mar 2 01:09:41 2025 +0800
Unify to save znode path fully in RuleRepositoryTuple.key (#34835)
* Unify save full znode path in RuleRepositoryTuple.key
* Unify to save znode path fully in RuleRepositoryTuple.key
* Unify to save znode path fully in RuleRepositoryTuple.key
---
...tionYamlRuleRepositoryTupleSwapperEngineIT.java | 2 +-
.../database/DatabaseRulePersistService.java | 43 +++++++---------------
.../config/global/GlobalRulePersistService.java | 10 ++---
.../node/path/type/version/VersionNodePath.java | 4 ++
.../YamlRuleRepositoryTupleSwapperEngine.java | 40 +++++++++++++-------
.../YamlRuleRepositoryTupleSwapperEngineTest.java | 26 ++++++-------
.../YamlRuleRepositoryTupleSwapperEngineIT.java | 13 ++-----
7 files changed, 68 insertions(+), 70 deletions(-)
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/ShardingConfigurationYamlRuleRepositoryTupleSwapperEngineIT.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/ShardingConfigurationYamlRuleRepositoryTupleSwapperEngineIT.java
index e54c93b4367..ce58ff31b8e 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/ShardingConfigurationYamlRuleRepositoryTupleSwapperEngineIT.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/ShardingConfigurationYamlRuleRepositoryTupleSwapperEngineIT.java
@@ -55,7 +55,7 @@ class
ShardingConfigurationYamlRuleRepositoryTupleSwapperEngineIT extends YamlRu
assertRepositoryTuple(actualTuples.get(12), "tables/t_stock",
((YamlShardingRuleConfiguration)
expectedYamlRuleConfig).getTables().get("t_stock"));
assertRepositoryTuple(actualTuples.get(13), "tables/t_order",
((YamlShardingRuleConfiguration)
expectedYamlRuleConfig).getTables().get("t_order"));
assertRepositoryTuple(actualTuples.get(14), "tables/t_order_item",
((YamlShardingRuleConfiguration)
expectedYamlRuleConfig).getTables().get("t_order_item"));
- assertRepositoryTuple(actualTuples.get(15), "binding_tables" +
actualTuples.get(15).getKey().substring(actualTuples.get(15).getKey().indexOf("/")),
+ assertRepositoryTuple(actualTuples.get(15), "binding_tables" +
actualTuples.get(15).getKey().substring(actualTuples.get(15).getKey().lastIndexOf("/")),
new ArrayList<>(((YamlShardingRuleConfiguration)
expectedYamlRuleConfig).getBindingTables()).get(0));
assertRepositoryTuple(actualTuples.get(16), "binding_tables/foo", new
ArrayList<>(((YamlShardingRuleConfiguration)
expectedYamlRuleConfig).getBindingTables()).get(1));
assertRepositoryTuple(actualTuples.get(17), "default_sharding_column",
((YamlShardingRuleConfiguration)
expectedYamlRuleConfig).getDefaultShardingColumn());
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/config/database/DatabaseRulePersistService.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/config/database/DatabaseRulePersistService.java
index 4cf5c344a8a..b240e93b78e 100644
---
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/config/database/DatabaseRulePersistService.java
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/config/database/DatabaseRulePersistService.java
@@ -30,7 +30,6 @@ import
org.apache.shardingsphere.mode.node.rule.node.DatabaseRuleNode;
import org.apache.shardingsphere.mode.node.rule.node.DatabaseRuleNodeGenerator;
import org.apache.shardingsphere.mode.node.rule.tuple.RuleRepositoryTuple;
import
org.apache.shardingsphere.mode.node.rule.tuple.YamlRuleRepositoryTupleSwapperEngine;
-import
org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleRepositoryTupleEntity;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
import java.util.Collection;
@@ -49,15 +48,9 @@ public final class DatabaseRulePersistService {
private final VersionPersistService versionPersistService;
- private final YamlRuleRepositoryTupleSwapperEngine
yamlRuleRepositoryTupleSwapperEngine;
-
- private final YamlRuleConfigurationSwapperEngine
yamlRuleConfigurationSwapperEngine;
-
public DatabaseRulePersistService(final PersistRepository repository) {
this.repository = repository;
versionPersistService = new VersionPersistService(repository);
- yamlRuleRepositoryTupleSwapperEngine = new
YamlRuleRepositoryTupleSwapperEngine();
- yamlRuleConfigurationSwapperEngine = new
YamlRuleConfigurationSwapperEngine();
}
/**
@@ -73,7 +66,7 @@ public final class DatabaseRulePersistService {
private RuleConfiguration load(final String databaseName, final String
ruleType) {
return new
YamlRuleConfigurationSwapperEngine().swapToRuleConfiguration(
-
yamlRuleRepositoryTupleSwapperEngine.swapToYamlDatabaseRuleConfiguration(ruleType,
load(databaseName, DatabaseRuleNodeGenerator.generate(ruleType))));
+ new
YamlRuleRepositoryTupleSwapperEngine(databaseName).swapToYamlDatabaseRuleConfiguration(ruleType,
load(databaseName, DatabaseRuleNodeGenerator.generate(ruleType))));
}
private Collection<RuleRepositoryTuple> load(final String databaseName,
final DatabaseRuleNode databaseRuleNode) {
@@ -107,24 +100,16 @@ public final class DatabaseRulePersistService {
*/
public Collection<MetaDataVersion> persist(final String databaseName,
final Collection<RuleConfiguration> configs) {
Collection<MetaDataVersion> result = new LinkedList<>();
- for (YamlRuleConfiguration each :
yamlRuleConfigurationSwapperEngine.swapToYamlRuleConfigurations(configs)) {
- Collection<RuleRepositoryTuple> tuples =
yamlRuleRepositoryTupleSwapperEngine.swapToTuples(each);
- if (!tuples.isEmpty()) {
- result.addAll(persistDataNodes(databaseName,
Objects.requireNonNull(each.getClass().getAnnotation(RuleRepositoryTupleEntity.class)).value(),
tuples));
- }
+ YamlRuleRepositoryTupleSwapperEngine tupleSwapperEngine = new
YamlRuleRepositoryTupleSwapperEngine(databaseName);
+ for (YamlRuleConfiguration each : new
YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs)) {
+
result.addAll(persistTuples(tupleSwapperEngine.swapToTuples(each)));
}
return result;
}
- private Collection<MetaDataVersion> persistDataNodes(final String
databaseName, final String ruleType, final Collection<RuleRepositoryTuple>
tuples) {
- Collection<MetaDataVersion> result = new LinkedList<>();
- for (RuleRepositoryTuple each : tuples) {
- DatabaseRuleItem databaseRuleItem = new
DatabaseRuleItem(each.getKey());
- DatabaseRuleNodePath databaseRuleNodePath = new
DatabaseRuleNodePath(databaseName, ruleType, databaseRuleItem);
- int nextVersion = versionPersistService.persist(new
VersionNodePath(databaseRuleNodePath), each.getValue());
- result.add(new
MetaDataVersion(NodePathGenerator.toPath(databaseRuleNodePath, false),
Math.max(MetaDataVersion.INIT_VERSION, nextVersion - 1)));
- }
- return result;
+ private Collection<MetaDataVersion> persistTuples(final
Collection<RuleRepositoryTuple> tuples) {
+ return tuples.stream().map(each -> new MetaDataVersion(
+ each.getKey(), Math.max(MetaDataVersion.INIT_VERSION,
versionPersistService.persist(new VersionNodePath(each.getKey()),
each.getValue()) - 1))).collect(Collectors.toList());
}
/**
@@ -146,23 +131,23 @@ public final class DatabaseRulePersistService {
*/
public Collection<MetaDataVersion> delete(final String databaseName, final
Collection<RuleConfiguration> configs) {
Collection<MetaDataVersion> result = new LinkedList<>();
- for (YamlRuleConfiguration each :
yamlRuleConfigurationSwapperEngine.swapToYamlRuleConfigurations(configs)) {
- List<RuleRepositoryTuple> tuples = new
LinkedList<>(yamlRuleRepositoryTupleSwapperEngine.swapToTuples(each));
+ YamlRuleRepositoryTupleSwapperEngine tupleSwapperEngine = new
YamlRuleRepositoryTupleSwapperEngine(databaseName);
+ for (YamlRuleConfiguration each : new
YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(configs)) {
+ List<RuleRepositoryTuple> tuples = new
LinkedList<>(tupleSwapperEngine.swapToTuples(each));
if (tuples.isEmpty()) {
continue;
}
Collections.reverse(tuples);
- result.addAll(delete(databaseName,
Objects.requireNonNull(each.getClass().getAnnotation(RuleRepositoryTupleEntity.class)).value(),
tuples));
+ result.addAll(deleteTuples(tuples));
}
return result;
}
- private Collection<MetaDataVersion> delete(final String databaseName,
final String ruleType, final Collection<RuleRepositoryTuple> tuples) {
+ private Collection<MetaDataVersion> deleteTuples(final
Collection<RuleRepositoryTuple> tuples) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (RuleRepositoryTuple each : tuples) {
- String toBeDeletedKey = NodePathGenerator.toPath(new
DatabaseRuleNodePath(databaseName, ruleType, new
DatabaseRuleItem(each.getKey())), false);
- repository.delete(toBeDeletedKey);
- result.add(new MetaDataVersion(toBeDeletedKey));
+ repository.delete(each.getKey());
+ result.add(new MetaDataVersion(each.getKey()));
}
return result;
}
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/config/global/GlobalRulePersistService.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/config/global/GlobalRulePersistService.java
index 1913521db44..8d43546b103 100644
---
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/config/global/GlobalRulePersistService.java
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/config/global/GlobalRulePersistService.java
@@ -42,13 +42,13 @@ public final class GlobalRulePersistService {
private final YamlRuleRepositoryTupleSwapperEngine tupleSwapperEngine;
- private final YamlRuleConfigurationSwapperEngine
yamlRuleConfigurationSwapperEngine;
+ private final YamlRuleConfigurationSwapperEngine yamlSwapperEngine;
public GlobalRulePersistService(final PersistRepository repository, final
VersionPersistService versionPersistService) {
this.repository = repository;
this.versionPersistService = versionPersistService;
tupleSwapperEngine = new YamlRuleRepositoryTupleSwapperEngine();
- yamlRuleConfigurationSwapperEngine = new
YamlRuleConfigurationSwapperEngine();
+ yamlSwapperEngine = new YamlRuleConfigurationSwapperEngine();
}
/**
@@ -68,7 +68,7 @@ public final class GlobalRulePersistService {
*/
public RuleConfiguration load(final String ruleType) {
String ruleContent = versionPersistService.loadContent(new
VersionNodePath(new GlobalRuleNodePath(ruleType)));
- return
yamlRuleConfigurationSwapperEngine.swapToRuleConfiguration(tupleSwapperEngine.swapToYamlGlobalRuleConfiguration(ruleType,
ruleContent));
+ return
yamlSwapperEngine.swapToRuleConfiguration(tupleSwapperEngine.swapToYamlGlobalRuleConfiguration(ruleType,
ruleContent));
}
/**
@@ -77,12 +77,12 @@ public final class GlobalRulePersistService {
* @param globalRuleConfigs global rule configurations
*/
public void persist(final Collection<RuleConfiguration> globalRuleConfigs)
{
- for (YamlRuleConfiguration each :
yamlRuleConfigurationSwapperEngine.swapToYamlRuleConfigurations(globalRuleConfigs))
{
+ for (YamlRuleConfiguration each :
yamlSwapperEngine.swapToYamlRuleConfigurations(globalRuleConfigs)) {
persistTuples(tupleSwapperEngine.swapToTuples(each));
}
}
private void persistTuples(final Collection<RuleRepositoryTuple> tuples) {
- tuples.forEach(each -> versionPersistService.persist(new
VersionNodePath(new GlobalRuleNodePath(each.getKey())), each.getValue()));
+ tuples.forEach(each -> versionPersistService.persist(new
VersionNodePath(each.getKey()), each.getValue()));
}
}
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/version/VersionNodePath.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/version/VersionNodePath.java
index 3f4f647281e..7eabc47ca9e 100644
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/version/VersionNodePath.java
+++
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/version/VersionNodePath.java
@@ -35,6 +35,10 @@ public final class VersionNodePath {
path = NodePathGenerator.toPath(nodePath, false);
}
+ public VersionNodePath(final String path) {
+ this.path = path;
+ }
+
/**
* Get active version path.
*
diff --git
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngine.java
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngine.java
index 47545e774c4..fcaeca54952 100644
---
a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngine.java
+++
b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngine.java
@@ -18,14 +18,17 @@
package org.apache.shardingsphere.mode.node.rule.tuple;
import com.google.common.base.Strings;
+import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import
org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
+import
org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
import
org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
import
org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;
import
org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearcher;
+import org.apache.shardingsphere.mode.node.path.type.global.GlobalRuleNodePath;
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.rule.node.DatabaseRuleNode;
@@ -47,8 +50,15 @@ import java.util.Optional;
/**
* YAML rule repository tuple swapper engine.
*/
+@RequiredArgsConstructor
public final class YamlRuleRepositoryTupleSwapperEngine {
+ private final String databaseName;
+
+ public YamlRuleRepositoryTupleSwapperEngine() {
+ this(NodePathPattern.IDENTIFIER);
+ }
+
/**
* Swap to rule repository tuples.
*
@@ -60,14 +70,15 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
if (null == entity) {
return Collections.emptyList();
}
+ String ruleType = entity.value();
if (entity.leaf()) {
- return Collections.singleton(new
RuleRepositoryTuple(entity.value(), YamlEngine.marshal(yamlRuleConfig)));
+ return Collections.singleton(new
RuleRepositoryTuple(NodePathGenerator.toPath(new GlobalRuleNodePath(ruleType),
false), YamlEngine.marshal(yamlRuleConfig)));
}
Collection<RuleRepositoryTuple> result = new LinkedList<>();
for (Field each :
YamlRuleConfigurationFieldUtil.getFields(yamlRuleConfig.getClass())) {
boolean isAccessible = each.isAccessible();
each.setAccessible(true);
- result.addAll(swapToTuples(yamlRuleConfig, each));
+ result.addAll(swapToTuples(ruleType, yamlRuleConfig, each));
each.setAccessible(isAccessible);
}
return result;
@@ -75,7 +86,7 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
@SneakyThrows(ReflectiveOperationException.class)
@SuppressWarnings("rawtypes")
- private Collection<RuleRepositoryTuple> swapToTuples(final
YamlRuleConfiguration yamlRuleConfig, final Field field) {
+ private Collection<RuleRepositoryTuple> swapToTuples(final String
ruleType, final YamlRuleConfiguration yamlRuleConfig, final Field field) {
Object fieldValue = field.get(yamlRuleConfig);
if (null == fieldValue) {
return Collections.emptyList();
@@ -86,30 +97,33 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
Collection<RuleRepositoryTuple> result = new LinkedList<>();
for (Object value : (Collection) fieldValue) {
String tupleKeyName =
tupleKeyListNameGenerator.value().getConstructor().newInstance().generate(value);
- result.add(new RuleRepositoryTuple(new
DatabaseRuleItem(tupleName, tupleKeyName).toString(), value.toString()));
+ String tupleKey = NodePathGenerator.toPath(new
DatabaseRuleNodePath(databaseName, ruleType, new DatabaseRuleItem(tupleName,
tupleKeyName)), false);
+ result.add(new RuleRepositoryTuple(tupleKey,
value.toString()));
}
return result;
}
if (fieldValue instanceof Map) {
Collection<RuleRepositoryTuple> result = new LinkedList<>();
for (Object entry : ((Map) fieldValue).entrySet()) {
- result.add(new RuleRepositoryTuple(new
DatabaseRuleItem(tupleName, ((Entry) entry).getKey().toString()).toString(),
YamlEngine.marshal(((Entry) entry).getValue())));
+ String tupleKey = NodePathGenerator.toPath(new
DatabaseRuleNodePath(databaseName, ruleType, new DatabaseRuleItem(tupleName,
((Entry) entry).getKey().toString())), false);
+ result.add(new RuleRepositoryTuple(tupleKey,
YamlEngine.marshal(((Entry) entry).getValue())));
}
return result;
}
+ String tupleKey = NodePathGenerator.toPath(new
DatabaseRuleNodePath(databaseName, ruleType, new DatabaseRuleItem(tupleName)),
false);
if (fieldValue instanceof Collection) {
- return ((Collection) fieldValue).isEmpty() ?
Collections.emptyList() : Collections.singleton(new
RuleRepositoryTuple(tupleName, YamlEngine.marshal(fieldValue)));
+ return ((Collection) fieldValue).isEmpty() ?
Collections.emptyList() : Collections.singleton(new
RuleRepositoryTuple(tupleKey, YamlEngine.marshal(fieldValue)));
}
if (fieldValue instanceof String) {
- return ((String) fieldValue).isEmpty() ? Collections.emptyList() :
Collections.singleton(new RuleRepositoryTuple(tupleName,
fieldValue.toString()));
+ return ((String) fieldValue).isEmpty() ? Collections.emptyList() :
Collections.singleton(new RuleRepositoryTuple(tupleKey, fieldValue.toString()));
}
if (fieldValue instanceof Boolean || fieldValue instanceof Integer ||
fieldValue instanceof Long) {
- return Collections.singleton(new RuleRepositoryTuple(tupleName,
fieldValue.toString()));
+ return Collections.singleton(new RuleRepositoryTuple(tupleKey,
fieldValue.toString()));
}
if (fieldValue instanceof Enum) {
- return Collections.singleton(new RuleRepositoryTuple(tupleName,
((Enum) fieldValue).name()));
+ return Collections.singleton(new RuleRepositoryTuple(tupleKey,
((Enum) fieldValue).name()));
}
- return Collections.singleton(new RuleRepositoryTuple(tupleName,
YamlEngine.marshal(fieldValue)));
+ return Collections.singleton(new RuleRepositoryTuple(tupleKey,
YamlEngine.marshal(fieldValue)));
}
/**
@@ -191,8 +205,8 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
@SneakyThrows(ReflectiveOperationException.class)
@SuppressWarnings({"unchecked", "rawtypes"})
private void setNamedItemFieldValue(final YamlRuleConfiguration
yamlRuleConfig, final String ruleType, final RuleRepositoryTuple tuple, final
String itemType, final Field field) {
- DatabaseRuleNodePath databaseRuleNodePath = new
DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, ruleType, new
DatabaseRuleItem(itemType, NodePathPattern.QUALIFIED_IDENTIFIER));
- Optional<String> itemValue = NodePathSearcher.find(tuple.getKey(), new
NodePathSearchCriteria(databaseRuleNodePath, false, true, 2));
+ DatabaseRuleNodePath databaseRuleNodePath = new
DatabaseRuleNodePath(databaseName, ruleType, new DatabaseRuleItem(itemType,
NodePathPattern.QUALIFIED_IDENTIFIER));
+ Optional<String> itemValue = NodePathSearcher.find(tuple.getKey(), new
NodePathSearchCriteria(databaseRuleNodePath, false, true, 1));
if (!itemValue.isPresent()) {
return;
}
@@ -215,7 +229,7 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
@SneakyThrows(ReflectiveOperationException.class)
private void setUniqueItemFieldValue(final YamlRuleConfiguration
yamlRuleConfig, final String ruleType, final RuleRepositoryTuple tuple, final
String itemType, final Field field) {
- DatabaseRuleNodePath databaseRuleNodePath = new
DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, ruleType, new
DatabaseRuleItem(itemType));
+ DatabaseRuleNodePath databaseRuleNodePath = new
DatabaseRuleNodePath(databaseName, ruleType, new DatabaseRuleItem(itemType));
if (!NodePathSearcher.isMatchedPath(tuple.getKey(), new
NodePathSearchCriteria(databaseRuleNodePath, false, true, 1))) {
return;
}
diff --git
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngineTest.java
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngineTest.java
index 66ffcd073fd..9475442e2e1 100644
---
a/mode/node/src/test/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngineTest.java
+++
b/mode/node/src/test/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngineTest.java
@@ -47,7 +47,7 @@ class YamlRuleRepositoryTupleSwapperEngineTest {
Collection<RuleRepositoryTuple> actual = new
YamlRuleRepositoryTupleSwapperEngine().swapToTuples(new
LeafYamlRuleConfiguration("foo"));
assertThat(actual.size(), is(1));
RuleRepositoryTuple actualTuple = actual.iterator().next();
- assertThat(actualTuple.getKey(), is("leaf"));
+ assertThat(actualTuple.getKey(), is("/rules/leaf"));
assertThat(actualTuple.getValue(), is("value: foo" +
System.lineSeparator()));
}
@@ -71,27 +71,27 @@ class YamlRuleRepositoryTupleSwapperEngineTest {
yamlRuleConfig.setLeaf(leaf);
yamlRuleConfig.setGens(Collections.singleton("value"));
yamlRuleConfig.setGen("single_gen");
- List<RuleRepositoryTuple> actual = new ArrayList<>(new
YamlRuleRepositoryTupleSwapperEngine().swapToTuples(yamlRuleConfig));
+ List<RuleRepositoryTuple> actual = new ArrayList<>(new
YamlRuleRepositoryTupleSwapperEngine("foo_db").swapToTuples(yamlRuleConfig));
assertThat(actual.size(), is(10));
- assertThat(actual.get(0).getKey(), is("map_value/k"));
+ assertThat(actual.get(0).getKey(),
is("/metadata/foo_db/rules/node/map_value/k"));
assertThat(actual.get(0).getValue(), is("value: v" +
System.lineSeparator()));
- assertThat(actual.get(1).getKey(), is("collection_value"));
+ assertThat(actual.get(1).getKey(),
is("/metadata/foo_db/rules/node/collection_value"));
assertThat(actual.get(1).getValue(), is("- !LEAF" +
System.lineSeparator() + " value: foo" + System.lineSeparator()));
- assertThat(actual.get(2).getKey(), is("string_value"));
+ assertThat(actual.get(2).getKey(),
is("/metadata/foo_db/rules/node/string_value"));
assertThat(actual.get(2).getValue(), is("str"));
- assertThat(actual.get(3).getKey(), is("boolean_value"));
+ assertThat(actual.get(3).getKey(),
is("/metadata/foo_db/rules/node/boolean_value"));
assertThat(actual.get(3).getValue(), is("true"));
- assertThat(actual.get(4).getKey(), is("integer_value"));
+ assertThat(actual.get(4).getKey(),
is("/metadata/foo_db/rules/node/integer_value"));
assertThat(actual.get(4).getValue(), is("1"));
- assertThat(actual.get(5).getKey(), is("long_value"));
+ assertThat(actual.get(5).getKey(),
is("/metadata/foo_db/rules/node/long_value"));
assertThat(actual.get(5).getValue(), is("10"));
- assertThat(actual.get(6).getKey(), is("enum_value"));
+ assertThat(actual.get(6).getKey(),
is("/metadata/foo_db/rules/node/enum_value"));
assertThat(actual.get(6).getValue(), is("FOO"));
- assertThat(actual.get(7).getKey(), is("leaf"));
+ assertThat(actual.get(7).getKey(),
is("/metadata/foo_db/rules/node/leaf"));
assertThat(actual.get(7).getValue(), is("value: leaf" +
System.lineSeparator()));
- assertThat(actual.get(8).getKey(), is("gens/gen: value"));
+ assertThat(actual.get(8).getKey(),
is("/metadata/foo_db/rules/node/gens/gen: value"));
assertThat(actual.get(8).getValue(), is("value"));
- assertThat(actual.get(9).getKey(), is("gen"));
+ assertThat(actual.get(9).getKey(),
is("/metadata/foo_db/rules/node/gen"));
assertThat(actual.get(9).getValue(), is("single_gen"));
}
@@ -104,7 +104,7 @@ class YamlRuleRepositoryTupleSwapperEngineTest {
@Test
void assertSwapToYamlRuleConfigurationWithNodeYamlRuleConfiguration() {
- NodeYamlRuleConfiguration actual = (NodeYamlRuleConfiguration) new
YamlRuleRepositoryTupleSwapperEngine().swapToYamlDatabaseRuleConfiguration("node",
Arrays.asList(
+ NodeYamlRuleConfiguration actual = (NodeYamlRuleConfiguration) new
YamlRuleRepositoryTupleSwapperEngine("foo_db").swapToYamlDatabaseRuleConfiguration("node",
Arrays.asList(
new
RuleRepositoryTuple("/metadata/foo_db/rules/node/map_value/k", "v"),
new
RuleRepositoryTuple("/metadata/foo_db/rules/node/map_value/k:qualified",
"k:qualified"),
new
RuleRepositoryTuple("/metadata/foo_db/rules/node/collection_value", "- !LEAF" +
System.lineSeparator() + " value: foo"),
diff --git
a/test/it/yaml/src/main/java/org/apache/shardingsphere/test/it/yaml/YamlRuleRepositoryTupleSwapperEngineIT.java
b/test/it/yaml/src/main/java/org/apache/shardingsphere/test/it/yaml/YamlRuleRepositoryTupleSwapperEngineIT.java
index bd3abdc3ac4..3e0bbcac8ef 100644
---
a/test/it/yaml/src/main/java/org/apache/shardingsphere/test/it/yaml/YamlRuleRepositoryTupleSwapperEngineIT.java
+++
b/test/it/yaml/src/main/java/org/apache/shardingsphere/test/it/yaml/YamlRuleRepositoryTupleSwapperEngineIT.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.test.it.yaml;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
-import
org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlGlobalRuleConfiguration;
import
org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import org.apache.shardingsphere.mode.node.rule.tuple.RuleRepositoryTuple;
import
org.apache.shardingsphere.mode.node.rule.tuple.YamlRuleRepositoryTupleSwapperEngine;
@@ -40,6 +39,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -55,7 +55,7 @@ public abstract class YamlRuleRepositoryTupleSwapperEngineIT {
URL url =
Thread.currentThread().getContextClassLoader().getResource(yamlFileName);
assertNotNull(url);
yamlFile = new File(url.getFile());
- engine = new YamlRuleRepositoryTupleSwapperEngine();
+ engine = new YamlRuleRepositoryTupleSwapperEngine("foo_db");
}
@Test
@@ -73,7 +73,7 @@ public abstract class YamlRuleRepositoryTupleSwapperEngineIT {
protected abstract void assertRepositoryTuples(List<RuleRepositoryTuple>
actualTuples, YamlRuleConfiguration expectedYamlRuleConfig);
protected void assertRepositoryTuple(final RuleRepositoryTuple actual,
final String expectedKey, final Object expectedValue) {
- assertThat(actual.getKey(), is(expectedKey));
+ assertThat(actual.getKey(), containsString(expectedKey));
assertThat(actual.getValue(), is(isSimpleObject(expectedValue) ?
expectedValue : YamlEngine.marshal(expectedValue)));
}
@@ -94,8 +94,7 @@ public abstract class YamlRuleRepositoryTupleSwapperEngineIT {
YamlRuleConfiguration yamlRuleConfig = loadYamlRuleConfiguration();
RuleRepositoryTupleEntity entity =
yamlRuleConfig.getClass().getAnnotation(RuleRepositoryTupleEntity.class);
String ruleType = Objects.requireNonNull(entity).value();
- Collection<RuleRepositoryTuple> tuples =
engine.swapToTuples(yamlRuleConfig).stream()
- .map(each -> new
RuleRepositoryTuple(getRepositoryTupleKey(yamlRuleConfig instanceof
YamlGlobalRuleConfiguration, ruleType, each),
each.getValue())).collect(Collectors.toList());
+ Collection<RuleRepositoryTuple> tuples =
engine.swapToTuples(yamlRuleConfig);
YamlRuleConfiguration actualYamlRuleConfig = entity.leaf()
? engine.swapToYamlGlobalRuleConfiguration(ruleType,
tuples.iterator().next().getValue())
: engine.swapToYamlDatabaseRuleConfiguration(ruleType, tuples);
@@ -104,10 +103,6 @@ public abstract class
YamlRuleRepositoryTupleSwapperEngineIT {
return YamlEngine.marshal(yamlRootConfig);
}
- private String getRepositoryTupleKey(final boolean isGlobalRule, final
String ruleType, final RuleRepositoryTuple tuple) {
- return isGlobalRule ? String.format("/rules/%s", ruleType) :
String.format("/metadata/foo_db/rules/%s/%s", ruleType, tuple.getKey());
- }
-
private String getExpectedYamlContent() throws IOException {
String content = Files.readAllLines(yamlFile.toPath()).stream()
.filter(each -> !each.contains("#") &&
!each.isEmpty()).collect(Collectors.joining(System.lineSeparator())) +
System.lineSeparator();