Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement revisions with neo4j?

I have an object and I need to keep a history of all changes made to it. How would I implement this using neo4j?

like image 697
Aaron Digulla Avatar asked Oct 03 '12 11:10

Aaron Digulla


People also ask

How do you create a relationship between existing nodes in Neo4j?

In Neo4j to create relationship between nodes you have to use the CREATE statement like we used to create nodes. lets create relation between two already created nodes.

What is Neo4j Browser sync?

Neo4j Browser Sync is a feature that allows you to synchronize your favorites, styling, and settings to cloud storage, so you can share it across machines and browsers.


2 Answers

As with a RDBMS, it would depend on your domain and data query requirements.

Does your application require regular access to all versions of the object or usually just to the most recent, with the older versions available via the current one? An example of this could be pages on Wikipedia. As as example, let's say we have a page which is on version 3. We could then model this as follows:

(pages)-[:PAGE]->(V3)-[:PREV]->(V2)-[:PREV]->(V1)
   ^               ^
   |               |
category        current
  node      version of page

Here, only the current version can be seen to form part of the main structure but you may wish to allow all versions to form part of that structure. In this case, you could use relationship properties to indicate the version and have all page versions link from the category node:

  (V1)
    ^
    |
[:PAGE(v=1)]
    |
 (pages)-[:PAGE(v=2)]->(V2)
    |
[:PAGE(v=3)]
    |
    v
  (V3)

Here, you can immediately traverse to a particular version of the page by simply specifying the version in which you are interested.

A third option could be that you wish all older versions to be completely separate from the main structure. For this you could use multiple category nodes, one for (current_pages) and another for (old_pages). As each page is superseded by a new version, it becomes unlinked from the former category and instead linked to the latter. This would form more of an "archive" type of system where the older versions could even be moved into a separate database instance.

So you have these three options, plus more that I haven't thought of! Neo4j allows you great flexibility with this sort of design and there's absolutely no "right" answer. If none of these inspire you however, post a little more information about your domain so that the answer can be more tailored for your needs.

Cheers, Nige

like image 174
Nigel Small Avatar answered Nov 02 '22 06:11

Nigel Small


You could also approach it from the other side:

(pages)-[:VERSION]->(V1)-[:VERSION]->(V2)-[:VERSION]->(V3)
   ^                                                   ^
   |                                                   |
category                                            current
  node                                          version of page

advantage : when you create a new version, you just add it at the end of the chain, no need to "insert" it between the (page) and the current version.

disadvantage :you can't just throw away old versions, unless you reconstruct the chain. But this is probably not a frequent operation.

like image 39
Graphileon Avatar answered Nov 02 '22 04:11

Graphileon