Skip to content

Commit

Permalink
Merge pull request #7766 from pontusmelke/3.1-readd-type-in-proc
Browse files Browse the repository at this point in the history
Add type back to `db.indexes()`
  • Loading branch information
Mats-SX committed Aug 23, 2016
2 parents 98fefdd + 1d3da5f commit dbcc0ba
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,6 @@ class BuiltInProcedureAcceptanceTest extends ProcedureCallAcceptanceTest {

// Then
result.toList should equal(
List(Map("description" -> "INDEX ON :A(prop)", "state" -> "ONLINE")))
List(Map("description" -> "INDEX ON :A(prop)", "state" -> "ONLINE", "type" -> "node_label_property")))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

Expand All @@ -41,6 +42,7 @@
import org.neo4j.procedure.Procedure;

import static org.neo4j.helpers.collection.Iterators.asList;
import static org.neo4j.helpers.collection.Iterators.asSet;
import static org.neo4j.procedure.Procedure.Mode.READ;

public class BuiltInProcedures
Expand Down Expand Up @@ -73,15 +75,29 @@ public Stream<IndexResult> listIndexes() throws ProcedureException
ReadOperations operations = tx.acquireStatement().readOperations();
TokenNameLookup tokens = new StatementTokenNameLookup( operations );

List<IndexDescriptor> indexes = asList( operations.indexesGetAll() );
indexes.sort( ( a, b ) -> a.userDescription( tokens ).compareTo( b.userDescription( tokens ) ) );
List<IndexDescriptor> indexes =
asList( operations.indexesGetAll() );

Set<IndexDescriptor> uniqueIndexes = asSet( operations.uniqueIndexesGetAll() );
indexes.addAll( uniqueIndexes );
indexes.sort( (a,b) -> a.userDescription(tokens).compareTo( b.userDescription(tokens) ) );

ArrayList<IndexResult> result = new ArrayList<>();
for ( IndexDescriptor index : indexes )
{
try
{
String type;
if (uniqueIndexes.contains( index ))
{
type = IndexType.NODE_UNIQUE_PROPERTY.typeName();
}
else {
type = IndexType.NODE_LABEL_PROPERTY.typeName();
}

result.add( new IndexResult( "INDEX ON " + index.userDescription( tokens ),
operations.indexGetState( index ).toString() ) );
operations.indexGetState( index ).toString(), type ) );
}
catch ( IndexNotFoundKernelException e )
{
Expand Down Expand Up @@ -156,11 +172,13 @@ public class IndexResult
{
public final String description;
public final String state;
public final String type;

private IndexResult( String description, String state )
private IndexResult( String description, String state, String type )
{
this.description = description;
this.state = state;
this.type = type;
}
}

Expand All @@ -185,4 +203,24 @@ private ProcedureResult( ProcedureSignature signature )
this.signature = signature.toString();
}
}

//When we have decided on what to call different indexes
//this should probably be moved to some more central place
private enum IndexType
{
NODE_LABEL_PROPERTY( "node_label_property" ),
NODE_UNIQUE_PROPERTY( "node_unique_property" );

private final String typeName;

IndexType( String typeName )
{
this.typeName = typeName;
}

public String typeName()
{
return typeName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
*/
package org.neo4j.kernel.builtinprocs;

import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
import org.mockito.stubbing.Answer;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Test;
import org.mockito.stubbing.Answer;

import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.ReadOperations;
Expand All @@ -48,7 +48,6 @@

import static java.util.Collections.emptyIterator;
import static java.util.Collections.singletonList;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
Expand All @@ -57,12 +56,12 @@
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import static org.neo4j.kernel.api.proc.CallableProcedure.Context.KERNEL_TRANSACTION;

public class BuiltInProceduresTest
{
private final List<IndexDescriptor> indexes = new LinkedList<>();
private final List<IndexDescriptor> uniqueIndexes = new LinkedList<>();
private final List<PropertyConstraint> constraints = new LinkedList<>();
private final Map<Integer, String> labels = new HashMap<>();
private final Map<Integer, String> propKeys = new HashMap<>();
Expand All @@ -82,7 +81,18 @@ public void shouldListAllIndexes() throws Throwable

// When/Then
assertThat( call("db.indexes"),
contains( record( "INDEX ON :User(name)", "ONLINE" ) ) );
contains( record( "INDEX ON :User(name)", "ONLINE", "node_label_property" ) ) );
}

@Test
public void shouldListAllUniqueIndexes() throws Throwable
{
// Given
givenUniqueConstraint( "User", "name" );

// When/Then
assertThat( call( "db.indexes" ),
contains( record( "INDEX ON :User(name)", "ONLINE", "node_unique_property" ) ) );
}

@Test
Expand Down Expand Up @@ -145,7 +155,7 @@ public void shouldListCorrectBuiltinProcedures() throws Throwable
assertThat( call( "dbms.procedures" ), contains(
record( "db.awaitIndex", "db.awaitIndex(label :: STRING?, property :: STRING?, timeOutSeconds :: INTEGER?) :: VOID" ),
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?, type :: STRING?)" ),
record( "db.labels", "db.labels() :: (label :: STRING?)" ),
record( "db.propertyKeys", "db.propertyKeys() :: (propertyKey :: STRING?)" ),
record( "db.relationshipTypes", "db.relationshipTypes() :: (relationshipType :: STRING?)" ),
Expand Down Expand Up @@ -182,6 +192,7 @@ private void givenUniqueConstraint( String label, String propKey )
int labelId = token( label, labels );
int propId = token( propKey, propKeys );

uniqueIndexes.add( new IndexDescriptor( labelId, propId ) );
constraints.add( new UniquenessConstraint( labelId, propId ) );
}

Expand Down Expand Up @@ -243,6 +254,7 @@ public void setup() throws Exception
when(read.labelsGetAllTokens()).thenAnswer( asTokens(labels) );
when(read.relationshipTypesGetAllTokens()).thenAnswer( asTokens(relTypes) );
when(read.indexesGetAll()).thenAnswer( (i) -> indexes.iterator() );
when(read.uniqueIndexesGetAll()).thenAnswer( (i) -> uniqueIndexes.iterator() );
when(read.constraintsGetAll()).thenAnswer( (i) -> constraints.iterator() );
when(read.proceduresGetAll() ).thenReturn( procs.getAll() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import org.junit.rules.ExpectedException;

import org.neo4j.collection.RawIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.api.DataWriteOperations;
import org.neo4j.kernel.api.SchemaWriteOperations;
import org.neo4j.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.security.AccessMode.Static;

import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static junit.framework.TestCase.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -103,7 +106,7 @@ public void listProcedures() throws Throwable
// Then
assertThat( asList( stream ), containsInAnyOrder(
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?, type :: STRING?)"} ),
equalTo( new Object[]{"db.awaitIndex", "db.awaitIndex(label :: STRING?, property :: STRING?, " +
"timeOutSeconds :: INTEGER?) :: VOID"} ),
equalTo( new Object[]{"db.propertyKeys", "db.propertyKeys() :: (propertyKey :: STRING?)"} ),
Expand Down Expand Up @@ -148,4 +151,34 @@ public void listAllComponents() throws Throwable
assertThat( asList( stream ), contains( equalTo( new Object[]{"Neo4j Kernel", singletonList( "dev" ),
"community"} ) ) );
}

@Test
public void listAllIndexes() throws Throwable
{
// Given
SchemaWriteOperations ops = schemaWriteOperationsInNewTransaction();
int labelId1 = ops.labelGetOrCreateForName( "Person" );
int labelId2 = ops.labelGetOrCreateForName( "Age" );
int propertyKeyId = ops.propertyKeyGetOrCreateForName( "foo" );
ops.indexCreate( labelId1, propertyKeyId );
ops.uniquePropertyConstraintCreate( labelId2, propertyKeyId );
commit();

//let indexes come online
try ( Transaction tx = db.beginTx() )
{
db.schema().awaitIndexOnline( db.schema().getIndexes().iterator().next(), 20, SECONDS );
tx.success();
}

// When
RawIterator<Object[],ProcedureException> stream =
readOperationsInNewTransaction().procedureCallRead( procedureName( "db", "indexes" ), new Object[0] );

// Then
assertThat( stream.next(), equalTo( new Object[]{"INDEX ON :Age(foo)", "ONLINE",
"node_unique_property"} ) );
assertThat( stream.next(), equalTo( new Object[]{"INDEX ON :Person(foo)", "ONLINE",
"node_label_property"} ) );
}
}

0 comments on commit dbcc0ba

Please sign in to comment.