Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improved argument error handling in .redisCmd; added internal support…

… for returning raw vectors.
  • Loading branch information...
commit bb229d477728708040f042ec2a737dd717ff35e1 1 parent f6d9e81
B. W. Lewis authored
Showing with 37 additions and 6 deletions.
  1. +37 −6 R/redis-internal.R
View
43 R/redis-internal.R
@@ -32,7 +32,7 @@
else value
}
-.getResponse <- function()
+.getResponse <- function(raw=FALSE)
{
con <- .redis()
socketSelect(list(con))
@@ -61,7 +61,10 @@
if(m==n) {
socketSelect(list(con))
l <- readLines(con,n=1) # Trailing \r\n
- return(tryCatch(unserialize(dat),
+ if(raw)
+ return(dat)
+ else
+ return(tryCatch(unserialize(dat),
error=function(e) rawToChar(dat)))
}
# The message was not fully recieved in one pass.
@@ -88,8 +91,10 @@
socketSelect(list(con))
l <- readLines(con,n=1) # Trailing \r\n
length(r) <- j
- # Try retrieving an R object, otherwise default to character:
- tryCatch(unserialize(do.call(c,r)),
+ if(raw)
+ do.call(c,r)
+ else
+ tryCatch(unserialize(do.call(c,r)),
error=function(e) rawToChar(do.call(c,r)))
},
'*' = {
@@ -115,7 +120,7 @@
#
.raw <- function(word)
{
- charToRaw(word)
+ tryCatch(charToRaw(word),warning=function(w) stop(w), error=function(e) stop(e))
}
# .redisCmd corresponds to the Redis "multi bulk" protocol. It
@@ -141,7 +146,10 @@
socketSelect(list(con), write=TRUE)
cat(hdr, file=con)
for(j in 1:n) {
- v <- eval(f[[j+1]],envir=sys.frame(-1))
+ tryCatch(
+ v <- eval(f[[j+1]],envir=sys.frame(-1)),
+ error=function(e) {.redisError("Invalid agrument");invisible()}
+ )
if(!is.raw(v)) v <- .cerealize(v)
l <- length(v)
hdr <- paste('$', as.character(l), '\r\n', sep='')
@@ -154,3 +162,26 @@
}
.getResponse()
}
+
+.redisRawCmd <- function(...)
+{
+ con <- .redis()
+ f <- match.call()
+ n <- length(f) - 1
+ hdr <- paste('*', as.character(n), '\r\n',sep='')
+ socketSelect(list(con), write=TRUE)
+ cat(hdr, file=con)
+ for(j in 1:n) {
+ v <- eval(f[[j+1]],envir=sys.frame(-1))
+ if(!is.raw(v)) v <- .cerealize(v)
+ l <- length(v)
+ hdr <- paste('$', as.character(l), '\r\n', sep='')
+ socketSelect(list(con), write=TRUE)
+ cat(hdr, file=con)
+ socketSelect(list(con), write=TRUE)
+ writeBin(v, con)
+ socketSelect(list(con), write=TRUE)
+ cat('\r\n', file=con)
+ }
+ .getResponse(raw=TRUE)
+}
Please sign in to comment.
Something went wrong with that request. Please try again.