4 ms·
I'm working on a project that requires a tree datastructure (basically a graph but with only single-direction parent-child relationships) and the number of node
by techtalsky 13y ago
I'm working on a project that requires a tree datastructure (basically a graph but with only single-direction parent-child relationships) and the number of nodes will stay under a thousand. I could have chosen a graph database, but for my level of complexity I just used postgres and a table that has foriegn-key relationships to itself.
Then I made a rails front end using the acts-as-sane-tree gem, which is designed to use this postgres data model and recursive queries: https://github.com/chrisroberts/acts_as_sane_tree https://github.com/chrisroberts/acts_as_sane_tree
- olefoo 13y agoHave you looked at the ltree extension for postgres? http://www.postgresql.org/docs/9.3/static/ltree.html http://www.postgresql.org/docs/9.3/static/ltree.html It's quite fast.
- techtalsky 13y agoNo, looks cool though. That would impact deployability to heroku though, yes?
- olefoo 13y agoIt's on their list of approved extensions https://postgres.heroku.com/blog/past/2012/8/2/announcing_support_for_17_new_postgres_extensions_including_dblink/ https://postgres.heroku.com/blog/past/2012/8/2/announcing_su... You should be able to say: CREATE EXTENSION ltree; In the database you want it in.
- espeed 13y agoGremlin has custom Tree step: https://github.com/tinkerpop/gremlin/wiki/Tree-Pattern https://github.com/tinkerpop/gremlin/wiki/Tree-Pattern You can use Gremlin with any TinkerPop/Blueprints (https://github.com/tinkerpop/blueprints/wiki https://github.com/tinkerpop/blueprints/wiki) enabled graph database (which means almost all graph DBs).
- vram22 13y ago>I could have chosen a graph database, but for my level of complexity I just used postgres and a table that has foriegn-key relationships to itself. Good point. Relational databases have been used for BOM (Bill-Of-Material) modelling (a manufacturing application) for ages. DB records representing a manufactured product or component can have fields that point to other record(s) in the same table, which can be child components of the product. E.g. airplane -> engine, wings. Engine -> engine parts. Wings -> wing parts. Etc. And this can be recursive. Another such example is when you want to model an employee entity, where a manager (who has employees - or reports) is also an employee.