Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 887 lines (811 sloc) 26.549 kB
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
1 require 'socket'
3778455 Implement special case keys for making sure you get the same server u…
Ezra Zygmuntowicz authored
2 require File.join(File.dirname(__FILE__),'better_timeout')
16fd66f set_members and set_intersect return a ruby Set now not an array
Ezra Zygmuntowicz authored
3 require 'set'
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
4
5 class RedisError < StandardError
6 end
7
8 class Redis
9 OK = "+OK".freeze
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
10 ERRCODE = "-".freeze
0951204 simplify
Ezra Zygmuntowicz authored
11 NIL = 'nil'.freeze
6bce5bd adding HashRing and RedisCLoud for distributing work across a farm of…
Ezra Zygmuntowicz authored
12 CTRLF = "\r\n".freeze
13
0f81ee1 properly delegate keys "*" to collect keys from each cloud member.
Ezra Zygmuntowicz authored
14 def to_s
15 "#{host}:#{port}"
16 end
17
6bce5bd adding HashRing and RedisCLoud for distributing work across a farm of…
Ezra Zygmuntowicz authored
18 def port
19 @opts[:port]
20 end
21
22 def host
23 @opts[:host]
24 end
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
25
26 def initialize(opts={})
27 @opts = {:host => 'localhost', :port => '6379'}.merge(opts)
28 end
29
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
30 # SET key value
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
31 # Time complexity: O(1)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
32 # Set the string value as value of the key. The string can't be longer
33 # than 1073741824 bytes (1 GB).
34 #
35 # Return value: status code reply
36 def []=(key, val)
e9034d9 Now with marshal where and when appropriate.
Taylor Weibley authored
37 val = redis_marshal(val)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
38 timeout_retry(3, 3){
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
39 write "SET #{key} #{val.to_s.size}\r\n#{val}\r\n"
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
40 status_code_reply
41 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
42 end
43
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
44 # SETNX key value
45 #
46 # Time complexity: O(1)
47 # SETNX works exactly like SET with the only difference that if the key
48 # already exists no operation is performed. SETNX actually means "SET if Not eXists".
49 #
50 # *Return value: integer reply, specifically:
51 #
52 # 1 if the key was set 0 if the key was not set
53 def set_unless_exists(key, val)
e9034d9 Now with marshal where and when appropriate.
Taylor Weibley authored
54 val = redis_marshal(val)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
55 timeout_retry(3, 3){
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
56 write "SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n"
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
57 integer_reply == 1
58 }
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
59 end
60
61 # GET key
62 # Time complexity: O(1)
63 # Get the value of the specified key. If the key does not exist the special value
64 # 'nil' is returned. If the value stored at key is not a string an error is
65 # returned because GET can only handle string values.
66 #
6bce5bd adding HashRing and RedisCLoud for distributing work across a farm of…
Ezra Zygmuntowicz authored
67 # Return value: bulk reply
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
68 def [](key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
69 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
70 write "GET #{key}\r\n"
e9034d9 Now with marshal where and when appropriate.
Taylor Weibley authored
71 redis_unmarshal(bulk_reply)
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
72 }
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
73 end
74
75 # INCR key
76 # INCRBY key value
77 # Time complexity: O(1)
78 # Increment the number stored at key by one. If the key does not exist or contains
79 # a value of a wrong type, set the key to the value of "1" (like if the previous
80 # value was zero).
81 #
82 # INCRBY works just like INCR but instead to increment by 1 the increment is value.
83 #
84 # Return value: integer reply
85 def incr(key, increment=nil)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
86 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
87 if increment
88 write "INCRBY #{key} #{increment}\r\n"
89 else
90 write "INCR #{key}\r\n"
91 end
92 integer_reply
93 }
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
94 end
95
96
97 # DECR key
98 #
99 # DECRBY key value
100 #
101 # Time complexity: O(1) Like INCR/INCRBY but decrementing instead of incrementing.
102 def decr(key, increment=nil)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
103 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
104 if increment
105 write "DECRBY #{key} #{increment}\r\n"
106 else
107 write "DECR #{key}\r\n"
108 end
109 integer_reply
110 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
111 end
112
113 # RANDOMKEY
114 # Time complexity: O(1)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
115 # Returns a random key from the currently seleted DB.
116 #
117 # Return value: single line reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
118 def randkey
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
119 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
120 write "RANDOMKEY\r\n"
121 single_line_reply
122 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
123 end
124
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
125 # RENAME oldkey newkey
126 #
127 # Atomically renames the key oldkey to newkey. If the source and destination
128 # name are the same an error is returned. If newkey already exists it is
129 # overwritten.
130 #
131 # Return value: status code reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
132 def rename!(oldkey, newkey)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
133 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
134 write "RENAME #{oldkey} #{newkey}\r\n"
135 status_code_reply
136 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
137 end
138
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
139 # RENAMENX oldkey newkey
140 # Just like RENAME but fails if the destination key newkey already exists.
141 #
142 # *Return value: integer reply, specifically:
143 #
144 # 1 if the key was renamed 0 if the target key already exist -1 if the
145 # source key does not exist -3 if source and destination keys are the same
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
146 def rename(oldkey, newkey)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
147 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
148 write "RENAMENX #{oldkey} #{newkey}\r\n"
149 case integer_reply
150 when -1
151 raise RedisError, "source key: #{oldkey} does not exist"
152 when 0
153 raise RedisError, "target key: #{oldkey} already exists"
154 when -3
155 raise RedisError, "source and destination keys are the same"
156 when 1
157 true
158 end
159 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
160 end
161
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
162 # EXISTS key
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
163 # Time complexity: O(1)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
164 # Test if the specified key exists. The command returns "0" if the key
165 # exists, otherwise "1" is returned. Note that even keys set with an empty
166 # string as value will return "1".
167 #
168 # *Return value: integer reply, specifically:
169 #
170 # 1 if the key exists 0 if the key does not exist
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
171 def key?(key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
172 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
173 write "EXISTS #{key}\r\n"
174 integer_reply == 1
175 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
176 end
177
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
178 # DEL key
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
179 # Time complexity: O(1)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
180 # Remove the specified key. If the key does not exist no operation is
181 # performed. The command always returns success.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
182 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
183 # *Return value: integer reply, specifically:
184 #
185 # 1 if the key was removed 0 if the key does not exist
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
186 def delete(key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
187 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
188 write "DEL #{key}\r\n"
189 integer_reply == 1
190 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
191 end
192
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
193 # KEYS pattern
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
194 # Time complexity: O(n) (with n being the number of keys in the DB)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
195 # Returns all the keys matching the glob-style pattern as space separated strings.
196 # For example if you have in the database the keys "foo" and "foobar" the command
197 # "KEYS foo*" will return "foo foobar".
198 #
199 # Note that while the time complexity for this operation is O(n) the constant times
200 # are pretty low. For example Redis running on an entry level laptop can scan a 1
201 # million keys database in 40 milliseconds. Still it's better to consider this one
202 # of the slow commands that may ruin the DB performance if not used with care.
203 #
204 # Return value: bulk reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
205 def keys(glob)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
206 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
207 write "KEYS #{glob}\r\n"
208 bulk_reply.split(' ')
209 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
210 end
211
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
212 # TYPE key
213 #
214 # Time complexity: O(1) Return the type of the value stored at key in form of
215 # a string. The type can be one of "none", "string", "list", "set". "none" is
216 # returned if the key does not exist.
217 #
218 # Return value: single line reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
219 def type?(key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
220 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
221 write "TYPE #{key}\r\n"
222 single_line_reply
223 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
224 end
225
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
226 # RPUSH key string
227 #
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
228 # Time complexity: O(1)
6bce5bd adding HashRing and RedisCLoud for distributing work across a farm of…
Ezra Zygmuntowicz authored
229 # Add the given string to the tail of the list contained at key. If the key
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
230 # does not exist an empty list is created just before the append operation.
231 # If the key exists but is not a List an error is returned.
232 #
233 # Return value: status code reply
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
234 def push_tail(key, string)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
235 timeout_retry(3, 3){
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
236 write "RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
237 status_code_reply
238 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
239 end
240
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
241 # LPUSH key string
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
242 # Time complexity: O(1)
6bce5bd adding HashRing and RedisCLoud for distributing work across a farm of…
Ezra Zygmuntowicz authored
243 # Add the given string to the head of the list contained at key. If the
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
244 # key does not exist an empty list is created just before the append operation.
245 # If the key exists but is not a List an error is returned.
246 #
247 # Return value: status code reply
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
248 def push_head(key, string)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
249 timeout_retry(3, 3){
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
250 write "LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
251 status_code_reply
252 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
253 end
254
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
255 # LPOP key
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
256 #
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
257 # Time complexity: O(1)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
258 # Atomically return and remove the first element of the list. For example if
259 # the list contains the elements "a","b","c" LPOP will return "a" and the
260 # list will become "b","c".
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
261 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
262 # If the key does not exist or the list is already empty the special value
263 # 'nil' is returned.
264 #
265 # Return value: bulk reply
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
266 def pop_head(key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
267 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
268 write "LPOP #{key}\r\n"
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
269 bulk_reply
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
270 }
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
271 end
272
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
273 # RPOP key
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
274 # This command works exactly like LPOP, but the last element instead
275 # of the first element of the list is returned/deleted.
276 def pop_tail(key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
277 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
278 write "RPOP #{key}\r\n"
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
279 bulk_reply
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
280 }
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
281 end
282
6ccfb1b implement and spec LSET for setting a single element of a list value …
Ezra Zygmuntowicz authored
283 # LSET key index value
284 # Time complexity: O(N) (with N being the length of the list)
285 # Set the list element at index (see LINDEX for information about the index argument) with the new value. Out of range indexes will generate an error. Note that setting the first or last elements of the list is O(1).
286 #
287 # Return value: status code reply
288 def list_set(key, index, val)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
289 timeout_retry(3, 3){
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
290 write "LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n"
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
291 status_code_reply
292 }
6ccfb1b implement and spec LSET for setting a single element of a list value …
Ezra Zygmuntowicz authored
293 end
294
295
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
296 # LLEN key
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
297 # Time complexity: O(1)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
298 # Return the length of the list stored at the specified key. If the key does not
299 # exist zero is returned (the same behaviour as for empty lists). If the value
300 # stored at key is not a list the special value -1 is returned. Note: client
301 # library should raise an exception when -1 is returned instead to pass the
302 # value back to the caller like a normal list length value.
303 #
304 # *Return value: integer reply, specifically:
305 #
306 # the length of the list as an integer
307 # >=
308 # 0 if the operation succeeded -2 if the specified key does not hold a list valu
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
309 def list_length(key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
310 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
311 write "LLEN #{key}\r\n"
312 case i = integer_reply
313 when -2
314 raise RedisError, "key: #{key} does not hold a list value"
315 else
316 i
317 end
318 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
319 end
320
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
321 # LRANGE key start end
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
322 # Time complexity: O(n) (with n being the length of the range)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
323 # Return the specified elements of the list stored at the specified key. Start
324 # and end are zero-based indexes. 0 is the first element of the list (the list head),
325 # 1 the next element and so on.
326 #
327 # For example LRANGE foobar 0 2 will return the first three elements of the list.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
328 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
329 # start and end can also be negative numbers indicating offsets from the end of the list.
330 # For example -1 is the last element of the list, -2 the penultimate element and so on.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
331 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
332 # Indexes out of range will not produce an error: if start is over the end of the list,
333 # or start > end, an empty list is returned. If end is over the end of the list Redis
334 # will threat it just like the last element of the list.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
335 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
336 # Return value: multi bulk reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
337 def list_range(key, start, ending)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
338 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
339 write "LRANGE #{key} #{start} #{ending}\r\n"
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
340 multi_bulk_reply
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
341 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
342 end
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
343
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
344
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
345 # LTRIM key start end
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
346 # Time complexity: O(n) (with n being len of list - len of range)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
347 # Trim an existing list so that it will contain only the specified range of
348 # elements specified. Start and end are zero-based indexes. 0 is the first
349 # element of the list (the list head), 1 the next element and so on.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
350 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
351 # For example LTRIM foobar 0 2 will modify the list stored at foobar key so that
352 # only the first three elements of the list will remain.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
353 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
354 # start and end can also be negative numbers indicating offsets from the end of
355 # the list. For example -1 is the last element of the list, -2 the penultimate
356 # element and so on.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
357 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
358 # Indexes out of range will not produce an error: if start is over the end of
359 # the list, or start > end, an empty list is left as value. If end over the
360 # end of the list Redis will threat it just like the last element of the list.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
361 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
362 # Hint: the obvious use of LTRIM is together with LPUSH/RPUSH. For example:
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
363 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
364 # LPUSH mylist <someelement> LTRIM mylist 0 99
365 # The above two commands will push elements in the list taking care that the
366 # list will not grow without limits. This is very useful when using Redis
367 # to store logs for example. It is important to note that when used in this
368 # way LTRIM is an O(1) operation because in the average case just one element
369 # is removed from the tail of the list.
370 #
371 # Return value: status code reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
372 def list_trim(key, start, ending)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
373 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
374 write "LTRIM #{key} #{start} #{ending}\r\n"
375 status_code_reply
376 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
377 end
378
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
379 # LINDEX key index
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
380 # Time complexity: O(n) (with n being the length of the list)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
381 # Return the specified element of the list stored at the specified key. 0 is
382 # the first element, 1 the second and so on. Negative indexes are supported,
383 # for example -1 is the last element, -2 the penultimate and so on.
384 #
385 # If the value stored at key is not of list type an error is returned. If
386 # the index is out of range an empty string is returned.
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
387 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
388 # Note that even if the average time complexity is O(n) asking for the first
389 # or the last element of the list is O(1).
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
390 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
391 # Return value: bulk reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
392 def list_index(key, index)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
393 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
394 write "LINDEX #{key} #{index}\r\n"
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
395 bulk_reply
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
396 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
397 end
398
e13812e Added redis.list_rm for LREM to remove certain values form a list. pr…
Ezra Zygmuntowicz authored
399 # LREM key count value
400 #
401 # Time complexity: O(N) (with N being the length of the list)
402 #
403 # Remove the first count occurrences of the value element from the list.
404 # If count is zero all the elements are removed. If count is negative
405 # elements are removed from tail to head, instead to go from head to
406 # tail that is the normal behaviour. So for example LREM with count -2
407 # and hello as value to remove against the list (a,b,c,hello,x,hello,hello)
408 # will lave the list (a,b,c,hello,x). The number of removed elements is
409 # returned as an integer, see below for more information aboht the returned value.
410 # Return value
411 #
412 # Integer Reply, specifically:
413 #
414 # The number of removed elements if the operation succeeded
415 # -1 if the specified key does not exist
416 # -2 if the specified key does not hold a list value
417 def list_rm(key, count, value)
418 write "LREM #{key} #{count} #{value.to_s.size}\r\n#{value}\r\n"
419 case num = integer_reply
420 when -1
421 raise RedisError, "key: #{key} does not exist"
422 when -2
423 raise RedisError, "key: #{key} does not hold a list value"
424 else
425 num
426 end
427 end
428
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
429 # SADD key member
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
430 # Time complexity O(1)
431 # Add the specified member to the set value stored at key. If member is
432 # already a member of the set no operation is performed. If key does not
433 # exist a new set with the specified member as sole member is crated. If
434 # the key exists but does not hold a set value an error is returned.
435 #
436 # *Return value: integer reply, specifically:
437 #
438 # 1 if the new element was added 0 if the new element was already a member
439 # of the set -2 if the key contains a non set value
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
440 def set_add(key, member)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
441 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
442 write "SADD #{key} #{member.to_s.size}\r\n#{member}\r\n"
443 case integer_reply
444 when 1
445 true
446 when 0
447 false
448 when -2
449 raise RedisError, "key: #{key} contains a non set value"
450 end
451 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
452 end
453
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
454 # SREM key member
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
455 #
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
456 # Time complexity O(1)
457 # Remove the specified member from the set value stored at key. If member
458 # was not a member of the set no operation is performed. If key does not
459 # exist or does not hold a set value an error is returned.
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
460 #
461 # *Return value: integer reply, specifically:
462 #
463 # 1 if the new element was removed 0 if the new element was not a member
464 # of the set -2 if the key does not hold a set value
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
465 def set_delete(key, member)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
466 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
467 write "SREM #{key} #{member.to_s.size}\r\n#{member}\r\n"
468 case integer_reply
469 when 1
470 true
471 when 0
472 false
473 when -2
474 raise RedisError, "key: #{key} contains a non set value"
475 end
476 }
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
477 end
478
479 # SCARD key
480 # Time complexity O(1)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
481 # Return the set cardinality (number of elements). If the key does not
482 # exist 0 is returned, like for empty sets. If the key does not hold a
483 # set value -1 is returned. Client libraries should raise an error when -1
484 # is returned instead to pass the value to the caller.
485 #
486 # *Return value: integer reply, specifically:
487 #
488 # the cardinality (number of elements) of the set as an integer
489 # >=
490 # 0 if the operation succeeded -2 if the specified key does not hold a set value
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
491 def set_count(key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
492 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
493 write "SCARD #{key}\r\n"
494 case i = integer_reply
495 when -2
496 raise RedisError, "key: #{key} contains a non set value"
497 else
498 i
499 end
500 }
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
501 end
502
503 # SISMEMBER key member
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
504 #
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
505 # Time complexity O(1)
506 # Return 1 if member is a member of the set stored at key, otherwise 0 is
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
507 # returned. On error a negative value is returned. Client libraries should
508 # raise an error when a negative value is returned instead to pass the value
509 # to the caller.
510 #
511 # *Return value: integer reply, specifically:
512 #
513 # 1 if the element is a member of the set 0 if the element is not a member of
514 # the set OR if the key does not exist -2 if the key does not hold a set value
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
515 def set_member?(key, member)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
516 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
517 write "SISMEMBER #{key} #{member.to_s.size}\r\n#{member}\r\n"
518 case integer_reply
519 when 1
520 true
521 when 0
522 false
523 when -2
524 raise RedisError, "key: #{key} contains a non set value"
525 end
526 }
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
527 end
528
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
529 # SINTER key1 key2 ... keyN
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
530 # Time complexity O(N*M) worst case where N is the cardinality of the smallest
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
531 # set and M the number of sets
532 # Return the members of a set resulting from the intersection of all the sets
533 # hold at the specified keys. Like in LRANGE the result is sent to the client
534 # as a multi-bulk reply (see the protocol specification for more information).
535 # If just a single key is specified, then this command produces the same
536 # result as SELEMENTS. Actually SELEMENTS is just syntax sugar for SINTERSECT.
537 #
538 # If at least one of the specified keys does not exist or does not hold a set
539 # value an error is returned.
540 #
541 # Return value: multi bulk reply
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
542 def set_intersect(*keys)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
543 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
544 write "SINTER #{keys.join(' ')}\r\n"
16fd66f set_members and set_intersect return a ruby Set now not an array
Ezra Zygmuntowicz authored
545 Set.new(multi_bulk_reply)
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
546 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
547 end
548
8f12440 added SINTERSTORE
Ezra Zygmuntowicz authored
549 # SINTERSTORE dstkey key1 key2 ... keyN
550 #
551 # Time complexity O(N*M) worst case where N is the cardinality of the smallest set and M the number of sets
552 # This commnad works exactly like SINTER but instead of being returned the resulting set is sotred as dstkey.
553 #
554 # Return value: status code reply
555 def set_inter_store(destkey, *keys)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
556 timeout_retry(3, 3){
8f12440 added SINTERSTORE
Ezra Zygmuntowicz authored
557 write "SINTERSTORE #{destkey} #{keys.join(' ')}\r\n"
558 status_code_reply
559 }
560 end
561
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
562 # SMEMBERS key
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
563 #
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
564 # Time complexity O(N)
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
565 # Return all the members (elements) of the set value stored at key.
566 # This is just syntax glue for SINTERSECT.
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
567 def set_members(key)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
568 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
569 write "SMEMBERS #{key}\r\n"
16fd66f set_members and set_intersect return a ruby Set now not an array
Ezra Zygmuntowicz authored
570 Set.new(multi_bulk_reply)
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
571 }
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
572 end
573
574
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
575 # SORT key [BY pattern] [GET|DEL|INCR|DECR pattern] [ASC|DESC] [LIMIT start count]
576 # Sort the elements contained in the List or Set value at key. By default sorting is
577 # numeric with elements being compared as double precision floating point numbers.
578 # This is the simplest form of SORT.
579 # SORT mylist
580 #
581 # Assuming mylist contains a list of numbers, the return value will be the list of
582 # numbers ordered from the smallest to the bigger number. In order to get the sorting
583 # in reverse order use DESC:
584 # SORT mylist DESC
585 #
586 # ASC is also supported but it's the default so you don't really need it. If you
587 # want to sort lexicographically use ALPHA. Note that Redis is utf-8 aware
588 # assuming you set the right value for the LC_COLLATE environment variable.
589 #
590 # Sort is able to limit the number of results using the LIMIT option:
591 # SORT mylist LIMIT 0 10
592 # In the above example SORT will return only 10 elements, starting from the first one
593 # (star is zero-based). Almost all the sort options can be mixed together. For example:
594 # SORT mylist LIMIT 0 10 ALPHA DESC
595 # Will sort mylist lexicographically, in descending order, returning only the first
596 # 10 elements.
597 # Sometimes you want to sort elements using external keys as weights to compare
598 # instead to compare the actual List or Set elements. For example the list mylist
599 # may contain the elements 1, 2, 3, 4, that are just the unique IDs of objects
600 # stored at object_1, object_2, object_3 and object_4, while the keys weight_1,
601 # weight_2, weight_3 and weight_4 can contain weights we want to use to sort the
602 # list of objects identifiers. We can use the following command:
603 # SORT mylist BY weight_*
604 # the BY option takes a pattern (weight_* in our example) that is used in order to
605 # generate the key names of the weights used for sorting. Weight key names are obtained
606 # substituting the first occurrence of * with the actual value of the elements on the
607 # list (1,2,3,4 in our example).
608 # Still our previous example will return just the sorted IDs. Often it is needed to
609 # get the actual objects sorted (object_1, ..., object_4 in the example). We can do
610 # it with the following command:
611 # SORT mylist BY weight_* GET object_*
612 # Note that GET can be used multiple times in order to get more key for every
613 # element of the original List or Set sorted.
614
615 # redis.sort 'index', :by => 'weight_*',
616 # :order => 'DESC ALPHA',
617 # :limit => [0,10],
618 # :get => 'obj_*'
619 def sort(key, opts={})
620 cmd = "SORT #{key}"
621 cmd << " BY #{opts[:by]}" if opts[:by]
622 cmd << " GET #{opts[:get]}" if opts[:get]
623 cmd << " INCR #{opts[:incr]}" if opts[:incr]
624 cmd << " DEL #{opts[:del]}" if opts[:del]
625 cmd << " DECR #{opts[:decr]}" if opts[:decr]
626 cmd << " #{opts[:order]}" if opts[:order]
627 cmd << " LIMIT #{opts[:limit].join(' ')}" if opts[:limit]
628 cmd << "\r\n"
629 write cmd
630 multi_bulk_reply
631 end
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
632
633 # ADMIN functions for redis
634
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
635 # SELECT index
636 #
637 # Select the DB with having the specified zero-based numeric index.
638 # For default every new client connection is automatically selected to DB 0.
639 # Return value: status code reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
640 def select_db(index)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
641 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
642 write "SELECT #{index}\r\n"
643 status_code_reply
644 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
645 end
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
646
647 # MOVE key dbindex
648 #
649 # Move the specified key from the currently selected DB to the specified
650 # destination DB. Note that this command returns 1 only if the key was
651 # successfully moved, and 0 if the target key was already there or if
652 # the source key was not found at all, so it is possible to use MOVE
653 # as a locking primitive.
654 #
655 # *Return value: integer reply, specifically:
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
656 #
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
657 # 1 if the key was moved 0 if the key was not moved because already
658 # present on the target DB or was not found in the current DB. -3
659 # if the destination DB is the same as the source DB -4 if the database
660 # index if out of range
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
661 def move(key, index)
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
662 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
663 write "MOVE #{index}\r\n"
664 case integer_reply
665 when 1
666 true
667 when 0
668 false
669 when -3
670 raise RedisError, "destination db same as source db"
671 when -4
672 raise RedisError, "db index if out of range"
673 end
674 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
675 end
676
677 # SAVE
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
678 #
679 # Save the DB on disk. The server hangs while the saving is not completed,
680 # no connection is served in the meanwhile. An OK code is returned when
681 # the DB was fully stored in disk.
682 # Return value: status code reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
683 def save
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
684 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
685 write "SAVE\r\n"
686 status_code_reply
687 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
688 end
689
690 # BGSAVE
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
691 #
692 # Save the DB in background. The OK code is immediately returned. Redis
693 # forks, the parent continues to server the clients, the child saves
694 # the DB on disk then exit. A client my be able to check if the operation
695 # succeeded using the LASTSAVE command.
696 # Return value: status code reply
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
697 def bgsave
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
698 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
699 write "BGSAVE\r\n"
700 status_code_reply
701 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
702 end
703
704 # LASTSAVE
3c8ca06 Updating to redis from svn trunk with new protocol changes implemente…
Ezra Zygmuntowicz authored
705 #
706 # Return the UNIX TIME of the last DB save executed with success. A client
707 # may check if a BGSAVE command succeeded reading the LASTSAVE value, then
708 # issuing a BGSAVE command and checking at regular intervals every N seconds
709 # if LASTSAVE changed.
710 #
711 # Return value: integer reply (UNIX timestamp)
d6b7ea1 bgsave -> lastsave
Ezra Zygmuntowicz authored
712 def lastsave
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
713 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
714 write "LASTSAVE\r\n"
715 integer_reply
716 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
717 end
718
719 def quit
1283f57 timout in 3 second and retry 3 times. make sure LIST marshalling works
Ezra Zygmuntowicz authored
720 timeout_retry(3, 3){
1ce54e4 adding better_timeout and wrapping all redis network calls in timeout…
Ezra Zygmuntowicz authored
721 write "QUIT\r\n"
722 status_code_reply
723 }
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
724 end
b53a8c5 Added support for info command (w/spec). Added redis restart task. …
Taylor Weibley authored
725
726
727 def info
728 info = {}
729
730 x = timeout_retry(3, 3){
90f4edd Implemented flush_db and last_save (w/specs).
Taylor Weibley authored
731 write "INFO\r\n"
b53a8c5 Added support for info command (w/spec). Added redis restart task. …
Taylor Weibley authored
732 read(read_proto.to_i.abs).split("\r\n")
733 }
734
735 x.each do |kv|
736 k,v = kv.split(':')[0], kv.split(':')[1]
737 info[k.to_sym] = v
738 end
739
740 info
741 end
90f4edd Implemented flush_db and last_save (w/specs).
Taylor Weibley authored
742
743 def flush_db
744 timeout_retry(3, 3){
745 write "FLUSHDB\r\n"
746 status_code_reply
747 }
748 end
749
750
751 def last_save
752 timeout_retry(3, 3){
753 write "LASTSAVE\r\n"
754 single_line_reply.to_i
755 }
756 end
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
757
758 private
759
e9034d9 Now with marshal where and when appropriate.
Taylor Weibley authored
760 def redis_unmarshal(obj)
761 if obj[0] == 4
762 Marshal.load(obj)
763 else
764 obj
765 end
766 end
767
768 def redis_marshal(obj)
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
769 case obj
770 when String, Integer
e9034d9 Now with marshal where and when appropriate.
Taylor Weibley authored
771 obj
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
772 else
773 Marshal.dump(obj)
e9034d9 Now with marshal where and when appropriate.
Taylor Weibley authored
774 end
775 end
776
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
777 def close
778 socket.close unless socket.closed?
779 end
780
781 def timeout_retry(time, retries, &block)
782 timeout(time, &block)
783 rescue TimeoutError
784 retries -= 1
785 retry unless retries < 0
786 end
787
788 def socket
1f4070c don lazy reconnect socket
Ezra Zygmuntowicz authored
789 @socket ||= connect
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
790 end
791
792 def connect
793 @socket = TCPSocket.new(@opts[:host], @opts[:port])
794 @socket.sync = true
795 @socket
796 end
797
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
798 def read(length, nodebug=true)
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
799 retries = 3
800 res = socket.read(length)
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
801 puts "read: #{res}" if @opts[:debug] && nodebug
802 res
c247a4a read_proto
Ezra Zygmuntowicz authored
803 rescue
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
804 retries -= 1
805 if retries > 0
806 connect
807 retry
808 end
809 end
810
811 def write(data)
7534d77 implemented SET features, full proto complete and tested now
Ezra Zygmuntowicz authored
812 puts "write: #{data}" if @opts[:debug]
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
813 retries = 3
814 socket.write(data)
c247a4a read_proto
Ezra Zygmuntowicz authored
815 rescue
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
816 retries -= 1
817 if retries > 0
818 connect
819 retry
820 end
821 end
822
823 def nibble_end
824 read(2)
825 end
826
c247a4a read_proto
Ezra Zygmuntowicz authored
827 def read_proto
4d0b90a 10% or more faster overall with micro optz
Ezra Zygmuntowicz authored
828 socket.gets.chomp
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
829 end
830
28111dd bulk_reply doesnt read negative numbers
Ezra Zygmuntowicz authored
831
832 def status_code_reply
833 res = read_proto
834 if res.index(ERRCODE) == 0
835 raise RedisError, res
836 else
837 true
838 end
839 end
840
841 def bulk_reply
842 res = read_proto
843 if res.index(ERRCODE) == 0
f6d1717 optimize to not use nibble_end where possible
Ezra Zygmuntowicz authored
844 err = read(res.to_i.abs+2)
845 raise RedisError, err.chomp
28111dd bulk_reply doesnt read negative numbers
Ezra Zygmuntowicz authored
846 elsif res != NIL
f6d1717 optimize to not use nibble_end where possible
Ezra Zygmuntowicz authored
847 val = read(res.to_i.abs+2)
848 val.chomp
28111dd bulk_reply doesnt read negative numbers
Ezra Zygmuntowicz authored
849 else
850 nil
851 end
852 end
853
854
855 def multi_bulk_reply
856 res = read_proto
98d287d RedisCloud -> DistRedis
Ezra Zygmuntowicz authored
857 if res.index(ERRCODE) == 0
f6d1717 optimize to not use nibble_end where possible
Ezra Zygmuntowicz authored
858 err = read(res.to_i.abs+2)
859 raise RedisError, err.chomp
28111dd bulk_reply doesnt read negative numbers
Ezra Zygmuntowicz authored
860 elsif res == NIL
861 nil
862 else
863 items = Integer(res)
864 list = []
865 items.times do
ea13aaf iplemented SORT also fixed a bug with trying to push integers onto a …
Ezra Zygmuntowicz authored
866 len = Integer(read_proto)
867 if len == -1
868 nil
869 else
870 list << read(len)
871 end
28111dd bulk_reply doesnt read negative numbers
Ezra Zygmuntowicz authored
872 nibble_end
873 end
874 list
875 end
876 end
877
878 def single_line_reply
879 read_proto
880 end
881
882 def integer_reply
883 Integer(read_proto)
884 end
885
2c635cb make a proper gem. implement *all* protocol specs.
Ezra Zygmuntowicz authored
886 end
Something went wrong with that request. Please try again.