use of org.neo4j.values.virtual.RelationshipValue in project neo4j by neo4j.
the class PathValueBuilderTest method shouldHandleNoValue.
@Test
void shouldHandleNoValue() {
// Given (n1)<--(n2)-->(n3)
NodeValue node = node(42);
RelationshipValue relationship = relationship(1337, node(43), node);
PathValueBuilder builder = builder(node, relationship);
// When (n1)<--(n2)--(n3)
builder.addNode(node);
builder.addIncoming(relationship);
builder.addUndirected(NO_VALUE);
// Then
assertEquals(NO_VALUE, builder.build());
}
use of org.neo4j.values.virtual.RelationshipValue in project neo4j by neo4j.
the class PathValueBuilderTest method builder.
private static PathValueBuilder builder(AnyValue... values) {
DbAccess dbAccess = mock(DbAccess.class);
RelationshipScanCursor cursors = mock(RelationshipScanCursor.class);
Map<Long, RelationshipValue> relMap = new HashMap<>();
for (AnyValue value : values) {
if (value instanceof NodeValue) {
NodeValue nodeValue = (NodeValue) value;
when(dbAccess.nodeById(nodeValue.id())).thenReturn(nodeValue);
} else if (value instanceof RelationshipValue) {
RelationshipValue relationshipValue = (RelationshipValue) value;
relMap.put(relationshipValue.id(), relationshipValue);
} else {
throw new AssertionError("invalid input");
}
Mockito.doAnswer((Answer<Void>) invocationOnMock -> {
long id = invocationOnMock.getArgument(0);
RelationshipScanCursor cursor = invocationOnMock.getArgument(1);
RelationshipValue rel = relMap.get(id);
when(cursor.sourceNodeReference()).thenReturn(rel.startNode().id());
when(cursor.targetNodeReference()).thenReturn(rel.endNode().id());
return null;
}).when(dbAccess).singleRelationship(anyLong(), any(RelationshipScanCursor.class));
}
return new PathValueBuilder(dbAccess, cursors);
}
use of org.neo4j.values.virtual.RelationshipValue in project neo4j by neo4j.
the class PathValueBuilder method addMultipleUndirected.
/**
* Adds multiple undirected relationships to the path
*
* @param relationships the undirected relationships to add
* @param target the final target node of the path
*/
@CalledFromGeneratedCode
public void addMultipleUndirected(ListValue relationships, NodeValue target) {
if (relationships.isEmpty()) {
// nothing to add
return;
}
long previous = nodes.get(nodes.size() - 1).id();
RelationshipValue first = (RelationshipValue) relationships.head();
boolean correctDirection = startNode(first, dbAccess, cursor).id() == previous || endNode(first, dbAccess, cursor).id() == previous;
int i;
if (correctDirection) {
for (i = 0; i < relationships.size() - 1; i++) {
AnyValue value = relationships.value(i);
if (notNoValue(value)) {
// we know these relationships have already loaded start and end relationship
// so we should not use CypherFunctions::[start,end]Node to look them up
addUndirectedWhenRelationshipsAreFullyLoaded((RelationshipValue) value);
}
}
} else {
for (i = relationships.size() - 1; i > 0; i--) {
AnyValue value = relationships.value(i);
if (notNoValue(value)) {
// we know these relationships have already loaded start and end relationship
// so we should not use CypherFunctions::[start,end]Node to look them up
addUndirectedWhenRelationshipsAreFullyLoaded((RelationshipValue) relationships.value(i));
}
}
}
AnyValue last = relationships.value(i);
if (notNoValue(last)) {
rels.add((RelationshipValue) last);
nodes.add(target);
}
}
use of org.neo4j.values.virtual.RelationshipValue in project neo4j by neo4j.
the class PathValueBuilder method addMultipleUndirected.
/**
* Adds multiple undirected relationships to the path
*
* @param relationships the undirected relationships to add
*/
@CalledFromGeneratedCode
public void addMultipleUndirected(ListValue relationships) {
if (relationships.isEmpty()) {
// nothing to add
return;
}
long previous = nodes.get(nodes.size() - 1).id();
RelationshipValue first = (RelationshipValue) relationships.head();
boolean correctDirection = startNode(first, dbAccess, cursor).id() == previous || endNode(first, dbAccess, cursor).id() == previous;
if (correctDirection) {
for (AnyValue value : relationships) {
if (notNoValue(value)) {
addUndirectedWhenRelationshipsAreFullyLoaded((RelationshipValue) value);
}
}
} else {
ListValue reversed = relationships.reverse();
for (AnyValue rel : reversed) {
if (notNoValue(rel)) {
addUndirectedWhenRelationshipsAreFullyLoaded((RelationshipValue) rel);
}
}
}
}
Aggregations