use of org.neo4j.storageengine.api.Degrees in project neo4j by neo4j.
the class CachingExpandIntoTest method shouldComputeDegreeWithType.
@Test
void shouldComputeDegreeWithType() throws Exception {
// GIVEN
long node;
int in, out, loop;
try (KernelTransaction tx = transaction()) {
Write write = tx.dataWrite();
node = denseNode(tx);
TokenWrite tokenWrite = tx.tokenWrite();
out = tokenWrite.relationshipTypeGetOrCreateForName("OUT");
in = tokenWrite.relationshipTypeGetOrCreateForName("IN");
loop = tokenWrite.relationshipTypeGetOrCreateForName("LOOP");
write.relationshipCreate(node, out, write.nodeCreate());
write.relationshipCreate(node, out, write.nodeCreate());
write.relationshipCreate(write.nodeCreate(), in, node);
write.relationshipCreate(node, loop, node);
tx.commit();
}
try (KernelTransaction tx = transaction()) {
Read read = tx.dataRead();
CursorFactory cursors = tx.cursors();
try (NodeCursor nodes = cursors.allocateNodeCursor(tx.cursorContext())) {
CachingExpandInto expand = new CachingExpandInto(tx.dataRead(), OUTGOING, MEMORY_TRACKER);
read.singleNode(node, nodes);
assertThat(nodes.next()).isEqualTo(true);
assertThat(nodes.supportsFastDegreeLookup()).isEqualTo(true);
Degrees degrees = nodes.degrees(ALL_RELATIONSHIPS);
assertThat(degrees.outgoingDegree(out)).isEqualTo(2);
assertThat(degrees.outgoingDegree(in)).isEqualTo(0);
assertThat(degrees.outgoingDegree(loop)).isEqualTo(1);
assertThat(degrees.incomingDegree(out)).isEqualTo(0);
assertThat(degrees.incomingDegree(in)).isEqualTo(1);
assertThat(degrees.incomingDegree(loop)).isEqualTo(1);
assertThat(degrees.totalDegree(out)).isEqualTo(2);
assertThat(degrees.totalDegree(in)).isEqualTo(1);
assertThat(degrees.totalDegree(loop)).isEqualTo(1);
}
}
}
use of org.neo4j.storageengine.api.Degrees in project neo4j by neo4j.
the class NodeEntity method getRelationshipTypes.
@Override
public Iterable<RelationshipType> getRelationshipTypes() {
KernelTransaction transaction = internalTransaction.kernelTransaction();
try {
NodeCursor nodes = transaction.ambientNodeCursor();
TokenRead tokenRead = transaction.tokenRead();
singleNode(transaction, nodes);
Degrees degrees = nodes.degrees(ALL_RELATIONSHIPS);
List<RelationshipType> types = new ArrayList<>();
for (int type : degrees.types()) {
// only include this type if there are any relationships with this type
if (degrees.totalDegree(type) > 0) {
types.add(RelationshipType.withName(tokenRead.relationshipTypeName(type)));
}
}
return types;
} catch (KernelException e) {
throw new NotFoundException("Relationship name not found.", e);
}
}
use of org.neo4j.storageengine.api.Degrees in project neo4j by neo4j.
the class RelationshipTransactionStateTestBase method shouldNewRelationshipBetweenAlreadyConnectedSparseNodes.
@Test
void shouldNewRelationshipBetweenAlreadyConnectedSparseNodes() throws Exception {
long start;
long end;
long existingRelationship;
int type;
try (KernelTransaction tx = beginTransaction()) {
Write write = tx.dataWrite();
start = write.nodeCreate();
end = write.nodeCreate();
type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
existingRelationship = write.relationshipCreate(start, type, end);
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());
assertFalse(node.supportsFastDegreeLookup());
Degrees degrees = node.degrees(selection(type, BOTH));
assertEquals(2, degrees.outgoingDegree());
assertEquals(0, degrees.incomingDegree());
assertRelationships(OUTGOING, node, type, traversal, newRelationship, existingRelationship);
}
}
}
use of org.neo4j.storageengine.api.Degrees in project neo4j by neo4j.
the class RelationshipTransactionStateTestBase method assertCount.
private void assertCount(int count, RelationshipDirection direction, Consumer<Degrees> asserter) throws Exception {
long start;
int type;
try (KernelTransaction tx = beginTransaction()) {
Write write = tx.dataWrite();
start = write.nodeCreate();
type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
for (int i = 0; i < count; i++) {
createRelationship(direction, start, type, write);
}
tx.commit();
}
try (KernelTransaction tx = beginTransaction()) {
Write write = tx.dataWrite();
createRelationship(direction, start, type, write);
try (NodeCursor node = tx.cursors().allocateNodeCursor(NULL)) {
Read read = tx.dataRead();
read.singleNode(start, node);
assertTrue(node.next());
Degrees degrees = node.degrees(ALL_RELATIONSHIPS);
asserter.accept(degrees);
}
}
}
use of org.neo4j.storageengine.api.Degrees 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");
}
}
Aggregations