Class: ROC::Store::TransientStore
- Includes:
- ObjectInitializers
- Defined in:
- lib/roc/store/transient_store.rb
Constant Summary collapse
- KEYSPACES =
{}
- MDSPACES =
{}
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #append(key, val) ⇒ Object
- #blpop ⇒ Object
- #brpop ⇒ Object
- #brpoplpush ⇒ Object
- #call(method_name, *args) ⇒ Object
- #decr(key) ⇒ Object
- #decrby(key, by) ⇒ Object
-
#del(*keys) ⇒ Object
All keys.
- #discard ⇒ Object
- #enable_eval ⇒ Object
- #exec ⇒ Object
- #exists(key) ⇒ Object
- #expire(key, secs) ⇒ Object
- #expireat(key, epoch) ⇒ Object
- #flushdb ⇒ Object
-
#get(key) ⇒ Object
Strings.
- #getbit(key, index) ⇒ Object
- #getrange(key, first_index, last_index) ⇒ Object
- #getset(key, val) ⇒ Object
- #hdel(key, field) ⇒ Object
- #hexists(key, field) ⇒ Object
-
#hget(key, field) ⇒ Object
Hashes.
- #hgetall(key) ⇒ Object
- #hincrby(key, field, by) ⇒ Object
- #hkeys(key) ⇒ Object
- #hlen(key) ⇒ Object
- #hmget(key, *fields) ⇒ Object
- #hmset(key, *pairs) ⇒ Object
- #hset(key, field, val) ⇒ Object
- #hsetnx(key, field, val) ⇒ Object
- #hvals(key) ⇒ Object
- #in_multi? ⇒ Boolean
- #incr(key) ⇒ Object
- #incrby(key, by) ⇒ Object
-
#initialize(name = nil) ⇒ TransientStore
constructor
A new instance of TransientStore.
- #inspect ⇒ Object
- #keys(pattern = '*') ⇒ Object
- #lindex(key, ind) ⇒ Object
- #linsert(key, where, pivot, val) ⇒ Object
- #llen(key) ⇒ Object
- #lpop(key) ⇒ Object
- #lpush(key, val) ⇒ Object
- #lpushx(key, val) ⇒ Object
-
#lrange(key, start_index, stop_index) ⇒ Object
Lists.
- #lrem(key, count, val) ⇒ Object
- #lset(key, ind, val) ⇒ Object
- #ltrim(key, start_index, stop_index) ⇒ Object
- #mget(*keys) ⇒ Object
- #move(key, db) ⇒ Object
- #mset(*pairs) ⇒ Object
- #msetnx(*pairs) ⇒ Object
-
#multi ⇒ Object
Transactions.
- #persist(key) ⇒ Object
- #randomkey ⇒ Object
- #rename(key, newkey) ⇒ Object
- #renamenx(key, newkey) ⇒ Object
- #rpop(key) ⇒ Object
- #rpoplpush(source_key, dest_key) ⇒ Object
- #rpush(key, val) ⇒ Object
- #rpushx(key, val) ⇒ Object
-
#sadd(key, val) ⇒ Object
Set.
- #scard(key) ⇒ Object
- #sdiff(*keys) ⇒ Object
- #sdiffstore(key, *other_keys) ⇒ Object
- #set(key, val) ⇒ Object
- #setbit(key, index, value) ⇒ Object
- #setnx(key, val) ⇒ Object
- #setrange(key, start_index, val) ⇒ Object
- #shared? ⇒ Boolean
- #sinter(*keys) ⇒ Object
- #sinterstore(key, *other_keys) ⇒ Object
- #sismember(key, val) ⇒ Object
- #smembers(key) ⇒ Object
- #smove(source_key, dest_key, val) ⇒ Object
- #sort(key, *args) ⇒ Object
- #spop(key) ⇒ Object
- #srandmember(key) ⇒ Object
- #srem(key, val) ⇒ Object
- #strlen(key) ⇒ Object
- #sunion(*keys) ⇒ Object
- #sunionstore(key, *other_keys) ⇒ Object
- #ttl(key) ⇒ Object
- #type(key) ⇒ Object
- #unwatch ⇒ Object
- #watch(*keys) ⇒ Object
-
#zadd(key, score, val) ⇒ Object
Sorted Sets.
- #zcard(key) ⇒ Object
- #zcount(key, min, max) ⇒ Object
- #zincrby(key, by, val) ⇒ Object
- #zinterstore(key, *args) ⇒ Object
- #zrange(key, start_index, stop_index, opts = nil) ⇒ Object
- #zrangebyscore(key, min, max, *opts) ⇒ Object
- #zrank(key, val) ⇒ Object
- #zrem(key, val) ⇒ Object
- #zremrangebyrank(key, start, stop) ⇒ Object
- #zremrangebyscore(key, min, max) ⇒ Object
- #zrevrange(key, start_index, stop_index, opts = nil) ⇒ Object
- #zrevrangebyscore(key, max, min, *opts) ⇒ Object
- #zrevrank(key, val) ⇒ Object
- #zscore(key, val) ⇒ Object
-
#zunionstore(key, *args) ⇒ Object
craziness here is to support both the Ruby Redis gem args style and the raw redis args styl.
Methods included from ObjectInitializers
#class_for_key, #find, #init, #init_float, #init_hash, #init_integer, #init_list, #init_lock, #init_set, #init_sorted_set, #init_string, #init_time
Constructor Details
#initialize(name = nil) ⇒ TransientStore
Returns a new instance of TransientStore.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/roc/store/transient_store.rb', line 13 def initialize(name=nil) if name.nil? @keyspace = {} @mdspace = {} else @name = name.to_s TransientStore::KEYSPACES[@name] ||= {} TransientStore::MDSPACES[@name] ||= {} end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object (protected)
end of redis methods
1431 1432 1433 |
# File 'lib/roc/store/transient_store.rb', line 1431 def method_missing(*args) puts "unimplemented: #{args}" end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/roc/store/transient_store.rb', line 11 def name @name end |
Instance Method Details
#append(key, val) ⇒ Object
306 307 308 309 310 311 312 313 314 315 |
# File 'lib/roc/store/transient_store.rb', line 306 def append(key, val) if self.exists(key) with_type(key, 'string') do self.keyspace[key.to_s] << val.to_s end else self.set(key, val) end self.strlen(key) end |
#blpop ⇒ Object
640 641 642 |
# File 'lib/roc/store/transient_store.rb', line 640 def blpop raise "blocking methods not implemented" end |
#brpop ⇒ Object
644 645 646 |
# File 'lib/roc/store/transient_store.rb', line 644 def brpop raise "blocking methods not implemented" end |
#brpoplpush ⇒ Object
648 649 650 |
# File 'lib/roc/store/transient_store.rb', line 648 def brpoplpush raise "blocking methods not implemented" end |
#call(method_name, *args) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/roc/store/transient_store.rb', line 64 def call(method_name, *args) if @multi_mode @multi_calls << [method_name, *args] 'QUEUED' else self.send method_name, *args end end |
#decr(key) ⇒ Object
412 413 414 |
# File 'lib/roc/store/transient_store.rb', line 412 def decr(key) self.incrby(key, -1) end |
#decrby(key, by) ⇒ Object
416 417 418 |
# File 'lib/roc/store/transient_store.rb', line 416 def decrby(key, by) self.incrby(key, -by) end |
#del(*keys) ⇒ Object
All keys
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/roc/store/transient_store.rb', line 77 def del(*keys) keys.each{|key| expunge_if_expired(key)} i = 0 keys.each do |key| if self.exists(key) self.expunge(key) i += 1 end end if keys.size > 1 true else i end end |
#discard ⇒ Object
1258 1259 1260 1261 1262 1263 1264 1265 |
# File 'lib/roc/store/transient_store.rb', line 1258 def discard if @multi_mode @multi_mode = false @multi_calls = [] else raise "discard without a multi" end end |
#enable_eval ⇒ Object
1441 1442 1443 1444 1445 1446 |
# File 'lib/roc/store/transient_store.rb', line 1441 def enable_eval require 'roc/store/transient_eval' if !self.class.include?(ROC::Store::TransientEval) self.class.send(:include, ROC::Store::TransientEval) end end |
#exec ⇒ Object
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 |
# File 'lib/roc/store/transient_store.rb', line 1244 def exec if @multi_mode ret = [] @multi_mode = false @multi_calls.each do |call| ret << (self.call *call) end @multi_calls = [] ret else raise "exec without a multi" end end |
#exists(key) ⇒ Object
93 94 95 96 |
# File 'lib/roc/store/transient_store.rb', line 93 def exists(key) expunge_if_expired(key) self.keyspace.has_key?(key.to_s) end |
#expire(key, secs) ⇒ Object
98 99 100 |
# File 'lib/roc/store/transient_store.rb', line 98 def expire(key, secs) self.expireat(key, ::Time.now.to_i + secs.to_i) end |
#expireat(key, epoch) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/roc/store/transient_store.rb', line 102 def expireat(key, epoch) if self.exists(key) self.mdspace[key.to_s] ||= {} self.mdspace[key.to_s][:expire_at] = epoch.to_i true else false end end |
#flushdb ⇒ Object
1284 1285 1286 1287 1288 1289 1290 1291 1292 |
# File 'lib/roc/store/transient_store.rb', line 1284 def flushdb if self.shared? TransientStore::KEYSPACES[self.name] = {} TransientStore::MDSPACES[self.name] ={} else @keyspace = {} @mdspace = {} end end |
#get(key) ⇒ Object
Strings
235 236 237 238 239 240 241 |
# File 'lib/roc/store/transient_store.rb', line 235 def get(key) with_type(key, 'string') do expunge_if_expired(key) v = self.keyspace[key.to_s] v.nil? ? nil : v.dup end end |
#getbit(key, index) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/roc/store/transient_store.rb', line 317 def getbit(key, index) raise ArgumentError, 'setbit takes a non-negative index' unless index > 0 bitstring = self.get(key).unpack('B*')[0] if index < bitstring.length if RUBY_VERSION.match(/^1\.8/) bitstring[index].chr.to_i else bitstring[index].to_i end else 0 end end |
#getrange(key, first_index, last_index) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/roc/store/transient_store.rb', line 353 def getrange(key, first_index, last_index) if self.exists(key) with_type(key, 'string') do arr = self.keyspace[key.to_s].bytes.to_a[first_index..last_index] if arr arr.map{|c| c.chr}.join('') else '' end end else '' end end |
#getset(key, val) ⇒ Object
257 258 259 260 261 |
# File 'lib/roc/store/transient_store.rb', line 257 def getset(key, val) current_val = self.get(key) self.set(key, val) current_val end |
#hdel(key, field) ⇒ Object
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 |
# File 'lib/roc/store/transient_store.rb', line 1184 def hdel(key, field) with_type(key, 'hash') do hsh = self.keyspace[key.to_s] ret = self.exists(key) && !!hsh.delete(field.to_s) if 0 == hsh.size self.del(key) end ret end end |
#hexists(key, field) ⇒ Object
1126 1127 1128 1129 1130 |
# File 'lib/roc/store/transient_store.rb', line 1126 def hexists(key, field) with_type(key, 'hash') do self.exists(key) && self.keyspace[key.to_s].has_key?(field.to_s) end end |
#hget(key, field) ⇒ Object
Hashes
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 |
# File 'lib/roc/store/transient_store.rb', line 1114 def hget(key, field) with_type(key, 'hash') do expunge_if_expired(key) hsh = self.keyspace[key.to_s] if !hsh.nil? && hsh.has_key?(field.to_s) hsh[field.to_s].dup else nil end end end |
#hgetall(key) ⇒ Object
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 |
# File 'lib/roc/store/transient_store.rb', line 1149 def hgetall(key) with_type(key, 'hash') do hsh = self.keyspace[key.to_s] if hsh hsh.dup else {} end end end |
#hincrby(key, field, by) ⇒ Object
1195 1196 1197 1198 1199 1200 1201 |
# File 'lib/roc/store/transient_store.rb', line 1195 def hincrby(key, field, by) raise "value (#{by}) is not an integer" unless (by.is_a?(::Integer) || by.floor == by) val = self.hget(key, field) new_val = val.to_i + by.to_i self.hset(key, field, new_val) new_val end |
#hkeys(key) ⇒ Object
1160 1161 1162 1163 1164 1165 1166 |
# File 'lib/roc/store/transient_store.rb', line 1160 def hkeys(key) if hsh = self.hgetall(key) hsh.keys else [] end end |
#hlen(key) ⇒ Object
1176 1177 1178 1179 1180 1181 1182 |
# File 'lib/roc/store/transient_store.rb', line 1176 def hlen(key) if hsh = self.hgetall(key) hsh.size else 0 end end |
#hmget(key, *fields) ⇒ Object
1203 1204 1205 |
# File 'lib/roc/store/transient_store.rb', line 1203 def hmget(key, *fields) fields.map{|f| self.hget(key, f)} end |
#hmset(key, *pairs) ⇒ Object
1207 1208 1209 1210 1211 1212 1213 1214 |
# File 'lib/roc/store/transient_store.rb', line 1207 def hmset(key, *pairs) i = 0 while i < pairs.length self.hset(key, pairs[i], pairs[i+1]) i += 2 end true end |
#hset(key, field, val) ⇒ Object
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 |
# File 'lib/roc/store/transient_store.rb', line 1132 def hset(key, field, val) with_type(key, 'hash') do f = field.to_s v = if val.is_a?(::String) val.dup else val.to_s end if !self.exists(key) self.keyspace[key.to_s] = {} end ret = !self.keyspace[key.to_s].has_key?(f) self.keyspace[key.to_s][f] = v ret end end |
#hsetnx(key, field, val) ⇒ Object
1216 1217 1218 1219 1220 1221 1222 |
# File 'lib/roc/store/transient_store.rb', line 1216 def hsetnx(key, field, val) if self.hexists(key, field) false else self.hset(key, field, val) end end |
#hvals(key) ⇒ Object
1168 1169 1170 1171 1172 1173 1174 |
# File 'lib/roc/store/transient_store.rb', line 1168 def hvals(key) if hsh = self.hgetall(key) hsh.values else [] end end |
#in_multi? ⇒ Boolean
1267 1268 1269 |
# File 'lib/roc/store/transient_store.rb', line 1267 def in_multi? !!@multi_mode end |
#incr(key) ⇒ Object
400 401 402 |
# File 'lib/roc/store/transient_store.rb', line 400 def incr(key) self.incrby(key, 1) end |
#incrby(key, by) ⇒ Object
404 405 406 407 408 409 410 |
# File 'lib/roc/store/transient_store.rb', line 404 def incrby(key, by) raise "value (#{by}) is not an integer" unless by.is_a?(::Integer) val = self.get(key) new_val = val.to_i + by self.set(key, new_val.to_s) new_val end |
#inspect ⇒ Object
1437 1438 1439 |
# File 'lib/roc/store/transient_store.rb', line 1437 def inspect "<#{self.class} @name=#{self.name.inspect}>" end |
#keys(pattern = '*') ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/roc/store/transient_store.rb', line 112 def keys(pattern='*') if '*' == pattern self.keyspace.keys else raise "patterns not implemented yet" end end |
#lindex(key, ind) ⇒ Object
520 521 522 523 524 525 526 527 528 529 |
# File 'lib/roc/store/transient_store.rb', line 520 def lindex(key, ind) with_type(key, 'list') do if !self.exists(key) nil else v = self.keyspace[key.to_s][ind] v.nil? ? nil : v.dup end end end |
#linsert(key, where, pivot, val) ⇒ Object
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/roc/store/transient_store.rb', line 614 def linsert(key, where, pivot, val) if !['before', 'after'].include?(where.to_s.downcase) raise ArgumentError "BEFORE or AFTER please" else if self.exists(key) ind = self.keyspace[key.to_s].index(pivot) if ind if 'after' == where ind +=1 end v = if val.is_a?(::String) val.dup else val.to_s end self.keyspace[key.to_s].insert(ind, v) self.keyspace[key.to_s].size else -1 end else 0 end end end |
#llen(key) ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/roc/store/transient_store.rb', line 434 def llen(key) with_type(key, 'list') do expunge_if_expired(key) val = self.keyspace[key.to_s] if val.nil? 0 else val.size end end end |
#lpop(key) ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/roc/store/transient_store.rb', line 506 def lpop(key) with_type(key, 'list') do if !self.exists(key) nil else val = self.keyspace[key.to_s].shift if 0 == self.llen(key) self.del(key) end val end end end |
#lpush(key, val) ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/roc/store/transient_store.rb', line 469 def lpush(key, val) with_type(key, 'list') do if !self.exists(key) self.keyspace[key.to_s] = [] end v = if val.is_a?(::String) val.dup else val.to_s end self.keyspace[key.to_s].unshift(v) self.keyspace[key.to_s].size end end |
#lpushx(key, val) ⇒ Object
484 485 486 487 488 489 490 |
# File 'lib/roc/store/transient_store.rb', line 484 def lpushx(key, val) if self.exists(key) self.lpush(key, val) else 0 end end |
#lrange(key, start_index, stop_index) ⇒ Object
Lists
422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/roc/store/transient_store.rb', line 422 def lrange(key, start_index, stop_index) with_type(key, 'list') do expunge_if_expired(key) val = self.keyspace[key.to_s] if val.nil? || (start_index >= val.size) || ( (start_index < 0) && (stop_index < start_index) ) [] else val[start_index..stop_index] || [] ## never return nil -- happens if start_index is neg and before begining of list end end end |
#lrem(key, count, val) ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/roc/store/transient_store.rb', line 550 def lrem(key, count, val) with_type(key, 'list') do if self.exists(key) iterator = self.keyspace[key.to_s] limit = iterator.size reverse = false if count > 0 limit = count elsif count < 0 limit = count.abs iterator = iterator.reverse reverse = true end indexes_to_del = [] v = val.to_s iterator.each_with_index do |test, i| if test == v if reverse indexes_to_del.unshift iterator.size - (i + 1) else indexes_to_del << i end end if indexes_to_del.size == limit break end end correction = 0 indexes_to_del.each do |i| self.keyspace[key.to_s].delete_at(i - correction) correction += 1 end if 0 == self.llen(key) self.del(key) end indexes_to_del.size else 0 end end end |
#lset(key, ind, val) ⇒ Object
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/roc/store/transient_store.rb', line 531 def lset(key, ind, val) with_type(key, 'list') do expunge_if_expired(key) arr = self.keyspace[key.to_s] if arr.nil? raise ArgumentError, "No such key: #{key}" elsif ((ind < 0) && (ind < -arr.size)) || (ind >= arr.size) raise ArgumentError, "index out of range: #{ind}" else v = if val.is_a?(::String) val.dup else val.to_s end self.keyspace[key.to_s][ind] = v end end end |
#ltrim(key, start_index, stop_index) ⇒ Object
594 595 596 597 598 599 600 601 602 |
# File 'lib/roc/store/transient_store.rb', line 594 def ltrim(key, start_index, stop_index) arr = self.lrange(key, start_index, stop_index) if 0 == arr.size self.del(key) else self.keyspace[key.to_s] = arr end true end |
#mget(*keys) ⇒ Object
263 264 265 |
# File 'lib/roc/store/transient_store.rb', line 263 def mget(*keys) keys.map{|k| self.get(k)} end |
#move(key, db) ⇒ Object
120 121 122 |
# File 'lib/roc/store/transient_store.rb', line 120 def move(key, db) raise NotImplementedError end |
#mset(*pairs) ⇒ Object
267 268 269 270 271 272 273 274 |
# File 'lib/roc/store/transient_store.rb', line 267 def mset(*pairs) i=0 while i < pairs.size self.set(pairs[i], pairs[i+1]) i+=2 end true end |
#msetnx(*pairs) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/roc/store/transient_store.rb', line 285 def msetnx(*pairs) i=0 any_exist = false while i < pairs.size if self.exists(pairs[i]) any_exist = true break end end if !any_exist i=0 while i < pairs.size self.set(pairs[i], pairs[i+1]) i+=2 end true else false end end |
#multi ⇒ Object
Transactions
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 |
# File 'lib/roc/store/transient_store.rb', line 1226 def multi if @multi_mode raise "multi calls can't be nested" else @multi_mode = true @multi_calls = [] if block_given? begin yield rescue Exception => e self.discard raise e end self.exec end end end |
#persist(key) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/roc/store/transient_store.rb', line 124 def persist(key) if self.exists(key) && (md = self.mdspace[key.to_s]) && md.has_key?(:expire_at) md.delete(:expire_at) true else false end end |
#randomkey ⇒ Object
133 134 135 136 |
# File 'lib/roc/store/transient_store.rb', line 133 def randomkey ks = self.keys ks[Kernel.rand(ks.size)] end |
#rename(key, newkey) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/roc/store/transient_store.rb', line 138 def rename(key, newkey) if key.to_s == newkey.to_s raise ArgumentError, "keys are the same" elsif self.exists(key) self.keyspace[newkey.to_s] = self.keyspace.delete(key.to_s) true else raise ArgumentError, "no such key: #{key}" end end |
#renamenx(key, newkey) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/roc/store/transient_store.rb', line 149 def renamenx(key, newkey) if key.to_s == newkey.to_s raise ArgumentError, "keys are the same" elsif self.exists(key) if self.exists(newkey) false else self.keyspace[newkey.to_s] = self.keyspace.delete(key.to_s) true end else raise ArgumentError, "no such key: #{key}" end end |
#rpop(key) ⇒ Object
492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/roc/store/transient_store.rb', line 492 def rpop(key) with_type(key, 'list') do if !self.exists(key) nil else val = self.keyspace[key.to_s].pop if 0 == self.llen(key) self.del(key) end val end end end |
#rpoplpush(source_key, dest_key) ⇒ Object
604 605 606 607 608 609 610 611 612 |
# File 'lib/roc/store/transient_store.rb', line 604 def rpoplpush(source_key, dest_key) if self.exists(source_key) val = self.rpop(source_key) self.lpush(dest_key, val) val else nil end end |
#rpush(key, val) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/roc/store/transient_store.rb', line 446 def rpush(key, val) with_type(key, 'list') do if !self.exists(key) self.keyspace[key.to_s] = [] end v = if val.is_a?(::String) val.dup else val.to_s end self.keyspace[key.to_s] << v self.keyspace[key.to_s].size end end |
#rpushx(key, val) ⇒ Object
461 462 463 464 465 466 467 |
# File 'lib/roc/store/transient_store.rb', line 461 def rpushx(key, val) if self.exists(key) self.rpush(key, val) else 0 end end |
#sadd(key, val) ⇒ Object
Set
654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/roc/store/transient_store.rb', line 654 def sadd(key, val) with_type(key, 'set') do v = val.to_s if !self.exists(key) self.keyspace[key.to_s] = {} end if self.keyspace[key.to_s].has_key?(v) false else self.keyspace[key.to_s][v] = true true end end end |
#scard(key) ⇒ Object
669 670 671 672 673 674 675 676 677 678 679 |
# File 'lib/roc/store/transient_store.rb', line 669 def scard(key) with_type(key, 'set') do expunge_if_expired(key) val = self.keyspace[key.to_s] if val.nil? 0 else val.size end end end |
#sdiff(*keys) ⇒ Object
781 782 783 784 785 786 787 788 |
# File 'lib/roc/store/transient_store.rb', line 781 def sdiff(*keys) raise ArgumentError, 'sinter needs at least one key' unless keys.size > 0 diff = self.smembers(keys.shift) while k = keys.shift diff = diff - self.smembers(k) end diff end |
#sdiffstore(key, *other_keys) ⇒ Object
802 803 804 805 806 |
# File 'lib/roc/store/transient_store.rb', line 802 def sdiffstore(key, *other_keys) vals = self.sdiff(*other_keys) vals.each{|v| self.sadd(key, v)} vals.size end |
#set(key, val) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/roc/store/transient_store.rb', line 243 def set(key, val) with_type(key, 'string') do expunge_if_expired(key) v = if val.is_a?(::String) val.dup else val.to_s end self.keyspace[key.to_s] = v self.persist(key) true end end |
#setbit(key, index, value) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/roc/store/transient_store.rb', line 332 def setbit(key, index, value) raise ArgumentError, 'setbit takes a non-negative index' unless index > 0 raise ArgumentError, 'setbit takes a 1 or 0 for the value' unless((0 == value) || (1 == value)) bitstring = self.get(key).unpack('B*')[0] current_val = 0 if index < bitstring.length current_val = if RUBY_VERSION.match(/^1\.8/) bitstring[index].chr.to_i else bitstring[index].to_i end bitstring[index] = value.to_s else bitstring << ('0' * (index - bitstring.length)) bitstring << value.to_s end self.set(key, [bitstring].pack('B*')) current_val end |
#setnx(key, val) ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/roc/store/transient_store.rb', line 276 def setnx(key, val) if self.exists(key) false else self.set(key, val) true end end |
#setrange(key, start_index, val) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/roc/store/transient_store.rb', line 368 def setrange(key, start_index, val) with_type(key, 'string') do expunge_if_expired(key) if start_index < 1 raise "index out of range: #{start_index}" end length = self.strlen(key) padding_length = start_index - length v = val.to_s if padding_length > 0 #self.keyspace[key.to_s][length, padding_length + v.length] = ("\u0000" * padding_length) + v self.keyspace[key.to_s][length, padding_length + v.length] = ("\000" * padding_length) + v else self.keyspace[key.to_s][start_index, v.length] = v end self.strlen(key) end end |
#shared? ⇒ Boolean
24 25 26 |
# File 'lib/roc/store/transient_store.rb', line 24 def shared? !@name.nil? end |
#sinter(*keys) ⇒ Object
772 773 774 775 776 777 778 779 |
# File 'lib/roc/store/transient_store.rb', line 772 def sinter(*keys) raise ArgumentError, 'sinter needs at least one key' unless keys.size > 0 inter = self.smembers(keys.shift) while k = keys.shift inter = inter & self.smembers(k) end inter end |
#sinterstore(key, *other_keys) ⇒ Object
796 797 798 799 800 |
# File 'lib/roc/store/transient_store.rb', line 796 def sinterstore(key, *other_keys) vals = self.sinter(*other_keys) vals.each{|v| self.sadd(key, v)} vals.size end |
#sismember(key, val) ⇒ Object
710 711 712 713 714 715 716 717 718 719 720 |
# File 'lib/roc/store/transient_store.rb', line 710 def sismember(key, val) with_type(key, 'set') do expunge_if_expired(key) hsh = self.keyspace[key.to_s] if hsh.nil? false else hsh.has_key?(val.to_s) end end end |
#smembers(key) ⇒ Object
681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/roc/store/transient_store.rb', line 681 def smembers(key) with_type(key, 'set') do expunge_if_expired(key) val = self.keyspace[key.to_s] if val.nil? [] else val.keys end end end |
#smove(source_key, dest_key, val) ⇒ Object
750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/roc/store/transient_store.rb', line 750 def smove(source_key, dest_key, val) if self.exists(source_key) if self.srem(source_key, val) self.sadd(dest_key, val) true else false end else false end end |
#sort(key, *args) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/roc/store/transient_store.rb', line 164 def sort(key, *args) opts = parse_sort_args(args) raise ":by not yet supported" if opts.has_key?(:by) raise ":get not yet supported" if opts.has_key?(:get) limit = opts[:limit] order = (opts[:order] || '').split(' ') store = opts[:store] md = self.mdspace[key.to_s] vals = if md.nil? [] elsif 'list' == md[:type] self.lrange(key, 0, -1) elsif 'set' == md[:type] self.smembers(key) elsif 'zset' == md[:type] self.zrange(key, 0, -1) else raise TypeError, 'list, set or zset required' end sorter = if order.include?('alpha') if order.include?('desc') lambda{|a, b| b <=> a} else lambda{|a, b| a <=> b} end elsif order.include?('desc') lambda{|a, b| b.to_f <=> a.to_f} else lambda{|a, b| a.to_f <=> b.to_f} end vals.sort!{|a, b| sorter.call(a, b)} if limit vals = vals[*limit] end if store with_type(store, 'list') do self.keyspace[store.to_s] = vals end end vals end |
#spop(key) ⇒ Object
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
# File 'lib/roc/store/transient_store.rb', line 693 def spop(key) with_type(key, 'set') do expunge_if_expired(key) hsh = self.keyspace[key.to_s] if hsh.nil? nil else val = hsh.keys.sort{Kernel.rand}[0] self.keyspace[key.to_s].delete(val) if 0 == self.keyspace[key.to_s].size self.del(key) end val end end end |
#srandmember(key) ⇒ Object
738 739 740 741 742 743 744 745 746 747 748 |
# File 'lib/roc/store/transient_store.rb', line 738 def srandmember(key) with_type(key, 'set') do expunge_if_expired(key) hsh = self.keyspace[key.to_s] if hsh.nil? nil else hsh.keys.sort{Kernel.rand}[0] end end end |
#srem(key, val) ⇒ Object
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'lib/roc/store/transient_store.rb', line 722 def srem(key, val) with_type(key, 'set') do expunge_if_expired(key) hsh = self.keyspace[key.to_s] if hsh.nil? false else ret = !!hsh.delete(val.to_s) if 0 == hsh.size self.del(key) end ret end end end |
#strlen(key) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/roc/store/transient_store.rb', line 387 def strlen(key) val = self.get(key) if val.nil? 0 else if "".respond_to?(:bytesize) val.bytesize else val.length end end end |
#sunion(*keys) ⇒ Object
763 764 765 766 767 768 769 770 |
# File 'lib/roc/store/transient_store.rb', line 763 def sunion(*keys) raise ArgumentError, 'sunion needs at least one key' unless keys.size > 0 union = self.smembers(keys.shift) while k = keys.shift union = union | self.smembers(k) end union end |
#sunionstore(key, *other_keys) ⇒ Object
790 791 792 793 794 |
# File 'lib/roc/store/transient_store.rb', line 790 def sunionstore(key, *other_keys) vals = self.sunion(*other_keys) vals.each{|v| self.sadd(key, v)} vals.size end |
#ttl(key) ⇒ Object
215 216 217 218 219 220 221 222 223 |
# File 'lib/roc/store/transient_store.rb', line 215 def ttl(key) val = -1 if self.exists(key) if (md = self.mdspace[key.to_s]) && (ea = md[:expire_at]) val = ea - ::Time.now.to_i end end val end |
#type(key) ⇒ Object
225 226 227 228 229 230 231 |
# File 'lib/roc/store/transient_store.rb', line 225 def type(key) if md = self.mdspace[key.to_s] md[:type].dup else 'none' end end |
#unwatch ⇒ Object
1279 1280 1281 1282 |
# File 'lib/roc/store/transient_store.rb', line 1279 def unwatch ## nothing, we are non concurrent true end |
#watch(*keys) ⇒ Object
1271 1272 1273 1274 1275 1276 1277 |
# File 'lib/roc/store/transient_store.rb', line 1271 def watch(*keys) if @multi_mode raise "watch inside multi not allowed" end ## nothing, we are non concurrent true end |
#zadd(key, score, val) ⇒ Object
Sorted Sets
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/roc/store/transient_store.rb', line 810 def zadd(key, score, val) with_type(key, 'zset') do s = if score.is_a?(Numeric) score elsif score.is_a?(::String) (score.index('.') ? score.to_f : score.to_i) else raise ArgumentError, "score is not numeric" end if !self.exists(key) self.keyspace[key.to_s] = {:map => {}, :list => []} end ret = true v = val.to_s if self.keyspace[key.to_s][:map].has_key?(v) ret = false end self.keyspace[key.to_s][:map][v] = s self.resort(key) ret end end |
#zcard(key) ⇒ Object
833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/roc/store/transient_store.rb', line 833 def zcard(key) with_type(key, 'zset') do expunge_if_expired(key) val = self.keyspace[key.to_s] if val.nil? 0 else val[:list].size end end end |
#zcount(key, min, max) ⇒ Object
963 964 965 |
# File 'lib/roc/store/transient_store.rb', line 963 def zcount(key, min, max) self.zrangebyscore(key, min, max).size end |
#zincrby(key, by, val) ⇒ Object
1005 1006 1007 1008 1009 1010 |
# File 'lib/roc/store/transient_store.rb', line 1005 def zincrby(key, by, val) score = self.zscore(key, val) || '0' new_score = (score.index('.') ? score.to_f : score.to_i) + by self.zadd(key, new_score, val) new_score.to_s end |
#zinterstore(key, *args) ⇒ Object
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'lib/roc/store/transient_store.rb', line 1090 def zinterstore(key, *args) other_keys, opts = parse_zop_args(args) raise ArgumentError, 'zinterstore needs at least one key' unless other_keys.size > 0 with_type(key, 'zset') do sorted_sets = other_keys.map{|k| self.keyspace[k.to_s]}.compact i = sorted_sets.pop if i weight_a = (opts.has_key?(:weights) ? opts[:weights].pop : 1) while ss = sorted_sets.pop weight_b = (opts.has_key?(:weights) ? opts[:weights].pop : 1) i = self.ss_intersect(i, ss, weight_a, weight_b, (opts.has_key?(:aggregate) ? opts[:aggregate] : 'sum')) ##@@ weights and agg weight_a = 1 end else i = {:map => {}, :list => []} end self.keyspace[key.to_s] = i i[:list].size end end |
#zrange(key, start_index, stop_index, opts = nil) ⇒ Object
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
# File 'lib/roc/store/transient_store.rb', line 845 def zrange(key, start_index, stop_index, opts=nil) opts = parse_zrange_arg(opts) with_type(key, 'zset') do expunge_if_expired(key) val = self.keyspace[key.to_s] if val.nil? || (start_index >= val[:list].size) || ( (start_index < 0) && (stop_index < start_index) ) [] else ## emulate redis -- a neg start index before beginning meams the beginning if (start_index < 0) && (-start_index > val[:list].size) start_index = 0 end if opts[:with_scores] || opts[:withscores] ret = [] val[:list][start_index..stop_index].each do |v| ret << v ret << val[:map][v].to_s end ret else val[:list][start_index..stop_index] || [] ## never return nil end end end end |
#zrangebyscore(key, min, max, *opts) ⇒ Object
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
# File 'lib/roc/store/transient_store.rb', line 896 def zrangebyscore(key, min, max, *opts) opts = parse_zscore_args(opts) with_type(key, 'zset') do expunge_if_expired(key) val = self.keyspace[key.to_s] if val.nil? [] else parse = lambda {|v| if v.is_a?(::String) if v[0] == '('[0] [v[1..v.length-1].to_i, false] elsif '+inf' == v [1.0/0, true] elsif '-inf' == v [-1.0/0, true] else [v.to_i, true] end else [v.to_i, true] end } min_int, min_incl = *parse.call(min) max_int, max_incl = *parse.call(max) ret = [] pass = lambda { |v, op, incl, test| v.send( op + (incl ? '=' : ''), test) } val[:list].each do |v| if pass.call(val[:map][v], '>', min_incl, min_int) && pass.call(val[:map][v], '<', max_incl, max_int) ret << v if opts[:with_scores] || opts[:withscores] ret << val[:map][v].to_s end end end if opts.has_key?(:limit) limit = if opts[:with_scores] || opts[:withscores] opts[:limit].map{|x| x * 2} else opts[:limit] end ret[limit[0], limit[1]] else ret end end end end |
#zrank(key, val) ⇒ Object
967 968 969 970 971 972 973 974 975 976 977 |
# File 'lib/roc/store/transient_store.rb', line 967 def zrank(key, val) with_type(key, 'zset') do expunge_if_expired(key) hsh = self.keyspace[key.to_s] if hsh.nil? nil else hsh[:list].index(val.to_s) end end end |
#zrem(key, val) ⇒ Object
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 |
# File 'lib/roc/store/transient_store.rb', line 1012 def zrem(key, val) with_type(key, 'zset') do expunge_if_expired(key) hsh = self.keyspace[key.to_s] if hsh.nil? false else if hsh[:map].delete(val.to_s) self.resort(key) if 0 == hsh[:list].size self.del(key) end true else false end end end end |
#zremrangebyrank(key, start, stop) ⇒ Object
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 |
# File 'lib/roc/store/transient_store.rb', line 1049 def zremrangebyrank(key, start, stop) vals = self.zrange(key, start, stop) if vals.size > 0 hsh = self.keyspace[key.to_s] vals.each do |val| hsh[:map].delete(val) end self.resort(key) if 0 == hsh[:list].size self.del(key) end vals.size else 0 end end |
#zremrangebyscore(key, min, max) ⇒ Object
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
# File 'lib/roc/store/transient_store.rb', line 1032 def zremrangebyscore(key, min, max) vals = self.zrangebyscore(key, min, max) if vals.size > 0 hsh = self.keyspace[key.to_s] vals.each do |val| hsh[:map].delete(val) end self.resort(key) if 0 == hsh[:list].size self.del(key) end vals.size else 0 end end |
#zrevrange(key, start_index, stop_index, opts = nil) ⇒ Object
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 |
# File 'lib/roc/store/transient_store.rb', line 872 def zrevrange(key, start_index, stop_index, opts=nil) opts = parse_zrange_arg(opts) with_type(key, 'zset') do expunge_if_expired(key) val = self.keyspace[key.to_s] if val.nil? || (start_index >= val[:list].size) || ( (start_index < 0) && (stop_index < start_index) ) [] else list = val[:list].reverse if opts[:with_scores] || opts[:withscores] ret = [] list[start_index..stop_index].each do |v| ret << v ret << val[:map][v].to_s end ret else list[start_index..stop_index] end end end end |
#zrevrangebyscore(key, max, min, *opts) ⇒ Object
951 952 953 954 955 956 957 958 959 960 961 |
# File 'lib/roc/store/transient_store.rb', line 951 def zrevrangebyscore(key, max, min, *opts) opts = parse_zscore_args(opts) limit = opts.delete(:limit) ret = self.zrangebyscore(key, min, max, opts).reverse if limit ret[limit[0], limit[1]] else ret end end |
#zrevrank(key, val) ⇒ Object
979 980 981 982 983 984 985 986 |
# File 'lib/roc/store/transient_store.rb', line 979 def zrevrank(key, val) r = self.zrank(key, val) if r self.keyspace[key.to_s][:list].size - (r + 1) else nil end end |
#zscore(key, val) ⇒ Object
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 |
# File 'lib/roc/store/transient_store.rb', line 988 def zscore(key, val) with_type(key, 'zset') do expunge_if_expired(key) hsh = self.keyspace[key.to_s] if hsh.nil? nil else v = hsh[:map][val.to_s] if v v.to_s else nil end end end end |
#zunionstore(key, *args) ⇒ Object
craziness here is to support both the Ruby Redis gem args style and the raw redis args styl
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 |
# File 'lib/roc/store/transient_store.rb', line 1067 def zunionstore(key, *args) other_keys, opts = parse_zop_args(args) raise ArgumentError, 'zunionstore needs at least one key' unless other_keys.size > 0 raise ArgumentError, 'mismatch weights count' unless (!opts.has_key?(:weights) || (opts[:weights].size == other_keys.size)) with_type(key, 'zset') do sorted_sets = other_keys.map{|k| self.keyspace[k.to_s]}.compact u = sorted_sets.pop if u weight_a = (opts.has_key?(:weights) ? opts[:weights].pop : 1) while ss = sorted_sets.pop weight_b = (opts.has_key?(:weights) ? opts[:weights].pop : 1) u = self.ss_union(u, ss, weight_a, weight_b, (opts.has_key?(:aggregate) ? opts[:aggregate] : 'sum')) ##@@ weights and agg weight_a = 1 end else u = {:map => {}, :list => []} end self.keyspace[key.to_s] = u u[:list].size end end |