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