Skip to content

Instantly share code, notes, and snippets.

@ducky427
Created August 12, 2014 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducky427/9125b39d797738865c21 to your computer and use it in GitHub Desktop.
Save ducky427/9125b39d797738865c21 to your computer and use it in GitHub Desktop.
Length of linked list
try(Transaction tx = db.beginTx()) {
Node startNode = db.getNodeById( nodeId );
int maxLength = 0;
boolean to_continue = true;
while (to_continue) {
maxLength++;
Iterator<Relationship> rels = startNode.getRelationships(Direction.OUTGOING, NEXT).iterator();
if (rels.hasNext()) {
startNode = rels.next().getEndNode();
to_continue = true;
} else {
to_continue = false;
}
}
System.out.printf( "%15s Node: %4d, Length: %3d, Time (ms): %3d%n", "(Traversal API)", nodeId, maxLength, System.currentTimeMillis() - start );
tx.failure();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment