Search in sources :

Example 6 with NodeLabelIndexCursor

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

the class ParallelNodeLabelScanTransactionStateTestBase method scanShouldNotSeeDeletedNode.

@Test
void scanShouldNotSeeDeletedNode() throws Exception {
    int size = 1000;
    Set<Long> created = new HashSet<>(size);
    Set<Long> deleted = new HashSet<>(size);
    int label = label("L");
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        for (int i = 0; i < size; i++) {
            long createId = write.nodeCreate();
            long deleteId = write.nodeCreate();
            write.nodeAddLabel(createId, label);
            write.nodeAddLabel(deleteId, label);
            created.add(createId);
            deleted.add(deleteId);
        }
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        for (long delete : deleted) {
            tx.dataWrite().nodeDelete(delete);
        }
        try (NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor(tx.cursorContext())) {
            Scan<NodeLabelIndexCursor> scan = tx.dataRead().nodeLabelScan(label);
            Set<Long> seen = new HashSet<>();
            while (scan.reserveBatch(cursor, 128)) {
                while (cursor.next()) {
                    long nodeId = cursor.nodeReference();
                    assertTrue(seen.add(nodeId));
                    assertTrue(created.remove(nodeId));
                }
            }
            assertTrue(created.isEmpty());
        }
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with NodeLabelIndexCursor

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

the class ParallelNodeLabelScanTransactionStateTestBase method shouldScanAllNodesFromMultipleThreads.

@Test
void shouldScanAllNodesFromMultipleThreads() throws InterruptedException, ExecutionException, KernelException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    int size = 1024;
    try (KernelTransaction tx = beginTransaction()) {
        int label = tx.tokenWrite().labelGetOrCreateForName("L");
        LongList ids = createNodesWithLabel(tx.dataWrite(), label, size);
        Read read = tx.dataRead();
        Scan<NodeLabelIndexCursor> scan = read.nodeLabelScan(label);
        // when
        Supplier<NodeLabelIndexCursor> allocateCursor = () -> cursors.allocateNodeLabelIndexCursor(tx.cursorContext());
        Future<LongList> future1 = service.submit(singleBatchWorker(scan, allocateCursor, NODE_GET, size / 4));
        Future<LongList> future2 = service.submit(singleBatchWorker(scan, allocateCursor, NODE_GET, size / 4));
        Future<LongList> future3 = service.submit(singleBatchWorker(scan, allocateCursor, NODE_GET, size / 4));
        Future<LongList> future4 = service.submit(singleBatchWorker(scan, allocateCursor, NODE_GET, size / 4));
        // then
        LongList ids1 = future1.get();
        LongList ids2 = future2.get();
        LongList ids3 = future3.get();
        LongList ids4 = future4.get();
        assertDistinct(ids1, ids2, ids3, ids4);
        LongList concat = concat(ids1, ids2, ids3, ids4);
        assertEquals(ids.toSortedList(), concat.toSortedList());
        tx.rollback();
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Read(org.neo4j.internal.kernel.api.Read) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) ExecutorService(java.util.concurrent.ExecutorService) LongList(org.eclipse.collections.api.list.primitive.LongList) MutableLongList(org.eclipse.collections.api.list.primitive.MutableLongList) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 8 with NodeLabelIndexCursor

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

the class KernelReadTracerTest method shouldTraceLabelScan.

@Test
void shouldTraceLabelScan() throws KernelException {
    // given
    TestKernelReadTracer tracer = new TestKernelReadTracer();
    int barId = token.labelGetOrCreateForName("Bar");
    List<TraceEvent> expectedEvents = new ArrayList<>();
    expectedEvents.add(OnLabelScan(barId));
    try (NodeLabelIndexCursor cursor = cursors.allocateNodeLabelIndexCursor(NULL)) {
        // when
        cursor.setTracer(tracer);
        read.nodeLabelScan(getTokenReadSession(tx, EntityType.NODE), cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(barId));
        while (cursor.next()) {
            expectedEvents.add(OnNode(cursor.nodeReference()));
        }
    }
    // then
    tracer.assertEvents(expectedEvents);
}
Also used : TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) ArrayList(java.util.ArrayList) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) TraceEvent(org.neo4j.kernel.impl.newapi.TestKernelReadTracer.TraceEvent) Test(org.junit.jupiter.api.Test)

Example 9 with NodeLabelIndexCursor

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

the class NodeLabelTokenIndexCursorTest method shouldFindNodesByLabelInTx.

@Test
void shouldFindNodesByLabelInTx() throws Exception {
    long inStore;
    long deletedInTx;
    long createdInTx;
    try (KernelTransaction tx = beginTransaction()) {
        inStore = createNode(tx.dataWrite(), labelOne);
        createNode(tx.dataWrite(), labelTwo);
        deletedInTx = createNode(tx.dataWrite(), labelOne);
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        tx.dataWrite().nodeDelete(deletedInTx);
        createdInTx = createNode(tx.dataWrite(), labelOne);
        createNode(tx.dataWrite(), labelTwo);
        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
            assertNodes(cursor, uniqueIds, inStore, createdInTx);
        }
    }
}
Also used : Read(org.neo4j.internal.kernel.api.Read) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 10 with NodeLabelIndexCursor

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

the class NodeTransactionStateTestBase method shouldFindUpdatedNodeInInLabelScan.

@Test
void shouldFindUpdatedNodeInInLabelScan() throws Exception {
    // Given
    Node node = createNode();
    try (KernelTransaction tx = beginTransaction();
        NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor(tx.cursorContext())) {
        // when
        int label = tx.tokenWrite().labelGetOrCreateForName("label");
        tx.dataWrite().nodeAddLabel(node.node, label);
        tx.dataRead().nodeLabelScan(getTokenReadSession(tx, EntityType.NODE), cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(label));
        // then
        assertTrue(cursor.next());
        assertEquals(node.node, cursor.nodeReference());
    }
}
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)

Aggregations

NodeLabelIndexCursor (org.neo4j.internal.kernel.api.NodeLabelIndexCursor)25 Test (org.junit.jupiter.api.Test)23 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)19 TokenPredicate (org.neo4j.internal.kernel.api.TokenPredicate)11 ExecutorService (java.util.concurrent.ExecutorService)8 LongList (org.eclipse.collections.api.list.primitive.LongList)8 MutableLongList (org.eclipse.collections.api.list.primitive.MutableLongList)8 CursorFactory (org.neo4j.internal.kernel.api.CursorFactory)8 ArrayList (java.util.ArrayList)7 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)7 Future (java.util.concurrent.Future)6 Read (org.neo4j.internal.kernel.api.Read)4 Write (org.neo4j.internal.kernel.api.Write)4 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 Executors (java.util.concurrent.Executors)3 TimeUnit (java.util.concurrent.TimeUnit)3 Supplier (java.util.function.Supplier)3 ToLongFunction (java.util.function.ToLongFunction)3 Collectors (java.util.stream.Collectors)3