Skip to content

Commit

Permalink
Open stores with custom name and OpenOptions
Browse files Browse the repository at this point in the history
this to, as an example, support opening temporary stores which can
act as scratch areas. Specifically StandardOpenOptions.DELETE_ON_CLOSE
will delete the store as part of closing it, even the id generator
will be deleted if this option is supplied.
  • Loading branch information
tinwelint committed Jul 25, 2016
1 parent 74efe58 commit d7f8800
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 77 deletions.
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.file.OpenOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -76,11 +77,12 @@ public AbstractDynamicStore(
String typeDescriptor,
int dataSizeFromConfiguration,
RecordFormat<DynamicRecord> recordFormat,
String storeVersion )
String storeVersion,
OpenOption... openOptions )
{
super( fileName, conf, idType, idGeneratorFactory, pageCache, logProvider, typeDescriptor,
recordFormat, new DynamicStoreHeaderFormat( dataSizeFromConfiguration, recordFormat ),
storeVersion );
storeVersion, openOptions );
}

public static void allocateRecordsFromBytes(
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.Collection;

Expand All @@ -45,6 +46,9 @@
import org.neo4j.logging.Logger;
import org.neo4j.string.UTF8;

import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;

import static org.neo4j.helpers.ArrayUtil.contains;
import static org.neo4j.helpers.Exceptions.launderedException;
import static org.neo4j.io.pagecache.PageCacheOpenOptions.ANY_PAGE_SIZE;
import static org.neo4j.io.pagecache.PagedFile.PF_READ_AHEAD;
Expand Down Expand Up @@ -80,6 +84,8 @@ public abstract class CommonAbstractStore<RECORD extends AbstractBaseRecord,HEAD
private final StoreHeaderFormat<HEADER> storeHeaderFormat;
private HEADER storeHeader;

private final OpenOption[] openOptions;

/**
* Opens and validates the store contained in <CODE>fileName</CODE>
* loading any configuration defined in <CODE>config</CODE>. After
Expand All @@ -105,7 +111,8 @@ public CommonAbstractStore(
String typeDescriptor,
RecordFormat<RECORD> recordFormat,
StoreHeaderFormat<HEADER> storeHeaderFormat,
String storeVersion )
String storeVersion,
OpenOption... openOptions )
{
this.storageFileName = fileName;
this.configuration = configuration;
Expand All @@ -116,6 +123,7 @@ public CommonAbstractStore(
this.recordFormat = recordFormat;
this.storeHeaderFormat = storeHeaderFormat;
this.storeVersion = storeVersion;
this.openOptions = openOptions;
this.log = logProvider.getLog( getClass() );
}

Expand Down Expand Up @@ -243,7 +251,7 @@ protected void loadStorage()
{
extractHeaderRecord();
int filePageSize = pageCache.pageSize() - pageCache.pageSize() % getRecordSize();
storeFile = pageCache.map( getStorageFileName(), filePageSize );
storeFile = pageCache.map( getStorageFileName(), filePageSize, openOptions );
}
catch ( IOException e )
{
Expand Down Expand Up @@ -877,7 +885,14 @@ private void closeStoreFile() throws IOException
storeFile.close();
if ( idGenerator != null )
{
idGenerator.close();
if ( contains( openOptions, DELETE_ON_CLOSE ) )
{
idGenerator.delete();
}
else
{
idGenerator.close();
}
}
}
finally
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.nio.file.OpenOption;
import java.util.Collection;
import java.util.Iterator;

Expand Down Expand Up @@ -57,10 +58,11 @@ public DynamicArrayStore(
LogProvider logProvider,
int dataSizeFromConfiguration,
RecordFormat<DynamicRecord> recordFormat,
String storeVersion )
String storeVersion,
OpenOption... openOptions )
{
super( fileName, configuration, idType, idGeneratorFactory, pageCache,
logProvider, TYPE_DESCRIPTOR, dataSizeFromConfiguration, recordFormat, storeVersion );
logProvider, TYPE_DESCRIPTOR, dataSizeFromConfiguration, recordFormat, storeVersion, openOptions );
}

@Override
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.kernel.impl.store;

import java.io.File;
import java.nio.file.OpenOption;

import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
Expand All @@ -46,10 +47,11 @@ public DynamicStringStore(
LogProvider logProvider,
int dataSizeFromConfiguration,
RecordFormat<DynamicRecord> recordFormat,
String storeVersion )
String storeVersion,
OpenOption... openOptions )
{
super( fileName, configuration, idType, idGeneratorFactory, pageCache,
logProvider, TYPE_DESCRIPTOR, dataSizeFromConfiguration, recordFormat, storeVersion );
logProvider, TYPE_DESCRIPTOR, dataSizeFromConfiguration, recordFormat, storeVersion, openOptions );
}

@Override
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.kernel.impl.store;

import java.io.File;
import java.nio.file.OpenOption;

import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.configuration.Config;
Expand All @@ -44,11 +45,12 @@ public LabelTokenStore(
PageCache pageCache,
LogProvider logProvider,
DynamicStringStore nameStore,
RecordFormats recordFormats)
RecordFormats recordFormats,
OpenOption... openOptions )
{
super( file, config, IdType.LABEL_TOKEN, idGeneratorFactory, pageCache,
logProvider, nameStore, TYPE_DESCRIPTOR, new Token.Factory(), recordFormats.labelToken(),
recordFormats.storeVersion() );
recordFormats.storeVersion(), openOptions );
}

@Override
Expand Down
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.OpenOption;
import java.util.concurrent.atomic.AtomicLong;

import org.neo4j.helpers.Clock;
Expand Down Expand Up @@ -148,10 +149,11 @@ public String description()
MetaDataStore( File fileName, Config conf,
IdGeneratorFactory idGeneratorFactory,
PageCache pageCache, LogProvider logProvider, RecordFormat<MetaDataRecord> recordFormat,
String storeVersion )
String storeVersion,
OpenOption... openOptions )
{
super( fileName, conf, IdType.NEOSTORE_BLOCK, idGeneratorFactory, pageCache, logProvider,
TYPE_DESCRIPTOR, recordFormat, NoStoreHeaderFormat.NO_STORE_HEADER_FORMAT, storeVersion );
TYPE_DESCRIPTOR, recordFormat, NoStoreHeaderFormat.NO_STORE_HEADER_FORMAT, storeVersion, openOptions );
this.transactionCloseWaitLogger = new CappedLogger( logProvider.getLog( MetaDataStore.class ) );
transactionCloseWaitLogger.setTimeLimit( 30, SECONDS, Clock.SYSTEM_CLOCK );
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.OpenOption;
import java.util.Iterator;
import java.util.function.Predicate;

Expand Down Expand Up @@ -107,6 +108,7 @@ public boolean test( StoreType type )
private final RecordFormats recordFormats;
// All stores, as Object due to CountsTracker being different that all other stores.
private final Object[] stores;
private final OpenOption[] openOptions;

NeoStores(
File neoStoreFileName,
Expand All @@ -117,7 +119,8 @@ public boolean test( StoreType type )
FileSystemAbstraction fileSystemAbstraction,
RecordFormats recordFormats,
boolean createIfNotExist,
StoreType... storeTypes )
StoreType[] storeTypes,
OpenOption[] openOptions )
{
this.neoStoreFileName = neoStoreFileName;
this.config = config;
Expand All @@ -127,6 +130,7 @@ public boolean test( StoreType type )
this.fileSystemAbstraction = fileSystemAbstraction;
this.recordFormats = recordFormats;
this.createIfNotExist = createIfNotExist;
this.openOptions = openOptions;
this.storeDir = neoStoreFileName.getParentFile();

verifyRecordFormat();
Expand Down Expand Up @@ -499,22 +503,22 @@ CommonAbstractStore createDynamicArrayStore( String storeName, IdType idType, in
}
File storeFile = getStoreFile( storeName );
return initialize( new DynamicArrayStore( storeFile, config, idType, idGeneratorFactory, pageCache,
logProvider, blockSize, recordFormats.dynamic(), recordFormats.storeVersion() ) );
logProvider, blockSize, recordFormats.dynamic(), recordFormats.storeVersion(), openOptions ) );
}

CommonAbstractStore createNodeStore( String storeName )
{
File storeFile = getStoreFile( storeName );
return initialize( new NodeStore( storeFile, config, idGeneratorFactory, pageCache, logProvider,
(DynamicArrayStore) getOrCreateStore( StoreType.NODE_LABEL ), recordFormats ) );
(DynamicArrayStore) getOrCreateStore( StoreType.NODE_LABEL ), recordFormats, openOptions ) );
}

CommonAbstractStore createPropertyKeyTokenStore( String storeName )
{
File storeFile = getStoreFile( storeName );
return initialize( new PropertyKeyTokenStore( storeFile, config, idGeneratorFactory,
pageCache, logProvider, (DynamicStringStore) getOrCreateStore( StoreType.PROPERTY_KEY_TOKEN_NAME ),
recordFormats.propertyKeyToken(), recordFormats.storeVersion() ) );
recordFormats, openOptions ) );
}

CommonAbstractStore createPropertyStore( String storeName )
Expand All @@ -523,14 +527,14 @@ CommonAbstractStore createPropertyStore( String storeName )
return initialize( new PropertyStore( storeFile, config, idGeneratorFactory, pageCache, logProvider,
(DynamicStringStore) getOrCreateStore( StoreType.PROPERTY_STRING ),
(PropertyKeyTokenStore) getOrCreateStore( StoreType.PROPERTY_KEY_TOKEN ),
(DynamicArrayStore) getOrCreateStore( StoreType.PROPERTY_ARRAY ), recordFormats ) );
(DynamicArrayStore) getOrCreateStore( StoreType.PROPERTY_ARRAY ), recordFormats, openOptions ) );
}

CommonAbstractStore createRelationshipStore( String storeName )
{
File file = getStoreFile( storeName );
return initialize( new RelationshipStore( file, config, idGeneratorFactory, pageCache, logProvider,
recordFormats ) );
recordFormats, openOptions ) );
}

CommonAbstractStore createDynamicStringStore( String storeName, IdType idType,
Expand All @@ -543,36 +547,39 @@ CommonAbstractStore createDynamicStringStore( String storeName, IdType idType, i
{
File storeFile = getStoreFile( storeName );
return initialize( new DynamicStringStore( storeFile, config, idType, idGeneratorFactory,
pageCache, logProvider, blockSize, recordFormats.dynamic(), recordFormats.storeVersion() ) );
pageCache, logProvider, blockSize, recordFormats.dynamic(), recordFormats.storeVersion(),
openOptions ) );
}

CommonAbstractStore createRelationshipTypeTokenStore( String storeName )
{
File storeFile = getStoreFile( storeName );
return initialize( new RelationshipTypeTokenStore( storeFile, config, idGeneratorFactory,
pageCache, logProvider,
(DynamicStringStore) getOrCreateStore( StoreType.RELATIONSHIP_TYPE_TOKEN_NAME ), recordFormats ) );
(DynamicStringStore) getOrCreateStore( StoreType.RELATIONSHIP_TYPE_TOKEN_NAME ), recordFormats,
openOptions ) );
}

CommonAbstractStore createLabelTokenStore( String storeName )
{
File fileName = getStoreFile( storeName );
return initialize( new LabelTokenStore( fileName, config, idGeneratorFactory, pageCache,
logProvider, (DynamicStringStore) getOrCreateStore( StoreType.LABEL_TOKEN_NAME ), recordFormats ) );
logProvider, (DynamicStringStore) getOrCreateStore( StoreType.LABEL_TOKEN_NAME ), recordFormats,
openOptions ) );
}

CommonAbstractStore createSchemaStore( String storeName )
{
File fileName = getStoreFile( storeName );
return initialize( new SchemaStore( fileName, config, IdType.SCHEMA, idGeneratorFactory, pageCache,
logProvider, recordFormats.dynamic(), recordFormats.storeVersion() ) );
logProvider, recordFormats, openOptions ) );
}

CommonAbstractStore createRelationshipGroupStore( String storeName )
{
File storeFile = getStoreFile( storeName );
return initialize( new RelationshipGroupStore( storeFile, config, idGeneratorFactory, pageCache, logProvider,
recordFormats.relationshipGroup(), recordFormats.storeVersion() ) );
recordFormats, openOptions ) );
}

CountsTracker createCountStore( String storeName )
Expand Down Expand Up @@ -615,7 +622,7 @@ public long initialVersion()
CommonAbstractStore createMetadataStore()
{
return initialize( new MetaDataStore( neoStoreFileName, config, idGeneratorFactory, pageCache, logProvider,
recordFormats.metaData(), recordFormats.storeVersion() ) );
recordFormats.metaData(), recordFormats.storeVersion(), openOptions ) );
}

public void registerDiagnostics( DiagnosticsManager diagnosticsManager )
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.kernel.impl.store;

import java.io.File;
import java.nio.file.OpenOption;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -72,10 +73,11 @@ public NodeStore(
PageCache pageCache,
LogProvider logProvider,
DynamicArrayStore dynamicLabelStore,
RecordFormats recordFormats)
RecordFormats recordFormats,
OpenOption... openOptions )
{
super( fileName, config, IdType.NODE, idGeneratorFactory, pageCache, logProvider, TYPE_DESCRIPTOR,
recordFormats.node(), NO_STORE_HEADER_FORMAT, recordFormats.storeVersion() );
recordFormats.node(), NO_STORE_HEADER_FORMAT, recordFormats.storeVersion(), openOptions );
this.dynamicLabelStore = dynamicLabelStore;
}

Expand Down
Expand Up @@ -20,10 +20,11 @@
package org.neo4j.kernel.impl.store;

import java.io.File;
import java.nio.file.OpenOption;

import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.format.RecordFormat;
import org.neo4j.kernel.impl.store.format.RecordFormats;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.impl.store.id.IdType;
import org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord;
Expand All @@ -45,11 +46,12 @@ public PropertyKeyTokenStore(
PageCache pageCache,
LogProvider logProvider,
DynamicStringStore nameStore,
RecordFormat<PropertyKeyTokenRecord> recordFormat,
String storeVersion )
RecordFormats recordFormats,
OpenOption... openOptions )
{
super( fileName, config, IdType.PROPERTY_KEY_TOKEN, idGeneratorFactory, pageCache, logProvider, nameStore,
TYPE_DESCRIPTOR, new Token.Factory(), recordFormat, storeVersion );
TYPE_DESCRIPTOR, new Token.Factory(), recordFormats.propertyKeyToken(), recordFormats.storeVersion(),
openOptions );
}

@Override
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.kernel.impl.store;

import java.io.File;
import java.nio.file.OpenOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
Expand Down Expand Up @@ -73,10 +74,11 @@ public PropertyStore(
DynamicStringStore stringPropertyStore,
PropertyKeyTokenStore propertyKeyTokenStore,
DynamicArrayStore arrayPropertyStore,
RecordFormats recordFormats)
RecordFormats recordFormats,
OpenOption... openOptions )
{
super( fileName, configuration, IdType.PROPERTY, idGeneratorFactory, pageCache, logProvider, TYPE_DESCRIPTOR,
recordFormats.property(), NO_STORE_HEADER_FORMAT, recordFormats.storeVersion() );
recordFormats.property(), NO_STORE_HEADER_FORMAT, recordFormats.storeVersion(), openOptions );
this.stringStore = stringPropertyStore;
this.propertyKeyTokenStore = propertyKeyTokenStore;
this.arrayStore = arrayPropertyStore;
Expand Down

0 comments on commit d7f8800

Please sign in to comment.