Skip to content

Instantly share code, notes, and snippets.

@jexp
Created August 4, 2011 09:19
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 jexp/1124816 to your computer and use it in GitHub Desktop.
Save jexp/1124816 to your computer and use it in GitHub Desktop.
How to store a directory tree in the Neo4j Graph Database
private static final RelationshipType SUB_DIR = DynamicRelationshipType.withName("SUB_DIR");
public void storePath(String path) {
Node dir = graphDb.getReferenceNode();
for (String name : path.split(File.separator)) {
dir = obtainSubDir(dir, name);
}
}
private Node obtainSubDir(Node dir, String name) {
Node subDir = getSubDir(dir,name);
if (subDir!=null) return subDir;
return createSubDir(dir, name);
}
private Node getSubDir(Node dir, String name) {
for (Relationship rel : dir.getRelationships(SUB_DIR, Direction.OUTGOING)) {
final Node subDir = rel.getEndNode();
if (subDir.getProperty("name", "").equals(name)) return subDir;
}
return null;
}
private Node createSubDir(Node dir, String name) {
Node subDir = dir.getGraphDatabase().createNode();
subDir.setProperty("name", name);
dir.createRelationshipTo(subDir, SUB_DIR);
return subDir;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment