Skip to content

Commit 0424d58

Browse files
authored
Merge pull request #34963 from terrymanu/dev
Use hierarchical layer to process NodePath
2 parents 063112e + cbabf40 commit 0424d58

File tree

36 files changed

+568
-174
lines changed

36 files changed

+568
-174
lines changed

mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/rule/DatabaseRuleItemManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void alter(final DatabaseRuleNodePath databaseRuleNodePath) throws SQLExc
5757
RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class,
5858
new RuleChangedItemType(databaseRuleNodePath.getRuleType(), databaseRuleNodePath.getDatabaseRuleItem().getType()));
5959
String yamlContent = metaDataPersistFacade.getMetaDataVersionService().loadContent(new VersionNodePath(databaseRuleNodePath));
60-
String databaseName = databaseRuleNodePath.getDatabaseName();
60+
String databaseName = databaseRuleNodePath.getDatabase().getDatabaseName();
6161
RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(metaDataContexts.getMetaData().getDatabase(databaseName));
6262
String itemName = databaseRuleNodePath.getDatabaseRuleItem().getName();
6363
synchronized (this) {
@@ -74,7 +74,7 @@ public void alter(final DatabaseRuleNodePath databaseRuleNodePath) throws SQLExc
7474
*/
7575
@SuppressWarnings({"rawtypes", "unchecked"})
7676
public void drop(final DatabaseRuleNodePath databaseRuleNodePath) throws SQLException {
77-
String databaseName = databaseRuleNodePath.getDatabaseName();
77+
String databaseName = databaseRuleNodePath.getDatabase().getDatabaseName();
7878
Preconditions.checkState(metaDataContexts.getMetaData().containsDatabase(databaseName), "No database '%s' exists.", databaseName);
7979
RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class,
8080
new RuleChangedItemType(databaseRuleNodePath.getRuleType(), databaseRuleNodePath.getDatabaseRuleItem().getType()));

mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/metadata/service/DatabaseMetaDataPersistService.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import lombok.RequiredArgsConstructor;
2121
import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
22-
import org.apache.shardingsphere.mode.node.path.type.database.metadata.schema.TableMetadataNodePath;
22+
import org.apache.shardingsphere.mode.node.path.type.database.metadata.DatabaseMetaDataNodePath;
2323
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
2424

2525
import java.util.Collection;
@@ -38,7 +38,7 @@ public final class DatabaseMetaDataPersistService {
3838
* @param databaseName to be added database name
3939
*/
4040
public void add(final String databaseName) {
41-
repository.persist(NodePathGenerator.toPath(new TableMetadataNodePath(databaseName, null, null), true), "");
41+
repository.persist(NodePathGenerator.toPath(new DatabaseMetaDataNodePath(databaseName), false), "");
4242
}
4343

4444
/**
@@ -47,7 +47,7 @@ public void add(final String databaseName) {
4747
* @param databaseName to be dropped database name
4848
*/
4949
public void drop(final String databaseName) {
50-
repository.delete(NodePathGenerator.toPath(new TableMetadataNodePath(databaseName, null, null), true));
50+
repository.delete(NodePathGenerator.toPath(new DatabaseMetaDataNodePath(databaseName), false));
5151
}
5252

5353
/**
@@ -56,6 +56,6 @@ public void drop(final String databaseName) {
5656
* @return loaded database names
5757
*/
5858
public Collection<String> loadAllDatabaseNames() {
59-
return repository.getChildrenKeys(NodePathGenerator.toPath(new TableMetadataNodePath(null, null, null), false));
59+
return repository.getChildrenKeys(NodePathGenerator.toPath(new DatabaseMetaDataNodePath(null), false));
6060
}
6161
}

mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/metadata/service/SchemaMetaDataPersistService.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
2222
import org.apache.shardingsphere.mode.metadata.persist.version.VersionPersistService;
2323
import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
24+
import org.apache.shardingsphere.mode.node.path.type.database.metadata.schema.SchemaMetadataNodePath;
2425
import org.apache.shardingsphere.mode.node.path.type.database.metadata.schema.TableMetadataNodePath;
2526
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
2627

@@ -62,7 +63,7 @@ public void add(final String databaseName, final String schemaName) {
6263
* @param schemaName to be dropped schema name
6364
*/
6465
public void drop(final String databaseName, final String schemaName) {
65-
repository.delete(NodePathGenerator.toPath(new TableMetadataNodePath(databaseName, schemaName, null), true));
66+
repository.delete(NodePathGenerator.toPath(new SchemaMetadataNodePath(databaseName, schemaName), false));
6667
}
6768

6869
/**
@@ -112,7 +113,7 @@ public void alterByRuleDropped(final String databaseName, final ShardingSphereSc
112113
* @return schemas
113114
*/
114115
public Collection<ShardingSphereSchema> load(final String databaseName) {
115-
return repository.getChildrenKeys(NodePathGenerator.toPath(new TableMetadataNodePath(databaseName, null, null), false)).stream()
116+
return repository.getChildrenKeys(NodePathGenerator.toPath(new SchemaMetadataNodePath(databaseName, null), false)).stream()
116117
.map(each -> new ShardingSphereSchema(each, tableMetaDataPersistService.load(databaseName, each), viewMetaDataPersistService.load(databaseName, each))).collect(Collectors.toList());
117118
}
118119
}

mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/metadata/service/TableRowDataPersistService.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlRowStatisticsSwapper;
2727
import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
2828
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsDataNodePath;
29+
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsTableNodePath;
2930
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
3031

3132
import java.util.ArrayList;
@@ -49,7 +50,7 @@ public final class TableRowDataPersistService {
4950
*/
5051
public void persist(final String databaseName, final String schemaName, final String tableName, final Collection<YamlRowStatistics> rows) {
5152
if (rows.isEmpty()) {
52-
repository.persist(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, tableName.toLowerCase(), null), false), "");
53+
repository.persist(NodePathGenerator.toPath(new StatisticsTableNodePath(databaseName, schemaName, tableName.toLowerCase()), false), "");
5354
} else {
5455
rows.forEach(each -> repository.persist(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey()), false),
5556
YamlEngine.marshal(each)));
@@ -79,7 +80,7 @@ public void delete(final String databaseName, final String schemaName, final Str
7980
public TableStatistics load(final String databaseName, final String schemaName, final ShardingSphereTable table) {
8081
TableStatistics result = new TableStatistics(table.getName());
8182
YamlRowStatisticsSwapper swapper = new YamlRowStatisticsSwapper(new ArrayList<>(table.getAllColumns()));
82-
for (String each : repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, table.getName(), null), false))) {
83+
for (String each : repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsTableNodePath(databaseName, schemaName, table.getName()), false))) {
8384
String yamlRow = repository.query(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, table.getName(), each), false));
8485
if (!Strings.isNullOrEmpty(yamlRow)) {
8586
result.getRows().add(swapper.swapToObject(YamlEngine.unmarshal(yamlRow, YamlRowStatistics.class)));

mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/statistics/StatisticsPersistService.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlRowStatisticsSwapper;
2929
import org.apache.shardingsphere.mode.metadata.persist.metadata.service.TableRowDataPersistService;
3030
import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
31-
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsDataNodePath;
31+
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsDatabaseNodePath;
32+
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsSchemaNodePath;
33+
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsTableNodePath;
3234
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
3335

3436
import java.util.ArrayList;
@@ -57,7 +59,7 @@ public StatisticsPersistService(final PersistRepository repository) {
5759
* @return statistics
5860
*/
5961
public ShardingSphereStatistics load(final ShardingSphereMetaData metaData) {
60-
Collection<String> databaseNames = repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsDataNodePath(null, null, null, null), false));
62+
Collection<String> databaseNames = repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsDatabaseNodePath(null), false));
6163
if (databaseNames.isEmpty()) {
6264
return new ShardingSphereStatistics();
6365
}
@@ -70,7 +72,7 @@ public ShardingSphereStatistics load(final ShardingSphereMetaData metaData) {
7072

7173
private DatabaseStatistics load(final ShardingSphereDatabase database) {
7274
DatabaseStatistics result = new DatabaseStatistics();
73-
for (String each : repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsDataNodePath(database.getName(), null, null, null), false)).stream()
75+
for (String each : repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsSchemaNodePath(database.getName(), null), false)).stream()
7476
.filter(database::containsSchema).collect(Collectors.toList())) {
7577
result.putSchemaStatistics(each, load(database.getName(), database.getSchema(each)));
7678
}
@@ -79,7 +81,7 @@ private DatabaseStatistics load(final ShardingSphereDatabase database) {
7981

8082
private SchemaStatistics load(final String databaseName, final ShardingSphereSchema schema) {
8183
SchemaStatistics result = new SchemaStatistics();
82-
for (String each : repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsDataNodePath("foo_db", "foo_schema", null, null), false)).stream()
84+
for (String each : repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsTableNodePath(databaseName, schema.getName(), null), false)).stream()
8385
.filter(schema::containsTable).collect(Collectors.toList())) {
8486
result.putTableStatistics(each, tableRowDataPersistService.load(databaseName, schema.getName(), schema.getTable(each)));
8587

@@ -102,7 +104,7 @@ public void persist(final ShardingSphereDatabase database, final String schemaNa
102104
}
103105

104106
private void persistSchema(final String databaseName, final String schemaName) {
105-
repository.persist(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, null, null), true), "");
107+
repository.persist(NodePathGenerator.toPath(new StatisticsSchemaNodePath(databaseName, schemaName), false), "");
106108
}
107109

108110
private void persistTableData(final ShardingSphereDatabase database, final String schemaName, final SchemaStatistics schemaStatistics) {
@@ -139,6 +141,6 @@ public void update(final AlteredDatabaseStatistics alteredDatabaseStatistics) {
139141
* @param databaseName database name
140142
*/
141143
public void delete(final String databaseName) {
142-
repository.delete(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, null, null, null), true));
144+
repository.delete(NodePathGenerator.toPath(new StatisticsDatabaseNodePath(databaseName), false));
143145
}
144146
}

mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/engine/generator/NodePathGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static String toPath(final NodePath nodePath, final boolean trimEmptyNode
4545
String templatePath = Objects.requireNonNull(nodePath.getClass().getAnnotation(NodePathEntity.class), "NodePathEntity annotation is missing").value();
4646
LinkedList<String> nodeSegments = new LinkedList<>();
4747
for (String each : templatePath.split(PATH_DELIMITER)) {
48-
Optional<String> segmentLiteral = new NodePathSegment(each).getLiteral(nodePath);
48+
Optional<String> segmentLiteral = new NodePathSegment(each, trimEmptyNode).getLiteral(nodePath);
4949
if (segmentLiteral.isPresent()) {
5050
nodeSegments.add(segmentLiteral.get());
5151
continue;

mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/engine/generator/NodePathSegment.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public final class NodePathSegment {
3131

3232
private final String input;
3333

34+
private final boolean trimEmptyNode;
35+
3436
/**
3537
* Get segment literal.
3638
*
@@ -41,7 +43,13 @@ public Optional<String> getLiteral(final NodePath nodePath) {
4143
Optional<String> variableName = new NodePathVariable(input).findVariableName();
4244
if (variableName.isPresent()) {
4345
Object variableValue = ReflectionUtils.getFieldValue(nodePath, variableName.get()).orElse(null);
44-
return null == variableValue ? Optional.empty() : Optional.of(variableValue.toString());
46+
if (null == variableValue) {
47+
return Optional.empty();
48+
}
49+
if (variableValue instanceof NodePath) {
50+
return Optional.of(NodePathGenerator.toPath((NodePath) variableValue, trimEmptyNode));
51+
}
52+
return Optional.of(variableValue.toString());
4553
}
4654
return Optional.of(input);
4755
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.mode.node.path.type.database.metadata;
19+
20+
import lombok.Getter;
21+
import lombok.RequiredArgsConstructor;
22+
import org.apache.shardingsphere.mode.node.path.NodePath;
23+
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
24+
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
25+
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;
26+
27+
/**
28+
* Database metadata node path.
29+
*/
30+
@NodePathEntity("/metadata/${databaseName}")
31+
@RequiredArgsConstructor
32+
@Getter
33+
public final class DatabaseMetaDataNodePath implements NodePath {
34+
35+
private final String databaseName;
36+
37+
/**
38+
* Create database search criteria.
39+
*
40+
* @return created search criteria
41+
*/
42+
public static NodePathSearchCriteria createDatabaseSearchCriteria() {
43+
return new NodePathSearchCriteria(new DatabaseMetaDataNodePath(NodePathPattern.IDENTIFIER), false, true, 1);
44+
}
45+
}

mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/database/metadata/datasource/StorageNodeNodePath.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,28 @@
1818
package org.apache.shardingsphere.mode.node.path.type.database.metadata.datasource;
1919

2020
import lombok.Getter;
21-
import lombok.RequiredArgsConstructor;
2221
import org.apache.shardingsphere.mode.node.path.NodePath;
2322
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
2423
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
2524
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;
25+
import org.apache.shardingsphere.mode.node.path.type.database.metadata.DatabaseMetaDataNodePath;
2626

2727
/**
2828
* Storage node node path.
2929
*/
30-
@NodePathEntity("/metadata/${databaseName}/data_sources/nodes/${storageNodeName}")
31-
@RequiredArgsConstructor
30+
@NodePathEntity("${database}/data_sources/nodes/${storageNodeName}")
3231
@Getter
3332
public final class StorageNodeNodePath implements NodePath {
3433

35-
private final String databaseName;
34+
private final DatabaseMetaDataNodePath database;
3635

3736
private final String storageNodeName;
3837

38+
public StorageNodeNodePath(final String databaseName, final String storageNodeName) {
39+
database = new DatabaseMetaDataNodePath(databaseName);
40+
this.storageNodeName = storageNodeName;
41+
}
42+
3943
/**
4044
* Create storage node search criteria.
4145
*

mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/database/metadata/datasource/StorageUnitNodePath.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,28 @@
1818
package org.apache.shardingsphere.mode.node.path.type.database.metadata.datasource;
1919

2020
import lombok.Getter;
21-
import lombok.RequiredArgsConstructor;
2221
import org.apache.shardingsphere.mode.node.path.NodePath;
2322
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
2423
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
2524
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;
25+
import org.apache.shardingsphere.mode.node.path.type.database.metadata.DatabaseMetaDataNodePath;
2626

2727
/**
2828
* Storage unit node path.
2929
*/
30-
@NodePathEntity("/metadata/${databaseName}/data_sources/units/${storageUnitName}")
31-
@RequiredArgsConstructor
30+
@NodePathEntity("${database}/data_sources/units/${storageUnitName}")
3231
@Getter
3332
public final class StorageUnitNodePath implements NodePath {
3433

35-
private final String databaseName;
34+
private final DatabaseMetaDataNodePath database;
3635

3736
private final String storageUnitName;
3837

38+
public StorageUnitNodePath(final String databaseName, final String storageUnitName) {
39+
database = new DatabaseMetaDataNodePath(databaseName);
40+
this.storageUnitName = storageUnitName;
41+
}
42+
3943
/**
4044
* Create storage unit search criteria.
4145
*

mode/node/src/main/java/org/apache/shardingsphere/mode/node/path/type/database/metadata/rule/DatabaseRuleNodePath.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,31 @@
1818
package org.apache.shardingsphere.mode.node.path.type.database.metadata.rule;
1919

2020
import lombok.Getter;
21-
import lombok.RequiredArgsConstructor;
2221
import org.apache.shardingsphere.mode.node.path.NodePath;
2322
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
2423
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
2524
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;
25+
import org.apache.shardingsphere.mode.node.path.type.database.metadata.DatabaseMetaDataNodePath;
2626

2727
/**
2828
* Database rule node path.
2929
*/
30-
@NodePathEntity("/metadata/${databaseName}/rules/${ruleType}/${databaseRuleItem}")
31-
@RequiredArgsConstructor
30+
@NodePathEntity("${database}/rules/${ruleType}/${databaseRuleItem}")
3231
@Getter
3332
public final class DatabaseRuleNodePath implements NodePath {
3433

35-
private final String databaseName;
34+
private final DatabaseMetaDataNodePath database;
3635

3736
private final String ruleType;
3837

3938
private final DatabaseRuleItem databaseRuleItem;
4039

40+
public DatabaseRuleNodePath(final String databaseName, final String ruleType, final DatabaseRuleItem databaseRuleItem) {
41+
database = new DatabaseMetaDataNodePath(databaseName);
42+
this.ruleType = ruleType;
43+
this.databaseRuleItem = databaseRuleItem;
44+
}
45+
4146
/**
4247
* Create rule type search criteria.
4348
*

0 commit comments

Comments
 (0)