Skip to content

Instantly share code, notes, and snippets.

@zeryx
Last active November 22, 2015 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeryx/291f2b453dfc664603be to your computer and use it in GitHub Desktop.
Save zeryx/291f2b453dfc664603be to your computer and use it in GitHub Desktop.
titan does create an index, but while using the index with the query in checkAccountID.scala, it fails to find matching verticies.
def doesAccountIDExist(id: Int): Boolean = {
///this is the line that fails
val tmp = g._graph.V.hasLabel("account").has(accountIDKey -> id).toList()
g._graph.tx().commit()
if (tmp.nonEmpty) Console.println("Success, index works.")
}
def createAccountIndicies(): Unit = {
//define all account specific property keys/labels
val accountID = g._mgmt.makePropertyKey("ACCOUNT_ID").dataType(classOf[Integer]).make()
val region = g._mgmt.makePropertyKey("ACCOUNT_REGION").dataType(classOf[Integer]).make()
val username = g._mgmt.makePropertyKey("ACCOUNT_USERNAME").dataType(classOf[String]).make()
g._mgmt.makePropertyKey("ACCOUNT_TIMESTAMP_CREATED").dataType(classOf[java.lang.Long]).make()
val accountLabel = g._mgmt.makeVertexLabel("account").partition().make()
//accountToProfileLabel is M2O in that accounts can have many profile edges, but not vice versa
val AtoP_edgeLabel = g._mgmt.makeEdgeLabel(accountToProfileLabel).multiplicity(Multiplicity.MANY2ONE).make()
//create index by account_ID
val accountIDComposite =g._mgmt.buildIndex("accountIDComposite", classOf[Vertex])
.indexOnly(accountLabel)
.addKey(accountID)
.unique()
.buildCompositeIndex()
g.commitMgmt()
var accepted = false
//wait until the backend sees the new index
while (!accepted) {
Thread.sleep(500L)
accepted = g._mgmt.containsGraphIndex("accountIDComposite")
}
accepted = false
//wait until the backend registers the new index
while(!accepted){
Thread.sleep(500L)
val idx = g._mgmt.getGraphIndex("accountIDComposite")
for(key <- idx.getFieldKeys)
if(idx.getIndexStatus(key) == SchemaStatus.ENABLED
|| idx.getIndexStatus(key) == SchemaStatus.REGISTERED) accepted = true
}
g.commitMgmt()
}
object g extends graphDbConnection{
import com.thinkaurelius.titan.core.{TitanFactory, TitanGraph}
import gremlin.scala._
var _graph: ScalaGraph[TitanGraph] = null
var _mgmt: TitanManagement = null
def connect(): Unit = {
val titanGraph = TitanFactory.open("titan-cassandra.properties")
_mgmt = titanGraph.openManagement()
_graph = GraphAsScala(titanGraph).asScala
}
def commitMgmt(): Unit = {//each _mgmt closes after it's commited, this might be the problem spot, not sure.
_mgmt.commit()
_mgmt = _graph.graph.openManagement()
}
def disconnect(): Unit = {
_graph.close()
}
}
def newAccount(accountID: Int,
username: String,
accountRegion: Int = 0): Boolean = {
if (accountID >= 0
&& !this.doesAccountIDExist(accountID)
&& username.nonEmpty) {
val timestamp :Long = DateTime.now(DateTimeZone.UTC).toString("yyyyMMddHHmmss").toLong
val newAccount =g._graph +("account",
accountIDKey -> accountID,
accountUsernameKey -> username,
accountRegionKey -> accountRegion,
accountTimeCreatedKey -> timestamp)
g._graph.tx().commit()
true
}
else {
false
}
}
g.connect()
createAccountIndicies()
newAccount(..., id, ...)
checkAccountID(id)
g.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment