use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class ShortestPath method hitsToPaths.
private static Collection<Path> hitsToPaths(EvaluationContext context, Collection<Hit> depthHits, Node start, Node end, boolean stopAsap, int maxResultCount, MemoryTracker memoryTracker) {
Set<Path> paths = HeapTrackingCollections.newSet(memoryTracker);
for (Hit hit : depthHits) {
for (PathImpl path : hitToPaths(context, hit, start, end, stopAsap)) {
memoryTracker.allocateHeap(path.estimatedHeapUsage());
paths.add(path);
if (paths.size() >= maxResultCount) {
break;
}
}
}
return paths;
}
use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class PathProxyTest method shouldIterateThroughRelationshipsInReverse.
@Test
void shouldIterateThroughRelationshipsInReverse() {
// given
Path path = new PathProxy(transaction, new long[] { 1, 2, 3 }, new long[] { 100, 200 }, new int[] { 0, ~0 });
Iterator<Relationship> iterator = path.reverseRelationships().iterator();
Relationship relationship;
// then
assertTrue(iterator.hasNext());
assertThat(relationship = iterator.next()).isInstanceOf(Relationship.class);
assertEquals(200, relationship.getId());
assertEquals(3, relationship.getStartNodeId());
assertEquals(2, relationship.getEndNodeId());
assertTrue(iterator.hasNext());
assertThat(relationship = iterator.next()).isInstanceOf(Relationship.class);
assertEquals(100, relationship.getId());
assertEquals(1, relationship.getStartNodeId());
assertEquals(2, relationship.getEndNodeId());
assertFalse(iterator.hasNext());
}
use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class ProcedureCompilation method toAnyValue.
/**
* Takes an expression evaluating to one of the supported java values and turns
* it into the corresponding AnyValue
*
* @param expression the expression to evaluate
* @param userType the type of the expression to map
* @return an expression properly mapped to AnyValue
*/
private static Expression toAnyValue(Expression expression, Class<?> userType, Expression context) {
if (AnyValue.class.isAssignableFrom(userType)) {
return nullCheck(expression, cast(userType, expression));
}
String type = userType.getCanonicalName();
if (type.equals(LONG)) {
return invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), expression);
} else if (type.equals(BOXED_LONG)) {
return nullCheck(expression, invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), unbox(expression)));
} else if (type.equals(DOUBLE)) {
return invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), expression);
} else if (type.equals(BOXED_DOUBLE)) {
return nullCheck(expression, invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), unbox(expression)));
} else if (type.equals(NUMBER)) {
return nullCheck(expression, invoke(methodReference(Values.class, NumberValue.class, "numberValue", Number.class), expression));
} else if (type.equals(BOOLEAN)) {
return invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), expression);
} else if (type.equals(BOXED_BOOLEAN)) {
return nullCheck(expression, invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), unbox(expression)));
} else if (type.equals(STRING)) {
return invoke(methodReference(Values.class, Value.class, "stringOrNoValue", String.class), expression);
} else if (type.equals(BYTE_ARRAY)) {
return nullCheck(expression, invoke(methodReference(Values.class, ByteArray.class, "byteArray", byte[].class), expression));
} else if (type.equals(LIST)) {
return nullCheck(expression, invoke(methodReference(ValueUtils.class, ListValue.class, "asListValue", Iterable.class), expression));
} else if (type.equals(MAP)) {
return nullCheck(expression, invoke(methodReference(ValueUtils.class, MapValue.class, "asMapValue", Map.class), expression));
} else if (type.equals(ZONED_DATE_TIME)) {
return nullCheck(expression, invoke(methodReference(DateTimeValue.class, DateTimeValue.class, "datetime", ZonedDateTime.class), expression));
} else if (type.equals(OFFSET_TIME)) {
return nullCheck(expression, invoke(methodReference(TimeValue.class, TimeValue.class, "time", OffsetTime.class), expression));
} else if (type.equals(LOCAL_DATE)) {
return nullCheck(expression, invoke(methodReference(DateValue.class, DateValue.class, "date", LocalDate.class), expression));
} else if (type.equals(LOCAL_TIME)) {
return nullCheck(expression, invoke(methodReference(LocalTimeValue.class, LocalTimeValue.class, "localTime", LocalTime.class), expression));
} else if (type.equals(LOCAL_DATE_TIME)) {
return nullCheck(expression, invoke(methodReference(LocalDateTimeValue.class, LocalDateTimeValue.class, "localDateTime", LocalDateTime.class), expression));
} else if (type.equals(TEMPORAL_AMOUNT)) {
return nullCheck(expression, invoke(methodReference(Values.class, DurationValue.class, "durationValue", TemporalAmount.class), expression));
} else if (type.equals(NODE)) {
Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
Expression getNode = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
return nullCheck(expression, invoke(methodReference(ValueUtils.class, NodeValue.class, "fromNodeEntity", Node.class), getNode));
} else if (type.equals(RELATIONSHIP)) {
Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
Expression getRelationship = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
return nullCheck(expression, invoke(methodReference(ValueUtils.class, RelationshipValue.class, "fromRelationshipEntity", Relationship.class), getRelationship));
} else if (type.equals(PATH)) {
return nullCheck(expression, invoke(methodReference(ValueUtils.class, PathValue.class, "fromPath", Path.class), expression));
} else if (type.equals(POINT)) {
return nullCheck(expression, invoke(methodReference(ValueUtils.class, PointValue.class, "asPointValue", Point.class), expression));
} else {
return invoke(methodReference(ValueUtils.class, AnyValue.class, "of", Object.class), expression);
}
}
use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class ExecutionResultSerializerTest method shouldProduceResultStreamWithLegacyRestFormat.
@Test
void shouldProduceResultStreamWithLegacyRestFormat() throws Exception {
// given
Node[] node = { node(0, properties(property("name", "node0"))), node(1, properties(property("name", "node1"))), node(2, properties(property("name", "node2"))) };
Relationship[] rel = { relationship(0, node[0], "KNOWS", node[1], property("name", "rel0")), relationship(1, node[2], "LOVES", node[1], property("name", "rel1")) };
Path path = GraphMock.path(node[0], link(rel[0], node[1]), link(rel[1], node[2]));
serializer = getSerializerWith(transactionHandle, output, "http://base.uri/");
var resultRow = Map.of("node", node[0], "rel", rel[0], "path", path, "map", Map.of("n1", node[1], "r1", rel[1]));
// when
writeStatementStart(serializer, Collections.singletonList(ResultDataContent.rest), "node", "rel", "path", "map");
writeRecord(serializer, resultRow, "node", "rel", "path", "map");
writeStatementEnd(serializer);
writeTransactionInfo(serializer);
// then
String result = output.toString(UTF_8);
JsonNode json = jsonNode(result);
Map<String, Integer> columns = new HashMap<>();
int col = 0;
JsonNode results = json.get("results").get(0);
for (JsonNode column : results.get("columns")) {
columns.put(column.asText(), col++);
}
JsonNode row = results.get("data").get(0).get("rest");
JsonNode jsonNode = row.get(columns.get("node"));
JsonNode jsonRel = row.get(columns.get("rel"));
JsonNode jsonPath = row.get(columns.get("path"));
JsonNode jsonMap = row.get(columns.get("map"));
assertEquals("http://base.uri/node/0", jsonNode.get("self").asText());
assertEquals("http://base.uri/relationship/0", jsonRel.get("self").asText());
assertEquals(2, jsonPath.get("length").asInt());
assertEquals("http://base.uri/node/0", jsonPath.get("start").asText());
assertEquals("http://base.uri/node/2", jsonPath.get("end").asText());
assertEquals("http://base.uri/node/1", jsonMap.get("n1").get("self").asText());
assertEquals("http://base.uri/relationship/1", jsonMap.get("r1").get("self").asText());
}
use of org.neo4j.graphdb.Path in project neo4j by neo4j.
the class StandardBranchCollisionDetector method evaluate.
@Override
@SuppressWarnings("unchecked")
public Collection<Path> evaluate(TraversalBranch branch, Direction direction) {
// [0] for paths from start, [1] for paths from end
Collection<TraversalBranch>[] pathsHere = paths.get(branch.endNode());
int index = direction.ordinal();
if (pathsHere == null) {
pathsHere = new Collection[] { new ArrayList<>(), new ArrayList<>() };
paths.put(branch.endNode(), pathsHere);
}
pathsHere[index].add(branch);
// If there are paths from the other side then include all the
// combined paths
Collection<TraversalBranch> otherCollections = pathsHere[index == 0 ? 1 : 0];
if (!otherCollections.isEmpty()) {
Collection<Path> foundPaths = new ArrayList<>();
for (TraversalBranch otherBranch : otherCollections) {
TraversalBranch startPath = index == 0 ? branch : otherBranch;
TraversalBranch endPath = index == 0 ? otherBranch : branch;
BidirectionalTraversalBranchPath path = new BidirectionalTraversalBranchPath(startPath, endPath);
if (isAcceptablePath(path)) {
if (returnedPaths.add(path) && includePath(path, startPath, endPath)) {
foundPaths.add(path);
}
}
}
if (!foundPaths.isEmpty()) {
return foundPaths;
}
}
return null;
}
Aggregations