Permalink
Browse files

Consolidated control functions into controlCMD.R; finished set functions

  • Loading branch information...
1 parent c214911 commit 18ef79657947d938c9198ac5d341557eb784079a B. W. Lewis committed Mar 12, 2010
View
@@ -26,58 +26,31 @@ redisType <- function(key) {
redisKeys <- function(pattern="*")
{
- msg <- paste('KEYS ', pattern, '\r\n', sep='')
- .sendCmd(msg)
+ .sendCmd(.redismsg('KEYS',pattern))
}
redisRandomKey <- function() {
- msg <- 'RANDOMKEY\r\n'
- .sendCmd(msg)
+ .sendCmd(.redismsg('RANDOMKEY'))
}
redisRename <- function(old, new, NX=FALSE) {
if (NX) cmd <- 'RENAMENX ' else cmd <- 'RENAME '
- msg <- paste(cmd, old, ' ', new, '\r\n', sep='')
- ret <- .sendCmd(msg)
+ ret <- .sendCmd(.redismsg(cmd,old,new))
if (NX) 1==ret else ret
}
-redisDBSize <- function() {
- msg <- 'DBSIZE\r\n'
- .sendCmd(msg)
-}
-
redisExpire <- function(key, seconds) {
- msg <- paste('EXPIRE ', key, ' ', seconds, '\r\n', sep='')
- 1==.sendCmd(msg)
+ 1==.sendCmd(.redismsg('EXPIRE',key,seconds))
}
redisExpireAt <- function(key, time) {
- msg <- paste('EXPIREAT ', key, ' ', time, '\r\n', sep='')
- 1==.sendCmd(msg)
+ 1==.sendCmd(.redismsg('EXPIREAT',key,time))
}
redisTTL <- function(key) {
- msg <- paste('TTL ', key, '\r\n', sep='')
- .sendCmd(msg)
-}
-
-redisSelect <- function(index) {
- msg <- paste('SELECT ', index, '\r\n', sep='')
- .sendCmd(msg)
+ .sendCmd(.redismsg('TTL',key))
}
redisMove <- function(key, dbindex) {
- msg <- paste('MOVE ', key, ' ', dbindex, '\r\n', sep='')
- 1==.sendCmd(msg)
-}
-
-redisFlushDB <- function() {
- msg <- 'FLUSHDB\r\n'
- .sendCmd(msg)
-}
-
-redisFlushAll <- function() {
- msg <- 'FLUSHALL\r\n'
- .sendCmd(msg)
+ 1==.sendCmd(.redismsg('MOVE',key,dbindex))
}
View
@@ -0,0 +1,96 @@
+# This file contains various control functions.
+
+`redisConnect` <-
+function(host='localhost', port=6379, returnRef=FALSE)
+{
+ con <- socketConnection(host, port,open='a+b')
+# Stash state in the redis enivronment describing this connection:
+ assign('con',con,envir=.redisEnv)
+ assign('host',host,envir=.redisEnv)
+ assign('port',port,envir=.redisEnv)
+ tryCatch(.redisPP(),
+ error=function(e) {
+ cat(paste('Error: ',e,'\n'))
+ close(con);
+ rm(list='con',envir=.redisEnv)
+ })
+ if(returnRef) return(list(con=con,host=host,port=port))
+ invisible()
+}
+
+`redisClose` <-
+function()
+{
+ con <- .redis()
+ close(con)
+ remove(list='con',envir=.redisEnv)
+}
+
+`redisAuth` <-
+function(pwd)
+{
+ .sendCmd(.redismsg('AUTH',pwd))
+}
+
+`redisSave` <-
+function()
+{
+ .sendCmd(.redismsg('SAVE'))
+}
+
+`redisBgSave` <-
+function()
+{
+ .sendCmd(.redismsg('BGSAVE'))
+}
+
+`redisBgRewriteAOF` <-
+function()
+{
+ .sendCmd(.redismsg('BGREWRITEAOF'))
+}
+
+`redisShutdown` <-
+function()
+{
+ .sendCmd(.redismsg('SHUTDOWN'))
+ remove(list='con',envir=.redisEnv)
+}
+
+`redisInfo` <-
+function()
+{
+ x <- .sendCmd(.redismsg('INFO'))
+ z <- strsplit(x,'\r\n')
+ w <- unlist(lapply(z,strsplit,':'))
+ n <- length(w)
+ e <- seq(from=2,to=n,by=2)
+ o <- seq(from=1,to=n,by=2)
+ z <- as.list(w[e])
+ names(z) <- w[o]
+ z
+}
+
+`redisSlaveOf` <-
+function(host,port)
+{
+# Use host="no" port="one" to disable slave replication
+ .sendCmd(.redismsg('SLAVEOF'),host,port)
+}
+
+redisFlushDB <- function() {
+ .sendCmd(.redismsg('FLUSHDB'))
+}
+
+redisFlushAll <- function() {
+ .sendCmd(.redismsg('FLUSHALL'))
+}
+
+redisSelect <- function(index) {
+ .sendCmd(.redismsg('SELECT',index))
+}
+
+redisDBSize <- function() {
+ .sendCmd(.redismsg('DBSIZE'))
+}
+
View
@@ -15,7 +15,10 @@ redisLLen <- function(key) {
}
redisLRange <- function(key, start, end) {
- .sendCmd(.redismsg('LRANGE', key, start, end))
+ start <- charToRaw(as.character(start))
+ end <- charToRaw(as.character(end))
+ cmd <- list(LRANGE=charToRaw(key),start,end)
+ .sendCmdMulti(cmd)
}
redisLTrim <- function(key,start,end) {
View
@@ -26,15 +26,15 @@
con <- .redis()
socketSelect(list(con))
l <- readLines(con=con, n=1)
- c <- substr(l, 1, 1)
+ s <- substr(l, 1, 1)
if (nchar(l) < 2) {
- if(c == '+') {
+ if(s == '+') {
# '+' is a valid retrun message on at least one cmd (RANDOMKEY)
return('')
}
stop('Message garbled')
}
- switch(c,
+ switch(s,
'-' = stop(substr(l,2,nchar(l))),
'+' = substr(l,2,nchar(l)),
':' = as.numeric(substr(l,2,nchar(l))),
@@ -51,9 +51,10 @@
error=function(e) rawToChar(dat))
},
'*' = {
+ vals <- NULL
numVars <- as.numeric(substr(l,2,nchar(l)))
- vals <- vector('list',numVars)
if(numVars > 0) {
+ vals <- vector('list',numVars)
if(!is.null(names)) names(vals) <- names
for (i in 1:numVars) {
vals[[i]] <- .getResponse()
@@ -86,6 +87,10 @@
.sendCmdMulti <- function(keyvalues) {
numItems <- length(keyvalues)
keys <- names(keyvalues)
+ if(is.null(keys)) {
+ names(keyvalues) <- NA
+ keys <- names(keyvalues)
+ }
n <- numItems + length(keys[(nchar(keys)!=0) & !is.na(keys)])
foo <- paste('*', as.character(n), '\r\n',sep='')
.sendCmd(foo,checkResponse=FALSE)
@@ -97,8 +102,6 @@
}
keyvalues[[i]] <- .cerealize(keyvalues[[i]])
l <- length(keyvalues[[i]])
-# 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
@@ -1,5 +0,0 @@
-redisAuth <- function(pwd)
-{
- msg <- paste('AUTH ',pwd,'\r\n',sep='')
- sendCmd(msg)
-}
View
@@ -1,8 +0,0 @@
-redisClose <-
-function()
-{
- con <- .redis()
- close(con)
- remove(list='con',envir=.redisEnv)
-}
-
View
@@ -1,18 +0,0 @@
-redisConnect <-
-function(host='localhost', port=6379, returnRef=FALSE)
-{
- con <- socketConnection(host, port,open='a+b')
-# Stash state in the redis enivronment describing this connection:
- assign('con',con,envir=.redisEnv)
- assign('host',host,envir=.redisEnv)
- assign('port',port,envir=.redisEnv)
- tryCatch(.redisPP(),
- error=function(e) {
- cat(paste('Error: ',e,'\n'))
- close(con);
- rm(list='con',envir=.redisEnv)
- })
- if(returnRef) return(list(con=con,host=host,port=port))
- invisible()
-}
-
View
@@ -1,3 +1,63 @@
+redisSInter <- function(keys, ...)
+{
+ sets <- c(as.list(keys),list(...))
+ cmd <- list(charToRaw('SINTER'))
+ cmd <- c(cmd, lapply(sets,charToRaw))
+ .sendCmdMulti(cmd)
+}
+
+redisSUnion <- function(keys, ...)
+{
+ sets <- c(as.list(keys),list(...))
+ cmd <- list(charToRaw('SUNION'))
+ cmd <- c(cmd, lapply(sets,charToRaw))
+ .sendCmdMulti(cmd)
+}
+
+redisSUnionStore <- function(dest, keys, ...)
+{
+ sets <- c(as.list(dest),as.list(keys),list(...))
+ cmd <- list(charToRaw('SUNIONSTORE'))
+ cmd <- c(cmd, lapply(sets,charToRaw))
+ .sendCmdMulti(cmd)
+}
+
+redisSInterStore <- function(dest, keys, ...)
+{
+ sets <- c(as.list(dest),as.list(keys),list(...))
+ cmd <- list(charToRaw('SINTERSTORE'))
+ cmd <- c(cmd, lapply(sets,charToRaw))
+ .sendCmdMulti(cmd)
+}
+
+redisSDiff <- function(keys, ...)
+{
+ sets <- c(as.list(keys),list(...))
+ cmd <- list(charToRaw('SDIFF'))
+ cmd <- c(cmd, lapply(sets,charToRaw))
+ .sendCmdMulti(cmd)
+}
+
+redisSDiffStore <- function(dest, keys, ...)
+{
+ sets <- c(as.list(dest),as.list(keys),list(...))
+ cmd <- list(charToRaw('SDIFFSTORE'))
+ cmd <- c(cmd, lapply(sets,charToRaw))
+ .sendCmdMulti(cmd)
+}
+
+redisSIsMember <- function(set, element)
+{
+ cmd <- list(SISMEMBER=charToRaw(set),element)
+ 1 == .sendCmdMulti(cmd)
+}
+
+redisSRandMember <- function(set)
+{
+ cmd <- list(SRANDMEMBER=charToRaw(set))
+ .sendCmdMulti(cmd)
+}
+
redisSAdd <- function(set, element)
{
cmd <- list(SADD=charToRaw(set),element)
View
@@ -1,8 +1,7 @@
# This file contains functions that operate on Redis 'string' values.
redisGet <- function(key) {
- msg <- paste('GET ',key,'\r\n',sep='')
- .sendCmd(msg)
+ .sendCmd(.redismsg('GET',key))
}
# This is only useful right now because it is faster than mset.
@@ -36,26 +35,20 @@ redisMSet <- function(keyvalues, NX=FALSE) {
redisIncr <- function(key)
{
- msg <- paste('INCR ',key,'\r\n',sep='')
- .sendCmd(msg)
+ .sendCmd(.redismsg('INCR',key))
}
redisIncrBy <- function(key, value)
{
-# XXX Add check for integer value
- msg <- paste('INCRBY ',key,' ',value,'\r\n',sep='')
- .sendCmd(msg)
+ .sendCmd(.redismsg('INCRBY',key,value))
}
redisDecrBy <- function(key, value)
{
-# XXX Add check for integer value
- msg <- paste('DECRBY ',key,' ',value,'\r\n',sep='')
- .sendCmd(msg)
+ .sendCmd(.redismsg('DECRBY',key,value))
}
redisDecr <- function(key)
{
- msg <- paste('DECR ',key,'\r\n',sep='')
- .sendCmd(msg)
+ .sendCmd(.redismsg('DECR',key))
}
Oops, something went wrong.

0 comments on commit 18ef796

Please sign in to comment.