private static void getNodeHashMap(Long id, GraphDatabaseService gdb, Cache<Long, HashMap<String, Object>> cache) { Node thisNode = gdb.getNodeById(id); List<String> keys = new ArrayList<>(); HashMap<String, Object> nodeMap = new HashMap<>(); IteratorUtil.addToCollection(thisNode.getPropertyKeys(), keys) .stream() .forEach(n -> nodeMap.put(n, thisNode.getProperty(n))); nodeMap.put("id", id); cache.put(id, nodeMap); }
private <T> List<T> toList( ExecutionResult result, String column ) { List<T> results = new ArrayList<T>(); IteratorUtil.addToCollection( result.<T>columnAs( column ), results ); return results; }
public List<Node> friendsThatCanIntroduceMeTo(Node me, final Node target, Uniqueness uniqueness) { TraversalDescription description = Traversal.description() .relationships(KNOWS) .relationships(IS_FRIEND_OF) .evaluator(new Evaluator() { @Override public Evaluation evaluate(Path path) { Node currentNode = path.endNode(); if (currentNode.getId() == target.getId()) { return Evaluation.EXCLUDE_AND_PRUNE; } Path singlePath = GraphAlgoFactory.shortestPath(Traversal.expanderForTypes(KNOWS, Direction.BOTH, IS_FRIEND_OF, Direction.BOTH), 1).findSinglePath(currentNode, target); if (singlePath != null) { //direct link exists return Evaluation.INCLUDE_AND_CONTINUE; } else { return Evaluation.EXCLUDE_AND_CONTINUE; } } }) // .evaluator(Evaluators.atDepth(1)) .uniqueness(uniqueness); Iterable<Node> nodes = description.traverse(me).nodes(); ArrayList<Node> result = new ArrayList<Node>(); IteratorUtil.addToCollection(nodes, result); return result; }
public List<Path> pathThatICanUseToIntroduceMeTo(Node me, final Node target, Uniqueness uniqueness) { TraversalDescription description = Traversal.description() .relationships(KNOWS) .relationships(IS_FRIEND_OF) .evaluator(new Evaluator() { @Override public Evaluation evaluate(Path path) { Node currentNode = path.endNode(); if (currentNode.getId() == target.getId()) { return Evaluation.EXCLUDE_AND_PRUNE; } Path singlePath = GraphAlgoFactory.shortestPath(Traversal.expanderForTypes(KNOWS, Direction.BOTH, IS_FRIEND_OF, Direction.BOTH), 1).findSinglePath(currentNode, target); if (singlePath != null) { //direct link exists return Evaluation.INCLUDE_AND_CONTINUE; } else { return Evaluation.EXCLUDE_AND_CONTINUE; } } }) .uniqueness(uniqueness); ArrayList<Path> result = new ArrayList<Path>(); try (Transaction tx = graphDb.beginTx()) { IteratorUtil.addToCollection(description.traverse(me).iterator(), result); } return result; } }