-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use traversal to add vertex, mostly to fix remote graph setups
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
1 parent
b7fad2f
commit f740788
Showing
2 changed files
with
26 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,21 +17,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 | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
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 = | ||
|
@@ -43,10 +44,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 | ||
This comment has been minimized.
Sorry, something went wrong.
rwilcox
|
||
*/ | ||
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: _*) | ||
|
@@ -59,29 +63,31 @@ case class ScalaGraph(traversalSource: TraversalSource) { | |
def +(label: String, properties: KeyValue[_]*): Vertex = | ||
addVertex(label, properties.map(v => (v.key.name, v.value)).toMap) | ||
|
||
def inject[S](starts: S*): GremlinScala.Aux[S, HNil] = | ||
GremlinScala[S, HNil](traversalSource.underlying.inject(starts: _*)) | ||
|
||
/** start a traversal with `addV` */ | ||
def addV(): GremlinScala.Aux[Vertex, HNil] = | ||
GremlinScala[Vertex, HNil](traversalSource.underlying.addV()) | ||
|
||
/** start a traversal with `addV` */ | ||
def addV(label: String): GremlinScala.Aux[Vertex, HNil] = | ||
GremlinScala[Vertex, HNil](traversalSource.underlying.addV(label)) | ||
|
||
def inject[S](starts: S*): GremlinScala.Aux[S, HNil] = | ||
GremlinScala[S, HNil](traversalSource.underlying.inject(starts: _*)) | ||
|
||
// start traversal with all vertices | ||
/** start traversal with all vertices */ | ||
def V(): GremlinScala.Aux[Vertex, HNil] = | ||
GremlinScala[Vertex, HNil](traversalSource.underlying.V()) | ||
|
||
// start traversal with all edges | ||
/** start traversal with all edges */ | ||
def E(): GremlinScala.Aux[Edge, HNil] = | ||
GremlinScala[Edge, HNil](traversalSource.underlying.E()) | ||
|
||
// start traversal with some vertices identified by given ids | ||
/** start traversal with some vertices identified by given ids */ | ||
def V(vertexIds: Any*): GremlinScala.Aux[Vertex, HNil] = | ||
GremlinScala[Vertex, HNil]( | ||
traversalSource.underlying.V(vertexIds.asInstanceOf[Seq[AnyRef]]: _*)) | ||
|
||
// start traversal with some edges identified by given ids | ||
/** start traversal with some edges identified by given ids */ | ||
def E(edgeIds: Any*): GremlinScala.Aux[Edge, HNil] = | ||
GremlinScala[Edge, HNil](traversalSource.underlying.E(edgeIds.asInstanceOf[Seq[AnyRef]]: _*)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Woh, neat, auto calling
next
for me! (I think this is a good thing)