Skip to content

Commit

Permalink
Move findSuccessor method into RecordFormatSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed Nov 3, 2016
1 parent 850c1c2 commit c5ba716
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
Expand Up @@ -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()
Expand Down Expand Up @@ -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<RecordFormats> 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 );
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RecordFormats> 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<RecordFormats> allFormats()
private static Iterable<RecordFormats> allFormats()
{
Iterable<RecordFormats.Factory> loadableFormatFactories = Service.load( RecordFormats.Factory.class );
Iterable<RecordFormats> loadableFormats = map( RecordFormats.Factory::newInstance, loadableFormatFactories );
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit c5ba716

Please sign in to comment.