Module: MockRedis::StringMethods

Includes:
Assertions
Included in:
Database
Defined in:
lib/mock_redis/string_methods.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/mock_redis/string_methods.rb', line 27

def [](key)
  get(key)
end

#[]=(key, value, options = {}) ⇒ Object



169
170
171
# File 'lib/mock_redis/string_methods.rb', line 169

def []=(key, value, options = {})
  set(key, value, options)
end

#append(key, value) ⇒ Object



7
8
9
10
11
12
# File 'lib/mock_redis/string_methods.rb', line 7

def append(key, value)
  assert_stringy(key)
  data[key] ||= ''
  data[key] << value
  data[key].length
end

#bitcount(key, start = 0, stop = -1)) ⇒ Object



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
# File 'lib/mock_redis/string_methods.rb', line 219

def bitcount(key, start = 0, stop = -1)
  assert_stringy(key)

  str   = data[key] || ''
  count = 0
  m1    = 0x5555555555555555
  m2    = 0x3333333333333333
  m4    = 0x0f0f0f0f0f0f0f0f
  m8    = 0x00ff00ff00ff00ff
  m16   = 0x0000ffff0000ffff
  m32   = 0x00000000ffffffff

  str.bytes.to_a[start..stop].each do |byte|
    # Naive Hamming weight
    c = byte
    c = (c & m1) + ((c >> 1) & m1)
    c = (c & m2) + ((c >> 2) & m2)
    c = (c & m4) + ((c >> 4) & m4)
    c = (c & m8) + ((c >> 8) & m8)
    c = (c & m16) + ((c >> 16) & m16)
    c = (c & m32) + ((c >> 32) & m32)
    count += c
  end

  count
end

#decr(key) ⇒ Object



14
15
16
# File 'lib/mock_redis/string_methods.rb', line 14

def decr(key)
  decrby(key, 1)
end

#decrby(key, n) ⇒ Object



18
19
20
# File 'lib/mock_redis/string_methods.rb', line 18

def decrby(key, n)
  incrby(key, -n)
end

#get(key) ⇒ Object



22
23
24
25
# File 'lib/mock_redis/string_methods.rb', line 22

def get(key)
  assert_stringy(key)
  data[key]
end

#getbit(key, offset) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mock_redis/string_methods.rb', line 31

def getbit(key, offset)
  assert_stringy(key)

  offset_of_byte = offset / 8
  offset_within_byte = offset % 8

  # String#getbyte would be lovely, but it's not in 1.8.7.
  byte = (data[key] || '').each_byte.drop(offset_of_byte).first

  if byte
    (byte & (2**7 >> offset_within_byte)) > 0 ? 1 : 0
  else
    0
  end
end

#getrange(key, start, stop) ⇒ Object



47
48
49
50
# File 'lib/mock_redis/string_methods.rb', line 47

def getrange(key, start, stop)
  assert_stringy(key)
  (data[key] || '')[start..stop]
end

#getset(key, value) ⇒ Object



52
53
54
55
56
# File 'lib/mock_redis/string_methods.rb', line 52

def getset(key, value)
  retval = get(key)
  set(key, value)
  retval
end

#incr(key) ⇒ Object



58
59
60
# File 'lib/mock_redis/string_methods.rb', line 58

def incr(key)
  incrby(key, 1)
end

#incrby(key, n) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mock_redis/string_methods.rb', line 62

def incrby(key, n)
  assert_stringy(key)
  unless can_incr?(data[key])
    raise Redis::CommandError, 'ERR value is not an integer or out of range'
  end

  unless looks_like_integer?(n.to_s)
    raise Redis::CommandError, 'ERR value is not an integer or out of range'
  end

  new_value = data[key].to_i + n.to_i
  data[key] = new_value.to_s
  # for some reason, redis-rb doesn't return this as a string.
  new_value
end

#incrbyfloat(key, n) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mock_redis/string_methods.rb', line 78

def incrbyfloat(key, n)
  assert_stringy(key)
  unless can_incr_float?(data[key])
    raise Redis::CommandError, 'ERR value is not a valid float'
  end

  unless looks_like_float?(n.to_s)
    raise Redis::CommandError, 'ERR value is not a valid float'
  end

  new_value = data[key].to_f + n.to_f
  data[key] = new_value.to_s
  # for some reason, redis-rb doesn't return this as a string.
  new_value
end

#mapped_mget(*keys) ⇒ Object



102
103
104
# File 'lib/mock_redis/string_methods.rb', line 102

def mapped_mget(*keys)
  Hash[keys.zip(mget(*keys))]
end

#mapped_mset(hash) ⇒ Object



119
120
121
# File 'lib/mock_redis/string_methods.rb', line 119

def mapped_mset(hash)
  mset(*hash.to_a.flatten)
end

#mapped_msetnx(hash) ⇒ Object



134
135
136
# File 'lib/mock_redis/string_methods.rb', line 134

def mapped_msetnx(hash)
  msetnx(*hash.to_a.flatten)
end

#mget(*keys) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/mock_redis/string_methods.rb', line 94

def mget(*keys)
  assert_has_args(keys, 'mget')

  keys.map do |key|
    get(key) if stringy?(key)
  end
end

#mset(*kvpairs) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mock_redis/string_methods.rb', line 106

def mset(*kvpairs)
  assert_has_args(kvpairs, 'mset')
  if kvpairs.length.odd?
    raise Redis::CommandError, 'ERR wrong number of arguments for MSET'
  end

  kvpairs.each_slice(2) do |(k, v)|
    set(k, v)
  end

  'OK'
end

#msetnx(*kvpairs) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/mock_redis/string_methods.rb', line 123

def msetnx(*kvpairs)
  assert_has_args(kvpairs, 'msetnx')

  if kvpairs.each_slice(2).any? { |(k, _)| exists(k) }
    false
  else
    mset(*kvpairs)
    true
  end
end

#set(key, value, options = {}) ⇒ Object



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
# File 'lib/mock_redis/string_methods.rb', line 138

def set(key, value, options = {})
  return_true = false
  options = options.dup
  if options.delete(:nx)
    if exists(key)
      return false
    else
      return_true = true
    end
  end
  if options.delete(:xx)
    if exists(key)
      return_true = true
    else
      return false
    end
  end
  data[key] = value.to_s

  # take latter
  expire_option = options.to_a.last
  if expire_option
    type, duration = expire_option
    if duration == 0
      raise Redis::CommandError, 'ERR invalid expire time in set'
    end
    expire(key, type.to_sym == :ex ? duration : duration / 1000.0)
  end
  return_true ? true : 'OK'
end

#setbit(key, offset, value) ⇒ Object



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
# File 'lib/mock_redis/string_methods.rb', line 173

def setbit(key, offset, value)
  assert_stringy(key, 'ERR bit is not an integer or out of range')
  retval = getbit(key, offset)

  str = data[key] || ''

  offset_of_byte = offset / 8
  offset_within_byte = offset % 8

  if offset_of_byte >= str.bytesize
    str = zero_pad(str, offset_of_byte + 1)
  end

  char_index = byte_index = offset_within_char = 0
  str.each_char do |c|
    if byte_index < offset_of_byte
      char_index += 1
      byte_index += c.bytesize
    else
      offset_within_char = byte_index - offset_of_byte
      break
    end
  end

  char = str[char_index]
  char = char.chr if char.respond_to?(:chr) # ruby 1.8 vs 1.9
  char_as_number = char.each_byte.reduce(0) do |a, byte|
    (a << 8) + byte
  end

  bitmask_length = (char.bytesize * 8 - offset_within_char * 8 - offset_within_byte - 1)
  bitmask = 1 << bitmask_length

  if value.zero?
    bitmask ^= 2**(char.bytesize * 8) - 1
    char_as_number &= bitmask
  else
    char_as_number |= bitmask
  end

  str[char_index] = char_as_number.chr

  data[key] = str
  retval
end

#setex(key, seconds, value) ⇒ Object



246
247
248
249
250
# File 'lib/mock_redis/string_methods.rb', line 246

def setex(key, seconds, value)
  set(key, value)
  expire(key, seconds)
  'OK'
end

#setnx(key, value) ⇒ Object



252
253
254
255
256
257
258
259
# File 'lib/mock_redis/string_methods.rb', line 252

def setnx(key, value)
  if exists(key)
    false
  else
    set(key, value)
    true
  end
end

#setrange(key, offset, value) ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'lib/mock_redis/string_methods.rb', line 261

def setrange(key, offset, value)
  assert_stringy(key)
  value = value.to_s
  old_value = (data[key] || '')

  prefix = zero_pad(old_value[0...offset], offset)
  data[key] = prefix + value + (old_value[(offset + value.length)..-1] || '')
  data[key].length
end

#strlen(key) ⇒ Object



271
272
273
274
# File 'lib/mock_redis/string_methods.rb', line 271

def strlen(key)
  assert_stringy(key)
  (data[key] || '').bytesize
end