Class: MockRedis::Database
- Inherits:
-
Object
- Object
- MockRedis::Database
show all
- Includes:
- ConnectionMethod, GeospatialMethods, HashMethods, InfoMethod, ListMethods, MemoryMethod, SetMethods, SortMethod, StreamMethods, StringMethods, UtilityMethods, ZsetMethods
- Defined in:
- lib/mock_redis/database.rb
Constant Summary
GeospatialMethods::D_R, GeospatialMethods::EARTH_RADIUS_IN_METERS, GeospatialMethods::LAT_RANGE, GeospatialMethods::LNG_RANGE, GeospatialMethods::STEP, GeospatialMethods::UNITS
Constants included
from InfoMethod
InfoMethod::ALL_SECTIONS, InfoMethod::CLIENTS_INFO, InfoMethod::COMMAND_STATS_COMBINED_INFO, InfoMethod::COMMAND_STATS_SOLO_INFO, InfoMethod::CPU_INFO, InfoMethod::DEFAULT_SECTIONS, InfoMethod::KEYSPACE_INFO, InfoMethod::MEMORY_INFO, InfoMethod::PERSISTENCE_INFO, InfoMethod::REPLICATION_INFO, InfoMethod::SECTIONS, InfoMethod::SECTION_NAMES, InfoMethod::SERVER_INFO, InfoMethod::STATS_INFO
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#auth(_) ⇒ Object
-
#bgrewriteaof ⇒ Object
-
#bgsave ⇒ Object
-
#call(*command, &_block) ⇒ Object
FIXME: Current implementation of ‘call` does not work propetly with kwarg-options.
-
#connected? ⇒ Boolean
-
#dbsize ⇒ Object
-
#del(*keys) ⇒ Object
(also: #unlink)
-
#disconnect ⇒ Object
(also: #close)
-
#dump(key) ⇒ Object
-
#echo(msg) ⇒ Object
-
#eval(*args) ⇒ Object
-
#evalsha(*args) ⇒ Object
-
#exists(*keys) ⇒ Object
-
#exists?(*keys) ⇒ Boolean
-
#expire(key, seconds, nx: nil, xx: nil, lt: nil, gt: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#expire_keys ⇒ Object
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.
-
#expireat(key, timestamp, nx: nil, xx: nil, lt: nil, gt: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#flushdb ⇒ Object
-
#initialize(base, *_args) ⇒ Database
constructor
A new instance of Database.
-
#initialize_copy(_source) ⇒ Object
-
#keys(format = '*') ⇒ Object
-
#lastsave ⇒ Object
-
#now ⇒ Object
(also: #time)
-
#persist(key) ⇒ Object
-
#pexpire(key, ms, nx: nil, xx: nil, lt: nil, gt: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#pexpireat(key, timestamp_ms, nx: nil, xx: nil, lt: nil, gt: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#ping(response = 'PONG') ⇒ Object
-
#pttl(key) ⇒ Object
-
#quit ⇒ Object
-
#randomkey ⇒ Object
-
#rename(key, newkey) ⇒ Object
-
#renamenx(key, newkey) ⇒ Object
-
#restore(key, ttl, value, replace: false) ⇒ Object
-
#save ⇒ Object
-
#scan(cursor, opts = {}) ⇒ Object
-
#scan_each(opts = {}, &block) ⇒ Object
-
#script(subcommand, *args) ⇒ Object
-
#ttl(key) ⇒ Object
-
#type(key) ⇒ Object
#memory
#connection
#xadd, #xlen, #xrange, #xread, #xrevrange, #xtrim
#geodist, #geohash, #geopos
Methods included from InfoMethod
#info
Methods included from SortMethod
#sort
#zadd, #zcard, #zcount, #zincrby, #zinterstore, #zmscore, #zpopmax, #zpopmin, #zrange, #zrangebyscore, #zrank, #zrem, #zremrangebyrank, #zremrangebyscore, #zrevrange, #zrevrangebyscore, #zrevrank, #zscan, #zscan_each, #zscore, #zunionstore
#append, #bitcount, #bitfield, #decr, #decrby, #get, #getbit, #getdel, #getrange, #getset, #incr, #incrby, #incrbyfloat, #mapped_mget, #mapped_mset, #mapped_msetnx, #mget, #mset, #msetnx, #psetex, #set, #setbit, #setex, #setnx, #setrange, #strlen
Methods included from SetMethods
#sadd, #sadd?, #scard, #sdiff, #sdiffstore, #sinter, #sinterstore, #sismember, #smembers, #smismember, #smove, #spop, #srandmember, #srem, #srem?, #sscan, #sscan_each, #sunion, #sunionstore
#blmove, #blpop, #brpop, #brpoplpush, #lindex, #linsert, #llen, #lmove, #lmpop, #lpop, #lpush, #lpushx, #lrange, #lrem, #lset, #ltrim, #rpop, #rpoplpush, #rpush, #rpushx
#hdel, #hexists, #hget, #hgetall, #hincrby, #hincrbyfloat, #hkeys, #hlen, #hmget, #hmset, #hscan, #hscan_each, #hset, #hsetnx, #hvals, #mapped_hmget, #mapped_hmset
Constructor Details
#initialize(base, *_args) ⇒ Database
Returns a new instance of Database.
34
35
36
37
38
|
# File 'lib/mock_redis/database.rb', line 34
def initialize(base, *_args)
@base = base
@data = MockRedis::IndifferentHash.new
@expire_times = []
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
32
33
34
|
# File 'lib/mock_redis/database.rb', line 32
def data
@data
end
|
#expire_times ⇒ Object
Returns the value of attribute expire_times.
32
33
34
|
# File 'lib/mock_redis/database.rb', line 32
def expire_times
@expire_times
end
|
Instance Method Details
#auth(_) ⇒ Object
67
68
69
|
# File 'lib/mock_redis/database.rb', line 67
def auth(_)
'OK'
end
|
#bgrewriteaof ⇒ Object
71
72
73
|
# File 'lib/mock_redis/database.rb', line 71
def bgrewriteaof
'Background append only file rewriting started'
end
|
#bgsave ⇒ Object
75
76
77
|
# File 'lib/mock_redis/database.rb', line 75
def bgsave
'Background saving started'
end
|
#call(*command, &_block) ⇒ Object
FIXME: Current implementation of ‘call` does not work propetly with kwarg-options. i.e. `call(“EXPIRE”, “foo”, 40, “NX”)` (which redis-rb will simply transmit to redis-server) will be passed to `#expire` without keywords transformation.
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/mock_redis/database.rb', line 51
def call(*command, &_block)
command = command.flatten
cmd_name = command[0].downcase.to_s
if cmd_name.include?('expire')
send_expires(command)
elsif cmd_name == 'info'
info_raw(*command[1..])
else
public_send(cmd_name, *command[1..])
end
end
|
#connected? ⇒ Boolean
85
86
87
|
# File 'lib/mock_redis/database.rb', line 85
def connected?
true
end
|
#dbsize ⇒ Object
89
90
91
|
# File 'lib/mock_redis/database.rb', line 89
def dbsize
data.keys.length
end
|
#del(*keys) ⇒ Object
Also known as:
unlink
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/mock_redis/database.rb', line 93
def del(*keys)
keys = keys.flatten.map(&:to_s)
keys.
find_all { |key| data[key] }.
each { |k| persist(k) }.
each { |k| data.delete(k) }.
length
end
|
#disconnect ⇒ Object
Also known as:
close
79
80
81
|
# File 'lib/mock_redis/database.rb', line 79
def disconnect
nil
end
|
#dump(key) ⇒ Object
166
167
168
169
|
# File 'lib/mock_redis/database.rb', line 166
def dump(key)
value = data[key]
value ? Marshal.dump(value) : nil
end
|
#echo(msg) ⇒ Object
105
106
107
|
# File 'lib/mock_redis/database.rb', line 105
def echo(msg)
msg.to_s
end
|
#eval(*args) ⇒ Object
313
|
# File 'lib/mock_redis/database.rb', line 313
def eval(*args); end
|
#evalsha(*args) ⇒ Object
311
|
# File 'lib/mock_redis/database.rb', line 311
def evalsha(*args); end
|
#exists(*keys) ⇒ Object
152
153
154
|
# File 'lib/mock_redis/database.rb', line 152
def exists(*keys)
keys.count { |key| data.key?(key) }
end
|
#exists?(*keys) ⇒ Boolean
156
157
158
159
|
# File 'lib/mock_redis/database.rb', line 156
def exists?(*keys)
keys.each { |key| return true if data.key?(key) }
false
end
|
#expire(key, seconds, nx: nil, xx: nil, lt: nil, gt: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
109
110
111
112
113
|
# File 'lib/mock_redis/database.rb', line 109
def expire(key, seconds, nx: nil, xx: nil, lt: nil, gt: nil) seconds = Integer(seconds)
pexpire(key, seconds.to_i * 1000, nx: nx, xx: xx, lt: lt, gt: gt)
end
|
#expire_keys ⇒ Object
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.
408
409
410
411
412
413
414
415
416
417
418
419
|
# File 'lib/mock_redis/database.rb', line 408
def expire_keys
now_sec, miliseconds = now
now_ms = now_sec * 1_000 + miliseconds
to_delete = expire_times.take_while do |(time, _key)|
(time.to_r * 1_000).to_i <= now_ms
end
to_delete.each do |(_time, key)|
del(key)
end
end
|
#expireat(key, timestamp, nx: nil, xx: nil, lt: nil, gt: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
123
124
125
126
127
|
# File 'lib/mock_redis/database.rb', line 123
def expireat(key, timestamp, nx: nil, xx: nil, lt: nil, gt: nil) timestamp = Integer(timestamp)
pexpireat(key, timestamp.to_i * 1000, nx: nx, xx: xx, lt: lt, gt: gt)
end
|
#flushdb ⇒ Object
161
162
163
164
|
# File 'lib/mock_redis/database.rb', line 161
def flushdb
data.each_key { |k| del(k) }
'OK'
end
|
#initialize_copy(_source) ⇒ Object
40
41
42
43
44
|
# File 'lib/mock_redis/database.rb', line 40
def initialize_copy(_source)
@data = @data.clone
@data.each_key { |k| @data[k] = @data[k].clone }
@expire_times = @expire_times.map(&:clone)
end
|
#keys(format = '*') ⇒ Object
182
183
184
|
# File 'lib/mock_redis/database.rb', line 182
def keys(format = '*')
data.keys.grep(redis_pattern_to_ruby_regex(format))
end
|
#lastsave ⇒ Object
200
201
202
|
# File 'lib/mock_redis/database.rb', line 200
def lastsave
now.first
end
|
#now ⇒ Object
Also known as:
time
282
283
284
285
286
|
# File 'lib/mock_redis/database.rb', line 282
def now
current_time = @base.options[:time_class].now
miliseconds = (current_time.to_r - current_time.to_i) * 1_000
[current_time.to_i, miliseconds.to_i]
end
|
#persist(key) ⇒ Object
204
205
206
207
208
209
210
211
|
# File 'lib/mock_redis/database.rb', line 204
def persist(key)
if exists?(key) && has_expiration?(key)
remove_expiration(key)
true
else
false
end
end
|
#pexpire(key, ms, nx: nil, xx: nil, lt: nil, gt: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
115
116
117
118
119
120
121
|
# File 'lib/mock_redis/database.rb', line 115
def pexpire(key, ms, nx: nil, xx: nil, lt: nil, gt: nil) ms = Integer(ms)
now, miliseconds = @base.now
now_ms = (now * 1000) + miliseconds
pexpireat(key, now_ms + ms.to_i, nx: nx, xx: xx, lt: lt, gt: gt)
end
|
#pexpireat(key, timestamp_ms, nx: nil, xx: nil, lt: nil, gt: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/mock_redis/database.rb', line 129
def pexpireat(key, timestamp_ms, nx: nil, xx: nil, lt: nil, gt: nil) timestamp_ms = Integer(timestamp_ms)
if nx && gt || gt && lt || lt && nx || nx && xx
raise Error.command_error(
'ERR NX and XX, GT or LT options at the same time are not compatible',
self
)
end
return false unless exists?(key)
expiry = expiration(key)
new_expiry = @base.time_at(Rational(timestamp_ms.to_i, 1000))
if should_update_expiration?(expiry, new_expiry, nx: nx, xx: xx, lt: lt, gt: gt)
set_expiration(key, new_expiry)
true
else
false
end
end
|
#ping(response = 'PONG') ⇒ Object
213
214
215
|
# File 'lib/mock_redis/database.rb', line 213
def ping(response = 'PONG')
response
end
|
#pttl(key) ⇒ Object
269
270
271
272
273
274
275
276
277
278
279
280
|
# File 'lib/mock_redis/database.rb', line 269
def pttl(key)
now, miliseconds = @base.now
now_ms = now * 1000 + miliseconds
if !exists?(key)
-2
elsif has_expiration?(key)
(expiration(key).to_r * 1000).to_i - now_ms
else
-1
end
end
|
#quit ⇒ Object
217
218
219
|
# File 'lib/mock_redis/database.rb', line 217
def quit
'OK'
end
|
#randomkey ⇒ Object
221
222
223
|
# File 'lib/mock_redis/database.rb', line 221
def randomkey
data.keys[rand(data.length)]
end
|
#rename(key, newkey) ⇒ Object
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/mock_redis/database.rb', line 225
def rename(key, newkey)
unless data.include?(key)
raise Error.command_error('ERR no such key', self)
end
if key != newkey
data[newkey] = data.delete(key)
if has_expiration?(key)
set_expiration(newkey, expiration(key))
remove_expiration(key)
end
end
'OK'
end
|
#renamenx(key, newkey) ⇒ Object
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/mock_redis/database.rb', line 241
def renamenx(key, newkey)
unless data.include?(key)
raise Error.command_error('ERR no such key', self)
end
if exists?(newkey)
false
else
rename(key, newkey)
true
end
end
|
#restore(key, ttl, value, replace: false) ⇒ Object
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/mock_redis/database.rb', line 171
def restore(key, ttl, value, replace: false)
if !replace && exists?(key)
raise Error.command_error('BUSYKEY Target key name already exists.', self)
end
data[key] = Marshal.load(value) if ttl > 0
pexpire(key, ttl)
end
'OK'
end
|
#save ⇒ Object
254
255
256
|
# File 'lib/mock_redis/database.rb', line 254
def save
'OK'
end
|
#scan(cursor, opts = {}) ⇒ Object
186
187
188
|
# File 'lib/mock_redis/database.rb', line 186
def scan(cursor, opts = {})
common_scan(data.keys, cursor, opts)
end
|
#scan_each(opts = {}, &block) ⇒ Object
190
191
192
193
194
195
196
197
198
|
# File 'lib/mock_redis/database.rb', line 190
def scan_each(opts = {}, &block)
return to_enum(:scan_each, opts) unless block_given?
cursor = 0
loop do
cursor, keys = scan(cursor, opts)
keys.each(&block)
break if cursor == '0'
end
end
|
#script(subcommand, *args) ⇒ Object
309
|
# File 'lib/mock_redis/database.rb', line 309
def script(subcommand, *args); end
|
#ttl(key) ⇒ Object
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/mock_redis/database.rb', line 258
def ttl(key)
if !exists?(key)
-2
elsif has_expiration?(key)
now, = @base.now
expiration(key).to_i - now
else
-1
end
end
|
#type(key) ⇒ Object
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/mock_redis/database.rb', line 289
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'
elsif streamy?(key)
'stream'
else
raise ArgumentError, "Not sure how #{data[key].inspect} got in here"
end
end
|