palash kulshreshtha
palash kulshreshtha

Reputation: 587

how to ensure there single edge in a graph for a given order_id?

My current scenario is like I have I have products,customer and seller nodes in my graph ecosystem. The problem I am facing is that I have to ensure uniqueness of

(customer)-[buys]->product

with order_item_id as property of the buys relation edge.I have to ensure that there is an unique edge with buys property for a given order_item_id. In this way I want to ensure that my graph insertion remains idempotent and no repeated buys edges are created for a given order_item_id.

creating a order_item_id property

if(!mgmt.getPropertyKey("order_item_id")){
    order_item_id=mgmt.makePropertyKey("order_item_id").dataType(Integer.class).make();
}else{
    order_item_id=mgmt.getPropertyKey("order_item_id");
} 

What I have found so far is that building unique index might solve my problem. like

if(mgmt.getGraphIndex('order_item_id')){
    ridIndexBuilder=mgmt.getGraphIndex('order_item_id')
}else{
    ridIndexBuilder=mgmt.buildIndex("order_item_id",Edge.class).addKey(order_item_id).unique().buildCompositeIndex();
}

Or I can also use something like

mgmt.buildEdgeIndex(graph.getOrCreateEdgeLabel("product"),"uniqueOrderItemId",Direction.BOTH,order_item_id)

Upvotes: 2

Views: 297

Answers (1)

jbmusso
jbmusso

Reputation: 3456

You cannot enforce the uniqueness of properties at the edge-level, ie. between two vertices (see this question on the mailing list). If I understand your problem correctly, building a CompositeIndex on edge with a uniqueness constraint for a given property should address your problem, even though you do not plan to search these edges by the indexed property. However, this could lead to performance issues when inserting many edges simultaneously due to locking. Depending on the rate at which you insert data, you may have to skip the locking (= skip the uniqueness constraint) and risk duplicate edges, then handle the deduplication yourself at read time and/or run post-import batch jobs to clean up potential duplicates.

  • buildIndex() builds a global, graph-index (either CompositeIndex or MixedIndex). These kind of indexes typically allows you to quickly find starting points of your Traversal within a graph.
  • However, buildEdgeIndex() allows you to build a local, "vertex-centric index" which is meant to speed-up the traversal between vertices with a potentially high degree (= huge number of incident edges, incoming and/or outgoing). Such vertices are called "super nodes" (see A solution to the supernode problem blog post from Aurelius) - and although they tend to be quite rare, the likelihood of traversing them isn't that low.

Reference: Titan documentation, Ch. 8 - Indexing for better Performance.

Upvotes: 3

Related Questions