10
votes

Hierarchical data structures are often stored in relational databases. This kind of storage is flexible but flat, so the tree structure has to be build with each query. I want to store forum posts as a tree structure, but it should be possible to query efficiently, say for example selecting posts by date or author.

I'd like to have an open source database that is accessible from Java.

What is the best way to do so? CouchDB? neo4j? ...?

2
An ORM would make the handling more convenient, but the underlying data structure would still remain relational and probably the performance wouldn't be so good.deamon

2 Answers

7
votes

When I first encountered this problem, I've found the great article (link).

In tho words: in RDBMS world there are 2 main tree model storage approaches:

  • The Adjacency List Model
  • The Nested Set Model
4
votes

Here's a great article on Neo4j. In general, it looks like neo4j is your best option since document databases are still relatively flat and can result in some awkward setup (still doable though).

Neo4j, being a graph database, should be a solid fit for storing you tree. I've never used it, but given your problem domain, it seems like the best option (at least the one to investigate first).

As far as what the "best way" is, I think that depends on your implementation and requirements. I think you should write a simple test against a graph database, a document database, an object database and a relational database (or not) and see which one fits the problem that you're trying to solve.