Skip to content

Commit

Permalink
Implemented StringSchemaIndexProvider
Browse files Browse the repository at this point in the history
Broke out commonlatities from NumberSchemaIndexProvider into NativeSchemaIndexProvider
  • Loading branch information
tinwelint authored and burqen committed Feb 15, 2018
1 parent 81e91ba commit 85cd232
Show file tree
Hide file tree
Showing 8 changed files with 805 additions and 508 deletions.
@@ -0,0 +1,182 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.index.schema;

import org.neo4j.kernel.api.index.IndexDirectoryStructure.Factory;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.kernel.impl.storemigration.StoreMigrationParticipant;

import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_FAILED;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_ONLINE;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_POPULATING;

import java.io.File;
import java.io.IOException;

import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.index.internal.gbptree.Layout;
import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector;
import org.neo4j.internal.kernel.api.InternalIndexState;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.index.IndexAccessor;
import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.index.SchemaIndexProvider;

abstract class NativeSchemaIndexProvider<KEY extends NativeSchemaKey,VALUE extends NativeSchemaValue> extends SchemaIndexProvider
{
protected final PageCache pageCache;
protected final FileSystemAbstraction fs;
protected final Monitor monitor;
protected final RecoveryCleanupWorkCollector recoveryCleanupWorkCollector;
protected final boolean readOnly;

protected NativeSchemaIndexProvider( Descriptor descriptor, int priority, Factory directoryStructureFactory, PageCache pageCache,
FileSystemAbstraction fs, Monitor monitor, RecoveryCleanupWorkCollector recoveryCleanupWorkCollector, boolean readOnly )
{
super( descriptor, priority, directoryStructureFactory );
this.pageCache = pageCache;
this.fs = fs;
this.monitor = monitor;
this.recoveryCleanupWorkCollector = recoveryCleanupWorkCollector;
this.readOnly = readOnly;
}

@Override
public IndexPopulator getPopulator( long indexId, IndexDescriptor descriptor, IndexSamplingConfig samplingConfig )
{
if ( readOnly )
{
throw new UnsupportedOperationException( "Can't create populator for read only index" );
}

File storeFile = nativeIndexFileFromIndexId( indexId );
switch ( descriptor.type() )
{
case GENERAL:
return new NativeNonUniqueSchemaIndexPopulator<>( pageCache, fs, storeFile, layoutNonUnique(), samplingConfig,
monitor, descriptor, indexId );
case UNIQUE:
return new NativeUniqueSchemaIndexPopulator<>( pageCache, fs, storeFile, layoutUnique(), monitor, descriptor,
indexId );
default:
throw new UnsupportedOperationException( "Can not create index populator of type " + descriptor.type() );
}
}

@Override
public IndexAccessor getOnlineAccessor(
long indexId, IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) throws IOException
{
File storeFile = nativeIndexFileFromIndexId( indexId );
Layout<KEY,VALUE> layout = layout( descriptor );
return newIndexAccessor( storeFile, layout, descriptor, indexId, samplingConfig );
}

protected abstract IndexAccessor newIndexAccessor( File storeFile, Layout<KEY,VALUE> layout, IndexDescriptor descriptor,
long indexId, IndexSamplingConfig samplingConfig ) throws IOException;

@Override
public String getPopulationFailure( long indexId, IndexDescriptor descriptor ) throws IllegalStateException
{
try
{
String failureMessage = readPopulationFailure( indexId, descriptor );
if ( failureMessage == null )
{
throw new IllegalStateException( "Index " + indexId + " isn't failed" );
}
return failureMessage;
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}

@Override
public InternalIndexState getInitialState( long indexId, IndexDescriptor descriptor )
{
try
{
NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader();
GBPTree.readHeader( pageCache, nativeIndexFileFromIndexId( indexId ), 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 );
}
}
catch ( IOException e )
{
monitor.failedToOpenIndex( indexId, descriptor, "Requesting re-population.", e );
return InternalIndexState.POPULATING;
}
}

@Override
public StoreMigrationParticipant storeMigrationParticipant( FileSystemAbstraction fs, PageCache pageCache )
{
// Since this native provider is a new one, there's no need for migration on this level.
// Migration should happen in the combined layer for the time being.
return StoreMigrationParticipant.NOT_PARTICIPATING;
}

private String readPopulationFailure( long indexId, IndexDescriptor descriptor ) throws IOException
{
NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader();
GBPTree.readHeader( pageCache, nativeIndexFileFromIndexId( indexId ), layout( descriptor ), headerReader );
return headerReader.failureMessage;
}

private Layout<KEY,VALUE> layout( IndexDescriptor descriptor )
{
switch ( descriptor.type() )
{
case GENERAL:
return layoutNonUnique();
case UNIQUE:
return layoutUnique();
default:
throw new UnsupportedOperationException( "Can not create index accessor of type " + descriptor.type() );
}
}

protected abstract Layout<KEY,VALUE> layoutUnique();

protected abstract Layout<KEY,VALUE> layoutNonUnique();

private File nativeIndexFileFromIndexId( long indexId )
{
return new File( directoryStructure().directoryForIndex( indexId ), indexFileName( indexId ) );
}

private static String indexFileName( long indexId )
{
return "index-" + indexId;
}
}
Expand Up @@ -23,151 +23,53 @@
import java.io.IOException; import java.io.IOException;


import org.neo4j.index.internal.gbptree.GBPTree; import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.index.internal.gbptree.Layout;
import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector; import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector;
import org.neo4j.internal.kernel.api.IndexCapability; import org.neo4j.internal.kernel.api.IndexCapability;
import org.neo4j.internal.kernel.api.IndexOrder; import org.neo4j.internal.kernel.api.IndexOrder;
import org.neo4j.internal.kernel.api.IndexValueCapability; import org.neo4j.internal.kernel.api.IndexValueCapability;
import org.neo4j.internal.kernel.api.InternalIndexState;
import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache; import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.index.IndexAccessor; import org.neo4j.kernel.api.index.IndexAccessor;
import org.neo4j.kernel.api.index.IndexDirectoryStructure; import org.neo4j.kernel.api.index.IndexDirectoryStructure;
import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.index.SchemaIndexProvider;
import org.neo4j.kernel.api.schema.index.IndexDescriptor; import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig; import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.kernel.impl.storemigration.StoreMigrationParticipant;
import org.neo4j.values.storable.ValueGroup; import org.neo4j.values.storable.ValueGroup;


import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_FAILED;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_ONLINE;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_POPULATING;

/** /**
* Schema index provider for native indexes backed by e.g. {@link GBPTree}. * Schema index provider for native indexes backed by e.g. {@link GBPTree}.
*/ */
public class NumberSchemaIndexProvider extends SchemaIndexProvider public class NumberSchemaIndexProvider extends NativeSchemaIndexProvider<NumberSchemaKey,NativeSchemaValue>
{ {
public static final String KEY = "native"; public static final String KEY = "native";
public static final Descriptor NATIVE_PROVIDER_DESCRIPTOR = new Descriptor( KEY, "1.0" ); public static final Descriptor NATIVE_PROVIDER_DESCRIPTOR = new Descriptor( KEY, "1.0" );
static final IndexCapability CAPABILITY = new NativeIndexCapability(); static final IndexCapability CAPABILITY = new NativeIndexCapability();


private final PageCache pageCache;
private final FileSystemAbstraction fs;
private final Monitor monitor;
private final RecoveryCleanupWorkCollector recoveryCleanupWorkCollector;
private final boolean readOnly;

public NumberSchemaIndexProvider( PageCache pageCache, FileSystemAbstraction fs, public NumberSchemaIndexProvider( PageCache pageCache, FileSystemAbstraction fs,
IndexDirectoryStructure.Factory directoryStructure, Monitor monitor, RecoveryCleanupWorkCollector recoveryCleanupWorkCollector, IndexDirectoryStructure.Factory directoryStructure, Monitor monitor, RecoveryCleanupWorkCollector recoveryCleanupWorkCollector,
boolean readOnly ) boolean readOnly )
{ {
super( NATIVE_PROVIDER_DESCRIPTOR, 0, directoryStructure ); super( NATIVE_PROVIDER_DESCRIPTOR, 0, directoryStructure, pageCache, fs, monitor, recoveryCleanupWorkCollector, readOnly );
this.pageCache = pageCache;
this.fs = fs;
this.monitor = monitor;
this.recoveryCleanupWorkCollector = recoveryCleanupWorkCollector;
this.readOnly = readOnly;
} }


@Override @Override
public IndexPopulator getPopulator( long indexId, IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) protected NumberLayoutUnique layoutUnique()
{ {
if ( readOnly ) return new NumberLayoutUnique();
{
throw new UnsupportedOperationException( "Can't create populator for read only index" );
}

File storeFile = nativeIndexFileFromIndexId( indexId );
switch ( descriptor.type() )
{
case GENERAL:
return new NativeNonUniqueSchemaIndexPopulator<>( pageCache, fs, storeFile, new NumberLayoutNonUnique(), samplingConfig,
monitor, descriptor, indexId );
case UNIQUE:
return new NativeUniqueSchemaIndexPopulator<>( pageCache, fs, storeFile, new NumberLayoutUnique(), monitor, descriptor,
indexId );
default:
throw new UnsupportedOperationException( "Can not create index populator of type " + descriptor.type() );
}
} }


@Override @Override
public IndexAccessor getOnlineAccessor( protected NumberLayoutNonUnique layoutNonUnique()
long indexId, IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) throws IOException
{ {
File storeFile = nativeIndexFileFromIndexId( indexId ); return new NumberLayoutNonUnique();
NumberLayout layout = layout( descriptor );
return new NumberSchemaIndexAccessor<>( pageCache, fs, storeFile, layout, recoveryCleanupWorkCollector, monitor,
descriptor, indexId, samplingConfig );
}

private NumberLayout layout( IndexDescriptor descriptor )
{
NumberLayout layout;
switch ( descriptor.type() )
{
case GENERAL:
layout = new NumberLayoutNonUnique();
break;
case UNIQUE:
layout = new NumberLayoutUnique();
break;
default:
throw new UnsupportedOperationException( "Can not create index accessor of type " + descriptor.type() );
}
return layout;
} }


@Override @Override
public String getPopulationFailure( long indexId, IndexDescriptor descriptor ) throws IllegalStateException protected IndexAccessor newIndexAccessor( File storeFile, Layout<NumberSchemaKey,NativeSchemaValue> layout, IndexDescriptor descriptor,
{ long indexId, IndexSamplingConfig samplingConfig ) throws IOException
try
{
String failureMessage = readPopulationFailure( indexId, descriptor );
if ( failureMessage == null )
{
throw new IllegalStateException( "Index " + indexId + " isn't failed" );
}
return failureMessage;
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}

private String readPopulationFailure( long indexId, IndexDescriptor descriptor ) throws IOException
{ {
NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader(); return new NumberSchemaIndexAccessor<>( pageCache, fs, storeFile, layout, recoveryCleanupWorkCollector, monitor, descriptor,
GBPTree.readHeader( pageCache, nativeIndexFileFromIndexId( indexId ), layout( descriptor ), headerReader ); indexId, samplingConfig );
return headerReader.failureMessage;
}

@Override
public InternalIndexState getInitialState( long indexId, IndexDescriptor descriptor )
{
try
{
NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader();
GBPTree.readHeader( pageCache, nativeIndexFileFromIndexId( indexId ), 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 );
}
}
catch ( IOException e )
{
monitor.failedToOpenIndex( indexId, descriptor, "Requesting re-population.", e );
return InternalIndexState.POPULATING;
}
} }


@Override @Override
Expand All @@ -176,24 +78,6 @@ public IndexCapability getCapability( IndexDescriptor indexDescriptor )
return CAPABILITY; return CAPABILITY;
} }


@Override
public StoreMigrationParticipant storeMigrationParticipant( FileSystemAbstraction fs, PageCache pageCache )
{
// Since this native provider is a new one, there's no need for migration on this level.
// Migration should happen in the combined layer for the time being.
return StoreMigrationParticipant.NOT_PARTICIPATING;
}

private File nativeIndexFileFromIndexId( long indexId )
{
return new File( directoryStructure().directoryForIndex( indexId ), indexFileName( indexId ) );
}

private static String indexFileName( long indexId )
{
return "index-" + indexId;
}

private static class NativeIndexCapability implements IndexCapability private static class NativeIndexCapability implements IndexCapability
{ {
private static final IndexOrder[] SUPPORTED_ORDER = {IndexOrder.ASCENDING}; private static final IndexOrder[] SUPPORTED_ORDER = {IndexOrder.ASCENDING};
Expand Down

0 comments on commit 85cd232

Please sign in to comment.