Class: MockRedis::Database

Inherits:
Object
  • Object
show all
Includes:
HashMethods, ListMethods, SetMethods, StringMethods, ZsetMethods
Defined in:
lib/mock_redis/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ZsetMethods

#zadd, #zcard, #zcount, #zincrby, #zinterstore, #zrange, #zrangebyscore, #zrank, #zrem, #zremrangebyrank, #zremrangebyscore, #zrevrange, #zrevrangebyscore, #zrevrank, #zscore, #zunionstore

Methods included from StringMethods

#[], #[]=, #append, #decr, #decrby, #get, #getbit, #getrange, #getset, #incr, #incrby, #mget, #mset, #msetnx, #set, #setbit, #setex, #setnx, #setrange, #strlen

Methods included from SetMethods

#sadd, #scard, #sdiff, #sdiffstore, #sinter, #sinterstore, #sismember, #smembers, #smove, #spop, #srandmember, #srem, #sunion, #sunionstore

Methods included from ListMethods

#blpop, #brpop, #brpoplpush, #lindex, #linsert, #llen, #lpop, #lpush, #lpushx, #lrange, #lrem, #lset, #ltrim, #rpop, #rpoplpush, #rpush, #rpushx

Methods included from HashMethods

#hdel, #hexists, #hget, #hgetall, #hincrby, #hkeys, #hlen, #hmget, #hmset, #hset, #hsetnx, #hvals

Constructor Details

#initialize(*args) ⇒ Database

Returns a new instance of Database.



19
20
21
22
# File 'lib/mock_redis/database.rb', line 19

def initialize(*args)
  @data = {}
  @expire_times = []
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/mock_redis/database.rb', line 17

def data
  @data
end

#expire_timesObject (readonly)

Returns the value of attribute expire_times.



17
18
19
# File 'lib/mock_redis/database.rb', line 17

def expire_times
  @expire_times
end

Instance Method Details

#auth(_) ⇒ Object

Redis commands go below this line and above ‘private’



32
# File 'lib/mock_redis/database.rb', line 32

def auth(_) 'OK' end

#bgrewriteaofObject



34
# File 'lib/mock_redis/database.rb', line 34

def bgrewriteaof() "Background append only file rewriting started" end

#bgsaveObject



36
# File 'lib/mock_redis/database.rb', line 36

def bgsave() "Background saving started" end

#dbsizeObject



38
39
40
# File 'lib/mock_redis/database.rb', line 38

def dbsize
  data.keys.length
end

#del(*keys) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/mock_redis/database.rb', line 42

def del(*keys)
  keys.
    find_all{|key| data[key]}.
    each {|k| persist(k)}.
    each {|k| data.delete(k)}.
    length
end

#echo(msg) ⇒ Object



50
51
52
# File 'lib/mock_redis/database.rb', line 50

def echo(msg)
  msg.to_s
end

#exists(key) ⇒ Object



71
72
73
# File 'lib/mock_redis/database.rb', line 71

def exists(key)
  data.has_key?(key)
end

#expire(key, seconds) ⇒ Object



54
55
56
# File 'lib/mock_redis/database.rb', line 54

def expire(key, seconds)
  expireat(key, Time.now.to_i + seconds.to_i)
end

#expire_keysObject

This method isn’t private, but it also isn’t a Redis command, so it doesn’t belong up above with all the Redis commands.



418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/mock_redis/database.rb', line 418

def expire_keys
  now = Time.now

  to_delete = expire_times.take_while do |(time, key)|
    time <= now
  end

  to_delete.each do |(time, key)|
    del(key)
  end

  expire_times.slice!(0, to_delete.length)
end

#expireat(key, timestamp) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mock_redis/database.rb', line 58

def expireat(key, timestamp)
  unless looks_like_integer?(timestamp.to_s)
    raise RuntimeError, "ERR value is not an integer or out of range"
  end

  if exists(key)
    set_expiration(key, Time.at(timestamp.to_i))
    true
  else
    false
  end
end

#flushdbObject



75
76
77
78
# File 'lib/mock_redis/database.rb', line 75

def flushdb
  data.keys.each {|k| del(k)}
  'OK'
end

#infoObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/mock_redis/database.rb', line 80

def info
  astats = [
    ["2", "2699"],
    ["6", "1"],
    ["7", "1"],
    ["8", "17197"],
    ["9", "109875"],
    ["10", "94348"],
    ["11", "32580"],
    ["12", "52347"],
    ["13", "86475"],
    ["14", "58175"],
    ["15", "53408"],
    ["16", "876949"],
    ["17", "71157"],
    ["18", "5104"],
    ["19", "2705"],
    ["20", "2903"],
    ["21", "1024"],
    ["22", "2546"],
    ["23", "952"],
    ["24", "186080"],
    ["25", "611"],
    ["26", "40936"],
    ["27", "960"],
    ["28", "1323"],
    ["29", "14216"],
    ["30", "52412"],
    ["31", "21130"],
    ["32", "47959"],
    ["33", "6891"],
    ["34", "9712"],
    ["35", "3366"],
    ["36", "5737"],
    ["37", "11274"],
    ["38", "8057"],
    ["39", "2957"],
    ["40", "51200"],
    ["42", "8220"],
    ["43", "8278"],
    ["44", "6539"],
    ["45", "764"],
    ["47", "1018"],
    ["48", "19250"],
    ["49", "713"],
    ["51", "51"],
    ["53", "2"],
    ["55", "3922"],
    ["56", "153"],
    ["57", "614"],
    ["58", "1"],
    ["59", "1775"],
    ["61", "32865"],
    ["63", "2530"],
    ["64", "565"],
    ["65", "1322"],
    ["67", "1572"],
    ["69", "1421"],
    ["71", "1220"],
    ["72", "241"],
    ["73", "5432"],
    ["74", "1122"],
    ["75", "2555"],
    ["77", "1539"],
    ["78", "612"],
    ["79", "902"],
    ["81", "1678"],
    ["83", "51"],
    ["84", "612"],
    ["85", "706"],
    ["87", "410"],
    ["88", "5435"],
    ["89", "813"],
    ["90", "612"],
    ["93", "153"],
    ["94", "612"],
    ["96", "159"],
    ["97", "306"],
    ["99", "153"],
    ["101", "456"],
    ["103", "741"],
    ["105", "447"],
    ["107", "754"],
    ["109", "414"],
    ["111", "475"],
    ["113", "757"],
    ["115", "287"],
    ["117", "420"],
    ["118", "765"],
    ["119", "642"],
    ["120", "159"],
    ["121", "926"],
    ["122", "612"],
    ["123", "251"],
    ["125", "390"],
    ["127", "354"],
    ["128", "617"],
    ["129", "528"],
    ["131", "298"],
    ["132", "612"],
    ["133", "809"],
    ["135", "244"],
    ["136", "306"],
    ["137", "504"],
    ["139", "201"],
    ["141", "1124"],
    ["143", "139"],
    ["144", "159"],
    ["145", "1322"],
    ["147", "410"],
    ["149", "253"],
    ["151", "304"],
    ["153", "312"],
    ["155", "249"],
    ["157", "306"],
    ["159", "348"],
    ["161", "255"],
    ["163", "458"],
    ["165", "5"],
    ["167", "306"],
    ["168", "47"],
    ["169", "214"],
    ["171", "250"],
    ["173", "5"],
    ["177", "10"],
    ["179", "158"],
    ["181", "5"],
    ["183", "10"],
    ["185", "51"],
    ["187", "49"],
    ["191", "5"],
    ["192", "47"],
    ["193", "51"],
    ["197", "112"],
    ["199", "5"],
    ["201", "5"],
    ["203", "5"],
    ["209", "5"],
    ["213", "51"],
    ["217", "102"],
    ["225", "357"],
    ["229", "51"],
    ["233", "204"],
    ["237", "51"],
    ["239", "1"],
    ["247", "46"],
    ["255", "102"],
    [">=256", "6201"],
  ]

  {
    "allocation_stats" => astats.map {|(a,b)| "#{a}=#{b}"}.join(','),
    "aof_enabled" => "0",
    "arch_bits" => "64",
    "bgrewriteaof_in_progress" => "0",
    "bgsave_in_progress" => "0",
    "blocked_clients" => "0",
    "changes_since_last_save" => "0",
    "client_biggest_input_buf" => "0",
    "client_longest_output_list" => "0",
    "connected_clients" => "1",
    "connected_slaves" => "0",
    "db0" => "keys=8,expires=0",
    "evicted_keys" => "0",
    "expired_keys" => "0",
    "hash_max_zipmap_entries" => "512",
    "hash_max_zipmap_value" => "64",
    "keyspace_hits" => "62645",
    "keyspace_misses" => "29757",
    "last_save_time" => "1310596333",
    "loading" => "0",
    "lru_clock" => "1036434",
    "mem_fragmentation_ratio" => "2.04",
    "multiplexing_api" => "kqueue",
    "process_id" => "14508",
    "pubsub_channels" => "0",
    "pubsub_patterns" => "0",
    "redis_git_dirty" => "0",
    "redis_git_sha1" => "00000000",
    "redis_version" => "2.2.11",
    "role" => "master",
    "total_commands_processed" => "196800",
    "total_connections_received" => "4359",
    "uptime_in_days" => "0",
    "uptime_in_seconds" => "84215",
    "use_tcmalloc" => "0",
    "used_cpu_sys" => "5.54",
    "used_cpu_sys_childrens" => "0.00",
    "used_cpu_user" => "7.65",
    "used_cpu_user_childrens" => "0.02",
    "used_memory" => "931456",
    "used_memory_human" => "909.62K",
    "used_memory_rss" => "1904640",
    "vm_enabled" => "0",
  }
end

#initialize_copy(source) ⇒ Object



24
25
26
27
28
# File 'lib/mock_redis/database.rb', line 24

def initialize_copy(source)
  @data = @data.clone
  @data.keys.each {|k| @data[k] = @data[k].clone}
  @expire_times = @expire_times.map{|x| x.clone}
end

#keys(format) ⇒ Object



277
278
279
# File 'lib/mock_redis/database.rb', line 277

def keys(format)
  data.keys.grep(redis_pattern_to_ruby_regex(format))
end

#lastsaveObject



281
282
283
# File 'lib/mock_redis/database.rb', line 281

def lastsave
  Time.now.to_i
end

#persist(key) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/mock_redis/database.rb', line 285

def persist(key)
  if exists(key) && has_expiration?(key)
    remove_expiration(key)
    true
  else
    false
  end
end

#pingObject



294
295
296
# File 'lib/mock_redis/database.rb', line 294

def ping
  'PONG'
end

#quitObject



298
299
300
# File 'lib/mock_redis/database.rb', line 298

def quit
  'OK'
end

#randomkeyObject



302
303
304
# File 'lib/mock_redis/database.rb', line 302

def randomkey
  data.keys[rand(data.length)]
end

#rename(key, newkey) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/mock_redis/database.rb', line 306

def rename(key, newkey)
  if key == newkey
    raise RuntimeError, "ERR source and destination objects are the same"
  end
  data[newkey] = data.delete(key)
  'OK'
end

#renamenx(key, newkey) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/mock_redis/database.rb', line 314

def renamenx(key, newkey)
  if key == newkey
    raise RuntimeError, "ERR source and destination objects are the same"
  end
  if exists(newkey)
    false
  else
    rename(key, newkey)
    true
  end
end

#saveObject



326
327
328
# File 'lib/mock_redis/database.rb', line 326

def save
  'OK'
end

#ttl(key) ⇒ Object



330
331
332
333
334
335
336
# File 'lib/mock_redis/database.rb', line 330

def ttl(key)
  if has_expiration?(key)
    (expiration(key) - Time.now).to_i
  else
    -1
  end
end

#type(key) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/mock_redis/database.rb', line 338

def type(key)
  if !exists(key)
    'none'
  elsif hashy?(key)
    'hash'
  elsif stringy?(key)
    'string'
  elsif listy?(key)
    'list'
  elsif sety?(key)
    'set'
  elsif zsety?(key)
    'zset'
  else
    raise ArgumentError, "Not sure how #{data[key].inspect} got in here"
  end
end