Search in sources :

Example 6 with RelationshipScanCursor

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

the class ParallelRelationshipCursorTransactionStateTestBase method shouldScanAllRelationshipsFromMultipleThreads.

@Test
void shouldScanAllRelationshipsFromMultipleThreads() throws InterruptedException, ExecutionException, KernelException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    int size = 128;
    LongArrayList ids = new LongArrayList();
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < size; i++) {
            ids.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
        }
        org.neo4j.internal.kernel.api.Read read = tx.dataRead();
        Scan<RelationshipScanCursor> scan = read.allRelationshipsScan();
        // when
        Future<LongList> future1 = service.submit(singleBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET, size / 4));
        Future<LongList> future2 = service.submit(singleBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET, size / 4));
        Future<LongList> future3 = service.submit(singleBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET, size / 4));
        Future<LongList> future4 = service.submit(singleBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET, size / 4));
        // then
        LongList ids1 = future1.get();
        LongList ids2 = future2.get();
        LongList ids3 = future3.get();
        LongList ids4 = future4.get();
        TestUtils.assertDistinct(ids1, ids2, ids3, ids4);
        LongList concat = TestUtils.concat(ids1, ids2, ids3, ids4);
        assertEquals(ids.toSortedList(), concat.toSortedList());
        tx.rollback();
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Read(org.neo4j.internal.kernel.api.Read) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 7 with RelationshipScanCursor

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

the class ParallelRelationshipCursorTransactionStateTestBase method shouldScanAllRelationshipFromRandomlySizedWorkers.

@Test
void shouldScanAllRelationshipFromRandomlySizedWorkers() throws InterruptedException, KernelException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    int size = 128;
    LongArrayList ids = new LongArrayList();
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < size; i++) {
            ids.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
        }
        Read read = tx.dataRead();
        Scan<RelationshipScanCursor> scan = read.allRelationshipsScan();
        CursorFactory cursors = testSupport.kernelToTest().cursors();
        // when
        List<Future<LongList>> futures = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            futures.add(service.submit(randomBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET)));
        }
        // then
        List<LongList> lists = futures.stream().map(TestUtils::unsafeGet).collect(Collectors.toList());
        TestUtils.assertDistinct(lists);
        LongList concat = TestUtils.concat(lists);
        assertEquals(ids.toSortedList(), concat.toSortedList());
        tx.rollback();
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) ArrayList(java.util.ArrayList) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) Read(org.neo4j.internal.kernel.api.Read) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 8 with RelationshipScanCursor

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

the class ParallelRelationshipCursorTransactionStateTestBase method scanShouldNotSeeDeletedRelationships.

@Test
void scanShouldNotSeeDeletedRelationships() throws Exception {
    int size = 100;
    MutableLongSet created = LongSets.mutable.empty();
    MutableLongSet deleted = LongSets.mutable.empty();
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < size; i++) {
            created.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
            deleted.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
        }
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        deleted.each(new CheckedLongProcedure() {

            @Override
            public void safeValue(long item) throws Exception {
                tx.dataWrite().relationshipDelete(item);
            }
        });
        try (RelationshipScanCursor cursor = tx.cursors().allocateRelationshipScanCursor(NULL)) {
            Scan<RelationshipScanCursor> scan = tx.dataRead().allRelationshipsScan();
            MutableLongSet seen = LongSets.mutable.empty();
            while (scan.reserveBatch(cursor, 17)) {
                while (cursor.next()) {
                    long relationshipId = cursor.relationshipReference();
                    assertTrue(seen.add(relationshipId));
                    assertTrue(created.remove(relationshipId));
                }
            }
            assertTrue(created.isEmpty());
        }
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) CheckedLongProcedure(org.eclipse.collections.impl.block.procedure.checked.primitive.CheckedLongProcedure) ExecutionException(java.util.concurrent.ExecutionException) KernelException(org.neo4j.exceptions.KernelException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) Test(org.junit.jupiter.api.Test)

Example 9 with RelationshipScanCursor

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

the class ParallelRelationshipCursorTransactionStateTestBase method shouldScanAllRelationshipsFromMultipleThreadWithBigSizeHints.

@Test
void shouldScanAllRelationshipsFromMultipleThreadWithBigSizeHints() throws InterruptedException, ExecutionException, KernelException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    int size = 128;
    LongArrayList ids = new LongArrayList();
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < size; i++) {
            ids.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
        }
        org.neo4j.internal.kernel.api.Read read = tx.dataRead();
        Scan<RelationshipScanCursor> scan = read.allRelationshipsScan();
        // when
        Supplier<RelationshipScanCursor> allocateCursor = () -> cursors.allocateRelationshipScanCursor(NULL);
        Future<LongList> future1 = service.submit(singleBatchWorker(scan, allocateCursor, REL_GET, 100));
        Future<LongList> future2 = service.submit(singleBatchWorker(scan, allocateCursor, REL_GET, 100));
        Future<LongList> future3 = service.submit(singleBatchWorker(scan, allocateCursor, REL_GET, 100));
        Future<LongList> future4 = service.submit(singleBatchWorker(scan, allocateCursor, REL_GET, 100));
        // then
        LongList ids1 = future1.get();
        LongList ids2 = future2.get();
        LongList ids3 = future3.get();
        LongList ids4 = future4.get();
        TestUtils.assertDistinct(ids1, ids2, ids3, ids4);
        LongList concat = TestUtils.concat(ids1, ids2, ids3, ids4);
        assertEquals(ids.toSortedList(), concat.toSortedList());
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Read(org.neo4j.internal.kernel.api.Read) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 10 with RelationshipScanCursor

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

the class ParallelRelationshipCursorTransactionStateTestBase method shouldReserveBatchFromTxState.

@Test
void shouldReserveBatchFromTxState() throws KernelException {
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < 11; i++) {
            write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate());
        }
        try (RelationshipScanCursor cursor = tx.cursors().allocateRelationshipScanCursor(NULL)) {
            Scan<RelationshipScanCursor> scan = tx.dataRead().allRelationshipsScan();
            assertTrue(scan.reserveBatch(cursor, 5));
            assertEquals(5, count(cursor));
            assertTrue(scan.reserveBatch(cursor, 4));
            assertEquals(4, count(cursor));
            assertTrue(scan.reserveBatch(cursor, 6));
            assertEquals(2, count(cursor));
            // now we should have fetched all relationships
            while (scan.reserveBatch(cursor, 3)) {
                assertFalse(cursor.next());
            }
        }
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) Test(org.junit.jupiter.api.Test)

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