Skip to content

Commit 23f9ab1

Browse files
#10 implement list commands (#26)
* #10 implement list commands Signed-off-by: Mohsen Alizadeh <mohsen@alizadeh.us>
1 parent 74319c7 commit 23f9ab1

6 files changed

Lines changed: 314 additions & 39 deletions

File tree

.rubocop.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
inherit_from: .rubocop_todo.yml
2+
13
AllCops:
24
TargetRubyVersion: 2.6
35
Exclude:
@@ -39,4 +41,3 @@ Metrics/ModuleLength:
3941
- 'lib/valkey/utils.rb'
4042
- 'lib/valkey/commands/*.rb'
4143
- 'test/**/*.rb'
42-

.rubocop_todo.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2025-07-15 13:27:05 UTC using RuboCop version 1.78.0.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 1
10+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
11+
# AllowedMethods: refine
12+
Metrics/BlockLength:
13+
Max: 35
14+
15+
# Offense count: 1
16+
# Configuration parameters: CountComments, CountAsOne.
17+
Metrics/ClassLength:
18+
Max: 111

lib/valkey.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ def send_command(command_type, command_args = [], &block)
8989
item = Bindings::CommandResponse.new(ptr + i * Bindings::CommandResponse.size)
9090
convert_response.call(item)
9191
end
92+
when ResponseType::MAP
93+
key = if result[:map_key].null?
94+
nil
95+
else
96+
convert_response.call(result[:map_key])
97+
end
98+
99+
value = if result[:map_value].null?
100+
nil
101+
else
102+
convert_response.call(result[:map_value])
103+
end
104+
105+
[key, value]
92106
when ResponseType::NULL
93107
nil
94108
when ResponseType::OK

lib/valkey/commands/list_commands.rb

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module ListCommands
1212
# @param [String] key
1313
# @return [Integer]
1414
def llen(key)
15-
send_command([:llen, key])
15+
send_command(RequestType::LLEN, [key])
1616
end
1717

1818
# Remove the first/last element in a list, append/prepend it to another list and return it.
@@ -31,18 +31,18 @@ def llen(key)
3131
def lmove(source, destination, where_source, where_destination)
3232
where_source, where_destination = _normalize_move_wheres(where_source, where_destination)
3333

34-
send_command([:lmove, source, destination, where_source, where_destination])
34+
send_command(RequestType::LMOVE, [source, destination, where_source, where_destination])
3535
end
3636

3737
# Remove the first/last element in a list and append/prepend it
3838
# to another list and return it, or block until one is available.
3939
#
4040
# @example With timeout
41-
# element = redis.blmove("foo", "bar", "LEFT", "RIGHT", timeout: 5)
41+
# element = valkey.blmove("foo", "bar", "LEFT", "RIGHT", timeout: 5)
4242
# # => nil on timeout
4343
# # => "element" on success
4444
# @example Without timeout
45-
# element = redis.blmove("foo", "bar", "LEFT", "RIGHT")
45+
# element = valkey.blmove("foo", "bar", "LEFT", "RIGHT")
4646
# # => "element"
4747
#
4848
# @param [String] source source key
@@ -59,8 +59,8 @@ def lmove(source, destination, where_source, where_destination)
5959
def blmove(source, destination, where_source, where_destination, timeout: 0)
6060
where_source, where_destination = _normalize_move_wheres(where_source, where_destination)
6161

62-
command = [:blmove, source, destination, where_source, where_destination, timeout]
63-
send_blocking_command(command, timeout)
62+
args = [:blmove, source, destination, where_source, where_destination, timeout]
63+
send_command(RequestType::BLMOVE, args)
6464
end
6565

6666
# Prepend one or more values to a list, creating the list if it doesn't exist
@@ -69,7 +69,7 @@ def blmove(source, destination, where_source, where_destination, timeout: 0)
6969
# @param [String, Array<String>] value string value, or array of string values to push
7070
# @return [Integer] the length of the list after the push operation
7171
def lpush(key, value)
72-
send_command(RequestType::LPUSH, [key, value])
72+
send_command(RequestType::LPUSH, [key, *value])
7373
end
7474

7575
# Prepend a value to a list, only if the list exists.
@@ -78,7 +78,7 @@ def lpush(key, value)
7878
# @param [String] value
7979
# @return [Integer] the length of the list after the push operation
8080
def lpushx(key, value)
81-
send_command([:lpushx, key, value])
81+
send_command(RequestType::LPUSHX, [key, value])
8282
end
8383

8484
# Append one or more values to a list, creating the list if it doesn't exist
@@ -100,7 +100,7 @@ def rpush(key, value)
100100
# @param [String] value
101101
# @return [Integer] the length of the list after the push operation
102102
def rpushx(key, value)
103-
send_command([:rpushx, key, value])
103+
send_command(RequestType::RPUSHX, [key, value])
104104
end
105105

106106
# Remove and get the first elements in a list.
@@ -109,9 +109,9 @@ def rpushx(key, value)
109109
# @param [Integer] count number of elements to remove
110110
# @return [nil, String, Array<String>] the values of the first elements
111111
def lpop(key, count = nil)
112-
command = [:lpop, key]
113-
command << Integer(count) if count
114-
send_command(command)
112+
args = [key]
113+
args << Integer(count) if count
114+
send_command(RequestType::LPOP, args)
115115
end
116116

117117
# Remove and get the last elements in a list.
@@ -120,9 +120,9 @@ def lpop(key, count = nil)
120120
# @param [Integer] count number of elements to remove
121121
# @return [nil, String, Array<String>] the values of the last elements
122122
def rpop(key, count = nil)
123-
command = [:rpop, key]
124-
command << Integer(count) if count
125-
send_command(command)
123+
args = [key]
124+
args << Integer(count) if count
125+
send_command(RequestType::RPOP, args)
126126
end
127127

128128
# Remove the last element in a list, append it to another list and return it.
@@ -131,20 +131,20 @@ def rpop(key, count = nil)
131131
# @param [String] destination destination key
132132
# @return [nil, String] the element, or nil when the source key does not exist
133133
def rpoplpush(source, destination)
134-
send_command([:rpoplpush, source, destination])
134+
send_command(RequestType::RPOPLPUSH, [source, destination])
135135
end
136136

137137
# Remove and get the first element in a list, or block until one is available.
138138
#
139139
# @example With timeout
140-
# list, element = redis.blpop("list", :timeout => 5)
140+
# list, element = valkey.blpop("list", :timeout => 5)
141141
# # => nil on timeout
142142
# # => ["list", "element"] on success
143143
# @example Without timeout
144-
# list, element = redis.blpop("list")
144+
# list, element = valkey.blpop("list")
145145
# # => ["list", "element"]
146146
# @example Blocking pop on multiple lists
147-
# list, element = redis.blpop(["list", "another_list"])
147+
# list, element = valkey.blpop(["list", "another_list"])
148148
# # => ["list", "element"]
149149
#
150150
# @param [String, Array<String>] keys one or more keys to perform the
@@ -172,7 +172,7 @@ def blpop(*args)
172172
#
173173
# @see #blpop
174174
def brpop(*args)
175-
_bpop(:brpop, args)
175+
_bpop(RequestType::BRPOP, args.flatten)
176176
end
177177

178178
# Pop a value from a list, push it to another list and return it; or block
@@ -187,18 +187,18 @@ def brpop(*args)
187187
# - `nil` when the operation timed out
188188
# - the element was popped and pushed otherwise
189189
def brpoplpush(source, destination, timeout: 0)
190-
command = [:brpoplpush, source, destination, timeout]
191-
send_blocking_command(command, timeout)
190+
args = [:brpoplpush, source, destination, timeout]
191+
send_blocking_command(RequestType::BRPOPLPUSH, args, timeout)
192192
end
193193

194194
# Pops one or more elements from the first non-empty list key from the list
195195
# of provided key names. If lists are empty, blocks until timeout has passed.
196196
#
197197
# @example Popping a element
198-
# redis.blmpop(1.0, 'list')
198+
# valkey.blmpop(1.0, 'list')
199199
# #=> ['list', ['a']]
200200
# @example With count option
201-
# redis.blmpop(1.0, 'list', count: 2)
201+
# valkey.blmpop(1.0, 'list', count: 2)
202202
# #=> ['list', ['a', 'b']]
203203
#
204204
# @params timeout [Float] a float value specifying the maximum number of seconds to block) elapses.
@@ -213,20 +213,20 @@ def brpoplpush(source, destination, timeout: 0)
213213
def blmpop(timeout, *keys, modifier: "LEFT", count: nil)
214214
raise ArgumentError, "Pick either LEFT or RIGHT" unless %w[LEFT RIGHT].include?(modifier)
215215

216-
args = [:blmpop, timeout, keys.size, *keys, modifier]
216+
args = [timeout, keys.size, *keys, modifier]
217217
args << "COUNT" << Integer(count) if count
218218

219-
send_blocking_command(args, timeout)
219+
send_command(RequestType::BLMPOP, args)
220220
end
221221

222222
# Pops one or more elements from the first non-empty list key from the list
223223
# of provided key names.
224224
#
225225
# @example Popping a element
226-
# redis.lmpop('list')
226+
# valkey.lmpop('list')
227227
# #=> ['list', ['a']]
228228
# @example With count option
229-
# redis.lmpop('list', count: 2)
229+
# valkey.lmpop('list', count: 2)
230230
# #=> ['list', ['a', 'b']]
231231
#
232232
# @params key [String, Array<String>] one or more keys with lists
@@ -239,10 +239,12 @@ def blmpop(timeout, *keys, modifier: "LEFT", count: nil)
239239
def lmpop(*keys, modifier: "LEFT", count: nil)
240240
raise ArgumentError, "Pick either LEFT or RIGHT" unless %w[LEFT RIGHT].include?(modifier)
241241

242-
args = [:lmpop, keys.size, *keys, modifier]
242+
args = [keys.size, *keys, modifier]
243243
args << "COUNT" << Integer(count) if count
244244

245-
send_command(args)
245+
# pp args
246+
247+
send_command(RequestType::LMPOP, args)
246248
end
247249

248250
# Get an element from a list by its index.
@@ -251,7 +253,7 @@ def lmpop(*keys, modifier: "LEFT", count: nil)
251253
# @param [Integer] index
252254
# @return [String]
253255
def lindex(key, index)
254-
send_command([:lindex, key, Integer(index)])
256+
send_command(RequestType::LINDEX, [key, Integer(index)])
255257
end
256258

257259
# Insert an element before or after another element in a list.
@@ -263,7 +265,7 @@ def lindex(key, index)
263265
# @return [Integer] length of the list after the insert operation, or `-1`
264266
# when the element `pivot` was not found
265267
def linsert(key, where, pivot, value)
266-
send_command([:linsert, key, where, pivot, value])
268+
send_command(RequestType::LINSERT, [key, where, pivot, value])
267269
end
268270

269271
# Get a range of elements from a list.
@@ -286,7 +288,7 @@ def lrange(key, start, stop)
286288
# @param [String] value
287289
# @return [Integer] the number of removed elements
288290
def lrem(key, count, value)
289-
send_command([:lrem, key, Integer(count), value])
291+
send_command(RequestType::LREM, [key, Integer(count), value])
290292
end
291293

292294
# Set the value of an element in a list by its index.
@@ -296,7 +298,7 @@ def lrem(key, count, value)
296298
# @param [String] value
297299
# @return [String] `OK`
298300
def lset(key, index, value)
299-
send_command([:lset, key, Integer(index), value])
301+
send_command(RequestType::LSET, [key, Integer(index), value])
300302
end
301303

302304
# Trim a list to the specified range.
@@ -306,7 +308,7 @@ def lset(key, index, value)
306308
# @param [Integer] stop stop index
307309
# @return [String] `OK`
308310
def ltrim(key, start, stop)
309-
send_command([:ltrim, key, Integer(start), Integer(stop)])
311+
send_command(RequestType::LTRIM, [key, Integer(start), Integer(stop)])
310312
end
311313

312314
private
@@ -323,9 +325,8 @@ def _bpop(cmd, args, &blk)
323325
end
324326

325327
args.flatten!(1)
326-
command = [cmd].concat(args)
327-
command << timeout
328-
send_blocking_command(command, timeout, &blk)
328+
args << timeout
329+
send_blocking_command(cmd, args, &blk)
329330
end
330331

331332
def _normalize_move_wheres(where_source, where_destination)

0 commit comments

Comments
 (0)