Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #10 from kennyhelsens/master

Merged bitops operations for rredis, thanks for the code!
  • Loading branch information...
commit c3b0c4ad6453181d13ac31ee99dc4bf3ea1337db 2 parents dd54d96 + aee5538
@bwlewis authored
Showing with 97 additions and 0 deletions.
  1. +48 −0 R/bitops.R
  2. +49 −0 test/bitopTest.R
View
48 R/bitops.R
@@ -0,0 +1,48 @@
+#' @title Redis BITSET - set binary value
+#' @description Sets or clears the bit at offset in the string value stored at key.
+#' @param key redis key
+#' @param offset integer index to be updated
+#' @param bit binary number to be set
+#' @return bit binary number with previous value, or '0' if it had not been set before.
+#' @detail Official redis documentation: http://redis.io/commands/setbit
+redisSetBit <- function(key, offset, bit)
+{
+ .redisCmd(.raw('SETBIT'), .raw(key), .raw(as.character(offset)), .raw(as.character(bit)))
+}
+
+
+#' @title Redis BITSET gets - get binary value
+#' @description Returns the bit value at offset in the string value stored at key.
+#' @param key redis key
+#' @param offset integer index
+#' @return bit binary integer
+#' @detail Official redis documentation: http://redis.io/commands/getbit
+redisGetBit <- function(key, offset)
+{
+ .redisCmd(.raw('GETBIT'), .raw(key), .raw(as.character(offset)))
+}
+
+
+#' @title Redis BITCOUNT - count all bits in key
+#' @description Count the number of set bits (population counting) in a string.
+#' @param key redis key
+#' @return the counted bits as an integer value
+#' @detail Official redis documentation: http://redis.io/commands/bitcount
+redisBitCount <- function(key)
+{
+ .redisCmd(.raw('BITCOUNT'), .raw(key))
+}
+
+
+#' @title Redis BITOP - execute bitoperations on multiple bitsets
+#' @description Perform a bitwise operation between multiple keys (containing string values) and store the result in the destination key
+#' @param operation bit operation as character: 'AND', 'OR', 'XOR', 'NOT'
+#' @param destkey destination key where the resulting bit operation will be stored
+#' @param sourcekeys one or more source keys subject to the bit operations
+#' @return the counted bits as an integer value
+#' @detail Official redis documentation: http://redis.io/commands/bitop
+redisBitOp <- function(operation, destkey, sourcekeys)
+{
+ sets <- c(as.list(sourcekeys))
+ do.call('.redisCmd',lapply(c(list('BITOP'),operation, destkey, sets),charToRaw))
+}
View
49 test/bitopTest.R
@@ -0,0 +1,49 @@
+test01_connect <- function() {
+ redisConnect()
+ if(!is.null(redisKeys("bitmapr_test*"))){
+ redisDelete(redisKeys("bitmapr_test*"))
+ }
+}
+
+test02_getsetbit <- function() {
+ redisSetBit('bitmapr_test02', 10, 1)
+ checkEquals(1, redisGetBit('bitmapr_test02', 10))
+}
+
+
+test03_bitcount <- function() {
+ redisSetBit('bitmapr_test03', 1, 1)
+ redisSetBit('bitmapr_test03', 10, 1)
+ redisSetBit('bitmapr_test03', 100, 1)
+ checkEquals(3, redisBitCount('bitmapr_test03'))
+}
+
+test04_bitops <- function() {
+
+ sourcekeys <- c('bitmapr_test04_src_1', 'bitmapr_test04_src_2','bitmapr_test04_src_3')
+
+ redisSetBit(sourcekeys[1], 1, 1)
+ redisSetBit(sourcekeys[1], 2, 1)
+ redisSetBit(sourcekeys[1], 3, 1)
+ redisSetBit(sourcekeys[1], 100, 1)
+
+ redisSetBit(sourcekeys[2], 1, 1)
+ redisSetBit(sourcekeys[2], 4, 1)
+ redisSetBit(sourcekeys[2], 5, 1)
+ redisSetBit(sourcekeys[2], 100, 1)
+
+ redisSetBit(sourcekeys[3], 1, 1)
+ redisSetBit(sourcekeys[3], 6, 1)
+ redisSetBit(sourcekeys[3], 7, 1)
+ redisSetBit(sourcekeys[3], 100, 1)
+
+ # id 1 and 100 are present in all keys.
+ operation <- 'AND'
+ destkey <- 'bitmapr_test04_dest'
+
+ redisBitOp(operation, destkey, sourcekeys)
+ checkEquals(2, redisBitCount(destkey))
+ checkEquals(0, redisGetBit(destkey, 5))
+ checkEquals(1, redisGetBit(destkey, 1))
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.