Permalink
Browse files

Improved multi bulk send function

  • Loading branch information...
1 parent d9a3222 commit c21491154111a00f52b1469330f27d71d610f5bf B. W. Lewis committed Mar 11, 2010
Showing with 32 additions and 24 deletions.
  1. +2 −1 R/allValCMD.R
  2. +1 −1 R/listCMD.R
  3. +20 −12 R/redis-internal.R
  4. +7 −9 R/setVal.R
  5. +2 −1 R/strValCMD.R
View
3 R/allValCMD.R
@@ -24,7 +24,8 @@ redisType <- function(key) {
.sendCmd(msg)
}
-redisKeys <- function(pattern) {
+redisKeys <- function(pattern="*")
+{
msg <- paste('KEYS ', pattern, '\r\n', sep='')
.sendCmd(msg)
}
View
2 R/listCMD.R
@@ -23,7 +23,7 @@ redisLTrim <- function(key,start,end) {
}
redisLIndex <- function(key, index) {
- .sendCmd(.redismsg('LINDEX', key, start))
+ .sendCmd(.redismsg('LINDEX', key, index))
}
redisLSet <- function(key, index, value) {
View
32 R/redis-internal.R
@@ -53,9 +53,11 @@
'*' = {
numVars <- as.numeric(substr(l,2,nchar(l)))
vals <- vector('list',numVars)
- if(!is.null(names)) names(vals) <- names
- for (i in 1:numVars) {
- vals[[i]] <- .getResponse()
+ if(numVars > 0) {
+ if(!is.null(names)) names(vals) <- names
+ for (i in 1:numVars) {
+ vals[[i]] <- .getResponse()
+ }
}
vals
},
@@ -76,21 +78,27 @@
}
# Requires a list of key1=value1, key2=value2, ...
-# It's OK to have a NULL value in which case only the key will be transmitted.
-.sendCmdMulti <- function(cmd, keyvalues) {
+# This represents the multi-bulk send protocol. Keys are sent as plain
+# text (not as R objects), values as serialized objects.
+# NA or zero-length keys are allowed, for example:
+# list(SADD=charToRaw("mykey"), myvalue)
+# NA or zero-length keys are simply skipped in the outgoing message.
+.sendCmdMulti <- function(keyvalues) {
numItems <- length(keyvalues)
keys <- names(keyvalues)
- if(any(nchar(keys)==0)) stop("Invalid key name")
- foo <- paste('*', as.character((2* numItems) + 1), '\r\n',
- '$', as.character(nchar(cmd)), '\r\n',
- cmd, '\r\n', sep='')
+ n <- numItems + length(keys[(nchar(keys)!=0) & !is.na(keys)])
+ foo <- paste('*', as.character(n), '\r\n',sep='')
.sendCmd(foo,checkResponse=FALSE)
for (i in 1:numItems) {
+ if((nchar(keys[[i]])>0) & (!is.na(keys[[i]]))) {
+ foo <- paste('$', as.character(nchar(keys[[i]])), '\r\n',
+ keys[[i]], '\r\n', sep='')
+ .sendCmd(foo, checkResponse=FALSE)
+ }
keyvalues[[i]] <- .cerealize(keyvalues[[i]])
l <- length(keyvalues[[i]])
- foo <- paste('$', as.character(nchar(keys[[i]])), '\r\n',
- keys[[i]], '\r\n', sep='')
- .sendCmd(foo, checkResponse=FALSE)
+# Check for null value, use special -1 length designation
+ if(l<1) l <- -1
if(l>0) {
bar <- paste('$', as.character(l), '\r\n', sep='')
.sendCmd(bar, bin = keyvalues[[i]], checkResponse=FALSE)
View
16 R/setVal.R
@@ -1,9 +1,7 @@
-
redisSAdd <- function(set, element)
{
- cmd <- list(element)
- names(cmd) <- set
- .sendCmdMulti('SADD',cmd)
+ cmd <- list(SADD=charToRaw(set),element)
+ .sendCmdMulti(cmd)
}
redisSPop <- function(set)
@@ -18,10 +16,10 @@ redisSMembers <- function(set)
.sendCmd(msg)
}
-redisSRem <- function(set)
+redisSRem <- function(set, element)
{
- msg <- paste('SREM ',set,'\r\n',sep='')
- .sendCmd(msg)
+ cmd <- list(SREM=charToRaw(set),element)
+ .sendCmdMulti(cmd)
}
redisSCard <- function(set)
@@ -32,7 +30,7 @@ redisSCard <- function(set)
redisSMove <- function(setA, setB, element)
{
- msg <- paste('SMOVE ',setA,' ',setB, ' ',element,'\r\n',sep='')
- .sendCmd(msg)
+ cmd <- list(SMOVE=charToRaw(setA),charToRaw(setB),element)
+ .sendCmdMulti(cmd)
}
View
3 R/strValCMD.R
@@ -29,7 +29,8 @@ redisMGet <- function(keys) {
redisMSet <- function(keyvalues, NX=FALSE) {
if (NX) cmd <- 'MSETNX' else cmd <- 'MSET'
- ret <- .sendCmdMulti(cmd, keyvalues)
+ cmd <- c(list(charToRaw(cmd)),keyvalues)
+ ret <- .sendCmdMulti(cmd)
if (NX) 1==ret else ret
}

0 comments on commit c214911

Please sign in to comment.