Search in sources :

Example 16 with TokenPredicate

use of org.neo4j.internal.kernel.api.TokenPredicate in project neo4j by neo4j.

the class AllStoreHolder method countsForRelationshipWithoutTxState.

@Override
public long countsForRelationshipWithoutTxState(int startLabelId, int typeId, int endLabelId) {
    AccessMode mode = ktx.securityContext().mode();
    CursorContext cursorContext = ktx.cursorContext();
    if (mode.allowsTraverseRelType(typeId) && mode.allowsTraverseNode(startLabelId) && mode.allowsTraverseNode(endLabelId)) {
        return storageReader.countsForRelationship(startLabelId, typeId, endLabelId, cursorContext);
    }
    if (mode.disallowsTraverseRelType(typeId) || mode.disallowsTraverseLabel(startLabelId) || mode.disallowsTraverseLabel(endLabelId)) {
        // so the count will be 0.
        return 0;
    }
    // token index scan can only scan for single relationship type
    if (typeId != TokenRead.ANY_RELATIONSHIP_TYPE) {
        try {
            var index = findUsableTokenIndex(EntityType.RELATIONSHIP);
            if (index != IndexDescriptor.NO_INDEX) {
                long count = 0;
                try (DefaultRelationshipTypeIndexCursor relationshipsWithType = cursors.allocateRelationshipTypeIndexCursor(cursorContext);
                    DefaultRelationshipScanCursor relationship = cursors.allocateRelationshipScanCursor(cursorContext);
                    DefaultNodeCursor sourceNode = cursors.allocateNodeCursor(cursorContext);
                    DefaultNodeCursor targetNode = cursors.allocateNodeCursor(cursorContext)) {
                    var session = tokenReadSession(index);
                    this.relationshipTypeScan(session, relationshipsWithType, unconstrained(), new TokenPredicate(typeId));
                    while (relationshipsWithType.next()) {
                        relationshipsWithType.relationship(relationship);
                        count += countRelationshipsWithEndLabels(relationship, sourceNode, targetNode, startLabelId, endLabelId);
                    }
                }
                return count - countsForRelationshipInTxState(startLabelId, typeId, endLabelId);
            }
        } catch (KernelException ignored) {
        // ignore, fallback to allRelationshipsScan
        }
    }
    long count;
    try (DefaultRelationshipScanCursor rels = cursors.allocateRelationshipScanCursor(cursorContext);
        DefaultNodeCursor sourceNode = cursors.allocateFullAccessNodeCursor(cursorContext);
        DefaultNodeCursor targetNode = cursors.allocateFullAccessNodeCursor(cursorContext)) {
        this.allRelationshipsScan(rels);
        Predicate<RelationshipScanCursor> predicate = typeId == TokenRead.ANY_RELATIONSHIP_TYPE ? alwaysTrue() : CursorPredicates.hasType(typeId);
        var filteredCursor = new FilteringRelationshipScanCursorWrapper(rels, predicate);
        count = countRelationshipsWithEndLabels(filteredCursor, sourceNode, targetNode, startLabelId, endLabelId);
    }
    return count - countsForRelationshipInTxState(startLabelId, typeId, endLabelId);
}
Also used : RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) AdminAccessMode(org.neo4j.internal.kernel.api.security.AdminAccessMode) AccessMode(org.neo4j.internal.kernel.api.security.AccessMode) RestrictedAccessMode(org.neo4j.kernel.impl.api.security.RestrictedAccessMode) OverriddenAccessMode(org.neo4j.kernel.impl.api.security.OverriddenAccessMode) CursorContext(org.neo4j.io.pagecache.context.CursorContext) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException)

Example 17 with TokenPredicate

use of org.neo4j.internal.kernel.api.TokenPredicate in project neo4j by neo4j.

the class RelationshipTypeIndexOrderTest method tokenScan.

@Override
protected void tokenScan(IndexOrder indexOrder, KernelTransaction tx, int label, RelationshipTypeIndexCursor cursor) throws KernelException {
    IndexDescriptor index = tx.schemaRead().index(SchemaDescriptor.forAnyEntityTokens(EntityType.RELATIONSHIP)).next();
    TokenReadSession tokenReadSession = tx.dataRead().tokenReadSession(index);
    tx.dataRead().relationshipTypeScan(tokenReadSession, cursor, IndexQueryConstraints.ordered(indexOrder), new TokenPredicate(label));
}
Also used : TokenReadSession(org.neo4j.internal.kernel.api.TokenReadSession) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 18 with TokenPredicate

use of org.neo4j.internal.kernel.api.TokenPredicate in project neo4j by neo4j.

the class TransactionImpl method getRelationshipsByTypeAndPropertyWithoutPropertyIndex.

private ResourceIterator<Relationship> getRelationshipsByTypeAndPropertyWithoutPropertyIndex(KernelTransaction ktx, int typeId, PropertyIndexQuery... queries) {
    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));
            var relationshipScanCursor = ktx.cursors().allocateRelationshipScanCursor(ktx.cursorContext());
            var propertyCursor = ktx.cursors().allocatePropertyCursor(ktx.cursorContext(), ktx.memoryTracker());
            return new RelationshipTypePropertyIterator(ktx.dataRead(), cursor, relationshipScanCursor, propertyCursor, c -> newRelationshipEntity(c.relationshipReference()), coreApiResourceTracker, queries);
        } catch (KernelException e) {
        // ignore, fallback to all node scan
        }
    }
    return getRelationshipsByTypeAndPropertyViaAllRelsScan(ktx, typeId, queries);
}
Also used : RelationshipTypePropertyIterator(org.neo4j.kernel.impl.coreapi.internal.RelationshipTypePropertyIterator) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) 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)

Example 19 with TokenPredicate

use of org.neo4j.internal.kernel.api.TokenPredicate 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 20 with TokenPredicate

use of org.neo4j.internal.kernel.api.TokenPredicate in project neo4j by neo4j.

the class RelationshipTypeIndexCursorTestBase method relationshipTypeScan.

private void relationshipTypeScan(KernelTransaction tx, int label, RelationshipTypeIndexCursor cursor, IndexOrder indexOrder) throws KernelException {
    IndexDescriptor index = tx.schemaRead().indexGetForName("rti");
    TokenReadSession tokenReadSession = tx.dataRead().tokenReadSession(index);
    tx.dataRead().relationshipTypeScan(tokenReadSession, cursor, IndexQueryConstraints.ordered(indexOrder), new TokenPredicate(label));
}
Also used : TokenReadSession(org.neo4j.internal.kernel.api.TokenReadSession) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

TokenPredicate (org.neo4j.internal.kernel.api.TokenPredicate)32 Test (org.junit.jupiter.api.Test)16 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)13 NodeLabelIndexCursor (org.neo4j.internal.kernel.api.NodeLabelIndexCursor)11 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)6 KernelException (org.neo4j.exceptions.KernelException)5 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)5 SimpleEntityTokenClient (org.neo4j.storageengine.api.schema.SimpleEntityTokenClient)5 ArrayList (java.util.ArrayList)4 TokenReadSession (org.neo4j.internal.kernel.api.TokenReadSession)4 InvalidTransactionTypeKernelException (org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException)4 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)4 QueryExecutionKernelException (org.neo4j.kernel.impl.query.QueryExecutionKernelException)4 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)2 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)2 Read (org.neo4j.internal.kernel.api.Read)2 RelationshipTypeIndexCursor (org.neo4j.internal.kernel.api.RelationshipTypeIndexCursor)2 CursorIterator (org.neo4j.kernel.impl.coreapi.internal.CursorIterator)2 TraceEvent (org.neo4j.kernel.impl.newapi.TestKernelReadTracer.TraceEvent)2 PrimitiveIterator (java.util.PrimitiveIterator)1