Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(dsl): Support refresh parameter #33

Merged
merged 7 commits into from
Dec 27, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ final case class RepositoriesElasticsearch(executor: ElasticExecutor) {
def create(repository: GitHubRepo): Task[Option[DocumentId]] =
for {
routing <- routingOf(repository.organization)
req = ElasticRequest.create(Index, repository).routing(routing)
req = ElasticRequest.create(Index, repository).routing(routing).refreshTrue
res <- executor.execute(req)
} yield res

def upsert(id: String, repository: GitHubRepo): Task[Unit] =
for {
routing <- routingOf(repository.organization)
req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing)
req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing).refresh(value = true)
_ <- executor.execute(req)
} yield ()

def remove(organization: String, id: String): Task[Either[DocumentNotFound.type, Unit]] =
for {
routing <- routingOf(organization)
req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing)
req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refreshFalse
res <- executor.execute(req)
} yield res

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sttp.client3.SttpBackend
import zio.{Task, ZIO, ZLayer}

trait ElasticExecutor {
def execute[A](request: ElasticRequest[A]): Task[A]
def execute[A](request: ElasticRequest[A, _]): Task[A]
}

object ElasticExecutor {
Expand Down
134 changes: 86 additions & 48 deletions modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,142 @@ package zio.elasticsearch
import zio.elasticsearch.ElasticError.DocumentRetrievingError._
import zio.elasticsearch.ElasticError._
import zio.elasticsearch.ElasticRequest._
import zio.elasticsearch.Refresh.WithRefresh
import zio.schema.Schema
import zio.{RIO, ZIO}

sealed trait ElasticRequest[+A] { self =>
sealed trait ElasticRequest[+A, ERT <: ElasticRequestType] { self =>

final def execute: RIO[ElasticExecutor, A] =
ZIO.serviceWithZIO[ElasticExecutor](_.execute(self))

final def map[B](f: A => B): ElasticRequest[B] = ElasticRequest.Map(self, f)

final def routing(value: Routing): ElasticRequest[A] =
self match {
case Map(request, mapper) => Map(request.routing(value), mapper)
case r: Create => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case r: CreateOrUpdate => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case r: DeleteById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case r: Exists => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case r: GetById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case _ => self
}
final def map[B](f: A => B): ElasticRequest[B, ERT] = ElasticRequest.Map(self, f)

final def refresh(value: Boolean)(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] =
wr.withRefresh(request = self, value = value)

final def refreshFalse(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] =
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
wr.withRefresh(request = self, value = false)

final def refreshTrue(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] =
wr.withRefresh(request = self, value = true)

final def routing(value: Routing): ElasticRequest[A, ERT] = self match {
case Map(request, mapper) => Map(request.routing(value), mapper)
case r: CreateRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: CreateOrUpdateRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: DeleteByIdRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: ExistsRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: GetByIdRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case _ => self
}
}

object ElasticRequest {

def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] =
Create(index, Some(id), Document.from(doc)).map(_ => ())
import zio.elasticsearch.ElasticRequestType._

def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId]] =
Create(index, None, Document.from(doc))
def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, Create] =
CreateRequest(index, Some(id), Document.from(doc)).map(_ => ())

def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit] =
CreateIndex(name, definition)
def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId], Create] =
CreateRequest(index, None, Document.from(doc))

def deleteById(index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentNotFound.type, Unit]] =
DeleteById(index, id).map(deleted => if (deleted) Right(()) else Left(DocumentNotFound))
def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit, CreateIndex] =
CreateIndexRequest(name, definition)

def deleteIndex(name: IndexName): ElasticRequest[Boolean] =
DeleteIndex(name)
def deleteById(
index: IndexName,
id: DocumentId
): ElasticRequest[Either[DocumentNotFound.type, Unit], DeleteById] =
DeleteByIdRequest(index, id).map(deleted => if (deleted) Right(()) else Left(DocumentNotFound))

def deleteIndex(name: IndexName): ElasticRequest[Boolean, DeleteIndex] =
DeleteIndexRequest(name)

def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] =
Exists(index, id)
def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean, Exists] =
ExistsRequest(index, id)

def getById[A: Schema](index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentRetrievingError, A]] =
GetById(index, id).map {
def getById[A: Schema](
index: IndexName,
id: DocumentId
): ElasticRequest[Either[DocumentRetrievingError, A], GetById] =
GetByIdRequest(index, id).map {
case Some(document) => document.decode.left.map(err => DecoderError(err.message))
case None => Left(DocumentNotFound)
}

def search(index: IndexName, query: ElasticQuery): ElasticRequest[Option[ElasticQueryResponse]] =
GetByQuery(index, query)
def search(index: IndexName, query: ElasticQuery): ElasticRequest[Option[ElasticQueryResponse], GetByQuery] =
GetByQueryRequest(index, query)

def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] =
CreateOrUpdate(index, id, Document.from(doc))
def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, Upsert] =
CreateOrUpdateRequest(index, id, Document.from(doc))

private[elasticsearch] final case class Create(
private[elasticsearch] final case class CreateRequest(
index: IndexName,
id: Option[DocumentId],
document: Document,
refresh: Boolean = false,
routing: Option[Routing] = None
) extends ElasticRequest[Option[DocumentId]]
) extends ElasticRequest[Option[DocumentId], Create]

private[elasticsearch] final case class CreateIndex(
private[elasticsearch] final case class CreateIndexRequest(
name: IndexName,
definition: Option[String]
) extends ElasticRequest[Unit]
) extends ElasticRequest[Unit, CreateIndex]

private[elasticsearch] final case class CreateOrUpdate(
private[elasticsearch] final case class CreateOrUpdateRequest(
index: IndexName,
id: DocumentId,
document: Document,
refresh: Boolean = false,
routing: Option[Routing] = None
) extends ElasticRequest[Unit]
) extends ElasticRequest[Unit, Upsert]

private[elasticsearch] final case class DeleteById(
private[elasticsearch] final case class DeleteByIdRequest(
index: IndexName,
id: DocumentId,
refresh: Boolean = false,
routing: Option[Routing] = None
) extends ElasticRequest[Boolean]
) extends ElasticRequest[Boolean, DeleteById]

private[elasticsearch] final case class DeleteIndex(name: IndexName) extends ElasticRequest[Boolean]
private[elasticsearch] final case class DeleteIndexRequest(name: IndexName)
extends ElasticRequest[Boolean, DeleteIndex]

private[elasticsearch] final case class Exists(
private[elasticsearch] final case class ExistsRequest(
index: IndexName,
id: DocumentId,
routing: Option[Routing] = None
) extends ElasticRequest[Boolean]
) extends ElasticRequest[Boolean, Exists]

private[elasticsearch] final case class GetById(
private[elasticsearch] final case class GetByIdRequest(
index: IndexName,
id: DocumentId,
routing: Option[Routing] = None
) extends ElasticRequest[Option[Document]]
) extends ElasticRequest[Option[Document], GetById]

private[elasticsearch] final case class GetByQuery(
private[elasticsearch] final case class GetByQueryRequest(
index: IndexName,
query: ElasticQuery,
routing: Option[Routing] = None
) extends ElasticRequest[Option[ElasticQueryResponse]]
) extends ElasticRequest[Option[ElasticQueryResponse], GetByQuery]

private[elasticsearch] final case class Map[A, B, ERT <: ElasticRequestType](
request: ElasticRequest[A, ERT],
mapper: A => B
) extends ElasticRequest[B, ERT]

}

private[elasticsearch] final case class Map[A, B](request: ElasticRequest[A], mapper: A => B)
extends ElasticRequest[B]
sealed trait ElasticRequestType

object ElasticRequestType {
trait CreateIndex extends ElasticRequestType
trait Create extends ElasticRequestType
trait DeleteById extends ElasticRequestType
trait DeleteIndex extends ElasticRequestType
trait Exists extends ElasticRequestType
trait GetById extends ElasticRequestType
trait GetByQuery extends ElasticRequestType
trait Upsert extends ElasticRequestType
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC

import HttpElasticExecutor._

override def execute[A](request: ElasticRequest[A]): Task[A] =
override def execute[A](request: ElasticRequest[A, _]): Task[A] =
request match {
case r: Create => executeCreate(r)
case r: CreateIndex => executeCreateIndex(r)
case r: CreateOrUpdate => executeCreateOrUpdate(r)
case r: DeleteById => executeDeleteById(r)
case r: DeleteIndex => executeDeleteIndex(r)
case r: Exists => executeExists(r)
case r: GetById => executeGetById(r)
case r: GetByQuery => executeGetByQuery(r)
case map @ Map(_, _) => execute(map.request).map(map.mapper)
case r: CreateRequest => executeCreate(r)
case r: CreateIndexRequest => executeCreateIndex(r)
case r: CreateOrUpdateRequest => executeCreateOrUpdate(r)
case r: DeleteByIdRequest => executeDeleteById(r)
case r: DeleteIndexRequest => executeDeleteIndex(r)
case r: ExistsRequest => executeExists(r)
case r: GetByIdRequest => executeGetById(r)
case r: GetByQueryRequest => executeGetByQuery(r)
case map @ Map(_, _) => execute(map.request).map(map.mapper)
}

private def executeCreate(r: Create): Task[Option[DocumentId]] = {
private def executeCreate(r: CreateRequest): Task[Option[DocumentId]] = {
val uri = r.id match {
case Some(documentId) =>
uri"${config.uri}/${r.index}/$Create/$documentId".withParam("routing", r.routing.map(Routing.unwrap))
uri"${config.uri}/${r.index}/$Create/$documentId"
.withParam("routing", r.routing.map(Routing.unwrap))
.withParam("refresh", r.refresh.toString)
case None =>
uri"${config.uri}/${r.index}/$Doc".withParam("routing", r.routing.map(Routing.unwrap))
uri"${config.uri}/${r.index}/$Doc"
.withParam("routing", r.routing.map(Routing.unwrap))
.withParam("refresh", r.refresh.toString)
}

sendRequestWithCustomResponse[ElasticCreateResponse](
Expand All @@ -43,22 +47,26 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
).map(_.body.toOption).map(_.flatMap(body => DocumentId.make(body.id).toOption))
}

private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] =
private def executeCreateIndex(createIndex: CreateIndexRequest): Task[Unit] =
sendRequest(
request
.put(uri"${config.uri}/${createIndex.name}")
.contentType(ApplicationJson)
.body(createIndex.definition.getOrElse(""))
).unit

private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = {
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap))
private def executeCreateOrUpdate(r: CreateOrUpdateRequest): Task[Unit] = {
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}"
.withParam("routing", r.routing.map(Routing.unwrap))
.withParam("refresh", r.refresh.toString)

sendRequest(request.put(uri).contentType(ApplicationJson).body(r.document.json)).unit
}

private def executeDeleteById(r: DeleteById): Task[Boolean] = {
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap))
private def executeDeleteById(r: DeleteByIdRequest): Task[Boolean] = {
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}"
.withParam("routing", r.routing.map(Routing.unwrap))
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
.withParam("refresh", r.refresh.toString)

sendRequestWithCustomResponse(
request
Expand All @@ -67,16 +75,16 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
).map(_.body.toOption).map(_.exists(_.result == "deleted"))
}

private def executeDeleteIndex(r: DeleteIndex): Task[Boolean] =
private def executeDeleteIndex(r: DeleteIndexRequest): Task[Boolean] =
sendRequest(request.delete(uri"${config.uri}/${r.name}")).map(_.code.equals(Ok))

private def executeExists(r: Exists): Task[Boolean] = {
private def executeExists(r: ExistsRequest): Task[Boolean] = {
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap))

sendRequest(request.head(uri)).map(_.code.equals(Ok))
}

private def executeGetById(r: GetById): Task[Option[Document]] = {
private def executeGetById(r: GetByIdRequest): Task[Option[Document]] = {
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap))

sendRequestWithCustomResponse[ElasticGetResponse](
Expand All @@ -86,7 +94,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
).map(_.body.toOption).map(_.flatMap(d => if (d.found) Some(Document.from(d.source)) else None))
}

private def executeGetByQuery(r: GetByQuery): Task[Option[ElasticQueryResponse]] =
private def executeGetByQuery(r: GetByQueryRequest): Task[Option[ElasticQueryResponse]] =
sendRequestWithCustomResponse(
request
.post(uri"${config.uri}/${IndexName.unwrap(r.index)}/_search")
Expand Down
37 changes: 37 additions & 0 deletions modules/library/src/main/scala/zio/elasticsearch/Refresh.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package zio.elasticsearch

import zio.elasticsearch.ElasticRequest.{CreateOrUpdateRequest, CreateRequest, DeleteByIdRequest, Map}
import zio.elasticsearch.ElasticRequestType.{Create, DeleteById, Upsert}

object Refresh {

trait WithRefresh[ERT <: ElasticRequestType] {
def withRefresh[A](request: ElasticRequest[A, ERT], value: Boolean): ElasticRequest[A, ERT]
}

object WithRefresh {
implicit val createWithRefresh: WithRefresh[Create] = new WithRefresh[Create] {
def withRefresh[A](request: ElasticRequest[A, Create], value: Boolean): ElasticRequest[A, Create] =
request match {
case Map(r, mapper) => Map(withRefresh(r, value), mapper)
case r: CreateRequest => r.copy(refresh = value)
}
}
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved

implicit val deleteByIdWithRefresh: WithRefresh[DeleteById] = new WithRefresh[DeleteById] {
def withRefresh[A](request: ElasticRequest[A, DeleteById], value: Boolean): ElasticRequest[A, DeleteById] =
request match {
case Map(r, mapper) => Map(withRefresh(r, value), mapper)
case r: DeleteByIdRequest => r.copy(refresh = value)
}
}

implicit val upsertWithRefresh: WithRefresh[Upsert] = new WithRefresh[Upsert] {
def withRefresh[A](request: ElasticRequest[A, Upsert], value: Boolean): ElasticRequest[A, Upsert] =
request match {
case Map(r, mapper) => Map(withRefresh(r, value), mapper)
case r: CreateOrUpdateRequest => r.copy(refresh = value)
}
}
}
}