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 DatabaseRuleMetaDataNode #34276

Merged
merged 2 commits into from
Jan 7, 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,106 @@
/*
* 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.metadata;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

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

private static final String ROOT_NODE = "/metadata";

private static final String RULE_NODE = "rules";

private static final String VERSIONS_NODE = "versions";

private static final String ACTIVE_VERSION_NODE = "active_version";

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

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

/**
* Get database rule path.
*
* @param databaseName database name
* @param ruleTypeName rule type name
* @param key key
* @return database rule path without version
*/
public static String getRulePath(final String databaseName, final String ruleTypeName, final String key) {
return String.join("/", getRulePath(databaseName, ruleTypeName), key);
}

/**
* Get database rule versions path.
*
* @param databaseName database name
* @param ruleTypeName rule type name
* @param key key
* @return database rule versions path
*/
public static String getVersionsPath(final String databaseName, final String ruleTypeName, final String key) {
return String.join("/", getRulePath(databaseName, ruleTypeName, key), VERSIONS_NODE);
}

/**
* Get database rule version path.
*
* @param databaseName database name
* @param ruleTypeName rule type name
* @param key key
* @param version version
* @return database rule next version
*/
public static String getVersionPath(final String databaseName, final String ruleTypeName, final String key, final String version) {
return String.join("/", getVersionsPath(databaseName, ruleTypeName, key), version);
}

/**
* Get database rule active version path.
*
* @param databaseName database name
* @param ruleTypeName rule type name
* @param key key
* @return database rule active version path
*/
public static String getActiveVersionPath(final String databaseName, final String ruleTypeName, final String key) {
return String.join("/", getRulePath(databaseName, ruleTypeName, key), ACTIVE_VERSION_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.metadata.persist.node.metadata.DatabaseRuleMetaDataNode;
import org.apache.shardingsphere.metadata.persist.node.metadata.DatabaseRuleMetaDataNodePath;
import org.apache.shardingsphere.metadata.persist.service.config.RepositoryTuplePersistService;
import org.apache.shardingsphere.metadata.persist.service.version.MetaDataVersionPersistService;
import org.apache.shardingsphere.mode.spi.PersistRepository;
Expand Down Expand Up @@ -60,7 +60,7 @@ public DatabaseRulePersistService(final PersistRepository repository) {
* @return configurations
*/
public Collection<RuleConfiguration> load(final String databaseName) {
return new RepositoryTupleSwapperEngine().swapToRuleConfigurations(repositoryTuplePersistService.load(DatabaseRuleMetaDataNode.getRulesNode(databaseName)));
return new RepositoryTupleSwapperEngine().swapToRuleConfigurations(repositoryTuplePersistService.load(DatabaseRuleMetaDataNodePath.getRootPath(databaseName)));
}

/**
Expand All @@ -85,19 +85,19 @@ public Collection<MetaDataVersion> persist(final String databaseName, final Coll
private Collection<MetaDataVersion> persistDataNodes(final String databaseName, final String ruleName, final Collection<RepositoryTuple> repositoryTuples) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (RepositoryTuple each : repositoryTuples) {
List<String> versions = metaDataVersionPersistService.getVersions(DatabaseRuleMetaDataNode.getDatabaseRuleVersionsNode(databaseName, ruleName, each.getKey()));
List<String> versions = metaDataVersionPersistService.getVersions(DatabaseRuleMetaDataNodePath.getVersionsPath(databaseName, ruleName, each.getKey()));
String nextVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
repository.persist(DatabaseRuleMetaDataNode.getDatabaseRuleVersionNode(databaseName, ruleName, each.getKey(), nextVersion), each.getValue());
repository.persist(DatabaseRuleMetaDataNodePath.getVersionPath(databaseName, ruleName, each.getKey(), nextVersion), each.getValue());
if (Strings.isNullOrEmpty(getActiveVersion(databaseName, ruleName, each.getKey()))) {
repository.persist(DatabaseRuleMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, each.getKey()), MetaDataVersion.DEFAULT_VERSION);
repository.persist(DatabaseRuleMetaDataNodePath.getActiveVersionPath(databaseName, ruleName, each.getKey()), MetaDataVersion.DEFAULT_VERSION);
}
result.add(new MetaDataVersion(DatabaseRuleMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion));
result.add(new MetaDataVersion(DatabaseRuleMetaDataNodePath.getRulePath(databaseName, ruleName, each.getKey()), getActiveVersion(databaseName, ruleName, each.getKey()), nextVersion));
}
return result;
}

private String getActiveVersion(final String databaseName, final String ruleName, final String key) {
return repository.query(DatabaseRuleMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, key));
return repository.query(DatabaseRuleMetaDataNodePath.getActiveVersionPath(databaseName, ruleName, key));
}

/**
Expand All @@ -107,7 +107,7 @@ private String getActiveVersion(final String databaseName, final String ruleName
* @param ruleTypeName rule type name
*/
public void delete(final String databaseName, final String ruleTypeName) {
repository.delete(DatabaseRuleMetaDataNode.getDatabaseRuleNode(databaseName, ruleTypeName));
repository.delete(DatabaseRuleMetaDataNodePath.getRulePath(databaseName, ruleTypeName));
}

/**
Expand Down Expand Up @@ -135,7 +135,7 @@ public Collection<MetaDataVersion> delete(final String databaseName, final Colle
private Collection<MetaDataVersion> delete(final String databaseName, final String ruleName, final Collection<RepositoryTuple> repositoryTuples) {
Collection<MetaDataVersion> result = new LinkedList<>();
for (RepositoryTuple each : repositoryTuples) {
String toBeDeletedKey = DatabaseRuleMetaDataNode.getDatabaseRuleNode(databaseName, ruleName, each.getKey());
String toBeDeletedKey = DatabaseRuleMetaDataNodePath.getRulePath(databaseName, ruleName, each.getKey());
repository.delete(toBeDeletedKey);
result.add(new MetaDataVersion(toBeDeletedKey));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.metadata;

import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class DatabaseRuleMetaDataNodePathTest {

@Test
void assertGetRootPath() {
assertThat(DatabaseRuleMetaDataNodePath.getRootPath("foo_db"), is("/metadata/foo_db/rules"));
}

@Test
void assertGetRulePath() {
assertThat(DatabaseRuleMetaDataNodePath.getRulePath("foo_db", "foo_rule"), is("/metadata/foo_db/rules/foo_rule"));
}

@Test
void assertGetRulePathWithKey() {
assertThat(DatabaseRuleMetaDataNodePath.getRulePath("foo_db", "foo_rule", "sharding"), is("/metadata/foo_db/rules/foo_rule/sharding"));
}

@Test
void assertGetVersionsPath() {
assertThat(DatabaseRuleMetaDataNodePath.getVersionsPath("foo_db", "sharding", "foo_key"), is("/metadata/foo_db/rules/sharding/foo_key/versions"));
}

@Test
void assertGetVersionPath() {
assertThat(DatabaseRuleMetaDataNodePath.getVersionPath("foo_db", "foo_rule", "foo_tbl", "1"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/versions/1"));
}

@Test
void assertGetActiveVersionPath() {
assertThat(DatabaseRuleMetaDataNodePath.getActiveVersionPath("foo_db", "foo_rule", "foo_tbl"), is("/metadata/foo_db/rules/foo_rule/foo_tbl/active_version"));
}
}

This file was deleted.

Loading