valkey-rb 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +58 -0
  3. data/.rubocop_todo.yml +22 -0
  4. data/README.md +95 -0
  5. data/Rakefile +23 -0
  6. data/lib/valkey/bindings.rb +224 -0
  7. data/lib/valkey/commands/bitmap_commands.rb +86 -0
  8. data/lib/valkey/commands/cluster_commands.rb +259 -0
  9. data/lib/valkey/commands/connection_commands.rb +318 -0
  10. data/lib/valkey/commands/function_commands.rb +255 -0
  11. data/lib/valkey/commands/generic_commands.rb +525 -0
  12. data/lib/valkey/commands/geo_commands.rb +87 -0
  13. data/lib/valkey/commands/hash_commands.rb +587 -0
  14. data/lib/valkey/commands/hyper_log_log_commands.rb +51 -0
  15. data/lib/valkey/commands/json_commands.rb +389 -0
  16. data/lib/valkey/commands/list_commands.rb +348 -0
  17. data/lib/valkey/commands/module_commands.rb +125 -0
  18. data/lib/valkey/commands/pubsub_commands.rb +237 -0
  19. data/lib/valkey/commands/scripting_commands.rb +286 -0
  20. data/lib/valkey/commands/server_commands.rb +961 -0
  21. data/lib/valkey/commands/set_commands.rb +220 -0
  22. data/lib/valkey/commands/sorted_set_commands.rb +971 -0
  23. data/lib/valkey/commands/stream_commands.rb +636 -0
  24. data/lib/valkey/commands/string_commands.rb +359 -0
  25. data/lib/valkey/commands/transaction_commands.rb +175 -0
  26. data/lib/valkey/commands/vector_search_commands.rb +271 -0
  27. data/lib/valkey/commands.rb +68 -0
  28. data/lib/valkey/errors.rb +41 -0
  29. data/lib/valkey/libglide_ffi.so +0 -0
  30. data/lib/valkey/opentelemetry.rb +207 -0
  31. data/lib/valkey/pipeline.rb +20 -0
  32. data/lib/valkey/protobuf/command_request_pb.rb +51 -0
  33. data/lib/valkey/protobuf/connection_request_pb.rb +51 -0
  34. data/lib/valkey/protobuf/response_pb.rb +39 -0
  35. data/lib/valkey/pubsub_callback.rb +10 -0
  36. data/lib/valkey/request_error_type.rb +10 -0
  37. data/lib/valkey/request_type.rb +436 -0
  38. data/lib/valkey/response_type.rb +20 -0
  39. data/lib/valkey/utils.rb +253 -0
  40. data/lib/valkey/version.rb +5 -0
  41. data/lib/valkey.rb +551 -0
  42. metadata +119 -0
@@ -0,0 +1,255 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Valkey
4
+ module Commands
5
+ # This module contains commands related to Valkey Functions.
6
+ #
7
+ # @see https://valkey.io/commands/#scripting
8
+ #
9
+ module FunctionCommands
10
+ # Delete a library and all its functions.
11
+ #
12
+ # @example Delete a library
13
+ # valkey.function_delete("mylib")
14
+ # # => "OK"
15
+ #
16
+ # @param [String] library_name the library name to delete
17
+ # @return [String] "OK"
18
+ #
19
+ # @see https://valkey.io/commands/function-delete/
20
+ def function_delete(library_name)
21
+ send_command(RequestType::FUNCTION_DELETE, [library_name])
22
+ end
23
+
24
+ # Return the serialized payload of loaded libraries.
25
+ #
26
+ # @example Dump all libraries
27
+ # valkey.function_dump
28
+ # # => <binary string>
29
+ #
30
+ # @return [String] the serialized payload
31
+ #
32
+ # @see https://valkey.io/commands/function-dump/
33
+ def function_dump
34
+ send_command(RequestType::FUNCTION_DUMP)
35
+ end
36
+
37
+ # Delete all libraries.
38
+ #
39
+ # @example Flush all libraries
40
+ # valkey.function_flush
41
+ # # => "OK"
42
+ # @example Flush all libraries asynchronously
43
+ # valkey.function_flush(async: true)
44
+ # # => "OK"
45
+ # @example Flush all libraries synchronously
46
+ # valkey.function_flush(sync: true)
47
+ # # => "OK"
48
+ #
49
+ # @param [Boolean] async flush asynchronously
50
+ # @param [Boolean] sync flush synchronously
51
+ # @return [String] "OK"
52
+ #
53
+ # @see https://valkey.io/commands/function-flush/
54
+ def function_flush(async: false, sync: false)
55
+ args = []
56
+
57
+ if async
58
+ args << "ASYNC"
59
+ elsif sync
60
+ args << "SYNC"
61
+ end
62
+
63
+ send_command(RequestType::FUNCTION_FLUSH, args)
64
+ end
65
+
66
+ # Kill a function that is currently executing.
67
+ #
68
+ # @example Kill a running function
69
+ # valkey.function_kill
70
+ # # => "OK"
71
+ #
72
+ # @return [String] "OK"
73
+ #
74
+ # @see https://valkey.io/commands/function-kill/
75
+ def function_kill
76
+ send_command(RequestType::FUNCTION_KILL)
77
+ end
78
+
79
+ # Return information about the functions and libraries.
80
+ #
81
+ # @example List all libraries
82
+ # valkey.function_list
83
+ # # => [{"library_name" => "mylib", "engine" => "LUA", ...}]
84
+ # @example List libraries matching a pattern
85
+ # valkey.function_list(library_name: "mylib*")
86
+ # # => [{"library_name" => "mylib", ...}]
87
+ # @example List libraries with code
88
+ # valkey.function_list(with_code: true)
89
+ # # => [{"library_name" => "mylib", "library_code" => "...", ...}]
90
+ #
91
+ # @param [String] library_name filter by library name pattern
92
+ # @param [Boolean] with_code include the library code in the response
93
+ # @return [Array<Hash>] array of library information
94
+ #
95
+ # @see https://valkey.io/commands/function-list/
96
+ def function_list(library_name: nil, with_code: false)
97
+ args = []
98
+
99
+ if library_name
100
+ args << "LIBRARYNAME"
101
+ args << library_name
102
+ end
103
+
104
+ args << "WITHCODE" if with_code
105
+
106
+ send_command(RequestType::FUNCTION_LIST, args)
107
+ end
108
+
109
+ # Load a library to Valkey.
110
+ #
111
+ # @example Load a library
112
+ # code = "#!lua name=mylib\nvalkey.register_function('myfunc', function(keys, args) return args[1] end)"
113
+ # valkey.function_load(code)
114
+ # # => "mylib"
115
+ # @example Load a library, replacing if exists
116
+ # valkey.function_load(code, replace: true)
117
+ # # => "mylib"
118
+ #
119
+ # @param [String] function_code the source code
120
+ # @param [Boolean] replace replace the library if it exists
121
+ # @return [String] the library name that was loaded
122
+ #
123
+ # @see https://valkey.io/commands/function-load/
124
+ def function_load(function_code, replace: false)
125
+ args = []
126
+ args << "REPLACE" if replace
127
+ args << function_code
128
+
129
+ send_command(RequestType::FUNCTION_LOAD, args)
130
+ end
131
+
132
+ # Restore libraries from a payload.
133
+ #
134
+ # @example Restore libraries
135
+ # payload = valkey.function_dump
136
+ # valkey.function_restore(payload)
137
+ # # => "OK"
138
+ # @example Restore libraries with FLUSH policy
139
+ # valkey.function_restore(payload, policy: "FLUSH")
140
+ # # => "OK"
141
+ # @example Restore libraries with APPEND policy
142
+ # valkey.function_restore(payload, policy: "APPEND")
143
+ # # => "OK"
144
+ # @example Restore libraries with REPLACE policy
145
+ # valkey.function_restore(payload, policy: "REPLACE")
146
+ # # => "OK"
147
+ #
148
+ # @param [String] serialized_value the serialized payload from FUNCTION DUMP
149
+ # @param [String] policy the restore policy: "FLUSH", "APPEND", or "REPLACE"
150
+ # @return [String] "OK"
151
+ #
152
+ # @see https://valkey.io/commands/function-restore/
153
+ def function_restore(serialized_value, policy: nil)
154
+ args = [serialized_value]
155
+
156
+ args << policy.to_s.upcase if policy
157
+
158
+ send_command(RequestType::FUNCTION_RESTORE, args)
159
+ end
160
+
161
+ # Return information about the function that's currently running.
162
+ #
163
+ # @example Get function stats
164
+ # valkey.function_stats
165
+ # # => {"running_script" => {...}, "engines" => {...}}
166
+ #
167
+ # @return [Hash] function execution statistics
168
+ #
169
+ # @see https://valkey.io/commands/function-stats/
170
+ def function_stats
171
+ send_command(RequestType::FUNCTION_STATS)
172
+ end
173
+
174
+ # Invoke a function.
175
+ #
176
+ # @example Call a function
177
+ # valkey.fcall("myfunc", keys: ["key1"], args: ["arg1"])
178
+ # # => <function result>
179
+ # @example Call a function without keys
180
+ # valkey.fcall("myfunc", args: ["arg1", "arg2"])
181
+ # # => <function result>
182
+ #
183
+ # @param [String] function the function name
184
+ # @param [Array<String>] keys the keys to pass to the function
185
+ # @param [Array<String>] args the arguments to pass to the function
186
+ # @return [Object] the function result
187
+ #
188
+ # @see https://valkey.io/commands/fcall/
189
+ def fcall(function, keys: [], args: [])
190
+ command_args = [function, keys.size] + keys + args
191
+ send_command(RequestType::FCALL, command_args)
192
+ end
193
+
194
+ # Invoke a read-only function.
195
+ #
196
+ # @example Call a read-only function
197
+ # valkey.fcall_ro("myfunc", keys: ["key1"], args: ["arg1"])
198
+ # # => <function result>
199
+ #
200
+ # @param [String] function the function name
201
+ # @param [Array<String>] keys the keys to pass to the function
202
+ # @param [Array<String>] args the arguments to pass to the function
203
+ # @return [Object] the function result
204
+ #
205
+ # @see https://valkey.io/commands/fcall_ro/
206
+ def fcall_ro(function, keys: [], args: [])
207
+ command_args = [function, keys.size] + keys + args
208
+ send_command(RequestType::FCALL_READ_ONLY, command_args)
209
+ end
210
+
211
+ # Control function registry (convenience method).
212
+ #
213
+ # @example Delete a library
214
+ # valkey.function(:delete, "mylib")
215
+ # # => "OK"
216
+ # @example Dump all libraries
217
+ # valkey.function(:dump)
218
+ # # => <binary string>
219
+ # @example Flush all libraries
220
+ # valkey.function(:flush)
221
+ # # => "OK"
222
+ # @example Kill a running function
223
+ # valkey.function(:kill)
224
+ # # => "OK"
225
+ # @example List all libraries
226
+ # valkey.function(:list)
227
+ # # => [...]
228
+ # @example Load a library
229
+ # valkey.function(:load, code)
230
+ # # => "mylib"
231
+ # @example Restore libraries
232
+ # valkey.function(:restore, payload)
233
+ # # => "OK"
234
+ # @example Get function stats
235
+ # valkey.function(:stats)
236
+ # # => {...}
237
+ #
238
+ # @param [String, Symbol] subcommand the subcommand (delete, dump, flush, kill, list, load, restore, stats)
239
+ # @param [Array] args arguments for the subcommand
240
+ # @param [Hash] options options for the subcommand
241
+ # @return [Object] depends on subcommand
242
+ def function(subcommand, *args, **options)
243
+ subcommand = subcommand.to_s.downcase
244
+
245
+ if args.empty? && options.empty?
246
+ send("function_#{subcommand}")
247
+ elsif options.empty?
248
+ send("function_#{subcommand}", *args)
249
+ else
250
+ send("function_#{subcommand}", *args, **options)
251
+ end
252
+ end
253
+ end
254
+ end
255
+ end