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

Refactor DatabaseMetaDataNode #34254

Merged
merged 5 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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.metadata.persist.node;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Database meta data node path.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DatabaseMetaDataNodePath {

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 = "versions";

private static final String IDENTIFIER_PATTERN = "([\\w\\-]+)";

/**
* Get meta data root path.
*
* @return meta data root path
*/
public static String getRootPath() {
return String.join("/", "", ROOT_NODE);
}

/**
* Get database path.
*
* @param databaseName database name
* @return database path
*/
public static String getDatabasePath(final String databaseName) {
return String.join("/", getRootPath(), databaseName);
}

/**
* Get schemas path.
*
* @param databaseName database name
* @return schemas path
*/
public static String getSchemasPath(final String databaseName) {
return String.join("/", getDatabasePath(databaseName), SCHEMAS_NODE);
}

/**
* Get schema path.
*
* @param databaseName database name
* @param schemaName schema name
* @return schema path
*/
public static String getSchemaPath(final String databaseName, final String schemaName) {
return String.join("/", getSchemasPath(databaseName), schemaName);
}

/**
* Get tables path.
*
* @param databaseName database name
* @param schemaName schema name
* @return tables path
*/
public static String getTablesPath(final String databaseName, final String schemaName) {
return String.join("/", getSchemaPath(databaseName, schemaName), TABLES_NODE);
}

/**
* Get version path.
*
* @param rulePath rule path
* @param activeVersion active version
* @return version path
*/
public static String getVersionPath(final String rulePath, final String activeVersion) {
return rulePath.replace(ACTIVE_VERSION, VERSIONS) + "/" + activeVersion;
}

/**
* Find database name.
*
* @param path path
* @param containsChildPath whether contains child path
* @return found database name
*/
public static Optional<String> findDatabaseName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(getDatabasePath(IDENTIFIER_PATTERN) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}

/**
* Find schema name.
*
* @param path path
* @param containsChildPath whether contains child path
* @return found schema name
*/
public static Optional<String> findSchemaName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(getSchemaPath(IDENTIFIER_PATTERN, IDENTIFIER_PATTERN) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.metadata.persist.service.metadata.database;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode;
import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNodePath;
import org.apache.shardingsphere.mode.spi.PersistRepository;

import java.util.Collection;
Expand All @@ -37,7 +37,7 @@ public final class DatabaseMetaDataPersistService {
* @param databaseName to be added database name
*/
public void add(final String databaseName) {
repository.persist(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), "");
repository.persist(DatabaseMetaDataNodePath.getDatabasePath(databaseName), "");
}

/**
Expand All @@ -46,7 +46,7 @@ public void add(final String databaseName) {
* @param databaseName to be dropped database name
*/
public void drop(final String databaseName) {
repository.delete(DatabaseMetaDataNode.getDatabaseNamePath(databaseName));
repository.delete(DatabaseMetaDataNodePath.getDatabasePath(databaseName));
}

/**
Expand All @@ -55,6 +55,6 @@ public void drop(final String databaseName) {
* @return loaded database names
*/
public Collection<String> loadAllDatabaseNames() {
return repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataNode());
return repository.getChildrenKeys(DatabaseMetaDataNodePath.getRootPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.apache.shardingsphere.infra.metadata.database.schema.manager.GenericSchemaManager;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode;
import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNodePath;
import org.apache.shardingsphere.metadata.persist.service.metadata.table.TableMetaDataPersistService;
import org.apache.shardingsphere.metadata.persist.service.metadata.table.ViewMetaDataPersistService;
import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService;
Expand Down Expand Up @@ -53,7 +53,7 @@ public SchemaMetaDataPersistService(final PersistRepository repository, final Me
* @param schemaName to be added schema name
*/
public void add(final String databaseName, final String schemaName) {
repository.persist(DatabaseMetaDataNode.getMetaDataTablesPath(databaseName, schemaName), "");
repository.persist(DatabaseMetaDataNodePath.getTablesPath(databaseName, schemaName), "");
}

/**
Expand All @@ -63,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(DatabaseMetaDataNode.getMetaDataSchemaPath(databaseName, schemaName));
repository.delete(DatabaseMetaDataNodePath.getSchemaPath(databaseName, schemaName));
}

/**
Expand Down Expand Up @@ -113,7 +113,7 @@ public void alterByRuleDropped(final String databaseName, final ShardingSphereSc
* @return schemas
*/
public Collection<ShardingSphereSchema> load(final String databaseName) {
return repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataSchemasPath(databaseName)).stream()
return repository.getChildrenKeys(DatabaseMetaDataNodePath.getSchemasPath(databaseName)).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 @@ -20,7 +20,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode;
import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNodePath;
import org.apache.shardingsphere.mode.spi.PersistRepository;

import java.util.Collection;
Expand Down Expand Up @@ -55,7 +55,7 @@ public String getActiveVersionByFullPath(final String fullPath) {

@Override
public String getVersionPathByActiveVersion(final String path, final String activeVersion) {
return repository.query(DatabaseMetaDataNode.getVersionNodeByActiveVersionPath(path, activeVersion));
return repository.query(DatabaseMetaDataNodePath.getVersionPath(path, activeVersion));
}

@Override
Expand Down
Loading
Loading