ruby-redis 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +9 -0
- data/README +90 -0
- data/bin/ruby-redis +12 -0
- data/bin/ruby-redis.compiled.rbc +243 -0
- data/lib/redis/bin.rb +74 -0
- data/lib/redis/bin.rbc +1325 -0
- data/lib/redis/buftok.rbc +2658 -0
- data/lib/redis/config.rb +46 -0
- data/lib/redis/config.rbc +1009 -0
- data/lib/redis/connection.rb +69 -0
- data/lib/redis/connection.rbc +1354 -0
- data/lib/redis/database.rb +109 -0
- data/lib/redis/database.rbc +2275 -0
- data/lib/redis/hashes.rb +72 -0
- data/lib/redis/hashes.rbc +1843 -0
- data/lib/redis/hiredis.rbc +658 -0
- data/lib/redis/keys.rb +165 -0
- data/lib/redis/keys.rbc +3386 -0
- data/lib/redis/lists.rb +227 -0
- data/lib/redis/lists.rbc +5241 -0
- data/lib/redis/logger.rb +81 -0
- data/lib/redis/logger.rbc +2106 -0
- data/lib/redis/protocol.rb +170 -0
- data/lib/redis/protocol.rbc +3735 -0
- data/lib/redis/pubsub.rb +153 -0
- data/lib/redis/pubsub.rbc +3447 -0
- data/lib/redis/reader.rb +164 -0
- data/lib/redis/reader.rbc +2769 -0
- data/lib/redis/send.rbc +1268 -0
- data/lib/redis/sender.rb +49 -0
- data/lib/redis/sender.rbc +1057 -0
- data/lib/redis/server.rb +62 -0
- data/lib/redis/server.rbc +1177 -0
- data/lib/redis/sets.rb +105 -0
- data/lib/redis/sets.rbc +2800 -0
- data/lib/redis/strict.rb +67 -0
- data/lib/redis/strict.rbc +1419 -0
- data/lib/redis/strings.rb +144 -0
- data/lib/redis/strings.rbc +3338 -0
- data/lib/redis/synchrony.rb +58 -0
- data/lib/redis/synchrony.rbc +1397 -0
- data/lib/redis/version.rb +7 -0
- data/lib/redis/version.rbc +180 -0
- data/lib/redis/zsets.rb +281 -0
- data/lib/redis/zsets.rbc +6596 -0
- data/lib/redis.rb +215 -0
- data/lib/redis.rbc +4391 -0
- metadata +117 -0
data/lib/redis/lists.rb
ADDED
@@ -0,0 +1,227 @@
|
|
1
|
+
require File.expand_path '../redis', File.dirname(__FILE__)
|
2
|
+
require 'eventmachine'
|
3
|
+
|
4
|
+
class Redis
|
5
|
+
|
6
|
+
module Lists
|
7
|
+
|
8
|
+
class DeferredPop
|
9
|
+
include EventMachine::Deferrable
|
10
|
+
|
11
|
+
attr_reader :bound
|
12
|
+
|
13
|
+
def initialize database, timeout_secs, *keys
|
14
|
+
@database = database
|
15
|
+
@keys = keys
|
16
|
+
timeout timeout_secs if timeout_secs > 0
|
17
|
+
errback { unbind }
|
18
|
+
callback { unbind }
|
19
|
+
keys.each do |key|
|
20
|
+
(@database.blocked_pops[key] ||= []).push self
|
21
|
+
end
|
22
|
+
@bound = true
|
23
|
+
end
|
24
|
+
|
25
|
+
def unbind
|
26
|
+
return unless @bound
|
27
|
+
@keys.each do |key|
|
28
|
+
key_df_list = @database.blocked_pops[key]
|
29
|
+
next unless key_df_list
|
30
|
+
key_df_list.delete_if { |e| e == self }
|
31
|
+
end
|
32
|
+
@bound = false
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def redis_LRANGE key, first, last
|
38
|
+
first = redis_i first
|
39
|
+
last = redis_i last
|
40
|
+
list = @database[key] || []
|
41
|
+
first = 0 if first < -list.size
|
42
|
+
list[first..last]
|
43
|
+
end
|
44
|
+
|
45
|
+
def redis_LTRIM key, start, stop
|
46
|
+
@database[key] = redis_LRANGE key, start, stop
|
47
|
+
end
|
48
|
+
|
49
|
+
def redis_BRPOP *args
|
50
|
+
timeout = redis_pos_i args.pop
|
51
|
+
args.each do |key|
|
52
|
+
list = @database[key]
|
53
|
+
if list and list.size > 0
|
54
|
+
value = list.pop
|
55
|
+
@database.delete key if list.empty?
|
56
|
+
return [key, value]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
df = DeferredPop.new(@database, timeout, *args)
|
60
|
+
df.errback { send_redis Response::NIL_MB }
|
61
|
+
df.callback { |key, value| send_redis [key, value] }
|
62
|
+
df
|
63
|
+
end
|
64
|
+
|
65
|
+
def redis_BLPOP *args
|
66
|
+
timeout = redis_pos_i args.pop
|
67
|
+
args.each do |key|
|
68
|
+
list = @database[key]
|
69
|
+
if list and list.size > 0
|
70
|
+
value = list.shift
|
71
|
+
@database.delete key if list.empty?
|
72
|
+
return [key, value]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
df = DeferredPop.new(@database, timeout, *args)
|
76
|
+
df.errback { send_redis Response::NIL_MB }
|
77
|
+
df.callback { |key, value| send_redis [key, value] }
|
78
|
+
df
|
79
|
+
end
|
80
|
+
|
81
|
+
def redis_RPOPLPUSH source, destination
|
82
|
+
source_list = @database[source]
|
83
|
+
return nil unless source_list
|
84
|
+
redis_t Array, source_list
|
85
|
+
redis_t NilClass, Array, @database[destination]
|
86
|
+
value = source_list.pop
|
87
|
+
@database.delete source if source_list.empty?
|
88
|
+
redis_LPUSH destination, value
|
89
|
+
return value
|
90
|
+
end
|
91
|
+
|
92
|
+
def redis_BRPOPLPUSH source, destination, timeout
|
93
|
+
source_list = @database[source]
|
94
|
+
if source_list
|
95
|
+
redis_t Array, source_list
|
96
|
+
value = source_list.pop
|
97
|
+
@database.delete source if source_list.empty?
|
98
|
+
redis_LPUSH destination, value
|
99
|
+
return value
|
100
|
+
end
|
101
|
+
redis_t NilClass, Array, @database[destination]
|
102
|
+
df = DeferredPop.new @database, redis_pos_i(timeout), source
|
103
|
+
df.errback {send_redis Response::NIL_MB}
|
104
|
+
df.callback do |key, value|
|
105
|
+
redis_LPUSH destination, value
|
106
|
+
send_redis value
|
107
|
+
end
|
108
|
+
df
|
109
|
+
end
|
110
|
+
|
111
|
+
def redis_RPUSH key, value
|
112
|
+
list = @database[key]
|
113
|
+
redis_t NilClass, Array, list
|
114
|
+
(@database.blocked_pops[key] ||= []).each do |deferrable|
|
115
|
+
deferrable.succeed key, value
|
116
|
+
return 0
|
117
|
+
end
|
118
|
+
list = @database[key] = [] unless list
|
119
|
+
list.push(value).size
|
120
|
+
end
|
121
|
+
|
122
|
+
def redis_LPUSH key, value
|
123
|
+
list = @database[key]
|
124
|
+
redis_t NilClass, Array, list
|
125
|
+
(@database.blocked_pops[key] ||= []).each do |deferrable|
|
126
|
+
deferrable.succeed key, value
|
127
|
+
return 0
|
128
|
+
end
|
129
|
+
list = @database[key] = [] unless list
|
130
|
+
list.unshift(value).size
|
131
|
+
end
|
132
|
+
|
133
|
+
def redis_LPUSHX key, value
|
134
|
+
list = @database[key]
|
135
|
+
return 0 unless Array === list and list.size > 0
|
136
|
+
redis_LPUSH key, value
|
137
|
+
list.size
|
138
|
+
end
|
139
|
+
|
140
|
+
def redis_RPUSHX key, value
|
141
|
+
list = @database[key]
|
142
|
+
return 0 unless Array === list and list.size > 0
|
143
|
+
redis_RPUSH key, value
|
144
|
+
list.size
|
145
|
+
end
|
146
|
+
|
147
|
+
def redis_LINSERT key, mode, pivot, value
|
148
|
+
list = @database[key]
|
149
|
+
index = list.find_index pivot
|
150
|
+
return -1 unless index
|
151
|
+
case mode.upcase
|
152
|
+
when 'BEFORE'
|
153
|
+
list[index,0] = value
|
154
|
+
when 'AFTER'
|
155
|
+
list[index+1,0] = value
|
156
|
+
else
|
157
|
+
raise 'only BEFORE|AFTER supported'
|
158
|
+
end
|
159
|
+
list.size
|
160
|
+
end
|
161
|
+
|
162
|
+
def redis_RPOP key
|
163
|
+
list = @database[key]
|
164
|
+
return nil unless list
|
165
|
+
value = list.pop
|
166
|
+
@database.delete key if list.empty?
|
167
|
+
value
|
168
|
+
end
|
169
|
+
|
170
|
+
def redis_LPOP key
|
171
|
+
list = @database[key]
|
172
|
+
return nil unless list
|
173
|
+
value = list.shift
|
174
|
+
@database.delete key if list.empty?
|
175
|
+
value
|
176
|
+
end
|
177
|
+
|
178
|
+
def redis_LLEN key
|
179
|
+
list = @database[key] || []
|
180
|
+
redis_t Array, list
|
181
|
+
list.size
|
182
|
+
end
|
183
|
+
|
184
|
+
def redis_LINDEX key, index
|
185
|
+
list = @database[key] || []
|
186
|
+
redis_t Array, list
|
187
|
+
list[redis_i index]
|
188
|
+
end
|
189
|
+
|
190
|
+
def redis_LSET key, index, value
|
191
|
+
list = @database[key] || []
|
192
|
+
redis_t Array, list
|
193
|
+
raise 'out of range' unless list.size > redis_i(index).abs
|
194
|
+
list[redis_i index] = value
|
195
|
+
end
|
196
|
+
|
197
|
+
def redis_LREM key, count, value
|
198
|
+
list = @database[key] || []
|
199
|
+
count = redis_i count
|
200
|
+
size = list.size
|
201
|
+
if count == 0
|
202
|
+
list.delete value
|
203
|
+
elsif count < 0
|
204
|
+
i = list.size
|
205
|
+
while i > 0 and count < 0
|
206
|
+
i -= 1
|
207
|
+
if list[i] == value
|
208
|
+
list.delete_at i
|
209
|
+
count += 1
|
210
|
+
end
|
211
|
+
end
|
212
|
+
else # count > 0
|
213
|
+
i = 0
|
214
|
+
while i < list.size and count > 0
|
215
|
+
if list[i] == value
|
216
|
+
list.delete_at i
|
217
|
+
count -= 1
|
218
|
+
else
|
219
|
+
i += 1
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
size - list.size
|
224
|
+
end
|
225
|
+
|
226
|
+
end
|
227
|
+
end
|