Skip to content

Commit

Permalink
Made spatial index settings immutable
Browse files Browse the repository at this point in the history
So that the difference between config based settings for new indexes
is more clearly separate from settings read from existing indexes.
  • Loading branch information
craigtaverner authored and burqen committed Jun 28, 2018
1 parent 8d5e68f commit ea6160a
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 151 deletions.
Expand Up @@ -189,7 +189,7 @@ static class PartAccessor extends NativeIndexAccessor<SpatialIndexKey,NativeInde
RecoveryCleanupWorkCollector recoveryCleanupWorkCollector, IndexProvider.Monitor monitor, StoreIndexDescriptor descriptor, RecoveryCleanupWorkCollector recoveryCleanupWorkCollector, IndexProvider.Monitor monitor, StoreIndexDescriptor descriptor,
IndexSamplingConfig samplingConfig, SpaceFillingCurveConfiguration searchConfiguration ) throws IOException IndexSamplingConfig samplingConfig, SpaceFillingCurveConfiguration searchConfiguration ) throws IOException
{ {
super( pageCache, fs, fileLayout.indexFile, fileLayout.layout, recoveryCleanupWorkCollector, monitor, descriptor, samplingConfig ); super( pageCache, fs, fileLayout.spatialFile.indexFile, fileLayout.layout, recoveryCleanupWorkCollector, monitor, descriptor, samplingConfig );
this.layout = fileLayout.layout; this.layout = fileLayout.layout;
this.descriptor = descriptor; this.descriptor = descriptor;
this.samplingConfig = samplingConfig; this.samplingConfig = samplingConfig;
Expand Down Expand Up @@ -237,19 +237,21 @@ static class PartFactory implements Factory<PartAccessor>
@Override @Override
public PartAccessor newSpatial( CoordinateReferenceSystem crs ) throws IOException public PartAccessor newSpatial( CoordinateReferenceSystem crs ) throws IOException
{ {
return createPartAccessor( spatialIndexFiles.forCrs( crs ) ); SpatialIndexFiles.SpatialFile indexFile = spatialIndexFiles.forCrs( crs );
} if ( !fs.fileExists( indexFile.indexFile ) )

private PartAccessor createPartAccessor( SpatialIndexFiles.SpatialFileLayout fileLayout ) throws IOException
{
if ( !fs.fileExists( fileLayout.indexFile ) )
{ {
SpatialIndexFiles.SpatialFileLayout fileLayout = indexFile.getLayoutForNewIndex();
createEmptyIndex( fileLayout ); createEmptyIndex( fileLayout );
return createPartAccessor( fileLayout );
} }
else else
{ {
fileLayout.readHeader( pageCache ); return createPartAccessor( indexFile.getLayoutForExistingIndex( pageCache ) );
} }
}

private PartAccessor createPartAccessor( SpatialIndexFiles.SpatialFileLayout fileLayout ) throws IOException
{
return new PartAccessor( pageCache, return new PartAccessor( pageCache,
fs, fs,
fileLayout, fileLayout,
Expand Down
Expand Up @@ -26,7 +26,6 @@
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;


import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.index.internal.gbptree.Layout; import org.neo4j.index.internal.gbptree.Layout;
import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache; import org.neo4j.io.pagecache.PageCache;
Expand All @@ -49,27 +48,27 @@ class SpatialIndexFiles
indexDirectory = directoryStructure.directoryForIndex( indexId ); indexDirectory = directoryStructure.directoryForIndex( indexId );
} }


Iterable<SpatialFileLayout> existing() Iterable<SpatialFile> existing()
{ {
List<SpatialFileLayout> existing = new ArrayList<>(); List<SpatialFile> existing = new ArrayList<>();
addExistingFiles( existing ); addExistingFiles( existing );
return existing; return existing;
} }


<T> void loadExistingIndexes( SpatialIndexCache<T> indexCache ) throws IOException <T> void loadExistingIndexes( SpatialIndexCache<T> indexCache ) throws IOException
{ {
for ( SpatialFileLayout fileLayout : existing() ) for ( SpatialFile fileLayout : existing() )
{ {
indexCache.select( fileLayout.crs ); indexCache.select( fileLayout.crs );
} }
} }


SpatialFileLayout forCrs( CoordinateReferenceSystem crs ) SpatialFile forCrs( CoordinateReferenceSystem crs )
{ {
return new SpatialFileLayout( crs, settingsFactory.settingsFor( crs ).clone(), indexDirectory ); return new SpatialFile( crs, settingsFactory, indexDirectory );
} }


private void addExistingFiles( List<SpatialFileLayout> existing ) private void addExistingFiles( List<SpatialFile> existing )
{ {
File[] files = fs.listFiles( indexDirectory ); File[] files = fs.listFiles( indexDirectory );
if ( files != null ) if ( files != null )
Expand All @@ -89,30 +88,50 @@ private void addExistingFiles( List<SpatialFileLayout> existing )
} }
} }


static class SpatialFileLayout static class SpatialFile
{ {
final File indexFile; final File indexFile;
final SpaceFillingCurveSettings settings; final SpaceFillingCurveSettingsFactory settings;
private final CoordinateReferenceSystem crs; private final CoordinateReferenceSystem crs;
Layout<SpatialIndexKey,NativeIndexValue> layout;


SpatialFileLayout( CoordinateReferenceSystem crs, SpaceFillingCurveSettings settings, File indexDirectory ) SpatialFile( CoordinateReferenceSystem crs, SpaceFillingCurveSettingsFactory settingsFactory, File indexDirectory )
{ {
this.crs = crs; this.crs = crs;
this.settings = settings; this.settings = settingsFactory;
this.layout = new SpatialLayout( crs, settings.curve() );
String s = crs.getTable().getTableId() + "-" + Integer.toString( crs.getCode() ); String s = crs.getTable().getTableId() + "-" + Integer.toString( crs.getCode() );
this.indexFile = new File( indexDirectory, s ); this.indexFile = new File( indexDirectory, s );
} }


public void readHeader( PageCache pageCache ) throws IOException /**
* If this is the first time an index is being created, get the layout settings from the config settings only
*/
SpatialFileLayout getLayoutForNewIndex()
{ {
GBPTree.readHeader( pageCache, indexFile, settings.headerReader( NativeIndexHeaderReader::readFailureMessage ) ); return new SpatialFileLayout( this, settings.settingsFor( crs ) );
if ( settings.isFailed() ) }
{
throw new IOException( settings.getFailureMessage() ); /**
} * If we are loading a layout for an existing index, read the settings from the index header, and ignore config settings
this.layout = new SpatialLayout( crs, settings.curve() ); */
SpatialFileLayout getLayoutForExistingIndex( PageCache pageCache ) throws IOException
{
SpaceFillingCurveSettings settings =
SpaceFillingCurveSettings.fromGBPTree( indexFile, pageCache, NativeIndexHeaderReader::readFailureMessage );
return new SpatialFileLayout( this, settings );
}
}

static class SpatialFileLayout
{
final SpaceFillingCurveSettings settings;
final SpatialFile spatialFile;
final Layout<SpatialIndexKey,NativeIndexValue> layout;

SpatialFileLayout( SpatialFile spatialFile, SpaceFillingCurveSettings settings )
{
this.spatialFile = spatialFile;
this.settings = settings;
this.layout = new SpatialLayout( spatialFile.crs, settings.curve() );
} }
} }
} }
Expand Up @@ -151,7 +151,7 @@ static class PartPopulator extends NativeIndexPopulator<SpatialIndexKey,NativeIn
PartPopulator( PageCache pageCache, FileSystemAbstraction fs, SpatialIndexFiles.SpatialFileLayout fileLayout, IndexProvider.Monitor monitor, PartPopulator( PageCache pageCache, FileSystemAbstraction fs, SpatialIndexFiles.SpatialFileLayout fileLayout, IndexProvider.Monitor monitor,
StoreIndexDescriptor descriptor, IndexSamplingConfig samplingConfig, SpaceFillingCurveConfiguration configuration ) StoreIndexDescriptor descriptor, IndexSamplingConfig samplingConfig, SpaceFillingCurveConfiguration configuration )
{ {
super( pageCache, fs, fileLayout.indexFile, fileLayout.layout, monitor, descriptor, samplingConfig ); super( pageCache, fs, fileLayout.spatialFile.indexFile, fileLayout.layout, monitor, descriptor, samplingConfig );
this.configuration = configuration; this.configuration = configuration;
this.settings = fileLayout.settings; this.settings = fileLayout.settings;
} }
Expand Down Expand Up @@ -200,7 +200,7 @@ static class PartFactory implements Factory<PartPopulator>
@Override @Override
public PartPopulator newSpatial( CoordinateReferenceSystem crs ) throws IOException public PartPopulator newSpatial( CoordinateReferenceSystem crs ) throws IOException
{ {
return create( spatialIndexFiles.forCrs(crs) ); return create( spatialIndexFiles.forCrs( crs ).getLayoutForNewIndex() );
} }


private PartPopulator create( SpatialIndexFiles.SpatialFileLayout fileLayout ) throws IOException private PartPopulator create( SpatialIndexFiles.SpatialFileLayout fileLayout ) throws IOException
Expand Down
Expand Up @@ -118,7 +118,7 @@ public String getPopulationFailure( StoreIndexDescriptor descriptor ) throws Ill


try try
{ {
for ( SpatialIndexFiles.SpatialFileLayout subIndex : spatialIndexFiles.existing() ) for ( SpatialIndexFiles.SpatialFile subIndex : spatialIndexFiles.existing() )
{ {
String indexFailure = NativeIndexes.readFailureMessage( pageCache, subIndex.indexFile ); String indexFailure = NativeIndexes.readFailureMessage( pageCache, subIndex.indexFile );
if ( indexFailure != null ) if ( indexFailure != null )
Expand All @@ -139,9 +139,9 @@ public InternalIndexState getInitialState( StoreIndexDescriptor descriptor )
{ {
SpatialIndexFiles spatialIndexFiles = new SpatialIndexFiles( directoryStructure(), descriptor.getId(), fs, settingsFactory ); SpatialIndexFiles spatialIndexFiles = new SpatialIndexFiles( directoryStructure(), descriptor.getId(), fs, settingsFactory );


final Iterable<SpatialIndexFiles.SpatialFileLayout> existing = spatialIndexFiles.existing(); final Iterable<SpatialIndexFiles.SpatialFile> existing = spatialIndexFiles.existing();
InternalIndexState state = InternalIndexState.ONLINE; InternalIndexState state = InternalIndexState.ONLINE;
for ( SpatialIndexFiles.SpatialFileLayout subIndex : existing ) for ( SpatialIndexFiles.SpatialFile subIndex : existing )
{ {
try try
{ {
Expand Down

0 comments on commit ea6160a

Please sign in to comment.