Create a Gremlin AddEdge () request that will not duplicate Titan

Is there a way to create a unique edge between two vertices on the Titan chart and confirm that it cannot be created again if it is not deleted and not created?

Basically, I need to create:

vertex1--follows-->vertex2 

But I keep creating multiple edges for the same relationship:

 vertex1--follows-->vertex2 vertex1--follows-->vertex2 vertex1--follows-->vertex2 vertex1--follows-->vertex2 

My basic addEdge request is this:

 def follow(target) grem = "g.addEdge( gV('id', '#{id}').next(), gV('id', '#{target.id}').next(), 'follows', [since:#{Time.now.year}] )" $graph.execute(grem).results end 

I'm trying to find something like this

 def follow(target) grem = "g.addEdge( gV('id', '#{id}').next(), gV('id', '#{target.id}').next(), 'follows', [since:#{Time.now.year}] ).unique(Direction.OUT)" $graph.execute(grem).results end 

This document has a method called unique, but I cannot get it to work with edges, only the properties of the vertices.

https://github.com/thinkaurelius/titan/wiki/Type-Definition-Overview

I can run the query before creating addEdge to check the existing edge, but this seems to be hacked and can cause problems with the race condition.

Is it possible that there is a method that I can add to addEdge that will prevent the creation of a duplicated edge if the edge already exists?

Or is there a way to create a unique property label on an edge?

The following is a Gremlins session:

 gremlin> g.makeType().name('follows').unique(IN).makeEdgeLabel(); ==>v[36028797018964558] gremlin> u = g.addVertex([name:'brett']) ==>v[120004] gremlin> u2 = g.addVertex([name:'brettU']) ==>v[120008] gremlin> e = g.addEdge(u, u2, 'follows') ==>e[2w5N-vdy-2F0LaTPQK2][120004-follows->120008] gremlin> e = g.addEdge(u, u2, 'follows') An edge with the given type already exists on the in-vertex Display stack trace? [yN] gremlin> e = g.addEdge(u2, u, 'follows') ==>e[2w5P-vdC-2F0LaTPQK2][120008-follows->120004] gremlin> u3 = g.addVertex([name:'brett3']) ==>v[120012] gremlin> e = g.addEdge(u3, u, 'follows') An edge with the given type already exists on the in-vertex Display stack trace? [yN] N gremlin> gE ==>e[2w5N-vdy-2F0LaTPQK2][120004-follows->120008] ==>e[2w5P-vdC-2F0LaTPQK2][120008-follows->120004] 

Setting unique (IN | BOTH | OUT) creates a problem when we can only have one follower per user. This, of course, would make it impossible for the user to communicate → follows → [users].

Here is another example of trying to set a unique property on an edge, this also does not work:

 gremlin> g.makeType().name('follows_id').unique(BOTH).makeEdgeLabel(); ==>v[36028797018964942] gremlin> u = g.addVertex([name:'brett']) ==>v[200004] gremlin> u2 = g.addVertex([name:'brett2']) ==>v[200008] gremlin> u3 = g.addVertex([name:'brett3']) ==>v[200012] gremlin> e = g.addEdge(u, u2, 'follows', [follows_id:'200004-20008']) Value must be a vertex Display stack trace? [yN] N gremlin> gE ==>e[4c9z-Q1S-2F0LaTPQQu][200004-follows->200008] gremlin> e = g.addEdge(u, u2, 'follows', [follows_id:'200004-20008']) Value must be a vertex Display stack trace? [yN] N gremlin> gE ==>e[4c9z-Q1S-2F0LaTPQQu][200004-follows->200008] ==>e[4c9B-Q1S-2F0LaTPQQu][200004-follows->200008] 
+4
source share
2 answers

To close the loop, this question was answered on the Aurelius Graphs mailing list . Basically:

we really do not see a precedent for uniqueness restrictions to pairs of vertices (a la - only one edge can exist between vertex A and B) for these reasons:

  • In most cases, you can get rid of duplication quite cheaply on the request side with dedup (): v.out ('follow'). dedup () .....
  • the likelihood of conflict is much lower (due to N ^ 2 vertex combinations), which makes waaaay locks simply costly compared to the probability of conflict.

In short, you should check for the existence of an edge in your application, since Titan cannot be applied.

+6
source

This prevents duplication of application code compared to the database configuration and solves the problem that we have.

  grem = " if(gV('uid', '#{id}').out('follows').has('id', gV('uid', '#{target.id}').next().id).hasNext() == true){ println 'already connected' } else{ g.addEdge( gV('uid', '#{id}').next(), gV('uid', '#{target.id}').next(), 'follows', [since:(new java.util.Date()).getTime()] ) }" $graph.execute(grem).results 
0
source

Source: https://habr.com/ru/post/1501955/


All Articles