Skip to content

Commit

Permalink
Refactor TableMetaDataNodePath and ViewMetaDataNodePath
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 7, 2025
1 parent 801a9dc commit 656131c
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
import java.util.regex.Pattern;

/**
* Table meta data node.
* Table meta data node path.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TableMetaDataNode {
public final class TableMetaDataNodePath {

private static final String ROOT_NODE = "metadata";
private static final String ROOT_NODE = "/metadata";

private static final String SCHEMAS_NODE = "schemas";

private static final String TABLES_NODE = "tables";

private static final String ACTIVE_VERSION = "active_version";
private static final String VERSIONS_NODE = "versions";

private static final String VERSIONS = "versions";
private static final String ACTIVE_VERSION_NODE = "active_version";

private static final String TABLES_PATTERN = "/([\\w\\-]+)/schemas/([\\w\\-]+)/tables";

Expand All @@ -47,110 +47,106 @@ public final class TableMetaDataNode {
private static final String TABLE_SUFFIX = "/([\\w\\-]+)$";

/**
* Get meta data tables node.
* Get meta data tables path.
*
* @param databaseName database name
* @param schemaName schema name
* @return tables path
*/
public static String getMetaDataTablesNode(final String databaseName, final String schemaName) {
return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE);
public static String getMetaDataTablesPath(final String databaseName, final String schemaName) {
return String.join("/", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE);
}

/**
* Get table active version node.
* Get table active version path.
*
* @param databaseName database name
* @param schemaName schema name
* @param tableName table name
* @return tables active version node
* @return tables active version path
*/
public static String getTableActiveVersionNode(final String databaseName, final String schemaName, final String tableName) {
return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE, tableName, ACTIVE_VERSION);
public static String getTableActiveVersionPath(final String databaseName, final String schemaName, final String tableName) {
return String.join("/", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE, tableName, ACTIVE_VERSION_NODE);
}

/**
* Get table versions node.
* Get table versions path.
*
* @param databaseName database name
* @param schemaName schema name
* @param tableName table name
* @return tables versions node
* @return tables versions path
*/
public static String getTableVersionsNode(final String databaseName, final String schemaName, final String tableName) {
return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE, tableName, VERSIONS);
public static String getTableVersionsPath(final String databaseName, final String schemaName, final String tableName) {
return String.join("/", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE, tableName, VERSIONS_NODE);
}

/**
* Get table version node.
* Get table version path.
*
* @param databaseName database name
* @param schemaName schema name
* @param tableName table name
* @param version version
* @return table version node
* @return table version path
*/
public static String getTableVersionNode(final String databaseName, final String schemaName, final String tableName, final String version) {
return String.join("/", getTableVersionsNode(databaseName, schemaName, tableName), version);
public static String getTableVersionPath(final String databaseName, final String schemaName, final String tableName, final String version) {
return String.join("/", getTableVersionsPath(databaseName, schemaName, tableName), version);
}

/**
* Get table node.
* Get table path.
*
* @param databaseName database name
* @param schemaName schema name
* @param tableName table name
* @return table node
* @return table path
*/
public static String getTableNode(final String databaseName, final String schemaName, final String tableName) {
return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE, tableName);
public static String getTablePath(final String databaseName, final String schemaName, final String tableName) {
return String.join("/", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, TABLES_NODE, tableName);
}

/**
* Get table name by active version node.
* Get table name by active version path.
*
* @param path path
* @return table name
*/
public static Optional<String> getTableNameByActiveVersionNode(final String path) {
Pattern pattern = Pattern.compile(getMetaDataNode() + TABLES_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE);
public static Optional<String> getTableNameByActiveVersionPath(final String path) {
Pattern pattern = Pattern.compile(ROOT_NODE + TABLES_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}

/**
* Get table name.
* Find table name.
*
* @param path path
* @return table name
* @return found table name
*/
public static Optional<String> getTableName(final String path) {
Pattern pattern = Pattern.compile(getMetaDataNode() + TABLES_PATTERN + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
public static Optional<String> findTableName(final String path) {
Pattern pattern = Pattern.compile(ROOT_NODE + TABLES_PATTERN + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}

/**
* Is table active version node.
* Is table active version path.
*
* @param path path
* @return true or false
*/
public static boolean isTableActiveVersionNode(final String path) {
return Pattern.compile(getMetaDataNode() + TABLES_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find();
public static boolean isTableActiveVersionPath(final String path) {
return Pattern.compile(ROOT_NODE + TABLES_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find();
}

/**
* Is table node.
* Is table path.
*
* @param path path
* @return true or false
*/
public static boolean isTableNode(final String path) {
return Pattern.compile(getMetaDataNode() + TABLES_PATTERN + TABLE_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find();
}

private static String getMetaDataNode() {
return String.join("/", "", ROOT_NODE);
public static boolean isTablePath(final String path) {
return Pattern.compile(ROOT_NODE + TABLES_PATTERN + TABLE_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
import java.util.regex.Pattern;

/**
* View meta data node.
* View meta data path.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ViewMetaDataNode {
public final class ViewMetaDataNodePath {

private static final String ROOT_NODE = "metadata";
private static final String ROOT_NODE = "/metadata";

private static final String SCHEMAS_NODE = "schemas";

private static final String VIEWS_NODE = "views";

private static final String ACTIVE_VERSION = "active_version";
private static final String VERSIONS_NODE = "versions";

private static final String VERSIONS = "versions";
private static final String ACTIVE_VERSION_NODE = "active_version";

private static final String VIEWS_PATTERN = "/([\\w\\-]+)/schemas/([\\w\\-]+)/views";

Expand All @@ -47,73 +47,73 @@ public final class ViewMetaDataNode {
private static final String VIEW_SUFFIX = "/([\\w\\-]+)$";

/**
* Get meta data views node.
* Get meta data views path.
*
* @param databaseName database name
* @param schemaName schema name
* @return views path
*/
public static String getMetaDataViewsNode(final String databaseName, final String schemaName) {
return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE);
public static String getMetaDataViewsPath(final String databaseName, final String schemaName) {
return String.join("/", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE);
}

/**
* Get view active version node.
* Get view active version path.
*
* @param databaseName database name
* @param schemaName schema name
* @param viewName view name
* @return view active version node
* @return view active version path
*/
public static String getViewActiveVersionNode(final String databaseName, final String schemaName, final String viewName) {
return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE, viewName, ACTIVE_VERSION);
public static String getViewActiveVersionPath(final String databaseName, final String schemaName, final String viewName) {
return String.join("/", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE, viewName, ACTIVE_VERSION_NODE);
}

/**
* Get view versions node.
* Get view versions path.
*
* @param databaseName database name
* @param schemaName schema name
* @param viewName view name
* @return view versions node
* @return view versions path
*/
public static String getViewVersionsNode(final String databaseName, final String schemaName, final String viewName) {
return String.join("/", getMetaDataNode(), databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE, viewName, VERSIONS);
public static String getViewVersionsPath(final String databaseName, final String schemaName, final String viewName) {
return String.join("/", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE, viewName, VERSIONS_NODE);
}

/**
* Get view version node.
* Get view version path.
*
* @param databaseName database name
* @param schemaName schema name
* @param viewName view name
* @param version version
* @return view version node
* @return view version path
*/
public static String getViewVersionNode(final String databaseName, final String schemaName, final String viewName, final String version) {
return String.join("/", getViewVersionsNode(databaseName, schemaName, viewName), version);
public static String getViewVersionPath(final String databaseName, final String schemaName, final String viewName, final String version) {
return String.join("/", getViewVersionsPath(databaseName, schemaName, viewName), version);
}

/**
* Get view node.
* Get view path.
*
* @param databaseName database name
* @param schemaName schema name
* @param viewName view name
* @return view node
* @return view path
*/
public static String getViewNode(final String databaseName, final String schemaName, final String viewName) {
return String.join("/", "", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE, viewName);
public static String getViewPath(final String databaseName, final String schemaName, final String viewName) {
return String.join("/", ROOT_NODE, databaseName, SCHEMAS_NODE, schemaName, VIEWS_NODE, viewName);
}

/**
* Get view name by active version node.
* Get view name by active version path.
*
* @param path path
* @return view name
*/
public static Optional<String> getViewNameByActiveVersionNode(final String path) {
Pattern pattern = Pattern.compile(getMetaDataNode() + VIEWS_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE);
public static Optional<String> getViewNameByActiveVersionPath(final String path) {
Pattern pattern = Pattern.compile(ROOT_NODE + VIEWS_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
Expand All @@ -124,33 +124,29 @@ public static Optional<String> getViewNameByActiveVersionNode(final String path)
* @param path path
* @return view name
*/
public static Optional<String> getViewName(final String path) {
Pattern pattern = Pattern.compile(getMetaDataNode() + VIEWS_PATTERN + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
public static Optional<String> findViewName(final String path) {
Pattern pattern = Pattern.compile(ROOT_NODE + VIEWS_PATTERN + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}

/**
* Is view active version node.
* Is view active version path.
*
* @param path path
* @return true or false
*/
public static boolean isViewActiveVersionNode(final String path) {
return Pattern.compile(getMetaDataNode() + VIEWS_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find();
public static boolean isViewActiveVersionPath(final String path) {
return Pattern.compile(ROOT_NODE + VIEWS_PATTERN + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find();
}

/**
* Is view node.
* Is view path.
*
* @param path path
* @return true or false
*/
public static boolean isViewNode(final String path) {
return Pattern.compile(getMetaDataNode() + VIEWS_PATTERN + VIEW_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find();
}

private static String getMetaDataNode() {
return String.join("/", "", ROOT_NODE);
public static boolean isViewPath(final String path) {
return Pattern.compile(ROOT_NODE + VIEWS_PATTERN + VIEW_SUFFIX, Pattern.CASE_INSENSITIVE).matcher(path).find();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable;
import org.apache.shardingsphere.infra.yaml.schema.swapper.YamlTableSwapper;
import org.apache.shardingsphere.metadata.persist.node.metadata.TableMetaDataNode;
import org.apache.shardingsphere.metadata.persist.node.metadata.TableMetaDataNodePath;
import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService;
import org.apache.shardingsphere.mode.spi.PersistRepository;

Expand All @@ -51,7 +51,7 @@ public final class TableMetaDataPersistService {
* @return loaded tables
*/
public Collection<ShardingSphereTable> load(final String databaseName, final String schemaName) {
return repository.getChildrenKeys(TableMetaDataNode.getMetaDataTablesNode(databaseName, schemaName)).stream().map(each -> load(databaseName, schemaName, each)).collect(Collectors.toList());
return repository.getChildrenKeys(TableMetaDataNodePath.getMetaDataTablesPath(databaseName, schemaName)).stream().map(each -> load(databaseName, schemaName, each)).collect(Collectors.toList());
}

/**
Expand All @@ -63,8 +63,8 @@ public Collection<ShardingSphereTable> load(final String databaseName, final Str
* @return loaded table
*/
public ShardingSphereTable load(final String databaseName, final String schemaName, final String tableName) {
String tableContent = repository.query(TableMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName,
repository.query(TableMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName))));
String tableContent = repository.query(TableMetaDataNodePath.getTableVersionPath(databaseName, schemaName, tableName,
repository.query(TableMetaDataNodePath.getTableActiveVersionPath(databaseName, schemaName, tableName))));
return new YamlTableSwapper().swapToObject(YamlEngine.unmarshal(tableContent, YamlShardingSphereTable.class));
}

Expand All @@ -79,20 +79,20 @@ public void persist(final String databaseName, final String schemaName, final Co
Collection<MetaDataVersion> metaDataVersions = new LinkedList<>();
for (ShardingSphereTable each : tables) {
String tableName = each.getName().toLowerCase();
List<String> versions = metaDataVersionPersistService.getVersions(TableMetaDataNode.getTableVersionsNode(databaseName, schemaName, tableName));
List<String> versions = metaDataVersionPersistService.getVersions(TableMetaDataNodePath.getTableVersionsPath(databaseName, schemaName, tableName));
String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
repository.persist(
TableMetaDataNode.getTableVersionNode(databaseName, schemaName, tableName, nextActiveVersion), YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(each)));
TableMetaDataNodePath.getTableVersionPath(databaseName, schemaName, tableName, nextActiveVersion), YamlEngine.marshal(new YamlTableSwapper().swapToYamlConfiguration(each)));
if (Strings.isNullOrEmpty(getActiveVersion(databaseName, schemaName, tableName))) {
repository.persist(TableMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName), MetaDataVersion.DEFAULT_VERSION);
repository.persist(TableMetaDataNodePath.getTableActiveVersionPath(databaseName, schemaName, tableName), MetaDataVersion.DEFAULT_VERSION);
}
metaDataVersions.add(new MetaDataVersion(TableMetaDataNode.getTableNode(databaseName, schemaName, tableName), getActiveVersion(databaseName, schemaName, tableName), nextActiveVersion));
metaDataVersions.add(new MetaDataVersion(TableMetaDataNodePath.getTablePath(databaseName, schemaName, tableName), getActiveVersion(databaseName, schemaName, tableName), nextActiveVersion));
}
metaDataVersionPersistService.switchActiveVersion(metaDataVersions);
}

private String getActiveVersion(final String databaseName, final String schemaName, final String tableName) {
return repository.query(TableMetaDataNode.getTableActiveVersionNode(databaseName, schemaName, tableName));
return repository.query(TableMetaDataNodePath.getTableActiveVersionPath(databaseName, schemaName, tableName));
}

/**
Expand All @@ -103,7 +103,7 @@ private String getActiveVersion(final String databaseName, final String schemaNa
* @param tableName to be dropped table name
*/
public void drop(final String databaseName, final String schemaName, final String tableName) {
repository.delete(TableMetaDataNode.getTableNode(databaseName, schemaName, tableName.toLowerCase()));
repository.delete(TableMetaDataNodePath.getTablePath(databaseName, schemaName, tableName.toLowerCase()));
}

/**
Expand Down
Loading

0 comments on commit 656131c

Please sign in to comment.