diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/SpatialKnownIndex.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/SpatialKnownIndex.java index 1cee71d982e6..f307764f9b7c 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/SpatialKnownIndex.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/SpatialKnownIndex.java @@ -117,35 +117,60 @@ public SpatialKnownIndex( IndexDirectoryStructure directoryStructure, Coordinate state = State.NONE; } - public void initialize( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) + public void initIfNeeded( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) { - assert state == State.NONE; - layout = layout( descriptor ); - treeKey = layout.newKey(); - treeValue = layout.newValue(); - schemaIndex = new NativeSchemaIndex<>( pageCache, fs, indexFile, layout, monitor, descriptor, indexId ); - if ( uniqueSampler( descriptor ) ) + if ( state == State.NONE ) { - uniqueSampler = new UniqueIndexSampler(); + initialize( descriptor, samplingConfig ); } - else + } + + public void createIfNeeded () throws IOException + { + if ( state == State.INIT ) { - generalSampler = new DefaultNonUniqueIndexSampler( samplingConfig.sampleSizeLimit() ); + // First add to sub-index, make sure to create + create(); } - state = State.INIT; } - public void online() throws IOException + public void takeOnline( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) throws IOException { - assert state == State.POPULATED || state == State.INIT; - singleUpdater = new NativeSchemaIndexUpdater<>( treeKey, treeValue ); - schemaIndex.instantiateTree( recoveryCleanupWorkCollector, NO_HEADER_WRITER ); - state = State.ONLINE; + initIfNeeded( descriptor, samplingConfig ); + if ( state == State.INIT || state == State.POPULATED ) + { + online(); + } + if ( state != State.ONLINE ) + { + throw new IllegalStateException( "" ); + } } - public State getState() + public IndexUpdater updateWithCreate(IndexDescriptor descriptor, IndexSamplingConfig samplingConfig, boolean populating ) throws IOException { - return state; + if ( populating ) + { + if ( state == State.NONE ) + { + // sub-index didn't exist, create in populating mode + initialize( descriptor, samplingConfig ); + create(); + } + return newPopulatingUpdater(); + } + else + { + if ( state == State.NONE ) + { + // sub-index didn't exist, create and make it online + initialize( descriptor, samplingConfig ); + create(); + close( true ); + online(); + } + return newUpdater(); + } } public synchronized void drop() throws IOException @@ -163,17 +188,6 @@ public synchronized void drop() throws IOException } } - public synchronized void create() throws IOException - { - assert state == State.INIT; - // extra check here??? - schemaIndex.gbpTreeFileUtil.deleteFileIfPresent( indexFile ); /// TODO <- warning - schemaIndex.instantiateTree( RecoveryCleanupWorkCollector.IMMEDIATE, new NativeSchemaIndexHeaderWriter( BYTE_POPULATING ) ); - instantiateWriter(); - workSync = new WorkSync<>( new IndexUpdateApply<>( treeKey, treeValue, singleTreeWriter, new ConflictDetectingValueMerger<>() ) ); - state = State.POPULATING; - } - public void close() throws IOException { schemaIndex.closeTree(); @@ -343,6 +357,72 @@ public IndexReader newReader( IndexSamplingConfig samplingConfig, IndexDescripto return new SpatialSchemaIndexReader<>( schemaIndex.tree, layout, samplingConfig, descriptor ); } + public boolean indexExists() + { + return fs.fileExists( indexFile ); + } + + public String readPopulationFailure( IndexDescriptor descriptor ) throws IOException + { + NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader(); + GBPTree.readHeader( pageCache, indexFile, layout( descriptor ), headerReader ); + return headerReader.failureMessage; + } + + public InternalIndexState readState( IndexDescriptor descriptor ) throws IOException + { + NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader(); + GBPTree.readHeader( pageCache, indexFile, layout( descriptor ), headerReader ); + switch ( headerReader.state ) + { + case BYTE_FAILED: + return InternalIndexState.FAILED; + case BYTE_ONLINE: + return InternalIndexState.ONLINE; + case BYTE_POPULATING: + return InternalIndexState.POPULATING; + default: + throw new IllegalStateException( "Unexpected initial state byte value " + headerReader.state ); + } + } + + private synchronized void create() throws IOException + { + assert state == State.INIT; + // extra check here??? + schemaIndex.gbpTreeFileUtil.deleteFileIfPresent( indexFile ); /// TODO <- warning + schemaIndex.instantiateTree( RecoveryCleanupWorkCollector.IMMEDIATE, new NativeSchemaIndexHeaderWriter( BYTE_POPULATING ) ); + instantiateWriter(); + workSync = new WorkSync<>( new IndexUpdateApply<>( treeKey, treeValue, singleTreeWriter, new ConflictDetectingValueMerger<>() ) ); + state = State.POPULATING; + } + + private void initialize( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) + { + assert state == State.NONE; + layout = layout( descriptor ); + treeKey = layout.newKey(); + treeValue = layout.newValue(); + schemaIndex = new NativeSchemaIndex<>( pageCache, fs, indexFile, layout, monitor, descriptor, indexId ); + if ( uniqueSampler( descriptor ) ) + { + uniqueSampler = new UniqueIndexSampler(); + } + else + { + generalSampler = new DefaultNonUniqueIndexSampler( samplingConfig.sampleSizeLimit() ); + } + state = State.INIT; + } + + private void online() throws IOException + { + assert state == State.POPULATED || state == State.INIT; + singleUpdater = new NativeSchemaIndexUpdater<>( treeKey, treeValue ); + schemaIndex.instantiateTree( recoveryCleanupWorkCollector, NO_HEADER_WRITER ); + state = State.ONLINE; + } + private void instantiateWriter() throws IOException { assert singleTreeWriter == null; @@ -434,35 +514,6 @@ private static String indexFileName( long indexId ) return "index-" + indexId; } - public boolean indexExists() - { - return fs.fileExists( indexFile ); - } - - public String readPopulationFailure( IndexDescriptor descriptor ) throws IOException - { - NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader(); - GBPTree.readHeader( pageCache, indexFile, layout( descriptor ), headerReader ); - return headerReader.failureMessage; - } - - public InternalIndexState readState( IndexDescriptor descriptor ) throws IOException - { - NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader(); - GBPTree.readHeader( pageCache, indexFile, layout( descriptor ), headerReader ); - switch ( headerReader.state ) - { - case BYTE_FAILED: - return InternalIndexState.FAILED; - case BYTE_ONLINE: - return InternalIndexState.ONLINE; - case BYTE_POPULATING: - return InternalIndexState.POPULATING; - default: - throw new IllegalStateException( "Unexpected initial state byte value " + headerReader.state ); - } - } - private SpatialLayout layout( IndexDescriptor descriptor ) { SpatialLayout layout; @@ -490,7 +541,7 @@ private boolean uniqueSampler( IndexDescriptor descriptor ) } } - public enum State + private enum State { NONE, INIT, diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexAccessor.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexAccessor.java index da94b4e6fe25..0871eafd1f50 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexAccessor.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexAccessor.java @@ -62,18 +62,7 @@ class SpatialFusionIndexAccessor implements IndexAccessor this.indexFactory = indexFactory; for ( SpatialKnownIndex index : indexMap.values() ) { - if ( index.getState() == SpatialKnownIndex.State.NONE ) - { - index.initialize( descriptor, samplingConfig ); - } - if ( index.getState() == SpatialKnownIndex.State.INIT || index.getState() == SpatialKnownIndex.State.POPULATED ) - { - index.online(); - } - if ( index.getState() != SpatialKnownIndex.State.ONLINE ) - { - throw new IllegalStateException( "" ); - } + index.takeOnline(descriptor, samplingConfig); } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexPopulator.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexPopulator.java index 1c5418551891..68eea460fd56 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexPopulator.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexPopulator.java @@ -88,11 +88,7 @@ public void add( Collection> updates ) throws Inde for ( CoordinateReferenceSystem crs : batchMap.keySet() ) { SpatialKnownIndex index = getOrCreateInitializedIndex( crs ); - if ( index.getState() == SpatialKnownIndex.State.INIT ) - { - // First add to sub-index, make sure to create - index.create(); - } + index.createIfNeeded(); index.add( batchMap.get( crs ) ); } } @@ -142,10 +138,7 @@ public void includeSample( IndexEntryUpdate update ) private SpatialKnownIndex getOrCreateInitializedIndex( CoordinateReferenceSystem crs ) { SpatialKnownIndex index = indexFactory.selectAndCreate( indexMap, indexId, crs ); - if ( index.getState() == SpatialKnownIndex.State.NONE ) - { - index.initialize( descriptor, samplingConfig ); - } + index.initIfNeeded( descriptor, samplingConfig ); return index; } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexUpdater.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexUpdater.java index 8880e9ebb207..65804a4ed00b 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexUpdater.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexUpdater.java @@ -113,28 +113,8 @@ private IndexUpdater selectUpdater( Value... values ) throws IOException return updater; } SpatialKnownIndex index = indexFactory.selectAndCreate( indexMap, indexId, crs ); - if ( populating ) - { - if ( index.getState() == SpatialKnownIndex.State.NONE ) - { - // sub-index didn't exist, create in populating mode - index.initialize( descriptor, samplingConfig ); - index.create(); - } - return remember( crs, index.newPopulatingUpdater() ); - } - else - { - if ( index.getState() == SpatialKnownIndex.State.NONE ) - { - // sub-index didn't exist, create and make it online - index.initialize( descriptor, samplingConfig ); - index.create(); - index.close( true ); - index.online(); - } - return remember( crs, index.newUpdater() ); - } + IndexUpdater indexUpdater = index.updateWithCreate( descriptor, samplingConfig, populating ); + return remember( crs, indexUpdater ); } private IndexUpdater remember( CoordinateReferenceSystem crs, IndexUpdater indexUpdater ) diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexAccessorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexAccessorTest.java index 45df00eff917..d0dbfe1a49ba 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexAccessorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/fusion/SpatialFusionIndexAccessorTest.java @@ -73,7 +73,6 @@ public void setup() throws Exception for ( CoordinateReferenceSystem crs : asList( WGS84, Cartesian ) ) { indexMap.put( crs, mock( SpatialKnownIndex.class ) ); - when( indexMap.get( crs ).getState() ).thenReturn( SpatialKnownIndex.State.ONLINE ); } fusionIndexAccessor = new SpatialFusionIndexAccessor( indexMap, 0, mock( IndexDescriptor.class ), null, indexFactory );