Skip to content

Commit

Permalink
Refactor MetaDataVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 21, 2024
1 parent af77eec commit 693ba2c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,8 +38,8 @@ public void switchActiveVersion(final Collection<MetaDataVersion> 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());
}
}

Expand Down

0 comments on commit 693ba2c

Please sign in to comment.