redis 4.5.1 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,207 @@
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 [Boolean, Integer] `Boolean` when a single member is specified,
19
+ # holding whether or not adding the member succeeded, or `Integer` when an
20
+ # array of members is specified, holding the number of members that were
21
+ # successfully added
22
+ def sadd(key, member)
23
+ send_command([:sadd, key, member]) do |reply|
24
+ if member.is_a? Array
25
+ # Variadic: return integer
26
+ reply
27
+ else
28
+ # Single argument: return boolean
29
+ Boolify.call(reply)
30
+ end
31
+ end
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 [Boolean, Integer] `Boolean` when a single member is specified,
39
+ # holding whether or not removing the member succeeded, or `Integer` when an
40
+ # array of members is specified, holding the number of members that were
41
+ # successfully removed
42
+ def srem(key, member)
43
+ send_command([:srem, key, member]) do |reply|
44
+ if member.is_a? Array
45
+ # Variadic: return integer
46
+ reply
47
+ else
48
+ # Single argument: return boolean
49
+ Boolify.call(reply)
50
+ end
51
+ end
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, 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
+ send_command([:smismember, key, *members]) do |reply|
106
+ reply.map(&Boolify)
107
+ end
108
+ end
109
+
110
+ # Get all the members in a set.
111
+ #
112
+ # @param [String] key
113
+ # @return [Array<String>]
114
+ def smembers(key)
115
+ send_command([:smembers, key])
116
+ end
117
+
118
+ # Subtract multiple sets.
119
+ #
120
+ # @param [String, Array<String>] keys keys pointing to sets to subtract
121
+ # @return [Array<String>] members in the difference
122
+ def sdiff(*keys)
123
+ send_command([:sdiff, *keys])
124
+ end
125
+
126
+ # Subtract multiple sets and store the resulting set in a key.
127
+ #
128
+ # @param [String] destination destination key
129
+ # @param [String, Array<String>] keys keys pointing to sets to subtract
130
+ # @return [Integer] number of elements in the resulting set
131
+ def sdiffstore(destination, *keys)
132
+ send_command([:sdiffstore, destination, *keys])
133
+ end
134
+
135
+ # Intersect multiple sets.
136
+ #
137
+ # @param [String, Array<String>] keys keys pointing to sets to intersect
138
+ # @return [Array<String>] members in the intersection
139
+ def sinter(*keys)
140
+ send_command([:sinter, *keys])
141
+ end
142
+
143
+ # Intersect multiple sets and store the resulting set in a key.
144
+ #
145
+ # @param [String] destination destination key
146
+ # @param [String, Array<String>] keys keys pointing to sets to intersect
147
+ # @return [Integer] number of elements in the resulting set
148
+ def sinterstore(destination, *keys)
149
+ send_command([:sinterstore, destination, *keys])
150
+ end
151
+
152
+ # Add multiple sets.
153
+ #
154
+ # @param [String, Array<String>] keys keys pointing to sets to unify
155
+ # @return [Array<String>] members in the union
156
+ def sunion(*keys)
157
+ send_command([:sunion, *keys])
158
+ end
159
+
160
+ # Add multiple sets and store the resulting set in a key.
161
+ #
162
+ # @param [String] destination destination key
163
+ # @param [String, Array<String>] keys keys pointing to sets to unify
164
+ # @return [Integer] number of elements in the resulting set
165
+ def sunionstore(destination, *keys)
166
+ send_command([:sunionstore, destination, *keys])
167
+ end
168
+
169
+ # Scan a set
170
+ #
171
+ # @example Retrieve the first batch of keys in a set
172
+ # redis.sscan("set", 0)
173
+ #
174
+ # @param [String, Integer] cursor the cursor of the iteration
175
+ # @param [Hash] options
176
+ # - `:match => String`: only return keys matching the pattern
177
+ # - `:count => Integer`: return count keys at most per iteration
178
+ #
179
+ # @return [String, Array<String>] the next cursor and all found members
180
+ def sscan(key, cursor, **options)
181
+ _scan(:sscan, cursor, [key], **options)
182
+ end
183
+
184
+ # Scan a set
185
+ #
186
+ # @example Retrieve all of the keys in a set
187
+ # redis.sscan_each("set").to_a
188
+ # # => ["key1", "key2", "key3"]
189
+ #
190
+ # @param [Hash] options
191
+ # - `:match => String`: only return keys matching the pattern
192
+ # - `:count => Integer`: return count keys at most per iteration
193
+ #
194
+ # @return [Enumerator] an enumerator for all keys in the set
195
+ def sscan_each(key, **options, &block)
196
+ return to_enum(:sscan_each, key, **options) unless block_given?
197
+
198
+ cursor = 0
199
+ loop do
200
+ cursor, keys = sscan(key, cursor, **options)
201
+ keys.each(&block)
202
+ break if cursor == "0"
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end