From 693ba2c2bdd709222bfccd5da44b7d1055912231 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 21 May 2024 17:53:00 +0800 Subject: [PATCH] Refactor MetaDataVersion --- .../metadata/version/MetaDataVersion.java | 20 +++++++++ .../metadata/version/MetaDataVersionTest.java | 43 +++++++++++++++++++ .../MetaDataVersionPersistService.java | 8 +--- 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersionTest.java diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersion.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersion.java index d2c7cae829637..3f1c9d2d4ee39 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersion.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersion.java @@ -32,6 +32,8 @@ public final class MetaDataVersion { private static final String ACTIVE_VERSION = "active_version"; + private static final String VERSIONS = "versions"; + private final String key; private final String currentActiveVersion; @@ -44,6 +46,24 @@ public MetaDataVersion(final String key) { nextActiveVersion = ""; } + /** + * Get active version node path. + * + * @return path of active version node + */ + public String getActiveVersionNodePath() { + return String.join("/", key, ACTIVE_VERSION); + } + + /** + * Get versions node path. + * + * @return path of versions node + */ + public String getVersionsNodePath() { + return String.join("/", key, VERSIONS, currentActiveVersion); + } + /** * Get active version keys. * diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersionTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersionTest.java new file mode 100644 index 0000000000000..dda82a83f5da4 --- /dev/null +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/version/MetaDataVersionTest.java @@ -0,0 +1,43 @@ +/* + * 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.infra.metadata.version; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class MetaDataVersionTest { + + @Test + void assertGetActiveVersionNodePath() { + assertThat(new MetaDataVersion("foo", "0", "1").getActiveVersionNodePath(), is("foo/active_version")); + } + + @Test + void assertGetVersionsNodePath() { + assertThat(new MetaDataVersion("foo", "0", "1").getVersionsNodePath(), is("foo/versions/0")); + } + + @Test + void assertGetActiveVersionKeys() { + assertThat(new MetaDataVersion("foo", "0", "1").getActiveVersionKeys(), is(Arrays.asList("foo/active_version", "foo/active_version/0"))); + } +} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/version/MetaDataVersionPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/version/MetaDataVersionPersistService.java index 92c1690107f31..15d1824519251 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/version/MetaDataVersionPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/version/MetaDataVersionPersistService.java @@ -30,10 +30,6 @@ @RequiredArgsConstructor public final class MetaDataVersionPersistService implements MetaDataVersionBasedPersistService { - private static final String ACTIVE_VERSION = "/active_version"; - - private static final String VERSIONS = "/versions/"; - private final PersistRepository repository; @Override @@ -42,8 +38,8 @@ public void switchActiveVersion(final Collection metaDataVersio if (each.getNextActiveVersion().equals(each.getCurrentActiveVersion())) { continue; } - repository.persist(each.getKey() + ACTIVE_VERSION, each.getNextActiveVersion()); - repository.delete(each.getKey() + VERSIONS + each.getCurrentActiveVersion()); + repository.persist(each.getActiveVersionNodePath(), each.getNextActiveVersion()); + repository.delete(each.getVersionsNodePath()); } }