未验证 提交 b40713b3 编写于 作者: J Justin Heyes-Jones 提交者: GitHub

Update Scan api to return Long instead of String (#211)

上级 35252724
...@@ -110,16 +110,16 @@ object Output { ...@@ -110,16 +110,16 @@ object Output {
} }
case object ScanOutput extends Output[(String, Chunk[String])] { case object ScanOutput extends Output[(Long, Chunk[String])] {
override protected def tryDecode(respValue: RespValue): (String, Chunk[String]) = override protected def tryDecode(respValue: RespValue): (Long, Chunk[String]) =
respValue match { respValue match {
case RespValue.ArrayValues(cursor @ RespValue.BulkString(_), RespValue.Array(items)) => case RespValue.ArrayValues(cursor @ RespValue.BulkString(_), RespValue.Array(items)) =>
val strings = items.map { val strings = items.map {
case s @ RespValue.BulkString(_) => s.asString case s @ RespValue.BulkString(_) => s.asString
case other => s"$other is not a bulk string" case other => s"$other is not a bulk string"
} }
(cursor.asString, strings) (cursor.asString.toLong, strings)
case other => case other =>
throw ProtocolError(s"$other isn't scan output") throw ProtocolError(s"$other isn't scan output")
} }
......
...@@ -36,7 +36,7 @@ trait Hashes { ...@@ -36,7 +36,7 @@ trait Hashes {
b: Option[Regex] = None, b: Option[Regex] = None,
c: Option[Long] = None, c: Option[Long] = None,
d: Option[String] = None d: Option[String] = None
): ZIO[RedisExecutor, RedisError, (String, Chunk[String])] = HScan.run((a, b, c, d)) ): ZIO[RedisExecutor, RedisError, (Long, Chunk[String])] = HScan.run((a, b, c, d))
final def hSet(a: String, b: (String, String), bs: (String, String)*): ZIO[RedisExecutor, RedisError, Long] = final def hSet(a: String, b: (String, String), bs: (String, String)*): ZIO[RedisExecutor, RedisError, Long] =
HSet.run((a, (b, bs.toList))) HSet.run((a, (b, bs.toList)))
......
...@@ -68,7 +68,7 @@ trait Keys { ...@@ -68,7 +68,7 @@ trait Keys {
b: Option[Regex] = None, b: Option[Regex] = None,
c: Option[Long] = None, c: Option[Long] = None,
d: Option[String] = None d: Option[String] = None
): ZIO[RedisExecutor, RedisError, (String, Chunk[String])] = Scan.run((a, b, c, d)) ): ZIO[RedisExecutor, RedisError, (Long, Chunk[String])] = Scan.run((a, b, c, d))
final def touch(a: String, as: String*): ZIO[RedisExecutor, RedisError, Long] = Touch.run((a, as.toList)) final def touch(a: String, as: String*): ZIO[RedisExecutor, RedisError, Long] = Touch.run((a, as.toList))
......
...@@ -145,14 +145,14 @@ trait Sets { ...@@ -145,14 +145,14 @@ trait Sets {
* @param cursor Cursor to use for this iteration of scan * @param cursor Cursor to use for this iteration of scan
* @param regex Glob-style pattern that filters which elements are returned * @param regex Glob-style pattern that filters which elements are returned
* @param count Count of elements. Roughly this number will be returned by Redis if possible * @param count Count of elements. Roughly this number will be returned by Redis if possible
* @return Returns the items for this iteration or nothing when you reach the end * @return Returns the next cursor, and items for this iteration or nothing when you reach the end, as a tuple
*/ */
final def sScan( final def sScan(
key: String, key: String,
cursor: Long, cursor: Long,
regex: Option[Regex] = None, regex: Option[Regex] = None,
count: Option[Count] = None count: Option[Count] = None
): ZIO[RedisExecutor, RedisError, (String, Chunk[String])] = SScan.run((key, cursor, regex, count)) ): ZIO[RedisExecutor, RedisError, (Long, Chunk[String])] = SScan.run((key, cursor, regex, count))
/** /**
* Add multiple sets * Add multiple sets
......
...@@ -325,7 +325,7 @@ trait SortedSets { ...@@ -325,7 +325,7 @@ trait SortedSets {
cursor: Long, cursor: Long,
regex: Option[Regex] = None, regex: Option[Regex] = None,
count: Option[Count] = None count: Option[Count] = None
): ZIO[RedisExecutor, RedisError, (String, Chunk[String])] = ZScan.run((key, cursor, regex, count)) ): ZIO[RedisExecutor, RedisError, (Long, Chunk[String])] = ZScan.run((key, cursor, regex, count))
/** /**
* Get the score associated with the given member in a sorted set. * Get the score associated with the given member in a sorted set.
......
...@@ -84,7 +84,7 @@ trait KeysSpec extends BaseSpec { ...@@ -84,7 +84,7 @@ trait KeysSpec extends BaseSpec {
_ <- set(key, value) _ <- set(key, value)
scan <- scan(0L) scan <- scan(0L)
(next, elements) = scan (next, elements) = scan
} yield assert(next)(isNonEmptyString) && assert(elements)(isNonEmpty) } yield assert(next)(isGreaterThan(0L)) && assert(elements)(isNonEmpty)
}, },
testM("fetch random key") { testM("fetch random key") {
for { for {
......
...@@ -138,7 +138,7 @@ object OutputSpec extends BaseSpec { ...@@ -138,7 +138,7 @@ object OutputSpec extends BaseSpec {
val input = respArrayVals(respBulkString("5"), respArray("foo", "bar")) val input = respArrayVals(respBulkString("5"), respArray("foo", "bar"))
for { for {
res <- Task(ScanOutput.unsafeDecode(input)) res <- Task(ScanOutput.unsafeDecode(input))
} yield assert(res)(equalTo("5" -> Chunk("foo", "bar"))) } yield assert(res)(equalTo(5L -> Chunk("foo", "bar")))
} }
), ),
suite("string")( suite("string")(
......
...@@ -738,7 +738,7 @@ trait SetsSpec extends BaseSpec { ...@@ -738,7 +738,7 @@ trait SetsSpec extends BaseSpec {
_ <- sAdd(key, "a", "b", "c") _ <- sAdd(key, "a", "b", "c")
scan <- sScan(key, 0L) scan <- sScan(key, 0L)
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(isNonEmptyString) && } yield assert(cursor)(isZero) &&
assert(members)(isNonEmpty) assert(members)(isNonEmpty)
}, },
testM("empty set") { testM("empty set") {
...@@ -746,7 +746,7 @@ trait SetsSpec extends BaseSpec { ...@@ -746,7 +746,7 @@ trait SetsSpec extends BaseSpec {
key <- uuid key <- uuid
scan <- sScan(key, 0L) scan <- sScan(key, 0L)
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(equalTo("0")) && } yield assert(cursor)(isZero) &&
assert(members)(isEmpty) assert(members)(isEmpty)
}, },
testM("with match over non-empty set") { testM("with match over non-empty set") {
...@@ -755,16 +755,28 @@ trait SetsSpec extends BaseSpec { ...@@ -755,16 +755,28 @@ trait SetsSpec extends BaseSpec {
_ <- sAdd(key, "one", "two", "three") _ <- sAdd(key, "one", "two", "three")
scan <- sScan(key, 0L, Some("t[a-z]*".r)) scan <- sScan(key, 0L, Some("t[a-z]*".r))
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(isNonEmptyString) && } yield assert(cursor)(isZero) &&
assert(members)(isNonEmpty) assert(members)(isNonEmpty)
}, },
testM("with count over non-empty set with two iterations") {
for {
key <- uuid
_ <- sAdd(key, "a", "b", "c", "d", "e")
scan <- sScan(key, 0L, count = Some(Count(3L)))
(cursor, members) = scan
scan2 <- sScan(key, cursor, count = Some(Count(3L)))
(cursor2, members2) = scan2
} yield assert(cursor)(isGreaterThan(0L)) &&
assert(members)(isNonEmpty) &&
assert(cursor2)(isZero)
},
testM("with count over non-empty set") { testM("with count over non-empty set") {
for { for {
key <- uuid key <- uuid
_ <- sAdd(key, "a", "b", "c", "d", "e") _ <- sAdd(key, "a", "b", "c", "d", "e")
scan <- sScan(key, 0L, count = Some(Count(3L))) scan <- sScan(key, 0L, count = Some(Count(3L)))
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(isNonEmptyString) && } yield assert(cursor)(isGreaterThan(0L)) &&
assert(members)(isNonEmpty) assert(members)(isNonEmpty)
}, },
testM("error when not set") { testM("error when not set") {
......
...@@ -794,14 +794,14 @@ trait SortedSetsSpec extends BaseSpec { ...@@ -794,14 +794,14 @@ trait SortedSetsSpec extends BaseSpec {
_ <- zAdd(key)(MemberScore(1d, "atest"), MemberScore(2d, "btest"), MemberScore(3d, "ctest")) _ <- zAdd(key)(MemberScore(1d, "atest"), MemberScore(2d, "btest"), MemberScore(3d, "ctest"))
scan <- zScan(key, 0L) scan <- zScan(key, 0L)
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(isNonEmptyString) && assert(members)(isNonEmpty) } yield assert(cursor)(isZero) && assert(members)(isNonEmpty)
}, },
testM("empty set") { testM("empty set") {
for { for {
key <- uuid key <- uuid
scan <- zScan(key, 0L) scan <- zScan(key, 0L)
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(equalTo("0")) && assert(members)(isEmpty) } yield assert(cursor)(isZero) && assert(members)(isEmpty)
}, },
testM("with match over non-empty set") { testM("with match over non-empty set") {
for { for {
...@@ -809,7 +809,7 @@ trait SortedSetsSpec extends BaseSpec { ...@@ -809,7 +809,7 @@ trait SortedSetsSpec extends BaseSpec {
_ <- zAdd(key)(MemberScore(1d, "one"), MemberScore(2d, "two"), MemberScore(3d, "three")) _ <- zAdd(key)(MemberScore(1d, "one"), MemberScore(2d, "two"), MemberScore(3d, "three"))
scan <- zScan(key, 0L, Some("t[a-z]*".r)) scan <- zScan(key, 0L, Some("t[a-z]*".r))
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(isNonEmptyString) && assert(members)(isNonEmpty) } yield assert(cursor)(isZero) && assert(members)(isNonEmpty)
}, },
testM("with count over non-empty set") { testM("with count over non-empty set") {
for { for {
...@@ -823,7 +823,7 @@ trait SortedSetsSpec extends BaseSpec { ...@@ -823,7 +823,7 @@ trait SortedSetsSpec extends BaseSpec {
) )
scan <- zScan(key, 0L, None, Some(Count(3L))) scan <- zScan(key, 0L, None, Some(Count(3L)))
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(isNonEmptyString) && assert(members)(isNonEmpty) } yield assert(cursor)(isZero) && assert(members)(isNonEmpty)
}, },
testM("match with count over non-empty set") { testM("match with count over non-empty set") {
for { for {
...@@ -837,7 +837,7 @@ trait SortedSetsSpec extends BaseSpec { ...@@ -837,7 +837,7 @@ trait SortedSetsSpec extends BaseSpec {
) )
scan <- zScan(key, 0L, Some("t[a-z]*".r), Some(Count(3L))) scan <- zScan(key, 0L, Some("t[a-z]*".r), Some(Count(3L)))
(cursor, members) = scan (cursor, members) = scan
} yield assert(cursor)(isNonEmptyString) && assert(members)(isNonEmpty) } yield assert(cursor)(isZero) && assert(members)(isNonEmpty)
}, },
testM("error when not set") { testM("error when not set") {
for { for {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册