add java helpers on S2GraphLike.
Project: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/commit/97bd7e2c Tree: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/tree/97bd7e2c Diff: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/diff/97bd7e2c Branch: refs/heads/master Commit: 97bd7e2cc07e800ad31c3ba075808e399e52c62c Parents: 6c61087 Author: DO YUNG YOON <[email protected]> Authored: Sat Nov 18 09:56:53 2017 +0900 Committer: DO YUNG YOON <[email protected]> Committed: Sat Nov 18 10:34:19 2017 +0900 ---------------------------------------------------------------------- s2core/build.sbt | 3 +- .../s2graph/core/GraphElementBuilder.scala | 18 ++++++ .../org/apache/s2graph/core/GraphUtil.scala | 11 +++- .../org/apache/s2graph/core/S2GraphLike.scala | 66 ++++++++++++++------ 4 files changed, 77 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/97bd7e2c/s2core/build.sbt ---------------------------------------------------------------------- diff --git a/s2core/build.sbt b/s2core/build.sbt index 8033581..b4273ba 100644 --- a/s2core/build.sbt +++ b/s2core/build.sbt @@ -48,7 +48,8 @@ libraryDependencies ++= Seq( "org.specs2" %% "specs2-core" % specs2Version % "test", "org.apache.hadoop" % "hadoop-hdfs" % hadoopVersion , "org.apache.lucene" % "lucene-core" % "6.6.0", - "org.apache.lucene" % "lucene-queryparser" % "6.6.0" + "org.apache.lucene" % "lucene-queryparser" % "6.6.0", + "org.scala-lang.modules" %% "scala-java8-compat" % "0.8.0" ) libraryDependencies := { http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/97bd7e2c/s2core/src/main/scala/org/apache/s2graph/core/GraphElementBuilder.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/GraphElementBuilder.scala b/s2core/src/main/scala/org/apache/s2graph/core/GraphElementBuilder.scala index 08da355..aed84c7 100644 --- a/s2core/src/main/scala/org/apache/s2graph/core/GraphElementBuilder.scala +++ b/s2core/src/main/scala/org/apache/s2graph/core/GraphElementBuilder.scala @@ -2,9 +2,11 @@ package org.apache.s2graph.core import org.apache.s2graph.core.GraphExceptions.LabelNotExistException import org.apache.s2graph.core.JSONParser.{fromJsonToProperties, toInnerVal} +import org.apache.s2graph.core.S2Graph.{DefaultColumnName, DefaultServiceName} import org.apache.s2graph.core.mysqls._ import org.apache.s2graph.core.types._ import org.apache.s2graph.core.utils.logger +import org.apache.tinkerpop.gremlin.structure.T import play.api.libs.json.{JsObject, Json} import scala.util.Try @@ -241,6 +243,22 @@ class GraphElementBuilder(graph: S2GraphLike) { vertex } + def makeVertex(idValue: AnyRef, kvsMap: Map[String, AnyRef]): S2VertexLike = { + idValue match { + case vId: VertexId => + toVertex(vId.column.service.serviceName, vId.column.columnName, vId, kvsMap) + case _ => + val serviceColumnNames = kvsMap.getOrElse(T.label.toString, DefaultColumnName).toString + + val names = serviceColumnNames.split(S2Vertex.VertexLabelDelimiter) + val (serviceName, columnName) = + if (names.length == 1) (DefaultServiceName, names(0)) + else throw new RuntimeException("malformed data on vertex label.") + + toVertex(serviceName, columnName, idValue, kvsMap) + } + } + def toRequestEdge(queryRequest: QueryRequest, parentEdges: Seq[EdgeWithScore]): S2EdgeLike = { val srcVertex = queryRequest.vertex val queryParam = queryRequest.queryParam http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/97bd7e2c/s2core/src/main/scala/org/apache/s2graph/core/GraphUtil.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/GraphUtil.scala b/s2core/src/main/scala/org/apache/s2graph/core/GraphUtil.scala index 939b596..a4f6bde 100644 --- a/s2core/src/main/scala/org/apache/s2graph/core/GraphUtil.scala +++ b/s2core/src/main/scala/org/apache/s2graph/core/GraphUtil.scala @@ -21,6 +21,7 @@ package org.apache.s2graph.core import java.util.regex.Pattern +import org.apache.tinkerpop.gremlin.structure.Direction import play.api.libs.json.Json import scala.util.hashing.MurmurHash3 @@ -67,7 +68,15 @@ object GraphUtil { } } - def fromDirection(direction: Int) = { + def toDirection(direction: Direction): Int = { + direction.name() match { + case "out" => 0 + case "in" => 1 + case "both" => throw new IllegalArgumentException("only in/out direction is supported.") + } + } + + def fromDirection(direction: Int): String = { direction match { case 0 => "out" case 1 => "in" http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/97bd7e2c/s2core/src/main/scala/org/apache/s2graph/core/S2GraphLike.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/S2GraphLike.scala b/s2core/src/main/scala/org/apache/s2graph/core/S2GraphLike.scala index 1b80cb4..9772470 100644 --- a/s2core/src/main/scala/org/apache/s2graph/core/S2GraphLike.scala +++ b/s2core/src/main/scala/org/apache/s2graph/core/S2GraphLike.scala @@ -1,8 +1,10 @@ package org.apache.s2graph.core import java.util -import java.util.concurrent.TimeUnit +import java.util.concurrent.{CompletableFuture, TimeUnit} import java.util.concurrent.atomic.AtomicLong +import java.lang.{Boolean => JBoolean, Long => JLong} +import java.util.Optional import com.typesafe.config.Config import org.apache.commons.configuration.Configuration @@ -21,8 +23,11 @@ import org.apache.tinkerpop.gremlin.structure.io.{GraphReader, GraphWriter, Io, import org.apache.tinkerpop.gremlin.structure.{Direction, Edge, Element, Graph, T, Transaction, Vertex} import scala.collection.JavaConversions._ +import scala.collection.JavaConverters._ import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, Future} +import scala.compat.java8.FutureConverters._ +import scala.compat.java8.OptionConverters._ trait S2GraphLike extends Graph { @@ -72,34 +77,73 @@ trait S2GraphLike extends Graph { def getVertices(vertices: Seq[S2VertexLike]): Future[Seq[S2VertexLike]] + def getVerticesJava(vertices: util.List[S2VertexLike]): CompletableFuture[util.List[S2VertexLike]] = + getVertices(vertices.toSeq).map(_.asJava).toJava.toCompletableFuture + def checkEdges(edges: Seq[S2EdgeLike]): Future[StepResult] + def checkEdgesJava(edges: util.List[S2EdgeLike]): CompletableFuture[StepResult] = + checkEdges(edges.asScala).toJava.toCompletableFuture + def mutateVertices(vertices: Seq[S2VertexLike], withWait: Boolean = false): Future[Seq[MutateResponse]] + def mutateVerticesJava(vertices: util.List[S2VertexLike], withWait: JBoolean): CompletableFuture[util.List[MutateResponse]] = + mutateVertices(vertices.asScala, withWait.booleanValue()).map(_.asJava).toJava.toCompletableFuture + def mutateEdges(edges: Seq[S2EdgeLike], withWait: Boolean = false): Future[Seq[MutateResponse]] + def mutateEdgesJava(edges: util.List[S2EdgeLike], withWait: JBoolean): CompletableFuture[util.List[MutateResponse]] = + mutateEdges(edges.asScala, withWait.booleanValue()).map(_.asJava).toJava.toCompletableFuture + def mutateElements(elements: Seq[GraphElement], withWait: Boolean = false): Future[Seq[MutateResponse]] + def mutateElementsJava(elements: util.List[GraphElement], withWait: JBoolean): CompletableFuture[util.List[MutateResponse]] = + mutateElements(elements.asScala, withWait.booleanValue()).map(_.asJava).toJava.toCompletableFuture + def getEdges(q: Query): Future[StepResult] + def getEdgesJava(q: Query): CompletableFuture[StepResult] = + getEdges(q).toJava.toCompletableFuture + def getEdgesMultiQuery(mq: MultiQuery): Future[StepResult] + def getEdgesMultiQueryJava(mq: MultiQuery): CompletableFuture[StepResult] = + getEdgesMultiQuery(mq).toJava.toCompletableFuture + def deleteAllAdjacentEdges(srcVertices: Seq[S2VertexLike], labels: Seq[Label], dir: Int, ts: Long): Future[Boolean] + def deleteAllAdjacentEdgesJava(srcVertices: util.List[S2VertexLike], labels: util.List[Label], direction: Direction): CompletableFuture[JBoolean] = + deleteAllAdjacentEdges(srcVertices.asScala, labels.asScala, GraphUtil.toDirection(direction), System.currentTimeMillis()).map(JBoolean.valueOf(_)).toJava.toCompletableFuture + def incrementCounts(edges: Seq[S2EdgeLike], withWait: Boolean): Future[Seq[MutateResponse]] + def incrementCountsJava(edges: util.List[S2EdgeLike], withWait: JBoolean): CompletableFuture[util.List[MutateResponse]] = + incrementCounts(edges.asScala, withWait.booleanValue()).map(_.asJava).toJava.toCompletableFuture + def updateDegree(edge: S2EdgeLike, degreeVal: Long = 0): Future[MutateResponse] + def updateDegreeJava(edge: S2EdgeLike, degreeVal: JLong): CompletableFuture[MutateResponse] = + updateDegree(edge, degreeVal.longValue()).toJava.toCompletableFuture + def getVertex(vertexId: VertexId): Option[S2VertexLike] + def getVertexJava(vertexId: VertexId): Optional[S2VertexLike] = + getVertex(vertexId).asJava + def fetchEdges(vertex: S2VertexLike, labelNameWithDirs: Seq[(String, String)]): util.Iterator[Edge] + def fetchEdgesJava(vertex: S2VertexLike, labelNameWithDirs: util.List[(String, String)]): util.Iterator[Edge] = + fetchEdges(vertex, labelNameWithDirs.asScala) + def edgesAsync(vertex: S2VertexLike, direction: Direction, labelNames: String*): Future[util.Iterator[Edge]] + def edgesAsyncJava(vertex: S2VertexLike, direction: Direction, labelNames: String*): CompletableFuture[util.Iterator[Edge]] = + edgesAsync(vertex, direction, labelNames: _*).toJava.toCompletableFuture + /** Convert to Graph Element **/ def toVertex(serviceName: String, columnName: String, @@ -196,22 +240,6 @@ trait S2GraphLike extends Graph { addVertex(Seq(T.label, label): _*) } - def makeVertex(idValue: AnyRef, kvsMap: Map[String, AnyRef]): S2VertexLike = { - idValue match { - case vId: VertexId => - elementBuilder.toVertex(vId.column.service.serviceName, vId.column.columnName, vId, kvsMap) - case _ => - val serviceColumnNames = kvsMap.getOrElse(T.label.toString, DefaultColumnName).toString - - val names = serviceColumnNames.split(S2Vertex.VertexLabelDelimiter) - val (serviceName, columnName) = - if (names.length == 1) (DefaultServiceName, names(0)) - else throw new RuntimeException("malformed data on vertex label.") - - elementBuilder.toVertex(serviceName, columnName, idValue, kvsMap) - } - } - def addVertex(kvs: AnyRef*): structure.Vertex = { if (!features().vertex().supportsUserSuppliedIds() && kvs.contains(T.id)) { throw Vertex.Exceptions.userSuppliedIdsNotSupported @@ -232,9 +260,9 @@ trait S2GraphLike extends Graph { val vertex = kvsMap.get(T.id.name()) match { case None => // do nothing val id = localLongId.getAndIncrement() - makeVertex(Long.box(id), kvsMap) + elementBuilder.makeVertex(Long.box(id), kvsMap) case Some(idValue) if S2Property.validType(idValue) => - makeVertex(idValue, kvsMap) + elementBuilder.makeVertex(idValue, kvsMap) case _ => throw Vertex.Exceptions.userSuppliedIdsOfThisTypeNotSupported }
