Search in sources :

Example 11 with RelationshipScanCursor

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

the class ParallelRelationshipCursorTransactionStateTestBase method parallelTxStateScanStressTest.

@Test
void parallelTxStateScanStressTest() throws InterruptedException, KernelException {
    LongSet existingRelationships = createRelationships(77);
    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 allRels = LongSets.mutable.withAll(existingRelationships);
            try (KernelTransaction tx = beginTransaction()) {
                int relationshipsInTx = random.nextInt(100);
                Write write = tx.dataWrite();
                int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
                for (int j = 0; j < relationshipsInTx; j++) {
                    allRels.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
                }
                Scan<RelationshipScanCursor> scan = tx.dataRead().allRelationshipsScan();
                List<Future<LongList>> futures = new ArrayList<>(workers);
                for (int j = 0; j < workers; j++) {
                    futures.add(threadPool.submit(randomBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET)));
                }
                List<LongList> lists = futures.stream().map(TestUtils::unsafeGet).collect(Collectors.toList());
                TestUtils.assertDistinct(lists);
                LongList concat = TestUtils.concat(lists);
                assertEquals(allRels, LongSets.immutable.withAll(concat), format("relationships=%d, seen=%d, all=%d", relationshipsInTx, concat.size(), allRels.size()));
                assertEquals(allRels.size(), concat.size(), format("relationships=%d", relationshipsInTx));
                tx.rollback();
            }
        }
    } finally {
        threadPool.shutdown();
        threadPool.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) LongSet(org.eclipse.collections.api.set.primitive.LongSet) ArrayList(java.util.ArrayList) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Future(java.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 12 with RelationshipScanCursor

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

the class KernelReadTracerTest method shouldTraceSingleRelationship.

@Test
void shouldTraceSingleRelationship() {
    // given
    TestKernelReadTracer tracer = new TestKernelReadTracer();
    try (RelationshipScanCursor cursor = cursors.allocateRelationshipScanCursor(NULL)) {
        // when
        cursor.setTracer(tracer);
        read.singleRelationship(has, cursor);
        assertTrue(cursor.next());
        tracer.assertEvents(OnRelationship(has));
        cursor.removeTracer();
        read.singleRelationship(is, cursor);
        assertTrue(cursor.next());
        tracer.assertEvents();
        cursor.setTracer(tracer);
        read.singleRelationship(is, cursor);
        assertTrue(cursor.next());
        tracer.assertEvents(OnRelationship(is));
        assertFalse(cursor.next());
        tracer.assertEvents();
    }
}
Also used : RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) Test(org.junit.jupiter.api.Test)

Example 13 with RelationshipScanCursor

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

the class ManagedTestCursors method allocateFullAccessRelationshipScanCursor.

@Override
public RelationshipScanCursor allocateFullAccessRelationshipScanCursor(CursorContext cursorContext) {
    RelationshipScanCursor n = cursors.allocateFullAccessRelationshipScanCursor(cursorContext);
    allCursors.add(n);
    return n;
}
Also used : RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor)

Example 14 with RelationshipScanCursor

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

the class DefaultPooledCursorsTestBase method shouldReuseFullAccessRelationshipScanCursor.

@Test
void shouldReuseFullAccessRelationshipScanCursor() {
    RelationshipScanCursor c1 = cursors.allocateFullAccessRelationshipScanCursor(NULL);
    read.singleRelationship(relationship, c1);
    c1.close();
    RelationshipScanCursor c2 = cursors.allocateFullAccessRelationshipScanCursor(NULL);
    assertThat(c1).isSameAs(c2);
    c2.close();
}
Also used : RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) Test(org.junit.jupiter.api.Test)

Example 15 with RelationshipScanCursor

use of org.neo4j.internal.kernel.api.RelationshipScanCursor 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)

Aggregations

RelationshipScanCursor (org.neo4j.internal.kernel.api.RelationshipScanCursor)53 Test (org.junit.jupiter.api.Test)40 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)28 PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)16 Write (org.neo4j.internal.kernel.api.Write)16 CursorFactory (org.neo4j.internal.kernel.api.CursorFactory)8 ExecutorService (java.util.concurrent.ExecutorService)7 LongList (org.eclipse.collections.api.list.primitive.LongList)7 LongArrayList (org.eclipse.collections.impl.list.mutable.primitive.LongArrayList)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Read (org.neo4j.internal.kernel.api.Read)4 Future (java.util.concurrent.Future)3 MutableLongList (org.eclipse.collections.api.list.primitive.MutableLongList)3 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)3 TokenRead (org.neo4j.internal.kernel.api.TokenRead)3 KernelException (org.neo4j.exceptions.KernelException)2 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1