From c5ba716aa6bbb1be9bdf5ec41401f0508e9ecbda Mon Sep 17 00:00:00 2001 From: Jonas Kalderstam Date: Thu, 3 Nov 2016 12:09:36 +0100 Subject: [PATCH] Move findSuccessor method into RecordFormatSelector --- .../commandline/dbms/VersionCommand.java | 27 ++------------- .../store/format/RecordFormatSelector.java | 34 +++++++++++++++++-- .../format/RecordFormatSelectorTest.java | 28 +++++++++++++-- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/community/dbms/src/main/java/org/neo4j/commandline/dbms/VersionCommand.java b/community/dbms/src/main/java/org/neo4j/commandline/dbms/VersionCommand.java index cfc4528c7564c..0188c86840c72 100644 --- a/community/dbms/src/main/java/org/neo4j/commandline/dbms/VersionCommand.java +++ b/community/dbms/src/main/java/org/neo4j/commandline/dbms/VersionCommand.java @@ -34,12 +34,13 @@ import org.neo4j.io.pagecache.PageCache; import org.neo4j.kernel.impl.pagecache.StandalonePageCacheFactory; import org.neo4j.kernel.impl.store.MetaDataStore; -import org.neo4j.kernel.impl.store.format.FormatFamily; import org.neo4j.kernel.impl.store.format.RecordFormatSelector; import org.neo4j.kernel.impl.store.format.RecordFormats; import org.neo4j.kernel.impl.storemigration.StoreVersionCheck; import org.neo4j.kernel.impl.util.Validators; +import static org.neo4j.kernel.impl.store.format.RecordFormatSelector.findSuccessor; + public class VersionCommand implements AdminCommand { private static final Arguments arguments = new Arguments() @@ -118,28 +119,4 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed throw new CommandFailed( e.getMessage(), e ); } } - - /** - * @param format to find successor to. - * @return the format with the lowest generation > format.generation, or None if no such format is known. - */ - private Optional findSuccessor( final RecordFormats format ) - { - RecordFormats successor = null; - - for ( RecordFormats candidate : RecordFormatSelector.allFormats() ) - { - if ( !(FormatFamily.isSameFamily( format, candidate )) || - candidate.generation() <= format.generation() ) - { - continue; - } - if ( successor == null || candidate.generation() < successor.generation() ) - { - successor = candidate; - } - } - - return Optional.ofNullable( successor ); - } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/format/RecordFormatSelector.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/format/RecordFormatSelector.java index bc82ad275d2e1..10b2886d897a7 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/format/RecordFormatSelector.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/format/RecordFormatSelector.java @@ -19,13 +19,14 @@ */ package org.neo4j.kernel.impl.store.format; -import org.apache.commons.lang3.StringUtils; - import java.io.File; import java.io.IOException; +import java.util.Optional; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.commons.lang3.StringUtils; + import org.neo4j.graphdb.factory.GraphDatabaseSettings; import org.neo4j.helpers.Service; import org.neo4j.io.fs.FileSystemAbstraction; @@ -262,8 +263,35 @@ else if ( FormatFamily.isHigherFamilyFormat( DEFAULT_FORMAT, result ) || } } + /** + * Finds which format, if any, succeeded the specified format. Only formats in the same family are considered. + * + * @param format to find successor to. + * @return the format with the lowest generation > format.generation, or None if no such format is known. + */ + @Nonnull + public static Optional findSuccessor( @Nonnull final RecordFormats format ) + { + RecordFormats successor = null; + + for ( RecordFormats candidate : RecordFormatSelector.allFormats() ) + { + if ( !( FormatFamily.isSameFamily( format, candidate ) ) || + candidate.generation() <= format.generation() ) + { + continue; + } + if ( successor == null || candidate.generation() < successor.generation() ) + { + successor = candidate; + } + } + + return Optional.ofNullable( successor ); + } + @Nonnull - public static Iterable allFormats() + private static Iterable allFormats() { Iterable loadableFormatFactories = Service.load( RecordFormats.Factory.class ); Iterable loadableFormats = map( RecordFormats.Factory::newInstance, loadableFormatFactories ); diff --git a/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordFormatSelectorTest.java b/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordFormatSelectorTest.java index 142c86693cefd..bbb6baa6f345f 100644 --- a/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordFormatSelectorTest.java +++ b/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/store/format/RecordFormatSelectorTest.java @@ -19,12 +19,12 @@ */ package org.neo4j.kernel.impl.store.format; -import org.junit.Rule; -import org.junit.Test; - import java.io.File; import java.io.IOException; +import org.junit.Rule; +import org.junit.Test; + import org.neo4j.graphdb.factory.GraphDatabaseSettings; import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction; @@ -33,6 +33,7 @@ import org.neo4j.kernel.impl.store.MetaDataStore; import org.neo4j.kernel.impl.store.format.highlimit.HighLimit; import org.neo4j.kernel.impl.store.format.highlimit.v300.HighLimitV3_0_0; +import org.neo4j.kernel.impl.store.format.highlimit.v306.HighLimitV3_0_6; import org.neo4j.kernel.impl.store.format.standard.StandardV2_0; import org.neo4j.kernel.impl.store.format.standard.StandardV2_1; import org.neo4j.kernel.impl.store.format.standard.StandardV2_2; @@ -43,6 +44,8 @@ import org.neo4j.test.rule.PageCacheRule; import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; @@ -55,6 +58,7 @@ import static org.neo4j.helpers.collection.MapUtil.stringMap; import static org.neo4j.kernel.impl.store.MetaDataStore.Position.STORE_VERSION; import static org.neo4j.kernel.impl.store.format.RecordFormatSelector.defaultFormat; +import static org.neo4j.kernel.impl.store.format.RecordFormatSelector.findSuccessor; import static org.neo4j.kernel.impl.store.format.RecordFormatSelector.selectForConfig; import static org.neo4j.kernel.impl.store.format.RecordFormatSelector.selectForStore; import static org.neo4j.kernel.impl.store.format.RecordFormatSelector.selectForStoreOrConfig; @@ -338,6 +342,24 @@ public void selectNewestFormatForExistingStoreWithLegacyFormat() throws IOExcept assertSame( defaultFormat(), selectNewestFormat( config, storeDir, fs, getPageCache(), LOG ) ); } + @Test + public void findSuccessorLatestVersion() throws Exception + { + assertFalse( findSuccessor( defaultFormat() ).isPresent() ); + } + + @Test + public void findSuccessorToOlderVersion() throws Exception + { + assertEquals( StandardV2_1.RECORD_FORMATS, findSuccessor( StandardV2_0.RECORD_FORMATS ).get() ); + assertEquals( StandardV2_2.RECORD_FORMATS, findSuccessor( StandardV2_1.RECORD_FORMATS ).get() ); + assertEquals( StandardV2_3.RECORD_FORMATS, findSuccessor( StandardV2_2.RECORD_FORMATS ).get() ); + assertEquals( StandardV3_0.RECORD_FORMATS, findSuccessor( StandardV2_3.RECORD_FORMATS ).get() ); + + assertEquals( HighLimitV3_0_6.RECORD_FORMATS, findSuccessor( HighLimitV3_0_0.RECORD_FORMATS ).get() ); + assertEquals( HighLimit.RECORD_FORMATS, findSuccessor( HighLimitV3_0_6.RECORD_FORMATS ).get() ); + } + private PageCache getPageCache() { return pageCacheRule.getPageCache( fs );