Search in sources :

Example 1 with NodeLabelIndexCursor

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

the class NodeScanIT method trackPageCacheAccessOnNodeLabelScan.

@Test
void trackPageCacheAccessOnNodeLabelScan() throws KernelException {
    var testLabel = Label.label("testLabel");
    try (KernelTransaction tx = kernel.beginTransaction(IMPLICIT, read())) {
        var cursorContext = tx.cursorContext();
        assertThat(cursorContext.getCursorTracer().pins()).isZero();
        var label = tx.tokenRead().nodeLabel(testLabel.name());
        IndexDescriptor index = tx.schemaRead().index(SchemaDescriptor.forAnyEntityTokens(EntityType.NODE)).next();
        TokenReadSession tokenReadSession = tx.dataRead().tokenReadSession(index);
        try (NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor(cursorContext)) {
            tx.dataRead().nodeLabelScan(tokenReadSession, cursor, IndexQueryConstraints.unconstrained(), new TokenPredicate(label));
            assertThat(cursorContext.getCursorTracer().pins()).isNotZero();
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenReadSession(org.neo4j.internal.kernel.api.TokenReadSession) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 2 with NodeLabelIndexCursor

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

the class ParallelNodeLabelScanTestBase method shouldScanAllNodesFromRandomlySizedWorkers.

@Test
void shouldScanAllNodesFromRandomlySizedWorkers() throws InterruptedException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    Scan<NodeLabelIndexCursor> scan = read.nodeLabelScan(FOO_LABEL);
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    try {
        // when
        List<Future<LongList>> futures = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            futures.add(service.submit(randomBatchWorker(scan, () -> cursors.allocateNodeLabelIndexCursor(NULL), NODE_GET)));
        }
        // then
        List<LongList> lists = futures.stream().map(TestUtils::unsafeGet).collect(Collectors.toList());
        assertDistinct(lists);
        assertEquals(FOO_NODES, LongSets.immutable.withAll(concat(lists)));
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) 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 3 with NodeLabelIndexCursor

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

the class ParallelNodeLabelScanTestBase method shouldScanASubsetOfNodes.

@Test
void shouldScanASubsetOfNodes() {
    try (NodeLabelIndexCursor nodes = cursors.allocateNodeLabelIndexCursor(NULL)) {
        for (int label : ALL_LABELS) {
            Scan<NodeLabelIndexCursor> scan = read.nodeLabelScan(label);
            assertTrue(scan.reserveBatch(nodes, 11));
            MutableLongList found = LongLists.mutable.empty();
            while (nodes.next()) {
                found.add(nodes.nodeReference());
            }
            assertThat(found.size()).isGreaterThan(0);
            if (label == FOO_LABEL) {
                assertTrue(FOO_NODES.containsAll(found));
                assertTrue(found.noneSatisfy(f -> BAR_NODES.contains(f)));
            } else if (label == BAR_LABEL) {
                assertTrue(BAR_NODES.containsAll(found));
                assertTrue(found.noneSatisfy(f -> FOO_NODES.contains(f)));
            } else {
                fail();
            }
        }
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) TestUtils.concat(org.neo4j.kernel.impl.newapi.TestUtils.concat) TestUtils.randomBatchWorker(org.neo4j.kernel.impl.newapi.TestUtils.randomBatchWorker) LongLists(org.eclipse.collections.impl.factory.primitive.LongLists) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Scan(org.neo4j.internal.kernel.api.Scan) LongList(org.eclipse.collections.api.list.primitive.LongList) MutableLongList(org.eclipse.collections.api.list.primitive.MutableLongList) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Write(org.neo4j.internal.kernel.api.Write) Future(java.util.concurrent.Future) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ToLongFunction(java.util.function.ToLongFunction) ExecutorService(java.util.concurrent.ExecutorService) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) Collectors(java.util.stream.Collectors) TestUtils.assertDistinct(org.neo4j.kernel.impl.newapi.TestUtils.assertDistinct) Executors(java.util.concurrent.Executors) TestUtils.singleBatchWorker(org.neo4j.kernel.impl.newapi.TestUtils.singleBatchWorker) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) KernelException(org.neo4j.exceptions.KernelException) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) LongSets(org.eclipse.collections.impl.factory.primitive.LongSets) LongSet(org.eclipse.collections.api.set.primitive.LongSet) MutableLongList(org.eclipse.collections.api.list.primitive.MutableLongList) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 4 with NodeLabelIndexCursor

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

the class ParallelNodeLabelScanTestBase method shouldHandleSizeHintOverflow.

@Test
void shouldHandleSizeHintOverflow() {
    try (NodeLabelIndexCursor nodes = cursors.allocateNodeLabelIndexCursor(NULL)) {
        // when
        Scan<NodeLabelIndexCursor> scan = read.nodeLabelScan(FOO_LABEL);
        assertTrue(scan.reserveBatch(nodes, NUMBER_OF_NODES * 2));
        MutableLongList ids = LongLists.mutable.empty();
        while (nodes.next()) {
            ids.add(nodes.nodeReference());
        }
        assertEquals(FOO_NODES.size(), ids.size());
        assertTrue(FOO_NODES.containsAll(ids));
        assertTrue(ids.noneSatisfy(f -> BAR_NODES.contains(f)));
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) TestUtils.concat(org.neo4j.kernel.impl.newapi.TestUtils.concat) TestUtils.randomBatchWorker(org.neo4j.kernel.impl.newapi.TestUtils.randomBatchWorker) LongLists(org.eclipse.collections.impl.factory.primitive.LongLists) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Scan(org.neo4j.internal.kernel.api.Scan) LongList(org.eclipse.collections.api.list.primitive.LongList) MutableLongList(org.eclipse.collections.api.list.primitive.MutableLongList) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Write(org.neo4j.internal.kernel.api.Write) Future(java.util.concurrent.Future) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ToLongFunction(java.util.function.ToLongFunction) ExecutorService(java.util.concurrent.ExecutorService) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) Collectors(java.util.stream.Collectors) TestUtils.assertDistinct(org.neo4j.kernel.impl.newapi.TestUtils.assertDistinct) Executors(java.util.concurrent.Executors) TestUtils.singleBatchWorker(org.neo4j.kernel.impl.newapi.TestUtils.singleBatchWorker) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) KernelException(org.neo4j.exceptions.KernelException) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) LongSets(org.eclipse.collections.impl.factory.primitive.LongSets) LongSet(org.eclipse.collections.api.set.primitive.LongSet) MutableLongList(org.eclipse.collections.api.list.primitive.MutableLongList) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) Test(org.junit.jupiter.api.Test)

Example 5 with NodeLabelIndexCursor

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

the class ParallelNodeLabelScanTransactionStateTestBase method scanShouldSeeAddedNodes.

@Test
void scanShouldSeeAddedNodes() throws Exception {
    int size = 64;
    int label = label("L");
    MutableLongSet existing = LongSets.mutable.withAll(createNodesWithLabel(label, size));
    try (KernelTransaction tx = beginTransaction()) {
        MutableLongSet added = LongSets.mutable.withAll(createNodesWithLabel(tx.dataWrite(), label, size));
        try (NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor(tx.cursorContext())) {
            Scan<NodeLabelIndexCursor> scan = tx.dataRead().nodeLabelScan(label);
            Set<Long> seen = new HashSet<>();
            while (scan.reserveBatch(cursor, 64)) {
                while (cursor.next()) {
                    long nodeId = cursor.nodeReference();
                    assertTrue(seen.add(nodeId), format("%d was seen multiple times", nodeId));
                    assertTrue(existing.remove(nodeId) || added.remove(nodeId));
                }
            }
            // make sure we have seen all nodes
            assertTrue(existing.isEmpty());
            assertTrue(added.isEmpty());
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) NodeLabelIndexCursor(org.neo4j.internal.kernel.api.NodeLabelIndexCursor) HashSet(java.util.HashSet) 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