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 3 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).refresh
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
_ <- 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).refresh
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
120 changes: 88 additions & 32 deletions modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,101 +6,157 @@ import zio.elasticsearch.ElasticRequest._
import zio.schema.Schema
import zio.{RIO, ZIO}

sealed trait ElasticRequest[+A] { self =>
sealed trait ElasticRequestType
object ElasticRequestType {
trait CreateIndexType extends ElasticRequestType
trait CreateType extends ElasticRequestType
trait DeleteByIdType extends ElasticRequestType
trait DeleteIndexType extends ElasticRequestType
trait ExistsType extends ElasticRequestType
trait GetByIdType extends ElasticRequestType
trait GetByQueryType extends ElasticRequestType
trait UpsertType extends ElasticRequestType
}

sealed trait ElasticRequest[+A, RequestType <: 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, RequestType] = ElasticRequest.Map(self, f)

final def refresh(implicit addRefresh: AddRefresh[RequestType]): ElasticRequest[A, RequestType] =
addRefresh.addRefresh(self)

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

object ElasticRequest {

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

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

def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId]] =
def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId], CreateType] =
Create(index, None, Document.from(doc))

def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit] =
def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit, CreateIndexType] =
CreateIndex(name, definition)

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

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

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

def getById[A: Schema](index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentRetrievingError, A]] =
def getById[A: Schema](
index: IndexName,
id: DocumentId
): ElasticRequest[Either[DocumentRetrievingError, A], GetByIdType] =
GetById(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]] =
def search(index: IndexName, query: ElasticQuery): ElasticRequest[Option[ElasticQueryResponse], GetByQueryType] =
GetByQuery(index, query)

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

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

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

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

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

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

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

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

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

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

trait AddRefresh[T <: ElasticRequestType] {
def addRefresh[A](req: ElasticRequest[A, T]): ElasticRequest[A, T]
}

object AddRefresh {
implicit val addRefreshToCreate: AddRefresh[CreateType] = new AddRefresh[CreateType] {
override def addRefresh[A](req: ElasticRequest[A, CreateType]): ElasticRequest[A, CreateType] =
req match {
case Map(r, mapper) => Map(addRefresh(r), mapper)
case r: Create => r.copy(refresh = true)
}
}
implicit val addRefreshToUpsert: AddRefresh[UpsertType] = new AddRefresh[UpsertType] {
override def addRefresh[A](req: ElasticRequest[A, UpsertType]): ElasticRequest[A, UpsertType] =
req match {
case Map(r, mapper) => Map(addRefresh(r), mapper)
case r: CreateOrUpdate => r.copy(refresh = true)
}
}

private[elasticsearch] final case class Map[A, B](request: ElasticRequest[A], mapper: A => B)
extends ElasticRequest[B]
implicit val addRefreshToDeleteById: AddRefresh[DeleteByIdType] = new AddRefresh[DeleteByIdType] {
override def addRefresh[A](req: ElasticRequest[A, DeleteByIdType]): ElasticRequest[A, DeleteByIdType] =
req match {
case Map(r, mapper) => Map(addRefresh(r), mapper)
case r: DeleteById => r.copy(refresh = true)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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)
Expand All @@ -29,9 +29,13 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
private def executeCreate(r: Create): 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 @@ -52,13 +56,17 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
).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))
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))
val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}"
.withParam("routing", r.routing.map(Routing.unwrap))
.withParam("refresh", r.refresh.toString)

sendRequestWithCustomResponse(
request
Expand Down