Skip to content

Commit

Permalink
Improve methods for opening neo stores by using flags
Browse files Browse the repository at this point in the history
Added method without flags with a description of the behaviour in the
method name itself.
  • Loading branch information
davidegrohmann committed Oct 2, 2015
1 parent d301397 commit 5c621d1
Show file tree
Hide file tree
Showing 46 changed files with 118 additions and 82 deletions.
Expand Up @@ -134,7 +134,7 @@ public PrintWriter get()
}
} ) );

try ( NeoStores neoStores = factory.openNeoStores( false ) )
try ( NeoStores neoStores = factory.openNeoStoresEagerly() )
{
StoreAccess store = new StoreAccess( neoStores ).initialize();
LabelScanStore labelScanStore = null;
Expand Down
Expand Up @@ -139,7 +139,7 @@ public PrintWriter get()
}
} ) );

try ( NeoStores neoStores = factory.openNeoStores( false ) )
try ( NeoStores neoStores = factory.openNeoStoresEagerly() )
{
LabelScanStore labelScanStore = null;
try
Expand Down
Expand Up @@ -105,7 +105,8 @@ public DirectStoreAccess directStoreAccess()
{
DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
PageCache pageCache = getPageCache( fileSystem );
neoStore = new StoreFactory( fileSystem, directory, pageCache, NullLogProvider.getInstance() ).openNeoStores( false );
neoStore = new StoreFactory( fileSystem, directory, pageCache, NullLogProvider.getInstance() )
.openNeoStoresEagerly();
StoreAccess nativeStores;
if ( keepStatistics )
{
Expand Down
Expand Up @@ -177,6 +177,7 @@
import org.neo4j.unsafe.batchinsert.LabelScanWriter;

import static org.neo4j.helpers.collection.Iterables.toList;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_CREATE;
import static org.neo4j.kernel.impl.transaction.log.pruning.LogPruneStrategyFactory.fromConfigValue;

public class NeoStoreDataSource implements NeoStoresSupplier, Lifecycle, IndexProviders
Expand Down Expand Up @@ -625,8 +626,6 @@ private NeoStoreModule buildNeoStore( final StoreFactory storeFactory, final Lab
labelTokens, final RelationshipTypeTokenHolder relationshipTypeTokens,
final PropertyKeyTokenHolder propertyKeyTokenHolder )
{
final NeoStores neoStores = storeFactory.openNeoStores( true );

life.add( new LifecycleAdapter()
{
@Override
Expand All @@ -636,21 +635,22 @@ public void start() throws IOException
// needs to be cleaned up in latest version
if ( startupStatistics.numberOfRecoveredTransactions() > 0 )
{
neoStores.rebuildIdGenerators();
neoStoreModule.neoStores().rebuildIdGenerators();
}
neoStoreModule.neoStores().makeStoreOk();

propertyKeyTokenHolder.setInitialTokens(
neoStoreModule.neoStores().getPropertyKeyTokenStore().getTokens( Integer.MAX_VALUE ) );
relationshipTypeTokens.setInitialTokens(
neoStoreModule.neoStores().getRelationshipTypeTokenStore().getTokens( Integer.MAX_VALUE ) );
labelTokens.setInitialTokens( neoStoreModule.neoStores().getLabelTokenStore().getTokens( Integer
.MAX_VALUE ) );
labelTokens.setInitialTokens(
neoStoreModule.neoStores().getLabelTokenStore().getTokens( Integer.MAX_VALUE ) );

neoStores.rebuildCountStoreIfNeeded(); // TODO: move this to lifecycle
neoStoreModule.neoStores().rebuildCountStoreIfNeeded(); // TODO: move this to counts store lifecycle
}
} );

final NeoStores neoStores = storeFactory.openNeoStores( SF_CREATE );
return new NeoStoreModule()
{
@Override
Expand Down
Expand Up @@ -102,7 +102,7 @@ else if ( !file.isDirectory() && file.getName().indexOf( ':' ) != -1 )
throw new IllegalArgumentException( "No such file: " + arg );
}
}
try ( NeoStores neoStores = createStoreFactory.apply( file ).openNeoStores( false ) )
try ( NeoStores neoStores = createStoreFactory.apply( file ).openNeoStoresEagerly() )
{
switch ( file.getName() )
{
Expand Down
Expand Up @@ -105,7 +105,8 @@ void dump( File storeDir ) throws IOException
DefaultIdGeneratorFactory idGeneratorFactory = new DefaultIdGeneratorFactory( fs );
Config config = new Config();
StoreFactory storeFactory = new StoreFactory( storeDir, config, idGeneratorFactory, pageCache, fs, logProvider() );
try ( NeoStores neoStores = storeFactory.openNeoStores( false ) )

try ( NeoStores neoStores = storeFactory.openNeoStoresEagerly() )
{
RecordStore<RECORD> store = store( neoStores );
for ( long next = first; next != -1; )
Expand Down
Expand Up @@ -97,7 +97,7 @@ public StoreAccess( FileSystemAbstraction fileSystem, PageCache pageCache, File
private StoreAccess( FileSystemAbstraction fileSystem, PageCache pageCache, File storeDir, Config config )
{
this( new StoreFactory( storeDir, config, new DefaultIdGeneratorFactory( fileSystem ), pageCache,
fileSystem, NullLogProvider.getInstance() ).openNeoStores( false ) );
fileSystem, NullLogProvider.getInstance() ).openNeoStoresEagerly() );
this.closeable = true;
}

Expand Down
Expand Up @@ -59,6 +59,10 @@ public class StoreFactory
public static final String RELATIONSHIP_GROUP_STORE_NAME = ".relationshipgroupstore.db";
public static final String COUNTS_STORE = ".counts.db";

private static final byte SF_DEFAULT = 0;
public static final byte SF_CREATE = 1;
public static final byte SF_LAZY = 1 << 1;

private Config config;
@SuppressWarnings( "deprecation" )
private IdGeneratorFactory idGeneratorFactory;
Expand Down Expand Up @@ -119,14 +123,16 @@ public void setPageCache( PageCache pageCache )
this.pageCache = pageCache;
}

public NeoStores openNeoStores( boolean createIfNotExist )
public NeoStores openNeoStoresEagerly()
{
return openNeoStores( createIfNotExist, true );
return openNeoStores( SF_DEFAULT );
}

public NeoStores openNeoStores( boolean createIfNotExist, boolean eagerlyInitializeStores )
public NeoStores openNeoStores( int flags )
{
if ( createIfNotExist )
boolean createIfNotExists = (flags & SF_CREATE) != 0;
boolean eagerlyInitializeStores = (flags & SF_LAZY) == 0;
if ( createIfNotExists )
{
try
{
Expand All @@ -138,10 +144,8 @@ public NeoStores openNeoStores( boolean createIfNotExist, boolean eagerlyInitial
"Could not create store directory: " + neoStoreFileName.getParent(), e );
}
}
return new NeoStores( neoStoreFileName, config, idGeneratorFactory, pageCache,
logProvider,
fileSystemAbstraction,
createIfNotExist, eagerlyInitializeStores );
return new NeoStores( neoStoreFileName, config, idGeneratorFactory, pageCache, logProvider,
fileSystemAbstraction, createIfNotExists, eagerlyInitializeStores );
}

public abstract static class Configuration
Expand Down
Expand Up @@ -66,7 +66,7 @@ public static void dumpCountsStore( FileSystemAbstraction fs, File path, PrintSt
{
StoreFactory factory = new StoreFactory( fs, path, pages, NullLogProvider.getInstance() );

NeoStores neoStores = life.add( factory.openNeoStores( false ) );
NeoStores neoStores = life.add( factory.openNeoStoresEagerly() );
neoStores.getCounts().accept( new DumpCountsStore( out, neoStores ) );
}
else
Expand Down
Expand Up @@ -46,6 +46,7 @@
import org.neo4j.logging.NullLogProvider;

import static org.neo4j.kernel.api.index.SchemaIndexProvider.getRootDirectory;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_LAZY;
import static org.neo4j.kernel.impl.store.record.SchemaRule.Kind.UNIQUENESS_CONSTRAINT;

public class SchemaIndexMigrator implements StoreMigrationParticipant
Expand Down Expand Up @@ -91,7 +92,7 @@ private void deleteIndexesContainingArrayValues( File storeDir,
IndexSamplingConfig samplingConfig = new IndexSamplingConfig( new Config() );
List<File> indexesToBeDeleted = new ArrayList<>();
storeFactory.setStoreDir( storeDir );
try ( NeoStores neoStores = storeFactory.openNeoStores( false, false ) )
try ( NeoStores neoStores = storeFactory.openNeoStores( SF_LAZY ) )
{
SchemaStore schema = neoStores.getSchemaStore();
Iterator<SchemaRule> rules = schema.loadAllSchemaRules();
Expand Down
Expand Up @@ -100,6 +100,7 @@
import static org.neo4j.helpers.collection.IteratorUtil.loop;
import static org.neo4j.kernel.impl.store.CommonAbstractStore.ALL_STORES_VERSION;
import static org.neo4j.kernel.impl.store.MetaDataStore.DEFAULT_NAME;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_CREATE;
import static org.neo4j.kernel.impl.storemigration.FileOperation.COPY;
import static org.neo4j.kernel.impl.storemigration.FileOperation.DELETE;
import static org.neo4j.kernel.impl.storemigration.FileOperation.MOVE;
Expand Down Expand Up @@ -317,7 +318,7 @@ private void rebuildCountsFromScratch( File storeDir, long lastTxId, PageCache p

final StoreFactory storeFactory =
new StoreFactory( fileSystem, storeDir, pageCache, NullLogProvider.getInstance() );
try ( NeoStores neoStores = storeFactory.openNeoStores( false ) )
try ( NeoStores neoStores = storeFactory.openNeoStoresEagerly() )
{
NodeStore nodeStore = neoStores.getNodeStore();
RelationshipStore relationshipStore = neoStores.getRelationshipStore();
Expand Down Expand Up @@ -497,7 +498,7 @@ private void migratePropertyKeys( Legacy19Store legacyStore, PageCache pageCache
{ // The legacy property key token store contains duplicates, copy over and deduplicate
// property key token store and go through property store with the new token ids.
StoreFactory storeFactory = storeFactory( pageCache, migrationDir );
try ( NeoStores neoStores = storeFactory.openNeoStores( true ) )
try ( NeoStores neoStores = storeFactory.openNeoStores( SF_CREATE ) )
{
PropertyStore propertyStore = neoStores.getPropertyStore();
// dedup and write new property key token store (incl. names)
Expand Down
Expand Up @@ -42,6 +42,8 @@
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.logging.NullLogProvider;

import static org.neo4j.kernel.impl.store.StoreFactory.SF_LAZY;

public class PropertyDeduplicator
{
private final FileSystemAbstraction fileSystem;
Expand All @@ -67,7 +69,7 @@ public void deduplicateProperties() throws IOException
{
final StoreFactory storeFactory =
new StoreFactory( fileSystem, workingDir, pageCache, NullLogProvider.getInstance() );
try ( NeoStores neoStores = storeFactory.openNeoStores( false, false ) )
try ( NeoStores neoStores = storeFactory.openNeoStores( SF_LAZY ) )
{
PropertyStore propertyStore = neoStores.getPropertyStore();
NodeStore nodeStore = neoStores.getNodeStore();
Expand Down
Expand Up @@ -84,7 +84,7 @@ public static void main( String[] args ) throws IOException
try ( PageCache pageCache = createPageCache( files, config ) )
{
StoreFactory factory = openStore( new File( storedir, MetaDataStore.DEFAULT_NAME ), config, pageCache );
NeoStores neoStores = factory.openNeoStores( false );
NeoStores neoStores = factory.openNeoStoresEagerly();
interact( neoStores );
}
}
Expand Down
Expand Up @@ -148,12 +148,12 @@
import org.neo4j.logging.NullLog;

import static java.lang.Boolean.parseBoolean;

import static org.neo4j.collection.primitive.PrimitiveLongCollections.map;
import static org.neo4j.graphdb.DynamicLabel.label;
import static org.neo4j.helpers.collection.IteratorUtil.first;
import static org.neo4j.kernel.impl.store.NodeLabelsField.parseLabelsField;
import static org.neo4j.kernel.impl.store.PropertyStore.encodeString;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_CREATE;
import static org.neo4j.kernel.impl.util.IoPrimitiveUtils.safeCastLongToInt;

/**
Expand Down Expand Up @@ -276,7 +276,7 @@ public Label apply( long from )
}
msgLog.info( Thread.currentThread() + " Starting BatchInserter(" + this + ")" );
life.start();
neoStores = sf.openNeoStores( true );
neoStores = sf.openNeoStores( SF_CREATE );
neoStores.verifyStoreOk();

nodeStore = neoStores.getNodeStore();
Expand Down
Expand Up @@ -63,6 +63,7 @@
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.dense_node_threshold;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.pagecache_memory;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_CREATE;

/**
* Creator and accessor of {@link NeoStores} with some logic to provide very batch friendly services to the
Expand Down Expand Up @@ -181,7 +182,7 @@ public static void createStore( FileSystemAbstraction fileSystem, String storeDi
{
StoreFactory storeFactory =
new StoreFactory( fileSystem, new File( storeDir ), pageCache, NullLogProvider.getInstance() );
try ( NeoStores neoStores = storeFactory.openNeoStores( true ) )
try ( NeoStores neoStores = storeFactory.openNeoStores( SF_CREATE ) )
{
neoStores.getMetaDataStore();
neoStores.getLabelTokenStore();
Expand All @@ -199,7 +200,7 @@ private NeoStores newNeoStores( PageCache pageCache )
BatchingIdGeneratorFactory idGeneratorFactory = new BatchingIdGeneratorFactory( fileSystem );
StoreFactory storeFactory =
new StoreFactory( storeDir, neo4jConfig, idGeneratorFactory, pageCache, fileSystem, logProvider );
return storeFactory.openNeoStores( true );
return storeFactory.openNeoStores( SF_CREATE );
}

public IoTracer getIoTracer()
Expand Down
Expand Up @@ -75,7 +75,7 @@ public void idGeneratorsRebuildAfterRecovery() throws IOException
}

@Test
public void deleteAndRemoveNodePropertyDuringOneRecoveryRun() throws IOException
public void shouldRecoverIdsCorrectlyWhenWeCreateAndDeleteANodeInTheSameRecoveryRun() throws IOException
{
GraphDatabaseService database = startDatabase( directory.graphDbDir() );
Label testLabel = DynamicLabel.label( "testLabel" );
Expand Down
Expand Up @@ -99,6 +99,7 @@
import static org.mockito.Mockito.when;

import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_CREATE;

public class TransactionRepresentationCommitProcessIT
{
Expand Down Expand Up @@ -141,7 +142,7 @@ public void setUp()
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
storeDir = testDirectory.graphDbDir();
StoreFactory storeFactory = new StoreFactory( fileSystem, storeDir, pageCache, NullLogProvider.getInstance() );
neoStores = storeFactory.openNeoStores( true );
neoStores = storeFactory.openNeoStores( SF_CREATE );
}

@After
Expand Down
Expand Up @@ -49,6 +49,7 @@
import static org.neo4j.helpers.collection.Iterables.toList;
import static org.neo4j.helpers.collection.IteratorUtil.count;
import static org.neo4j.helpers.collection.IteratorUtil.single;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_CREATE;

public class PropertyPhysicalToLogicalConverterTest
{
Expand Down Expand Up @@ -221,7 +222,7 @@ public void before() throws Exception
fs.get().mkdirs( storeDir );
StoreFactory storeFactory = new StoreFactory( storeDir, new Config(), new DefaultIdGeneratorFactory( fs.get() ),
pageCacheRule.getPageCache( fs.get() ), fs.get(), NullLogProvider.getInstance() );
neoStores = storeFactory.openNeoStores( true );
neoStores = storeFactory.openNeoStores( SF_CREATE );
store = neoStores.getPropertyStore();
converter = new PropertyPhysicalToLogicalConverter( store );
}
Expand Down
Expand Up @@ -67,6 +67,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_CREATE;

@RunWith( Enclosed.class )
public class StorePropertyCursorTest
Expand Down Expand Up @@ -307,7 +308,7 @@ public void setup() throws IOException
}
fs.mkdirs( storeDir );
StoreFactory storeFactory = new StoreFactory( fs, storeDir, pageCache, log );
neoStores = storeFactory.openNeoStores( true );
neoStores = storeFactory.openNeoStores( SF_CREATE );
propertyStore = neoStores.getPropertyStore();
}

Expand Down
Expand Up @@ -49,6 +49,7 @@

import static org.junit.Assert.assertEquals;
import static org.neo4j.helpers.collection.IteratorUtil.first;
import static org.neo4j.kernel.impl.store.StoreFactory.SF_CREATE;

/**
* Tests for handling many property keys (even after restart of database)
Expand Down Expand Up @@ -122,7 +123,7 @@ private GraphDatabaseAPI databaseWithManyPropertyKeys( int propertyKeyCount ) th
DefaultFileSystemAbstraction fs = new DefaultFileSystemAbstraction();
PageCache pageCache = pageCacheRule.getPageCache( fs );
StoreFactory storeFactory = new StoreFactory( fs, storeDir, pageCache, NullLogProvider.getInstance() );
NeoStores neoStores = storeFactory.openNeoStores( true );
NeoStores neoStores = storeFactory.openNeoStores( SF_CREATE );
PropertyKeyTokenStore store = neoStores.getPropertyKeyTokenStore();
for ( int i = 0; i < propertyKeyCount; i++ )
{
Expand Down
Expand Up @@ -84,7 +84,7 @@ private void performTest() throws Exception
NeoStores neoStores = null;
try
{
neoStores = factory.openNeoStores( false );
neoStores = factory.openNeoStoresEagerly();
// emulate a failure during rebuild:
emulateFailureOnRebuildOf( neoStores );
}
Expand Down

0 comments on commit 5c621d1

Please sign in to comment.