ultra-smart-kit 0.0.1

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.
@@ -0,0 +1,188 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Redis
4
+ module Commands
5
+ module Server
6
+ # Asynchronously rewrite the append-only file.
7
+ #
8
+ # @return [String] `OK`
9
+ def bgrewriteaof
10
+ send_command([:bgrewriteaof])
11
+ end
12
+
13
+ # Asynchronously save the dataset to disk.
14
+ #
15
+ # @return [String] `OK`
16
+ def bgsave
17
+ send_command([:bgsave])
18
+ end
19
+
20
+ # Get or set server configuration parameters.
21
+ #
22
+ # @param [Symbol] action e.g. `:get`, `:set`, `:resetstat`
23
+ # @return [String, Hash] string reply, or hash when retrieving more than one
24
+ # property with `CONFIG GET`
25
+ def config(action, *args)
26
+ send_command([:config, action] + args) do |reply|
27
+ if reply.is_a?(Array) && action == :get
28
+ Hashify.call(reply)
29
+ else
30
+ reply
31
+ end
32
+ end
33
+ end
34
+
35
+ # Manage client connections.
36
+ #
37
+ # @param [String, Symbol] subcommand e.g. `kill`, `list`, `getname`, `setname`
38
+ # @return [String, Hash] depends on subcommand
39
+ def client(subcommand, *args)
40
+ send_command([:client, subcommand] + args) do |reply|
41
+ if subcommand.to_s == "list"
42
+ reply.lines.map do |line|
43
+ entries = line.chomp.split(/[ =]/)
44
+ Hash[entries.each_slice(2).to_a]
45
+ end
46
+ else
47
+ reply
48
+ end
49
+ end
50
+ end
51
+
52
+ # Return the number of keys in the selected database.
53
+ #
54
+ # @return [Integer]
55
+ def dbsize
56
+ send_command([:dbsize])
57
+ end
58
+
59
+ # Remove all keys from all databases.
60
+ #
61
+ # @param [Hash] options
62
+ # - `:async => Boolean`: async flush (default: false)
63
+ # @return [String] `OK`
64
+ def flushall(options = nil)
65
+ if options && options[:async]
66
+ send_command(%i[flushall async])
67
+ else
68
+ send_command([:flushall])
69
+ end
70
+ end
71
+
72
+ # Remove all keys from the current database.
73
+ #
74
+ # @param [Hash] options
75
+ # - `:async => Boolean`: async flush (default: false)
76
+ # @return [String] `OK`
77
+ def flushdb(options = nil)
78
+ if options && options[:async]
79
+ send_command(%i[flushdb async])
80
+ else
81
+ send_command([:flushdb])
82
+ end
83
+ end
84
+
85
+ # Get information and statistics about the server.
86
+ #
87
+ # @param [String, Symbol] cmd e.g. "commandstats"
88
+ # @return [Hash<String, String>]
89
+ def info(cmd = nil)
90
+ send_command([:info, cmd].compact) do |reply|
91
+ if reply.is_a?(String)
92
+ reply = HashifyInfo.call(reply)
93
+
94
+ if cmd && cmd.to_s == "commandstats"
95
+ # Extract nested hashes for INFO COMMANDSTATS
96
+ reply = Hash[reply.map do |k, v|
97
+ v = v.split(",").map { |e| e.split("=") }
98
+ [k[/^cmdstat_(.*)$/, 1], Hash[v]]
99
+ end]
100
+ end
101
+ end
102
+
103
+ reply
104
+ end
105
+ end
106
+
107
+ # Get the UNIX time stamp of the last successful save to disk.
108
+ #
109
+ # @return [Integer]
110
+ def lastsave
111
+ send_command([:lastsave])
112
+ end
113
+
114
+ # Listen for all requests received by the server in real time.
115
+ #
116
+ # There is no way to interrupt this command.
117
+ #
118
+ # @yield a block to be called for every line of output
119
+ # @yieldparam [String] line timestamp and command that was executed
120
+ def monitor
121
+ synchronize do |client|
122
+ client = client.pubsub
123
+ client.call_v([:monitor])
124
+ loop do
125
+ yield client.next_event
126
+ end
127
+ end
128
+ end
129
+
130
+ # Synchronously save the dataset to disk.
131
+ #
132
+ # @return [String]
133
+ def save
134
+ send_command([:save])
135
+ end
136
+
137
+ # Synchronously save the dataset to disk and then shut down the server.
138
+ def shutdown
139
+ synchronize do |client|
140
+ client.disable_reconnection do
141
+ client.call_v([:shutdown])
142
+ rescue ConnectionError
143
+ # This means Redis has probably exited.
144
+ nil
145
+ end
146
+ end
147
+ end
148
+
149
+ # Make the server a slave of another instance, or promote it as master.
150
+ def slaveof(host, port)
151
+ send_command([:slaveof, host, port])
152
+ end
153
+
154
+ # Interact with the slowlog (get, len, reset)
155
+ #
156
+ # @param [String] subcommand e.g. `get`, `len`, `reset`
157
+ # @param [Integer] length maximum number of entries to return
158
+ # @return [Array<String>, Integer, String] depends on subcommand
159
+ def slowlog(subcommand, length = nil)
160
+ args = [:slowlog, subcommand]
161
+ args << Integer(length) if length
162
+ send_command(args)
163
+ end
164
+
165
+ # Internal command used for replication.
166
+ def sync
167
+ send_command([:sync])
168
+ end
169
+
170
+ # Return the server time.
171
+ #
172
+ # @example
173
+ # r.time # => [ 1333093196, 606806 ]
174
+ #
175
+ # @return [Array<Integer>] tuple of seconds since UNIX epoch and
176
+ # microseconds in the current second
177
+ def time
178
+ send_command([:time]) do |reply|
179
+ reply&.map(&:to_i)
180
+ end
181
+ end
182
+
183
+ def debug(*args)
184
+ send_command([:debug] + args)
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,218 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Redis
4
+ module Commands
5
+ module Sets
6
+ # Get the number of members in a set.
7
+ #
8
+ # @param [String] key
9
+ # @return [Integer]
10
+ def scard(key)
11
+ send_command([:scard, key])
12
+ end
13
+
14
+ # Add one or more members to a set.
15
+ #
16
+ # @param [String] key
17
+ # @param [String, Array<String>] member one member, or array of members
18
+ # @return [Integer] The number of members that were successfully added
19
+ def sadd(key, *members)
20
+ members.flatten!(1)
21
+ send_command([:sadd, key].concat(members))
22
+ end
23
+
24
+ # Add one or more members to a set.
25
+ #
26
+ # @param [String] key
27
+ # @param [String, Array<String>] member one member, or array of members
28
+ # @return [Boolean] Wether at least one member was successfully added.
29
+ def sadd?(key, *members)
30
+ members.flatten!(1)
31
+ send_command([:sadd, key].concat(members), &Boolify)
32
+ end
33
+
34
+ # Remove one or more members from a set.
35
+ #
36
+ # @param [String] key
37
+ # @param [String, Array<String>] member one member, or array of members
38
+ # @return [Integer] The number of members that were successfully removed
39
+ def srem(key, *members)
40
+ members.flatten!(1)
41
+ send_command([:srem, key].concat(members))
42
+ end
43
+
44
+ # Remove one or more members from a set.
45
+ #
46
+ # @param [String] key
47
+ # @param [String, Array<String>] member one member, or array of members
48
+ # @return [Boolean] Wether at least one member was successfully removed.
49
+ def srem?(key, *members)
50
+ members.flatten!(1)
51
+ send_command([:srem, key].concat(members), &Boolify)
52
+ end
53
+
54
+ # Remove and return one or more random member from a set.
55
+ #
56
+ # @param [String] key
57
+ # @return [String]
58
+ # @param [Integer] count
59
+ def spop(key, count = nil)
60
+ if count.nil?
61
+ send_command([:spop, key])
62
+ else
63
+ send_command([:spop, key, Integer(count)])
64
+ end
65
+ end
66
+
67
+ # Get one or more random members from a set.
68
+ #
69
+ # @param [String] key
70
+ # @param [Integer] count
71
+ # @return [String]
72
+ def srandmember(key, count = nil)
73
+ if count.nil?
74
+ send_command([:srandmember, key])
75
+ else
76
+ send_command([:srandmember, key, count])
77
+ end
78
+ end
79
+
80
+ # Move a member from one set to another.
81
+ #
82
+ # @param [String] source source key
83
+ # @param [String] destination destination key
84
+ # @param [String] member member to move from `source` to `destination`
85
+ # @return [Boolean]
86
+ def smove(source, destination, member)
87
+ send_command([:smove, source, destination, member], &Boolify)
88
+ end
89
+
90
+ # Determine if a given value is a member of a set.
91
+ #
92
+ # @param [String] key
93
+ # @param [String] member
94
+ # @return [Boolean]
95
+ def sismember(key, member)
96
+ send_command([:sismember, key, member], &Boolify)
97
+ end
98
+
99
+ # Determine if multiple values are members of a set.
100
+ #
101
+ # @param [String] key
102
+ # @param [String, Array<String>] members
103
+ # @return [Array<Boolean>]
104
+ def smismember(key, *members)
105
+ members.flatten!(1)
106
+ send_command([:smismember, key].concat(members)) do |reply|
107
+ reply.map(&Boolify)
108
+ end
109
+ end
110
+
111
+ # Get all the members in a set.
112
+ #
113
+ # @param [String] key
114
+ # @return [Array<String>]
115
+ def smembers(key)
116
+ send_command([:smembers, key])
117
+ end
118
+
119
+ # Subtract multiple sets.
120
+ #
121
+ # @param [String, Array<String>] keys keys pointing to sets to subtract
122
+ # @return [Array<String>] members in the difference
123
+ def sdiff(*keys)
124
+ keys.flatten!(1)
125
+ send_command([:sdiff].concat(keys))
126
+ end
127
+
128
+ # Subtract multiple sets and store the resulting set in a key.
129
+ #
130
+ # @param [String] destination destination key
131
+ # @param [String, Array<String>] keys keys pointing to sets to subtract
132
+ # @return [Integer] number of elements in the resulting set
133
+ def sdiffstore(destination, *keys)
134
+ keys.flatten!(1)
135
+ send_command([:sdiffstore, destination].concat(keys))
136
+ end
137
+
138
+ # Intersect multiple sets.
139
+ #
140
+ # @param [String, Array<String>] keys keys pointing to sets to intersect
141
+ # @return [Array<String>] members in the intersection
142
+ def sinter(*keys)
143
+ keys.flatten!(1)
144
+ send_command([:sinter].concat(keys))
145
+ end
146
+
147
+ # Intersect multiple sets and store the resulting set in a key.
148
+ #
149
+ # @param [String] destination destination key
150
+ # @param [String, Array<String>] keys keys pointing to sets to intersect
151
+ # @return [Integer] number of elements in the resulting set
152
+ def sinterstore(destination, *keys)
153
+ keys.flatten!(1)
154
+ send_command([:sinterstore, destination].concat(keys))
155
+ end
156
+
157
+ # Add multiple sets.
158
+ #
159
+ # @param [String, Array<String>] keys keys pointing to sets to unify
160
+ # @return [Array<String>] members in the union
161
+ def sunion(*keys)
162
+ keys.flatten!(1)
163
+ send_command([:sunion].concat(keys))
164
+ end
165
+
166
+ # Add multiple sets and store the resulting set in a key.
167
+ #
168
+ # @param [String] destination destination key
169
+ # @param [String, Array<String>] keys keys pointing to sets to unify
170
+ # @return [Integer] number of elements in the resulting set
171
+ def sunionstore(destination, *keys)
172
+ keys.flatten!(1)
173
+ send_command([:sunionstore, destination].concat(keys))
174
+ end
175
+
176
+ # Scan a set
177
+ #
178
+ # @example Retrieve the first batch of keys in a set
179
+ # redis.sscan("set", 0)
180
+ #
181
+ # @param [String, Integer] cursor the cursor of the iteration
182
+ # @param [Hash] options
183
+ # - `:match => String`: only return keys matching the pattern
184
+ # - `:count => Integer`: return count keys at most per iteration
185
+ #
186
+ # @return [String, Array<String>] the next cursor and all found members
187
+ #
188
+ # See the [Redis Server SSCAN documentation](https://redis.io/docs/latest/commands/sscan/) for further details
189
+ def sscan(key, cursor, **options)
190
+ _scan(:sscan, cursor, [key], **options)
191
+ end
192
+
193
+ # Scan a set
194
+ #
195
+ # @example Retrieve all of the keys in a set
196
+ # redis.sscan_each("set").to_a
197
+ # # => ["key1", "key2", "key3"]
198
+ #
199
+ # @param [Hash] options
200
+ # - `:match => String`: only return keys matching the pattern
201
+ # - `:count => Integer`: return count keys at most per iteration
202
+ #
203
+ # @return [Enumerator] an enumerator for all keys in the set
204
+ #
205
+ # See the [Redis Server SSCAN documentation](https://redis.io/docs/latest/commands/sscan/) for further details
206
+ def sscan_each(key, **options, &block)
207
+ return to_enum(:sscan_each, key, **options) unless block_given?
208
+
209
+ cursor = 0
210
+ loop do
211
+ cursor, keys = sscan(key, cursor, **options)
212
+ keys.each(&block)
213
+ break if cursor == "0"
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end