Skip to content

Commit

Permalink
use traversal to add vertex, mostly to fix remote graph setups
Browse files Browse the repository at this point in the history
when using remote graphs, Graph is actually just an empty shell and
can't be used to e.g. add elements.

open problems:
* marshalled classes can use the @id annotation to specify
the ID of a vertex, but we cannot set the id of a vertex inside a traversal
* same needs to be applied for edges

re #223
  • Loading branch information
mpollmeier committed Mar 24, 2018
1 parent d04d82a commit 962b5ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
39 changes: 23 additions & 16 deletions gremlin-scala/src/main/scala/gremlin/scala/ScalaGraph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ case class ScalaGraph(traversalSource: TraversalSource) {
def configure(conf: TraversalSource => TraversalSource) =
ScalaGraph(conf(TraversalSource(graph)))

def addVertex(label: String): Vertex = graph.addVertex(label)
def addVertex(): Vertex =
traversalSource.underlying.addV().next

def addVertex(): Vertex = graph.addVertex()
def addVertex(label: String): Vertex =
traversalSource.underlying.addV(label).next

def addVertex(label: String, properties: (String, Any)*): Vertex = {
val labelParam = Seq(T.label, label)
val params =
properties.flatMap(pair => Seq(pair._1, pair._2.asInstanceOf[AnyRef]))
graph.addVertex(labelParam ++ params: _*)
def addVertex(properties: (String, Any)*): Vertex = {
val traversal = traversalSource.underlying.addV()
properties.foreach { case (key, value) => traversal.property(key, value) }
traversal.next
}

def addVertex(properties: (String, Any)*): Vertex = {
val params =
properties.flatMap(pair => Seq(pair._1, pair._2.asInstanceOf[AnyRef]))
graph.addVertex(params: _*)
def addVertex(label: String, properties: (String, Any)*): Vertex = {
val traversal = traversalSource.underlying.addV(label)
properties.foreach { case (key, value) => traversal.property(key, value) }
traversal.next
}

def addVertex(label: String, properties: Map[String, Any]): Vertex =
Expand All @@ -44,10 +45,13 @@ case class ScalaGraph(traversalSource: TraversalSource) {
/**
* Save an object's values into a new vertex
* @param cc The case class to persist as a vertex
*
* Note: this doesn't work with remote graphs, since remote graphs require you to use
* traversal steps to add a vertex (e.g. addV), but there's no step to set the ID
*/
def addVertex[CC <: Product: Marshallable](cc: CC): Vertex = {
val fromCC = implicitly[Marshallable[CC]].fromCC(cc)
val idParam = fromCC.id.toSeq.flatMap(List(T.id, _))
val idParam = fromCC.id.toSeq.flatMap(List(T.id, _)) //TODO: this will break things
val labelParam = Seq(T.label, fromCC.label)
val params = fromCC.valueMap.toSeq.flatMap(pair => Seq(pair._1, pair._2.asInstanceOf[AnyRef]))
graph.addVertex(idParam ++ labelParam ++ params: _*)
Expand All @@ -60,34 +64,37 @@ case class ScalaGraph(traversalSource: TraversalSource) {
def +(label: String, properties: KeyValue[_]*): Vertex =
addVertex(label, properties.map(v => (v.key.name, v.value)).toMap)

/** start a traversal with `addV` */
@deprecated("use `traversal.addV`", "3.3.1.2")
def addV(): GremlinScala.Aux[Vertex, HNil] =
traversalSource.addV()

/** start a traversal with `addV` */
@deprecated("use `traversal.addV`", "3.3.1.2")
def addV(label: String): GremlinScala.Aux[Vertex, HNil] =
traversalSource.addV(label)

@deprecated("use `traversal.inject`", "3.3.1.2")
/** start a traversal with given `starts`` */
def inject[S](starts: S*): GremlinScala.Aux[S, HNil] =
traversalSource.inject(starts: _*)

// start traversal with all vertices
/** start traversal with all vertices */
@deprecated("use `traversal.V`", "3.3.1.2")
def V(): GremlinScala.Aux[Vertex, HNil] =
traversalSource.V()

// start traversal with all edges
/** start traversal with all edges */
@deprecated("use `traversal.E`", "3.3.1.2")
def E(): GremlinScala.Aux[Edge, HNil] =
traversalSource.E()

// start traversal with some vertices identified by given ids
/** start traversal with some vertices identified by given ids */
@deprecated("use `traversal.V`", "3.3.1.2")
def V(vertexIds: Any*): GremlinScala.Aux[Vertex, HNil] =
traversalSource.V(vertexIds: _*)

// start traversal with some edges identified by given ids
/** start traversal with some edges identified by given ids */
@deprecated("use `traversal.E`", "3.3.1.2")
def E(edgeIds: Any*): GremlinScala.Aux[Edge, HNil] =
traversalSource.E(edgeIds: _*)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ElementSpec extends TestBase {
* TODO: `properties` should take `Key` as well
*/
it("sets a property with multiple values") {
val v = graph.addV().property(Name, "marko").property(Name, "marko a. rodriguez").head
val v = graph.addVertex((Name.name -> "marko"), (Name.name -> "marko a. rodriguez"))
graph.V(v).properties(Name.name).count.head shouldBe 2

v.property(Cardinality.list, Name.name, "m. a. rodriguez")
Expand Down

0 comments on commit 962b5ed

Please sign in to comment.