Skip to content

Commit

Permalink
Use new API for index operations in Core API
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke authored and fickludd committed Jan 24, 2018
1 parent 95da43d commit d1f00e2
Showing 1 changed file with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@
import org.neo4j.graphdb.traversal.BidirectionalTraversalDescription;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.helpers.collection.PrefetchingResourceIterator;
import org.neo4j.internal.kernel.api.CapableIndexReference;
import org.neo4j.internal.kernel.api.IndexOrder;
import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.internal.kernel.api.InternalIndexState;
import org.neo4j.internal.kernel.api.NodeCursor;
import org.neo4j.internal.kernel.api.NodeLabelIndexCursor;
import org.neo4j.internal.kernel.api.Write;
import org.neo4j.internal.kernel.api.NodeValueIndexCursor;
import org.neo4j.internal.kernel.api.Read;
import org.neo4j.internal.kernel.api.TokenRead;
import org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException;
import org.neo4j.internal.kernel.api.exceptions.KernelException;
import org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException;
Expand Down Expand Up @@ -166,8 +171,7 @@ public interface SPI

/**
* Begin a new kernel transaction with specified timeout in milliseconds.
* If a transaction is already associated to the current context
* (meaning, non-null is returned from {@link #currentTransaction()}), this should fail.
* If a transaction is already associated to the current context, this should fail.
*
* @throws org.neo4j.graphdb.TransactionFailureException if unable to begin, or a transaction already exists.
* @see SPI#beginTransaction(KernelTransaction.Type, SecurityContext)
Expand Down Expand Up @@ -614,34 +618,63 @@ private InternalTransaction beginTransactionInternal( KernelTransaction.Type typ

private ResourceIterator<Node> nodesByLabelAndProperty( Label myLabel, String key, Value value )
{
Statement statement = statementContext.get();

ReadOperations readOps = statement.readOperations();
int propertyId = readOps.propertyKeyGetForName( key );
int labelId = readOps.labelGetForName( myLabel.name() );
KernelTransaction transaction = statementContext.getKernelTransactionBoundToThisThread( true );
Statement statement = transaction.acquireStatement();
Read read = transaction.dataRead();
TokenRead tokenRead = transaction.tokenRead();
int propertyId = tokenRead.propertyKey( key );
int labelId = tokenRead.nodeLabel( myLabel.name() );

if ( propertyId == NO_SUCH_PROPERTY_KEY || labelId == NO_SUCH_LABEL )
{
statement.close();
return emptyResourceIterator();
}
CapableIndexReference index = transaction.schemaRead().index( labelId, propertyId );
if ( index != CapableIndexReference.NO_INDEX )
{
// Ha! We found an index - let's use it to find matching nodes
try
{
NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor();
IndexQuery.ExactPredicate query = IndexQuery.exact( propertyId, value );
read.nodeIndexSeek( index, cursor, IndexOrder.NONE, query );

IndexDescriptor descriptor = findAnyIndexByLabelAndProperty( readOps, propertyId, labelId );
return new PrefetchingResourceIterator<Node>()
{
private boolean closed;
@Override
protected Node fetchNextOrNull()
{
if ( cursor.next() )
{
return newNodeProxy( cursor.nodeReference() );
}
else
{
close();
return null;
}
}

try
{
if ( null != descriptor )
@Override
public void close()
{
if ( !closed )
{
statement.close();
cursor.close();
closed = true;
}
}
};
}
catch ( KernelException e )
{
// Ha! We found an index - let's use it to find matching nodes
IndexQuery.ExactPredicate query = IndexQuery.exact( descriptor.schema().getPropertyId(), value );
PrimitiveLongResourceIterator indexResult = readOps.indexQuery( descriptor, query );
return map2nodes( indexResult, statement, indexResult );
// weird at this point but ignore and fallback to a label scan
}
}
catch ( KernelException e )
{
// weird at this point but ignore and fallback to a label scan
}


return getNodesByLabelAndPropertyWithoutIndex( propertyId, value, statement, labelId );
}
Expand Down

0 comments on commit d1f00e2

Please sign in to comment.