remove SelfType bidirectional dependencies on S2VertexLike.
Project: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/commit/5f2d4ffc Tree: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/tree/5f2d4ffc Diff: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/diff/5f2d4ffc Branch: refs/heads/master Commit: 5f2d4ffc161abac372a7a1d4cbadc89cce45a84b Parents: 3514060 Author: DO YUNG YOON <[email protected]> Authored: Tue Nov 7 11:41:42 2017 +0900 Committer: DO YUNG YOON <[email protected]> Committed: Tue Nov 7 11:41:42 2017 +0900 ---------------------------------------------------------------------- .../org/apache/s2graph/core/PostProcess.scala | 6 +- .../scala/org/apache/s2graph/core/S2Graph.scala | 25 +++++++- .../apache/s2graph/core/S2VertexBuilder.scala | 35 +++++++++++ .../org/apache/s2graph/core/S2VertexLike.scala | 63 ++++---------------- 4 files changed, 74 insertions(+), 55 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/5f2d4ffc/s2core/src/main/scala/org/apache/s2graph/core/PostProcess.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/PostProcess.scala b/s2core/src/main/scala/org/apache/s2graph/core/PostProcess.scala index 900bbbd..7047214 100644 --- a/s2core/src/main/scala/org/apache/s2graph/core/PostProcess.scala +++ b/s2core/src/main/scala/org/apache/s2graph/core/PostProcess.scala @@ -147,9 +147,9 @@ object PostProcess { def s2VertexToJson(s2Vertex: S2VertexLike): Option[JsValue] = { val props = for { - (k, v) <- s2Vertex.properties - jsVal <- anyValToJsValue(v) - } yield k -> jsVal + (_, property) <- s2Vertex.props + jsVal <- anyValToJsValue(property.value) + } yield property.columnMeta.name -> jsVal for { id <- anyValToJsValue(s2Vertex.innerIdVal) http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/5f2d4ffc/s2core/src/main/scala/org/apache/s2graph/core/S2Graph.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/S2Graph.scala b/s2core/src/main/scala/org/apache/s2graph/core/S2Graph.scala index 3270e84..fc1205d 100644 --- a/s2core/src/main/scala/org/apache/s2graph/core/S2Graph.scala +++ b/s2core/src/main/scala/org/apache/s2graph/core/S2Graph.scala @@ -33,7 +33,7 @@ import org.apache.s2graph.core.storage.{MutateResponse, Storage} import org.apache.s2graph.core.types._ import org.apache.s2graph.core.utils.{DeferCache, Extensions, logger} import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies -import org.apache.tinkerpop.gremlin.structure.{Edge, Graph} +import org.apache.tinkerpop.gremlin.structure.{Direction, Edge, Graph} import scala.collection.JavaConversions._ import scala.collection.mutable @@ -733,6 +733,29 @@ class S2Graph(_config: Config)(implicit val ec: ExecutionContext) extends S2Grap } } + def edgesAsync(vertex: S2VertexLike, direction: Direction, labelNames: String*): Future[util.Iterator[Edge]] = { + val labelNameWithDirs = + if (labelNames.isEmpty) { + // TODO: Let's clarify direction + if (direction == Direction.BOTH) { + Label.findBySrcColumnId(vertex.id.colId).map(l => l.label -> Direction.OUT.name) ++ + Label.findByTgtColumnId(vertex.id.colId).map(l => l.label -> Direction.IN.name) + } else if (direction == Direction.IN) { + Label.findByTgtColumnId(vertex.id.colId).map(l => l.label -> direction.name) + } else { + Label.findBySrcColumnId(vertex.id.colId).map(l => l.label -> direction.name) + } + } else { + direction match { + case Direction.BOTH => + Seq(Direction.OUT, Direction.IN).flatMap { dir => labelNames.map(_ -> dir.name()) } + case _ => labelNames.map(_ -> direction.name()) + } + } + + fetchEdgesAsync(vertex, labelNameWithDirs.distinct) + } + /** mutate */ def deleteAllAdjacentEdges(srcVertices: Seq[S2VertexLike], labels: Seq[Label], http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/5f2d4ffc/s2core/src/main/scala/org/apache/s2graph/core/S2VertexBuilder.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/S2VertexBuilder.scala b/s2core/src/main/scala/org/apache/s2graph/core/S2VertexBuilder.scala new file mode 100644 index 0000000..4a75036 --- /dev/null +++ b/s2core/src/main/scala/org/apache/s2graph/core/S2VertexBuilder.scala @@ -0,0 +1,35 @@ +package org.apache.s2graph.core + +import java.util +import java.util.function.BiConsumer + +import org.apache.s2graph.core.S2Vertex.Props +import org.apache.s2graph.core.mysqls.ColumnMeta +import org.apache.s2graph.core.types.VertexId + +class S2VertexBuilder(vertex: S2VertexLike) { + def defaultProps: util.HashMap[String, S2VertexProperty[_]] = { + val default = S2Vertex.EmptyProps + val newProps = new S2VertexProperty(vertex, ColumnMeta.lastModifiedAtColumn, ColumnMeta.lastModifiedAtColumn.name, vertex.ts) + default.put(ColumnMeta.lastModifiedAtColumn.name, newProps) + default + } + + def copyVertex(graph: S2Graph = vertex.graph, + id: VertexId = vertex.id, + ts: Long = vertex.ts, + props: Props = vertex.props, + op: Byte = vertex.op, + belongLabelIds: Seq[Int] = vertex.belongLabelIds): S2VertexLike = { + val newProps = S2Vertex.EmptyProps + val v = new S2Vertex(graph, id, ts, newProps, op, belongLabelIds) + + props.forEach(new BiConsumer[String, S2VertexProperty[_]] { + override def accept(t: String, u: S2VertexProperty[_]) = { + v.property(t, u) + } + }) + + v + } +} http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/5f2d4ffc/s2core/src/main/scala/org/apache/s2graph/core/S2VertexLike.scala ---------------------------------------------------------------------- diff --git a/s2core/src/main/scala/org/apache/s2graph/core/S2VertexLike.scala b/s2core/src/main/scala/org/apache/s2graph/core/S2VertexLike.scala index 9ec2ab0..29db49d 100644 --- a/s2core/src/main/scala/org/apache/s2graph/core/S2VertexLike.scala +++ b/s2core/src/main/scala/org/apache/s2graph/core/S2VertexLike.scala @@ -9,12 +9,10 @@ import org.apache.tinkerpop.gremlin.structure.VertexProperty.Cardinality import org.apache.tinkerpop.gremlin.structure.{Direction, Edge, T, Vertex, VertexProperty} import play.api.libs.json.Json -import scala.concurrent.{Await, Future} import scala.collection.JavaConverters._ +import scala.concurrent.Await trait S2VertexLike extends Vertex with GraphElement { - this: S2Vertex => - val graph: S2Graph val id: VertexId val ts: Long @@ -23,12 +21,12 @@ trait S2VertexLike extends Vertex with GraphElement { val belongLabelIds: Seq[Int] val innerId = id.innerId + val innerIdVal = innerId.value + val builder = new S2VertexBuilder(this) - lazy val properties = for { - (k, v) <- props.asScala - } yield v.columnMeta.name -> v.value + def label(): String = serviceColumn.columnName def schemaVer = serviceColumn.schemaVersion @@ -37,20 +35,16 @@ trait S2VertexLike extends Vertex with GraphElement { def columnName = serviceColumn.columnName lazy val service = Service.findById(serviceColumn.serviceId) - lazy val (hbaseZkAddr, hbaseTableName) = (service.cluster, service.hTableName) - def defaultProps = { - val default = S2Vertex.EmptyProps - val newProps = new S2VertexProperty(this, ColumnMeta.lastModifiedAtColumn, ColumnMeta.lastModifiedAtColumn.name, ts) - default.put(ColumnMeta.lastModifiedAtColumn.name, newProps) - default - } + lazy val (hbaseZkAddr, hbaseTableName) = (service.cluster, service.hTableName) - def propsWithName = for { - (k, v) <- props.asScala - } yield (v.columnMeta.name -> v.value.toString) + def defaultProps: util.HashMap[String, S2VertexProperty[_]] = builder.defaultProps def toLogString(): String = { + val propsWithName = for { + (k, v) <- props.asScala + } yield (v.columnMeta.name -> v.value.toString) + val (serviceName, columnName) = if (!id.storeColId) ("", "") else (serviceColumn.service.serviceName, serviceColumn.columnName) @@ -61,12 +55,6 @@ trait S2VertexLike extends Vertex with GraphElement { Seq(ts, GraphUtil.fromOp(op), "v", id.innerId, serviceName, columnName).mkString("\t") } - def copyVertexWithState(props: Props): S2VertexLike = { - val newVertex = copy(props = S2Vertex.EmptyProps) - S2Vertex.fillPropsWithTs(newVertex, props) - newVertex - } - def vertices(direction: Direction, edgeLabels: String*): util.Iterator[Vertex] = { val arr = new util.ArrayList[Vertex]() edges(direction, edgeLabels: _*).forEachRemaining(new Consumer[Edge] { @@ -166,23 +154,19 @@ trait S2VertexLike extends Vertex with GraphElement { ls.iterator } - def label(): String = { - serviceColumn.columnName - } - def remove(): Unit = { if (graph.features().vertex().supportsRemoveVertices()) { // remove edge // TODO: remove related edges also. implicit val ec = graph.ec - val verticesToDelete = Seq(this.copy(op = GraphUtil.operations("delete"))) + val verticesToDelete = Seq(builder.copyVertex(op = GraphUtil.operations("delete"))) val vertexFuture = graph.mutateVertices(verticesToDelete, withWait = true) val future = for { vertexSuccess <- vertexFuture - edges <- edgesAsync(Direction.BOTH) + edges <- graph.edgesAsync(this, Direction.BOTH) } yield { edges.asScala.toSeq.foreach { edge => edge.remove() } if (!vertexSuccess.forall(_.isSuccess)) throw new RuntimeException("Vertex.remove vertex delete failed.") @@ -195,27 +179,4 @@ trait S2VertexLike extends Vertex with GraphElement { throw Vertex.Exceptions.vertexRemovalNotSupported() } } - - private def edgesAsync(direction: Direction, labelNames: String*): Future[util.Iterator[Edge]] = { - val labelNameWithDirs = - if (labelNames.isEmpty) { - // TODO: Let's clarify direction - if (direction == Direction.BOTH) { - Label.findBySrcColumnId(id.colId).map(l => l.label -> Direction.OUT.name) ++ - Label.findByTgtColumnId(id.colId).map(l => l.label -> Direction.IN.name) - } else if (direction == Direction.IN) { - Label.findByTgtColumnId(id.colId).map(l => l.label -> direction.name) - } else { - Label.findBySrcColumnId(id.colId).map(l => l.label -> direction.name) - } - } else { - direction match { - case Direction.BOTH => - Seq(Direction.OUT, Direction.IN).flatMap { dir => labelNames.map(_ -> dir.name()) } - case _ => labelNames.map(_ -> direction.name()) - } - } - - graph.fetchEdgesAsync(this, labelNameWithDirs.distinct) - } }
