Search code examples
titangremlintinkerpoprexster

Error while Trying to use indexes in titan graph db


Using the following commands to use indexes for better performance to query a node in titan db .

TitanManagement mgmt = graph.openManagement();
PropertyKey buyer = mgmt.makePropertyKey("buyer").dataType(String.class).cardinality(Cardinality.SINGLE).make();
TitanGraphIndex buyeri = mgmt.buildIndex("buyer", Vertex.class).addKey(buyer).buildCompositeIndex();
mgmt.setConsistency(buyeri, ConsistencyModifier.LOCK);
g.V().has("buyer","buyer", "buyer10").out("order_is").values("order").fill(list);     

Using titan 1.0.0 , gremlin query language, while running this query it throws an error:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:745)Caused by: com.thinkaurelius.titan.core.SchemaViolationException: Adding this property for key [~T$SchemaName] and value [rtbuyer] violates a uniqueness constraint [SystemIndex#~T$SchemaName]
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.addProperty(StandardTitanTx.java:780)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.addProperty(StandardTitanTx.java:706)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.makeSchemaVertex(StandardTitanTx.java:836)
at com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx.makePropertyKey(StandardTitanTx.java:856)
at com.thinkaurelius.titan.graphdb.types.StandardPropertyKeyMaker.make(StandardPropertyKeyMaker.java:86)
at pluradj.titan.tinkerpop3.example.JavaExample2.main(JavaExample2.java:56)

Updates as mentioned below in @jason Plurad answer

I have used

 PropertyKey buyer = (!mgmt.containsPropertyKey("buyer")) ?
        mgmt.makePropertyKey("buyer").dataType(String.class).cardinality(Cardinality.SINGLE).make() :
        mgmt.getPropertyKey("buyer");
    TitanGraphIndex buyeri = mgmt.getGraphIndex("buyeri");
    if (buyeri == null) {
        VertexLabel buyr = mgmt.getVertexLabel("buyer");
        buyeri = mgmt.buildIndex("buyeri", Vertex.class).addKey(buyer).indexOnly(buyr). buildCompositeIndex();
        mgmt.setConsistency(buyeri, ConsistencyModifier.LOCK);
    }

    mgmt.commit();
     st=System.currentTimeMillis();
     g.V().has("buyer","buyer", "buyer10").out("order_is").values("order").fill(list);     
     System.out.println(System.currentTimeMillis()-st +"millllli");

Using the following code to index buyer , to make searching vertex faster , but dont know why it is not indexing is not working , Can anyone fix this please.

i have read the documentation of titan db indexing part but i guess it is not working ..


Solution

  • The problem here is indicated by this in the stack trace:

    Caused by: com.thinkaurelius.titan.core.SchemaViolationException: Adding this property for key [~T$SchemaName] and value [rtbuyer] violates a uniqueness constraint [SystemIndex#~T$SchemaName]
    

    Your code is attempting to create the schema multiple times (perhaps by repeated invocations of your program). Your schema creation code above does not check whether the property key exists before attempting to create it.

    Your usage of has("buyer", "buyer", "buyer10") is looking for vertices that have a vertex label "buyer", and a vertex property "buyer" equal to "buyer10". Your schema definition definition didn't have a vertex label, so I think has("buyer", "buyer10") is more appropriate.

    Check out this example that you can run from gremlin.sh. It uses the index and does not show any warning messages.

    gremlin> graph = TitanFactory.build().set('storage.backend','inmemory').open()
    ==>standardtitangraph[inmemory:[127.0.0.1]]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[standardtitangraph[inmemory:[127.0.0.1]], standard]
    gremlin> mgmt = graph.openManagement()
    ==>com.thinkaurelius.titan.graphdb.database.management.ManagementSystem@7026b7ee
    gremlin> buyer = (!mgmt.containsPropertyKey("buyer")) ?
    gremlin>     mgmt.makePropertyKey("buyer").dataType(String.class).cardinality(Cardinality.SINGLE).make() :
    gremlin>     mgmt.getPropertyKey("buyer");
    ==>buyer
    gremlin> buyeri = mgmt.getGraphIndex("buyeri");
    ==>null
    gremlin> if (buyeri == null) {
    gremlin>     buyeri = mgmt.buildIndex("buyeri", Vertex.class).addKey(buyer).buildCompositeIndex();
    gremlin>     mgmt.setConsistency(buyeri, ConsistencyModifier.LOCK);
    gremlin> }
    ==>null
    gremlin> mgmt.commit();
    ==>null
    gremlin> v = graph.addVertex("buyer", "buyer10", "name", "ten")
    ==>v[4184]
    gremlin> g.V().has("buyer", "buyer10").values("name")
    ==>ten