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

Use module pattern 2.0 (move requests to executor) #92

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions modules/example/src/main/scala/example/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import sttp.client3.SttpBackend
import sttp.client3.httpclient.zio.HttpClientZioBackend
import zio._
import zio.config.getConfig
import zio.elasticsearch.{ElasticConfig, ElasticExecutor, ElasticRequest}
import zio.elasticsearch.{ElasticConfig, ElasticExecutor}
import zio.http.{Server, ServerConfig}

import scala.io.Source
Expand All @@ -46,14 +46,14 @@ object Main extends ZIOAppDefault {
val deleteIndex: RIO[ElasticExecutor, Unit] =
for {
_ <- ZIO.logInfo(s"Deleting index '$Index'...")
_ <- ElasticRequest.deleteIndex(Index).execute
_ <- ZIO.serviceWithZIO[ElasticExecutor](_.deleteIndex(Index).execute)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great not having needing the call to execute as it makes the API usage awkward.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only idea that comes to mind is an implicit conversion from ElasticRequest to ZIO.

} yield ()

val createIndex: RIO[ElasticExecutor, Unit] =
for {
_ <- ZIO.logInfo(s"Creating index '$Index'...")
mapping <- ZIO.fromTry(Using(Source.fromURL(getClass.getResource("/mapping.json")))(_.mkString))
_ <- ElasticRequest.createIndex(Index, Some(mapping)).execute
_ <- ZIO.serviceWithZIO[ElasticExecutor](_.createIndex(Index, Some(mapping)).execute)
} yield ()

val populate: RIO[SttpBackend[Task, Any] with ElasticExecutor, Unit] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,49 @@ package example

import zio._
import zio.elasticsearch.ElasticQuery.matchAll
import zio.elasticsearch.{
CreationOutcome,
DeletionOutcome,
DocumentId,
ElasticExecutor,
ElasticQuery,
ElasticRequest,
Routing
}
import zio.elasticsearch.{CreationOutcome, DeletionOutcome, DocumentId, ElasticExecutor, ElasticQuery, Routing}
import zio.prelude.Newtype.unsafeWrap

final case class RepositoriesElasticsearch(executor: ElasticExecutor) {

def findAll(): Task[List[GitHubRepo]] =
executor.execute(ElasticRequest.search[GitHubRepo](Index, matchAll))
executor.search[GitHubRepo](Index, matchAll).execute

def findById(organization: String, id: String): Task[Option[GitHubRepo]] =
for {
routing <- routingOf(organization)
req = ElasticRequest.getById[GitHubRepo](Index, DocumentId(id)).routing(routing)
res <- executor.execute(req)
res <- executor.getById[GitHubRepo](Index, DocumentId(id)).routing(routing).execute
} yield res

def create(repository: GitHubRepo): Task[CreationOutcome] =
for {
routing <- routingOf(repository.organization)
req = ElasticRequest.create(Index, DocumentId(repository.id), repository).routing(routing).refreshTrue
res <- executor.execute(req)
res <- executor.create(Index, DocumentId(repository.id), repository).routing(routing).refreshTrue.execute
} yield res

def createAll(repositories: List[GitHubRepo]): Task[Unit] =
for {
routing <- routingOf(organization)
reqs = repositories.map { repository =>
ElasticRequest.create[GitHubRepo](Index, unsafeWrap(DocumentId)(repository.id), repository)
executor.create[GitHubRepo](Index, unsafeWrap(DocumentId)(repository.id), repository)
}
bulkReq = ElasticRequest.bulk(reqs: _*).routing(routing)
_ <- executor.execute(bulkReq)
_ <- executor.bulk(reqs: _*).routing(routing).execute
} yield ()

def upsert(id: String, repository: GitHubRepo): Task[Unit] =
for {
routing <- routingOf(repository.organization)
req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing).refresh(value = true)
_ <- executor.execute(req)
_ <- executor.upsert(Index, DocumentId(id), repository).routing(routing).refresh(value = true).execute
} yield ()

def remove(organization: String, id: String): Task[DeletionOutcome] =
for {
routing <- routingOf(organization)
req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refreshFalse
res <- executor.execute(req)
res <- executor.deleteById(Index, DocumentId(id)).routing(routing).refreshFalse.execute
} yield res

def search(query: ElasticQuery[_]): Task[List[GitHubRepo]] =
executor.execute(ElasticRequest.search[GitHubRepo](Index, query))
executor.search[GitHubRepo](Index, query).execute

private def routingOf(value: String): IO[IllegalArgumentException, Routing.Type] =
Routing.make(value).toZIO.mapError(e => new IllegalArgumentException(e))
Expand Down
174 changes: 100 additions & 74 deletions modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ trait IntegrationSpec extends ZIOSpecDefault {
val createIndexTestName: IndexName = IndexName("create-index-test-name")

val prepareElasticsearchIndexForTests: TestAspect[Nothing, Any, Throwable, Any] = beforeAll((for {
_ <- ElasticRequest.createIndex(index, None).execute
_ <- ElasticRequest.deleteByQuery(index, matchAll).refreshTrue.execute
executor <- ZIO.service[ElasticExecutor]
_ <- executor.createIndex(index, None).execute
_ <- executor.deleteByQuery(index, matchAll).refreshTrue.execute
} yield ()).provide(elasticsearchLayer))

def genIndexName: Gen[Any, IndexName] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,36 @@
package zio.elasticsearch

import sttp.client3.SttpBackend
import zio.stm.TMap
import zio.{Task, ULayer, ZLayer}
import zio.elasticsearch.ElasticRequest.BulkableRequest
import zio.elasticsearch.ElasticRequestType._
import zio.schema.Schema
import zio.{Task, ZLayer}

trait ElasticExecutor {
def execute[A](request: ElasticRequest[A, _]): Task[A]

def bulk(requests: BulkableRequest*): ElasticRequest[Unit, Bulk]

def create[A: Schema](index: IndexName, doc: A): ElasticRequest[DocumentId, Create]

def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[CreationOutcome, CreateWithId]

def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[CreationOutcome, CreateIndex]

def deleteById(index: IndexName, id: DocumentId): ElasticRequest[DeletionOutcome, DeleteById]

def deleteByQuery(index: IndexName, query: ElasticQuery[_]): ElasticRequest[DeletionOutcome, DeleteByQuery]

def deleteIndex(name: IndexName): ElasticRequest[DeletionOutcome, DeleteIndex]

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

def getById[A: Schema](index: IndexName, id: DocumentId): ElasticRequest[Option[A], GetById]

def search[A](index: IndexName, query: ElasticQuery[_])(implicit
schema: Schema[A]
): ElasticRequest[List[A], GetByQuery]

def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, Upsert]
}

object ElasticExecutor {
Expand All @@ -30,7 +55,4 @@ object ElasticExecutor {

lazy val local: ZLayer[SttpBackend[Task, Any], Throwable, ElasticExecutor] =
ZLayer.succeed(ElasticConfig.Default) >>> live

lazy val test: ULayer[TestExecutor] =
ZLayer(TMap.empty[IndexName, TMap[DocumentId, Document]].map(TestExecutor).commit)
}
Loading