Created
August 4, 2011 09:19
-
-
Save jexp/1124816 to your computer and use it in GitHub Desktop.
How to store a directory tree in the Neo4j Graph Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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