Skip to content

Commit

Permalink
Faster SchemaStorageTest
Browse files Browse the repository at this point in the history
by sharing db and co-creating schema
  • Loading branch information
tinwelint committed Sep 19, 2016
1 parent 3db72d8 commit 7bf58e4
Showing 1 changed file with 78 additions and 44 deletions.
Expand Up @@ -20,6 +20,8 @@
package org.neo4j.kernel.impl.store; package org.neo4j.kernel.impl.store;


import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
Expand All @@ -28,10 +30,15 @@
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;


import org.neo4j.function.Predicates; import org.neo4j.function.Predicates;
import org.neo4j.graphdb.DependencyResolver; import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.schema.ConstraintDefinition;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.helpers.collection.Iterators; import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.ReadOperations; import org.neo4j.kernel.api.ReadOperations;
import org.neo4j.kernel.api.Statement; import org.neo4j.kernel.api.Statement;
Expand All @@ -49,12 +56,12 @@
import org.neo4j.kernel.impl.store.record.UniquePropertyConstraintRule; import org.neo4j.kernel.impl.store.record.UniquePropertyConstraintRule;
import org.neo4j.storageengine.api.schema.SchemaRule; import org.neo4j.storageengine.api.schema.SchemaRule;
import org.neo4j.test.mockito.matcher.KernelExceptionUserMessageMatcher; import org.neo4j.test.mockito.matcher.KernelExceptionUserMessageMatcher;
import org.neo4j.test.rule.EmbeddedDatabaseRule; import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.ImpermanentDatabaseRule;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.helpers.collection.Iterators.asSet; import static org.neo4j.helpers.collection.Iterators.asSet;
import static org.neo4j.kernel.impl.api.index.inmemory.InMemoryIndexProviderFactory.PROVIDER_DESCRIPTOR; import static org.neo4j.kernel.impl.api.index.inmemory.InMemoryIndexProviderFactory.PROVIDER_DESCRIPTOR;
import static org.neo4j.kernel.impl.store.record.UniquePropertyConstraintRule.uniquenessConstraintRule; import static org.neo4j.kernel.impl.store.record.UniquePropertyConstraintRule.uniquenessConstraintRule;
Expand All @@ -67,27 +74,45 @@ public class SchemaStorageTest
private static final String PROP1 = "prop1"; private static final String PROP1 = "prop1";
private static final String PROP2 = "prop2"; private static final String PROP2 = "prop2";


@Rule @ClassRule
public final EmbeddedDatabaseRule db = new EmbeddedDatabaseRule(); public static final DatabaseRule db = new ImpermanentDatabaseRule();
@Rule @Rule
public ExpectedException expectedException = ExpectedException.none(); public ExpectedException expectedException = ExpectedException.none();


private SchemaStorage storage; private static SchemaStorage storage;


@Before @BeforeClass
public void initStorage() throws Exception public static void initStorage() throws Exception
{ {
storage = new SchemaStorage( dependencyResolver().resolveDependency( RecordStorageEngine.class ) storage = new SchemaStorage( dependencyResolver().resolveDependency( RecordStorageEngine.class )
.testAccessNeoStores().getSchemaStore() ); .testAccessNeoStores().getSchemaStore() );
} }


@Before
public void clearSchema()
{
try ( Transaction tx = db.beginTx() )
{
for ( ConstraintDefinition constraint : db.schema().getConstraints() )
{
constraint.drop();
}
for ( IndexDefinition index : db.schema().getIndexes() )
{
index.drop();
}
tx.success();
}
}

@Test @Test
public void shouldReturnIndexRuleForLabelAndProperty() public void shouldReturnIndexRuleForLabelAndProperty()
{ {
// Given // Given
createIndex( LABEL1, PROP1 ); createSchema(
createIndex( LABEL1, PROP2 ); index( LABEL1, PROP1 ),
createIndex( LABEL2, PROP1 ); index( LABEL1, PROP2 ),
index( LABEL2, PROP1 ) );


// When // When
IndexRule rule = storage.indexRule( labelId( LABEL1 ), propId( PROP1 ) ); IndexRule rule = storage.indexRule( labelId( LABEL1 ), propId( PROP1 ) );
Expand All @@ -103,7 +128,8 @@ public void shouldReturnIndexRuleForLabelAndProperty()
public void shouldReturnNullIfIndexRuleForLabelAndPropertyDoesNotExist() public void shouldReturnNullIfIndexRuleForLabelAndPropertyDoesNotExist()
{ {
// Given // Given
createIndex( LABEL1, PROP1 ); createSchema(
index( LABEL1, PROP1 ) );


// When // When
IndexRule rule = storage.indexRule( labelId( LABEL1 ), propId( PROP2 ) ); IndexRule rule = storage.indexRule( labelId( LABEL1 ), propId( PROP2 ) );
Expand All @@ -116,8 +142,9 @@ public void shouldReturnNullIfIndexRuleForLabelAndPropertyDoesNotExist()
public void shouldListIndexRulesForLabelPropertyAndKind() public void shouldListIndexRulesForLabelPropertyAndKind()
{ {
// Given // Given
createUniquenessConstraint( LABEL1, PROP1 ); createSchema(
createIndex( LABEL1, PROP2 ); uniquenessConstraint( LABEL1, PROP1 ),
index( LABEL1, PROP2 ) );


// When // When
IndexRule rule = storage.indexRule( labelId( LABEL1 ), propId( PROP1 ), IndexRuleKind.CONSTRAINT ); IndexRule rule = storage.indexRule( labelId( LABEL1 ), propId( PROP1 ), IndexRuleKind.CONSTRAINT );
Expand All @@ -133,9 +160,10 @@ public void shouldListIndexRulesForLabelPropertyAndKind()
public void shouldListAllIndexRules() public void shouldListAllIndexRules()
{ {
// Given // Given
createIndex( LABEL1, PROP1 ); createSchema(
createIndex( LABEL1, PROP2 ); index( LABEL1, PROP1 ),
createUniquenessConstraint( LABEL2, PROP1 ); index( LABEL1, PROP2 ),
uniquenessConstraint( LABEL2, PROP1 ) );


// When // When
Set<IndexRule> listedRules = asSet( storage.allIndexRules() ); Set<IndexRule> listedRules = asSet( storage.allIndexRules() );
Expand All @@ -153,8 +181,9 @@ public void shouldListAllIndexRules()
public void shouldListAllSchemaRulesForNodes() public void shouldListAllSchemaRulesForNodes()
{ {
// Given // Given
createIndex( LABEL2, PROP1 ); createSchema(
createUniquenessConstraint( LABEL1, PROP1 ); index( LABEL2, PROP1 ),
uniquenessConstraint( LABEL1, PROP1 ) );


// When // When
Set<NodePropertyConstraintRule> listedRules = asSet( storage.schemaRulesForNodes( value -> value, NodePropertyConstraintRule.class, labelId( LABEL1 ), Set<NodePropertyConstraintRule> listedRules = asSet( storage.schemaRulesForNodes( value -> value, NodePropertyConstraintRule.class, labelId( LABEL1 ),
Expand All @@ -171,8 +200,9 @@ public void shouldListAllSchemaRulesForNodes()
public void shouldListSchemaRulesByClass() public void shouldListSchemaRulesByClass()
{ {
// Given // Given
createIndex( LABEL1, PROP1 ); createSchema(
createUniquenessConstraint( LABEL2, PROP1 ); index( LABEL1, PROP1 ),
uniquenessConstraint( LABEL2, PROP1 ) );


// When // When
Set<UniquePropertyConstraintRule> listedRules = asSet( Set<UniquePropertyConstraintRule> listedRules = asSet(
Expand All @@ -190,8 +220,9 @@ public void shouldReturnCorrectUniquenessRuleForLabelAndProperty()
throws SchemaRuleNotFoundException, DuplicateSchemaRuleException throws SchemaRuleNotFoundException, DuplicateSchemaRuleException
{ {
// Given // Given
createUniquenessConstraint( LABEL1, PROP1 ); createSchema(
createUniquenessConstraint( LABEL2, PROP1 ); uniquenessConstraint( LABEL1, PROP1 ),
uniquenessConstraint( LABEL2, PROP1 ) );


// When // When
UniquePropertyConstraintRule rule = storage.uniquenessConstraint( labelId( LABEL1 ), propId( PROP1 ) ); UniquePropertyConstraintRule rule = storage.uniquenessConstraint( labelId( LABEL1 ), propId( PROP1 ) );
Expand Down Expand Up @@ -304,27 +335,7 @@ private RelationshipPropertyExistenceConstraintRule getRelationshipPropertyExist
.relPropertyExistenceConstraintRule( id, typeId( type ), propId( property ) ); .relPropertyExistenceConstraintRule( id, typeId( type ), propId( property ) );
} }


private void createIndex( String labelName, String propertyName ) private static void awaitIndexes()
{
try ( Transaction tx = db.beginTx() )
{
db.schema().indexFor( label( labelName ) ).on( propertyName ).create();
tx.success();
}
awaitIndexes();
}

private void createUniquenessConstraint( String labelName, String propertyName )
{
try ( Transaction tx = db.beginTx() )
{
db.schema().constraintFor( label( labelName ) ).assertPropertyIsUnique( propertyName ).create();
tx.success();
}
awaitIndexes();
}

private void awaitIndexes()
{ {
try ( Transaction tx = db.beginTx() ) try ( Transaction tx = db.beginTx() )
{ {
Expand Down Expand Up @@ -364,9 +375,32 @@ private ReadOperations readOps()
return statement.readOperations(); return statement.readOperations();
} }


private DependencyResolver dependencyResolver() private static DependencyResolver dependencyResolver()
{ {
return db.getGraphDatabaseAPI().getDependencyResolver(); return db.getGraphDatabaseAPI().getDependencyResolver();
} }


private Consumer<GraphDatabaseService> index( String label, String prop )
{
return db -> db.schema().indexFor( Label.label( label ) ).on( prop ).create();
}

private Consumer<GraphDatabaseService> uniquenessConstraint( String label, String prop )
{
return db -> db.schema().constraintFor( Label.label( label ) ).assertPropertyIsUnique( prop ).create();
}

@SafeVarargs
private static void createSchema( Consumer<GraphDatabaseService>... creators )
{
try ( Transaction tx = db.beginTx() )
{
for ( Consumer<GraphDatabaseService> rule : creators )
{
rule.accept( db );
}
tx.success();
}
awaitIndexes();
}
} }

0 comments on commit 7bf58e4

Please sign in to comment.