oxblood 0.1.0.dev8 → 0.1.0.dev9

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,172 @@
1
+ module Oxblood
2
+ module Commands
3
+ module Sets
4
+ # Add one or more members to a set
5
+ # @see http://redis.io/commands/sadd
6
+ #
7
+ # @param [String] key under which store set
8
+ # @param [String, Array<String>] members to store
9
+ #
10
+ # @return [Integer] the number of elements that were added to the set,
11
+ # not including all the elements already present into the set.
12
+ def sadd(key, *members)
13
+ run(*members.unshift(:SADD, key))
14
+ end
15
+
16
+ # Get the number of members in a set
17
+ # @see http://redis.io/commands/scard
18
+ #
19
+ # @param [String] key
20
+ #
21
+ # @return [Integer] the cardinality (number of elements) of the set, or 0 if
22
+ # key does not exist
23
+ def scard(key)
24
+ run(:SCARD, key)
25
+ end
26
+
27
+ # Subtract multiple sets
28
+ # @see http://redis.io/commands/sdiff
29
+ #
30
+ # @param [String, Array<String>] keys
31
+ #
32
+ # @return [Array] array with members of the resulting set
33
+ def sdiff(*keys)
34
+ run(*keys.unshift(:SDIFF))
35
+ end
36
+
37
+ # Subtract multiple sets and store the resulting set in a key
38
+ # @see http://redis.io/commands/sdiffstore
39
+ #
40
+ # @param [String] destination key
41
+ # @param [String, Array<String>] keys of sets to diff
42
+ #
43
+ # @return [Integer] the number of elements in the resulting set
44
+ def sdiffstore(destination, *keys)
45
+ run(*keys.unshift(:SDIFFSTORE, destination))
46
+ end
47
+
48
+ # Intersect multiple sets
49
+ # @see http://redis.io/commands/sinter
50
+ #
51
+ # @param [String, Array<String>] keys to intersect
52
+ #
53
+ # @return [Array] array with members of the resulting set
54
+ def sinter(*keys)
55
+ run(*keys.unshift(:SINTER))
56
+ end
57
+
58
+ # Intersect multiple sets and store the resulting key in a key
59
+ # @see http://redis.io/commands/sinterstore
60
+ #
61
+ # @param [String] destination key
62
+ # @param [String, Array<String>] keys of sets to intersect
63
+ #
64
+ # @return [Integer] the number of elements in the resulting set
65
+ def sinterstore(destination, *keys)
66
+ run(*keys.unshift(:SINTERSTORE, destination))
67
+ end
68
+
69
+ # Determine if a given value is a member of a set
70
+ # @see http://redis.io/commands/sismember
71
+ #
72
+ # @param [String] key
73
+ # @param [String] member
74
+ #
75
+ # @return [Integer] 1 if the element is a member of the set or
76
+ # 0 if the element is not a member of the set, or if key does not exist
77
+ def sismember(key, member)
78
+ run(:SISMEMBER, key, member)
79
+ end
80
+
81
+ # Get all the members in a set
82
+ # @see http://redis.io/commands/smembers
83
+ #
84
+ # @param [String] key
85
+ #
86
+ # @return [Array] all elements of the set
87
+ def smembers(key)
88
+ run(:SMEMBERS, key)
89
+ end
90
+
91
+ # Move a member from one set to another
92
+ # @see http://redis.io/commands/smove
93
+ #
94
+ # @param [String] source
95
+ # @param [String] destination
96
+ # @param [String] member
97
+ #
98
+ # @return [Integer] 1 if the element is moved, or 0 if the element is not
99
+ # a member of source and no operation was performed
100
+ def smove(source, destination, member)
101
+ run(:SMOVE, source, destination, member)
102
+ end
103
+
104
+ # Remove and return one or multiple random members from a set
105
+ # @see http://redis.io/commands/spop
106
+ #
107
+ # @param [String] key
108
+ # @param [Integer] count
109
+ #
110
+ # @return [String] without the additional count argument the command returns
111
+ # the removed element, or nil when key does not exist
112
+ # @return [Array] when the additional count argument is passed the command
113
+ # returns an array of removed elements, or an empty array when key does
114
+ # not exist.
115
+ def spop(key, count = nil)
116
+ args = [:SPOP, key]
117
+ args << count if count
118
+ run(*args)
119
+ end
120
+
121
+ # Get one or multiple random members from a set
122
+ # @see http://redis.io/commands/srandmember
123
+ #
124
+ # @param [String] key
125
+ # @param [Integer] count
126
+ #
127
+ # @return [String, nil] without the additional count argument the command
128
+ # returns string with the randomly selected element, or nil when key
129
+ # does not exist
130
+ # @return [Array] when the additional count argument is passed the command
131
+ # returns an array of elements, or an empty array when key does not exist
132
+ def srandmember(key, count = nil)
133
+ args = [:SRANDMEMBER, key]
134
+ args << count if count
135
+ run(*args)
136
+ end
137
+
138
+ # Remove one or more members from a set
139
+ # @see http://redis.io/commands/srem
140
+ #
141
+ # @param [String] key
142
+ # @param [Array] members to remove
143
+ #
144
+ # @return [Integer] the number of members that were removed from the set,
145
+ # not including non existing members
146
+ def srem(key, *members)
147
+ run(*members.unshift(:SREM, key))
148
+ end
149
+
150
+ # Add multiple sets
151
+ # @see http://redis.io/commands/sunion
152
+ #
153
+ # @param [String, Array<String>] keys
154
+ #
155
+ # @return [Array] list with members of the resulting set
156
+ def sunion(*keys)
157
+ run(*keys.unshift(:SUNION))
158
+ end
159
+
160
+ # Add multipe sets and store the resulting set in a key
161
+ # @see http://redis.io/commands/sunionstore
162
+ #
163
+ # @param [String] destination
164
+ # @param [String, Array<String>] keys
165
+ #
166
+ # @return [Integer] the number of elements in the resulting set
167
+ def sunionstore(destination, *keys)
168
+ run(*keys.unshift(:SUNIONSTORE, destination))
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,276 @@
1
+ module Oxblood
2
+ module Commands
3
+ module SortedSets
4
+ # Add one or more members to a sorted set, or update its score if it already
5
+ # exists.
6
+ # @see http://redis.io/commands/zadd
7
+ #
8
+ # @todo Add support for zadd options
9
+ # http://redis.io/commands/zadd#zadd-options-redis-302-or-greater
10
+ #
11
+ # @param [String] key under which store set
12
+ # @param [[Float, String], Array<[Float, String]>] args scores and members
13
+ #
14
+ # @return [Integer] The number of elements added to the sorted sets, not
15
+ # including elements already existing for which the score was updated
16
+ def zadd(key, *args)
17
+ run(*args.unshift(:ZADD, key))
18
+ end
19
+
20
+ # Get the number of members in a sorted set
21
+ # @see http://redis.io/commands/zcard
22
+ #
23
+ # @param [String] key
24
+ #
25
+ # @return [Integer] the cardinality (number of elements) of the sorted set,
26
+ # or 0 if key does not exists
27
+ def zcard(key)
28
+ run(:ZCARD, key)
29
+ end
30
+
31
+ # Count the members in a sorted set with scores within the given values
32
+ # @see http://redis.io/commands/zcount
33
+ #
34
+ # @param [String] key
35
+ # @param [String] min
36
+ # @param [String] max
37
+ #
38
+ # @return [Integer] the number of elements in the specified score range
39
+ def zcount(key, min, max)
40
+ run(:ZCOUNT, key, min, max)
41
+ end
42
+
43
+ # Increment the score of a member in a sorted set
44
+ # @see http://redis.io/commands/zincrby
45
+ #
46
+ # @param [String] key
47
+ # @param [Float] increment
48
+ # @param [String] member
49
+ #
50
+ # @return [String] the new score of member (a double precision floating
51
+ # point number), represented as string
52
+ def zincrby(key, increment, member)
53
+ run(:ZINCRBY, key, increment, member)
54
+ end
55
+
56
+ # Count the number of members in a sorted set between a given
57
+ # lexicographical range
58
+ # @see http://redis.io/commands/zlexcount
59
+ #
60
+ # @param [String] key
61
+ # @param [String] min
62
+ # @param [String] max
63
+ #
64
+ # @return the number of elements in the specified score range
65
+ def zlexcount(key, min, max)
66
+ run(:ZLEXCOUNT, key, min, max)
67
+ end
68
+
69
+ # Return a range of members in a sorted set, by index
70
+ # @see http://redis.io/commands/zrange
71
+ #
72
+ # @example
73
+ # session.zrange('myzset', 0, -1)
74
+ # # => ['one', 'two']
75
+ #
76
+ # @example
77
+ # session.zrange('myzset', 0, -1, withscores: true)
78
+ # # => [['one', '1'], ['two', '2']]
79
+ #
80
+ # @param [String] key
81
+ # @param [Integer] start index
82
+ # @param [Integer] stop index
83
+ # @param [Hash] opts
84
+ #
85
+ # @option opts [Boolean] :withscores (false) Return the scores of
86
+ # the elements together with the elements
87
+ #
88
+ # @return [Array] list of elements in the specified range (optionally with
89
+ # their scores, in case the :withscores option is given)
90
+ def zrange(key, start, stop, opts = {})
91
+ common_range(:ZRANGE, key, start, stop, opts)
92
+ end
93
+
94
+ # Return a range of members in a sorted set, by score
95
+ # @see http://redis.io/commands/zrangebyscore
96
+ #
97
+ # @param [String] key under which set is stored
98
+ # @param [String] min score
99
+ # @param [String] max score
100
+ # @param [Hash] opts
101
+ #
102
+ # @option opts [Boolean] :withscores (false) Return the scores of
103
+ # the elements together with the elements
104
+ # @option opts [Array<Integer, Integer>] :limit Get a range of the matching
105
+ # elements (similar to SELECT LIMIT offset, count in SQL)
106
+ #
107
+ # @example
108
+ # session.zrangebyscore('myzset', '-inf', '+inf')
109
+ # # => ['one', 'two', 'three']
110
+ #
111
+ # @example
112
+ # session.zrangebyscore('myzset', '(1', 2, withscores: true)
113
+ # # => [['two', '2']]
114
+ #
115
+ # @example
116
+ # opts = { withscores: true, limit: [1, 1] }
117
+ # session.zrangebyscore('myzset', '-inf', '+inf', opts)
118
+ # # => [['two', '2']]
119
+ #
120
+ # @return [Array] list of elements in the specified score range (optionally
121
+ # with their scores, in case the :withscores option is given)
122
+ def zrangebyscore(key, min, max, opts = {})
123
+ common_rangebyscore(:ZRANGEBYSCORE, key, min, max, opts)
124
+ end
125
+
126
+ # Determine the index of a member in a sorted set
127
+ # @see http://redis.io/commands/zrank
128
+ #
129
+ # @param [String] key
130
+ # @param [String] member
131
+ #
132
+ # @return [Integer, nil] the rank of member or nil if member does not exist
133
+ # in the sorted set or key does not exist
134
+ def zrank(key, member)
135
+ run(:ZRANK, key, member)
136
+ end
137
+
138
+ # Remove one or more members from a sorted set
139
+ # @see http://redis.io/commands/zrem
140
+ #
141
+ # @param [String] key
142
+ # @param [Array<String>] members to delete
143
+ #
144
+ # @return [Integer] number of deleted members
145
+ # @return [RError] when key exists and does not hold a sorted set.
146
+ def zrem(key, *members)
147
+ run(*members.unshift(:ZREM, key))
148
+ end
149
+
150
+ # Remove all members in a sorted set within the given indexes
151
+ # @see http://redis.io/commands/zremrangebyrank
152
+ #
153
+ # @param [String] key
154
+ # @param [String] start
155
+ # @param [String] stop
156
+ #
157
+ # @return [Integer] the number of elements removed
158
+ def zremrangebyrank(key, start, stop)
159
+ run(:ZREMRANGEBYRANK, key, start, stop)
160
+ end
161
+
162
+ # Remove all members in a sorted set within the given scores
163
+ # @see http://redis.io/commands/zremrangebyscore
164
+ #
165
+ # @param [String] key
166
+ # @param [String] min score
167
+ # @param [String] max score
168
+ #
169
+ # @return [Integer] the number of elements removed
170
+ def zremrangebyscore(key, min, max)
171
+ run(:ZREMRANGEBYSCORE, key, min, max)
172
+ end
173
+
174
+ # Return a range of members in a sorted set, by index, with scores ordered
175
+ # from high to low
176
+ # @see http://redis.io/commands/zrevrange
177
+ #
178
+ # @example
179
+ # session.zrevrange('myzset', 0, -1)
180
+ # # => ['two', 'one']
181
+ #
182
+ # @example
183
+ # session.zrevrange('myzset', 0, -1, withscores: true)
184
+ # # => [['two', '2'], ['one', '1']]
185
+ #
186
+ # @param [String] key
187
+ # @param [Integer] start index
188
+ # @param [Integer] stop index
189
+ # @param [Hash] opts
190
+ #
191
+ # @option opts [Boolean] :withscores (false) Return the scores of
192
+ # the elements together with the elements
193
+ #
194
+ # @return [Array] list of elements in the specified range (optionally with
195
+ # their scores, in case the :withscores option is given)
196
+ def zrevrange(key, start, stop, opts = {})
197
+ common_range(:ZREVRANGE, key, start, stop, opts)
198
+ end
199
+
200
+ # Return a range of members in a sorted set, by score, with scores ordered
201
+ # from high to low
202
+ # @see http://redis.io/commands/zrevrangebyscore
203
+ #
204
+ # @param [String] key under which set is stored
205
+ # @param [String] min score
206
+ # @param [String] max score
207
+ # @param [Hash] opts
208
+ #
209
+ # @option opts [Boolean] :withscores (false) Return the scores of
210
+ # the elements together with the elements
211
+ # @option opts [Array<Integer, Integer>] :limit Get a range of the matching
212
+ # elements (similar to SELECT LIMIT offset, count in SQL)
213
+ #
214
+ # @example
215
+ # session.zrevrangebyscore('myzset', '+inf', '-inf')
216
+ # # => ['three', 'two', 'one']
217
+ #
218
+ # @example
219
+ # session.zrevrangebyscore('myzset', 2, '(1', withscores: true)
220
+ # # => [['two', '2']]
221
+ #
222
+ # @example
223
+ # opts = { withscores: true, limit: [1, 1] }
224
+ # session.zrevrangebyscore('myzset', '+inf', '-inf', opts)
225
+ # # => [['two', '2']]
226
+ #
227
+ # @return [Array] list of elements in the specified score range (optionally
228
+ # with their scores, in case the :withscores option is given)
229
+ def zrevrangebyscore(key, min, max, opts = {})
230
+ common_rangebyscore(:ZREVRANGEBYSCORE, key, min, max, opts)
231
+ end
232
+
233
+ # Determine the index of a member in a sorted set, with scores ordered from
234
+ # high to low
235
+ # @see http://redis.io/commands/zrevrank
236
+ #
237
+ # @param [String] key
238
+ # @param [String] member
239
+ #
240
+ # @return [Integer, nil] the rank of member, or nil if member does not
241
+ # exists in the sorted set or key does not exists
242
+ def zrevrank(key, member)
243
+ run(:ZREVRANK, key, member)
244
+ end
245
+
246
+ # Get the score associated with the given member in a sorted set
247
+ # @see http://redis.io/commands/zscore
248
+ #
249
+ # @param [String] key
250
+ # @param [String] member
251
+ #
252
+ # @return [String, nil] the score of member (a double precision floating
253
+ # point number), represented as string, or nil if member does not exist in
254
+ # the sorted set, or key does not exists
255
+ def zscore(key, member)
256
+ run(:ZSCORE, key, member)
257
+ end
258
+
259
+ private
260
+
261
+ def common_rangebyscore(command_name, key, min, max, opts)
262
+ args = [command_name, key, min, max]
263
+ args << :WITHSCORES if opts[:withscores]
264
+ args.push(:LIMIT).concat(opts[:limit]) if opts[:limit].is_a?(Array)
265
+
266
+ run(*args)
267
+ end
268
+
269
+ def common_range(command_name, key, start, stop, opts)
270
+ args = [command_name, key, start, stop]
271
+ args << :WITHSCORES if opts[:withscores]
272
+ run(*args)
273
+ end
274
+ end
275
+ end
276
+ end