Search in sources :

Example 1 with CursorIterator

use of org.neo4j.kernel.impl.coreapi.internal.CursorIterator in project neo4j by neo4j.

the class TransactionImpl method relationshipsByTypeAndProperty.

private ResourceIterator<Relationship> relationshipsByTypeAndProperty(KernelTransaction transaction, int typeId, PropertyIndexQuery query) {
    Read read = transaction.dataRead();
    if (query.propertyKeyId() == TokenRead.NO_TOKEN || typeId == TokenRead.NO_TOKEN) {
        return emptyResourceIterator();
    }
    var index = findUsableMatchingIndex(transaction, SchemaDescriptor.forRelType(typeId, query.propertyKeyId()));
    if (index != IndexDescriptor.NO_INDEX) {
        // Ha! We found an index - let's use it to find matching relationships
        try {
            var cursor = transaction.cursors().allocateRelationshipValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker());
            IndexReadSession indexSession = read.indexReadSession(index);
            read.relationshipIndexSeek(indexSession, cursor, unconstrained(), query);
            return new CursorIterator<>(cursor, RelationshipIndexCursor::relationshipReference, c -> newRelationshipEntity(c.relationshipReference()), coreApiResourceTracker);
        } catch (KernelException e) {
        // weird at this point but ignore and fallback to a type scan
        }
    }
    return getRelationshipsByTypeAndPropertyWithoutPropertyIndex(transaction, typeId, query);
}
Also used : SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Read(org.neo4j.internal.kernel.api.Read) CursorIterator(org.neo4j.kernel.impl.coreapi.internal.CursorIterator) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) KernelException(org.neo4j.exceptions.KernelException) RelationshipIndexCursor(org.neo4j.internal.kernel.api.RelationshipIndexCursor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 2 with CursorIterator

use of org.neo4j.kernel.impl.coreapi.internal.CursorIterator in project neo4j by neo4j.

the class TransactionImpl method allRelationshipsWithType.

private ResourceIterator<Relationship> allRelationshipsWithType(final RelationshipType type) {
    KernelTransaction ktx = kernelTransaction();
    int typeId = ktx.tokenRead().relationshipType(type.name());
    if (typeId == TokenRead.NO_TOKEN) {
        return emptyResourceIterator();
    }
    var index = findUsableMatchingIndex(ktx, SchemaDescriptor.forAnyEntityTokens(EntityType.RELATIONSHIP));
    if (index != IndexDescriptor.NO_INDEX) {
        try {
            var session = ktx.dataRead().tokenReadSession(index);
            var cursor = ktx.cursors().allocateRelationshipTypeIndexCursor(ktx.cursorContext());
            ktx.dataRead().relationshipTypeScan(session, cursor, unconstrained(), new TokenPredicate(typeId));
            return new CursorIterator<>(cursor, RelationshipIndexCursor::relationshipReference, c -> newRelationshipEntity(c.relationshipReference()), coreApiResourceTracker);
        } catch (KernelException e) {
        // ignore, fallback to all node scan
        }
    }
    return allRelationshipsByTypeWithoutIndex(ktx, typeId);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) CursorIterator(org.neo4j.kernel.impl.coreapi.internal.CursorIterator) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) KernelException(org.neo4j.exceptions.KernelException) RelationshipIndexCursor(org.neo4j.internal.kernel.api.RelationshipIndexCursor)

Example 3 with CursorIterator

use of org.neo4j.kernel.impl.coreapi.internal.CursorIterator in project neo4j by neo4j.

the class TransactionImpl method allNodesByLabelWithoutIndex.

private ResourceIterator<Node> allNodesByLabelWithoutIndex(KernelTransaction ktx, int labelId) {
    NodeCursor cursor = ktx.cursors().allocateNodeCursor(ktx.cursorContext());
    ktx.dataRead().allNodesScan(cursor);
    var filetredCursor = new FilteringNodeCursorWrapper(cursor, CursorPredicates.hasLabel(labelId));
    return new CursorIterator<>(filetredCursor, NodeCursor::nodeReference, c -> newNodeEntity(c.nodeReference()), coreApiResourceTracker);
}
Also used : FilteringNodeCursorWrapper(org.neo4j.kernel.impl.newapi.FilteringNodeCursorWrapper) CursorIterator(org.neo4j.kernel.impl.coreapi.internal.CursorIterator) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor)

Example 4 with CursorIterator

use of org.neo4j.kernel.impl.coreapi.internal.CursorIterator in project neo4j by neo4j.

the class TransactionImpl method relationshipsByTypeAndProperties.

private ResourceIterator<Relationship> relationshipsByTypeAndProperties(KernelTransaction tx, int typeId, PropertyIndexQuery.ExactPredicate... queries) {
    Read read = tx.dataRead();
    if (isInvalidQuery(typeId, queries)) {
        return emptyResourceIterator();
    }
    int[] propertyIds = getPropertyIds(queries);
    IndexDescriptor index = findUsableMatchingCompositeIndex(tx, SchemaDescriptor.forRelType(typeId, propertyIds), propertyIds, () -> tx.schemaRead().indexesGetForRelationshipType(typeId));
    if (index != IndexDescriptor.NO_INDEX) {
        try {
            RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
            IndexReadSession indexSession = read.indexReadSession(index);
            read.relationshipIndexSeek(indexSession, cursor, unconstrained(), getReorderedIndexQueries(index.schema().getPropertyIds(), queries));
            return new CursorIterator<>(cursor, RelationshipIndexCursor::relationshipReference, c -> newRelationshipEntity(c.relationshipReference()), coreApiResourceTracker);
        } catch (KernelException e) {
        // weird at this point but ignore and fallback to a label scan
        }
    }
    return getRelationshipsByTypeAndPropertyWithoutPropertyIndex(tx, typeId, queries);
}
Also used : SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Read(org.neo4j.internal.kernel.api.Read) RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) CursorIterator(org.neo4j.kernel.impl.coreapi.internal.CursorIterator) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) KernelException(org.neo4j.exceptions.KernelException) RelationshipIndexCursor(org.neo4j.internal.kernel.api.RelationshipIndexCursor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 5 with CursorIterator

use of org.neo4j.kernel.impl.coreapi.internal.CursorIterator in project neo4j by neo4j.

the class TransactionImpl method nodesByLabelAndProperties.

private ResourceIterator<Node> nodesByLabelAndProperties(KernelTransaction transaction, int labelId, PropertyIndexQuery.ExactPredicate... queries) {
    Read read = transaction.dataRead();
    if (isInvalidQuery(labelId, queries)) {
        return emptyResourceIterator();
    }
    int[] propertyIds = getPropertyIds(queries);
    IndexDescriptor index = findUsableMatchingCompositeIndex(transaction, SchemaDescriptor.forLabel(labelId, propertyIds), propertyIds, () -> transaction.schemaRead().indexesGetForLabel(labelId));
    if (index != IndexDescriptor.NO_INDEX) {
        try {
            NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker());
            IndexReadSession indexSession = read.indexReadSession(index);
            read.nodeIndexSeek(indexSession, cursor, unconstrained(), getReorderedIndexQueries(index.schema().getPropertyIds(), queries));
            return new CursorIterator<>(cursor, NodeIndexCursor::nodeReference, c -> newNodeEntity(c.nodeReference()), coreApiResourceTracker);
        } catch (KernelException e) {
        // weird at this point but ignore and fallback to a label scan
        }
    }
    return getNodesByLabelAndPropertyWithoutPropertyIndex(transaction, labelId, queries);
}
Also used : SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Read(org.neo4j.internal.kernel.api.Read) NodeIndexCursor(org.neo4j.internal.kernel.api.NodeIndexCursor) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) CursorIterator(org.neo4j.kernel.impl.coreapi.internal.CursorIterator) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) KernelException(org.neo4j.exceptions.KernelException) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Aggregations

CursorIterator (org.neo4j.kernel.impl.coreapi.internal.CursorIterator)7 KernelException (org.neo4j.exceptions.KernelException)6 InvalidTransactionTypeKernelException (org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException)6 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)6 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)6 QueryExecutionKernelException (org.neo4j.kernel.impl.query.QueryExecutionKernelException)6 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)4 Read (org.neo4j.internal.kernel.api.Read)4 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)4 TokenRead (org.neo4j.internal.kernel.api.TokenRead)4 NodeIndexCursor (org.neo4j.internal.kernel.api.NodeIndexCursor)3 RelationshipIndexCursor (org.neo4j.internal.kernel.api.RelationshipIndexCursor)3 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)2 TokenPredicate (org.neo4j.internal.kernel.api.TokenPredicate)2 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)2 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)1 RelationshipValueIndexCursor (org.neo4j.internal.kernel.api.RelationshipValueIndexCursor)1 FilteringNodeCursorWrapper (org.neo4j.kernel.impl.newapi.FilteringNodeCursorWrapper)1