Search in sources :

Example 21 with TokenPredicate

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

the class DefaultPooledCursorsTestBase method shouldReuseNodeLabelIndexCursor.

@Test
void shouldReuseNodeLabelIndexCursor() throws Exception {
    try (KernelTransaction tx = beginTransaction()) {
        NodeLabelIndexCursor c1 = tx.cursors().allocateNodeLabelIndexCursor(NULL);
        tx.dataRead().nodeLabelScan(getTokenReadSession(tx, EntityType.NODE), c1, IndexQueryConstraints.unconstrained(), new TokenPredicate(1));
        c1.close();
        NodeLabelIndexCursor c2 = tx.cursors().allocateNodeLabelIndexCursor(NULL);
        assertThat(c1).isSameAs(c2);
        c2.close();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 22 with TokenPredicate

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

the class TransactionImpl method getNodesByLabelAndPropertyWithoutPropertyIndex.

private ResourceIterator<Node> getNodesByLabelAndPropertyWithoutPropertyIndex(KernelTransaction ktx, int labelId, PropertyIndexQuery... queries) {
    var index = findUsableMatchingIndex(ktx, SchemaDescriptor.forAnyEntityTokens(EntityType.NODE));
    if (index != IndexDescriptor.NO_INDEX) {
        try {
            var session = ktx.dataRead().tokenReadSession(index);
            var cursor = ktx.cursors().allocateNodeLabelIndexCursor(ktx.cursorContext());
            ktx.dataRead().nodeLabelScan(session, cursor, unconstrained(), new TokenPredicate(labelId));
            var nodeCursor = ktx.cursors().allocateNodeCursor(ktx.cursorContext());
            var propertyCursor = ktx.cursors().allocatePropertyCursor(ktx.cursorContext(), ktx.memoryTracker());
            return new NodeLabelPropertyIterator(ktx.dataRead(), cursor, nodeCursor, propertyCursor, c -> newNodeEntity(c.nodeReference()), coreApiResourceTracker, queries);
        } catch (KernelException e) {
        // ignore, fallback to all node scan
        }
    }
    return getNodesByLabelAndPropertyViaAllNodesScan(ktx, labelId, queries);
}
Also used : TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) NodeLabelPropertyIterator(org.neo4j.kernel.impl.coreapi.internal.NodeLabelPropertyIterator) 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 23 with TokenPredicate

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

the class RelationshipIndexedRelationshipStoreScanTest method mockIdsReturnedFromTokenQueries.

private void mockIdsReturnedFromTokenQueries() {
    // Make token index reader return different ids for the different tokens
    doAnswer(invocation -> {
        IndexProgressor.EntityTokenClient client = invocation.getArgument(0);
        TokenPredicate token = invocation.getArgument(2);
        client.initialize(new IndexProgressor() {

            private final PrimitiveIterator.OfLong relationshipsWithType1 = Arrays.stream(new long[] { 1, 2, 4, 8 }).iterator();

            private final PrimitiveIterator.OfLong relationshipsWithType2 = Arrays.stream(new long[] { 2, 5, 6 }).iterator();

            @Override
            public boolean next() {
                PrimitiveIterator.OfLong relationshipsWithType = relationshipsWithType1;
                if (token.tokenId() == 2) {
                    relationshipsWithType = relationshipsWithType2;
                }
                if (relationshipsWithType.hasNext()) {
                    client.acceptEntity(relationshipsWithType.nextLong(), null);
                    return true;
                }
                return false;
            }

            @Override
            public void close() {
            }
        }, token.tokenId(), IndexOrder.NONE);
        return null;
    }).when(relationshipTypeScanReader).query(any(), any(), any(), any(), any());
}
Also used : PrimitiveIterator(java.util.PrimitiveIterator) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) IndexProgressor(org.neo4j.kernel.api.index.IndexProgressor)

Example 24 with TokenPredicate

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

the class NodeLabelTokenIndexCursorTest method shouldFindNodesByLabel.

@Test
void shouldFindNodesByLabel() throws Exception {
    long toDelete;
    try (KernelTransaction tx = beginTransaction()) {
        createNode(tx.dataWrite(), labelOne, labelFirst);
        createNode(tx.dataWrite(), labelTwo, labelFirst);
        createNode(tx.dataWrite(), labelThree, labelFirst);
        toDelete = createNode(tx.dataWrite(), labelOne);
        createNode(tx.dataWrite(), labelTwo);
        createNode(tx.dataWrite(), labelThree);
        createNode(tx.dataWrite(), labelThree);
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        tx.dataWrite().nodeDelete(toDelete);
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        org.neo4j.internal.kernel.api.Read read = tx.dataRead();
        var session = getTokenReadSession(tx);
        try (NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor(tx.cursorContext())) {
            MutableLongSet uniqueIds = new LongHashSet();
            // WHEN
            read.nodeLabelScan(session, cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(labelOne));
            // THEN
            assertNodeCount(cursor, 1, uniqueIds);
            // WHEN
            read.nodeLabelScan(session, cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(labelTwo));
            // THEN
            assertNodeCount(cursor, 2, uniqueIds);
            // WHEN
            read.nodeLabelScan(session, cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(labelThree));
            // THEN
            assertNodeCount(cursor, 3, uniqueIds);
            // WHEN
            uniqueIds.clear();
            read.nodeLabelScan(session, cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(labelFirst));
            // THEN
            assertNodeCount(cursor, 3, uniqueIds);
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) Read(org.neo4j.internal.kernel.api.Read) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 25 with TokenPredicate

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

the class KernelReadTracerTest method shouldTraceRelationshipTypeScan.

@Test
void shouldTraceRelationshipTypeScan() throws KernelException {
    // given
    TestKernelReadTracer tracer = new TestKernelReadTracer();
    int hasId = token.relationshipTypeGetOrCreateForName("HAS");
    List<TraceEvent> expectedEvents = new ArrayList<>();
    expectedEvents.add(OnRelationshipTypeScan(hasId));
    try (RelationshipTypeIndexCursor cursor = cursors.allocateRelationshipTypeIndexCursor(NULL)) {
        // when
        cursor.setTracer(tracer);
        read.relationshipTypeScan(getTokenReadSession(tx, EntityType.RELATIONSHIP), cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(hasId));
        while (cursor.next()) {
            expectedEvents.add(OnRelationship(cursor.relationshipReference()));
        }
    }
    // then
    tracer.assertEvents(expectedEvents);
}
Also used : TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) RelationshipTypeIndexCursor(org.neo4j.internal.kernel.api.RelationshipTypeIndexCursor) ArrayList(java.util.ArrayList) TraceEvent(org.neo4j.kernel.impl.newapi.TestKernelReadTracer.TraceEvent) Test(org.junit.jupiter.api.Test)

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