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);
}
}
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();
}
}
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);
}
}
}
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());
}
}
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());
}
}
Aggregations