Skip to content

Commit

Permalink
Can create composite index in Core API
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd authored and chrisvest committed Mar 9, 2017
1 parent 5958ba6 commit 347f880
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
Expand Up @@ -53,7 +53,6 @@
import org.neo4j.unsafe.impl.internal.dragons.FeatureToggles;

import static java.lang.String.format;

import static org.neo4j.collection.primitive.PrimitiveIntCollections.contains;
import static org.neo4j.kernel.impl.api.index.IndexPopulationFailure.failure;

Expand Down Expand Up @@ -310,10 +309,13 @@ public void flipAfterPopulation()

private int[] propertyKeyIds()
{
return populations.stream()
.flatMapToInt( population ->
IntStream.of( population.descriptor.schema().getPropertyIds() ) )
.toArray();
return populations.stream().flatMapToInt( this::propertyKeyIds ).distinct().toArray();
}

private IntStream propertyKeyIds( IndexPopulation population )
{
NewIndexDescriptor desc = population.descriptor;
return IntStream.of( desc.schema().getPropertyIds() );
}

private int[] labelIds()
Expand Down
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.neo4j.graphdb.ConstraintViolationException;
import org.neo4j.graphdb.Label;
Expand Down Expand Up @@ -54,14 +55,7 @@ private IndexCreatorImpl( InternalSchemaActions actions, Label label, Collection
public IndexCreator on( String propertyKey )
{
assertInUnterminatedTransaction();

if ( !propertyKeys.isEmpty() )
throw new UnsupportedOperationException(
"Compound indexes are not yet supported, only one property per index is allowed." );

return
new IndexCreatorImpl( actions, label,
Iterables.addToCollection( asList( propertyKey ), new ArrayList<>( propertyKeys ) ) );
return new IndexCreatorImpl( actions, label, copyAndAdd( propertyKeys, propertyKey) );
}

@Override
Expand All @@ -79,4 +73,11 @@ protected void assertInUnterminatedTransaction()
{
actions.assertInOpenTransaction();
}

private Collection<String> copyAndAdd( Collection<String> propertyKeys, String propertyKey )
{
Collection<String> ret = new ArrayList<>( propertyKeys );
ret.add( propertyKey );
return ret;
}
}
Expand Up @@ -59,6 +59,7 @@ public class SchemaAcceptanceTest
private GraphDatabaseService db;
private Label label = Labels.MY_LABEL;
private String propertyKey = "my_property_key";
private String secondPropertyKey = "my_second_property_key";

private enum Labels implements Label
{
Expand All @@ -82,6 +83,16 @@ public void addingAnIndexingRuleShouldSucceed() throws Exception
assertThat( getIndexes( db, label ), containsOnly( index ) );
}

@Test
public void addingACompositeIndexingRuleShouldSucceed() throws Exception
{
// WHEN
IndexDefinition index = createIndex( db, label, propertyKey, secondPropertyKey );

// THEN
assertThat( getIndexes( db, label ), containsOnly( index ) );
}

@Test
public void addingAnIndexingRuleInNestedTxShouldSucceed() throws Exception
{
Expand Down Expand Up @@ -156,25 +167,6 @@ public void shouldThrowConstraintViolationIfAskedToIndexPropertyThatIsAlreadyInd
assertThat( caught, not( nullValue() ) );
}

@Test
public void shouldThrowConstraintViolationIfAskedToCreateCompoundIndex() throws Exception
{
// WHEN
try ( Transaction tx = db.beginTx() )
{
Schema schema = db.schema();
schema.indexFor( label )
.on( "my_property_key" )
.on( "other_property" ).create();
tx.success();
fail( "Should not be able to create index on multiple propertyKey keys" );
}
catch ( UnsupportedOperationException e )
{
assertThat( e.getMessage(), containsString( "Compound indexes" ) );
}
}

@Test
public void shouldThrowConstraintViolationIfAskedToCreateCompoundConstraint() throws Exception
{
Expand Down Expand Up @@ -293,6 +285,20 @@ public void awaitingAllIndexesComingOnlineWorks()
}
}

@Test
public void shouldPopulateIndex() throws Exception
{
// GIVEN
Node node = createNode( db, propertyKey, "Neo", label );

// create an index
IndexDefinition index = createIndex( db, label, propertyKey );
waitForIndex( db, index );

// THEN
assertThat( findNodesByLabelAndProperty( label, propertyKey, "Neo", db ), containsOnly( node ) );
}

@Test
public void shouldRecreateDroppedIndex() throws Exception
{
Expand Down
Expand Up @@ -39,6 +39,7 @@
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.schema.ConstraintDefinition;
import org.neo4j.graphdb.schema.IndexCreator;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.Schema;
import org.neo4j.helpers.collection.Iterables;
Expand Down Expand Up @@ -618,20 +619,25 @@ public void describeTo( Description description )
};
}

public static IndexDefinition createIndex( GraphDatabaseService beansAPI, Label label, String property )
public static IndexDefinition createIndex( GraphDatabaseService beansAPI, Label label, String... property )
{
IndexDefinition indexDef = createIndexNoWait( beansAPI, label, property );

waitForIndex( beansAPI, indexDef );
return indexDef;
}

public static IndexDefinition createIndexNoWait( GraphDatabaseService beansAPI, Label label, String property )
public static IndexDefinition createIndexNoWait( GraphDatabaseService beansAPI, Label label, String... properties )
{
IndexDefinition indexDef;
try ( Transaction tx = beansAPI.beginTx() )
{
indexDef = beansAPI.schema().indexFor( label ).on( property ).create();
IndexCreator indexCreator = beansAPI.schema().indexFor( label );
for ( String property : properties )
{
indexCreator = indexCreator.on( property );
}
indexDef = indexCreator.create();
tx.success();
}
return indexDef;
Expand Down

0 comments on commit 347f880

Please sign in to comment.