Module: Redis::Lists

Defined in:
lib/redis/lists.rb

Defined Under Namespace

Classes: DeferredPop

Instance Method Summary collapse

Instance Method Details

#redis_BLPOP(*args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/redis/lists.rb', line 63

def redis_BLPOP *args
  timeout = redis_pos_i args.pop
  args.each do |key|
    list = @database[key]
    if list and list.size > 0
      value = list.shift
      @database.delete key if list.empty?
      return [key, value]
    end
  end
  df = DeferredPop.new(@database, timeout, *args)
  df.errback { send_redis :'*-1' }
  df.callback { |key, value| send_redis [key, value] }
  df
end

#redis_BRPOP(*args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/redis/lists.rb', line 47

def redis_BRPOP *args
  timeout = redis_pos_i args.pop
  args.each do |key|
    list = @database[key]
    if list and list.size > 0
      value = list.pop
      @database.delete key if list.empty?
      return [key, value]
    end
  end
  df = DeferredPop.new(@database, timeout, *args)
  df.errback { send_redis :'*-1' }
  df.callback { |key, value| send_redis [key, value] }
  df
end

#redis_BRPOPLPUSH(source, destination, timeout) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/redis/lists.rb', line 90

def redis_BRPOPLPUSH source, destination, timeout
  source_list = @database[source]
  if source_list
    redis_t Array, source_list
    value = source_list.pop
    @database.delete source if source_list.empty?
    redis_LPUSH destination, value
    return value
  end
  redis_t NilClass, Array, @database[destination]
  df = DeferredPop.new @database, redis_pos_i(timeout), source
  df.errback {send_redis :'*-1'}
  df.callback do |key, value|
    redis_LPUSH destination, value
    send_redis value
  end
  df
end

#redis_LINDEX(key, index) ⇒ Object



182
183
184
185
186
# File 'lib/redis/lists.rb', line 182

def redis_LINDEX key, index
  list = @database[key] || []
  redis_t Array, list
  list[redis_i index]
end

#redis_LINSERT(key, mode, pivot, value) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/redis/lists.rb', line 145

def redis_LINSERT key, mode, pivot, value
  list = @database[key]
  index = list.find_index pivot
  return -1 unless index
  case mode.upcase
  when 'BEFORE'
    list[index,0] = value
  when 'AFTER'
    list[index+1,0] = value
  else
    raise 'only BEFORE|AFTER supported'
  end
  list.size
end

#redis_LLEN(key) ⇒ Object



176
177
178
179
180
# File 'lib/redis/lists.rb', line 176

def redis_LLEN key
  list = @database[key] || []
  redis_t Array, list
  list.size
end

#redis_LPOP(key) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/redis/lists.rb', line 168

def redis_LPOP key
  list = @database[key]
  return nil unless list
  value = list.shift
  @database.delete key if list.empty?
  value
end

#redis_LPUSH(key, value) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/redis/lists.rb', line 120

def redis_LPUSH key, value
  list = @database[key]
  redis_t NilClass, Array, list
  (@database.blocked_pops[key] ||= []).each do |deferrable|
    deferrable.succeed key, value
    return 0
  end
  list = @database[key] = [] unless list
  list.unshift(value).size
end

#redis_LPUSHX(key, value) ⇒ Object



131
132
133
134
135
136
# File 'lib/redis/lists.rb', line 131

def redis_LPUSHX key, value
  list = @database[key]
  return 0 unless Array === list and list.size > 0
  redis_LPUSH key, value
  list.size
end

#redis_LRANGE(key, first, last) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/redis/lists.rb', line 35

def redis_LRANGE key, first, last
  first = redis_i first
  last = redis_i last
  list = @database[key] || []
  first = 0 if first < -list.size
  list[first..last]
end

#redis_LREM(key, count, value) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/redis/lists.rb', line 195

def redis_LREM key, count, value
  list = @database[key] || []
  count = redis_i count
  size = list.size
  if count == 0
    list.delete value
  elsif count < 0
    i = list.size
    while i > 0 and count < 0
      i -= 1
      if list[i] == value
        list.delete_at i
        count += 1
      end
    end
  else # count > 0
    i = 0
    while i < list.size and count > 0
      if list[i] == value
        list.delete_at i 
        count -= 1
      else
        i += 1
      end
    end
  end
  size - list.size
end

#redis_LSET(key, index, value) ⇒ Object



188
189
190
191
192
193
# File 'lib/redis/lists.rb', line 188

def redis_LSET key, index, value
  list = @database[key] || []
  redis_t Array, list
  raise 'out of range' unless list.size > redis_i(index).abs
  list[redis_i index] = value
end

#redis_LTRIM(key, start, stop) ⇒ Object



43
44
45
# File 'lib/redis/lists.rb', line 43

def redis_LTRIM key, start, stop
  @database[key] = redis_LRANGE key, start, stop
end

#redis_RPOP(key) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/redis/lists.rb', line 160

def redis_RPOP key
  list = @database[key]
  return nil unless list
  value = list.pop
  @database.delete key if list.empty?
  value
end

#redis_RPOPLPUSH(source, destination) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/redis/lists.rb', line 79

def redis_RPOPLPUSH source, destination
  source_list = @database[source]
  return nil unless source_list
  redis_t Array, source_list
  redis_t NilClass, Array, @database[destination]
  value = source_list.pop
  @database.delete source if source_list.empty?
  redis_LPUSH destination, value
  return value
end

#redis_RPUSH(key, value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/redis/lists.rb', line 109

def redis_RPUSH key, value
  list = @database[key]
  redis_t NilClass, Array, list
  (@database.blocked_pops[key] ||= []).each do |deferrable|
    deferrable.succeed key, value
    return 0
  end
  list = @database[key] = [] unless list
  list.push(value).size
end

#redis_RPUSHX(key, value) ⇒ Object



138
139
140
141
142
143
# File 'lib/redis/lists.rb', line 138

def redis_RPUSHX key, value
  list = @database[key]
  return 0 unless Array === list and list.size > 0
  redis_RPUSH key, value
  list.size
end