Skip to content

Instantly share code, notes, and snippets.

@yhemanth
Created May 3, 2016 13:08
Show Gist options
  • Save yhemanth/6f2fd46c9f21c597176ef52b3a5fe91f to your computer and use it in GitHub Desktop.
Save yhemanth/6f2fd46c9f21c597176ef52b3a5fe91f to your computer and use it in GitHub Desktop.
A Gremlin Groovy script that illustrates a non-obvious behavior with Titan composite Indexes
g = TitanFactory.open("conf/titan-berkeleydb-es.properties")
m = g.getManagementSystem()
//constants
propertyName = "name"
statePropertyName = "state"
propertyValue = "value"
activeStateValue = "active"
deletedStateValue = "deleted"
// create indices
stateKey = m.makePropertyKey(statePropertyName).dataType(String.class).cardinality(Cardinality.SINGLE).make()
nameKey = m.makePropertyKey(propertyName).dataType(String.class).cardinality(Cardinality.SINGLE).make()
//m.buildIndex(propertyName, Vertex.class).addKey(nameKey).addKey(stateKey).buildCompositeIndex()
m.buildIndex(propertyName, Vertex.class).addKey(nameKey).buildCompositeIndex()
//m.buildIndex(statePropertyName, Vertex.class).addKey(stateKey).buildCompositeIndex()
m.commit()
// create a vertex
v = g.addVertex(null)
v.setProperty(propertyName, propertyValue)
v.setProperty(statePropertyName, activeStateValue)
g.commit()
// query to get back the vertex
try {
v1 = g.query().has(propertyName, propertyValue).has(statePropertyName, activeStateValue).vertices().iterator().next()
System.out.println("Got vertex for active state: " + v1 + " with state: " + v1.getProperty(statePropertyName))
v1.setProperty(statePropertyName, deletedStateValue)
g.commit()
} catch (Exception e) {
System.out.println("Did not get vertex for active state.")
e.printStackTrace()
g.rollback()
}
try {
v2 = g.query().has(propertyName, propertyValue).has(statePropertyName, activeStateValue).vertices().iterator().next()
System.out.println("Got vertex for active state after setting state to deleted: " + v2 + " with state: " + v2.getProperty(statePropertyName))
g.commit()
} catch (Exception e) {
System.out.println("Did not get vertex for active state after setting state to deleted.")
e.printStackTrace()
g.rollback()
}
try {
v3 = g.query().has(propertyName, propertyValue).has(statePropertyName, deletedStateValue).vertices().iterator().next()
System.out.println("Got vertex for deleted state: " + v3 + " with state: " + v3.getProperty(statePropertyName))
g.commit()
} catch (Exception e) {
System.out.println("Did not get vertex for deleted state")
e.printStackTrace()
g.rollback()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment