Skip to content

Commit

Permalink
Refactor NodePath (#34698)
Browse files Browse the repository at this point in the history
* Refactor NodePath

* Refactor NodePath
  • Loading branch information
terrymanu authored Feb 16, 2025
1 parent fa91999 commit d55f244
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.shardingsphere.mode.node.path.NodePath;

/**
* Database node path generator.
* Database node path.
*/
public final class DatabaseNodePath implements NodePath {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.metadata.DatabaseNodePath;

/**
Expand All @@ -32,8 +33,14 @@ public final class SchemaNodePath implements NodePath {

private final String databaseName;

private final NodePathGenerator nodePathGenerator = new NodePathGenerator(new DatabaseNodePath());

public SchemaNodePath() {
this(NodePathPattern.IDENTIFIER);
}

@Override
public String getRootPath() {
return String.join("/", new NodePathGenerator(new DatabaseNodePath()).getPath(databaseName), SCHEMAS_NODE);
return String.join("/", nodePathGenerator.getPath(databaseName), SCHEMAS_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class SchemaNodePathParser {
*/
public static Optional<String> findSchemaName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(new NodePathGenerator(new SchemaNodePath(NodePathPattern.IDENTIFIER)).getPath(NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(new NodePathGenerator(new SchemaNodePath()).getPath(NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;

/**
* Table node path.
Expand All @@ -27,17 +28,21 @@ public final class TableNodePath implements NodePath {

private static final String TABLES_NODE = "tables";

private final SchemaNodePath schemaNodePath;

private final String schemaName;

private final NodePathGenerator nodePathGenerator;

public TableNodePath() {
this(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER);
}

public TableNodePath(final String databaseName, final String schemaName) {
schemaNodePath = new SchemaNodePath(databaseName);
this.schemaName = schemaName;
nodePathGenerator = new NodePathGenerator(new SchemaNodePath(databaseName));
}

@Override
public String getRootPath() {
return String.join("/", new NodePathGenerator(schemaNodePath).getPath(schemaName), TABLES_NODE);
return String.join("/", nodePathGenerator.getPath(schemaName), TABLES_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TableNodePathParser {

private static final Pattern PATTERN = Pattern.compile(
new NodePathGenerator(new TableNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER)).getPath(NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);
private static final Pattern PATTERN = Pattern.compile(new NodePathGenerator(new TableNodePath()).getPath(NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);

private static final VersionNodePathParser VERSION_PARSER = new VersionNodePathParser(
new NodePathGenerator(new TableNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER)).getPath(NodePathPattern.IDENTIFIER));
private static final VersionNodePathParser VERSION_PARSER = new VersionNodePathParser(new NodePathGenerator(new TableNodePath()).getPath(NodePathPattern.IDENTIFIER));

/**
* Find table name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;

/**
* View path.
Expand All @@ -27,17 +28,21 @@ public final class ViewNodePath implements NodePath {

private static final String VIEWS_NODE = "views";

private final SchemaNodePath schemaNodePath;

private final String schemaName;

private final NodePathGenerator nodePathGenerator;

public ViewNodePath() {
this(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER);
}

public ViewNodePath(final String databaseName, final String schemaName) {
schemaNodePath = new SchemaNodePath(databaseName);
this.schemaName = schemaName;
nodePathGenerator = new NodePathGenerator(new SchemaNodePath(databaseName));
}

@Override
public String getRootPath() {
return String.join("/", new NodePathGenerator(schemaNodePath).getPath(schemaName), VIEWS_NODE);
return String.join("/", nodePathGenerator.getPath(schemaName), VIEWS_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ViewNodePathParser {

private static final Pattern PATTERN = Pattern.compile(
new NodePathGenerator(new ViewNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER)).getPath(NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);
private static final Pattern PATTERN = Pattern.compile(new NodePathGenerator(new ViewNodePath()).getPath(NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);

private static final VersionNodePathParser VERSION_PARSER = new VersionNodePathParser(
new NodePathGenerator(new ViewNodePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER)).getPath(NodePathPattern.IDENTIFIER));
private static final VersionNodePathParser VERSION_PARSER = new VersionNodePathParser(new NodePathGenerator(new ViewNodePath()).getPath(NodePathPattern.IDENTIFIER));

/**
* Get view name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
*/
public final class DatabaseRuleItemNodePath implements NodePath {

private final DatabaseRuleNodePath databaseRuleNodePath;

private final String ruleType;

private final NodePathGenerator nodePathGenerator;

public DatabaseRuleItemNodePath(final String databaseName, final String ruleType) {
databaseRuleNodePath = new DatabaseRuleNodePath(databaseName);
this.ruleType = ruleType;
nodePathGenerator = new NodePathGenerator(new DatabaseRuleNodePath(databaseName));
}

@Override
public String getRootPath() {
return new NodePathGenerator(databaseRuleNodePath).getPath(ruleType);
return nodePathGenerator.getPath(ruleType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.metadata.DatabaseNodePath;

/**
* Database rule node path generator.
* Database rule node path.
*/
@RequiredArgsConstructor
public final class DatabaseRuleNodePath implements NodePath {
Expand All @@ -31,8 +32,10 @@ public final class DatabaseRuleNodePath implements NodePath {

private final String databaseName;

private final NodePathGenerator nodePathGenerator = new NodePathGenerator(new DatabaseNodePath());

@Override
public String getRootPath() {
return String.join("/", new DatabaseNodePath().getRootPath(), databaseName, RULE_NODE);
return String.join("/", nodePathGenerator.getPath(databaseName), RULE_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.metadata.DatabaseNodePath;

/**
Expand All @@ -31,8 +33,14 @@ public final class DataSourceNodePath implements NodePath {

private final String databaseName;

private final NodePathGenerator nodePathGenerator = new NodePathGenerator(new DatabaseNodePath());

public DataSourceNodePath() {
this(NodePathPattern.IDENTIFIER);
}

@Override
public String getRootPath() {
return String.join("/", new DatabaseNodePath().getRootPath(), databaseName, DATA_SOURCES_NODE);
return String.join("/", nodePathGenerator.getPath(databaseName), DATA_SOURCES_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;

import java.util.regex.Pattern;

Expand All @@ -29,7 +28,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DataSourceNodePathParser {

private static final Pattern PATTERN = Pattern.compile(new DataSourceNodePath(NodePathPattern.IDENTIFIER).getRootPath() + "?", Pattern.CASE_INSENSITIVE);
private static final Pattern PATTERN = Pattern.compile(new DataSourceNodePath().getRootPath() + "?", Pattern.CASE_INSENSITIVE);

/**
* Is data source path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@

package org.apache.shardingsphere.mode.node.path.metadata.storage;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;

/**
* Storage node node path.
*/
@RequiredArgsConstructor
public final class StorageNodeNodePath implements NodePath {

private static final String NODES_NODE = "nodes";

private final String databaseName;
private final NodePathGenerator nodePathGenerator;

public StorageNodeNodePath() {
this(NodePathPattern.IDENTIFIER);
}

public StorageNodeNodePath(final String databaseName) {
nodePathGenerator = new NodePathGenerator(new DataSourceNodePath(databaseName));
}

@Override
public String getRootPath() {
return String.join("/", new DataSourceNodePath(databaseName).getRootPath(), NODES_NODE);
return nodePathGenerator.getPath(NODES_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StorageNodeNodePathParser {

private static final Pattern PATTERN = Pattern.compile(
new NodePathGenerator(new StorageNodeNodePath(NodePathPattern.IDENTIFIER)).getPath(NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);
private static final Pattern PATTERN = Pattern.compile(new NodePathGenerator(new StorageNodeNodePath()).getPath(NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);

private static final VersionNodePathParser VERSION_PARSER =
new VersionNodePathParser(String.join("/", new StorageNodeNodePath(NodePathPattern.IDENTIFIER).getRootPath(), NodePathPattern.IDENTIFIER));
private static final VersionNodePathParser VERSION_PARSER = new VersionNodePathParser(String.join("/", new StorageNodeNodePath().getRootPath(), NodePathPattern.IDENTIFIER));

/**
* Find storage node name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;

/**
* Storage unit node path.
Expand All @@ -28,10 +30,18 @@ public final class StorageUnitNodePath implements NodePath {

private static final String UNITS_NODE = "units";

private final String databaseName;
private final NodePathGenerator nodePathGenerator;

public StorageUnitNodePath() {
this(NodePathPattern.IDENTIFIER);
}

public StorageUnitNodePath(final String databaseName) {
nodePathGenerator = new NodePathGenerator(new DataSourceNodePath(databaseName));
}

@Override
public String getRootPath() {
return String.join("/", new DataSourceNodePath(databaseName).getRootPath(), UNITS_NODE);
return nodePathGenerator.getPath(UNITS_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StorageUnitNodePathParser {

private static final Pattern PATTERN = Pattern.compile(
new NodePathGenerator(new StorageUnitNodePath(NodePathPattern.IDENTIFIER)).getPath(NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);
private static final Pattern PATTERN = Pattern.compile(new NodePathGenerator(new StorageUnitNodePath()).getPath(NodePathPattern.IDENTIFIER) + "$", Pattern.CASE_INSENSITIVE);

private static final VersionNodePathParser VERSION_PARSER = new VersionNodePathParser(
String.join("/", new StorageUnitNodePath(NodePathPattern.IDENTIFIER).getRootPath(), NodePathPattern.IDENTIFIER));
private static final VersionNodePathParser VERSION_PARSER = new VersionNodePathParser(String.join("/", new StorageUnitNodePath().getRootPath(), NodePathPattern.IDENTIFIER));

/**
* Find storage unit name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ class DatabaseRuleNodePathTest {
void assertGetRootPath() {
assertThat(new DatabaseRuleNodePath("foo_db").getRootPath(), is("/metadata/foo_db/rules"));
}

}

0 comments on commit d55f244

Please sign in to comment.