diff --git a/modules/redis/src/main/scala/zio/redis/GenRedis.scala b/modules/redis/src/main/scala/zio/redis/GenRedis.scala new file mode 100644 index 000000000..eae1cf839 --- /dev/null +++ b/modules/redis/src/main/scala/zio/redis/GenRedis.scala @@ -0,0 +1,25 @@ +package zio.redis +import zio.{IO, UIO} + +private[redis] trait GenRedis[G[+_]] + extends api.Connection[G] + with api.Geo[G] + with api.Hashes[G] + with api.HyperLogLog[G] + with api.Keys[G] + with api.Lists[G] + with api.Sets[G] + with api.Strings[G] + with api.SortedSets[G] + with api.Streams[G] + with api.Scripting[G] + with api.Cluster[G] + with api.Publishing[G] + +private[redis] object GenRedis { + type Async[+A] = UIO[IO[RedisError, A]] + type Sync[+A] = IO[RedisError, A] + + def async[A](io: UIO[IO[RedisError, A]]) = io + def sync[A](io: UIO[IO[RedisError, A]]) = io.flatten +} diff --git a/modules/redis/src/main/scala/zio/redis/Redis.scala b/modules/redis/src/main/scala/zio/redis/Redis.scala index a04bd9e50..6342efaa8 100644 --- a/modules/redis/src/main/scala/zio/redis/Redis.scala +++ b/modules/redis/src/main/scala/zio/redis/Redis.scala @@ -19,38 +19,31 @@ package zio.redis import zio._ import zio.redis.internal._ -trait Redis - extends api.Connection - with api.Geo - with api.Hashes - with api.HyperLogLog - with api.Keys - with api.Lists - with api.Sets - with api.Strings - with api.SortedSets - with api.Streams - with api.Scripting - with api.Cluster - with api.Publishing - object Redis { lazy val cluster: ZLayer[CodecSupplier & RedisClusterConfig, RedisError, Redis] = - ClusterExecutor.layer >>> makeLayer + ZLayer.makeSome[CodecSupplier & RedisClusterConfig, Redis](ClusterExecutor.layer, makeLayer) - lazy val local: ZLayer[CodecSupplier, RedisError.IOError, Redis] = + lazy val local: ZLayer[CodecSupplier, RedisError.IOError, Redis & AsyncRedis] = SingleNodeExecutor.local >>> makeLayer - lazy val singleNode: ZLayer[CodecSupplier & RedisConfig, RedisError.IOError, Redis] = + lazy val singleNode: ZLayer[CodecSupplier & RedisConfig, RedisError.IOError, Redis & AsyncRedis] = SingleNodeExecutor.layer >>> makeLayer - - private def makeLayer: URLayer[CodecSupplier & RedisExecutor, Redis] = - ZLayer { + private def makeLayer: URLayer[CodecSupplier & RedisExecutor, AsyncRedis & Redis] = + ZLayer.fromZIOEnvironment { for { codecSupplier <- ZIO.service[CodecSupplier] executor <- ZIO.service[RedisExecutor] - } yield new Live(codecSupplier, executor) + } yield ZEnvironment[AsyncRedis, Redis]( + new AsyncLive(codecSupplier, executor), + new SyncLive(codecSupplier, executor) + ) } - private final class Live(val codecSupplier: CodecSupplier, val executor: RedisExecutor) extends Redis + private final class SyncLive(val codecSupplier: CodecSupplier, val executor: RedisExecutor) extends Redis { + protected def lift[A](in: UIO[IO[RedisError, A]]): GenRedis.Sync[A] = GenRedis.sync(in) + } + + private final class AsyncLive(val codecSupplier: CodecSupplier, val executor: RedisExecutor) extends AsyncRedis { + protected def lift[A](in: UIO[IO[RedisError, A]]): GenRedis.Async[A] = GenRedis.async(in) + } } diff --git a/modules/redis/src/main/scala/zio/redis/ResultBuilder.scala b/modules/redis/src/main/scala/zio/redis/ResultBuilder.scala index ab76e0f12..90d5cc977 100644 --- a/modules/redis/src/main/scala/zio/redis/ResultBuilder.scala +++ b/modules/redis/src/main/scala/zio/redis/ResultBuilder.scala @@ -31,20 +31,20 @@ object ResultBuilder { @annotation.implicitNotFound("Use `returning[A]` to specify method's return type") final abstract class NeedsReturnType - trait ResultBuilder1[+F[_]] extends ResultBuilder { - def returning[R: Schema]: IO[RedisError, F[R]] + trait ResultBuilder1[+F[_], G[+_]] extends ResultBuilder { + def returning[R: Schema]: G[F[R]] } - trait ResultBuilder2[+F[_, _]] extends ResultBuilder { - def returning[R1: Schema, R2: Schema]: IO[RedisError, F[R1, R2]] + trait ResultBuilder2[+F[_, _], G[+_]] extends ResultBuilder { + def returning[R1: Schema, R2: Schema]: G[F[R1, R2]] } - trait ResultBuilder3[+F[_, _, _]] extends ResultBuilder { - def returning[R1: Schema, R2: Schema, R3: Schema]: IO[RedisError, F[R1, R2, R3]] + trait ResultBuilder3[+F[_, _, _], G[+_]] extends ResultBuilder { + def returning[R1: Schema, R2: Schema, R3: Schema]: G[F[R1, R2, R3]] } - trait ResultOutputBuilder extends ResultBuilder { - def returning[R: Output]: IO[RedisError, R] + trait ResultOutputBuilder[G[+_]] extends ResultBuilder { + def returning[R: Output]: G[R] } trait ResultStreamBuilder1[+F[_]] extends ResultBuilder { diff --git a/modules/redis/src/main/scala/zio/redis/api/Cluster.scala b/modules/redis/src/main/scala/zio/redis/api/Cluster.scala index 70db29415..b35924cc5 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Cluster.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Cluster.scala @@ -16,16 +16,15 @@ package zio.redis.api +import zio.Chunk import zio.redis.Input._ import zio.redis.Output.{ChunkOutput, ClusterPartitionOutput, UnitOutput} -import zio.redis._ -import zio.redis.api.Cluster.{AskingCommand, ClusterSetSlots, ClusterSlots} -import zio.redis.internal.{RedisCommand, RedisEnvironment, RedisExecutor} +import zio.redis.api.Cluster.{ClusterSetSlots, ClusterSlots, askingCommand} +import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.redis.options.Cluster.SetSlotSubCommand._ import zio.redis.options.Cluster.{Partition, Slot} -import zio.{Chunk, IO} -trait Cluster extends RedisEnvironment { +trait Cluster[G[+_]] extends RedisEnvironment[G] { /** * When a cluster client receives an -ASK redirect, the ASKING command is sent to the target node followed by the @@ -34,8 +33,8 @@ trait Cluster extends RedisEnvironment { * @return * the Unit value. */ - final def asking: IO[RedisError, Unit] = - AskingCommand(executor).run(()) + final def asking: G[Unit] = + askingCommand.run(()) /** * Set a hash slot in importing state. Command should be executed on the node where hash slot will be migrated @@ -48,12 +47,11 @@ trait Cluster extends RedisEnvironment { * @return * the Unit value. */ - final def setSlotImporting(slot: Slot, nodeId: String): IO[RedisError, Unit] = { + final def setSlotImporting(slot: Slot, nodeId: String): G[Unit] = { val command = RedisCommand( ClusterSetSlots, Tuple3(LongInput, ArbitraryValueInput[String](), ArbitraryValueInput[String]()), - UnitOutput, - executor + UnitOutput ) command.run((slot.number, Importing.asString, nodeId)) } @@ -69,12 +67,11 @@ trait Cluster extends RedisEnvironment { * @return * the Unit value. */ - final def setSlotMigrating(slot: Slot, nodeId: String): IO[RedisError, Unit] = { + final def setSlotMigrating(slot: Slot, nodeId: String): G[Unit] = { val command = RedisCommand( ClusterSetSlots, Tuple3(LongInput, ArbitraryValueInput[String](), ArbitraryValueInput[String]()), - UnitOutput, - executor + UnitOutput ) command.run((slot.number, Migrating.asString, nodeId)) } @@ -90,12 +87,11 @@ trait Cluster extends RedisEnvironment { * @return * the Unit value. */ - final def setSlotNode(slot: Slot, nodeId: String): IO[RedisError, Unit] = { + final def setSlotNode(slot: Slot, nodeId: String): G[Unit] = { val command = RedisCommand( ClusterSetSlots, Tuple3(LongInput, ArbitraryValueInput[String](), ArbitraryValueInput[String]()), - UnitOutput, - executor + UnitOutput ) command.run((slot.number, Node.asString, nodeId)) } @@ -108,9 +104,9 @@ trait Cluster extends RedisEnvironment { * @return * the Unit value. */ - final def setSlotStable(slot: Slot): IO[RedisError, Unit] = { + final def setSlotStable(slot: Slot): G[Unit] = { val command = - RedisCommand(ClusterSetSlots, Tuple2(LongInput, ArbitraryValueInput[String]()), UnitOutput, executor) + RedisCommand(ClusterSetSlots, Tuple2(LongInput, ArbitraryValueInput[String]()), UnitOutput) command.run((slot.number, Stable.asString)) } @@ -120,8 +116,8 @@ trait Cluster extends RedisEnvironment { * @return * details about which cluster */ - final def slots: IO[RedisError, Chunk[Partition]] = { - val command = RedisCommand(ClusterSlots, NoInput, ChunkOutput(ClusterPartitionOutput), executor) + final def slots: G[Chunk[Partition]] = { + val command = RedisCommand(ClusterSlots, NoInput, ChunkOutput(ClusterPartitionOutput)) command.run(()) } } @@ -131,6 +127,6 @@ private[redis] object Cluster { final val ClusterSetSlots = "CLUSTER SETSLOT" final val ClusterSlots = "CLUSTER SLOTS" - final val AskingCommand: RedisExecutor => RedisCommand[Unit, Unit] = - (executor: RedisExecutor) => RedisCommand(Asking, NoInput, UnitOutput, executor) + final val askingCommand: RedisCommand[Unit, Unit] = + RedisCommand(Asking, NoInput, UnitOutput) } diff --git a/modules/redis/src/main/scala/zio/redis/api/Connection.scala b/modules/redis/src/main/scala/zio/redis/api/Connection.scala index 8a6992bfb..501154ba5 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Connection.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Connection.scala @@ -16,13 +16,12 @@ package zio.redis.api -import zio._ import zio.redis.Input._ import zio.redis.Output._ import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} -trait Connection extends RedisEnvironment { +trait Connection[G[+_]] extends RedisEnvironment[G] { import Connection.{Auth => _, _} /** @@ -37,8 +36,8 @@ trait Connection extends RedisEnvironment { * if the password provided via AUTH matches the password in the configuration file, the Unit value is returned and * the server starts accepting commands. Otherwise, an error is returned and the client needs to try a new password. */ - final def auth(password: String): IO[RedisError, Unit] = { - val command = RedisCommand(Connection.Auth, AuthInput, UnitOutput, executor) + final def auth(password: String): G[Unit] = { + val command = RedisCommand(Connection.Auth, AuthInput, UnitOutput) command.run(Auth(None, password)) } @@ -54,8 +53,8 @@ trait Connection extends RedisEnvironment { * if the password provided via AUTH matches the password in the configuration file, the Unit value is returned and * the server starts accepting commands. Otherwise, an error is returned and the client needs to try a new password. */ - final def auth(username: String, password: String): IO[RedisError, Unit] = { - val command = RedisCommand(Connection.Auth, AuthInput, UnitOutput, executor) + final def auth(username: String, password: String): G[Unit] = { + val command = RedisCommand(Connection.Auth, AuthInput, UnitOutput) command.run(Auth(Some(username), password)) } @@ -66,8 +65,8 @@ trait Connection extends RedisEnvironment { * @return * the connection name, or None if a name wasn't set. */ - final def clientGetName: IO[RedisError, Option[String]] = { - val command = RedisCommand(ClientGetName, NoInput, OptionalOutput(MultiStringOutput), executor) + final def clientGetName: G[Option[String]] = { + val command = RedisCommand(ClientGetName, NoInput, OptionalOutput(MultiStringOutput)) command.run(()) } @@ -82,8 +81,8 @@ trait Connection extends RedisEnvironment { * @return * the ID of the current connection. */ - final def clientId: IO[RedisError, Long] = { - val command = RedisCommand(ClientId, NoInput, LongOutput, executor) + final def clientId: G[Long] = { + val command = RedisCommand(ClientId, NoInput, LongOutput) command.run(()) } @@ -96,8 +95,8 @@ trait Connection extends RedisEnvironment { * @return * the Unit value. */ - final def clientSetName(name: String): IO[RedisError, Unit] = { - val command = RedisCommand(ClientSetName, StringInput, UnitOutput, executor) + final def clientSetName(name: String): G[Unit] = { + val command = RedisCommand(ClientSetName, StringInput, UnitOutput) command.run(name) } @@ -112,8 +111,8 @@ trait Connection extends RedisEnvironment { * @return * the Unit value. */ - final def select(index: Long): IO[RedisError, Unit] = { - val command = RedisCommand(Select, LongInput, UnitOutput, executor) + final def select(index: Long): G[Unit] = { + val command = RedisCommand(Select, LongInput, UnitOutput) command.run(index) } diff --git a/modules/redis/src/main/scala/zio/redis/api/Geo.scala b/modules/redis/src/main/scala/zio/redis/api/Geo.scala index 83f561215..65b5f335a 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Geo.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Geo.scala @@ -23,7 +23,7 @@ import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.schema.Schema -trait Geo extends RedisEnvironment { +trait Geo[G[+_]] extends RedisEnvironment[G] { import Geo._ /** @@ -42,12 +42,11 @@ trait Geo extends RedisEnvironment { key: K, item: (LongLat, M), items: (LongLat, M)* - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( GeoAdd, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(Tuple2(LongLatInput, ArbitraryValueInput[M]()))), - LongOutput, - executor + LongOutput ) command.run((key, (item, items.toList))) } @@ -71,7 +70,7 @@ trait Geo extends RedisEnvironment { member1: M, member2: M, radiusUnit: Option[RadiusUnit] = None - ): IO[RedisError, Option[Double]] = { + ): G[Option[Double]] = { val command = RedisCommand( GeoDist, Tuple4( @@ -80,8 +79,7 @@ trait Geo extends RedisEnvironment { ArbitraryValueInput[M](), OptionalInput(RadiusUnitInput) ), - OptionalOutput(DoubleOutput), - executor + OptionalOutput(DoubleOutput) ) command.run((key, member1, member2, radiusUnit)) } @@ -103,12 +101,11 @@ trait Geo extends RedisEnvironment { key: K, member: M, members: M* - ): IO[RedisError, Chunk[Option[String]]] = { + ): G[Chunk[Option[String]]] = { val command = RedisCommand( GeoHash, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), - ChunkOutput(OptionalOutput(MultiStringOutput)), - executor + ChunkOutput(OptionalOutput(MultiStringOutput)) ) command.run((key, (member, members.toList))) } @@ -130,9 +127,9 @@ trait Geo extends RedisEnvironment { key: K, member: M, members: M* - ): IO[RedisError, Chunk[Option[LongLat]]] = { + ): G[Chunk[Option[LongLat]]] = { val command = - RedisCommand(GeoPos, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), GeoOutput, executor) + RedisCommand(GeoPos, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), GeoOutput) command.run((key, (member, members.toList))) } @@ -171,7 +168,7 @@ trait Geo extends RedisEnvironment { withHash: Option[WithHash] = None, count: Option[Count] = None, order: Option[Order] = None - ): IO[RedisError, Chunk[GeoView]] = { + ): G[Chunk[GeoView]] = { val command = RedisCommand( GeoRadius, Tuple9( @@ -185,8 +182,7 @@ trait Geo extends RedisEnvironment { OptionalInput(CountInput), OptionalInput(OrderInput) ), - GeoRadiusOutput, - executor + GeoRadiusOutput ) command.run((key, center, radius, radiusUnit, withCoord, withDist, withHash, count, order)) } @@ -233,7 +229,7 @@ trait Geo extends RedisEnvironment { withHash: Option[WithHash] = None, count: Option[Count] = None, order: Option[Order] = None - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( GeoRadius, Tuple11( @@ -249,8 +245,7 @@ trait Geo extends RedisEnvironment { OptionalInput(StoreInput), OptionalInput(StoreDistInput) ), - LongOutput, - executor + LongOutput ) command.run( (key, center, radius, radiusUnit, withCoord, withDist, withHash, count, order, store.store, store.storeDist) @@ -292,7 +287,7 @@ trait Geo extends RedisEnvironment { withHash: Option[WithHash] = None, count: Option[Count] = None, order: Option[Order] = None - ): IO[RedisError, Chunk[GeoView]] = { + ): G[Chunk[GeoView]] = { val command = RedisCommand( GeoRadiusByMember, Tuple9( @@ -306,8 +301,7 @@ trait Geo extends RedisEnvironment { OptionalInput(CountInput), OptionalInput(OrderInput) ), - GeoRadiusOutput, - executor + GeoRadiusOutput ) command.run((key, member, radius, radiusUnit, withCoord, withDist, withHash, count, order)) } @@ -354,7 +348,7 @@ trait Geo extends RedisEnvironment { withHash: Option[WithHash] = None, count: Option[Count] = None, order: Option[Order] = None - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( GeoRadiusByMember, Tuple11( @@ -370,8 +364,7 @@ trait Geo extends RedisEnvironment { OptionalInput(StoreInput), OptionalInput(StoreDistInput) ), - LongOutput, - executor + LongOutput ) command.run( (key, member, radius, radiusUnit, withCoord, withDist, withHash, count, order, store.store, store.storeDist) diff --git a/modules/redis/src/main/scala/zio/redis/api/Hashes.scala b/modules/redis/src/main/scala/zio/redis/api/Hashes.scala index c1d8e2ff7..04f067966 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Hashes.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Hashes.scala @@ -24,7 +24,7 @@ import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.schema.Schema -trait Hashes extends RedisEnvironment { +trait Hashes[G[+_]] extends RedisEnvironment[G] { import Hashes._ /** @@ -39,9 +39,9 @@ trait Hashes extends RedisEnvironment { * @return * number of fields removed from the hash. */ - final def hDel[K: Schema, F: Schema](key: K, field: F, fields: F*): IO[RedisError, Long] = { + final def hDel[K: Schema, F: Schema](key: K, field: F, fields: F*): G[Long] = { val command = - RedisCommand(HDel, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[F]())), LongOutput, executor) + RedisCommand(HDel, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[F]())), LongOutput) command.run((key, (field, fields.toList))) } @@ -55,9 +55,9 @@ trait Hashes extends RedisEnvironment { * @return * true if the field exists, otherwise false. */ - final def hExists[K: Schema, F: Schema](key: K, field: F): IO[RedisError, Boolean] = { + final def hExists[K: Schema, F: Schema](key: K, field: F): G[Boolean] = { val command = - RedisCommand(HExists, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[F]()), BoolOutput, executor) + RedisCommand(HExists, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[F]()), BoolOutput) command.run((key, field)) } @@ -71,16 +71,14 @@ trait Hashes extends RedisEnvironment { * @return * value stored in the field, if any. */ - final def hGet[K: Schema, F: Schema](key: K, field: F): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = + final def hGet[K: Schema, F: Schema](key: K, field: F): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = RedisCommand( HGet, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[F]()), - OptionalOutput(ArbitraryOutput[V]()), - executor - ) - .run((key, field)) + OptionalOutput(ArbitraryOutput[V]()) + ).run((key, field)) } /** @@ -91,14 +89,13 @@ trait Hashes extends RedisEnvironment { * @return * map of `field -> value` pairs under the key. */ - final def hGetAll[K: Schema](key: K): ResultBuilder2[Map] = new ResultBuilder2[Map] { - def returning[F: Schema, V: Schema]: IO[RedisError, Map[F, V]] = { + final def hGetAll[K: Schema](key: K): ResultBuilder2[Map, G] = new ResultBuilder2[Map, G] { + def returning[F: Schema, V: Schema]: G[Map[F, V]] = { val command = RedisCommand( HGetAll, ArbitraryKeyInput[K](), - KeyValueOutput(ArbitraryOutput[F](), ArbitraryOutput[V]()), - executor + KeyValueOutput(ArbitraryOutput[F](), ArbitraryOutput[V]()) ) command.run(key) } @@ -117,9 +114,9 @@ trait Hashes extends RedisEnvironment { * @return * integer value after incrementing, or error if the field is not an integer. */ - final def hIncrBy[K: Schema, F: Schema](key: K, field: F, increment: Long): IO[RedisError, Long] = { + final def hIncrBy[K: Schema, F: Schema](key: K, field: F, increment: Long): G[Long] = { val command = - RedisCommand(HIncrBy, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[F](), LongInput), LongOutput, executor) + RedisCommand(HIncrBy, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[F](), LongInput), LongOutput) command.run((key, field, increment)) } @@ -140,13 +137,12 @@ trait Hashes extends RedisEnvironment { key: K, field: F, increment: Double - ): IO[RedisError, Double] = { + ): G[Double] = { val command = RedisCommand( HIncrByFloat, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[F](), DoubleInput), - DoubleOutput, - executor + DoubleOutput ) command.run((key, field, increment)) } @@ -159,10 +155,10 @@ trait Hashes extends RedisEnvironment { * @return * chunk of field names. */ - final def hKeys[K: Schema](key: K): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[F: Schema]: IO[RedisError, Chunk[F]] = - RedisCommand(HKeys, ArbitraryKeyInput[K](), ChunkOutput(ArbitraryOutput[F]()), executor).run(key) + final def hKeys[K: Schema](key: K): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[F: Schema]: G[Chunk[F]] = + RedisCommand(HKeys, ArbitraryKeyInput[K](), ChunkOutput(ArbitraryOutput[F]())).run(key) } /** @@ -173,8 +169,8 @@ trait Hashes extends RedisEnvironment { * @return * number of fields. */ - final def hLen[K: Schema](key: K): IO[RedisError, Long] = { - val command = RedisCommand(HLen, ArbitraryKeyInput[K](), LongOutput, executor) + final def hLen[K: Schema](key: K): G[Long] = { + val command = RedisCommand(HLen, ArbitraryKeyInput[K](), LongOutput) command.run(key) } @@ -194,14 +190,13 @@ trait Hashes extends RedisEnvironment { key: K, field: F, fields: F* - ): ResultBuilder1[({ type lambda[x] = Chunk[Option[x]] })#lambda] = - new ResultBuilder1[({ type lambda[x] = Chunk[Option[x]] })#lambda] { - def returning[V: Schema]: IO[RedisError, Chunk[Option[V]]] = { + ): ResultBuilder1[({ type lambda[x] = Chunk[Option[x]] })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = Chunk[Option[x]] })#lambda, G] { + def returning[V: Schema]: G[Chunk[Option[V]]] = { val command = RedisCommand( HmGet, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[F]())), - ChunkOutput(OptionalOutput(ArbitraryOutput[V]())), - executor + ChunkOutput(OptionalOutput(ArbitraryOutput[V]())) ) command.run((key, (field, fields.toList))) } @@ -215,10 +210,10 @@ trait Hashes extends RedisEnvironment { * @return * random field in the hash or `None` when `key` does not exist. */ - final def hRandField[K: Schema](key: K): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = - RedisCommand(HRandField, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[V]()), executor).run(key) + final def hRandField[K: Schema](key: K): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = + RedisCommand(HRandField, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[V]())).run(key) } /** @@ -235,14 +230,13 @@ trait Hashes extends RedisEnvironment { * @return * a list of fields or fields and values if `withValues` is true. */ - final def hRandField[K: Schema](key: K, count: Long, withValues: Boolean = false): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[V: Schema]: IO[RedisError, Chunk[V]] = { + final def hRandField[K: Schema](key: K, count: Long, withValues: Boolean = false): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[V: Schema]: G[Chunk[V]] = { val command = RedisCommand( HRandField, Tuple3(ArbitraryKeyInput[K](), LongInput, OptionalInput(StringInput)), - ChunkOutput(ArbitraryOutput[V]()), - executor + ChunkOutput(ArbitraryOutput[V]()) ) command.run((key, count, if (withValues) Some("WITHVALUES") else None)) } @@ -265,12 +259,11 @@ trait Hashes extends RedisEnvironment { key: K, pair: (F, V), pairs: (F, V)* - ): IO[RedisError, Unit] = { + ): G[Unit] = { val command = RedisCommand( HmSet, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(Tuple2(ArbitraryValueInput[F](), ArbitraryValueInput[V]()))), - UnitOutput, - executor + UnitOutput ) command.run((key, (pair, pairs.toList))) } @@ -294,14 +287,13 @@ trait Hashes extends RedisEnvironment { cursor: Long, pattern: Option[String] = None, count: Option[Count] = None - ): ResultBuilder2[({ type lambda[x, y] = (Long, Chunk[(x, y)]) })#lambda] = - new ResultBuilder2[({ type lambda[x, y] = (Long, Chunk[(x, y)]) })#lambda] { - def returning[F: Schema, V: Schema]: IO[RedisError, (Long, Chunk[(F, V)])] = { + ): ResultBuilder2[({ type lambda[x, y] = (Long, Chunk[(x, y)]) })#lambda, G] = + new ResultBuilder2[({ type lambda[x, y] = (Long, Chunk[(x, y)]) })#lambda, G] { + def returning[F: Schema, V: Schema]: G[(Long, Chunk[(F, V)])] = { val command = RedisCommand( HScan, Tuple4(ArbitraryKeyInput[K](), LongInput, OptionalInput(PatternInput), OptionalInput(CountInput)), - Tuple2Output(ArbitraryOutput[Long](), ChunkTuple2Output(ArbitraryOutput[F](), ArbitraryOutput[V]())), - executor + Tuple2Output(ArbitraryOutput[Long](), ChunkTuple2Output(ArbitraryOutput[F](), ArbitraryOutput[V]())) ) command.run((key, cursor, pattern.map(Pattern(_)), count)) } @@ -323,12 +315,11 @@ trait Hashes extends RedisEnvironment { key: K, pair: (F, V), pairs: (F, V)* - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( HSet, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(Tuple2(ArbitraryValueInput[F](), ArbitraryValueInput[V]()))), - LongOutput, - executor + LongOutput ) command.run((key, (pair, pairs.toList))) } @@ -349,13 +340,12 @@ trait Hashes extends RedisEnvironment { key: K, field: F, value: V - ): IO[RedisError, Boolean] = { + ): G[Boolean] = { val command = RedisCommand( HSetNx, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[F](), ArbitraryValueInput[V]()), - BoolOutput, - executor + BoolOutput ) command.run((key, field, value)) } @@ -370,9 +360,9 @@ trait Hashes extends RedisEnvironment { * @return * string length of the value in field, or zero if either field or key do not exist. */ - final def hStrLen[K: Schema, F: Schema](key: K, field: F): IO[RedisError, Long] = { + final def hStrLen[K: Schema, F: Schema](key: K, field: F): G[Long] = { val command = - RedisCommand(HStrLen, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[F]()), LongOutput, executor) + RedisCommand(HStrLen, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[F]()), LongOutput) command.run((key, field)) } @@ -384,10 +374,10 @@ trait Hashes extends RedisEnvironment { * @return * list of values in the hash, or an empty list when `key` does not exist. */ - final def hVals[K: Schema](key: K): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[V: Schema]: IO[RedisError, Chunk[V]] = - RedisCommand(HVals, ArbitraryKeyInput[K](), ChunkOutput(ArbitraryOutput[V]()), executor).run(key) + final def hVals[K: Schema](key: K): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[V: Schema]: G[Chunk[V]] = + RedisCommand(HVals, ArbitraryKeyInput[K](), ChunkOutput(ArbitraryOutput[V]())).run(key) } } diff --git a/modules/redis/src/main/scala/zio/redis/api/HyperLogLog.scala b/modules/redis/src/main/scala/zio/redis/api/HyperLogLog.scala index 6a5b42479..dce4c9b61 100644 --- a/modules/redis/src/main/scala/zio/redis/api/HyperLogLog.scala +++ b/modules/redis/src/main/scala/zio/redis/api/HyperLogLog.scala @@ -16,13 +16,12 @@ package zio.redis.api -import zio.IO import zio.redis.Output._ import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.schema.Schema -trait HyperLogLog extends RedisEnvironment { +trait HyperLogLog[G[+_]] extends RedisEnvironment[G] { import HyperLogLog._ import Input._ @@ -38,9 +37,9 @@ trait HyperLogLog extends RedisEnvironment { * @return * boolean indicating if at least 1 HyperLogLog register was altered. */ - final def pfAdd[K: Schema, V: Schema](key: K, element: V, elements: V*): IO[RedisError, Boolean] = { + final def pfAdd[K: Schema, V: Schema](key: K, element: V, elements: V*): G[Boolean] = { val command = - RedisCommand(PfAdd, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), BoolOutput, executor) + RedisCommand(PfAdd, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), BoolOutput) command.run((key, (element, elements.toList))) } @@ -54,8 +53,8 @@ trait HyperLogLog extends RedisEnvironment { * @return * approximate number of unique elements observed via PFADD. */ - final def pfCount[K: Schema](key: K, keys: K*): IO[RedisError, Long] = { - val command = RedisCommand(PfCount, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput, executor) + final def pfCount[K: Schema](key: K, keys: K*): G[Long] = { + val command = RedisCommand(PfCount, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput) command.run((key, keys.toList)) } @@ -69,9 +68,9 @@ trait HyperLogLog extends RedisEnvironment { * @param sourceKeys * additional keys to merge */ - final def pfMerge[K: Schema](destKey: K, sourceKey: K, sourceKeys: K*): IO[RedisError, Unit] = { + final def pfMerge[K: Schema](destKey: K, sourceKey: K, sourceKeys: K*): G[Unit] = { val command = - RedisCommand(PfMerge, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryKeyInput[K]())), UnitOutput, executor) + RedisCommand(PfMerge, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryKeyInput[K]())), UnitOutput) command.run((destKey, (sourceKey, sourceKeys.toList))) } } diff --git a/modules/redis/src/main/scala/zio/redis/api/Keys.scala b/modules/redis/src/main/scala/zio/redis/api/Keys.scala index a21326dfe..1381e8635 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Keys.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Keys.scala @@ -26,7 +26,7 @@ import zio.schema.Schema import java.time.Instant -trait Keys extends RedisEnvironment { +trait Keys[G[+_]] extends RedisEnvironment[G] { import Keys.{Keys => _, _} /** @@ -49,12 +49,11 @@ trait Keys extends RedisEnvironment { destination: D, database: Option[Long] = None, replace: Option[Replace] = None - ): IO[RedisError, Boolean] = { + ): G[Boolean] = { val command = RedisCommand( Copy, Tuple4(ArbitraryKeyInput[S](), ArbitraryKeyInput[D](), OptionalInput(DbInput), OptionalInput(ReplaceInput)), - BoolOutput, - executor + BoolOutput ) command.run((source, destination, database, replace)) } @@ -72,8 +71,8 @@ trait Keys extends RedisEnvironment { * @see * [[unlink]] */ - final def del[K: Schema](key: K, keys: K*): IO[RedisError, Long] = { - val command = RedisCommand(Del, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput, executor) + final def del[K: Schema](key: K, keys: K*): G[Long] = { + val command = RedisCommand(Del, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput) command.run((key, keys.toList)) } @@ -85,8 +84,8 @@ trait Keys extends RedisEnvironment { * @return * bytes for value stored at key. */ - final def dump[K: Schema](key: K): IO[RedisError, Chunk[Byte]] = { - val command = RedisCommand(Dump, ArbitraryKeyInput[K](), BulkStringOutput, executor) + final def dump[K: Schema](key: K): G[Chunk[Byte]] = { + val command = RedisCommand(Dump, ArbitraryKeyInput[K](), BulkStringOutput) command.run(key) } @@ -101,8 +100,8 @@ trait Keys extends RedisEnvironment { * @return * The number of keys existing. */ - final def exists[K: Schema](key: K, keys: K*): IO[RedisError, Long] = { - val command = RedisCommand(Exists, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput, executor) + final def exists[K: Schema](key: K, keys: K*): G[Long] = { + val command = RedisCommand(Exists, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput) command.run((key, keys.toList)) } @@ -119,9 +118,9 @@ trait Keys extends RedisEnvironment { * @see * [[expireAt]] */ - final def expire[K: Schema](key: K, timeout: Duration): IO[RedisError, Boolean] = { + final def expire[K: Schema](key: K, timeout: Duration): G[Boolean] = { val command = - RedisCommand(Expire, Tuple2(ArbitraryKeyInput[K](), DurationSecondsInput), BoolOutput, executor) + RedisCommand(Expire, Tuple2(ArbitraryKeyInput[K](), DurationSecondsInput), BoolOutput) command.run((key, timeout)) } @@ -138,8 +137,8 @@ trait Keys extends RedisEnvironment { * @see * [[expire]] */ - final def expireAt[K: Schema](key: K, timestamp: Instant): IO[RedisError, Boolean] = { - val command = RedisCommand(ExpireAt, Tuple2(ArbitraryKeyInput[K](), TimeSecondsInput), BoolOutput, executor) + final def expireAt[K: Schema](key: K, timestamp: Instant): G[Boolean] = { + val command = RedisCommand(ExpireAt, Tuple2(ArbitraryKeyInput[K](), TimeSecondsInput), BoolOutput) command.run((key, timestamp)) } @@ -151,10 +150,10 @@ trait Keys extends RedisEnvironment { * @return * keys matching pattern. */ - final def keys(pattern: String): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[V: Schema]: IO[RedisError, Chunk[V]] = - RedisCommand(Keys.Keys, StringInput, ChunkOutput(ArbitraryOutput[V]()), executor).run(pattern) + final def keys(pattern: String): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[V: Schema]: G[Chunk[V]] = + RedisCommand(Keys.Keys, StringInput, ChunkOutput(ArbitraryOutput[V]())).run(pattern) } /** @@ -192,7 +191,7 @@ trait Keys extends RedisEnvironment { copy: Option[Copy] = None, replace: Option[Replace] = None, keys: Option[(K, List[K])] - ): IO[RedisError, String] = { + ): G[String] = { val command = RedisCommand( Migrate, Tuple9( @@ -206,8 +205,7 @@ trait Keys extends RedisEnvironment { OptionalInput(AuthInput), OptionalInput(NonEmptyList(ArbitraryKeyInput[K]())) ), - StringOutput, - executor + StringOutput ) command.run((host, port, key, destinationDb, timeout.toMillis, copy, replace, auth, keys)) } @@ -223,8 +221,8 @@ trait Keys extends RedisEnvironment { * @return * true if the key was moved. */ - final def move[K: Schema](key: K, destinationDb: Long): IO[RedisError, Boolean] = { - val command = RedisCommand(Move, Tuple2(ArbitraryKeyInput[K](), LongInput), BoolOutput, executor) + final def move[K: Schema](key: K, destinationDb: Long): G[Boolean] = { + val command = RedisCommand(Move, Tuple2(ArbitraryKeyInput[K](), LongInput), BoolOutput) command.run((key, destinationDb)) } @@ -236,8 +234,8 @@ trait Keys extends RedisEnvironment { * @return * true if timeout was removed, false if key does not exist or does not have an associated timeout. */ - final def persist[K: Schema](key: K): IO[RedisError, Boolean] = { - val command = RedisCommand(Persist, ArbitraryKeyInput[K](), BoolOutput, executor) + final def persist[K: Schema](key: K): G[Boolean] = { + val command = RedisCommand(Persist, ArbitraryKeyInput[K](), BoolOutput) command.run(key) } @@ -254,9 +252,9 @@ trait Keys extends RedisEnvironment { * @see * [[pExpireAt]] */ - final def pExpire[K: Schema](key: K, timeout: Duration): IO[RedisError, Boolean] = { + final def pExpire[K: Schema](key: K, timeout: Duration): G[Boolean] = { val command = - RedisCommand(PExpire, Tuple2(ArbitraryKeyInput[K](), DurationMillisecondsInput), BoolOutput, executor) + RedisCommand(PExpire, Tuple2(ArbitraryKeyInput[K](), DurationMillisecondsInput), BoolOutput) command.run((key, timeout)) } @@ -273,9 +271,9 @@ trait Keys extends RedisEnvironment { * @see * [[pExpire]] */ - final def pExpireAt[K: Schema](key: K, timestamp: Instant): IO[RedisError, Boolean] = { + final def pExpireAt[K: Schema](key: K, timestamp: Instant): G[Boolean] = { val command = - RedisCommand(PExpireAt, Tuple2(ArbitraryKeyInput[K](), TimeMillisecondsInput), BoolOutput, executor) + RedisCommand(PExpireAt, Tuple2(ArbitraryKeyInput[K](), TimeMillisecondsInput), BoolOutput) command.run((key, timestamp)) } @@ -287,8 +285,8 @@ trait Keys extends RedisEnvironment { * @return * remaining time to live of a key that has a timeout, error otherwise. */ - final def pTtl[K: Schema](key: K): IO[RedisError, Duration] = { - val command = RedisCommand(PTtl, ArbitraryKeyInput[K](), DurationMillisecondsOutput, executor) + final def pTtl[K: Schema](key: K): G[Duration] = { + val command = RedisCommand(PTtl, ArbitraryKeyInput[K](), DurationMillisecondsOutput) command.run(key) } @@ -298,10 +296,10 @@ trait Keys extends RedisEnvironment { * @return * key or None when the database is empty. */ - final def randomKey: ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = - RedisCommand(RandomKey, NoInput, OptionalOutput(ArbitraryOutput[V]()), executor).run(()) + final def randomKey: ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = + RedisCommand(RandomKey, NoInput, OptionalOutput(ArbitraryOutput[V]())).run(()) } /** @@ -314,9 +312,9 @@ trait Keys extends RedisEnvironment { * @return * unit if successful, error otherwise. */ - final def rename[K: Schema](key: K, newKey: K): IO[RedisError, Unit] = { + final def rename[K: Schema](key: K, newKey: K): G[Unit] = { val command = - RedisCommand(Rename, Tuple2(ArbitraryKeyInput[K](), ArbitraryKeyInput[K]()), UnitOutput, executor) + RedisCommand(Rename, Tuple2(ArbitraryKeyInput[K](), ArbitraryKeyInput[K]()), UnitOutput) command.run((key, newKey)) } @@ -330,9 +328,9 @@ trait Keys extends RedisEnvironment { * @return * true if key was renamed to newKey, false if newKey already exists. */ - final def renameNx[K: Schema](key: K, newKey: K): IO[RedisError, Boolean] = { + final def renameNx[K: Schema](key: K, newKey: K): G[Boolean] = { val command = - RedisCommand(RenameNx, Tuple2(ArbitraryKeyInput[K](), ArbitraryKeyInput[K]()), BoolOutput, executor) + RedisCommand(RenameNx, Tuple2(ArbitraryKeyInput[K](), ArbitraryKeyInput[K]()), BoolOutput) command.run((key, newKey)) } @@ -366,7 +364,7 @@ trait Keys extends RedisEnvironment { absTtl: Option[AbsTtl] = None, idleTime: Option[IdleTime] = None, freq: Option[Freq] = None - ): IO[RedisError, Unit] = { + ): G[Unit] = { val command = RedisCommand( Restore, Tuple7( @@ -378,8 +376,7 @@ trait Keys extends RedisEnvironment { OptionalInput(IdleTimeInput), OptionalInput(FreqInput) ), - UnitOutput, - executor + UnitOutput ) command.run((key, ttl, value, replace, absTtl, idleTime, freq)) } @@ -405,14 +402,13 @@ trait Keys extends RedisEnvironment { pattern: Option[String] = None, count: Option[Count] = None, `type`: Option[RedisType] = None - ): ResultBuilder1[({ type lambda[x] = (Long, Chunk[x]) })#lambda] = - new ResultBuilder1[({ type lambda[x] = (Long, Chunk[x]) })#lambda] { - def returning[K: Schema]: IO[RedisError, (Long, Chunk[K])] = { + ): ResultBuilder1[({ type lambda[x] = (Long, Chunk[x]) })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = (Long, Chunk[x]) })#lambda, G] { + def returning[K: Schema]: G[(Long, Chunk[K])] = { val command = RedisCommand( Scan, Tuple4(LongInput, OptionalInput(PatternInput), OptionalInput(CountInput), OptionalInput(RedisTypeInput)), - Tuple2Output(ArbitraryOutput[Long](), ChunkOutput(ArbitraryOutput[K]())), - executor + Tuple2Output(ArbitraryOutput[Long](), ChunkOutput(ArbitraryOutput[K]())) ) command.run((cursor, pattern.map(Pattern(_)), count, `type`)) } @@ -443,9 +439,9 @@ trait Keys extends RedisEnvironment { order: Order = Order.Ascending, get: Option[(String, List[String])] = None, alpha: Option[Alpha] = None - ): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[V: Schema]: IO[RedisError, Chunk[V]] = { + ): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[V: Schema]: G[Chunk[V]] = { val command = RedisCommand( Sort, Tuple6( @@ -456,8 +452,7 @@ trait Keys extends RedisEnvironment { OrderInput, OptionalInput(AlphaInput) ), - ChunkOutput(ArbitraryOutput[V]()), - executor + ChunkOutput(ArbitraryOutput[V]()) ) command.run((key, by, limit, get, order, alpha)) } @@ -495,7 +490,7 @@ trait Keys extends RedisEnvironment { order: Order = Order.Ascending, get: Option[(String, List[String])] = None, alpha: Option[Alpha] = None - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( Sort, Tuple7( @@ -507,8 +502,7 @@ trait Keys extends RedisEnvironment { OptionalInput(AlphaInput), StoreInput ), - LongOutput, - executor + LongOutput ) command.run((key, by, limit, get, order, alpha, storeAt)) } @@ -523,8 +517,8 @@ trait Keys extends RedisEnvironment { * @return * The number of keys that were touched. */ - final def touch[K: Schema](key: K, keys: K*): IO[RedisError, Long] = { - val command = RedisCommand(Touch, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput, executor) + final def touch[K: Schema](key: K, keys: K*): G[Long] = { + val command = RedisCommand(Touch, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput) command.run((key, keys.toList)) } @@ -536,8 +530,8 @@ trait Keys extends RedisEnvironment { * @return * remaining time to live of a key that has a timeout, error otherwise. */ - final def ttl[K: Schema](key: K): IO[RedisError, Duration] = { - val command = RedisCommand(Ttl, ArbitraryKeyInput[K](), DurationSecondsOutput, executor) + final def ttl[K: Schema](key: K): G[Duration] = { + val command = RedisCommand(Ttl, ArbitraryKeyInput[K](), DurationSecondsOutput) command.run(key) } @@ -549,8 +543,8 @@ trait Keys extends RedisEnvironment { * @return * type of the value stored at key. */ - final def typeOf[K: Schema](key: K): IO[RedisError, RedisType] = { - val command = RedisCommand(TypeOf, ArbitraryKeyInput[K](), TypeOutput, executor) + final def typeOf[K: Schema](key: K): G[RedisType] = { + val command = RedisCommand(TypeOf, ArbitraryKeyInput[K](), TypeOutput) command.run(key) } @@ -568,8 +562,8 @@ trait Keys extends RedisEnvironment { * @see * [[del]] */ - final def unlink[K: Schema](key: K, keys: K*): IO[RedisError, Long] = { - val command = RedisCommand(Unlink, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput, executor) + final def unlink[K: Schema](key: K, keys: K*): G[Long] = { + val command = RedisCommand(Unlink, NonEmptyList(ArbitraryKeyInput[K]()), LongOutput) command.run((key, keys.toList)) } @@ -584,8 +578,8 @@ trait Keys extends RedisEnvironment { * @return * the number of replicas reached both in case of failure and success. */ - final def wait_(replicas: Long, timeout: Duration): IO[RedisError, Long] = { - val command = RedisCommand(Wait, Tuple2(LongInput, LongInput), LongOutput, executor) + final def wait_(replicas: Long, timeout: Duration): G[Long] = { + val command = RedisCommand(Wait, Tuple2(LongInput, LongInput), LongOutput) command.run((replicas, timeout.toMillis)) } } diff --git a/modules/redis/src/main/scala/zio/redis/api/Lists.scala b/modules/redis/src/main/scala/zio/redis/api/Lists.scala index ad193eebc..80dddb469 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Lists.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Lists.scala @@ -24,7 +24,7 @@ import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.schema.Schema -trait Lists extends RedisEnvironment { +trait Lists[G[+_]] extends RedisEnvironment[G] { import Lists._ /** @@ -52,14 +52,13 @@ trait Lists extends RedisEnvironment { sourceSide: Side, destinationSide: Side, timeout: Duration - ): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = { + ): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = { val command = RedisCommand( BlMove, Tuple5(ArbitraryValueInput[S](), ArbitraryValueInput[D](), SideInput, SideInput, DurationSecondsInput), - OptionalOutput(ArbitraryOutput[V]()), - executor + OptionalOutput(ArbitraryOutput[V]()) ) command.run((source, destination, sourceSide, destinationSide, timeout)) @@ -83,14 +82,13 @@ trait Lists extends RedisEnvironment { */ final def blPop[K: Schema](key: K, keys: K*)( timeout: Duration - ): ResultBuilder1[({ type lambda[x] = Option[(K, x)] })#lambda] = - new ResultBuilder1[({ type lambda[x] = Option[(K, x)] })#lambda] { - def returning[V: Schema]: IO[RedisError, Option[(K, V)]] = { + ): ResultBuilder1[({ type lambda[x] = Option[(K, x)] })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = Option[(K, x)] })#lambda, G] { + def returning[V: Schema]: G[Option[(K, V)]] = { val command = RedisCommand( BlPop, Tuple2(NonEmptyList(ArbitraryKeyInput[K]()), DurationSecondsInput), - OptionalOutput(Tuple2Output(ArbitraryOutput[K](), ArbitraryOutput[V]())), - executor + OptionalOutput(Tuple2Output(ArbitraryOutput[K](), ArbitraryOutput[V]())) ) command.run(((key, keys.toList), timeout)) } @@ -113,14 +111,13 @@ trait Lists extends RedisEnvironment { */ final def brPop[K: Schema](key: K, keys: K*)( timeout: Duration - ): ResultBuilder1[({ type lambda[x] = Option[(K, x)] })#lambda] = - new ResultBuilder1[({ type lambda[x] = Option[(K, x)] })#lambda] { - def returning[V: Schema]: IO[RedisError, Option[(K, V)]] = { + ): ResultBuilder1[({ type lambda[x] = Option[(K, x)] })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = Option[(K, x)] })#lambda, G] { + def returning[V: Schema]: G[Option[(K, V)]] = { val command = RedisCommand( BrPop, Tuple2(NonEmptyList(ArbitraryKeyInput[K]()), DurationSecondsInput), - OptionalOutput(Tuple2Output(ArbitraryOutput[K](), ArbitraryOutput[V]())), - executor + OptionalOutput(Tuple2Output(ArbitraryOutput[K](), ArbitraryOutput[V]())) ) command.run(((key, keys.toList), timeout)) } @@ -144,14 +141,13 @@ trait Lists extends RedisEnvironment { source: S, destination: D, timeout: Duration - ): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = { + ): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = { val command = RedisCommand( BrPopLPush, Tuple3(ArbitraryValueInput[S](), ArbitraryValueInput[D](), DurationSecondsInput), - OptionalOutput(ArbitraryOutput[V]()), - executor + OptionalOutput(ArbitraryOutput[V]()) ) command.run((source, destination, timeout)) @@ -169,10 +165,10 @@ trait Lists extends RedisEnvironment { * @return * the requested element, or empty if the index is out of range. */ - final def lIndex[K: Schema](key: K, index: Long): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = - RedisCommand(LIndex, Tuple2(ArbitraryKeyInput[K](), LongInput), OptionalOutput(ArbitraryOutput[V]()), executor) + final def lIndex[K: Schema](key: K, index: Long): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = + RedisCommand(LIndex, Tuple2(ArbitraryKeyInput[K](), LongInput), OptionalOutput(ArbitraryOutput[V]())) .run((key, index)) } @@ -195,12 +191,11 @@ trait Lists extends RedisEnvironment { position: Position, pivot: V, element: V - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( LInsert, Tuple4(ArbitraryKeyInput[K](), PositionInput, ArbitraryValueInput[V](), ArbitraryValueInput[V]()), - LongOutput, - executor + LongOutput ) command.run((key, position, pivot, element)) } @@ -213,8 +208,8 @@ trait Lists extends RedisEnvironment { * @return * the length of the list at key. */ - final def lLen[K: Schema](key: K): IO[RedisError, Long] = { - val command = RedisCommand(LLen, ArbitraryKeyInput[K](), LongOutput, executor) + final def lLen[K: Schema](key: K): G[Long] = { + val command = RedisCommand(LLen, ArbitraryKeyInput[K](), LongOutput) command.run(key) } @@ -239,14 +234,13 @@ trait Lists extends RedisEnvironment { destination: D, sourceSide: Side, destinationSide: Side - ): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = { + ): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = { val command = RedisCommand( LMove, Tuple4(ArbitraryValueInput[S](), ArbitraryValueInput[D](), SideInput, SideInput), - OptionalOutput(ArbitraryOutput[V]()), - executor + OptionalOutput(ArbitraryOutput[V]()) ) command.run((source, destination, sourceSide, destinationSide)) } @@ -260,10 +254,10 @@ trait Lists extends RedisEnvironment { * @return * the value of the first element, or empty when key does not exist. */ - final def lPop[K: Schema](key: K): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = - RedisCommand(LPop, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[V]()), executor).run(key) + final def lPop[K: Schema](key: K): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = + RedisCommand(LPop, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[V]())).run(key) } /** @@ -287,7 +281,7 @@ trait Lists extends RedisEnvironment { element: V, rank: Option[Rank] = None, maxLen: Option[ListMaxLen] = None - ): IO[RedisError, Option[Long]] = { + ): G[Option[Long]] = { val command = RedisCommand( LPos, Tuple4( @@ -296,8 +290,7 @@ trait Lists extends RedisEnvironment { OptionalInput(RankInput), OptionalInput(ListMaxLenInput) ), - OptionalOutput(LongOutput), - executor + OptionalOutput(LongOutput) ) command.run((key, element, rank, maxLen)) @@ -327,7 +320,7 @@ trait Lists extends RedisEnvironment { count: Count, rank: Option[Rank] = None, maxLen: Option[ListMaxLen] = None - ): IO[RedisError, Chunk[Long]] = { + ): G[Chunk[Long]] = { val command = RedisCommand( LPos, Tuple5( @@ -337,8 +330,7 @@ trait Lists extends RedisEnvironment { OptionalInput(RankInput), OptionalInput(ListMaxLenInput) ), - ChunkOutput(LongOutput), - executor + ChunkOutput(LongOutput) ) command.run((key, element, count, rank, maxLen)) @@ -357,9 +349,9 @@ trait Lists extends RedisEnvironment { * @return * the length of the list after the push operation. */ - final def lPush[K: Schema, V: Schema](key: K, element: V, elements: V*): IO[RedisError, Long] = { + final def lPush[K: Schema, V: Schema](key: K, element: V, elements: V*): G[Long] = { val command = - RedisCommand(LPush, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), LongOutput, executor) + RedisCommand(LPush, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), LongOutput) command.run((key, (element, elements.toList))) } @@ -376,9 +368,9 @@ trait Lists extends RedisEnvironment { * @return * the length of the list after the push operation. */ - final def lPushX[K: Schema, V: Schema](key: K, element: V, elements: V*): IO[RedisError, Long] = { + final def lPushX[K: Schema, V: Schema](key: K, element: V, elements: V*): G[Long] = { val command = - RedisCommand(LPushX, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), LongOutput, executor) + RedisCommand(LPushX, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), LongOutput) command.run((key, (element, elements.toList))) } @@ -393,10 +385,10 @@ trait Lists extends RedisEnvironment { * @return * a chunk of elements in the specified range. */ - final def lRange[K: Schema](key: K, range: Range): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[V: Schema]: IO[RedisError, Chunk[V]] = - RedisCommand(LRange, Tuple2(ArbitraryKeyInput[K](), RangeInput), ChunkOutput(ArbitraryOutput[V]()), executor) + final def lRange[K: Schema](key: K, range: Range): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[V: Schema]: G[Chunk[V]] = + RedisCommand(LRange, Tuple2(ArbitraryKeyInput[K](), RangeInput), ChunkOutput(ArbitraryOutput[V]())) .run((key, range)) } @@ -417,9 +409,9 @@ trait Lists extends RedisEnvironment { * @return * the number of removed elements. */ - final def lRem[K: Schema](key: K, count: Long, element: String): IO[RedisError, Long] = { + final def lRem[K: Schema](key: K, count: Long, element: String): G[Long] = { val command = - RedisCommand(LRem, Tuple3(ArbitraryKeyInput[K](), LongInput, StringInput), LongOutput, executor) + RedisCommand(LRem, Tuple3(ArbitraryKeyInput[K](), LongInput, StringInput), LongOutput) command.run((key, count, element)) } @@ -435,9 +427,9 @@ trait Lists extends RedisEnvironment { * @return * the Unit value. */ - final def lSet[K: Schema, V: Schema](key: K, index: Long, element: V): IO[RedisError, Unit] = { + final def lSet[K: Schema, V: Schema](key: K, index: Long, element: V): G[Unit] = { val command = - RedisCommand(LSet, Tuple3(ArbitraryKeyInput[K](), LongInput, ArbitraryValueInput[V]()), UnitOutput, executor) + RedisCommand(LSet, Tuple3(ArbitraryKeyInput[K](), LongInput, ArbitraryValueInput[V]()), UnitOutput) command.run((key, index, element)) } @@ -452,8 +444,8 @@ trait Lists extends RedisEnvironment { * @return * the Unit value. */ - final def lTrim[K: Schema](key: K, range: Range): IO[RedisError, Unit] = { - val command = RedisCommand(LTrim, Tuple2(ArbitraryKeyInput[K](), RangeInput), UnitOutput, executor) + final def lTrim[K: Schema](key: K, range: Range): G[Unit] = { + val command = RedisCommand(LTrim, Tuple2(ArbitraryKeyInput[K](), RangeInput), UnitOutput) command.run((key, range)) } @@ -465,10 +457,10 @@ trait Lists extends RedisEnvironment { * @return * the value of the last element, or empty when key does not exist. */ - final def rPop[K: Schema](key: K): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = - RedisCommand(RPop, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[V]()), executor).run(key) + final def rPop[K: Schema](key: K): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = + RedisCommand(RPop, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[V]())).run(key) } /** @@ -483,16 +475,14 @@ trait Lists extends RedisEnvironment { * @return * the element being popped and pushed. If source does not exist, empty is returned and no operation is performed. */ - final def rPopLPush[S: Schema, D: Schema](source: S, destination: D): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[V: Schema]: IO[RedisError, Option[V]] = + final def rPopLPush[S: Schema, D: Schema](source: S, destination: D): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[V: Schema]: G[Option[V]] = RedisCommand( RPopLPush, Tuple2(ArbitraryValueInput[S](), ArbitraryValueInput[D]()), - OptionalOutput(ArbitraryOutput[V]()), - executor - ) - .run((source, destination)) + OptionalOutput(ArbitraryOutput[V]()) + ).run((source, destination)) } /** @@ -508,9 +498,9 @@ trait Lists extends RedisEnvironment { * @return * the length of the list after the push operation. */ - final def rPush[K: Schema, V: Schema](key: K, element: V, elements: V*): IO[RedisError, Long] = { + final def rPush[K: Schema, V: Schema](key: K, element: V, elements: V*): G[Long] = { val command = - RedisCommand(RPush, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), LongOutput, executor) + RedisCommand(RPush, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), LongOutput) command.run((key, (element, elements.toList))) } @@ -527,9 +517,9 @@ trait Lists extends RedisEnvironment { * @return * the length of the list after the push operation. */ - final def rPushX[K: Schema, V: Schema](key: K, element: V, elements: V*): IO[RedisError, Long] = { + final def rPushX[K: Schema, V: Schema](key: K, element: V, elements: V*): G[Long] = { val command = - RedisCommand(RPushX, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), LongOutput, executor) + RedisCommand(RPushX, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[V]())), LongOutput) command.run((key, (element, elements.toList))) } } diff --git a/modules/redis/src/main/scala/zio/redis/api/Publishing.scala b/modules/redis/src/main/scala/zio/redis/api/Publishing.scala index 112a171a5..3360f96c5 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Publishing.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Publishing.scala @@ -16,14 +16,13 @@ package zio.redis.api +import zio.Chunk import zio.redis.Input._ import zio.redis.Output._ -import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.schema.Schema -import zio.{Chunk, IO} -trait Publishing extends RedisEnvironment { +trait Publishing[G[+_]] extends RedisEnvironment[G] { import Publishing._ /** @@ -36,8 +35,8 @@ trait Publishing extends RedisEnvironment { * @return * Returns the number of clients that received the message. */ - final def publish[A: Schema](channel: String, message: A): IO[RedisError, Long] = { - val command = RedisCommand(Publish, Tuple2(StringInput, ArbitraryKeyInput[A]()), LongOutput, executor) + final def publish[A: Schema](channel: String, message: A): G[Long] = { + val command = RedisCommand(Publish, Tuple2(StringInput, ArbitraryKeyInput[A]()), LongOutput) command.run((channel, message)) } @@ -49,8 +48,8 @@ trait Publishing extends RedisEnvironment { * @return * Returns a list of active channels matching the specified pattern. */ - final def pubSubChannels(pattern: String): IO[RedisError, Chunk[String]] = { - val command = RedisCommand(PubSubChannels, StringInput, ChunkOutput(MultiStringOutput), executor) + final def pubSubChannels(pattern: String): G[Chunk[String]] = { + val command = RedisCommand(PubSubChannels, StringInput, ChunkOutput(MultiStringOutput)) command.run(pattern) } @@ -60,8 +59,8 @@ trait Publishing extends RedisEnvironment { * @return * Returns the number of patterns all the clients are subscribed to. */ - final def pubSubNumPat: IO[RedisError, Long] = { - val command = RedisCommand(PubSubNumPat, NoInput, LongOutput, executor) + final def pubSubNumPat: G[Long] = { + val command = RedisCommand(PubSubNumPat, NoInput, LongOutput) command.run(()) } @@ -75,8 +74,8 @@ trait Publishing extends RedisEnvironment { * @return * Returns a map of channel and number of subscribers for channel. */ - final def pubSubNumSub(channel: String, channels: String*): IO[RedisError, Map[String, Long]] = { - val command = RedisCommand(PubSubNumSub, NonEmptyList(StringInput), NumSubResponseOutput, executor) + final def pubSubNumSub(channel: String, channels: String*): G[Map[String, Long]] = { + val command = RedisCommand(PubSubNumSub, NonEmptyList(StringInput), NumSubResponseOutput) command.run((channel, channels.toList)) } } diff --git a/modules/redis/src/main/scala/zio/redis/api/Scripting.scala b/modules/redis/src/main/scala/zio/redis/api/Scripting.scala index be3f0e96b..70aa2eff9 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Scripting.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Scripting.scala @@ -22,7 +22,7 @@ import zio.redis.ResultBuilder.ResultOutputBuilder import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} -trait Scripting extends RedisEnvironment { +trait Scripting[G[+_]] extends RedisEnvironment[G] { import Input._ import Scripting._ @@ -43,9 +43,9 @@ trait Scripting extends RedisEnvironment { script: String, keys: Chunk[K], args: Chunk[A] - ): ResultOutputBuilder = new ResultOutputBuilder { - def returning[R: Output]: IO[RedisError, R] = { - val command = RedisCommand(Eval, EvalInput(Input[K], Input[A]), Output[R], executor) + ): ResultOutputBuilder[G] = new ResultOutputBuilder[G] { + def returning[R: Output]: G[R] = { + val command = RedisCommand(Eval, EvalInput(Input[K], Input[A]), Output[R]) command.run((script, keys, args)) } } @@ -68,9 +68,9 @@ trait Scripting extends RedisEnvironment { sha1: String, keys: Chunk[K], args: Chunk[A] - ): ResultOutputBuilder = new ResultOutputBuilder { - def returning[R: Output]: IO[RedisError, R] = { - val command = RedisCommand(EvalSha, EvalInput(Input[K], Input[A]), Output[R], executor) + ): ResultOutputBuilder[G] = new ResultOutputBuilder[G] { + def returning[R: Output]: G[R] = { + val command = RedisCommand(EvalSha, EvalInput(Input[K], Input[A]), Output[R]) command.run((sha1, keys, args)) } } @@ -85,8 +85,8 @@ trait Scripting extends RedisEnvironment { * @return * the Unit value. */ - final def scriptDebug(mode: DebugMode): IO[RedisError, Unit] = { - val command = RedisCommand(ScriptDebug, ScriptDebugInput, UnitOutput, executor) + final def scriptDebug(mode: DebugMode): G[Unit] = { + val command = RedisCommand(ScriptDebug, ScriptDebugInput, UnitOutput) command.run(mode) } @@ -101,8 +101,8 @@ trait Scripting extends RedisEnvironment { * for every corresponding SHA1 digest of a script that actually exists in the script cache, an true is returned, * otherwise false is returned. */ - final def scriptExists(sha1: String, sha1s: String*): IO[RedisError, Chunk[Boolean]] = { - val command = RedisCommand(ScriptExists, NonEmptyList(StringInput), ChunkOutput(BoolOutput), executor) + final def scriptExists(sha1: String, sha1s: String*): G[Chunk[Boolean]] = { + val command = RedisCommand(ScriptExists, NonEmptyList(StringInput), ChunkOutput(BoolOutput)) command.run((sha1, sha1s.toList)) } @@ -119,8 +119,8 @@ trait Scripting extends RedisEnvironment { * @return * the Unit value. */ - final def scriptFlush(mode: Option[FlushMode] = None): IO[RedisError, Unit] = { - val command = RedisCommand(ScriptFlush, OptionalInput(ScriptFlushInput), UnitOutput, executor) + final def scriptFlush(mode: Option[FlushMode] = None): G[Unit] = { + val command = RedisCommand(ScriptFlush, OptionalInput(ScriptFlushInput), UnitOutput) command.run(mode) } @@ -131,8 +131,8 @@ trait Scripting extends RedisEnvironment { * @return * the Unit value. */ - final def scriptKill: IO[RedisError, Unit] = { - val command = RedisCommand(ScriptKill, NoInput, UnitOutput, executor) + final def scriptKill: G[Unit] = { + val command = RedisCommand(ScriptKill, NoInput, UnitOutput) command.run(()) } @@ -145,8 +145,8 @@ trait Scripting extends RedisEnvironment { * @return * the SHA1 digest of the script added into the script cache. */ - final def scriptLoad(script: String): IO[RedisError, String] = { - val command = RedisCommand(ScriptLoad, StringInput, MultiStringOutput, executor) + final def scriptLoad(script: String): G[String] = { + val command = RedisCommand(ScriptLoad, StringInput, MultiStringOutput) command.run(script) } } diff --git a/modules/redis/src/main/scala/zio/redis/api/Sets.scala b/modules/redis/src/main/scala/zio/redis/api/Sets.scala index 6a4416339..cddf8f7e4 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Sets.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Sets.scala @@ -24,7 +24,7 @@ import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.schema.Schema -trait Sets extends RedisEnvironment { +trait Sets[G[+_]] extends RedisEnvironment[G] { import Sets._ /** @@ -40,9 +40,9 @@ trait Sets extends RedisEnvironment { * Returns the number of elements that were added to the set, not including all the elements already present into * the set. */ - final def sAdd[K: Schema, M: Schema](key: K, member: M, members: M*): IO[RedisError, Long] = { + final def sAdd[K: Schema, M: Schema](key: K, member: M, members: M*): G[Long] = { val command = - RedisCommand(SAdd, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), LongOutput, executor) + RedisCommand(SAdd, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), LongOutput) command.run((key, (member, members.toList))) } @@ -54,8 +54,8 @@ trait Sets extends RedisEnvironment { * @return * Returns the cardinality (number of elements) of the set, or 0 if key does not exist. */ - final def sCard[K: Schema](key: K): IO[RedisError, Long] = { - val command = RedisCommand(SCard, ArbitraryKeyInput[K](), LongOutput, executor) + final def sCard[K: Schema](key: K): G[Long] = { + val command = RedisCommand(SCard, ArbitraryKeyInput[K](), LongOutput) command.run(key) } @@ -69,10 +69,10 @@ trait Sets extends RedisEnvironment { * @return * Returns the members of the set resulting from the difference between the first set and all the successive sets. */ - final def sDiff[K: Schema](key: K, keys: K*): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[R: Schema]: IO[RedisError, Chunk[R]] = - RedisCommand(SDiff, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(ArbitraryOutput[R]()), executor) + final def sDiff[K: Schema](key: K, keys: K*): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[R: Schema]: G[Chunk[R]] = + RedisCommand(SDiff, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(ArbitraryOutput[R]())) .run((key, keys.toList)) } @@ -88,12 +88,11 @@ trait Sets extends RedisEnvironment { * @return * Returns the number of elements in the resulting set. */ - final def sDiffStore[D: Schema, K: Schema](destination: D, key: K, keys: K*): IO[RedisError, Long] = { + final def sDiffStore[D: Schema, K: Schema](destination: D, key: K, keys: K*): G[Long] = { val command = RedisCommand( SDiffStore, Tuple2(ArbitraryValueInput[D](), NonEmptyList(ArbitraryKeyInput[K]())), - LongOutput, - executor + LongOutput ) command.run((destination, (key, keys.toList))) } @@ -108,10 +107,10 @@ trait Sets extends RedisEnvironment { * @return * Returns the members of the set resulting from the intersection of all the given sets. */ - final def sInter[K: Schema](destination: K, keys: K*): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[R: Schema]: IO[RedisError, Chunk[R]] = - RedisCommand(SInter, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(ArbitraryOutput[R]()), executor) + final def sInter[K: Schema](destination: K, keys: K*): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[R: Schema]: G[Chunk[R]] = + RedisCommand(SInter, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(ArbitraryOutput[R]())) .run((destination, keys.toList)) } @@ -131,12 +130,11 @@ trait Sets extends RedisEnvironment { destination: D, key: K, keys: K* - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( SInterStore, Tuple2(ArbitraryValueInput[D](), NonEmptyList(ArbitraryKeyInput[K]())), - LongOutput, - executor + LongOutput ) command.run((destination, (key, keys.toList))) } @@ -152,9 +150,9 @@ trait Sets extends RedisEnvironment { * Returns 1 if the element is a member of the set. 0 if the element is not a member of the set, or if key does not * exist. */ - final def sIsMember[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Boolean] = { + final def sIsMember[K: Schema, M: Schema](key: K, member: M): G[Boolean] = { val command = - RedisCommand(SIsMember, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[M]()), BoolOutput, executor) + RedisCommand(SIsMember, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[M]()), BoolOutput) command.run((key, member)) } @@ -166,10 +164,10 @@ trait Sets extends RedisEnvironment { * @return * Returns the members of the set. */ - final def sMembers[K: Schema](key: K): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[R: Schema]: IO[RedisError, Chunk[R]] = - RedisCommand(SMembers, ArbitraryKeyInput[K](), ChunkOutput(ArbitraryOutput[R]()), executor).run(key) + final def sMembers[K: Schema](key: K): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[R: Schema]: G[Chunk[R]] = + RedisCommand(SMembers, ArbitraryKeyInput[K](), ChunkOutput(ArbitraryOutput[R]())).run(key) } /** @@ -188,12 +186,11 @@ trait Sets extends RedisEnvironment { source: S, destination: D, member: M - ): IO[RedisError, Boolean] = { + ): G[Boolean] = { val command = RedisCommand( SMove, Tuple3(ArbitraryValueInput[S](), ArbitraryValueInput[D](), ArbitraryValueInput[M]()), - BoolOutput, - executor + BoolOutput ) command.run((source, destination, member)) } @@ -208,14 +205,13 @@ trait Sets extends RedisEnvironment { * @return * Returns the elements removed. */ - final def sPop[K: Schema](key: K, count: Option[Long] = None): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[R: Schema]: IO[RedisError, Chunk[R]] = { + final def sPop[K: Schema](key: K, count: Option[Long] = None): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[R: Schema]: G[Chunk[R]] = { val command = RedisCommand( SPop, Tuple2(ArbitraryKeyInput[K](), OptionalInput(LongInput)), - MultiStringChunkOutput(ArbitraryOutput[R]()), - executor + MultiStringChunkOutput(ArbitraryOutput[R]()) ) command.run((key, count)) } @@ -231,14 +227,13 @@ trait Sets extends RedisEnvironment { * @return * Returns the random members. */ - final def sRandMember[K: Schema](key: K, count: Option[Long] = None): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[R: Schema]: IO[RedisError, Chunk[R]] = { + final def sRandMember[K: Schema](key: K, count: Option[Long] = None): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[R: Schema]: G[Chunk[R]] = { val command = RedisCommand( SRandMember, Tuple2(ArbitraryKeyInput[K](), OptionalInput(LongInput)), - MultiStringChunkOutput(ArbitraryOutput[R]()), - executor + MultiStringChunkOutput(ArbitraryOutput[R]()) ) command.run((key, count)) } @@ -256,9 +251,9 @@ trait Sets extends RedisEnvironment { * @return * Returns the number of members that were removed from the set, not including non existing members. */ - final def sRem[K: Schema, M: Schema](key: K, member: M, members: M*): IO[RedisError, Long] = { + final def sRem[K: Schema, M: Schema](key: K, member: M, members: M*): G[Long] = { val command = - RedisCommand(SRem, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), LongOutput, executor) + RedisCommand(SRem, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), LongOutput) command.run((key, (member, members.toList))) } @@ -284,14 +279,13 @@ trait Sets extends RedisEnvironment { cursor: Long, pattern: Option[String] = None, count: Option[Count] = None - ): ResultBuilder1[({ type lambda[x] = (Long, Chunk[x]) })#lambda] = - new ResultBuilder1[({ type lambda[x] = (Long, Chunk[x]) })#lambda] { - def returning[R: Schema]: IO[RedisError, (Long, Chunk[R])] = { + ): ResultBuilder1[({ type lambda[x] = (Long, Chunk[x]) })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = (Long, Chunk[x]) })#lambda, G] { + def returning[R: Schema]: G[(Long, Chunk[R])] = { val command = RedisCommand( SScan, Tuple4(ArbitraryKeyInput[K](), LongInput, OptionalInput(PatternInput), OptionalInput(CountInput)), - Tuple2Output(MultiStringOutput.map(_.toLong), ChunkOutput(ArbitraryOutput[R]())), - executor + Tuple2Output(MultiStringOutput.map(_.toLong), ChunkOutput(ArbitraryOutput[R]())) ) command.run((key, cursor, pattern.map(Pattern(_)), count)) } @@ -307,10 +301,10 @@ trait Sets extends RedisEnvironment { * @return * Returns a list with members of the resulting set. */ - final def sUnion[K: Schema](key: K, keys: K*): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[R: Schema]: IO[RedisError, Chunk[R]] = - RedisCommand(SUnion, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(ArbitraryOutput[R]()), executor) + final def sUnion[K: Schema](key: K, keys: K*): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[R: Schema]: G[Chunk[R]] = + RedisCommand(SUnion, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(ArbitraryOutput[R]())) .run((key, keys.toList)) } @@ -330,12 +324,11 @@ trait Sets extends RedisEnvironment { destination: D, key: K, keys: K* - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( SUnionStore, Tuple2(ArbitraryValueInput[D](), NonEmptyList(ArbitraryKeyInput[K]())), - LongOutput, - executor + LongOutput ) command.run((destination, (key, keys.toList))) } diff --git a/modules/redis/src/main/scala/zio/redis/api/SortedSets.scala b/modules/redis/src/main/scala/zio/redis/api/SortedSets.scala index 2c349a7df..222bce133 100644 --- a/modules/redis/src/main/scala/zio/redis/api/SortedSets.scala +++ b/modules/redis/src/main/scala/zio/redis/api/SortedSets.scala @@ -24,7 +24,7 @@ import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.schema.Schema -trait SortedSets extends RedisEnvironment { +trait SortedSets[G[+_]] extends RedisEnvironment[G] { import SortedSets._ /** @@ -45,9 +45,9 @@ trait SortedSets extends RedisEnvironment { timeout: Duration, key: K, keys: K* - ): ResultBuilder1[({ type lambda[x] = Option[(K, MemberScore[x])] })#lambda] = - new ResultBuilder1[({ type lambda[x] = Option[(K, MemberScore[x])] })#lambda] { - def returning[M: Schema]: IO[RedisError, Option[(K, MemberScore[M])]] = { + ): ResultBuilder1[({ type lambda[x] = Option[(K, MemberScore[x])] })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = Option[(K, MemberScore[x])] })#lambda, G] { + def returning[M: Schema]: G[Option[(K, MemberScore[M])]] = { val memberScoreOutput = Tuple3Output(ArbitraryOutput[K](), ArbitraryOutput[M](), DoubleOutput).map { case (k, m, s) => (k, MemberScore(m, s)) @@ -57,8 +57,7 @@ trait SortedSets extends RedisEnvironment { RedisCommand( BzPopMax, Tuple2(NonEmptyList(ArbitraryKeyInput[K]()), DurationSecondsInput), - OptionalOutput(memberScoreOutput), - executor + OptionalOutput(memberScoreOutput) ) command.run(((key, keys.toList), timeout)) @@ -83,9 +82,9 @@ trait SortedSets extends RedisEnvironment { timeout: Duration, key: K, keys: K* - ): ResultBuilder1[({ type lambda[x] = Option[(K, MemberScore[x])] })#lambda] = - new ResultBuilder1[({ type lambda[x] = Option[(K, MemberScore[x])] })#lambda] { - def returning[M: Schema]: IO[RedisError, Option[(K, MemberScore[M])]] = { + ): ResultBuilder1[({ type lambda[x] = Option[(K, MemberScore[x])] })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = Option[(K, MemberScore[x])] })#lambda, G] { + def returning[M: Schema]: G[Option[(K, MemberScore[M])]] = { val memberScoreOutput = Tuple3Output(ArbitraryOutput[K](), ArbitraryOutput[M](), DoubleOutput).map { case (k, m, s) => (k, MemberScore(m, s)) @@ -95,8 +94,7 @@ trait SortedSets extends RedisEnvironment { RedisCommand( BzPopMin, Tuple2(NonEmptyList(ArbitraryKeyInput[K]()), DurationSecondsInput), - OptionalOutput(memberScoreOutput), - executor + OptionalOutput(memberScoreOutput) ) command.run(((key, keys.toList), timeout)) @@ -123,7 +121,7 @@ trait SortedSets extends RedisEnvironment { final def zAdd[K: Schema, M: Schema](key: K, update: Option[Update] = None, change: Option[Changed] = None)( memberScore: MemberScore[M], memberScores: MemberScore[M]* - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( ZAdd, Tuple4( @@ -132,8 +130,7 @@ trait SortedSets extends RedisEnvironment { OptionalInput(ChangedInput), NonEmptyList(MemberScoreInput[M]()) ), - LongOutput, - executor + LongOutput ) command.run((key, update, change, (memberScore, memberScores.toList))) } @@ -161,7 +158,7 @@ trait SortedSets extends RedisEnvironment { increment: Increment, memberScore: MemberScore[M], memberScores: MemberScore[M]* - ): IO[RedisError, Option[Double]] = { + ): G[Option[Double]] = { val command = RedisCommand( ZAdd, Tuple5( @@ -171,8 +168,7 @@ trait SortedSets extends RedisEnvironment { IncrementInput, NonEmptyList(MemberScoreInput[M]()) ), - OptionalOutput(DoubleOutput), - executor + OptionalOutput(DoubleOutput) ) command.run((key, update, change, increment, (memberScore, memberScores.toList))) } @@ -185,8 +181,8 @@ trait SortedSets extends RedisEnvironment { * @return * The cardinality (number of elements) of the sorted set, or 0 if key does not exist. */ - final def zCard[K: Schema](key: K): IO[RedisError, Long] = { - val command = RedisCommand(ZCard, ArbitraryKeyInput[K](), LongOutput, executor) + final def zCard[K: Schema](key: K): G[Long] = { + val command = RedisCommand(ZCard, ArbitraryKeyInput[K](), LongOutput) command.run(key) } @@ -200,8 +196,8 @@ trait SortedSets extends RedisEnvironment { * @return * the number of elements in the specified score range. */ - final def zCount[K: Schema](key: K, range: Range): IO[RedisError, Long] = { - val command = RedisCommand(ZCount, Tuple2(ArbitraryKeyInput[K](), RangeInput), LongOutput, executor) + final def zCount[K: Schema](key: K, range: Range): G[Long] = { + val command = RedisCommand(ZCount, Tuple2(ArbitraryKeyInput[K](), RangeInput), LongOutput) command.run((key, range)) } @@ -215,9 +211,9 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of differences between the first and successive input sorted sets. */ - final def zDiff[K: Schema](key: K, keys: K*): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + final def zDiff[K: Schema](key: K, keys: K*): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZDiff, @@ -225,8 +221,7 @@ trait SortedSets extends RedisEnvironment { IntInput, NonEmptyList(ArbitraryKeyInput[K]()) ), - ChunkOutput(ArbitraryOutput[M]()), - executor + ChunkOutput(ArbitraryOutput[M]()) ) command.run((keys.size + 1, (key, keys.toList))) } @@ -242,9 +237,9 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of differences and scores between the first and successive input sorted sets. */ - final def zDiffWithScores[K: Schema](key: K, keys: K*): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + final def zDiffWithScores[K: Schema](key: K, keys: K*): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZDiff, @@ -254,8 +249,7 @@ trait SortedSets extends RedisEnvironment { WithScoresInput ), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((keys.size + 1, (key, keys.toList), WithScores)) } @@ -273,7 +267,7 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of differences between the first and successive input sorted sets. */ - final def zDiffStore[DK: Schema, K: Schema](destination: DK, key: K, keys: K*): IO[RedisError, Long] = { + final def zDiffStore[DK: Schema, K: Schema](destination: DK, key: K, keys: K*): G[Long] = { val command = RedisCommand( ZDiffStore, @@ -282,8 +276,7 @@ trait SortedSets extends RedisEnvironment { IntInput, NonEmptyList(ArbitraryKeyInput[K]()) ), - LongOutput, - executor + LongOutput ) command.run((destination, keys.size + 1, (key, keys.toList))) } @@ -300,9 +293,9 @@ trait SortedSets extends RedisEnvironment { * @return * The new score of member (a double precision floating point number). */ - final def zIncrBy[K: Schema, M: Schema](key: K, increment: Long, member: M): IO[RedisError, Double] = { + final def zIncrBy[K: Schema, M: Schema](key: K, increment: Long, member: M): G[Double] = { val command = - RedisCommand(ZIncrBy, Tuple3(ArbitraryKeyInput[K](), LongInput, ArbitraryValueInput[M]()), DoubleOutput, executor) + RedisCommand(ZIncrBy, Tuple3(ArbitraryKeyInput[K](), LongInput, ArbitraryValueInput[M]()), DoubleOutput) command.run((key, increment, member)) } @@ -325,9 +318,9 @@ trait SortedSets extends RedisEnvironment { final def zInter[K: Schema](key: K, keys: K*)( aggregate: Option[Aggregate] = None, weights: Option[::[Double]] = None - ): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + ): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZInter, Tuple4( @@ -336,8 +329,7 @@ trait SortedSets extends RedisEnvironment { OptionalInput(AggregateInput), OptionalInput(WeightsInput) ), - ChunkOutput(ArbitraryOutput[M]()), - executor + ChunkOutput(ArbitraryOutput[M]()) ) command.run((keys.size + 1, (key, keys.toList), aggregate, weights)) } @@ -362,9 +354,9 @@ trait SortedSets extends RedisEnvironment { final def zInterWithScores[K: Schema](key: K, keys: K*)( aggregate: Option[Aggregate] = None, weights: Option[::[Double]] = None - ): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + ): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZInter, Tuple5( @@ -375,8 +367,7 @@ trait SortedSets extends RedisEnvironment { WithScoresInput ), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((keys.size + 1, (key, keys.toList), aggregate, weights, WithScores)) } @@ -403,7 +394,7 @@ trait SortedSets extends RedisEnvironment { final def zInterStore[DK: Schema, K: Schema](destination: DK, key: K, keys: K*)( aggregate: Option[Aggregate] = None, weights: Option[::[Double]] = None - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( ZInterStore, Tuple5( @@ -413,8 +404,7 @@ trait SortedSets extends RedisEnvironment { OptionalInput(AggregateInput), OptionalInput(WeightsInput) ), - LongOutput, - executor + LongOutput ) command.run((destination, keys.size + 1, (key, keys.toList), aggregate, weights)) } @@ -429,12 +419,11 @@ trait SortedSets extends RedisEnvironment { * @return * The number of elements in the specified score range. */ - final def zLexCount[K: Schema](key: K, lexRange: LexRange): IO[RedisError, Long] = { + final def zLexCount[K: Schema](key: K, lexRange: LexRange): G[Long] = { val command = RedisCommand( ZLexCount, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[String](), ArbitraryValueInput[String]()), - LongOutput, - executor + LongOutput ) command.run((key, lexRange.min.asString, lexRange.max.asString)) } @@ -449,9 +438,9 @@ trait SortedSets extends RedisEnvironment { * @return * List of scores or None associated with the specified member values (a double precision floating point number). */ - final def zMScore[K: Schema](key: K, keys: K*): IO[RedisError, Chunk[Option[Double]]] = { + final def zMScore[K: Schema](key: K, keys: K*): G[Chunk[Option[Double]]] = { val command = - RedisCommand(ZMScore, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(OptionalOutput(DoubleOutput)), executor) + RedisCommand(ZMScore, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(OptionalOutput(DoubleOutput))) command.run((key, keys.toList)) } @@ -467,15 +456,14 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of popped elements and scores. */ - final def zPopMax[K: Schema](key: K, count: Option[Long] = None): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + final def zPopMax[K: Schema](key: K, count: Option[Long] = None): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZPopMax, Tuple2(ArbitraryKeyInput[K](), OptionalInput(LongInput)), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((key, count)) } @@ -493,15 +481,14 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of popped elements and scores. */ - final def zPopMin[K: Schema](key: K, count: Option[Long] = None): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + final def zPopMin[K: Schema](key: K, count: Option[Long] = None): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZPopMin, Tuple2(ArbitraryKeyInput[K](), OptionalInput(LongInput)), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((key, count)) } @@ -515,11 +502,10 @@ trait SortedSets extends RedisEnvironment { * @return * Return a random element from the sorted set value stored at key. */ - final def zRandMember[K: Schema](key: K): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[R: Schema]: IO[RedisError, Option[R]] = - RedisCommand(ZRandMember, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[R]()), executor) - .run(key) + final def zRandMember[K: Schema](key: K): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[R: Schema]: G[Option[R]] = + RedisCommand(ZRandMember, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[R]())).run(key) } /** @@ -533,14 +519,13 @@ trait SortedSets extends RedisEnvironment { * @return * Return an array of elements from the sorted set value stored at key. */ - final def zRandMember[K: Schema](key: K, count: Long): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + final def zRandMember[K: Schema](key: K, count: Long): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZRandMember, Tuple2(ArbitraryKeyInput[K](), LongInput), - ZRandMemberOutput(ArbitraryOutput[M]()), - executor + ZRandMemberOutput(ArbitraryOutput[M]()) ) command.run((key, count)) } @@ -559,15 +544,14 @@ trait SortedSets extends RedisEnvironment { * key does not exist. If the WITHSCORES modifier is used, the reply is a list elements and their scores from the * sorted set. */ - final def zRandMemberWithScores[K: Schema](key: K, count: Long): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + final def zRandMemberWithScores[K: Schema](key: K, count: Long): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZRandMember, Tuple3(ArbitraryKeyInput[K](), LongInput, WithScoresInput), ZRandMemberTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((key, count, WithScores)) @@ -584,11 +568,11 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of elements in the specified range. */ - final def zRange[K: Schema](key: K, range: Range): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + final def zRange[K: Schema](key: K, range: Range): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = - RedisCommand(ZRange, Tuple2(ArbitraryKeyInput[K](), RangeInput), ChunkOutput(ArbitraryOutput[M]()), executor) + RedisCommand(ZRange, Tuple2(ArbitraryKeyInput[K](), RangeInput), ChunkOutput(ArbitraryOutput[M]())) command.run((key, range)) } } @@ -603,15 +587,14 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of elements with their scores in the specified range. */ - final def zRangeWithScores[K: Schema](key: K, range: Range): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + final def zRangeWithScores[K: Schema](key: K, range: Range): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZRange, Tuple3(ArbitraryKeyInput[K](), RangeInput, WithScoresInput), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((key, range, WithScores)) } @@ -630,9 +613,9 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of elements in the specified score range. */ - final def zRangeByLex[K: Schema](key: K, lexRange: LexRange, limit: Option[Limit] = None): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + final def zRangeByLex[K: Schema](key: K, lexRange: LexRange, limit: Option[Limit] = None): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZRangeByLex, Tuple4( @@ -641,8 +624,7 @@ trait SortedSets extends RedisEnvironment { ArbitraryValueInput[String](), OptionalInput(LimitInput) ), - ChunkOutput(ArbitraryOutput[M]()), - executor + ChunkOutput(ArbitraryOutput[M]()) ) command.run((key, lexRange.min.asString, lexRange.max.asString, limit)) } @@ -665,9 +647,9 @@ trait SortedSets extends RedisEnvironment { key: K, scoreRange: ScoreRange, limit: Option[Limit] = None - ): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + ): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZRangeByScore, Tuple4( @@ -676,8 +658,7 @@ trait SortedSets extends RedisEnvironment { ArbitraryValueInput[String](), OptionalInput(LimitInput) ), - ChunkOutput(ArbitraryOutput[M]()), - executor + ChunkOutput(ArbitraryOutput[M]()) ) command.run((key, scoreRange.min.asString, scoreRange.max.asString, limit)) } @@ -700,9 +681,9 @@ trait SortedSets extends RedisEnvironment { key: K, scoreRange: ScoreRange, limit: Option[Limit] = None - ): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + ): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZRangeByScore, Tuple5( @@ -713,8 +694,7 @@ trait SortedSets extends RedisEnvironment { OptionalInput(LimitInput) ), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((key, scoreRange.min.asString, scoreRange.max.asString, WithScores, limit)) } @@ -730,13 +710,12 @@ trait SortedSets extends RedisEnvironment { * @return * The rank of member in the sorted set stored at key. */ - final def zRank[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[Long]] = { + final def zRank[K: Schema, M: Schema](key: K, member: M): G[Option[Long]] = { val command = RedisCommand( ZRank, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[M]()), - OptionalOutput(LongOutput), - executor + OptionalOutput(LongOutput) ) command.run((key, member)) } @@ -751,13 +730,12 @@ trait SortedSets extends RedisEnvironment { * @return * The rank of member along with the score in the sorted set stored at key. */ - final def zRankWithScore[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[RankScore]] = { + final def zRankWithScore[K: Schema, M: Schema](key: K, member: M): G[Option[RankScore]] = { val command = RedisCommand( ZRank, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[M](), WithScoreInput), - OptionalOutput(Tuple2Output(LongOutput, DoubleOutput).map { case (r, s) => RankScore(r, s) }), - executor + OptionalOutput(Tuple2Output(LongOutput, DoubleOutput).map { case (r, s) => RankScore(r, s) }) ) command.run((key, member, WithScore)) } @@ -774,9 +752,9 @@ trait SortedSets extends RedisEnvironment { * @return * The number of members removed from the sorted set, not including non existing members. */ - final def zRem[K: Schema, M: Schema](key: K, member: M, members: M*): IO[RedisError, Long] = { + final def zRem[K: Schema, M: Schema](key: K, member: M, members: M*): G[Long] = { val command = - RedisCommand(ZRem, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), LongOutput, executor) + RedisCommand(ZRem, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(ArbitraryValueInput[M]())), LongOutput) command.run((key, (member, members.toList))) } @@ -790,12 +768,11 @@ trait SortedSets extends RedisEnvironment { * @return * The number of elements removed. */ - final def zRemRangeByLex[K: Schema](key: K, lexRange: LexRange): IO[RedisError, Long] = { + final def zRemRangeByLex[K: Schema](key: K, lexRange: LexRange): G[Long] = { val command = RedisCommand( ZRemRangeByLex, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[String](), ArbitraryValueInput[String]()), - LongOutput, - executor + LongOutput ) command.run((key, lexRange.min.asString, lexRange.max.asString)) } @@ -810,8 +787,8 @@ trait SortedSets extends RedisEnvironment { * @return * The number of elements removed. */ - final def zRemRangeByRank[K: Schema](key: K, range: Range): IO[RedisError, Long] = { - val command = RedisCommand(ZRemRangeByRank, Tuple2(ArbitraryKeyInput[K](), RangeInput), LongOutput, executor) + final def zRemRangeByRank[K: Schema](key: K, range: Range): G[Long] = { + val command = RedisCommand(ZRemRangeByRank, Tuple2(ArbitraryKeyInput[K](), RangeInput), LongOutput) command.run((key, range)) } @@ -825,12 +802,11 @@ trait SortedSets extends RedisEnvironment { * @return * The number of elements removed. */ - final def zRemRangeByScore[K: Schema](key: K, scoreRange: ScoreRange): IO[RedisError, Long] = { + final def zRemRangeByScore[K: Schema](key: K, scoreRange: ScoreRange): G[Long] = { val command = RedisCommand( ZRemRangeByScore, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[String](), ArbitraryValueInput[String]()), - LongOutput, - executor + LongOutput ) command.run((key, scoreRange.min.asString, scoreRange.max.asString)) } @@ -845,14 +821,13 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of elements in the specified range. */ - final def zRevRange[K: Schema](key: K, range: Range): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + final def zRevRange[K: Schema](key: K, range: Range): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZRevRange, Tuple2(ArbitraryKeyInput[K](), RangeInput), - ChunkOutput(ArbitraryOutput[M]()), - executor + ChunkOutput(ArbitraryOutput[M]()) ) command.run((key, range)) } @@ -868,15 +843,14 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of elements with their scores in the specified range. */ - final def zRevRangeWithScores[K: Schema](key: K, range: Range): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + final def zRevRangeWithScores[K: Schema](key: K, range: Range): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZRevRange, Tuple3(ArbitraryKeyInput[K](), RangeInput, WithScoresInput), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((key, range, WithScores)) } @@ -895,9 +869,13 @@ trait SortedSets extends RedisEnvironment { * @return * Chunk of elements in the specified score range. */ - final def zRevRangeByLex[K: Schema](key: K, lexRange: LexRange, limit: Option[Limit] = None): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + final def zRevRangeByLex[K: Schema]( + key: K, + lexRange: LexRange, + limit: Option[Limit] = None + ): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZRevRangeByLex, Tuple4( @@ -906,8 +884,7 @@ trait SortedSets extends RedisEnvironment { ArbitraryValueInput[String](), OptionalInput(LimitInput) ), - ChunkOutput(ArbitraryOutput[M]()), - executor + ChunkOutput(ArbitraryOutput[M]()) ) command.run((key, lexRange.max.asString, lexRange.min.asString, limit)) } @@ -930,9 +907,9 @@ trait SortedSets extends RedisEnvironment { key: K, scoreRange: ScoreRange, limit: Option[Limit] = None - ): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + ): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZRevRangeByScore, Tuple4( @@ -941,8 +918,7 @@ trait SortedSets extends RedisEnvironment { ArbitraryValueInput[String](), OptionalInput(LimitInput) ), - ChunkOutput(ArbitraryOutput[M]()), - executor + ChunkOutput(ArbitraryOutput[M]()) ) command.run((key, scoreRange.max.asString, scoreRange.min.asString, limit)) } @@ -965,9 +941,9 @@ trait SortedSets extends RedisEnvironment { key: K, scoreRange: ScoreRange, limit: Option[Limit] = None - ): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + ): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZRevRangeByScore, Tuple5( @@ -978,8 +954,7 @@ trait SortedSets extends RedisEnvironment { OptionalInput(LimitInput) ), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((key, scoreRange.max.asString, scoreRange.min.asString, WithScores, limit)) } @@ -995,12 +970,11 @@ trait SortedSets extends RedisEnvironment { * @return * The rank of member in the sorted set stored at key. */ - final def zRevRank[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[Long]] = { + final def zRevRank[K: Schema, M: Schema](key: K, member: M): G[Option[Long]] = { val command = RedisCommand( ZRevRank, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[M]()), - OptionalOutput(LongOutput), - executor + OptionalOutput(LongOutput) ) command.run((key, member)) } @@ -1015,12 +989,11 @@ trait SortedSets extends RedisEnvironment { * @return * The rank of member along with the score in the sorted set stored at key. */ - final def zRevRankWithScore[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[RankScore]] = { + final def zRevRankWithScore[K: Schema, M: Schema](key: K, member: M): G[Option[RankScore]] = { val command = RedisCommand( ZRevRank, Tuple3(ArbitraryKeyInput[K](), ArbitraryValueInput[M](), WithScoreInput), - OptionalOutput(Tuple2Output(LongOutput, DoubleOutput).map { case (r, s) => RankScore(r, s) }), - executor + OptionalOutput(Tuple2Output(LongOutput, DoubleOutput).map { case (r, s) => RankScore(r, s) }) ) command.run((key, member, WithScore)) } @@ -1044,9 +1017,9 @@ trait SortedSets extends RedisEnvironment { cursor: Long, pattern: Option[String] = None, count: Option[Count] = None - ): ResultBuilder1[({ type lambda[x] = (Long, MemberScores[x]) })#lambda] = - new ResultBuilder1[({ type lambda[x] = (Long, MemberScores[x]) })#lambda] { - def returning[M: Schema]: IO[RedisError, (Long, Chunk[MemberScore[M]])] = { + ): ResultBuilder1[({ type lambda[x] = (Long, MemberScores[x]) })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = (Long, MemberScores[x]) })#lambda, G] { + def returning[M: Schema]: G[(Long, Chunk[MemberScore[M]])] = { val memberScoresOutput = ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput).map(_.map { case (m, s) => MemberScore(m, s) }) @@ -1054,8 +1027,7 @@ trait SortedSets extends RedisEnvironment { RedisCommand( ZScan, Tuple4(ArbitraryKeyInput[K](), LongInput, OptionalInput(PatternInput), OptionalInput(CountInput)), - Tuple2Output(MultiStringOutput.map(_.toLong), memberScoresOutput), - executor + Tuple2Output(MultiStringOutput.map(_.toLong), memberScoresOutput) ) command.run((key, cursor, pattern.map(Pattern(_)), count)) @@ -1072,12 +1044,11 @@ trait SortedSets extends RedisEnvironment { * @return * The score of member (a double precision floating point number. */ - final def zScore[K: Schema, M: Schema](key: K, member: M): IO[RedisError, Option[Double]] = { + final def zScore[K: Schema, M: Schema](key: K, member: M): G[Option[Double]] = { val command = RedisCommand( ZScore, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[M]()), - OptionalOutput(DoubleOutput), - executor + OptionalOutput(DoubleOutput) ) command.run((key, member)) } @@ -1101,9 +1072,9 @@ trait SortedSets extends RedisEnvironment { final def zUnion[K: Schema](key: K, keys: K*)( weights: Option[::[Double]] = None, aggregate: Option[Aggregate] = None - ): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[M: Schema]: IO[RedisError, Chunk[M]] = { + ): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[M: Schema]: G[Chunk[M]] = { val command = RedisCommand( ZUnion, @@ -1113,8 +1084,7 @@ trait SortedSets extends RedisEnvironment { OptionalInput(WeightsInput), OptionalInput(AggregateInput) ), - ChunkOutput(ArbitraryOutput[M]()), - executor + ChunkOutput(ArbitraryOutput[M]()) ) command.run((keys.size + 1, (key, keys.toList), weights, aggregate)) } @@ -1139,9 +1109,9 @@ trait SortedSets extends RedisEnvironment { final def zUnionWithScores[K: Schema](key: K, keys: K*)( weights: Option[::[Double]] = None, aggregate: Option[Aggregate] = None - ): ResultBuilder1[MemberScores] = - new ResultBuilder1[MemberScores] { - def returning[M: Schema]: IO[RedisError, Chunk[MemberScore[M]]] = { + ): ResultBuilder1[MemberScores, G] = + new ResultBuilder1[MemberScores, G] { + def returning[M: Schema]: G[Chunk[MemberScore[M]]] = { val command = RedisCommand( ZUnion, @@ -1153,8 +1123,7 @@ trait SortedSets extends RedisEnvironment { WithScoresInput ), ChunkTuple2Output(ArbitraryOutput[M](), DoubleOutput) - .map(_.map { case (m, s) => MemberScore(m, s) }), - executor + .map(_.map { case (m, s) => MemberScore(m, s) }) ) command.run((keys.size + 1, (key, keys.toList), weights, aggregate, WithScores)) } @@ -1181,7 +1150,7 @@ trait SortedSets extends RedisEnvironment { final def zUnionStore[DK: Schema, K: Schema](destination: DK, key: K, keys: K*)( weights: Option[::[Double]] = None, aggregate: Option[Aggregate] = None - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( ZUnionStore, Tuple5( @@ -1191,8 +1160,7 @@ trait SortedSets extends RedisEnvironment { OptionalInput(WeightsInput), OptionalInput(AggregateInput) ), - LongOutput, - executor + LongOutput ) command.run((destination, keys.size + 1, (key, keys.toList), weights, aggregate)) } diff --git a/modules/redis/src/main/scala/zio/redis/api/Streams.scala b/modules/redis/src/main/scala/zio/redis/api/Streams.scala index 1fea46239..df4e982ea 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Streams.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Streams.scala @@ -24,7 +24,7 @@ import zio.redis._ import zio.redis.internal.{RedisCommand, RedisEnvironment} import zio.schema.Schema -trait Streams extends RedisEnvironment { +trait Streams[G[+_]] extends RedisEnvironment[G] { import StreamInfoWithFull._ import Streams._ import XGroupCommand._ @@ -43,17 +43,16 @@ trait Streams extends RedisEnvironment { * @return * the number of messages successfully acknowledged. */ - final def xAck[SK: Schema, G: Schema, I: Schema]( + final def xAck[SK: Schema, GG: Schema, I: Schema]( key: SK, - group: G, + group: GG, id: I, ids: I* - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( XAck, - Tuple3(ArbitraryKeyInput[SK](), ArbitraryValueInput[G](), NonEmptyList(ArbitraryValueInput[I]())), - LongOutput, - executor + Tuple3(ArbitraryKeyInput[SK](), ArbitraryValueInput[GG](), NonEmptyList(ArbitraryValueInput[I]())), + LongOutput ) command.run((key, group, (id, ids.toList))) } @@ -77,9 +76,9 @@ trait Streams extends RedisEnvironment { id: I, pair: (K, V), pairs: (K, V)* - ): ResultBuilder1[Id] = - new ResultBuilder1[Id] { - def returning[R: Schema]: IO[RedisError, Id[R]] = { + ): ResultBuilder1[Id, G] = + new ResultBuilder1[Id, G] { + def returning[R: Schema]: G[Id[R]] = { val command = RedisCommand( XAdd, Tuple4( @@ -88,8 +87,7 @@ trait Streams extends RedisEnvironment { ArbitraryValueInput[I](), NonEmptyList(Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]())) ), - ArbitraryOutput[R](), - executor + ArbitraryOutput[R]() ) command.run((key, None, id, (pair, pairs.toList))) } @@ -121,9 +119,9 @@ trait Streams extends RedisEnvironment { )( pair: (K, V), pairs: (K, V)* - ): ResultBuilder1[Id] = - new ResultBuilder1[Id] { - def returning[R: Schema]: IO[RedisError, Id[R]] = { + ): ResultBuilder1[Id, G] = + new ResultBuilder1[Id, G] { + def returning[R: Schema]: G[Id[R]] = { val command = RedisCommand( XAdd, Tuple4( @@ -132,8 +130,7 @@ trait Streams extends RedisEnvironment { ArbitraryValueInput[I](), NonEmptyList(Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]())) ), - ArbitraryOutput[R](), - executor + ArbitraryOutput[R]() ) command.run((key, Some(StreamMaxLen(approximate, count)), id, (pair, pairs.toList))) } @@ -175,9 +172,9 @@ trait Streams extends RedisEnvironment { time: Option[Duration] = None, retryCount: Option[Long] = None, force: Boolean = false - )(id: I, ids: I*): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] = - new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] { - def returning[RK: Schema, RV: Schema]: IO[RedisError, StreamEntries[I, RK, RV]] = { + )(id: I, ids: I*): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] = + new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] { + def returning[RK: Schema, RV: Schema]: G[StreamEntries[I, RK, RV]] = { val command = RedisCommand( XClaim, @@ -192,8 +189,7 @@ trait Streams extends RedisEnvironment { OptionalInput(RetryCountInput), OptionalInput(WithForceInput) ), - StreamEntriesOutput[I, RK, RV](), - executor + StreamEntriesOutput[I, RK, RV]() ) val forceOpt = if (force) Some(WithForce) else None @@ -237,9 +233,9 @@ trait Streams extends RedisEnvironment { time: Option[Duration] = None, retryCount: Option[Long] = None, force: Boolean = false - )(id: I, ids: I*): ResultBuilder1[Chunk] = - new ResultBuilder1[Chunk] { - def returning[R: Schema]: IO[RedisError, Chunk[R]] = { + )(id: I, ids: I*): ResultBuilder1[Chunk, G] = + new ResultBuilder1[Chunk, G] { + def returning[R: Schema]: G[Chunk[R]] = { val command = RedisCommand( XClaim, Tuple10( @@ -254,8 +250,7 @@ trait Streams extends RedisEnvironment { OptionalInput(WithForceInput), WithJustIdInput ), - ChunkOutput(ArbitraryOutput[R]()), - executor + ChunkOutput(ArbitraryOutput[R]()) ) val forceOpt = if (force) Some(WithForce) else None command.run((key, group, consumer, minIdleTime, (id, ids.toList), idle, time, retryCount, forceOpt, WithJustId)) @@ -274,9 +269,9 @@ trait Streams extends RedisEnvironment { * @return * the number of entries deleted. */ - final def xDel[SK: Schema, I: Schema](key: SK, id: I, ids: I*): IO[RedisError, Long] = { + final def xDel[SK: Schema, I: Schema](key: SK, id: I, ids: I*): G[Long] = { val command = - RedisCommand(XDel, Tuple2(ArbitraryKeyInput[SK](), NonEmptyList(ArbitraryValueInput[I]())), LongOutput, executor) + RedisCommand(XDel, Tuple2(ArbitraryKeyInput[SK](), NonEmptyList(ArbitraryValueInput[I]())), LongOutput) command.run((key, (id, ids.toList))) } @@ -297,8 +292,8 @@ trait Streams extends RedisEnvironment { group: SG, id: I, mkStream: Boolean = false - ): IO[RedisError, Unit] = { - val command = RedisCommand(XGroup, XGroupCreateInput[SK, SG, I](), UnitOutput, executor) + ): G[Unit] = { + val command = RedisCommand(XGroup, XGroupCreateInput[SK, SG, I](), UnitOutput) command.run(Create(key, group, id, mkStream)) } @@ -318,8 +313,8 @@ trait Streams extends RedisEnvironment { key: SK, group: SG, consumer: SC - ): IO[RedisError, Boolean] = { - val command = RedisCommand(XGroup, XGroupCreateConsumerInput[SK, SG, SC](), BoolOutput, executor) + ): G[Boolean] = { + val command = RedisCommand(XGroup, XGroupCreateConsumerInput[SK, SG, SC](), BoolOutput) command.run(CreateConsumer(key, group, consumer)) } @@ -339,8 +334,8 @@ trait Streams extends RedisEnvironment { key: SK, group: SG, consumer: SC - ): IO[RedisError, Long] = { - val command = RedisCommand(XGroup, XGroupDelConsumerInput[SK, SG, SC](), LongOutput, executor) + ): G[Long] = { + val command = RedisCommand(XGroup, XGroupDelConsumerInput[SK, SG, SC](), LongOutput) command.run(DelConsumer(key, group, consumer)) } @@ -354,8 +349,8 @@ trait Streams extends RedisEnvironment { * @return * flag that indicates if the deletion was successful. */ - final def xGroupDestroy[SK: Schema, SG: Schema](key: SK, group: SG): IO[RedisError, Boolean] = - RedisCommand(XGroup, XGroupDestroyInput[SK, SG](), BoolOutput, executor).run(Destroy(key, group)) + final def xGroupDestroy[SK: Schema, SG: Schema](key: SK, group: SG): G[Boolean] = + RedisCommand(XGroup, XGroupDestroyInput[SK, SG](), BoolOutput).run(Destroy(key, group)) /** * Set the consumer group last delivered ID to something else. @@ -371,8 +366,8 @@ trait Streams extends RedisEnvironment { key: SK, group: SG, id: I - ): IO[RedisError, Unit] = { - val command = RedisCommand(XGroup, XGroupSetIdInput[SK, SG, I](), UnitOutput, executor) + ): G[Unit] = { + val command = RedisCommand(XGroup, XGroupSetIdInput[SK, SG, I](), UnitOutput) command.run(SetId(key, group, id)) } @@ -389,13 +384,12 @@ trait Streams extends RedisEnvironment { final def xInfoConsumers[SK: Schema, SG: Schema]( key: SK, group: SG - ): IO[RedisError, Chunk[StreamConsumersInfo]] = { + ): G[Chunk[StreamConsumersInfo]] = { val command = RedisCommand( XInfoConsumers, Tuple2(ArbitraryKeyInput[SK](), ArbitraryValueInput[SG]()), - StreamConsumersInfoOutput, - executor + StreamConsumersInfoOutput ) command.run((key, group)) } @@ -408,8 +402,8 @@ trait Streams extends RedisEnvironment { * @return * List of consumer groups associated with the stream stored at the specified key. */ - final def xInfoGroups[SK: Schema](key: SK): IO[RedisError, Chunk[StreamGroupsInfo]] = { - val command = RedisCommand(XInfoGroups, ArbitraryKeyInput[SK](), StreamGroupsInfoOutput, executor) + final def xInfoGroups[SK: Schema](key: SK): G[Chunk[StreamGroupsInfo]] = { + val command = RedisCommand(XInfoGroups, ArbitraryKeyInput[SK](), StreamGroupsInfoOutput) command.run(key) } @@ -423,9 +417,9 @@ trait Streams extends RedisEnvironment { */ final def xInfoStream[SK: Schema]( key: SK - ): ResultBuilder3[StreamInfo] = new ResultBuilder3[StreamInfo] { - def returning[RI: Schema, RK: Schema, RV: Schema]: IO[RedisError, StreamInfo[RI, RK, RV]] = { - val command = RedisCommand(XInfoStream, ArbitraryKeyInput[SK](), StreamInfoOutput[RI, RK, RV](), executor) + ): ResultBuilder3[StreamInfo, G] = new ResultBuilder3[StreamInfo, G] { + def returning[RI: Schema, RK: Schema, RV: Schema]: G[StreamInfo[RI, RK, RV]] = { + val command = RedisCommand(XInfoStream, ArbitraryKeyInput[SK](), StreamInfoOutput[RI, RK, RV]()) command.run(key) } } @@ -440,13 +434,12 @@ trait Streams extends RedisEnvironment { */ final def xInfoStreamFull[SK: Schema]( key: SK - ): ResultBuilder3[FullStreamInfo] = new ResultBuilder3[FullStreamInfo] { - def returning[RI: Schema, RK: Schema, RV: Schema]: IO[RedisError, FullStreamInfo[RI, RK, RV]] = { + ): ResultBuilder3[FullStreamInfo, G] = new ResultBuilder3[FullStreamInfo, G] { + def returning[RI: Schema, RK: Schema, RV: Schema]: G[FullStreamInfo[RI, RK, RV]] = { val command = RedisCommand( XInfoStream, Tuple2(ArbitraryKeyInput[SK](), ArbitraryValueInput[String]()), - StreamInfoFullOutput[RI, RK, RV](), - executor + StreamInfoFullOutput[RI, RK, RV]() ) command.run((key, "FULL")) } @@ -465,13 +458,12 @@ trait Streams extends RedisEnvironment { final def xInfoStreamFull[SK: Schema]( key: SK, count: Long - ): ResultBuilder3[FullStreamInfo] = new ResultBuilder3[FullStreamInfo] { - def returning[RI: Schema, RK: Schema, RV: Schema]: IO[RedisError, FullStreamInfo[RI, RK, RV]] = { + ): ResultBuilder3[FullStreamInfo, G] = new ResultBuilder3[FullStreamInfo, G] { + def returning[RI: Schema, RK: Schema, RV: Schema]: G[FullStreamInfo[RI, RK, RV]] = { val command = RedisCommand( XInfoStream, Tuple3(ArbitraryKeyInput[SK](), ArbitraryValueInput[String](), CountInput), - StreamInfoFullOutput[RI, RK, RV](), - executor + StreamInfoFullOutput[RI, RK, RV]() ) command.run((key, "FULL", Count(count))) } @@ -485,8 +477,8 @@ trait Streams extends RedisEnvironment { * @return * the number of entries inside a stream. */ - final def xLen[SK: Schema](key: SK): IO[RedisError, Long] = { - val command = RedisCommand(XLen, ArbitraryKeyInput[SK](), LongOutput, executor) + final def xLen[SK: Schema](key: SK): G[Long] = { + val command = RedisCommand(XLen, ArbitraryKeyInput[SK](), LongOutput) command.run(key) } @@ -500,12 +492,11 @@ trait Streams extends RedisEnvironment { * @return * summary about the pending messages in a given consumer group. */ - final def xPending[SK: Schema, SG: Schema](key: SK, group: SG): IO[RedisError, PendingInfo] = { + final def xPending[SK: Schema, SG: Schema](key: SK, group: SG): G[PendingInfo] = { val command = RedisCommand( XPending, Tuple3(ArbitraryKeyInput[SK](), ArbitraryValueInput[SG](), OptionalInput(IdleInput)), - XPendingOutput, - executor + XPendingOutput ) command.run((key, group, None)) } @@ -538,7 +529,7 @@ trait Streams extends RedisEnvironment { count: Long, consumer: Option[SC] = None, idle: Option[Duration] = None - ): IO[RedisError, Chunk[PendingMessage]] = { + ): G[Chunk[PendingMessage]] = { val command = RedisCommand( XPending, Tuple7( @@ -550,8 +541,7 @@ trait Streams extends RedisEnvironment { LongInput, OptionalInput(ArbitraryValueInput[SC]()) ), - PendingMessagesOutput, - executor + PendingMessagesOutput ) command.run((key, group, idle, start, end, count, consumer)) } @@ -572,9 +562,9 @@ trait Streams extends RedisEnvironment { key: SK, start: I, end: I - ): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] = - new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] { - def returning[RK: Schema, RV: Schema]: IO[RedisError, StreamEntries[I, RK, RV]] = { + ): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] = + new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] { + def returning[RK: Schema, RV: Schema]: G[StreamEntries[I, RK, RV]] = { val command = RedisCommand( XRange, Tuple4( @@ -583,8 +573,7 @@ trait Streams extends RedisEnvironment { ArbitraryValueInput[I](), OptionalInput(CountInput) ), - StreamEntriesOutput[I, RK, RV](), - executor + StreamEntriesOutput[I, RK, RV]() ) command.run((key, start, end, None)) } @@ -609,9 +598,9 @@ trait Streams extends RedisEnvironment { start: I, end: I, count: Long - ): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] = - new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] { - def returning[RK: Schema, RV: Schema]: IO[RedisError, StreamEntries[I, RK, RV]] = { + ): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] = + new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] { + def returning[RK: Schema, RV: Schema]: G[StreamEntries[I, RK, RV]] = { val command = RedisCommand( XRange, Tuple4( @@ -620,8 +609,7 @@ trait Streams extends RedisEnvironment { ArbitraryValueInput[I](), OptionalInput(CountInput) ), - StreamEntriesOutput[I, RK, RV](), - executor + StreamEntriesOutput[I, RK, RV]() ) command.run((key, start, end, Some(Count(count)))) } @@ -647,14 +635,13 @@ trait Streams extends RedisEnvironment { )( stream: (SK, I), streams: (SK, I)* - ): ResultBuilder2[({ type lambda[x, y] = StreamChunks[SK, I, x, y] })#lambda] = - new ResultBuilder2[({ type lambda[x, y] = StreamChunks[SK, I, x, y] })#lambda] { - def returning[RK: Schema, RV: Schema]: IO[RedisError, StreamChunks[SK, I, RK, RV]] = { + ): ResultBuilder2[({ type lambda[x, y] = StreamChunks[SK, I, x, y] })#lambda, G] = + new ResultBuilder2[({ type lambda[x, y] = StreamChunks[SK, I, x, y] })#lambda, G] { + def returning[RK: Schema, RV: Schema]: G[StreamChunks[SK, I, RK, RV]] = { val command = RedisCommand( XRead, Tuple3(OptionalInput(CountInput), OptionalInput(BlockInput), StreamsInput[SK, I]()), - ChunkOutput(StreamOutput[SK, I, RK, RV]()), - executor + ChunkOutput(StreamOutput[SK, I, RK, RV]()) ) command.run((count.map(Count(_)), block, (stream, Chunk.fromIterable(streams)))) } @@ -689,9 +676,9 @@ trait Streams extends RedisEnvironment { )( stream: (SK, I), streams: (SK, I)* - ): ResultBuilder2[({ type lambda[x, y] = StreamChunks[SK, I, x, y] })#lambda] = - new ResultBuilder2[({ type lambda[x, y] = StreamChunks[SK, I, x, y] })#lambda] { - def returning[RK: Schema, RV: Schema]: IO[RedisError, StreamChunks[SK, I, RK, RV]] = { + ): ResultBuilder2[({ type lambda[x, y] = StreamChunks[SK, I, x, y] })#lambda, G] = + new ResultBuilder2[({ type lambda[x, y] = StreamChunks[SK, I, x, y] })#lambda, G] { + def returning[RK: Schema, RV: Schema]: G[StreamChunks[SK, I, RK, RV]] = { val command = RedisCommand( XReadGroup, @@ -703,8 +690,7 @@ trait Streams extends RedisEnvironment { OptionalInput(NoAckInput), StreamsInput[SK, I]() ), - ChunkOutput(StreamOutput[SK, I, RK, RV]()), - executor + ChunkOutput(StreamOutput[SK, I, RK, RV]()) ) val noAckOpt = if (noAck) Some(NoAck) else None @@ -728,9 +714,9 @@ trait Streams extends RedisEnvironment { key: SK, end: I, start: I - ): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] = - new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] { - def returning[RK: Schema, RV: Schema]: IO[RedisError, StreamEntries[I, RK, RV]] = { + ): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] = + new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] { + def returning[RK: Schema, RV: Schema]: G[StreamEntries[I, RK, RV]] = { val command = RedisCommand( XRevRange, Tuple4( @@ -739,8 +725,7 @@ trait Streams extends RedisEnvironment { ArbitraryValueInput[I](), OptionalInput(CountInput) ), - StreamEntriesOutput[I, RK, RV](), - executor + StreamEntriesOutput[I, RK, RV]() ) command.run((key, end, start, None)) } @@ -765,9 +750,9 @@ trait Streams extends RedisEnvironment { end: I, start: I, count: Long - ): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] = - new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda] { - def returning[RK: Schema, RV: Schema]: IO[RedisError, StreamEntries[I, RK, RV]] = { + ): ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] = + new ResultBuilder2[({ type lambda[x, y] = StreamEntries[I, x, y] })#lambda, G] { + def returning[RK: Schema, RV: Schema]: G[StreamEntries[I, RK, RV]] = { val command = RedisCommand( XRevRange, Tuple4( @@ -776,8 +761,7 @@ trait Streams extends RedisEnvironment { ArbitraryValueInput[I](), OptionalInput(CountInput) ), - StreamEntriesOutput[I, RK, RV](), - executor + StreamEntriesOutput[I, RK, RV]() ) command.run((key, end, start, Some(Count(count)))) } @@ -799,8 +783,8 @@ trait Streams extends RedisEnvironment { key: SK, count: Long, approximate: Boolean = false - ): IO[RedisError, Long] = { - val command = RedisCommand(XTrim, Tuple2(ArbitraryKeyInput[SK](), StreamMaxLenInput), LongOutput, executor) + ): G[Long] = { + val command = RedisCommand(XTrim, Tuple2(ArbitraryKeyInput[SK](), StreamMaxLenInput), LongOutput) command.run((key, StreamMaxLen(approximate, count))) } } diff --git a/modules/redis/src/main/scala/zio/redis/api/Strings.scala b/modules/redis/src/main/scala/zio/redis/api/Strings.scala index 8a5087bff..a2c50d78b 100644 --- a/modules/redis/src/main/scala/zio/redis/api/Strings.scala +++ b/modules/redis/src/main/scala/zio/redis/api/Strings.scala @@ -26,7 +26,7 @@ import zio.schema.Schema import java.time.Instant -trait Strings extends RedisEnvironment { +trait Strings[G[+_]] extends RedisEnvironment[G] { import Strings._ /** @@ -39,9 +39,9 @@ trait Strings extends RedisEnvironment { * @return * Returns the length of the string after the append operation. */ - final def append[K: Schema, V: Schema](key: K, value: V): IO[RedisError, Long] = { + final def append[K: Schema, V: Schema](key: K, value: V): G[Long] = { val command = - RedisCommand(Append, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]()), LongOutput, executor) + RedisCommand(Append, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]()), LongOutput) command.run((key, value)) } @@ -55,9 +55,9 @@ trait Strings extends RedisEnvironment { * @return * Returns the number of bits set to 1. */ - final def bitCount[K: Schema](key: K, range: Option[Range] = None): IO[RedisError, Long] = { + final def bitCount[K: Schema](key: K, range: Option[Range] = None): G[Long] = { val command = - RedisCommand(BitCount, Tuple2(ArbitraryKeyInput[K](), OptionalInput(RangeInput)), LongOutput, executor) + RedisCommand(BitCount, Tuple2(ArbitraryKeyInput[K](), OptionalInput(RangeInput)), LongOutput) command.run((key, range)) } @@ -77,12 +77,11 @@ trait Strings extends RedisEnvironment { key: K, bitFieldCommand: BitFieldCommand, bitFieldCommands: BitFieldCommand* - ): IO[RedisError, Chunk[Option[Long]]] = { + ): G[Chunk[Option[Long]]] = { val command = RedisCommand( BitField, Tuple2(ArbitraryKeyInput[K](), NonEmptyList(BitFieldCommandInput)), - ChunkOutput(OptionalOutput(LongOutput)), - executor + ChunkOutput(OptionalOutput(LongOutput)) ) command.run((key, (bitFieldCommand, bitFieldCommands.toList))) } @@ -106,13 +105,12 @@ trait Strings extends RedisEnvironment { destKey: D, srcKey: S, srcKeys: S* - ): IO[RedisError, Long] = { + ): G[Long] = { val command = RedisCommand( BitOp, Tuple3(BitOperationInput, ArbitraryValueInput[D](), NonEmptyList(ArbitraryValueInput[S]())), - LongOutput, - executor + LongOutput ) command.run((operation, destKey, (srcKey, srcKeys.toList))) } @@ -133,14 +131,9 @@ trait Strings extends RedisEnvironment { key: K, bit: Boolean, range: Option[BitPosRange] = None - ): IO[RedisError, Long] = { + ): G[Long] = { val command = - RedisCommand( - BitPos, - Tuple3(ArbitraryKeyInput[K](), BoolInput, OptionalInput(BitPosRangeInput)), - LongOutput, - executor - ) + RedisCommand(BitPos, Tuple3(ArbitraryKeyInput[K](), BoolInput, OptionalInput(BitPosRangeInput)), LongOutput) command.run((key, bit, range)) } @@ -152,8 +145,8 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of key after the decrement. */ - final def decr[K: Schema](key: K): IO[RedisError, Long] = { - val command = RedisCommand(Decr, ArbitraryKeyInput[K](), LongOutput, executor) + final def decr[K: Schema](key: K): G[Long] = { + val command = RedisCommand(Decr, ArbitraryKeyInput[K](), LongOutput) command.run(key) } @@ -167,8 +160,8 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of key after the decrement. */ - final def decrBy[K: Schema](key: K, decrement: Long): IO[RedisError, Long] = { - val command = RedisCommand(DecrBy, Tuple2(ArbitraryKeyInput[K](), LongInput), LongOutput, executor) + final def decrBy[K: Schema](key: K, decrement: Long): G[Long] = { + val command = RedisCommand(DecrBy, Tuple2(ArbitraryKeyInput[K](), LongInput), LongOutput) command.run((key, decrement)) } @@ -180,10 +173,10 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of the string or None if it does not exist. */ - final def get[K: Schema](key: K): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[R: Schema]: IO[RedisError, Option[R]] = - RedisCommand(Get, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[R]()), executor).run(key) + final def get[K: Schema](key: K): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[R: Schema]: G[Option[R]] = + RedisCommand(Get, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[R]())).run(key) } /** @@ -196,8 +189,8 @@ trait Strings extends RedisEnvironment { * @return * Returns the bit value stored at offset. */ - final def getBit[K: Schema](key: K, offset: Long): IO[RedisError, Long] = { - val command = RedisCommand(GetBit, Tuple2(ArbitraryKeyInput[K](), LongInput), LongOutput, executor) + final def getBit[K: Schema](key: K, offset: Long): G[Long] = { + val command = RedisCommand(GetBit, Tuple2(ArbitraryKeyInput[K](), LongInput), LongOutput) command.run((key, offset)) } @@ -209,10 +202,10 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of the string or None if it did not previously have a value. */ - final def getDel[K: Schema](key: K): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[R: Schema]: IO[RedisError, Option[R]] = - RedisCommand(GetDel, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[R]()), executor).run(key) + final def getDel[K: Schema](key: K): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[R: Schema]: G[Option[R]] = + RedisCommand(GetDel, ArbitraryKeyInput[K](), OptionalOutput(ArbitraryOutput[R]())).run(key) } /** @@ -228,11 +221,10 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of the string or None if it did not previously have a value. */ - final def getEx[K: Schema](key: K, expire: Expire, expireTime: Duration): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[R: Schema]: IO[RedisError, Option[R]] = - RedisCommand(GetEx, GetExInput[K](), OptionalOutput(ArbitraryOutput[R]()), executor) - .run((key, expire, expireTime)) + final def getEx[K: Schema](key: K, expire: Expire, expireTime: Duration): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[R: Schema]: G[Option[R]] = + RedisCommand(GetEx, GetExInput[K](), OptionalOutput(ArbitraryOutput[R]())).run((key, expire, expireTime)) } /** @@ -248,11 +240,10 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of the string or None if it did not previously have a value. */ - final def getEx[K: Schema](key: K, expiredAt: ExpiredAt, timestamp: Instant): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[R: Schema]: IO[RedisError, Option[R]] = - RedisCommand(GetEx, GetExAtInput[K](), OptionalOutput(ArbitraryOutput[R]()), executor) - .run((key, expiredAt, timestamp)) + final def getEx[K: Schema](key: K, expiredAt: ExpiredAt, timestamp: Instant): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[R: Schema]: G[Option[R]] = + RedisCommand(GetEx, GetExAtInput[K](), OptionalOutput(ArbitraryOutput[R]())).run((key, expiredAt, timestamp)) } /** @@ -265,11 +256,10 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of the string or None if it did not previously have a value. */ - final def getEx[K: Schema](key: K, persist: Boolean): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[R: Schema]: IO[RedisError, Option[R]] = - RedisCommand(GetEx, GetExPersistInput[K](), OptionalOutput(ArbitraryOutput[R]()), executor) - .run((key, persist)) + final def getEx[K: Schema](key: K, persist: Boolean): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[R: Schema]: G[Option[R]] = + RedisCommand(GetEx, GetExPersistInput[K](), OptionalOutput(ArbitraryOutput[R]())).run((key, persist)) } /** @@ -282,15 +272,10 @@ trait Strings extends RedisEnvironment { * @return * Returns the substring. */ - final def getRange[K: Schema](key: K, range: Range): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[R: Schema]: IO[RedisError, Option[R]] = - RedisCommand( - GetRange, - Tuple2(ArbitraryKeyInput[K](), RangeInput), - OptionalOutput(ArbitraryOutput[R]()), - executor - ) + final def getRange[K: Schema](key: K, range: Range): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[R: Schema]: G[Option[R]] = + RedisCommand(GetRange, Tuple2(ArbitraryKeyInput[K](), RangeInput), OptionalOutput(ArbitraryOutput[R]())) .run((key, range)) } @@ -304,16 +289,14 @@ trait Strings extends RedisEnvironment { * @return * Returns the previous value of the string or None if it did not previously have a value. */ - final def getSet[K: Schema, V: Schema](key: K, value: V): ResultBuilder1[Option] = - new ResultBuilder1[Option] { - def returning[R: Schema]: IO[RedisError, Option[R]] = + final def getSet[K: Schema, V: Schema](key: K, value: V): ResultBuilder1[Option, G] = + new ResultBuilder1[Option, G] { + def returning[R: Schema]: G[Option[R]] = RedisCommand( GetSet, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]()), - OptionalOutput(ArbitraryOutput[R]()), - executor - ) - .run((key, value)) + OptionalOutput(ArbitraryOutput[R]()) + ).run((key, value)) } /** @@ -324,8 +307,8 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of key after the increment. */ - final def incr[K: Schema](key: K): IO[RedisError, Long] = { - val command = RedisCommand(Incr, ArbitraryKeyInput[K](), LongOutput, executor) + final def incr[K: Schema](key: K): G[Long] = { + val command = RedisCommand(Incr, ArbitraryKeyInput[K](), LongOutput) command.run(key) } @@ -339,9 +322,9 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of key after the increment. */ - final def incrBy[K: Schema](key: K, increment: Long): IO[RedisError, Long] = { + final def incrBy[K: Schema](key: K, increment: Long): G[Long] = { val command = - RedisCommand(IncrBy, Tuple2(ArbitraryKeyInput[K](), LongInput), LongOutput, executor) + RedisCommand(IncrBy, Tuple2(ArbitraryKeyInput[K](), LongInput), LongOutput) command.run((key, increment)) } @@ -355,8 +338,8 @@ trait Strings extends RedisEnvironment { * @return * Returns the value of key after the increment. */ - final def incrByFloat[K: Schema](key: K, increment: Double): IO[RedisError, Double] = { - val command = RedisCommand(IncrByFloat, Tuple2(ArbitraryKeyInput[K](), DoubleInput), DoubleOutput, executor) + final def incrByFloat[K: Schema](key: K, increment: Double): G[Double] = { + val command = RedisCommand(IncrByFloat, Tuple2(ArbitraryKeyInput[K](), DoubleInput), DoubleOutput) command.run((key, increment)) } @@ -375,7 +358,7 @@ trait Strings extends RedisEnvironment { * length and all the ranges in both the strings, start and end offset for each string, where there are matches. * When withMatchLen is given each array representing a match will also have the length of the match (see examples). */ - final def lcs[K: Schema](keyA: K, keyB: K, lcsQueryType: Option[LcsQueryType] = None): IO[RedisError, Lcs] = { + final def lcs[K: Schema](keyA: K, keyB: K, lcsQueryType: Option[LcsQueryType] = None): G[Lcs] = { val redisCommand = RedisCommand( Lcs, Tuple3( @@ -383,8 +366,7 @@ trait Strings extends RedisEnvironment { ArbitraryKeyInput[K](), OptionalInput(LcsQueryTypeInput) ), - LcsOutput, - executor + LcsOutput ) redisCommand.run((keyA, keyB, lcsQueryType)) } @@ -402,16 +384,11 @@ trait Strings extends RedisEnvironment { final def mGet[K: Schema]( key: K, keys: K* - ): ResultBuilder1[({ type lambda[x] = Chunk[Option[x]] })#lambda] = - new ResultBuilder1[({ type lambda[x] = Chunk[Option[x]] })#lambda] { - def returning[V: Schema]: IO[RedisError, Chunk[Option[V]]] = { + ): ResultBuilder1[({ type lambda[x] = Chunk[Option[x]] })#lambda, G] = + new ResultBuilder1[({ type lambda[x] = Chunk[Option[x]] })#lambda, G] { + def returning[V: Schema]: G[Chunk[Option[V]]] = { val command = - RedisCommand( - MGet, - NonEmptyList(ArbitraryKeyInput[K]()), - ChunkOutput(OptionalOutput(ArbitraryOutput[V]())), - executor - ) + RedisCommand(MGet, NonEmptyList(ArbitraryKeyInput[K]()), ChunkOutput(OptionalOutput(ArbitraryOutput[V]()))) command.run((key, keys.toList)) } } @@ -424,9 +401,9 @@ trait Strings extends RedisEnvironment { * @param keyValues * Subsequent tuples of key values */ - final def mSet[K: Schema, V: Schema](keyValue: (K, V), keyValues: (K, V)*): IO[RedisError, Unit] = { + final def mSet[K: Schema, V: Schema](keyValue: (K, V), keyValues: (K, V)*): G[Unit] = { val command = - RedisCommand(MSet, NonEmptyList(Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]())), UnitOutput, executor) + RedisCommand(MSet, NonEmptyList(Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]())), UnitOutput) command.run((keyValue, keyValues.toList)) } @@ -443,9 +420,9 @@ trait Strings extends RedisEnvironment { final def mSetNx[K: Schema, V: Schema]( keyValue: (K, V), keyValues: (K, V)* - ): IO[RedisError, Boolean] = { + ): G[Boolean] = { val command = - RedisCommand(MSetNx, NonEmptyList(Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]())), BoolOutput, executor) + RedisCommand(MSetNx, NonEmptyList(Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]())), BoolOutput) command.run((keyValue, keyValues.toList)) } @@ -463,13 +440,12 @@ trait Strings extends RedisEnvironment { key: K, milliseconds: Duration, value: V - ): IO[RedisError, Unit] = { + ): G[Unit] = { val command = RedisCommand( PSetEx, Tuple3(ArbitraryKeyInput[K](), DurationMillisecondsInput, ArbitraryValueInput[V]()), - UnitOutput, - executor + UnitOutput ) command.run((key, milliseconds, value)) } @@ -497,7 +473,7 @@ trait Strings extends RedisEnvironment { expireTime: Option[Duration] = None, update: Option[Update] = None, keepTtl: Option[KeepTtl] = None - ): IO[RedisError, Boolean] = { + ): G[Boolean] = { val input = Tuple5( ArbitraryKeyInput[K](), @@ -507,7 +483,7 @@ trait Strings extends RedisEnvironment { OptionalInput(KeepTtlInput) ) - val command = RedisCommand(Set, input, SetOutput, executor) + val command = RedisCommand(Set, input, SetOutput) command.run((key, value, expireTime, update, keepTtl)) } @@ -523,9 +499,9 @@ trait Strings extends RedisEnvironment { * @return * Returns the original bit value stored at offset. */ - final def setBit[K: Schema](key: K, offset: Long, value: Boolean): IO[RedisError, Boolean] = { + final def setBit[K: Schema](key: K, offset: Long, value: Boolean): G[Boolean] = { val command = - RedisCommand(SetBit, Tuple3(ArbitraryKeyInput[K](), LongInput, BoolInput), BoolOutput, executor) + RedisCommand(SetBit, Tuple3(ArbitraryKeyInput[K](), LongInput, BoolInput), BoolOutput) command.run((key, offset, value)) } @@ -543,14 +519,9 @@ trait Strings extends RedisEnvironment { key: K, expiration: Duration, value: V - ): IO[RedisError, Unit] = { + ): G[Unit] = { val command = - RedisCommand( - SetEx, - Tuple3(ArbitraryKeyInput[K](), DurationSecondsInput, ArbitraryValueInput[V]()), - UnitOutput, - executor - ) + RedisCommand(SetEx, Tuple3(ArbitraryKeyInput[K](), DurationSecondsInput, ArbitraryValueInput[V]()), UnitOutput) command.run((key, expiration, value)) } @@ -577,7 +548,7 @@ trait Strings extends RedisEnvironment { expireTime: Option[Duration] = None, update: Option[Update] = None, keepTtl: Option[KeepTtl] = None - ): IO[RedisError, Option[V]] = { + ): G[Option[V]] = { val input = Tuple6( ArbitraryKeyInput[K](), @@ -588,7 +559,7 @@ trait Strings extends RedisEnvironment { GetKeywordInput ) - val command = RedisCommand(Set, input, OptionalOutput(ArbitraryOutput[V]()), executor) + val command = RedisCommand(Set, input, OptionalOutput(ArbitraryOutput[V]())) command.run((key, value, expireTime, update, keepTtl, GetKeyword)) } @@ -602,9 +573,9 @@ trait Strings extends RedisEnvironment { * @return * Returns 1 if the key was set. 0 if the key was not set. */ - final def setNx[K: Schema, V: Schema](key: K, value: V): IO[RedisError, Boolean] = { + final def setNx[K: Schema, V: Schema](key: K, value: V): G[Boolean] = { val command = - RedisCommand(SetNx, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]()), BoolOutput, executor) + RedisCommand(SetNx, Tuple2(ArbitraryKeyInput[K](), ArbitraryValueInput[V]()), BoolOutput) command.run((key, value)) } @@ -620,9 +591,9 @@ trait Strings extends RedisEnvironment { * @return * Returns the length of the string after it was modified by the command. */ - final def setRange[K: Schema, V: Schema](key: K, offset: Long, value: V): IO[RedisError, Long] = { + final def setRange[K: Schema, V: Schema](key: K, offset: Long, value: V): G[Long] = { val command = - RedisCommand(SetRange, Tuple3(ArbitraryKeyInput[K](), LongInput, ArbitraryValueInput[V]()), LongOutput, executor) + RedisCommand(SetRange, Tuple3(ArbitraryKeyInput[K](), LongInput, ArbitraryValueInput[V]()), LongOutput) command.run((key, offset, value)) } @@ -634,8 +605,8 @@ trait Strings extends RedisEnvironment { * @return * Returns the length of the string. */ - final def strLen[K: Schema](key: K): IO[RedisError, Long] = { - val command = RedisCommand(StrLen, ArbitraryKeyInput[K](), LongOutput, executor) + final def strLen[K: Schema](key: K): G[Long] = { + val command = RedisCommand(StrLen, ArbitraryKeyInput[K](), LongOutput) command.run(key) } } diff --git a/modules/redis/src/main/scala/zio/redis/internal/ClusterExecutor.scala b/modules/redis/src/main/scala/zio/redis/internal/ClusterExecutor.scala index 9ae46caa7..e3fd88cde 100644 --- a/modules/redis/src/main/scala/zio/redis/internal/ClusterExecutor.scala +++ b/modules/redis/src/main/scala/zio/redis/internal/ClusterExecutor.scala @@ -18,7 +18,7 @@ package zio.redis.internal import zio._ import zio.redis._ -import zio.redis.api.Cluster.AskingCommand +import zio.redis.api.Cluster.askingCommand import zio.redis.options.Cluster._ import java.io.IOException @@ -28,40 +28,41 @@ private[redis] final class ClusterExecutor private ( config: RedisClusterConfig, scope: Scope.Closeable ) extends RedisExecutor { - import ClusterExecutor._ - def execute(command: RespCommand): IO[RedisError, RespValue] = { + def execute(command: RespCommand): UIO[IO[RedisError, RespValue]] = { - def execute(keySlot: Slot) = + def execute(keySlot: Slot): GenRedis.Sync[RespValue] = for { executor <- executor(keySlot) - res <- executor.execute(command) + res <- GenRedis.sync(executor.execute(command)) } yield res - def executeAsk(address: RedisUri) = + def executeAsk(address: RedisUri): GenRedis.Sync[RespValue] = for { executor <- executor(address) - _ <- executor.execute(AskingCommand(this).resp(())) - res <- executor.execute(command) + _ <- GenRedis.sync(executor.execute(askingCommand.resp(()))) + res <- GenRedis.sync(executor.execute(command)) } yield res - def executeSafe(keySlot: Slot) = { + def executeSafe(keySlot: Slot): IO[RedisError, RespValue] = { val recover = execute(keySlot).flatMap { case e: RespValue.Error => ZIO.fail(e.asRedisError) case success => ZIO.succeed(success) - }.catchSome { + }.catchSome[Any, RedisError, RespValue] { case e: RedisError.Ask => executeAsk(e.address) case _: RedisError.Moved => refreshConnect *> execute(keySlot) } recover.retry(retryPolicy) } - for { - keyOpt <- ZIO.succeed(command.args.collectFirst { case key: RespCommandArgument.Key => key }) - keySlot = keyOpt.fold(Slot.Default)(key => Slot((key.asCRC16 & (SlotsAmount - 1)).toLong)) - result <- executeSafe(keySlot) - } yield result + ZIO.succeed { + for { + keyOpt <- ZIO.succeed(command.args.collectFirst { case key: RespCommandArgument.Key => key }) + keySlot = keyOpt.fold(Slot.Default)(key => Slot((key.asCRC16 & (SlotsAmount - 1)).toLong)) + result <- executeSafe(keySlot) + } yield result + } } private def executor(slot: Slot): IO[RedisError.IOError, RedisExecutor] = diff --git a/modules/redis/src/main/scala/zio/redis/internal/RedisCommand.scala b/modules/redis/src/main/scala/zio/redis/internal/RedisCommand.scala index 84d30e55c..dd7353d42 100644 --- a/modules/redis/src/main/scala/zio/redis/internal/RedisCommand.scala +++ b/modules/redis/src/main/scala/zio/redis/internal/RedisCommand.scala @@ -16,32 +16,23 @@ package zio.redis.internal -import zio._ import zio.redis.Input.{CommandNameInput, Varargs} import zio.redis._ private[redis] final class RedisCommand[-In, +Out] private ( val name: String, val input: Input[In], - val output: Output[Out], - val executor: RedisExecutor + val output: Output[Out] ) { - def run(in: In): IO[RedisError, Out] = - executor - .execute(resp(in)) - .flatMap[Any, Throwable, Out](out => ZIO.attempt(output.unsafeDecode(out))) - .refineToOrDie[RedisError] - def resp(in: In): RespCommand = Varargs(CommandNameInput).encode(name.split(" ")) ++ input.encode(in) } private[redis] object RedisCommand { - def apply[In, Out]( + def apply[In, Out, G[+_]]( name: String, input: Input[In], - output: Output[Out], - executor: RedisExecutor + output: Output[Out] ): RedisCommand[In, Out] = - new RedisCommand(name, input, output, executor) + new RedisCommand(name, input, output) } diff --git a/modules/redis/src/main/scala/zio/redis/internal/RedisEnvironment.scala b/modules/redis/src/main/scala/zio/redis/internal/RedisEnvironment.scala index 025aae087..33f78bef1 100644 --- a/modules/redis/src/main/scala/zio/redis/internal/RedisEnvironment.scala +++ b/modules/redis/src/main/scala/zio/redis/internal/RedisEnvironment.scala @@ -16,13 +16,23 @@ package zio.redis.internal -import zio.redis.CodecSupplier +import zio.redis.{CodecSupplier, RedisError} import zio.schema.Schema import zio.schema.codec.BinaryCodec +import zio.{IO, UIO, ZIO} -private[redis] trait RedisEnvironment { +private[redis] trait RedisEnvironment[G[+_]] { protected def codecSupplier: CodecSupplier protected def executor: RedisExecutor + protected implicit final class RunOps[In, Out](cmd: RedisCommand[In, Out]) { + def run(in: In): G[Out] = lift( + executor + .execute(cmd.resp(in)) + .map(_.flatMap[Any, Throwable, Out](out => ZIO.attempt(cmd.output.unsafeDecode(out))).refineToOrDie[RedisError]) + ) + } + + protected def lift[A](in: UIO[IO[RedisError, A]]): G[A] protected final implicit def codec[A: Schema]: BinaryCodec[A] = codecSupplier.get } diff --git a/modules/redis/src/main/scala/zio/redis/internal/RedisExecutor.scala b/modules/redis/src/main/scala/zio/redis/internal/RedisExecutor.scala index 8dfb0ec8c..d779d86b1 100644 --- a/modules/redis/src/main/scala/zio/redis/internal/RedisExecutor.scala +++ b/modules/redis/src/main/scala/zio/redis/internal/RedisExecutor.scala @@ -16,9 +16,9 @@ package zio.redis.internal -import zio.IO import zio.redis.RedisError +import zio.{IO, UIO} private[redis] trait RedisExecutor { - def execute(command: RespCommand): IO[RedisError, RespValue] + def execute(command: RespCommand): UIO[IO[RedisError, RespValue]] } diff --git a/modules/redis/src/main/scala/zio/redis/internal/SingleNodeExecutor.scala b/modules/redis/src/main/scala/zio/redis/internal/SingleNodeExecutor.scala index 5febe70c4..493f2cf0e 100644 --- a/modules/redis/src/main/scala/zio/redis/internal/SingleNodeExecutor.scala +++ b/modules/redis/src/main/scala/zio/redis/internal/SingleNodeExecutor.scala @@ -28,10 +28,10 @@ private[redis] final class SingleNodeExecutor private ( with RedisExecutor { // TODO NodeExecutor doesn't throw connection errors, timeout errors, it is hanging forever - def execute(command: RespCommand): IO[RedisError, RespValue] = + def execute(command: RespCommand): UIO[IO[RedisError, RespValue]] = Promise .make[RedisError, RespValue] - .flatMap(promise => requests.offer(Request(command.args.map(_.value), promise)) *> promise.await) + .flatMap(promise => requests.offer(Request(command.args.map(_.value), promise)).as(promise.await)) def onError(e: RedisError): UIO[Unit] = responses.takeAll.flatMap(ZIO.foreachDiscard(_)(_.fail(e))) diff --git a/modules/redis/src/main/scala/zio/redis/package.scala b/modules/redis/src/main/scala/zio/redis/package.scala index cb1131fbe..0e60145db 100644 --- a/modules/redis/src/main/scala/zio/redis/package.scala +++ b/modules/redis/src/main/scala/zio/redis/package.scala @@ -28,4 +28,7 @@ package object redis with options.Scripting { type Id[+A] = A + + type AsyncRedis = GenRedis[GenRedis.Async] + type Redis = GenRedis[GenRedis.Sync] }