jiangpengcheng commented on a change in pull request #4963:
URL: https://github.com/apache/openwhisk/pull/4963#discussion_r614503992



##########
File path: 
common/scala/src/main/scala/org/apache/openwhisk/core/database/mongodb/MongoDBViewMapper.scala
##########
@@ -0,0 +1,224 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openwhisk.core.database.mongodb
+
+import org.apache.openwhisk.core.database._
+import org.apache.openwhisk.core.entity.WhiskQueries
+import org.mongodb.scala.Document
+import org.mongodb.scala.bson.conversions.Bson
+import org.mongodb.scala.model.Filters._
+import org.mongodb.scala.model.Sorts
+
+trait MongoDBViewMapper {
+  protected val _computed: String = "_computed"
+  protected val TOP: String = WhiskQueries.TOP
+
+  val indexes: List[Document]
+
+  def filter(ddoc: String, view: String, startKey: List[Any], endKey: 
List[Any]): Bson
+
+  def sort(ddoc: String, view: String, descending: Boolean): Option[Bson]
+
+  protected def checkKeys(startKey: List[Any], endKey: List[Any]): Unit = {
+    require(startKey.nonEmpty)
+    require(endKey.nonEmpty)
+    require(startKey.head == endKey.head, s"First key should be same => 
($startKey) - ($endKey)")
+  }
+}
+
+private object ActivationViewMapper extends MongoDBViewMapper {
+  private val NS = "namespace"
+  private val NS_WITH_PATH = s"${_computed}.${ActivationHandler.NS_PATH}"
+  private val START = "start"
+  override val indexes: List[Document] =
+    List(
+      Document(s"$START" -> -1),
+      Document(s"$START" -> -1, s"$NS" -> -1),
+      Document(s"$NS_WITH_PATH" -> -1, s"$START" -> -1))
+
+  override def filter(ddoc: String, view: String, startKey: List[Any], endKey: 
List[Any]): Bson = {
+    checkKeys(startKey, endKey)
+    view match {
+      //whisks-filters ddoc uses namespace + invoking action path as first key
+      case "activations" if ddoc.startsWith("whisks-filters") => 
createActivationFilter(NS_WITH_PATH, startKey, endKey)
+      //whisks ddoc uses namespace as first key
+      case "activations" if ddoc.startsWith("whisks") => 
createActivationFilter(NS, startKey, endKey)
+      case _                                          => throw 
UnsupportedView(s"$ddoc/$view")
+    }
+  }
+
+  override def sort(ddoc: String, view: String, descending: Boolean): 
Option[Bson] = {
+    view match {
+      case "activations" if ddoc.startsWith("whisks-filters") =>
+        val sort = if (descending) Sorts.descending(NS_WITH_PATH, START) else 
Sorts.ascending(NS_WITH_PATH, START)
+        Some(sort)
+      case "activations" if ddoc.startsWith("whisks") =>
+        val sort = if (descending) Sorts.descending(NS, START) else 
Sorts.ascending(NS, START)
+        Some(sort)
+      case _ => throw UnsupportedView(s"$ddoc/$view")
+    }
+  }
+
+  private def createActivationFilter(nsPropName: String, startKey: List[Any], 
endKey: List[Any]) = {
+    require(startKey.head.isInstanceOf[String])
+    val matchNS = equal(nsPropName, startKey.head)
+
+    val filter = (startKey, endKey) match {
+      case (_ :: Nil, _ :: `TOP` :: Nil) =>
+        matchNS
+      case (_ :: since :: Nil, _ :: `TOP` :: `TOP` :: Nil) =>
+        and(matchNS, gte(START, since))
+      case (_ :: since :: Nil, _ :: upto :: `TOP` :: Nil) =>
+        and(matchNS, gte(START, since), lte(START, upto))
+      case _ => throw UnsupportedQueryKeys(s"$startKey, $endKey")
+    }
+    filter
+  }
+}
+
+private object WhisksViewMapper extends MongoDBViewMapper {
+  private val NS = "namespace"
+  private val ROOT_NS = s"${_computed}.${WhisksHandler.ROOT_NS}"
+  private val TYPE = "entityType"
+  private val UPDATED = "updated"
+  private val PUBLISH = "publish"
+  private val BINDING = "binding"
+  override val indexes: List[Document] =
+    List(Document(s"$NS" -> -1, s"$UPDATED" -> -1), Document(s"$ROOT_NS" -> 
-1, s"$UPDATED" -> -1))
+
+  override def filter(ddoc: String, view: String, startKey: List[Any], endKey: 
List[Any]): Bson = {

Review comment:
       it do filter results got from DB




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to