Module: MockRedis::StringMethods

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

Instance Method Summary collapse

Instance Method Details

#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

#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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mock_redis/string_methods.rb', line 27

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



43
44
45
46
# File 'lib/mock_redis/string_methods.rb', line 43

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

#getset(key, value) ⇒ Object



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

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

#incr(key) ⇒ Object



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

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

#incrby(key, n) ⇒ Object



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

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

  unless looks_like_integer?(n.to_s)
    raise RuntimeError, "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

#mget(*keys) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/mock_redis/string_methods.rb', line 74

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

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

#mset(*kvpairs) ⇒ Object



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

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

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

  "OK"
end

#msetnx(*kvpairs) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/mock_redis/string_methods.rb', line 95

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

  if kvpairs.each_slice(2).any? {|(k,v)| exists(k)}
    0
  else
    mset(*kvpairs)
    1
  end
end

#set(key, value) ⇒ Object



106
107
108
109
# File 'lib/mock_redis/string_methods.rb', line 106

def set(key, value)
  data[key] = value.to_s
  'OK'
end

#setbit(key, offset, value) ⇒ Object



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

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
  char_as_number |=
    (2**((char.bytesize * 8)-1) >>
    (offset_within_char * 8 + offset_within_byte))
  str[char_index] = char_as_number.chr

  data[key] = str
  retval
end

#setex(key, seconds, value) ⇒ Object



149
150
151
152
153
# File 'lib/mock_redis/string_methods.rb', line 149

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

#setnx(key, value) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/mock_redis/string_methods.rb', line 155

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

#setrange(key, offset, value) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/mock_redis/string_methods.rb', line 164

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



174
175
176
177
# File 'lib/mock_redis/string_methods.rb', line 174

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