From ed78046e20992c1c43be7367473d5bc8fd0b6d83 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Fri, 16 Dec 2022 12:32:29 +0100 Subject: [PATCH 1/4] Fix deleting index --- .../zio/elasticsearch/HttpExecutorSpec.scala | 15 ++++++++------- .../scala/zio/elasticsearch/ElasticRequest.scala | 4 ++-- .../zio/elasticsearch/HttpElasticExecutor.scala | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala index 6b136fbc9..ea0b50546 100644 --- a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -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} @@ -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 diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index bf33c86b7..bac98c249 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -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] = @@ -81,7 +81,7 @@ object ElasticRequest { routing: Option[Routing] = None ) extends ElasticRequest[Option[Unit]] - 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, diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 608919ea7..f47ae9b67 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -73,8 +73,8 @@ 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]] = { val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) From b9b4ee2633c187b78878b103ad78f79889d5c659 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Fri, 16 Dec 2022 13:15:31 +0100 Subject: [PATCH 2/4] Refactor deleting by id --- .../scala/zio/elasticsearch/ElasticRequest.scala | 13 +++++-------- .../zio/elasticsearch/HttpElasticExecutor.scala | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index bac98c249..b1c283242 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -33,10 +33,9 @@ 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(a => if (a) Right(()) else 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 { @@ -47,11 +46,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) - def deleteIndex(name: IndexName): ElasticRequest[Boolean] = - DeleteIndex(name) + def deleteIndex(name: IndexName): ElasticRequest[Boolean] = DeleteIndex(name) def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] = CreateOrUpdate(index, id, Document.from(doc)) @@ -79,7 +76,7 @@ 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[Boolean] diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index f47ae9b67..82b1dcfc2 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -76,14 +76,14 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC 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]( From 3bce109017a2bae175d0290aed1f7943ddcd7900 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Fri, 16 Dec 2022 13:28:37 +0100 Subject: [PATCH 3/4] Refactor ElasticRequest --- .../src/main/scala/zio/elasticsearch/ElasticRequest.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index b1c283242..e2dc367c7 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -33,7 +33,10 @@ object ElasticRequest { Create(index, None, Document.from(doc)) def deleteById(index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentNotFound.type, Unit]] = - DeleteById(index, id).map(a => if (a) Right(()) else Left(DocumentNotFound)) + DeleteById(index, id).map { + case true => Right(()) + case false => Left(DocumentNotFound) + } def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] = Exists(index, id) From c001779acfb549b8b9a466513ed04a451acb131e Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Fri, 16 Dec 2022 13:46:19 +0100 Subject: [PATCH 4/4] Fix code remarks --- .../scala/zio/elasticsearch/ElasticRequest.scala | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index e2dc367c7..5b8f9409c 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -33,12 +33,10 @@ object ElasticRequest { Create(index, None, Document.from(doc)) def deleteById(index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentNotFound.type, Unit]] = - DeleteById(index, id).map { - case true => Right(()) - case false => Left(DocumentNotFound) - } + DeleteById(index, id).map(deleted => if (deleted) Right(()) else 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 { @@ -49,9 +47,11 @@ 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) - def deleteIndex(name: IndexName): ElasticRequest[Boolean] = DeleteIndex(name) + def deleteIndex(name: IndexName): ElasticRequest[Boolean] = + DeleteIndex(name) def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] = CreateOrUpdate(index, id, Document.from(doc))