markusthoemmes commented on a change in pull request #3155: Support ?count on
collections
URL:
https://github.com/apache/incubator-openwhisk/pull/3155#discussion_r161245524
##########
File path:
common/scala/src/main/scala/whisk/core/database/CouchDbRestStore.scala
##########
@@ -329,6 +329,44 @@ class CouchDbRestStore[DocumentAbstraction <:
DocumentSerializer](dbProtocol: St
transid.failed(this, start, s"[QUERY] '$dbName' internal error,
failure: '${failure.getMessage}'", ErrorLevel))
}
+ protected[core] def count(table: String, startKey: List[Any], endKey:
List[Any], skip: Int, stale: StaleParameter)(
+ implicit transid: TransactionId): Future[Long] = {
+
+ val (realStartKey, realEndKey) = (startKey, endKey)
+ val parts = table.split("/")
+
+ val start = transid.started(this, LoggingMarkers.DATABASE_QUERY, s"[COUNT]
'$dbName' searching '$table")
+
+ val f = for (eitherResponse <- client.executeView(parts(0), parts(1))(
+ startKey = realStartKey,
+ endKey = realEndKey,
+ skip = Some(skip),
+ stale = stale,
+ reduce = true))
+ yield
+ eitherResponse match {
+ case Right(response) =>
+ val rows = response.fields("rows").convertTo[List[JsObject]]
+
+ val out = if (!rows.isEmpty) {
+ assert(rows.length == 1, s"result of reduced view contains more
than one value: '$rows'")
+ rows.head.fields("value").convertTo[Long]
+ } else 0L
+
+ transid.finished(this, start, s"[COUNT] '$dbName' completed: count
$out")
+ out
+
+ case Left(code) =>
+ transid.failed(this, start, s"Unexpected http response code:
$code", ErrorLevel)
+ throw new Exception("Unexpected http response code: " + code)
Review comment:
I know the rest of the file does this differently, but would you mind adding
a `flatMap` above and use an explicit `Future.failed` here?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services