Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hierarchical layer to process NodePath #34963

Merged
merged 12 commits into from
Mar 13, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void alter(final DatabaseRuleNodePath databaseRuleNodePath) throws SQLExc
RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class,
new RuleChangedItemType(databaseRuleNodePath.getRuleType(), databaseRuleNodePath.getDatabaseRuleItem().getType()));
String yamlContent = metaDataPersistFacade.getMetaDataVersionService().loadContent(new VersionNodePath(databaseRuleNodePath));
String databaseName = databaseRuleNodePath.getDatabaseName();
String databaseName = databaseRuleNodePath.getDatabase().getDatabaseName();
RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(metaDataContexts.getMetaData().getDatabase(databaseName));
String itemName = databaseRuleNodePath.getDatabaseRuleItem().getName();
synchronized (this) {
Expand All @@ -74,7 +74,7 @@ public void alter(final DatabaseRuleNodePath databaseRuleNodePath) throws SQLExc
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public void drop(final DatabaseRuleNodePath databaseRuleNodePath) throws SQLException {
String databaseName = databaseRuleNodePath.getDatabaseName();
String databaseName = databaseRuleNodePath.getDatabase().getDatabaseName();
Preconditions.checkState(metaDataContexts.getMetaData().containsDatabase(databaseName), "No database '%s' exists.", databaseName);
RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class,
new RuleChangedItemType(databaseRuleNodePath.getRuleType(), databaseRuleNodePath.getDatabaseRuleItem().getType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.type.database.metadata.schema.TableMetadataNodePath;
import org.apache.shardingsphere.mode.node.path.type.database.metadata.DatabaseMetaDataNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

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

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

/**
Expand All @@ -56,6 +56,6 @@ public void drop(final String databaseName) {
* @return loaded database names
*/
public Collection<String> loadAllDatabaseNames() {
return repository.getChildrenKeys(NodePathGenerator.toPath(new TableMetadataNodePath(null, null, null), false));
return repository.getChildrenKeys(NodePathGenerator.toPath(new DatabaseMetaDataNodePath(null), false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.mode.metadata.persist.version.VersionPersistService;
import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.type.database.metadata.schema.SchemaMetadataNodePath;
import org.apache.shardingsphere.mode.node.path.type.database.metadata.schema.TableMetadataNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

Expand Down Expand Up @@ -62,7 +63,7 @@ public void add(final String databaseName, final String schemaName) {
* @param schemaName to be dropped schema name
*/
public void drop(final String databaseName, final String schemaName) {
repository.delete(NodePathGenerator.toPath(new TableMetadataNodePath(databaseName, schemaName, null), true));
repository.delete(NodePathGenerator.toPath(new SchemaMetadataNodePath(databaseName, schemaName), false));
}

/**
Expand Down Expand Up @@ -112,7 +113,7 @@ public void alterByRuleDropped(final String databaseName, final ShardingSphereSc
* @return schemas
*/
public Collection<ShardingSphereSchema> load(final String databaseName) {
return repository.getChildrenKeys(NodePathGenerator.toPath(new TableMetadataNodePath(databaseName, null, null), false)).stream()
return repository.getChildrenKeys(NodePathGenerator.toPath(new SchemaMetadataNodePath(databaseName, null), false)).stream()
.map(each -> new ShardingSphereSchema(each, tableMetaDataPersistService.load(databaseName, each), viewMetaDataPersistService.load(databaseName, each))).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlRowStatisticsSwapper;
import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsDataNodePath;
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsTableNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.ArrayList;
Expand All @@ -49,7 +50,7 @@ public final class TableRowDataPersistService {
*/
public void persist(final String databaseName, final String schemaName, final String tableName, final Collection<YamlRowStatistics> rows) {
if (rows.isEmpty()) {
repository.persist(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, tableName.toLowerCase(), null), false), "");
repository.persist(NodePathGenerator.toPath(new StatisticsTableNodePath(databaseName, schemaName, tableName.toLowerCase()), false), "");
} else {
rows.forEach(each -> repository.persist(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey()), false),
YamlEngine.marshal(each)));
Expand Down Expand Up @@ -79,7 +80,7 @@ public void delete(final String databaseName, final String schemaName, final Str
public TableStatistics load(final String databaseName, final String schemaName, final ShardingSphereTable table) {
TableStatistics result = new TableStatistics(table.getName());
YamlRowStatisticsSwapper swapper = new YamlRowStatisticsSwapper(new ArrayList<>(table.getAllColumns()));
for (String each : repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, table.getName(), null), false))) {
for (String each : repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsTableNodePath(databaseName, schemaName, table.getName()), false))) {
String yamlRow = repository.query(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, table.getName(), each), false));
if (!Strings.isNullOrEmpty(yamlRow)) {
result.getRows().add(swapper.swapToObject(YamlEngine.unmarshal(yamlRow, YamlRowStatistics.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlRowStatisticsSwapper;
import org.apache.shardingsphere.mode.metadata.persist.metadata.service.TableRowDataPersistService;
import org.apache.shardingsphere.mode.node.path.engine.generator.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsDataNodePath;
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsDatabaseNodePath;
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsSchemaNodePath;
import org.apache.shardingsphere.mode.node.path.type.database.statistics.StatisticsTableNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.ArrayList;
Expand Down Expand Up @@ -57,7 +59,7 @@ public StatisticsPersistService(final PersistRepository repository) {
* @return statistics
*/
public ShardingSphereStatistics load(final ShardingSphereMetaData metaData) {
Collection<String> databaseNames = repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsDataNodePath(null, null, null, null), false));
Collection<String> databaseNames = repository.getChildrenKeys(NodePathGenerator.toPath(new StatisticsDatabaseNodePath(null), false));
if (databaseNames.isEmpty()) {
return new ShardingSphereStatistics();
}
Expand All @@ -70,7 +72,7 @@ public ShardingSphereStatistics load(final ShardingSphereMetaData metaData) {

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

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

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

private void persistSchema(final String databaseName, final String schemaName) {
repository.persist(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, schemaName, null, null), true), "");
repository.persist(NodePathGenerator.toPath(new StatisticsSchemaNodePath(databaseName, schemaName), false), "");
}

private void persistTableData(final ShardingSphereDatabase database, final String schemaName, final SchemaStatistics schemaStatistics) {
Expand Down Expand Up @@ -139,6 +141,6 @@ public void update(final AlteredDatabaseStatistics alteredDatabaseStatistics) {
* @param databaseName database name
*/
public void delete(final String databaseName) {
repository.delete(NodePathGenerator.toPath(new StatisticsDataNodePath(databaseName, null, null, null), true));
repository.delete(NodePathGenerator.toPath(new StatisticsDatabaseNodePath(databaseName), false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static String toPath(final NodePath nodePath, final boolean trimEmptyNode
String templatePath = Objects.requireNonNull(nodePath.getClass().getAnnotation(NodePathEntity.class), "NodePathEntity annotation is missing").value();
LinkedList<String> nodeSegments = new LinkedList<>();
for (String each : templatePath.split(PATH_DELIMITER)) {
Optional<String> segmentLiteral = new NodePathSegment(each).getLiteral(nodePath);
Optional<String> segmentLiteral = new NodePathSegment(each, trimEmptyNode).getLiteral(nodePath);
if (segmentLiteral.isPresent()) {
nodeSegments.add(segmentLiteral.get());
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public final class NodePathSegment {

private final String input;

private final boolean trimEmptyNode;

/**
* Get segment literal.
*
Expand All @@ -41,7 +43,13 @@ public Optional<String> getLiteral(final NodePath nodePath) {
Optional<String> variableName = new NodePathVariable(input).findVariableName();
if (variableName.isPresent()) {
Object variableValue = ReflectionUtils.getFieldValue(nodePath, variableName.get()).orElse(null);
return null == variableValue ? Optional.empty() : Optional.of(variableValue.toString());
if (null == variableValue) {
return Optional.empty();
}
if (variableValue instanceof NodePath) {
return Optional.of(NodePathGenerator.toPath((NodePath) variableValue, trimEmptyNode));
}
return Optional.of(variableValue.toString());
}
return Optional.of(input);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.database.metadata;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;

/**
* Database metadata node path.
*/
@NodePathEntity("/metadata/${databaseName}")
@RequiredArgsConstructor
@Getter
public final class DatabaseMetaDataNodePath implements NodePath {

private final String databaseName;

/**
* Create database search criteria.
*
* @return created search criteria
*/
public static NodePathSearchCriteria createDatabaseSearchCriteria() {
return new NodePathSearchCriteria(new DatabaseMetaDataNodePath(NodePathPattern.IDENTIFIER), false, true, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@
package org.apache.shardingsphere.mode.node.path.type.database.metadata.datasource;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;
import org.apache.shardingsphere.mode.node.path.type.database.metadata.DatabaseMetaDataNodePath;

/**
* Storage node node path.
*/
@NodePathEntity("/metadata/${databaseName}/data_sources/nodes/${storageNodeName}")
@RequiredArgsConstructor
@NodePathEntity("${database}/data_sources/nodes/${storageNodeName}")
@Getter
public final class StorageNodeNodePath implements NodePath {

private final String databaseName;
private final DatabaseMetaDataNodePath database;

private final String storageNodeName;

public StorageNodeNodePath(final String databaseName, final String storageNodeName) {
database = new DatabaseMetaDataNodePath(databaseName);
this.storageNodeName = storageNodeName;
}

/**
* Create storage node search criteria.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@
package org.apache.shardingsphere.mode.node.path.type.database.metadata.datasource;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;
import org.apache.shardingsphere.mode.node.path.type.database.metadata.DatabaseMetaDataNodePath;

/**
* Storage unit node path.
*/
@NodePathEntity("/metadata/${databaseName}/data_sources/units/${storageUnitName}")
@RequiredArgsConstructor
@NodePathEntity("${database}/data_sources/units/${storageUnitName}")
@Getter
public final class StorageUnitNodePath implements NodePath {

private final String databaseName;
private final DatabaseMetaDataNodePath database;

private final String storageUnitName;

public StorageUnitNodePath(final String databaseName, final String storageUnitName) {
database = new DatabaseMetaDataNodePath(databaseName);
this.storageUnitName = storageUnitName;
}

/**
* Create storage unit search criteria.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,31 @@
package org.apache.shardingsphere.mode.node.path.type.database.metadata.rule;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathEntity;
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.engine.searcher.NodePathSearchCriteria;
import org.apache.shardingsphere.mode.node.path.type.database.metadata.DatabaseMetaDataNodePath;

/**
* Database rule node path.
*/
@NodePathEntity("/metadata/${databaseName}/rules/${ruleType}/${databaseRuleItem}")
@RequiredArgsConstructor
@NodePathEntity("${database}/rules/${ruleType}/${databaseRuleItem}")
@Getter
public final class DatabaseRuleNodePath implements NodePath {

private final String databaseName;
private final DatabaseMetaDataNodePath database;

private final String ruleType;

private final DatabaseRuleItem databaseRuleItem;

public DatabaseRuleNodePath(final String databaseName, final String ruleType, final DatabaseRuleItem databaseRuleItem) {
database = new DatabaseMetaDataNodePath(databaseName);
this.ruleType = ruleType;
this.databaseRuleItem = databaseRuleItem;
}

/**
* Create rule type search criteria.
*
Expand Down
Loading
Loading