Repository: incubator-s2graph
Updated Branches:
  refs/heads/master 51e6ea34b -> f3cdd0724


[S2GRAPH-44]: Provide cache for WhereParser on query.

  add RequestCache for WhereParser on Query.

JIRA:
  [S2GRAPH-44] https://issues.apache.org/jira/browse/S2GRAPH-44

Pull Request:
  Closes #28


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

Branch: refs/heads/master
Commit: f3cdd0724229269d8ec4c22f9a7ddede5902128f
Parents: 51e6ea3
Author: DO YUNG YOON <[email protected]>
Authored: Tue Feb 23 19:48:40 2016 +0900
Committer: DO YUNG YOON <[email protected]>
Committed: Tue Feb 23 19:48:40 2016 +0900

----------------------------------------------------------------------
 CHANGES                                         |  2 ++
 .../kakao/s2graph/core/rest/RequestParser.scala | 26 +++++++++++++++-----
 2 files changed, 22 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/f3cdd072/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 1a64beb..1f2e277 100644
--- a/CHANGES
+++ b/CHANGES
@@ -29,6 +29,8 @@ Release 0.12.1 - unreleased
 
     S2GRAPH-41: Refactor PostProcess's toSimpleVertexArrJson (Committed by 
DOYUNG YOON).
 
+    S2GRAPH-44: Provide cache for WhereParser on query (Committed by DOYUNG 
YOON).
+
   BUG FIXES
 
     S2GRAPH-18: Query Option "interval" is Broken. 

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/f3cdd072/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala
----------------------------------------------------------------------
diff --git 
a/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala 
b/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala
index a14a3b0..f0360c2 100644
--- a/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala
+++ b/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala
@@ -1,9 +1,12 @@
 package com.kakao.s2graph.core.rest
 
+import java.util.concurrent.{Callable, TimeUnit}
+
+import com.google.common.cache.CacheBuilder
 import com.kakao.s2graph.core.GraphExceptions.{BadQueryException, 
ModelNotFoundException}
 import com.kakao.s2graph.core._
 import com.kakao.s2graph.core.mysqls._
-import com.kakao.s2graph.core.parsers.WhereParser
+import com.kakao.s2graph.core.parsers.{Where, WhereParser}
 import com.kakao.s2graph.core.types._
 import com.kakao.s2graph.core.utils.logger
 import com.typesafe.config.Config
@@ -22,6 +25,12 @@ class RequestParser(config: Config) extends JSONParser {
   val DefaultCluster = config.getString("hbase.zookeeper.quorum")
   val DefaultCompressionAlgorithm = 
config.getString("hbase.table.compression.algorithm")
   val DefaultPhase = config.getString("phase")
+  val parserCache = CacheBuilder.newBuilder()
+    .expireAfterAccess(10000, TimeUnit.MILLISECONDS)
+    .expireAfterWrite(10000, TimeUnit.MILLISECONDS)
+    .maximumSize(10000)
+    .initialCapacity(1000)
+    .build[String, Try[Where]]
 
   private def extractScoring(labelId: Int, value: JsValue) = {
     val ret = for {
@@ -90,14 +99,19 @@ class RequestParser(config: Config) extends JSONParser {
     ret.map(_.toMap).getOrElse(Map.empty[Byte, InnerValLike])
   }
 
-  def extractWhere(label: Label, whereClauseOpt: Option[String]) = {
+  def extractWhere(label: Label, whereClauseOpt: Option[String]): Try[Where] = 
{
     whereClauseOpt match {
       case None => Success(WhereParser.success)
       case Some(where) =>
-        WhereParser(label).parse(where) match {
-          case s@Success(_) => s
-          case Failure(ex) => throw BadQueryException(ex.getMessage, ex)
-        }
+        val whereParserKey = s"${label.label}_${where}"
+        parserCache.get(whereParserKey, new Callable[Try[Where]] {
+          override def call(): Try[Where] = {
+            WhereParser(label).parse(where) match {
+              case s@Success(_) => s
+              case Failure(ex) => throw BadQueryException(ex.getMessage, ex)
+            }
+          }
+        })
     }
   }
 

Reply via email to