Search code examples
graphgraph-databasesgremlintitan

How to increment Number of Visit count in Titan graph database Edge Label?


I'm having vertices as User1 and User2. When User1 visits User2 profile, am adding one edge ( i.e visited ) with count variable.

How can i increment the count variable when User1 again visits User2 profile.


Solution

  • When using Titan, and depending on the use case, mutating an edge is not recommended as it internally leads to a deletion and re-creation of that edge (notice that the edge id will change). If using Cassandra backend, this can lead to the creation of tombstones that you will have to handle yourself. Avoid mutating edges if you can. Actually, edges should be seen as "facts" that don't change (hence the immutability): a "visit fact" happened, so a "visited edge" was created. Facts don't change, but they can be invalidated by new facts (new edges).

    If you expect the users to have few visits, mutating an edge and incrementing a count property should be fine, though you kind of lose some of the benefits of using a graph database (I'd say this is kind of an antipattern). If you expect a lot of visits, you may want to add a new edge per visit (I'd go for that route) and that would be a perfect use case for a graph database. For higher volumes and for quicker retrieval of the total number of visits, you may also want to keep track of the visit count (edge count) by storing a counter property on the visited vertex.