Alexey Romanov
Alexey Romanov

Reputation: 170805

Store and query property history with Neo4j

I need to maintain a tree which contains nodes with attributes which are usually or strings, new attributes can be added to a node at runtime, etc. So a graph database like Neo4j is an obvious solution. But there is a slight twist: when an attribute is changed, I need to keep a record of its old value, and be able to efficiently query these old values (in particular, queries like "give me up 25 values of attribute X of node Y, starting from time Z" to support paging, and obviously "get latest value" needs to be efficient as well). What would be a good way to represent this?

Upvotes: 1

Views: 946

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170805

TimelineIndex almost gives what I need, except for limiting number of hits. But from JavaDoc of IndexHits it seems like it shouldn't matter too much if I iterate over as many as I need and then close the iterator.

So a solution looks like this: represent each property for which I need history as node instead; so instead of

Sensor
-----
name = "battery"
measurement = 1

I have

Sensor           attr
-----            -+--> name = "measurement"
name = "battery"  |    value = 1
                  |    timestamp = 100000000
                  |
                  +--> name = "measurement"
                  |    value = 2
                  |    timestamp = 100000001
                  |
                  +--> name = "measurement"
                       value = 1
                       timestamp = 100000002

And then I maintain a TimelineIndex for each Sensor node and each property-with-history.

Upvotes: 1

Related Questions