Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/scala/com/redis/StringOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ trait StringOperations { self: Redis =>
def setbit(key: Any, offset: Int, value: Any)(implicit format: Format): Option[Int] =
send("SETBIT", List(key, offset, value))(asInt)

// BITOP op destKey srcKey...
// Perform a bitwise operation between multiple keys (containing string values) and store the result in the destination key.
def bitop(op: String, destKey: Any, srcKeys: Any*)(implicit format: Format): Option[Int] =
send("BITOP", op :: destKey :: srcKeys.toList)(asInt)

// BITCOUNT key range
// Count the number of set bits in the given key within the optional range
def bitcount(key: Any, range: Option[(Int, Int)] = None)(implicit format: Format): Option[Int] =
send("BITCOUNT", List[Any](key) ++ (range.map { r => List[Any](r._1, r._2) } getOrElse List[Any]()))(asInt)

// SET EXPIRE (key, ttl)
// sets the ttl for the key
def expire(key: Any, ttl: Any)(implicit format: Format): Boolean =
Expand Down
35 changes: 35 additions & 0 deletions src/test/scala/com/redis/StringOperationsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,41 @@ class StringOperationsSpec extends Spec
}
}

describe("bitcount") {
it("should do a population count") {
r.setbit("mykey", 7, 1)
r.bitcount("mykey") should equal(Some(1))
r.setbit("mykey", 8, 1)
r.bitcount("mykey") should equal(Some(2))
}
}

describe("bitop") {
it("should apply logical operators to the srckeys and store the results in destKey") {
// key1: 101
// key2: 10
r.setbit("key1", 0, 1)
r.setbit("key1", 2, 1)
r.setbit("key2", 1, 1)
r.bitop("AND", "destKey", "key1", "key2") should equal(Some(1))
// 101 AND 010 = 000
(0 to 2).foreach { bit =>
r.getbit("destKey", bit) should equal(Some(0))
}

r.bitop("OR", "destKey", "key1", "key2") should equal(Some(1))
// 101 OR 010 = 111
(0 to 2).foreach { bit =>
r.getbit("destKey", bit) should equal(Some(1))
}

r.bitop("NOT", "destKey", "key1") should equal(Some(1))
r.getbit("destKey", 0) should equal(Some(0))
r.getbit("destKey", 1) should equal(Some(1))
r.getbit("destKey", 2) should equal(Some(0))
}
}

/** uncomment to test timeout : need a custom redis.conf
describe("timeout") {
it("should append value to that of a key") {
Expand Down