Skip to content

Commit

Permalink
add overloaded scan that handles Redis Type argument (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
yisraelU authored Sep 12, 2024
1 parent b1e1267 commit f0965b6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package dev.profunktor.redis4cats.algebra

import java.time.Instant
import dev.profunktor.redis4cats.data.KeyScanCursor
import dev.profunktor.redis4cats.effects.{ CopyArgs, ExpireExistenceArg, RedisType, RestoreArgs, ScanArgs }
import dev.profunktor.redis4cats.effects.{ CopyArgs, ExpireExistenceArg, KeyScanArgs, RedisType, RestoreArgs, ScanArgs }

import scala.concurrent.duration.FiniteDuration

Expand All @@ -42,10 +42,14 @@ trait KeyCommands[F[_], K] {
@deprecated("In favor of scan(cursor: KeyScanCursor[K])", since = "0.10.4")
def scan(cursor: Long): F[KeyScanCursor[K]]
def scan(previous: KeyScanCursor[K]): F[KeyScanCursor[K]]
@deprecated("In favor of scan(keyScanArgs: KeyScanArgs)", since = "1.7.2")
def scan(scanArgs: ScanArgs): F[KeyScanCursor[K]]
def scan(keyScanArgs: KeyScanArgs): F[KeyScanCursor[K]]
@deprecated("In favor of scan(cursor: KeyScanCursor[K], scanArgs: ScanArgs)", since = "0.10.4")
def scan(cursor: Long, scanArgs: ScanArgs): F[KeyScanCursor[K]]
@deprecated("In favor of scan(previous: KeyScanCursor[K], keyScanArgs: KeyScanArgs)", since = "1.7.2")
def scan(previous: KeyScanCursor[K], scanArgs: ScanArgs): F[KeyScanCursor[K]]
def scan(cursor: KeyScanCursor[K], keyScanArgs: KeyScanArgs): F[KeyScanCursor[K]]
def typeOf(key: K): F[Option[RedisType]]
def ttl(key: K): F[Option[FiniteDuration]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ package dev.profunktor.redis4cats

import java.time.Instant

import io.lettuce.core.{ GeoArgs, ScriptOutputType => JScriptOutputType, ScanArgs => JScanArgs }
import io.lettuce.core.{
GeoArgs,
ScriptOutputType => JScriptOutputType,
ScanArgs => JScanArgs,
KeyScanArgs => JKeyScanArgs
}

import scala.concurrent.duration.FiniteDuration

Expand Down Expand Up @@ -123,6 +128,27 @@ object effects {
def apply(`match`: String, count: Long): ScanArgs = ScanArgs(Some(`match`), Some(count))
}

sealed abstract class KeyScanArgs(tpe: Option[RedisType], pattern: Option[String], count: Option[Long]) {
def underlying: JKeyScanArgs = {
val u = new JKeyScanArgs
pattern.foreach(u.`match`)
count.foreach(u.limit)
tpe.foreach(t => u.`type`(t.asString))
u
}
}

object KeyScanArgs {
def apply(pattern: String): KeyScanArgs = new KeyScanArgs(None, Some(pattern), None) {}
def apply(tpe: RedisType): KeyScanArgs = new KeyScanArgs(Some(tpe), None, None) {}
def apply(tpe: RedisType, pattern: String): KeyScanArgs = new KeyScanArgs(Some(tpe), Some(pattern), None) {}
def apply(count: Long): KeyScanArgs = new KeyScanArgs(None, None, Some(count)) {}
def apply(pattern: String, count: Long): KeyScanArgs = new KeyScanArgs(None, Some(pattern), Some(count)) {}
def apply(tpe: RedisType, count: Long): KeyScanArgs = new KeyScanArgs(Some(tpe), None, Some(count)) {}
def apply(tpe: RedisType, pattern: String, count: Long): KeyScanArgs =
new KeyScanArgs(Some(tpe), Some(pattern), Some(count)) {}
}

sealed trait FlushMode {
def asJava: io.lettuce.core.FlushMode =
this match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,12 @@ private[redis4cats] class BaseRedis[F[_]: FutureLift: MonadThrow: Log, K, V](
override def scan(previous: KeyScanCursor[K], scanArgs: ScanArgs): F[KeyScanCursor[K]] =
async.flatMap(_.scan(previous.underlying, scanArgs.underlying).futureLift.map(KeyScanCursor[K]))

override def scan(keyScanArgs: KeyScanArgs): F[KeyScanCursor[K]] =
async.flatMap(_.scan(keyScanArgs.underlying).futureLift.map(KeyScanCursor[K]))

override def scan(cursor: KeyScanCursor[K], keyScanArgs: KeyScanArgs): F[KeyScanCursor[K]] =
async.flatMap(_.scan(cursor.underlying, keyScanArgs.underlying).futureLift.map(KeyScanCursor[K]))

override def ttl(key: K): F[Option[FiniteDuration]] =
async.flatMap(_.ttl(key).futureLift.map(toFiniteDuration(TimeUnit.SECONDS)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,12 @@ trait TestScenarios { self: FunSuite =>
scan0 <- redis.scan
_ <- IO(assertEquals(scan0.cursor, "0"))
_ <- IO(assertEquals(scan0.keys.sorted, keys))
scan1 <- redis.scan(ScanArgs(1))
scan00 <- redis.scan(KeyScanArgs(RedisType.Hash))
_ <- IO(assertEquals(scan00.cursor, "0"))
scan1 <- redis.scan(KeyScanArgs(RedisType.String, 1))
_ <- IO(assert(scan1.keys.nonEmpty, "read at least something but no hard requirement"))
_ <- IO(assert(scan1.keys.size < keys.size, "but read less than all of them"))
scan2 <- redis.scan(scan1, ScanArgs("key*"))
scan2 <- redis.scan(scan1, KeyScanArgs("key*"))
_ <- IO(assertEquals(scan2.cursor, "0"))
_ <- IO(assertEquals((scan1.keys ++ scan2.keys).sorted, keys, "read to the end in result"))
} yield ()
Expand All @@ -344,11 +346,11 @@ trait TestScenarios { self: FunSuite =>
tp <- clusterScan(redis, args = None)
(keys0, iterations0) = tp
_ <- IO(assertEquals(keys0.sorted, keys))
tp <- clusterScan(redis, args = Some(ScanArgs("key*")))
tp <- clusterScan(redis, args = Some(KeyScanArgs("key*")))
(keys1, iterations1) = tp
_ <- IO(assertEquals(keys1.sorted, keys))
_ <- IO(assertEquals(iterations1, iterations0))
tp <- clusterScan(redis, args = Some(ScanArgs(1)))
tp <- clusterScan(redis, args = Some(KeyScanArgs(1)))
(keys2, iterations2) = tp
_ <- IO(assertEquals(keys2.sorted, keys))
_ <- IO(assert(iterations2 > iterations0, "made more iterations because of limit"))
Expand All @@ -362,7 +364,7 @@ trait TestScenarios { self: FunSuite =>
*/
private def clusterScan(
redis: RedisCommands[IO, String, String],
args: Option[ScanArgs]
args: Option[KeyScanArgs]
): IO[(List[String], Iterations)] = {
def scanRec(previous: KeyScanCursor[String], acc: List[String], cnt: Int): IO[(List[String], Iterations)] =
if (previous.isFinished) IO.pure((previous.keys ++ acc, cnt))
Expand Down

0 comments on commit f0965b6

Please sign in to comment.