PageRenderTime 33ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/R/zsetVal.R

http://github.com/bwlewis/rredis
R | 140 lines | 125 code | 14 blank | 1 comment | 18 complexity | 6976fd14696eaeafe3e5b2302187c10e MD5 | raw file
  1# Redis ordered set functions
  2redisZAdd <- function(key, score, member)
  3{
  4  .redisCmd(.raw('ZADD'), .raw(key), .raw(as.character(score)), .raw(member))
  5}
  6
  7redisZRem <- function(key, member)
  8{
  9  .redisCmd(.raw('ZREM'), .raw(key), .raw(member))
 10}
 11
 12redisZIncrBy <- function(key, member, increment)
 13{
 14  z <- NULL
 15  .redisCmd(.raw('ZINCRBY'), .raw(key), .raw(as.character(increment)),
 16                 .raw(member))
 17}
 18
 19redisZRank <- function(key, member, decreasing=FALSE)
 20{
 21  cmd <- .raw('ZRANK')
 22  if(decreasing) cmd <- .raw('ZREVRANK')
 23  .redisCmd(cmd, .raw(key), .raw(member))
 24}
 25
 26redisZRange <- function(key, start=0, end=-1, decreasing=FALSE,
 27                        withscores=FALSE, ...)
 28{
 29  cmd <- .raw('ZRANGE')
 30  if(decreasing) cmd <- .raw('ZREVRANGE')
 31  start <- as.character(start)
 32  end <- as.character(end)
 33  if(withscores) {
 34    z <- .redisCmd(cmd,.raw(key),.raw(start),.raw(end),.raw('WITHSCORES'),...)
 35    if(!is.null(z)) {
 36      return(list(elements=z[seq(1,length(z),by=2)],
 37                  scores=as.list(as.numeric(z[seq(2,length(z),by=2)]))))
 38    }
 39   }
 40  else
 41    .redisCmd(cmd,.raw(key),.raw(start),.raw(end),...)
 42}
 43
 44redisZRangeByScore <- function(key, min, max, offset=NULL, count=NULL, withscores=FALSE,...)
 45{
 46  min <- as.character(min)
 47  max <- as.character(max)
 48  a <- c(alist(), list(.raw('ZRANGEBYSCORE'), .raw(key), .raw(min), .raw(max)),...)
 49  if(!is.null(offset) && !is.null(count)) {
 50    a <- c(a, list(.raw('LIMIT'), .raw(offset), .raw(count)))
 51  }
 52  if(withscores) {
 53    a <- c(a,list(.raw('WITHSCORES')))
 54    z <- do.call('.redisCmd',a)
 55    if(!is.null(z)) {
 56      return(list(elements=z[seq(1,length(z),by=2)],
 57                  scores=as.list(as.numeric(z[seq(2,length(z),by=2)]))))
 58    }
 59   }
 60  do.call('.redisCmd', a)
 61}
 62
 63redisZRemRangeByRank <- function(key, start, end)
 64{
 65  start <- as.character(start)
 66  end <- as.character(end)
 67  .redisCmd(.raw('ZREMRANGEBYRANK'), .raw(key), .raw(start), .raw(end))
 68}
 69
 70redisZRemRangeByScore <- function(key, min, max)
 71{
 72  min <- as.character(min)
 73  max <- as.character(max)
 74  .redisCmd(.raw('ZREMRANGEBYSCORE'), .raw(key), .raw(min), .raw(max))
 75}
 76
 77redisZCount <- function(key, min, max)
 78{
 79  min <- as.character(min)
 80  max <- as.character(max)
 81  .redisCmd(.raw('ZCOUNT'), .raw(key), .raw(min), .raw(max))
 82}
 83
 84redisZCard <- function(key)
 85{
 86  .redisCmd(.raw('ZCARD'), .raw(key))
 87}
 88
 89redisZScore <- function(key, element)
 90{
 91  ret <- .redisCmd(.raw('ZSCORE'), .raw(key), .raw(element))
 92  if(!is.null(ret)) ret <- as.numeric(ret)
 93  ret
 94}
 95
 96.zinu <- function(type, dstkey, keys, weights=c(), aggregate=NULL,...)
 97{
 98  N <- length(keys)
 99  a <- c(alist(), list(.raw(type), .raw(dstkey), .raw(as.character(N))),...)
100  sets <- lapply(keys,charToRaw)
101  a <- c(a, sets)
102  if(!is.null(weights)) {
103    a <- c(a, list(.raw('WEIGHTS')), lapply(as.character(weights), charToRaw))
104  }
105  if(!is.null(aggregate)) {
106    a <- c(a, list(.raw('AGGREGATE'), .raw(aggregate)))
107  }
108  do.call('.redisCmd', a)
109}
110
111redisZInterStore <- function(dstkey, keys, weights=c(), aggregate=NULL,...)
112{
113  .zinu('ZINTERSTORE', dstkey, keys, weights, aggregate,...)
114}
115
116redisZUnionStore <- function(dstkey, keys, weights=c(), aggregate=NULL,...)
117{
118  .zinu('ZUNIONSTORE', dstkey, keys, weights, aggregate,...)
119}
120
121redisSort <- function(key, decreasing=FALSE, alpha=FALSE,  by=NULL, start=NULL, 
122                      count=NULL, get=NULL, store=NULL,...)
123{
124  a <- c(alist(), list(.raw('SORT')), list(.raw(key)),...)
125  if(!is.null(by))
126    a <- c(a, list(.raw('BY'), .raw(by)))
127  if(!is.null(start) && !is.null(count))
128    a <- c(a, list(.raw('LIMIT'), .raw(start), .raw(count)))
129  if(!is.null(get))
130    a <- c(a, list(.raw('GET'), .raw(get)))
131  if(decreasing)
132    a <- c(a, list(.raw('DESC')))
133  else
134    a <- c(a, list(.raw('ASC')))
135  if(alpha)
136    a <- c(a, list(.raw('ALPHA')))
137  if(!is.null(store))
138    a <- c(a, list(.raw('STORE'), .raw(store)))
139  do.call('.redisCmd', a)
140}