You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@s2graph.apache.org by st...@apache.org on 2018/06/21 01:20:35 UTC

[3/7] incubator-s2graph git commit: - Separate SearchParam from VertexQueryParam.

- Separate SearchParam from VertexQueryParam.


Project: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/commit/8e86536a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/tree/8e86536a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/diff/8e86536a

Branch: refs/heads/master
Commit: 8e86536a547dc1fe4c4f3aef29181ac8f2388eed
Parents: f47622f
Author: DO YUNG YOON <st...@apache.org>
Authored: Fri Jun 15 16:16:46 2018 +0900
Committer: DO YUNG YOON <st...@apache.org>
Committed: Fri Jun 15 16:16:46 2018 +0900

----------------------------------------------------------------------
 .../org/apache/s2graph/core/QueryParam.scala      | 11 +++++++----
 .../s2graph/core/index/ESIndexProvider.scala      | 10 +++++-----
 .../s2graph/core/index/LuceneIndexProvider.scala  |  8 ++++----
 .../apache/s2graph/core/rest/RequestParser.scala  | 18 +++++++++++++++---
 .../s2graph/core/Integrate/VertexTestHelper.scala | 10 +++++++---
 .../s2graph/core/index/IndexProviderTest.scala    |  2 +-
 .../s2graph/graphql/types/FieldResolver.scala     | 11 +++++++++--
 7 files changed, 48 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/8e86536a/s2core/src/main/scala/org/apache/s2graph/core/QueryParam.scala
----------------------------------------------------------------------
diff --git a/s2core/src/main/scala/org/apache/s2graph/core/QueryParam.scala b/s2core/src/main/scala/org/apache/s2graph/core/QueryParam.scala
index cb1434f..5fdd3ee 100644
--- a/s2core/src/main/scala/org/apache/s2graph/core/QueryParam.scala
+++ b/s2core/src/main/scala/org/apache/s2graph/core/QueryParam.scala
@@ -294,17 +294,20 @@ object QueryParam {
 }
 
 object VertexQueryParam {
-  def Empty: VertexQueryParam = VertexQueryParam(Nil, 0, 1, None)
+  def Empty: VertexQueryParam = VertexQueryParam(Nil)
 
   def apply(vertexIds: Seq[VertexId]): VertexQueryParam = {
     VertexQueryParam(vertexIds)
   }
 }
 
+object SearchParam {
+  def Empty: SearchParam = SearchParam("", 0, 1)
+}
+case class SearchParam(searchString: String, offset: Int, limit: Int)
+
 case class VertexQueryParam(vertexIds: Seq[VertexId],
-                            offset: Int = 0,
-                            limit: Int = 1,
-                            searchString: Option[String] = None,
+                            searchParamOpt: Option[SearchParam] = None,
                             fetchProp: Boolean = true,
                             where: Try[Where] = Success(WhereParser.success)) {
 }

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/8e86536a/s2core/src/main/scala/org/apache/s2graph/core/index/ESIndexProvider.scala
----------------------------------------------------------------------
diff --git a/s2core/src/main/scala/org/apache/s2graph/core/index/ESIndexProvider.scala b/s2core/src/main/scala/org/apache/s2graph/core/index/ESIndexProvider.scala
index be1ad6e..c047be2 100644
--- a/s2core/src/main/scala/org/apache/s2graph/core/index/ESIndexProvider.scala
+++ b/s2core/src/main/scala/org/apache/s2graph/core/index/ESIndexProvider.scala
@@ -202,12 +202,12 @@ class ESIndexProvider(config: Config)(implicit ec: ExecutionContext) extends Ind
     val field = vidField
     val empty = new util.ArrayList[VertexId]()
 
-    vertexQueryParam.searchString match {
-      case Some(queryString) =>
+    vertexQueryParam.searchParamOpt match {
+      case Some(searchParam) =>
         fetchInner[VertexId](
-          queryString,
-          vertexQueryParam.offset,
-          vertexQueryParam.limit,
+          searchParam.searchString,
+          searchParam.offset,
+          searchParam.limit,
           VertexIndexName,
           field,
           Conversions.s2VertexIdReads)(v => VertexId.isValid(v).isDefined)

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/8e86536a/s2core/src/main/scala/org/apache/s2graph/core/index/LuceneIndexProvider.scala
----------------------------------------------------------------------
diff --git a/s2core/src/main/scala/org/apache/s2graph/core/index/LuceneIndexProvider.scala b/s2core/src/main/scala/org/apache/s2graph/core/index/LuceneIndexProvider.scala
index 00f2e20..efb44f6 100644
--- a/s2core/src/main/scala/org/apache/s2graph/core/index/LuceneIndexProvider.scala
+++ b/s2core/src/main/scala/org/apache/s2graph/core/index/LuceneIndexProvider.scala
@@ -242,16 +242,16 @@ class LuceneIndexProvider(config: Config) extends IndexProvider {
   override def fetchVertexIdsAsync(hasContainers: java.util.List[HasContainer]): Future[util.List[VertexId]] = Future.successful(fetchVertexIds(hasContainers))
 
   override def fetchVertexIdsAsyncRaw(vertexQueryParam: VertexQueryParam): Future[util.List[VertexId]] = {
-    val ret = vertexQueryParam.searchString.fold(util.Arrays.asList[VertexId]()) { queryString =>
+    val ret = vertexQueryParam.searchParamOpt.fold(util.Arrays.asList[VertexId]()) { searchParam =>
       val field = vidField
       try {
         val qp = createQueryParser(field, analyzer)
-        val q = qp.parse(queryString)
+        val q = qp.parse(searchParam.searchString)
 
-        fetchInner[VertexId](q, vertexQueryParam.offset, vertexQueryParam.limit, VertexIndexName, vidField, Conversions.s2VertexIdReads)
+        fetchInner[VertexId](q, searchParam.offset, searchParam.limit, VertexIndexName, vidField, Conversions.s2VertexIdReads)
       } catch {
         case ex: ParseException =>
-          logger.error(s"[IndexProvider]: ${queryString} parse failed.", ex)
+          logger.error(s"[IndexProvider]: ${searchParam.searchString} parse failed.", ex)
           util.Arrays.asList[VertexId]()
       }
     }

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/8e86536a/s2core/src/main/scala/org/apache/s2graph/core/rest/RequestParser.scala
----------------------------------------------------------------------
diff --git a/s2core/src/main/scala/org/apache/s2graph/core/rest/RequestParser.scala b/s2core/src/main/scala/org/apache/s2graph/core/rest/RequestParser.scala
index 7f8b280..b71da8e 100644
--- a/s2core/src/main/scala/org/apache/s2graph/core/rest/RequestParser.scala
+++ b/s2core/src/main/scala/org/apache/s2graph/core/rest/RequestParser.scala
@@ -219,12 +219,16 @@ class RequestParser(graph: S2GraphLike) {
     ret.map(_.toMap).getOrElse(Map.empty[String, InnerValLike])
   }
 
-  def extractWhere(label: Label, whereClauseOpt: Option[String]): Try[Where] = {
+  private def toWhereParserKey(column: ServiceColumn, whereString: String): String =
+    s"${column.service.serviceName}_${column.columnName}_${whereString}"
+
+  private def toWhereParserKey(label: Label, whereString: String): String =
+    s"${label.label}_${whereString}"
+
+  private def extractWhereInner(whereParserKey: String, whereClauseOpt: Option[String]): Try[Where] = {
     whereClauseOpt match {
       case None => Success(WhereParser.success)
       case Some(where) =>
-        val whereParserKey = s"${label.label}_${where}"
-
         parserCache.get(whereParserKey, new Callable[Try[Where]] {
           override def call(): Try[Where] = {
             val _where = TemplateHelper.replaceVariable(System.currentTimeMillis(), where)
@@ -239,6 +243,14 @@ class RequestParser(graph: S2GraphLike) {
     }
   }
 
+  def extractWhere(column: ServiceColumn, whereClauseOpt: Option[String]): Try[Where] = {
+    extractWhereInner(toWhereParserKey(column, whereClauseOpt.getOrElse("")), whereClauseOpt)
+  }
+
+  def extractWhere(label: Label, whereClauseOpt: Option[String]): Try[Where] = {
+    extractWhereInner(toWhereParserKey(label, whereClauseOpt.getOrElse("")), whereClauseOpt)
+  }
+
   def extractGroupBy(value: Option[JsValue]): GroupBy = value.map {
     case obj: JsObject =>
       val keys = (obj \ "keys").asOpt[Seq[String]].getOrElse(Nil)

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/8e86536a/s2core/src/test/scala/org/apache/s2graph/core/Integrate/VertexTestHelper.scala
----------------------------------------------------------------------
diff --git a/s2core/src/test/scala/org/apache/s2graph/core/Integrate/VertexTestHelper.scala b/s2core/src/test/scala/org/apache/s2graph/core/Integrate/VertexTestHelper.scala
index 62c7e00..c4136ab 100644
--- a/s2core/src/test/scala/org/apache/s2graph/core/Integrate/VertexTestHelper.scala
+++ b/s2core/src/test/scala/org/apache/s2graph/core/Integrate/VertexTestHelper.scala
@@ -19,7 +19,7 @@
 
 package org.apache.s2graph.core.Integrate
 
-import org.apache.s2graph.core.PostProcess
+import org.apache.s2graph.core.{PostProcess, VertexQueryParam}
 import play.api.libs.json.{JsValue, Json}
 
 import scala.concurrent.Await
@@ -43,9 +43,11 @@ class VertexTestHelper extends IntegrateCommon {
 
     val vertices = parser.toVertices(payload, "insert", Option(serviceName), Option(columnName))
     val srcVertices = vertices
+    val srcVertexIds = srcVertices.map(_.id)
+    val queryParam = VertexQueryParam(vertexIds = srcVertexIds)
     Await.result(graph.mutateVertices(srcVertices, withWait = true), HttpRequestWaitingTime)
 
-    val res = graph.getVertices(srcVertices).map { vertices =>
+    val res = graph.getVertices(queryParam).map { vertices =>
       PostProcess.verticesToJson(vertices)
     }
 
@@ -70,9 +72,11 @@ class VertexTestHelper extends IntegrateCommon {
     val vertices = parser.toVertices(payload, "insert", Option(serviceName),
       Option(stringColumnName))
     val srcVertices = vertices
+    val srcVertexIds = srcVertices.map(_.id)
+    val queryParam = VertexQueryParam(vertexIds = srcVertexIds)
     Await.result(graph.mutateVertices(srcVertices, withWait = true), HttpRequestWaitingTime)
 
-    val res = graph.getVertices(srcVertices).map { vertices =>
+    val res = graph.getVertices(queryParam).map { vertices =>
       PostProcess.verticesToJson(vertices)
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/8e86536a/s2core/src/test/scala/org/apache/s2graph/core/index/IndexProviderTest.scala
----------------------------------------------------------------------
diff --git a/s2core/src/test/scala/org/apache/s2graph/core/index/IndexProviderTest.scala b/s2core/src/test/scala/org/apache/s2graph/core/index/IndexProviderTest.scala
index c12ad7d..12cec21 100644
--- a/s2core/src/test/scala/org/apache/s2graph/core/index/IndexProviderTest.scala
+++ b/s2core/src/test/scala/org/apache/s2graph/core/index/IndexProviderTest.scala
@@ -80,7 +80,7 @@ class IndexProviderTest extends IntegrateCommon {
 //      val hasContainer = new HasContainer(indexPropsColumnMeta.name, P.eq(Long.box(1)))
       val hasContainer = new HasContainer(serviceColumnField, P.eq(testColumn.columnName))
 
-      val f = graph.searchVertices(VertexQueryParam(0, 100, Option(s"${serviceColumnField}:${testColumn.columnName}")))
+      val f = graph.searchVertices(VertexQueryParam(Nil, Option(SearchParam(s"${serviceColumnField}:${testColumn.columnName}", 0, 100))))
       val a = Await.result(f, Duration("60 sec"))
       println(a)
 

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/8e86536a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/FieldResolver.scala
----------------------------------------------------------------------
diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/FieldResolver.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/FieldResolver.scala
index 6e1a7cc..43b3de6 100644
--- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/FieldResolver.scala
+++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/types/FieldResolver.scala
@@ -76,6 +76,8 @@ object FieldResolver {
     val ids = c.argOpt[Any]("id").toSeq ++ c.argOpt[List[Any]]("ids").toList.flatten
     val offset = c.arg[Int]("offset")
     val limit = c.arg[Int]("limit")
+    val whereClauseOpt = c.argOpt[String]("filter")
+    val where = c.ctx.parser.extractWhere(column, whereClauseOpt)
 
     val vertices = ids.map(vid => c.ctx.toS2VertexLike(vid, column))
     val searchOpt = c.argOpt[String]("search").map { qs =>
@@ -87,7 +89,9 @@ object FieldResolver {
     val selectedFields = AstHelper.selectedFields(c.astFields)
     val canSkipFetch = selectedFields.forall(f => f == "id" || !columnFields(f))
 
-    val vertexQueryParam = VertexQueryParam(vertices.map(_.id), offset, limit, searchOpt, !canSkipFetch)
+    val searchParamOpt = searchOpt.map(search => SearchParam(search, offset, limit))
+    val vertexQueryParam = VertexQueryParam(vertices.map(_.id),
+      searchParamOpt, fetchProp = !canSkipFetch, where = where)
 
     vertexQueryParam
   }
@@ -102,7 +106,10 @@ object FieldResolver {
     val columnFields = column.metasInvMap.keySet
     val canSkipFetch = selectedFields.forall(f => f == "id" || !columnFields(f))
 
-    val vertexQueryParam = VertexQueryParam(Seq(vertex.id), 0, 1, None, !canSkipFetch)
+    val whereClauseOpt = c.argOpt[String]("filter")
+    val where = c.ctx.parser.extractWhere(column, whereClauseOpt)
+
+    val vertexQueryParam = VertexQueryParam(Seq(vertex.id), None, fetchProp = !canSkipFetch, where = where)
 
     vertexQueryParam
   }