forked from mpollmeier/gremlin-scala-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexSpec.scala
52 lines (46 loc) · 1.77 KB
/
IndexSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gremlin.scala._
import org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph
import org.apache.tinkerpop.gremlin.process.traversal.Path
import org.neo4j.graphdb.DynamicLabel
import org.neo4j.tinkerpop.api.impl.Neo4jGraphAPIImpl
import scala.util.Random
import collection.JavaConversions._
import org.scalatest._
// http://tinkerpop.apache.org/docs/current/reference/#neo4j-gremlin
class IndexSpec extends FlatSpec with Matchers {
"Gremlin-Scala" should "create some vertices with indexed properties" in {
val dbPath = "target/indexspec"
FileUtils.removeAll(dbPath)
val graph = Neo4jGraph.open(dbPath)
val scalaGraph = graph.asScala
val db = graph.getBaseGraph.asInstanceOf[Neo4jGraphAPIImpl].getGraphDatabase
graph.tx.open
val label = DynamicLabel.label("Person")
db.schema.indexFor(label).on("name").create
graph.tx.commit
graph.tx.close
val vertexCount = 10000
graph.tx.open
(1 to vertexCount) foreach { i ⇒
scalaGraph.addVertex(label = "Person", Map("name" -> i.toString, "nonIndexedString" -> i.toString))
}
graph.tx.commit
graph.tx.close
def timeLookups(propertyName: String): Long = {
val t0 = System.currentTimeMillis
(1 to 100).foreach { _ ⇒
val i = Random.nextInt(vertexCount)
val v = scalaGraph.V.hasLabel("Person").has(Key[String](propertyName), i.toString).headOption
assert(v.isDefined)
}
val t1 = System.currentTimeMillis
t1 - t0
}
val timeNonIndexed = timeLookups("nonIndexedString")
val timeIndexed = timeLookups("name")
println(s"time for lookups of non-indexed vertices: ${timeNonIndexed}ms")
println(s"time for lookups of indexed vertices: ${timeIndexed}ms")
timeIndexed should be < timeNonIndexed
graph.close
}
}