Skip to content

Commit

Permalink
Verify IndexProvider in index migration test
Browse files Browse the repository at this point in the history
Had to rebuild 3.3 index to make labels used when creating the 3.2 store
align with expected provider.
  • Loading branch information
burqen committed Apr 13, 2018
1 parent cfebd68 commit 0e7d237
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
Expand Up @@ -38,11 +38,16 @@
import org.neo4j.graphdb.schema.IndexDefinition; import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.internal.kernel.api.CapableIndexReference; import org.neo4j.internal.kernel.api.CapableIndexReference;
import org.neo4j.internal.kernel.api.IndexQuery; import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.internal.kernel.api.InternalIndexState; import org.neo4j.internal.kernel.api.SchemaRead;
import org.neo4j.internal.kernel.api.TokenRead; import org.neo4j.internal.kernel.api.TokenRead;
import org.neo4j.internal.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.io.fs.FileUtils; import org.neo4j.io.fs.FileUtils;
import org.neo4j.kernel.api.KernelTransaction; import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.Statement; import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.impl.schema.LuceneIndexProviderFactory;
import org.neo4j.kernel.api.impl.schema.NativeLuceneFusionIndexProviderFactory10;
import org.neo4j.kernel.api.impl.schema.NativeLuceneFusionIndexProviderFactory20;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.configuration.Settings; import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.impl.api.KernelStatement; import org.neo4j.kernel.impl.api.KernelStatement;
import org.neo4j.kernel.impl.api.store.DefaultIndexReference; import org.neo4j.kernel.impl.api.store.DefaultIndexReference;
Expand All @@ -64,9 +69,9 @@ public class StartOldDbOn3_4AndCreateFusionIndexIT
private static final String ZIP_FILE_3_2 = "3_2-db.zip"; private static final String ZIP_FILE_3_2 = "3_2-db.zip";
private static final String ZIP_FILE_3_3 = "3_3-db.zip"; private static final String ZIP_FILE_3_3 = "3_3-db.zip";


private static final Label LABEL1 = Label.label( "Label1" ); private static final Label LABEL_LUCENE_10 = Label.label( "Label1" );
private static final Label LABEL2 = Label.label( "Label2" ); private static final Label LABEL_FUSION_10 = Label.label( "Label2" );
private static final Label LABEL0 = Label.label( "Label0" ); private static final Label LABEL_FUSION_20 = Label.label( "Label3" );
private static final String KEY1 = "key1"; private static final String KEY1 = "key1";
private static final String KEY2 = "key2"; private static final String KEY2 = "key2";


Expand All @@ -79,7 +84,7 @@ public void create3_2Database() throws Exception
{ {
File storeDir = tempStoreDirectory(); File storeDir = tempStoreDirectory();
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( storeDir ); GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( storeDir );
createIndexDataAndShutdown( db, LABEL1 ); createIndexDataAndShutdown( db, LABEL_LUCENE_10 );
} }


@Ignore( "Here as reference for how 3.3 db was created" ) @Ignore( "Here as reference for how 3.3 db was created" )
Expand All @@ -90,13 +95,13 @@ public void create3_3Database() throws Exception
GraphDatabaseFactory factory = new GraphDatabaseFactory(); GraphDatabaseFactory factory = new GraphDatabaseFactory();
GraphDatabaseBuilder builder = factory.newEmbeddedDatabaseBuilder( storeDir ); GraphDatabaseBuilder builder = factory.newEmbeddedDatabaseBuilder( storeDir );


builder.setConfig( GraphDatabaseSettings.enable_native_schema_index, Settings.TRUE ); builder.setConfig( GraphDatabaseSettings.enable_native_schema_index, Settings.FALSE );
GraphDatabaseService db = builder.newGraphDatabase(); GraphDatabaseService db = builder.newGraphDatabase();
createIndexDataAndShutdown( db, LABEL1 ); createIndexDataAndShutdown( db, LABEL_LUCENE_10 );


builder.setConfig( GraphDatabaseSettings.enable_native_schema_index, Settings.FALSE ); builder.setConfig( GraphDatabaseSettings.enable_native_schema_index, Settings.TRUE );
db = builder.newGraphDatabase(); db = builder.newGraphDatabase();
createIndexDataAndShutdown( db, LABEL2 ); createIndexDataAndShutdown( db, LABEL_FUSION_10 );
System.out.println( "Db created in " + storeDir.getAbsolutePath() ); System.out.println( "Db created in " + storeDir.getAbsolutePath() );
} }


Expand All @@ -111,19 +116,21 @@ public void shouldOpen3_2DbAndCreateAndWorkWithSomeFusionIndexes() throws Except
.newGraphDatabase(); .newGraphDatabase();
try try
{ {
verifyIndexes( db, LABEL1 ); verifyIndexes( db, LABEL_LUCENE_10 );


// when // when
createIndexesAndData( db, LABEL0 ); createIndexesAndData( db, LABEL_FUSION_20 );


// then // then
verifyIndexes( db, LABEL0 ); verifyIndexes( db, LABEL_FUSION_20 );


// when // when
additionalUpdates( db, LABEL1 ); additionalUpdates( db, LABEL_LUCENE_10 );


// then // then
verifyAfterAdditionalUpdate( db, LABEL1 ); verifyAfterAdditionalUpdate( db, LABEL_LUCENE_10 );
verifyExpectedProvider( db, LABEL_LUCENE_10, LuceneIndexProviderFactory.PROVIDER_DESCRIPTOR );
verifyExpectedProvider( db, LABEL_FUSION_20, NativeLuceneFusionIndexProviderFactory20.DESCRIPTOR );
} }
finally finally
{ {
Expand All @@ -142,33 +149,63 @@ public void shouldOpen3_3DbAndCreateAndWorkWithSomeFusionIndexes() throws Except
.newGraphDatabase(); .newGraphDatabase();
try try
{ {
verifyIndexes( db, LABEL1 ); verifyIndexes( db, LABEL_LUCENE_10 );
verifyIndexes( db, LABEL2 ); verifyIndexes( db, LABEL_FUSION_10 );


// when // when
createIndexesAndData( db, LABEL0 ); createIndexesAndData( db, LABEL_FUSION_20 );


// then // then
verifyIndexes( db, LABEL0 ); verifyIndexes( db, LABEL_FUSION_20 );


// when // when
additionalUpdates( db, LABEL1 ); additionalUpdates( db, LABEL_LUCENE_10 );


// then // then
verifyAfterAdditionalUpdate( db, LABEL1 ); verifyAfterAdditionalUpdate( db, LABEL_LUCENE_10 );


// when // when
additionalUpdates( db, LABEL2 ); additionalUpdates( db, LABEL_FUSION_10 );


// then // then
verifyAfterAdditionalUpdate( db, LABEL2 ); verifyAfterAdditionalUpdate( db, LABEL_FUSION_10 );
verifyExpectedProvider( db, LABEL_LUCENE_10, LuceneIndexProviderFactory.PROVIDER_DESCRIPTOR );
verifyExpectedProvider( db, LABEL_FUSION_10, NativeLuceneFusionIndexProviderFactory10.DESCRIPTOR );
verifyExpectedProvider( db, LABEL_FUSION_20, NativeLuceneFusionIndexProviderFactory20.DESCRIPTOR );
} }
finally finally
{ {
db.shutdown(); db.shutdown();
} }
} }


private void verifyExpectedProvider( GraphDatabaseAPI db, Label label, IndexProvider.Descriptor expectedDescriptor ) throws TransactionFailureException
{
try ( Transaction tx = db.beginTx();
KernelTransaction kernelTransaction =
db.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class ).getKernelTransactionBoundToThisThread( true ) )
{
TokenRead tokenRead = kernelTransaction.tokenRead();
SchemaRead schemaRead = kernelTransaction.schemaRead();

int labelId = tokenRead.nodeLabel( label.name() );
int key1Id = tokenRead.propertyKey( KEY1 );
int key2Id = tokenRead.propertyKey( KEY2 );

CapableIndexReference index = schemaRead.index( labelId, key1Id );
assertIndexHasExpectedProvider( expectedDescriptor, index );
index = schemaRead.index( labelId, key1Id, key2Id );
assertIndexHasExpectedProvider( expectedDescriptor, index );
tx.success();
}
}

private void assertIndexHasExpectedProvider( IndexProvider.Descriptor expectedDescriptor, CapableIndexReference index )
{
assertEquals( "same key", expectedDescriptor.getKey(), index.providerKey() );
assertEquals( "same version", expectedDescriptor.getVersion(), index.providerVersion() );
}

private void createIndexDataAndShutdown( GraphDatabaseService db, Label label ) private void createIndexDataAndShutdown( GraphDatabaseService db, Label label )
{ {
try try
Expand Down Expand Up @@ -231,22 +268,18 @@ private void additionalUpdates( GraphDatabaseAPI db, Label label )


private void verifyIndexes( GraphDatabaseAPI db, Label label ) throws Exception private void verifyIndexes( GraphDatabaseAPI db, Label label ) throws Exception
{ {
// There should be an index for the label and KEY1 containing 100 nodes
assertTrue( hasIndex( db, label, KEY1 ) ); assertTrue( hasIndex( db, label, KEY1 ) );
assertEquals( 100, countIndexedNodes( db, label, KEY1 ) ); assertEquals( 100, countIndexedNodes( db, label, KEY1 ) );


// There should be an index for the label and KEY1+KEY2 containing 34 nodes
assertTrue( hasIndex( db, label, KEY1, KEY2 ) ); assertTrue( hasIndex( db, label, KEY1, KEY2 ) );
assertEquals( 34, countIndexedNodes( db, label, KEY1, KEY2 ) ); assertEquals( 34, countIndexedNodes( db, label, KEY1, KEY2 ) );
} }


private void verifyAfterAdditionalUpdate( GraphDatabaseAPI db, Label label ) throws Exception private void verifyAfterAdditionalUpdate( GraphDatabaseAPI db, Label label ) throws Exception
{ {
// There should be an index for the label and KEY1 containing 100 nodes
assertTrue( hasIndex( db, label, KEY1 ) ); assertTrue( hasIndex( db, label, KEY1 ) );
assertEquals( 200, countIndexedNodes( db, label, KEY1 ) ); assertEquals( 200, countIndexedNodes( db, label, KEY1 ) );


// There should be an index for the label and KEY1+KEY2 containing 34 nodes
assertTrue( hasIndex( db, label, KEY1, KEY2 ) ); assertTrue( hasIndex( db, label, KEY1, KEY2 ) );
assertEquals( 68, countIndexedNodes( db, label, KEY1, KEY2 ) ); assertEquals( 68, countIndexedNodes( db, label, KEY1, KEY2 ) );
} }
Expand Down
Binary file modified community/neo4j/src/test/resources/migration/3_3-db.zip
Binary file not shown.

0 comments on commit 0e7d237

Please sign in to comment.