Reputation: 1221
I have a tree structure in my appplication that needs to be persisted to a file locally. I'm looking for some input as to what would be a good way of implementing this.
The tree is basically a file tree, with FileNode and FolderNode objects extending a Node object. All Nodes have a reference to their parent, and FolderNodes also have a Set of children. The root is a FolderNode with parent set to null.
The tree could have anything between 2 and 100 000s of nodes. Width and depth can be whatever is sensible for a file tree (usually more wide than deep).
Application work flow:
As you can tell, the file is only read once, but written after EVERY change, so performance is clearly an issue here.
What would be a good solution to this? It feels like JPA with an embedded database like SQLite, Derby or ObjectDB would be the best choice. Is it? And in that case, how should I model the database?
Upvotes: 2
Views: 547
Reputation: 1539
The proper answer is based on what you want to do with the file afterwards. As it is explained here, the best way is to save the thing to /dev/null
(NOT save it), because you aren't using it afterwards.
First try and see whether it is really necessary to save it separately. A java.io.File only uses 100 bytes, so it may be worth the effort to just keep the whole thing in memory and only write it out once.
Upvotes: 0
Reputation: 10241
Actually I should put this in comment bcos this wont be a proper answer to your question, but sorry for putting it in answer.
The tree is basically a file tree, with FileNode and FolderNode objects extending a Node object. All Nodes have a reference to their parent, and FolderNodes also have a Set of children. The root is a FolderNode with parent set to null.
I would suggest you rather than re-inventing the wheel and implementing this yourself. take a look at JCR(Java Content Repository), Apache Jackrabbit is an implementation of JCR, it has the advantages of both a file system and a hierarchy structure, and also has other features like versioning.
Also, it has an inbuilt persistence Manager, which will take care of persisting your tree.
Upvotes: 2