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

(executor): Fix deleteIndex and deleteById #26

Merged
merged 4 commits into from
Dec 16, 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 @@ -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,10 +33,12 @@ 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 {
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
case true => Right(())
case false => Left(DocumentNotFound)
}

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

def getById[A: Schema](index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentRetrievingError, A]] =
GetById(index, id).map {
Expand All @@ -47,11 +49,9 @@ object ElasticRequest {
def search(index: IndexName, query: ElasticQuery): ElasticRequest[Option[ElasticQueryResponse]] =
GetByQuery(index, query)

def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit] =
CreateIndex(name, definition)
def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit] = CreateIndex(name, definition)
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved

def deleteIndex(name: IndexName): ElasticRequest[Unit] =
DeleteIndex(name)
def deleteIndex(name: IndexName): ElasticRequest[Boolean] = DeleteIndex(name)
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved

def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] =
CreateOrUpdate(index, id, Document.from(doc))
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