Skip to content

Commit

Permalink
db.awaitIndex() responds to the state of an index rather than returni…
Browse files Browse the repository at this point in the history
…ng details
  • Loading branch information
benbc committed Aug 16, 2016
1 parent 48c560e commit b14a182
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException; import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException; import org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException;
import org.neo4j.kernel.api.index.IndexDescriptor; import org.neo4j.kernel.api.index.IndexDescriptor;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.proc.ProcedureSignature; import org.neo4j.kernel.api.proc.ProcedureSignature;
import org.neo4j.kernel.impl.api.TokenAccess; import org.neo4j.kernel.impl.api.TokenAccess;
import org.neo4j.procedure.Context; import org.neo4j.procedure.Context;
import org.neo4j.procedure.Name; import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure; import org.neo4j.procedure.Procedure;


import static org.neo4j.helpers.collection.Iterators.asList; import static org.neo4j.helpers.collection.Iterators.asList;
import static org.neo4j.kernel.api.index.InternalIndexState.ONLINE;
import static org.neo4j.procedure.Procedure.Mode.READ; import static org.neo4j.procedure.Procedure.Mode.READ;


public class BuiltInProcedures public class BuiltInProcedures
Expand Down Expand Up @@ -93,12 +95,11 @@ public Stream<IndexResult> listIndexes() throws ProcedureException
} }


@Procedure(name = "db.awaitIndex", mode = READ) @Procedure(name = "db.awaitIndex", mode = READ)
public Stream<IndexResult> awaitIndex( @Name("label") String labelName, @Name("property") String propertyKeyName ) public void awaitIndex( @Name("label") String labelName, @Name("property") String propertyKeyName )
throws ProcedureException throws ProcedureException


{ {
ReadOperations operations = tx.acquireStatement().readOperations(); ReadOperations operations = tx.acquireStatement().readOperations();
TokenNameLookup tokens = new StatementTokenNameLookup( operations );


int labelId = operations.labelGetForName( labelName ); int labelId = operations.labelGetForName( labelName );
if ( labelId == ReadOperations.NO_SUCH_LABEL ) if ( labelId == ReadOperations.NO_SUCH_LABEL )
Expand All @@ -113,17 +114,22 @@ public Stream<IndexResult> awaitIndex( @Name("label") String labelName, @Name("p
propertyKeyName ); propertyKeyName );
} }


InternalIndexState state;
try try
{ {
IndexDescriptor index = operations.indexGetForLabelAndPropertyKey( labelId, propertyKeyId ); IndexDescriptor index = operations.indexGetForLabelAndPropertyKey( labelId, propertyKeyId );
return Stream.of( new IndexResult( "INDEX ON " + index.userDescription( tokens ), state = operations.indexGetState( index );
operations.indexGetState( index ).toString() ) );
} }
catch ( SchemaRuleNotFoundException | IndexNotFoundKernelException e ) catch ( SchemaRuleNotFoundException | IndexNotFoundKernelException e )
{ {
throw new ProcedureException( Status.Schema.IndexNotFound, e, "No index on :%s(%s)", labelName, throw new ProcedureException( Status.Schema.IndexNotFound, e, "No index on :%s(%s)", labelName,
propertyKeyName ); propertyKeyName );
} }

if ( state != ONLINE )
{
throw new ProcedureException( Status.General.UnknownError, "Index not online" );
}
} }


@Procedure(name = "db.constraints", mode = READ) @Procedure(name = "db.constraints", mode = READ)
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;


import static org.neo4j.kernel.api.index.InternalIndexState.ONLINE; import static org.neo4j.kernel.api.index.InternalIndexState.ONLINE;
import static org.neo4j.kernel.api.index.InternalIndexState.POPULATING;


public class AwaitIndexTest public class AwaitIndexTest
{ {
Expand Down Expand Up @@ -107,6 +108,28 @@ public void shouldLookUpTheIndexByLabelIdAndPropertyKeyId()
verify( operations ).indexGetForLabelAndPropertyKey( 123, 456 ); verify( operations ).indexGetForLabelAndPropertyKey( 123, 456 );
} }


@Test
public void shouldThrowAnExceptionIfTheIndexIsNotOnline()
throws SchemaRuleNotFoundException, IndexNotFoundKernelException

{
when( operations.labelGetForName( anyString() ) ).thenReturn( 123 );
when( operations.propertyKeyGetForName( anyString() ) ).thenReturn( 456 );
when( operations.indexGetForLabelAndPropertyKey( anyInt(), anyInt() ) )
.thenReturn( new IndexDescriptor( -1, -1 ) );
when( operations.indexGetState( any( IndexDescriptor.class ) ) ).thenReturn( POPULATING );

try
{
procedures.awaitIndex( null, null );
fail( "Expected an exception" );
}
catch ( ProcedureException e )
{
assertThat( e.getMessage(), containsString( "not online" ) );
}
}

private class StubKernelTransaction implements KernelTransaction private class StubKernelTransaction implements KernelTransaction
{ {
private final ReadOperations readOperations; private final ReadOperations readOperations;
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void shouldListCorrectBuiltinProcedures() throws Throwable
{ {
// When/Then // When/Then
assertThat( call( "dbms.procedures" ), contains( assertThat( call( "dbms.procedures" ), contains(
record( "db.awaitIndex", "db.awaitIndex(label :: STRING?, property :: STRING?) :: (description :: STRING?, state :: STRING?)" ), record( "db.awaitIndex", "db.awaitIndex(label :: STRING?, property :: STRING?) :: VOID" ),
record( "db.constraints", "db.constraints() :: (description :: STRING?)" ), record( "db.constraints", "db.constraints() :: (description :: STRING?)" ),
record( "db.indexes", "db.indexes() :: (description :: STRING?, state :: STRING?)" ), record( "db.indexes", "db.indexes() :: (description :: STRING?, state :: STRING?)" ),
record( "db.labels", "db.labels() :: (label :: STRING?)" ), record( "db.labels", "db.labels() :: (label :: STRING?)" ),
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void listProcedures() throws Throwable
assertThat( asList( stream ), containsInAnyOrder( assertThat( asList( stream ), containsInAnyOrder(
equalTo( new Object[]{"db.constraints", "db.constraints() :: (description :: STRING?)"} ), equalTo( new Object[]{"db.constraints", "db.constraints() :: (description :: STRING?)"} ),
equalTo( new Object[]{"db.indexes", "db.indexes() :: (description :: STRING?, state :: STRING?)"} ), equalTo( new Object[]{"db.indexes", "db.indexes() :: (description :: STRING?, state :: STRING?)"} ),
equalTo( new Object[]{"db.awaitIndex", "db.awaitIndex(label :: STRING?, property :: STRING?) :: (description :: STRING?, state :: STRING?)"} ), equalTo( new Object[]{"db.awaitIndex", "db.awaitIndex(label :: STRING?, property :: STRING?) :: VOID"} ),
equalTo( new Object[]{"db.propertyKeys", "db.propertyKeys() :: (propertyKey :: STRING?)"} ), equalTo( new Object[]{"db.propertyKeys", "db.propertyKeys() :: (propertyKey :: STRING?)"} ),
equalTo( new Object[]{"db.labels", "db.labels() :: (label :: STRING?)"} ), equalTo( new Object[]{"db.labels", "db.labels() :: (label :: STRING?)"} ),
equalTo( new Object[]{"db.relationshipTypes", "db.relationshipTypes() :: (relationshipType :: " + equalTo( new Object[]{"db.relationshipTypes", "db.relationshipTypes() :: (relationshipType :: " +
Expand Down

0 comments on commit b14a182

Please sign in to comment.