use of org.neo4j.internal.kernel.api.RelationshipTraversalCursor in project neo4j by neo4j.
the class RelationshipTraversalCursorTestBase method traverse.
private void traverse(RelationshipTestSupport.StartNode start, boolean detached) throws KernelException {
// given
try (NodeCursor node = cursors.allocateNodeCursor(NULL);
RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor(NULL)) {
// when
read.singleNode(start.id, node);
assertTrue(node.next(), "access node");
if (detached) {
read.relationships(start.id, node.relationshipsReference(), ALL_RELATIONSHIPS, relationship);
} else {
node.relationships(relationship, ALL_RELATIONSHIPS);
}
Map<String, Integer> counts = count(tx, relationship);
// then
assertCounts(start.expectedCounts(), counts);
}
}
use of org.neo4j.internal.kernel.api.RelationshipTraversalCursor in project neo4j by neo4j.
the class RelationshipTraversalCursorTestBase method shouldTraverseRelationshipsOfGivenType.
@Test
void shouldTraverseRelationshipsOfGivenType() {
// given
try (NodeCursor node = cursors.allocateNodeCursor(NULL);
RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor(NULL)) {
int empty = 0;
// when
read.allNodesScan(node);
while (node.next()) {
// then
Degrees degrees = node.degrees(ALL_RELATIONSHIPS);
boolean none = true;
for (int type : degrees.types()) {
none = false;
Sizes degree = new Sizes();
node.relationships(relationship, selection(type, BOTH));
while (relationship.next()) {
assertEquals(type, relationship.type(), "node #" + node.nodeReference() + " relationship has label not part of selection");
if (relationship.sourceNodeReference() == node.nodeReference()) {
degree.outgoing++;
}
if (relationship.targetNodeReference() == node.nodeReference()) {
degree.incoming++;
}
degree.total++;
}
assertNotEquals(0, degree.total, "all");
assertEquals(degrees.outgoingDegree(type), degree.outgoing, "node #" + node.nodeReference() + " outgoing");
assertEquals(degrees.incomingDegree(type), degree.incoming, "node #" + node.nodeReference() + " incoming");
assertEquals(degrees.totalDegree(type), degree.total, "node #" + node.nodeReference() + " all = incoming + outgoing - loop");
}
if (none) {
empty++;
}
}
// then
assertEquals(1, empty, "number of empty nodes");
}
}
use of org.neo4j.internal.kernel.api.RelationshipTraversalCursor in project neo4j by neo4j.
the class RelationshipTraversalCursorTestBase method shouldFollowSpecificRelationship.
@Test
void shouldFollowSpecificRelationship() {
// given
try (NodeCursor node = cursors.allocateNodeCursor(NULL);
RelationshipTraversalCursor relationship = cursors.allocateRelationshipTraversalCursor(NULL)) {
// when - traversing from start to end
read.singleNode(start, node);
assertTrue(node.next(), "access start node");
int[] types = node.relationshipTypes();
assertTrue(types.length > 0);
node.relationships(relationship, selection(types[0], OUTGOING));
assertTrue(relationship.next(), "access outgoing relationships");
// then
assertEquals(start, relationship.sourceNodeReference(), "source node");
assertEquals(end, relationship.targetNodeReference(), "target node");
assertEquals(start, relationship.originNodeReference(), "node of origin");
assertEquals(end, relationship.otherNodeReference(), "neighbouring node");
assertEquals(types[0], relationship.type(), "relationship should have correct label");
assertFalse(relationship.next(), "only a single relationship");
node.relationships(relationship, selection(types[0], INCOMING));
assertFalse(relationship.next(), "no incoming relationships");
// when - traversing from end to start
read.singleNode(end, node);
assertTrue(node.next(), "access start node");
types = node.relationshipTypes();
assertTrue(types.length > 0);
node.relationships(relationship, selection(types[0], INCOMING));
assertTrue(relationship.next(), "access incoming relationships");
// then
assertEquals(start, relationship.sourceNodeReference(), "source node");
assertEquals(end, relationship.targetNodeReference(), "target node");
assertEquals(end, relationship.originNodeReference(), "node of origin");
assertEquals(start, relationship.otherNodeReference(), "neighbouring node");
assertEquals(types[0], relationship.type(), "relationship should have correct label");
assertFalse(relationship.next(), "only a single relationship");
node.relationships(relationship, selection(types[0], OUTGOING));
assertFalse(relationship.next(), "no outgoing relationships");
}
}
use of org.neo4j.internal.kernel.api.RelationshipTraversalCursor in project neo4j by neo4j.
the class RelationshipTransactionStateTestBase method shouldAddToCountFromTxState.
@Test
void shouldAddToCountFromTxState() throws Exception {
long start;
long existingRelationship;
int type;
try (KernelTransaction tx = beginTransaction()) {
Write write = tx.dataWrite();
start = write.nodeCreate();
type = tx.tokenWrite().relationshipTypeGetOrCreateForName("OUT");
existingRelationship = write.relationshipCreate(start, type, write.nodeCreate());
tx.commit();
}
try (KernelTransaction tx = beginTransaction()) {
Write write = tx.dataWrite();
long newRelationship = write.relationshipCreate(start, type, write.nodeCreate());
try (NodeCursor node = tx.cursors().allocateNodeCursor(NULL);
RelationshipTraversalCursor traversal = tx.cursors().allocateRelationshipTraversalCursor(NULL)) {
org.neo4j.internal.kernel.api.Read read = tx.dataRead();
read.singleNode(start, node);
assertTrue(node.next());
Degrees degrees = node.degrees(selection(type, BOTH));
assertEquals(2, degrees.outgoingDegree(type));
assertEquals(0, degrees.incomingDegree(type));
assertRelationships(OUTGOING, node, type, traversal, newRelationship, existingRelationship);
}
}
}
use of org.neo4j.internal.kernel.api.RelationshipTraversalCursor in project neo4j by neo4j.
the class RelationshipTransactionStateTestBase method traverse.
private void traverse(RelationshipTestSupport.StartNode start, boolean detached) throws Exception {
try (KernelTransaction tx = beginTransaction()) {
Map<String, Integer> expectedCounts = modifyStartNodeRelationships(start, tx);
// given
try (NodeCursor node = tx.cursors().allocateNodeCursor(NULL);
RelationshipTraversalCursor relationship = tx.cursors().allocateRelationshipTraversalCursor(NULL)) {
// when
tx.dataRead().singleNode(start.id, node);
assertTrue(node.next(), "access node");
if (detached) {
tx.dataRead().relationships(start.id, node.relationshipsReference(), ALL_RELATIONSHIPS, relationship);
} else {
node.relationships(relationship, ALL_RELATIONSHIPS);
}
Map<String, Integer> counts = count(tx, relationship);
// then
assertCounts(expectedCounts, counts);
}
tx.rollback();
}
}
Aggregations