Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Update mango options #95

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ branches:
- 2.0.x

# start the couchdb service (runs in admin party)
services: couchdb
addons:
apt:
sources:
- sourceline: deb https://apache.bintray.com/couchdb-deb trusty main
key_url: https://couchdb.apache.org/repo/bintray-pubkey.asc
packages:
- couchdb

script:
- sbt clean coverage test coverageReport coverageAggregate
Expand Down
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ lazy val globalDependencies = Seq(
"com.typesafe.akka" %% "akka-stream-testkit" % "2.5.7",
"org.gnieh" %% "diffson-spray-json" % "2.2.3",
"io.spray" %% "spray-json" % "1.3.4",
"org.slf4j" % "slf4j-api" % "1.7.25"
"org.slf4j" % "slf4j-api" % "1.7.25",
"com.beachape" %% "enumeratum" % "1.5.12"
)

lazy val publishSettings = Seq(
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.0.2
sbt.version=1.0.3
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.1")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1")

addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.3.1")

addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.2")
16 changes: 14 additions & 2 deletions src/main/scala/gnieh/sohva/Database.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,20 @@ class Database private[sohva] (
*
* @group CouchDB2
*/
def find[T <: AnyRef: JsonReader](selector: Selector, fields: Iterable[String] = Nil, sort: Seq[Sort], limit: Option[Int] = None, skip: Option[Int] = None, use_index: Option[UseIndex] = None): Future[SearchResult[T]] =
find[T](Query(selector, fields, sort, limit, skip, use_index))
def find[T <: AnyRef: JsonReader](
selector: Selector,
fields: Iterable[String] = Nil,
sort: Seq[Sort],
limit: Option[Int] = None,
skip: Option[Int] = None,
use_index: Option[UseIndex] = None,
r: Option[Int],
bookmark: Option[String],
update: Option[Boolean] = None,
stable: Option[Boolean] = None,
stale: Option[Boolean] = None,
execution_stats: Option[Boolean] = None): Future[SearchResult[T]] =
find[T](Query(selector, fields, sort, limit, skip, use_index, r, bookmark, update, stable, stale, execution_stats))

/** Finds documents using the declarative mango query syntax. See [[sohva.mango]] for details.
*
Expand Down
10 changes: 7 additions & 3 deletions src/main/scala/gnieh/sohva/mango/Index.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ class Index(db: Database) {
import db.couch.ec

/** Creates a new index for the given fields and optional name and design. */
def create(fields: Vector[Sort], ddoc: Option[String] = None, name: Option[String] = None): Future[IndexCreationResult] = {
val jsFields = Seq("index" -> JsObject(Map("fields" -> fields.toJson))) ++ ddoc.map("ddoc" -> _.toJson) ++ name.map("name" -> _.toJson)
def create(
fields: Vector[Sort],
ddoc: Option[String] = None,
name: Option[String] = None,
partial_filter_selector: Option[Selector] = None): Future[IndexCreationResult] = {
val jsFields = Seq("index" -> JsObject(Map("fields" -> fields.toJson) ++ partial_filter_selector.map("partial_filter_selector" -> _.toJson))) ++ ddoc.map("ddoc" -> _.toJson) ++ name.map("name" -> _.toJson)
for {
entity <- Marshal(JsObject(jsFields: _*)).to[RequestEntity]
res <- db.couch.http(HttpRequest(HttpMethods.POST, uri = uri, entity = entity))
Expand Down Expand Up @@ -66,4 +70,4 @@ final case class IndexInfo(total_rows: Int, indexes: Vector[IndexDef])

final case class IndexDef(ddoc: Option[String], name: String, `type`: String, `def`: Def)

final case class Def(fields: Vector[Sort])
final case class Def(fields: Vector[Sort], partial_filter_selector: Option[Selector])
8 changes: 5 additions & 3 deletions src/main/scala/gnieh/sohva/mango/MangoProtocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ trait MangoProtocol extends DefaultJsonProtocol {
}
}

implicit val queryFormat = jsonFormat6(Query)
implicit val queryFormat = jsonFormat12(Query)

implicit val indexCreationResultFormat = jsonFormat3(IndexCreationResult)

implicit val defFormat = jsonFormat1(Def)
implicit val defFormat = jsonFormat2(Def)

implicit val indexDefFormat = jsonFormat4(IndexDef)

Expand All @@ -146,7 +146,9 @@ trait MangoProtocol extends DefaultJsonProtocol {

implicit val explanationFormat = jsonFormat8(Explanation)

implicit def searchResultFormat[T: JsonFormat] = jsonFormat2(SearchResult[T])
implicit val executionStatsFormat = jsonFormat5(ExecutionStats)

implicit def searchResultFormat[T: JsonFormat] = jsonFormat3(SearchResult[T])

}

Expand Down
50 changes: 49 additions & 1 deletion src/main/scala/gnieh/sohva/mango/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@ package mango

import spray.json._

final case class Query(selector: Selector, fields: Iterable[String], sort: Seq[Sort], limit: Option[Int], skip: Option[Int], use_index: Option[UseIndex]) {
final case class Query(
selector: Selector,
fields: Iterable[String],
sort: Seq[Sort],
limit: Option[Int],
skip: Option[Int],
use_index: Option[UseIndex],
r: Option[Int],
bookmark: Option[String],
update: Option[Boolean],
stable: Option[Boolean],
stale: Option[Boolean],
execution_stats: Option[Boolean]) {

/** Creates a query with a new selector. */
def where(sel: Selector): Query =
Expand Down Expand Up @@ -49,6 +61,30 @@ final case class Query(selector: Selector, fields: Iterable[String], sort: Seq[S
def use(idx: (String, String)): Query =
copy(use_index = Some(Right(idx)))

/** Creates a query with a new read quorum. */
def r(quorum: Int): Query =
copy(r = Some(quorum))

/** Creates a query with a new bookmark. */
def bookmark(b: String): Query =
copy(bookmark = Some(b))

/** Creates a query with a new update. */
def update(u: Boolean): Query =
copy(update = Some(u))

/** Creates a query with a new stable. */
def stable(s: Boolean): Query =
copy(stable = Some(s))

/** Creates a query with a new stale. */
def stale(s: Boolean): Query =
copy(stale = Some(s))

/** Creates a query with a new execution_stats. */
def execution_stats(s: Boolean): Query =
copy(execution_stats = Some(s))

/** Creates a query where some properties are removed. */
def without(without: Without*): Query =
without.foldLeft(this) {
Expand All @@ -62,6 +98,18 @@ final case class Query(selector: Selector, fields: Iterable[String], sort: Seq[S
q.copy(skip = None)
case (q, Without.Index) =>
q.copy(use_index = None)
case (q, Without.R) =>
q.copy(r = None)
case (q, Without.Bookmark) =>
q.copy(bookmark = None)
case (q, Without.Update) =>
q.copy(update = None)
case (q, Without.Stable) =>
q.copy(stable = None)
case (q, Without.Stale) =>
q.copy(stale = None)
case (q, Without.ExecutionStats) =>
q.copy(execution_stats = None)
}

}
9 changes: 8 additions & 1 deletion src/main/scala/gnieh/sohva/mango/SearchResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@
package gnieh.sohva
package mango

final case class SearchResult[T](docs: Vector[T], warning: Option[String])
final case class SearchResult[T](docs: Vector[T], warning: Option[String], execution_stats: Option[ExecutionStats])

final case class ExecutionStats(
total_keys_examined: Int,
total_docs_examined: Int,
total_quorum_docs_examined: Int,
results_returned: Int,
execution_time_ms: Double)
39 changes: 39 additions & 0 deletions src/main/scala/gnieh/sohva/mango/Without.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of the sohva project.
* Copyright (c) 2016 Lucas Satabin
*
* Licensed 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 gnieh.sohva

import enumeratum._

sealed trait Without extends EnumEntry

object Without extends Enum[Without] {

val values = findValues

object Fields extends Without
object Sort extends Without
object Limit extends Without
object Skip extends Without
object Index extends Without
object R extends Without
object Bookmark extends Without
object Update extends Without
object Stable extends Without
object Stale extends Without
object ExecutionStats extends Without

}
20 changes: 13 additions & 7 deletions src/main/scala/gnieh/sohva/mango/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ package object mango {

/** Creates an empty query. */
def find: Query =
Query(Empty, Nil, Nil, None, None, None)

object Without extends Enumeration {
val Fields, Sort, Limit, Skip, Index = Value
}

type Without = Without.Value
Query(Empty, Nil, Nil, None, None, None, None, None, None, None, None, None)

val fields = Without.Fields

Expand All @@ -64,4 +58,16 @@ package object mango {

val index = Without.Index

val r = Without.R

val bookmark = Without.Bookmark

val update = Without.Update

val stable = Without.Stable

val stale = Without.Stale

val execution_stats = Without.ExecutionStats

}