Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Basic scan command implementation.
  • Loading branch information
tompro committed Oct 23, 2014
commit d585f5cf17767b05902830493a7e9314ef665dda
3 changes: 3 additions & 0 deletions src/main/scala/com/redis/api/KeyOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ trait KeyOperations { this: RedisOps =>
def keys(pattern: String = "*")(implicit timeout: Timeout) =
clientRef.ask(Keys(pattern)).mapTo[Keys#Ret]

def scan(cursor:Long = 0, pattern: String = "", count: Long = 0)(implicit timeout: Timeout) =
clientRef.ask(Scan(cursor, pattern, count)).mapTo[Scan#Ret]

// RANDOMKEY
// return a randomly selected key from the currently selected DB.
def randomkey(implicit timeout: Timeout) =
Expand Down
7 changes: 7 additions & 0 deletions src/main/scala/com/redis/protocol/KeyCommands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ object KeyCommands {
def params = pattern +: ANil
}

case class Scan(cursor:Long = 0, pattern:String = "", count:Long = 0) extends RedisCommand[(Long, List[String])]("SCAN") {
def params = Seq( Seq(cursor.toString),
if(pattern != "") Seq("MATCH", pattern) else Nil,
if(count > 0) Seq("COUNT", count.toString) else Nil
).flatten.toArgs
}

case object RandomKey extends RedisCommand[Option[String]]("RANDOMKEY") {
def params = ANil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ private[serialization] trait CommandSpecificPD { this: LowPriorityPD =>
}.toMap
}

// special deserializer for SCAN
implicit def scanResultPD(implicit reader: Reader[String]):PartialDeserializer[(Long, List[String])] = {
new PrefixDeserializer[(Long, List[String])](Multi, (x: RawReply) => {
parseBulk(x)
(parseLong(x), listPD[String].apply(x))
})
}

// special deserializer for EVAL(SHA)
def ensureListPD[A](implicit reader: Reader[A]): PartialDeserializer[List[A]] =
multiBulkPD[A, List].orElse(parsedOptionPD[A].andThen(_.toList))
Expand Down
40 changes: 39 additions & 1 deletion src/test/scala/com/redis/api/KeyOperationsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,50 @@ class KeyOperationsSpec extends RedisSpecBase {
it("should fetch keys with spaces") {
val prepare = Seq(client.set("anshin 1", "debasish"), client.set("anshin 2", "maulindu"))
val prepareRes = Future.sequence(prepare).futureValue

val res = client.keys("anshin*")
res.futureValue should have length (2)
}
}

describe("scan") {

val prepare = Seq(
client.set("key:1", "1"),
client.set("key:2", "2"),
client.set("key:3", "3"),
client.set("key:4", "4"),
client.set("key:5", "5"),
client.set("key:11", "11"),
client.set("key:22", "22"),
client.set("key:33", "33"),
client.set("key:44", "44"),
client.set("key:55", "55")
)

it("should collect all keys in keyspace") {
Future.sequence(prepare).futureValue
prepare.size equals iterateScan().size
}

it("should filter base on pattern") {
Future.sequence(prepare).futureValue
iterateScan(pattern = "*5*").size equals 2
}

def iterateScan(cursor:Long = 0, count:Long = 0, pattern:String = "") = {
var cursor = -1l
var keys = Seq[String]()
while(cursor != 0) {
val res = client.scan(cursor = if(cursor == -1) 0 else cursor, count = 2)
val w = res.futureValue
keys = keys ++ w._2
cursor = w._1
}
keys
}

}

describe("randomkey") {
it("should give") {
val prepare = Seq(client.set("anshin-1", "debasish"), client.set("anshin-2", "maulindu"))
Expand Down