Hi,
I would like to get all the nodes at most N distance from a specific node. I use DFS with a bound for the depth. I don't want to visit a node multiple times so I use a Set<Long> for storing the already visited nodes' ids. My question is when I have the edges (Relationship objects) of a node is it possible to get the corresponding nodes' id without referencing the Node object (I think this is when Neo4j loads the Node from the physical drive when it's not cached, isn't it)? I assume the Relationship object stores the corresponding 2 Nodes' ids? Can it make any difference or not because the already visited nodes are probably cached (1M total nodes, neostore.nodestore.db.mapped_memory=1000M)? I use Neo4j community 1.8 on Linux.
GraphDatabaseService graphDb = new EmbeddedGraphDatabase(dbPath, config);
Set<String> cities = new HashSet<String>();
Set<Long> visitedIds = new HashSet<Long>();
Transaction tx = graphDb.beginTx();
try
{
Node startNode = graphDb.getNodeById(startNodeId);
getDepth(startNode, 1, 6, cities, visitedIds);
tx.success();
}
finally
{
tx.finish();
}
private static void getDepth(Node node, int depth, int maxDepth, Set<String> cities, Set<Long> visitedIds) {
if(!visitedIds.contains(node.getId())) {
visitedIds.add(node.getId());
cities.add(String.valueOf(node.getProperty("City")));
if(depth<=maxDepth) {
for(Relationship rel : node.getRelationships()) {
//instead of this something like visitedIds.contains(rel.getEndNodeId())
Node target = rel.getEndNode();
if(target.getId() != node.getId()) {
getDepth(target, depth+1, maxDepth, cities, visitedIds);
}
}
}
}
}
Thanks,
Greg