use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class DefaultValueMapperTest method shouldHandleSingleRelationshipPath.
@Test
void shouldHandleSingleRelationshipPath() {
// Given
Node start, end;
Relationship relationship;
try (Transaction tx = db.beginTx()) {
start = tx.createNode();
end = tx.createNode();
relationship = start.createRelationshipTo(end, RelationshipType.withName("R"));
tx.commit();
}
// Then
try (Transaction tx = db.beginTx()) {
var mapper = new DefaultValueMapper((InternalTransaction) tx);
Path mapped = mapper.mapPath(path(asNodeValues(start, end), asRelationshipsValues(relationship)));
assertThat(mapped.length()).isEqualTo(1);
assertThat(mapped.startNode()).isEqualTo(start);
assertThat(mapped.endNode()).isEqualTo(end);
assertThat(Iterables.asList(mapped.relationships())).isEqualTo(singletonList(relationship));
assertThat(Iterables.asList(mapped.reverseRelationships())).isEqualTo(singletonList(relationship));
assertThat(Iterables.asList(mapped.nodes())).isEqualTo(Arrays.asList(start, end));
assertThat(Iterables.asList(mapped.reverseNodes())).isEqualTo(Arrays.asList(end, start));
assertThat(mapped.lastRelationship()).isEqualTo(relationship);
assertThat(Iterators.asList(mapped.iterator())).isEqualTo(Arrays.asList(start, relationship, end));
}
}
use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class DenseNodeConcurrencyIT method shouldHandleDetachDeleteWithConcurrentAdds.
@Test
void shouldHandleDetachDeleteWithConcurrentAdds() {
long initialNodes;
long initialRelationships;
try (Transaction tx = database.beginTx()) {
initialNodes = Iterables.count(tx.getAllNodes());
initialRelationships = Iterables.count(tx.getAllRelationships());
}
long denseNodeToDelete = createNode(new HashSet<>(), 100);
AtomicInteger numNodes = new AtomicInteger(2);
int numModifiers = Runtime.getRuntime().availableProcessors() - 1;
int numCreators = Math.max(numModifiers * 2 / 3, 2);
int numDeleters = Math.max(numModifiers / 3, 1);
CountDownLatch latch = new CountDownLatch(numCreators * 2);
AtomicBoolean done = new AtomicBoolean();
Race race = new Race().withFailureAction(t -> done.set(true));
race.addContestants(1, throwing(() -> {
latch.await();
while (true) {
try (Transaction tx = database.beginTx()) {
InternalTransaction internalTx = (InternalTransaction) tx;
internalTx.kernelTransaction().dataWrite().nodeDetachDelete(denseNodeToDelete);
tx.commit();
break;
} catch (DeadlockDetectedException ignore) {
// ignore deadlock, try again
}
}
numNodes.decrementAndGet();
done.set(true);
}));
race.addContestants(numCreators, throwing(() -> {
try {
int creations = 0;
while (!done.get() && creations < 20) {
latch.countDown();
try (Transaction tx = database.beginTx()) {
Node denseNode = tx.getNodeById(denseNodeToDelete);
var type = random.nextBoolean() ? TEST : TEST2;
switch(random.nextInt(3)) {
case 0:
denseNode.createRelationshipTo(tx.createNode(), type);
numNodes.incrementAndGet();
break;
case 1:
tx.createNode().createRelationshipTo(denseNode, type);
numNodes.incrementAndGet();
default:
denseNode.createRelationshipTo(denseNode, type);
break;
}
tx.commit();
creations++;
// Provoke race, since we do not have fair locking, this will give a small windows to grab exclusive
Thread.sleep(1);
} catch (DeadlockDetectedException ignore) {
// ignore deadlock, try again
}
}
} catch (NotFoundException e) {
// expected, exit!
}
}));
race.addContestants(numDeleters, throwing(() -> {
try {
while (!done.get()) {
try (Transaction tx = database.beginTx()) {
Node denseNode = tx.getNodeById(denseNodeToDelete);
Relationship[] relationships = Iterables.asArray(Relationship.class, denseNode.getRelationships());
if (relationships.length > 0) {
random.among(relationships).delete();
}
tx.commit();
// Provoke race, since we do not have fair locking, this will give a small windows to grab exclusive
Thread.sleep(1);
} catch (DeadlockDetectedException ignore) {
// deadlock, retry
}
}
} catch (NotFoundException e) {
// expected, exit!
}
}));
race.goUnchecked();
try (Transaction tx = database.beginTx()) {
assertThat(Iterables.count(tx.getAllNodes())).isEqualTo(numNodes.get() + initialNodes);
assertThat(Iterables.count(tx.getAllRelationships())).isEqualTo(initialRelationships);
assertThatThrownBy(() -> tx.getNodeById(denseNodeToDelete)).isInstanceOf(NotFoundException.class);
}
}
use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class LabelsAcceptanceTest method shouldAllowManyLabelsAndPropertyCursor.
@Test
void shouldAllowManyLabelsAndPropertyCursor() {
int propertyCount = 10;
int labelCount = 15;
Node node;
try (Transaction tx = db.beginTx()) {
node = tx.createNode();
for (int i = 0; i < propertyCount; i++) {
node.setProperty("foo" + i, "bar");
}
for (int i = 0; i < labelCount; i++) {
node.addLabel(label("label" + i));
}
tx.commit();
}
Set<Integer> seenProperties = new HashSet<>();
Set<Integer> seenLabels = new HashSet<>();
try (Transaction tx = db.beginTx()) {
KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
try (NodeCursor nodes = ktx.cursors().allocateNodeCursor(CursorContext.NULL);
PropertyCursor propertyCursor = ktx.cursors().allocatePropertyCursor(CursorContext.NULL, INSTANCE)) {
ktx.dataRead().singleNode(node.getId(), nodes);
while (nodes.next()) {
nodes.properties(propertyCursor);
while (propertyCursor.next()) {
seenProperties.add(propertyCursor.propertyKey());
}
TokenSet labels = nodes.labels();
for (int i = 0; i < labels.numberOfTokens(); i++) {
seenLabels.add(labels.token(i));
}
}
}
tx.commit();
}
assertEquals(propertyCount, seenProperties.size());
assertEquals(labelCount, seenLabels.size());
}
use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class NodeEntityTest method traceNodePageCacheAccessOnRelationshipsAccess.
@Test
void traceNodePageCacheAccessOnRelationshipsAccess() {
long targetId;
var relationshipType = RelationshipType.withName("connection");
try (Transaction tx = db.beginTx()) {
var target = tx.createNode();
for (int i = 0; i < 100; i++) {
tx.createNode().createRelationshipTo(target, relationshipType);
}
targetId = target.getId();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
var cursorContext = ((InternalTransaction) tx).kernelTransaction().cursorContext();
PageCursorTracer cursorTracer = cursorContext.getCursorTracer();
var source = tx.getNodeById(targetId);
cursorTracer.reportEvents();
assertZeroTracer(cursorContext);
assertThat(count(source.getRelationships(Direction.INCOMING, relationshipType))).isGreaterThan(0);
assertThat(cursorTracer.hits()).isEqualTo(3);
assertThat(cursorTracer.unpins()).isEqualTo(2);
assertThat(cursorTracer.pins()).isEqualTo(3);
}
}
use of org.neo4j.kernel.impl.coreapi.InternalTransaction in project neo4j by neo4j.
the class NodeEntityTest method traceNodePageCacheAccessOnDegreeCount.
@Test
void traceNodePageCacheAccessOnDegreeCount() {
long sourceId;
try (Transaction tx = db.beginTx()) {
var source = tx.createNode();
var relationshipType = RelationshipType.withName("connection");
createDenseNodeWithShortIncomingChain(tx, source, relationshipType);
sourceId = source.getId();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
var cursorContext = ((InternalTransaction) tx).kernelTransaction().cursorContext();
PageCursorTracer cursorTracer = cursorContext.getCursorTracer();
var source = tx.getNodeById(sourceId);
cursorTracer.reportEvents();
assertZeroTracer(cursorContext);
source.getDegree(Direction.INCOMING);
assertThat(cursorTracer.hits()).isEqualTo(3);
assertThat(cursorTracer.unpins()).isEqualTo(0);
assertThat(cursorTracer.pins()).isEqualTo(3);
}
}
Aggregations