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