Skip to content

Commit

Permalink
(executor): Fix deleteIndex and deleteById (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbulaja98 authored Dec 16, 2022
1 parent 4742581 commit 1715078
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zio.elasticsearch

import sttp.client3.httpclient.zio.HttpClientZioBackend
import sttp.client3.{SttpBackend, basicRequest}
import sttp.model.StatusCode.{NotFound, Ok}
import sttp.model.StatusCode.Ok
import zio.elasticsearch.ElasticConfig.Default
import zio.{Task, ZIO}
import zio.elasticsearch.ElasticError.DocumentRetrievingError.{DecoderError, DocumentNotFound}
Expand Down Expand Up @@ -153,16 +153,17 @@ object HttpExecutorSpec extends IntegrationSpec {
test("return true if deletion was successful") {
checkOnce(genIndexName) { name =>
val result = for {
_ <- ElasticRequest.deleteIndex(name).execute
sttp <- ZIO.service[SttpBackend[Task, Any]]
deleted <- basicRequest
.head(Default.uri.withPath(name.toString))
.send(sttp)
.map(_.code.equals(NotFound))
_ <- ElasticRequest.createIndex(name, None).execute
deleted <- ElasticRequest.deleteIndex(name).execute
} yield deleted

assertZIO(result)(isTrue)
}
},
test("return false if deletion was not successful") {
checkOnce(genIndexName) { name =>
assertZIO(ElasticRequest.deleteIndex(name).execute)(isFalse)
}
}
)
).provideShared(elasticsearchLayer, HttpClientZioBackend.layer()) @@ nondeterministic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object ElasticRequest {
Create(index, None, Document.from(doc))

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

def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] =
Exists(index, id)
Expand All @@ -50,7 +50,7 @@ object ElasticRequest {
def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit] =
CreateIndex(name, definition)

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

def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] =
Expand Down Expand Up @@ -79,9 +79,9 @@ object ElasticRequest {
index: IndexName,
id: DocumentId,
routing: Option[Routing] = None
) extends ElasticRequest[Option[Unit]]
) extends ElasticRequest[Boolean]

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

private[elasticsearch] final case class Exists(
index: IndexName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
sendRequest(request.head(uri)).map(_.code.equals(Ok))
}

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

private def executeDeleteById(r: DeleteById): Task[Option[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))

sendRequestWithCustomResponse(
request
.delete(uri)
.response(asJson[ElasticDeleteResponse])
).map(_.body.toOption).map(_.filter(_.result == "deleted").map(_ => ()))
).map(_.body.toOption).map(_.exists(_.result == "deleted"))
}

private def sendRequestWithCustomResponse[A](
Expand Down

0 comments on commit 1715078

Please sign in to comment.