Skip to content

Commit

Permalink
Replace List with Chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
mvelimir committed May 12, 2023
1 parent fb90379 commit 01110b0
Show file tree
Hide file tree
Showing 23 changed files with 294 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,7 @@ object HttpExecutorSpec extends IntegrationSpec {
doc3 <- Executor.execute(ElasticRequest.getById(index, thirdDocumentId)).documentAs[TestDocument]
} yield assert(res.items.size)(equalTo(7)) &&
assert(res.items.map(_.error.isDefined))(
equalTo(List(false, false, false, false, false, false, true))
equalTo(Chunk(false, false, false, false, false, false, true))
) &&
assert(res.items(6).status)(equalTo(Some(404))) &&
assert(res.items(6).error.map(_.`type`))(equalTo(Some("document_missing_exception"))) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object ElasticAggregation {
* an instance of empty [[zio.elasticsearch.aggregation.MultipleAggregations]].
*/
final def multipleAggregations: MultipleAggregations =
Multiple(aggregations = Nil)
Multiple(aggregations = Chunk.empty)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.aggregation.TermsAggregation]] using the specified
Expand All @@ -73,7 +73,7 @@ object ElasticAggregation {
* performed.
*/
final def termsAggregation(name: String, field: Field[_, String]): TermsAggregation =
Terms(name = name, field = field.toString, order = Chunk.empty, subAggregations = Nil, size = None)
Terms(name = name, field = field.toString, order = Chunk.empty, subAggregations = Chunk.empty, size = None)

/**
* Constructs an instance of [[zio.elasticsearch.aggregation.TermsAggregation]] using the specified parameters.
Expand All @@ -87,7 +87,7 @@ object ElasticAggregation {
* performed.
*/
final def termsAggregation(name: String, field: String): TermsAggregation =
Terms(name = name, field = field, order = Chunk.empty, subAggregations = Nil, size = None)
Terms(name = name, field = field, order = Chunk.empty, subAggregations = Chunk.empty, size = None)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.aggregation.MaxAggregation]] using the specified parameters.
Expand Down
77 changes: 67 additions & 10 deletions modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.ElasticPrimitive.ElasticPrimitive
import zio.elasticsearch.query._
import zio.schema.Schema
Expand Down Expand Up @@ -93,7 +94,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def filter[S: Schema](queries: ElasticQuery[S]*): BoolQuery[S] =
Bool[S](filter = queries.toList, must = Nil, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)
Bool[S](
filter = Chunk.fromIterable(queries),
must = Chunk.empty,
mustNot = Chunk.empty,
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must satisfy the criteria using
Expand All @@ -106,7 +114,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def filter(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = queries.toList, must = Nil, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)
Bool[Any](
filter = Chunk.fromIterable(queries),
must = Chunk.empty,
mustNot = Chunk.empty,
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.GeoDistanceQuery]] using the specified parameters.
Expand Down Expand Up @@ -365,7 +380,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def must[S: Schema](queries: ElasticQuery[S]*): BoolQuery[S] =
Bool[S](filter = Nil, must = queries.toList, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)
Bool[S](
filter = Chunk.empty,
must = Chunk.fromIterable(queries),
mustNot = Chunk.empty,
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must satisfy the criteria using
Expand All @@ -378,7 +400,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def must(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = Nil, must = queries.toList, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)
Bool[Any](
filter = Chunk.empty,
must = Chunk.fromIterable(queries),
mustNot = Chunk.empty,
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must not satisfy the criteria
Expand All @@ -393,7 +422,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def mustNot[S: Schema](queries: ElasticQuery[S]*): BoolQuery[S] =
Bool[S](filter = Nil, must = Nil, mustNot = queries.toList, should = Nil, boost = None, minimumShouldMatch = None)
Bool[S](
filter = Chunk.empty,
must = Chunk.empty,
mustNot = Chunk.fromIterable(queries),
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must not satisfy the criteria
Expand All @@ -406,7 +442,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def mustNot(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = Nil, must = Nil, mustNot = queries.toList, should = Nil, boost = None, minimumShouldMatch = None)
Bool[Any](
filter = Chunk.empty,
must = Chunk.empty,
mustNot = Chunk.fromIterable(queries),
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.NestedQuery]] using the specified parameters.
Expand Down Expand Up @@ -479,7 +522,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def should[S: Schema](queries: ElasticQuery[S]*): BoolQuery[S] =
Bool[S](filter = Nil, must = Nil, mustNot = Nil, should = queries.toList, boost = None, minimumShouldMatch = None)
Bool[S](
filter = Chunk.empty,
must = Chunk.empty,
mustNot = Chunk.empty,
should = Chunk.fromIterable(queries),
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that should satisfy the criteria using
Expand All @@ -492,7 +542,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def should(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = Nil, must = Nil, mustNot = Nil, should = queries.toList, boost = None, minimumShouldMatch = None)
Bool[Any](
filter = Chunk.empty,
must = Chunk.empty,
mustNot = Chunk.empty,
should = Chunk.fromIterable(queries),
boost = None,
minimumShouldMatch = None
)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.WildcardQuery]] using the specified parameters.
Expand Down Expand Up @@ -574,7 +631,7 @@ object ElasticQuery {
* an instance of [[zio.elasticsearch.query.TermsQuery]] that represents the term query to be performed.
*/
final def terms[S](field: Field[S, String], values: String*): Terms[S] =
Terms(field = field.toString, values = values.toList, boost = None)
Terms(field = field.toString, values = Chunk.fromIterable(values), boost = None)

/**
* Constructs an instance of [[zio.elasticsearch.query.TermsQuery]] using the specified parameters.
Expand All @@ -590,7 +647,7 @@ object ElasticQuery {
* an instance of [[zio.elasticsearch.query.TermsQuery]] that represents the term query to be performed.
*/
final def terms(field: String, values: String*): Terms[Any] =
Terms(field = field, values = values.toList, boost = None)
Terms(field = field, values = Chunk.fromIterable(values), boost = None)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.WildcardQuery]] using the specified parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ object ElasticRequest {
* an instance of [[BulkRequest]] that represents the bulk operation to be performed.
*/
final def bulk(requests: BulkableRequest[_]*): BulkRequest =
Bulk.of(requests = requests: _*)
Bulk(requests = Chunk.fromIterable(requests), index = None, refresh = None, routing = None)

/**
* Constructs an instance of [[CountRequest]] for whole specified index.
Expand Down Expand Up @@ -368,7 +368,7 @@ object ElasticRequest {
with HasRouting[BulkRequest]

private[elasticsearch] final case class Bulk(
requests: List[BulkableRequest[_]],
requests: Chunk[BulkableRequest[_]],
index: Option[IndexName],
refresh: Option[Boolean],
routing: Option[Routing]
Expand All @@ -382,38 +382,33 @@ object ElasticRequest {
lazy val body: String = requests.flatMap { r =>
(r: @unchecked) match {
case Create(index, document, _, routing) =>
List(getActionAndMeta("create", List(("_index", Some(index)), ("routing", routing))), document.json)
Chunk(getActionAndMeta("create", Chunk(("_index", Some(index)), ("routing", routing))), document.json)
case CreateWithId(index, id, document, _, routing) =>
List(
getActionAndMeta("create", List(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))),
Chunk(
getActionAndMeta("create", Chunk(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))),
document.json
)
case CreateOrUpdate(index, id, document, _, routing) =>
List(
getActionAndMeta("index", List(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))),
Chunk(
getActionAndMeta("index", Chunk(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))),
document.json
)
case DeleteById(index, id, _, routing) =>
List(getActionAndMeta("delete", List(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))))
Chunk(getActionAndMeta("delete", Chunk(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))))
case Update(index, id, Some(document), _, routing, None, _) =>
List(
getActionAndMeta("update", List(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))),
Chunk(
getActionAndMeta("update", Chunk(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))),
Obj("doc" -> document.json)
)
case Update(index, id, None, _, routing, Some(script), _) =>
List(
getActionAndMeta("update", List(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))),
Chunk(
getActionAndMeta("update", Chunk(("_index", Some(index)), ("_id", Some(id)), ("routing", routing))),
Obj("script" -> script.toJson)
)
}
}.mkString(start = "", sep = "\n", end = "\n")
}

object Bulk {
def of(requests: BulkableRequest[_]*): Bulk =
Bulk(requests = requests.toList, index = None, refresh = None, routing = None)
}

sealed trait CountRequest extends ElasticRequest[Int] with HasRouting[CountRequest]

private[elasticsearch] final case class Count(
Expand Down Expand Up @@ -625,7 +620,7 @@ object ElasticRequest {
self.copy(size = Some(value))

def sort(sort: Sort, sorts: Sort*): SearchRequest =
self.copy(sortBy = sortBy ++ (sort :: sorts.toList))
self.copy(sortBy = sortBy ++ (sort +: sorts))

def toJson: Json = {
val fromJson: Json = self.from.fold(Obj())(f => Obj("from" -> f.toJson))
Expand All @@ -637,15 +632,15 @@ object ElasticRequest {
val searchAfterJson: Json = searchAfter.fold(Obj())(sa => Obj("search_after" -> sa))

val sortJson: Json =
if (self.sortBy.nonEmpty) Obj("sort" -> Arr(self.sortBy.map(_.paramsToJson): _*)) else Obj()
if (self.sortBy.nonEmpty) Obj("sort" -> Arr(self.sortBy.map(_.paramsToJson))) else Obj()

val sourceJson: Json =
(included, excluded) match {
case (None, None) => Obj()
case (included, excluded) =>
Obj("_source" -> {
val includes = included.fold(Obj())(included => Obj("includes" -> Arr(included.map(_.toJson): _*)))
val excludes = excluded.fold(Obj())(excluded => Obj("excludes" -> Arr(excluded.map(_.toJson): _*)))
val includes = included.fold(Obj())(included => Obj("includes" -> Arr(included.map(_.toJson))))
val excludes = excluded.fold(Obj())(excluded => Obj("excludes" -> Arr(excluded.map(_.toJson))))
includes merge excludes
})
}
Expand Down Expand Up @@ -706,7 +701,7 @@ object ElasticRequest {
self.copy(searchAfter = Some(value))

def sort(sort: Sort, sorts: Sort*): SearchAndAggregateRequest =
self.copy(sortBy = sortBy ++ (sort :: sorts.toList))
self.copy(sortBy = sortBy ++ (sort +: sorts))

def toJson: Json = {
val fromJson: Json = self.from.fold(Obj())(f => Obj("from" -> f.toJson))
Expand All @@ -718,15 +713,15 @@ object ElasticRequest {
val searchAfterJson: Json = searchAfter.fold(Obj())(sa => Obj("search_after" -> sa))

val sortJson: Json =
if (self.sortBy.nonEmpty) Obj("sort" -> Arr(self.sortBy.map(_.paramsToJson): _*)) else Obj()
if (self.sortBy.nonEmpty) Obj("sort" -> Arr(self.sortBy.map(_.paramsToJson))) else Obj()

val sourceJson: Json =
(included, excluded) match {
case (None, None) => Obj()
case (included, excluded) =>
Obj("_source" -> {
val includes = included.fold(Obj())(included => Obj("includes" -> Arr(included.map(_.toJson): _*)))
val excludes = excluded.fold(Obj())(excluded => Obj("excludes" -> Arr(excluded.map(_.toJson): _*)))
val includes = included.fold(Obj())(included => Obj("includes" -> Arr(included.map(_.toJson))))
val excludes = excluded.fold(Obj())(excluded => Obj("excludes" -> Arr(excluded.map(_.toJson))))
includes merge excludes
})
}
Expand Down Expand Up @@ -806,7 +801,7 @@ object ElasticRequest {
query.foldLeft(Obj("script" -> script.toJson))((scriptJson, q) => scriptJson merge q.toJson)
}

private def getActionAndMeta(requestType: String, parameters: List[(String, Any)]): String =
private def getActionAndMeta(requestType: String, parameters: Chunk[(String, Any)]): String =
parameters.collect { case (name, Some(value)) => s""""$name" : "$value"""" }
.mkString(s"""{ "$requestType" : { """, ", ", " } }")
}
5 changes: 3 additions & 2 deletions modules/library/src/main/scala/zio/elasticsearch/Field.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.ElasticPrimitive.ElasticPrimitive
import zio.schema.{AccessorBuilder, Schema}

Expand Down Expand Up @@ -60,12 +61,12 @@ private[elasticsearch] final case class Field[-S, +A](parent: Option[Field[S, _]

override def toString: String = {
@tailrec
def loop(field: Field[_, _], acc: List[String]): List[String] = field match {
def loop(field: Field[_, _], acc: Chunk[String]): Chunk[String] = field match {
case Field(None, name) => s"$name" +: acc
case Field(Some(parent), name) => loop(parent, s".$name" +: acc)
}

loop(self, Nil).mkString
loop(self, Chunk.empty).mkString
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ sealed trait MultipleAggregations extends ElasticAggregation with WithAgg {
def aggregations(aggregations: SingleElasticAggregation*): MultipleAggregations
}

private[elasticsearch] final case class Multiple(aggregations: List[SingleElasticAggregation])
private[elasticsearch] final case class Multiple(aggregations: Chunk[SingleElasticAggregation])
extends MultipleAggregations { self =>
def aggregations(aggregations: SingleElasticAggregation*): MultipleAggregations =
self.copy(aggregations = self.aggregations ++ aggregations)
Expand All @@ -151,11 +151,11 @@ private[elasticsearch] final case class Terms(
name: String,
field: String,
order: Chunk[AggregationOrder],
subAggregations: List[SingleElasticAggregation],
subAggregations: Chunk[SingleElasticAggregation],
size: Option[Int]
) extends TermsAggregation { self =>
def orderBy(order: AggregationOrder, orders: AggregationOrder*): TermsAggregation =
self.copy(order = self.order ++ (order :: orders.toList))
self.copy(order = self.order ++ (order +: orders))

def size(value: Int): TermsAggregation =
self.copy(size = Some(value))
Expand All @@ -174,9 +174,9 @@ private[elasticsearch] final case class Terms(
case o :: Nil =>
Obj("order" -> Obj(o.value -> o.order.toString.toJson))
case orders =>
Obj("order" -> Arr(orders.collect { case AggregationOrder(value, order) =>
Obj("order" -> Arr(Chunk.fromIterable(orders).collect { case AggregationOrder(value, order) =>
Obj(value -> order.toString.toJson)
}: _*))
}))
}

val sizeJson = size.fold(Obj())(s => Obj("size" -> s.toJson))
Expand Down
Loading

0 comments on commit 01110b0

Please sign in to comment.