Search in sources :

Example 21 with NodeLabelIndexCursor

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

the class ParallelNodeLabelScanTransactionStateTestBase method parallelTxStateScanStressTest.

@Test
void parallelTxStateScanStressTest() throws KernelException, InterruptedException {
    int label = label("L");
    MutableLongSet existingNodes = LongSets.mutable.withAll(createNodesWithLabel(label, 1000));
    int workers = Runtime.getRuntime().availableProcessors();
    ExecutorService threadPool = Executors.newFixedThreadPool(workers);
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    ThreadLocalRandom random = ThreadLocalRandom.current();
    try {
        for (int i = 0; i < 1000; i++) {
            MutableLongSet allNodes = LongSets.mutable.withAll(existingNodes);
            try (KernelTransaction tx = beginTransaction()) {
                int nodeInTx = random.nextInt(1000);
                allNodes.addAll(createNodesWithLabel(tx.dataWrite(), label, nodeInTx));
                Scan<NodeLabelIndexCursor> scan = tx.dataRead().nodeLabelScan(label);
                List<Future<LongList>> futures = new ArrayList<>(workers);
                for (int j = 0; j < workers; j++) {
                    futures.add(threadPool.submit(randomBatchWorker(scan, () -> cursors.allocateNodeLabelIndexCursor(tx.cursorContext()), NODE_GET)));
                }
                List<LongList> lists = futures.stream().map(TestUtils::unsafeGet).collect(Collectors.toList());
                assertDistinct(lists);
                LongList concat = concat(lists);
                assertEquals(allNodes, LongSets.immutable.withAll(concat), format("nodes=%d, seen=%d, all=%d", nodeInTx, concat.size(), allNodes.size()));
                assertEquals(allNodes.size(), concat.size(), format("nodes=%d", nodeInTx));
                tx.rollback();
            }
        }
    } finally {
        threadPool.shutdown();
        threadPool.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ArrayList(java.util.ArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) MutableLongList(org.eclipse.collections.api.list.primitive.MutableLongList) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Future(java.util.concurrent.Future) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 22 with NodeLabelIndexCursor

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

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

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

the class NodeTransactionStateTestBase method shouldNotFindNodeWithRemovedLabelInLabelScan.

@Test
void shouldNotFindNodeWithRemovedLabelInLabelScan() throws Exception {
    // Given
    Node node = createNode("label");
    try (KernelTransaction tx = beginTransaction();
        NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor(tx.cursorContext())) {
        // when
        tx.dataWrite().nodeRemoveLabel(node.node, node.labels[0]);
        tx.dataRead().nodeLabelScan(getTokenReadSession(tx, EntityType.NODE), cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(node.labels[0]));
        // then
        assertFalse(cursor.next());
    }
}
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 25 with NodeLabelIndexCursor

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

the class NodeTransactionStateTestBase method shouldFindSwappedNodeInLabelScan.

@Test
void shouldFindSwappedNodeInLabelScan() throws Exception {
    // Given
    Node node1 = createNode("label");
    Node node2 = createNode();
    try (KernelTransaction tx = beginTransaction();
        NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor(tx.cursorContext())) {
        // when
        tx.dataWrite().nodeRemoveLabel(node1.node, node1.labels[0]);
        tx.dataWrite().nodeAddLabel(node2.node, node1.labels[0]);
        tx.dataRead().nodeLabelScan(getTokenReadSession(tx, EntityType.NODE), cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(node1.labels[0]));
        // then
        assertTrue(cursor.next());
        assertEquals(node2.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