use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.
the class Json method toTree.
@Procedure("apoc.convert.toTree")
@Description("apoc.convert.toTree([paths],[lowerCaseRels=true]) creates a stream of nested documents representing the at least one root of these paths")
public // todo optinally provide root node
Stream<MapResult> toTree(@Name("paths") List<Path> paths, @Name(value = "lowerCaseRels", defaultValue = "true") boolean lowerCaseRels) {
if (paths.isEmpty())
return Stream.of(new MapResult(Collections.emptyMap()));
Map<Long, Map<String, Object>> maps = new HashMap<>(paths.size() * 100);
for (Path path : paths) {
Iterator<PropertyContainer> it = path.iterator();
while (it.hasNext()) {
Node n = (Node) it.next();
Map<String, Object> nMap = maps.computeIfAbsent(n.getId(), (id) -> toMap(n));
if (it.hasNext()) {
Relationship r = (Relationship) it.next();
Node m = r.getOtherNode(n);
Map<String, Object> mMap = maps.computeIfAbsent(m.getId(), (id) -> toMap(m));
String typeName = r.getType().name();
if (lowerCaseRels)
typeName = typeName.toLowerCase();
mMap = addRelProperties(mMap, typeName, r);
// parent-[:HAS_CHILD]->(child) vs. (parent)<-[:PARENT_OF]-(child)
if (!nMap.containsKey(typeName))
nMap.put(typeName, new ArrayList<>(16));
List list = (List) nMap.get(typeName);
if (!list.contains(mMap))
// todo performance, use set instead and convert to map at the end?
list.add(mMap);
}
}
}
return paths.stream().map(Path::startNode).distinct().map(n -> maps.remove(n.getId())).map(m -> m == null ? Collections.<String, Object>emptyMap() : m).map(MapResult::new);
}
use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.
the class Xml method xmlXpathToMapResult.
private Stream<MapResult> xmlXpathToMapResult(@Name("url") String url, boolean simpleMode, String path, Map<String, Object> config) throws Exception {
if (config == null)
config = Collections.emptyMap();
boolean failOnError = (boolean) config.getOrDefault("failOnError", true);
List<MapResult> result = new ArrayList<>();
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
FileUtils.checkReadAllowed(url);
Document doc = documentBuilder.parse(Util.openInputStream(url, Collections.emptyMap(), null));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
path = StringUtils.isEmpty(path) ? "/" : path;
XPathExpression xPathExpression = xPath.compile(path);
NodeList nodeList = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
final Deque<Map<String, Object>> stack = new LinkedList<>();
handleNode(stack, nodeList.item(i), simpleMode);
for (int index = 0; index < stack.size(); index++) {
result.add(new MapResult(stack.pollFirst()));
}
}
} catch (FileNotFoundException e) {
if (!failOnError)
return Stream.of(new MapResult(Collections.emptyMap()));
else
throw new FileNotFoundException(e.getMessage());
} catch (Exception e) {
if (!failOnError)
return Stream.of(new MapResult(Collections.emptyMap()));
else
throw new Exception(e);
}
return result.stream();
}
use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.
the class LoadJson method loadJsonStream.
public static Stream<MapResult> loadJsonStream(@Name("url") String url, @Name("headers") Map<String, Object> headers, @Name("payload") String payload, String path, boolean failOnError) {
headers = null != headers ? headers : new HashMap<>();
headers.putAll(extractCredentialsIfNeeded(url, failOnError));
Stream<Object> stream = JsonUtil.loadJson(url, headers, payload, path, failOnError);
return stream.flatMap((value) -> {
if (value instanceof Map) {
return Stream.of(new MapResult((Map) value));
}
if (value instanceof List) {
if (((List) value).isEmpty())
return Stream.empty();
if (((List) value).get(0) instanceof Map)
return ((List) value).stream().map((v) -> new MapResult((Map) v));
return Stream.of(new MapResult(Collections.singletonMap("result", value)));
}
if (!failOnError)
throw new RuntimeException("Incompatible Type " + (value == null ? "null" : value.getClass()));
else
return Stream.of(new MapResult(Collections.emptyMap()));
});
}
use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.
the class Xml method xmlToMapResult.
private Stream<MapResult> xmlToMapResult(@Name("url") String url, boolean simpleMode) {
try {
XMLStreamReader reader = getXMLStreamReaderFromUrl(url);
final Deque<Map<String, Object>> stack = new LinkedList<>();
do {
handleXmlEvent(stack, reader, simpleMode);
} while (proceedReader(reader));
return Stream.of(new MapResult(stack.getFirst()));
} catch (IOException | XMLStreamException e) {
throw new RuntimeException("Can't read url " + cleanUrl(url) + " as XML", e);
}
}
use of apoc.result.MapResult in project neo4j-apoc-procedures by neo4j-contrib.
the class Meta method schema.
@Procedure
@Description("apoc.meta.schema - examines a subset of the graph to provide a map-like meta information")
public Stream<MapResult> schema() {
MetaStats metaStats = collectStats();
Map<String, Map<String, MetaResult>> metaData = collectMetaData();
Map<String, Object> relationships = collectRelationshipsMetaData(metaStats, metaData);
Map<String, Object> nodes = collectNodesMetaData(metaStats, metaData, relationships);
nodes.putAll(relationships);
return Stream.of(new MapResult(nodes));
}
Aggregations