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
Changes from 4 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
@@ -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

Original file line number Diff line number Diff line change
@@ -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 {
121 changes: 89 additions & 32 deletions modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala
Original file line number Diff line number Diff line change
@@ -6,101 +6,158 @@ import zio.elasticsearch.ElasticRequest._
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()(implicit request: WithRefresh[ERT]): ElasticRequest[A, ERT] = request.withRefresh(self)
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved

final def routing(value: Routing): ElasticRequest[A, ERT] = self match {
case Map(request, mapper) => Map(request.routing(value), mapper)
case r: Create => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: CreateOrUpdate => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: DeleteById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: Exists => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: GetById => 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] =
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, ERT <: ElasticRequestType](
request: ElasticRequest[A, ERT],
mapper: A => B
) extends ElasticRequest[B, ERT]

trait WithRefresh[ERT <: ElasticRequestType] {
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
def withRefresh[A](request: ElasticRequest[A, ERT]): ElasticRequest[A, ERT]
}

object WithRefresh {
implicit val createWithRefresh: WithRefresh[CreateType] = new WithRefresh[CreateType] {
override def withRefresh[A](request: ElasticRequest[A, CreateType]): ElasticRequest[A, CreateType] =
request match {
case Map(r, mapper) => Map(withRefresh(r), mapper)
case r: Create => r.copy(refresh = true)
}
}
implicit val upsertWithRefresh: WithRefresh[UpsertType] = new WithRefresh[UpsertType] {
override def withRefresh[A](request: ElasticRequest[A, UpsertType]): ElasticRequest[A, UpsertType] =
request match {
case Map(r, mapper) => Map(withRefresh(r), mapper)
case r: CreateOrUpdate => r.copy(refresh = true)
}
}

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

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

object ElasticRequestType {
trait CreateIndexType extends ElasticRequestType
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
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
}
Original file line number Diff line number Diff line change
@@ -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)
@@ -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](
@@ -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))
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
.withParam("refresh", r.refresh.toString)

sendRequestWithCustomResponse(
request