Search in sources :

Example 56 with MutableLongSet

use of org.eclipse.collections.api.set.primitive.MutableLongSet 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 57 with MutableLongSet

use of org.eclipse.collections.api.set.primitive.MutableLongSet 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 58 with MutableLongSet

use of org.eclipse.collections.api.set.primitive.MutableLongSet 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 59 with MutableLongSet

use of org.eclipse.collections.api.set.primitive.MutableLongSet in project neo4j by neo4j.

the class ParallelNodeCursorTransactionStateTestBase method scanShouldSeeAddedNodes.

@Test
void scanShouldSeeAddedNodes() throws Exception {
    int size = 100;
    MutableLongSet existing = createNodes(size);
    MutableLongSet added = LongSets.mutable.empty();
    try (KernelTransaction tx = beginTransaction()) {
        for (int i = 0; i < size; i++) {
            added.add(tx.dataWrite().nodeCreate());
        }
        try (NodeCursor cursor = tx.cursors().allocateNodeCursor(NULL)) {
            Scan<NodeCursor> scan = tx.dataRead().allNodesScan();
            MutableLongSet seen = LongSets.mutable.empty();
            while (scan.reserveBatch(cursor, 17)) {
                while (cursor.next()) {
                    long nodeId = cursor.nodeReference();
                    assertTrue(seen.add(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) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 60 with MutableLongSet

use of org.eclipse.collections.api.set.primitive.MutableLongSet in project neo4j by neo4j.

the class IdRangeMarkerTest method shouldIgnoreReservedIds.

@Test
void shouldIgnoreReservedIds() throws IOException {
    // given
    long reservedId = IdValidator.INTEGER_MINUS_ONE;
    // when
    MutableLongSet expectedIds = LongSets.mutable.empty();
    try (IdRangeMarker marker = new IdRangeMarker(idsPerEntry, layout, tree.writer(NULL), mock(Lock.class), IdRangeMerger.DEFAULT, true, new AtomicBoolean(), 1, new AtomicLong(reservedId - 1), true, NO_MONITOR)) {
        for (long id = reservedId - 1; id <= reservedId + 1; id++) {
            marker.markDeleted(id);
            if (id != reservedId) {
                expectedIds.add(id);
            }
        }
    }
    // then
    MutableLongSet deletedIdsInTree = LongSets.mutable.empty();
    tree.visit(new GBPTreeVisitor.Adaptor<>() {

        private IdRangeKey idRangeKey;

        @Override
        public void key(IdRangeKey idRangeKey, boolean isLeaf, long offloadId) {
            this.idRangeKey = idRangeKey;
        }

        @Override
        public void value(IdRange idRange) {
            for (int i = 0; i < idsPerEntry; i++) {
                if (idRange.getState(i) == DELETED) {
                    deletedIdsInTree.add(idRangeKey.getIdRangeIdx() * idsPerEntry + i);
                }
            }
        }
    }, NULL);
    assertEquals(expectedIds, deletedIdsInTree);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) GBPTreeVisitor(org.neo4j.index.internal.gbptree.GBPTreeVisitor) Lock(java.util.concurrent.locks.Lock) Test(org.junit.jupiter.api.Test)

Aggregations

MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)153 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)57 Test (org.junit.jupiter.api.Test)54 Test (org.junit.Test)49 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)17 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 ArrayList (java.util.ArrayList)13 IndexValueCapability (org.neo4j.internal.schema.IndexValueCapability)12 LongIterator (org.eclipse.collections.api.iterator.LongIterator)10 LongSet (org.eclipse.collections.api.set.primitive.LongSet)9 Value (org.neo4j.values.storable.Value)8 Map (java.util.Map)7 MutableLongList (org.eclipse.collections.api.list.primitive.MutableLongList)7 Transaction (org.neo4j.graphdb.Transaction)7 Write (org.neo4j.internal.kernel.api.Write)7 NavigableMap (java.util.NavigableMap)6 UnmodifiableMap (org.eclipse.collections.impl.UnmodifiableMap)6 LongArrayList (org.eclipse.collections.impl.list.mutable.primitive.LongArrayList)6 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)6