redis 3.3.5 → 4.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +236 -2
  3. data/README.md +169 -89
  4. data/lib/redis/client.rb +176 -108
  5. data/lib/redis/cluster/command.rb +79 -0
  6. data/lib/redis/cluster/command_loader.rb +33 -0
  7. data/lib/redis/cluster/key_slot_converter.rb +72 -0
  8. data/lib/redis/cluster/node.rb +120 -0
  9. data/lib/redis/cluster/node_key.rb +31 -0
  10. data/lib/redis/cluster/node_loader.rb +34 -0
  11. data/lib/redis/cluster/option.rb +100 -0
  12. data/lib/redis/cluster/slot.rb +86 -0
  13. data/lib/redis/cluster/slot_loader.rb +46 -0
  14. data/lib/redis/cluster.rb +315 -0
  15. data/lib/redis/commands/bitmaps.rb +63 -0
  16. data/lib/redis/commands/cluster.rb +45 -0
  17. data/lib/redis/commands/connection.rb +58 -0
  18. data/lib/redis/commands/geo.rb +84 -0
  19. data/lib/redis/commands/hashes.rb +251 -0
  20. data/lib/redis/commands/hyper_log_log.rb +37 -0
  21. data/lib/redis/commands/keys.rb +455 -0
  22. data/lib/redis/commands/lists.rb +290 -0
  23. data/lib/redis/commands/pubsub.rb +72 -0
  24. data/lib/redis/commands/scripting.rb +114 -0
  25. data/lib/redis/commands/server.rb +188 -0
  26. data/lib/redis/commands/sets.rb +223 -0
  27. data/lib/redis/commands/sorted_sets.rb +812 -0
  28. data/lib/redis/commands/streams.rb +382 -0
  29. data/lib/redis/commands/strings.rb +313 -0
  30. data/lib/redis/commands/transactions.rb +139 -0
  31. data/lib/redis/commands.rb +240 -0
  32. data/lib/redis/connection/command_helper.rb +7 -10
  33. data/lib/redis/connection/hiredis.rb +5 -3
  34. data/lib/redis/connection/registry.rb +2 -1
  35. data/lib/redis/connection/ruby.rb +136 -128
  36. data/lib/redis/connection/synchrony.rb +24 -9
  37. data/lib/redis/connection.rb +3 -1
  38. data/lib/redis/distributed.rb +255 -85
  39. data/lib/redis/errors.rb +57 -0
  40. data/lib/redis/hash_ring.rb +30 -73
  41. data/lib/redis/pipeline.rb +178 -13
  42. data/lib/redis/subscribe.rb +11 -12
  43. data/lib/redis/version.rb +3 -1
  44. data/lib/redis.rb +174 -2661
  45. metadata +66 -202
  46. data/.gitignore +0 -16
  47. data/.travis/Gemfile +0 -11
  48. data/.travis.yml +0 -89
  49. data/.yardopts +0 -3
  50. data/Gemfile +0 -4
  51. data/Rakefile +0 -87
  52. data/benchmarking/logging.rb +0 -71
  53. data/benchmarking/pipeline.rb +0 -51
  54. data/benchmarking/speed.rb +0 -21
  55. data/benchmarking/suite.rb +0 -24
  56. data/benchmarking/worker.rb +0 -71
  57. data/examples/basic.rb +0 -15
  58. data/examples/consistency.rb +0 -114
  59. data/examples/dist_redis.rb +0 -43
  60. data/examples/incr-decr.rb +0 -17
  61. data/examples/list.rb +0 -26
  62. data/examples/pubsub.rb +0 -37
  63. data/examples/sentinel/sentinel.conf +0 -9
  64. data/examples/sentinel/start +0 -49
  65. data/examples/sentinel.rb +0 -41
  66. data/examples/sets.rb +0 -36
  67. data/examples/unicorn/config.ru +0 -3
  68. data/examples/unicorn/unicorn.rb +0 -20
  69. data/redis.gemspec +0 -44
  70. data/test/bitpos_test.rb +0 -69
  71. data/test/blocking_commands_test.rb +0 -42
  72. data/test/client_test.rb +0 -59
  73. data/test/command_map_test.rb +0 -30
  74. data/test/commands_on_hashes_test.rb +0 -21
  75. data/test/commands_on_hyper_log_log_test.rb +0 -21
  76. data/test/commands_on_lists_test.rb +0 -20
  77. data/test/commands_on_sets_test.rb +0 -77
  78. data/test/commands_on_sorted_sets_test.rb +0 -137
  79. data/test/commands_on_strings_test.rb +0 -101
  80. data/test/commands_on_value_types_test.rb +0 -133
  81. data/test/connection_handling_test.rb +0 -277
  82. data/test/connection_test.rb +0 -57
  83. data/test/db/.gitkeep +0 -0
  84. data/test/distributed_blocking_commands_test.rb +0 -46
  85. data/test/distributed_commands_on_hashes_test.rb +0 -10
  86. data/test/distributed_commands_on_hyper_log_log_test.rb +0 -33
  87. data/test/distributed_commands_on_lists_test.rb +0 -22
  88. data/test/distributed_commands_on_sets_test.rb +0 -83
  89. data/test/distributed_commands_on_sorted_sets_test.rb +0 -18
  90. data/test/distributed_commands_on_strings_test.rb +0 -59
  91. data/test/distributed_commands_on_value_types_test.rb +0 -95
  92. data/test/distributed_commands_requiring_clustering_test.rb +0 -164
  93. data/test/distributed_connection_handling_test.rb +0 -23
  94. data/test/distributed_internals_test.rb +0 -79
  95. data/test/distributed_key_tags_test.rb +0 -52
  96. data/test/distributed_persistence_control_commands_test.rb +0 -26
  97. data/test/distributed_publish_subscribe_test.rb +0 -92
  98. data/test/distributed_remote_server_control_commands_test.rb +0 -66
  99. data/test/distributed_scripting_test.rb +0 -102
  100. data/test/distributed_sorting_test.rb +0 -20
  101. data/test/distributed_test.rb +0 -58
  102. data/test/distributed_transactions_test.rb +0 -32
  103. data/test/encoding_test.rb +0 -18
  104. data/test/error_replies_test.rb +0 -59
  105. data/test/fork_safety_test.rb +0 -65
  106. data/test/helper.rb +0 -232
  107. data/test/helper_test.rb +0 -24
  108. data/test/internals_test.rb +0 -417
  109. data/test/lint/blocking_commands.rb +0 -150
  110. data/test/lint/hashes.rb +0 -162
  111. data/test/lint/hyper_log_log.rb +0 -60
  112. data/test/lint/lists.rb +0 -143
  113. data/test/lint/sets.rb +0 -140
  114. data/test/lint/sorted_sets.rb +0 -316
  115. data/test/lint/strings.rb +0 -260
  116. data/test/lint/value_types.rb +0 -122
  117. data/test/persistence_control_commands_test.rb +0 -26
  118. data/test/pipelining_commands_test.rb +0 -242
  119. data/test/publish_subscribe_test.rb +0 -282
  120. data/test/remote_server_control_commands_test.rb +0 -118
  121. data/test/scanning_test.rb +0 -413
  122. data/test/scripting_test.rb +0 -78
  123. data/test/sentinel_command_test.rb +0 -80
  124. data/test/sentinel_test.rb +0 -255
  125. data/test/sorting_test.rb +0 -59
  126. data/test/ssl_test.rb +0 -73
  127. data/test/support/connection/hiredis.rb +0 -1
  128. data/test/support/connection/ruby.rb +0 -1
  129. data/test/support/connection/synchrony.rb +0 -17
  130. data/test/support/redis_mock.rb +0 -130
  131. data/test/support/ssl/gen_certs.sh +0 -31
  132. data/test/support/ssl/trusted-ca.crt +0 -25
  133. data/test/support/ssl/trusted-ca.key +0 -27
  134. data/test/support/ssl/trusted-cert.crt +0 -81
  135. data/test/support/ssl/trusted-cert.key +0 -28
  136. data/test/support/ssl/untrusted-ca.crt +0 -26
  137. data/test/support/ssl/untrusted-ca.key +0 -27
  138. data/test/support/ssl/untrusted-cert.crt +0 -82
  139. data/test/support/ssl/untrusted-cert.key +0 -28
  140. data/test/support/wire/synchrony.rb +0 -24
  141. data/test/support/wire/thread.rb +0 -5
  142. data/test/synchrony_driver.rb +0 -88
  143. data/test/test.conf.erb +0 -9
  144. data/test/thread_safety_test.rb +0 -62
  145. data/test/transactions_test.rb +0 -264
  146. data/test/unknown_commands_test.rb +0 -14
  147. data/test/url_param_test.rb +0 -138
@@ -0,0 +1,251 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Redis
4
+ module Commands
5
+ module Hashes
6
+ # Get the number of fields in a hash.
7
+ #
8
+ # @param [String] key
9
+ # @return [Integer] number of fields in the hash
10
+ def hlen(key)
11
+ send_command([:hlen, key])
12
+ end
13
+
14
+ # Set one or more hash values.
15
+ #
16
+ # @example
17
+ # redis.hset("hash", "f1", "v1", "f2", "v2") # => 2
18
+ # redis.hset("hash", { "f1" => "v1", "f2" => "v2" }) # => 2
19
+ #
20
+ # @param [String] key
21
+ # @param [Array<String> | Hash<String, String>] attrs array or hash of fields and values
22
+ # @return [Integer] The number of fields that were added to the hash
23
+ def hset(key, *attrs)
24
+ attrs = attrs.first.flatten if attrs.size == 1 && attrs.first.is_a?(Hash)
25
+
26
+ send_command([:hset, key, *attrs])
27
+ end
28
+
29
+ # Set the value of a hash field, only if the field does not exist.
30
+ #
31
+ # @param [String] key
32
+ # @param [String] field
33
+ # @param [String] value
34
+ # @return [Boolean] whether or not the field was **added** to the hash
35
+ def hsetnx(key, field, value)
36
+ send_command([:hsetnx, key, field, value], &Boolify)
37
+ end
38
+
39
+ # Set one or more hash values.
40
+ #
41
+ # @example
42
+ # redis.hmset("hash", "f1", "v1", "f2", "v2")
43
+ # # => "OK"
44
+ #
45
+ # @param [String] key
46
+ # @param [Array<String>] attrs array of fields and values
47
+ # @return [String] `"OK"`
48
+ #
49
+ # @see #mapped_hmset
50
+ def hmset(key, *attrs)
51
+ send_command([:hmset, key] + attrs)
52
+ end
53
+
54
+ # Set one or more hash values.
55
+ #
56
+ # @example
57
+ # redis.mapped_hmset("hash", { "f1" => "v1", "f2" => "v2" })
58
+ # # => "OK"
59
+ #
60
+ # @param [String] key
61
+ # @param [Hash] hash a non-empty hash with fields mapping to values
62
+ # @return [String] `"OK"`
63
+ #
64
+ # @see #hmset
65
+ def mapped_hmset(key, hash)
66
+ hmset(key, hash.to_a.flatten)
67
+ end
68
+
69
+ # Get the value of a hash field.
70
+ #
71
+ # @param [String] key
72
+ # @param [String] field
73
+ # @return [String]
74
+ def hget(key, field)
75
+ send_command([:hget, key, field])
76
+ end
77
+
78
+ # Get the values of all the given hash fields.
79
+ #
80
+ # @example
81
+ # redis.hmget("hash", "f1", "f2")
82
+ # # => ["v1", "v2"]
83
+ #
84
+ # @param [String] key
85
+ # @param [Array<String>] fields array of fields
86
+ # @return [Array<String>] an array of values for the specified fields
87
+ #
88
+ # @see #mapped_hmget
89
+ def hmget(key, *fields, &blk)
90
+ send_command([:hmget, key] + fields, &blk)
91
+ end
92
+
93
+ # Get the values of all the given hash fields.
94
+ #
95
+ # @example
96
+ # redis.mapped_hmget("hash", "f1", "f2")
97
+ # # => { "f1" => "v1", "f2" => "v2" }
98
+ #
99
+ # @param [String] key
100
+ # @param [Array<String>] fields array of fields
101
+ # @return [Hash] a hash mapping the specified fields to their values
102
+ #
103
+ # @see #hmget
104
+ def mapped_hmget(key, *fields)
105
+ hmget(key, *fields) do |reply|
106
+ if reply.is_a?(Array)
107
+ Hash[fields.zip(reply)]
108
+ else
109
+ reply
110
+ end
111
+ end
112
+ end
113
+
114
+ # Get one or more random fields from a hash.
115
+ #
116
+ # @example Get one random field
117
+ # redis.hrandfield("hash")
118
+ # # => "f1"
119
+ # @example Get multiple random fields
120
+ # redis.hrandfield("hash", 2)
121
+ # # => ["f1, "f2"]
122
+ # @example Get multiple random fields with values
123
+ # redis.hrandfield("hash", 2, with_values: true)
124
+ # # => [["f1", "s1"], ["f2", "s2"]]
125
+ #
126
+ # @param [String] key
127
+ # @param [Integer] count
128
+ # @param [Hash] options
129
+ # - `:with_values => true`: include values in output
130
+ #
131
+ # @return [nil, String, Array<String>, Array<[String, Float]>]
132
+ # - when `key` does not exist, `nil`
133
+ # - when `count` is not specified, a field name
134
+ # - when `count` is specified and `:with_values` is not specified, an array of field names
135
+ # - when `:with_values` is specified, an array with `[field, value]` pairs
136
+ def hrandfield(key, count = nil, withvalues: false, with_values: withvalues)
137
+ if with_values && count.nil?
138
+ raise ArgumentError, "count argument must be specified"
139
+ end
140
+
141
+ args = [:hrandfield, key]
142
+ args << count if count
143
+ args << "WITHVALUES" if with_values
144
+
145
+ parser = Pairify if with_values
146
+ send_command(args, &parser)
147
+ end
148
+
149
+ # Delete one or more hash fields.
150
+ #
151
+ # @param [String] key
152
+ # @param [String, Array<String>] field
153
+ # @return [Integer] the number of fields that were removed from the hash
154
+ def hdel(key, *fields)
155
+ send_command([:hdel, key, *fields])
156
+ end
157
+
158
+ # Determine if a hash field exists.
159
+ #
160
+ # @param [String] key
161
+ # @param [String] field
162
+ # @return [Boolean] whether or not the field exists in the hash
163
+ def hexists(key, field)
164
+ send_command([:hexists, key, field], &Boolify)
165
+ end
166
+
167
+ # Increment the integer value of a hash field by the given integer number.
168
+ #
169
+ # @param [String] key
170
+ # @param [String] field
171
+ # @param [Integer] increment
172
+ # @return [Integer] value of the field after incrementing it
173
+ def hincrby(key, field, increment)
174
+ send_command([:hincrby, key, field, increment])
175
+ end
176
+
177
+ # Increment the numeric value of a hash field by the given float number.
178
+ #
179
+ # @param [String] key
180
+ # @param [String] field
181
+ # @param [Float] increment
182
+ # @return [Float] value of the field after incrementing it
183
+ def hincrbyfloat(key, field, increment)
184
+ send_command([:hincrbyfloat, key, field, increment], &Floatify)
185
+ end
186
+
187
+ # Get all the fields in a hash.
188
+ #
189
+ # @param [String] key
190
+ # @return [Array<String>]
191
+ def hkeys(key)
192
+ send_command([:hkeys, key])
193
+ end
194
+
195
+ # Get all the values in a hash.
196
+ #
197
+ # @param [String] key
198
+ # @return [Array<String>]
199
+ def hvals(key)
200
+ send_command([:hvals, key])
201
+ end
202
+
203
+ # Get all the fields and values in a hash.
204
+ #
205
+ # @param [String] key
206
+ # @return [Hash<String, String>]
207
+ def hgetall(key)
208
+ send_command([:hgetall, key], &Hashify)
209
+ end
210
+
211
+ # Scan a hash
212
+ #
213
+ # @example Retrieve the first batch of key/value pairs in a hash
214
+ # redis.hscan("hash", 0)
215
+ #
216
+ # @param [String, Integer] cursor the cursor of the iteration
217
+ # @param [Hash] options
218
+ # - `:match => String`: only return keys matching the pattern
219
+ # - `:count => Integer`: return count keys at most per iteration
220
+ #
221
+ # @return [String, Array<[String, String]>] the next cursor and all found keys
222
+ def hscan(key, cursor, **options)
223
+ _scan(:hscan, cursor, [key], **options) do |reply|
224
+ [reply[0], reply[1].each_slice(2).to_a]
225
+ end
226
+ end
227
+
228
+ # Scan a hash
229
+ #
230
+ # @example Retrieve all of the key/value pairs in a hash
231
+ # redis.hscan_each("hash").to_a
232
+ # # => [["key70", "70"], ["key80", "80"]]
233
+ #
234
+ # @param [Hash] options
235
+ # - `:match => String`: only return keys matching the pattern
236
+ # - `:count => Integer`: return count keys at most per iteration
237
+ #
238
+ # @return [Enumerator] an enumerator for all found keys
239
+ def hscan_each(key, **options, &block)
240
+ return to_enum(:hscan_each, key, **options) unless block_given?
241
+
242
+ cursor = 0
243
+ loop do
244
+ cursor, values = hscan(key, cursor, **options)
245
+ values.each(&block)
246
+ break if cursor == "0"
247
+ end
248
+ end
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Redis
4
+ module Commands
5
+ module HyperLogLog
6
+ # Add one or more members to a HyperLogLog structure.
7
+ #
8
+ # @param [String] key
9
+ # @param [String, Array<String>] member one member, or array of members
10
+ # @return [Boolean] true if at least 1 HyperLogLog internal register was altered. false otherwise.
11
+ def pfadd(key, member)
12
+ send_command([:pfadd, key, member], &Boolify)
13
+ end
14
+
15
+ # Get the approximate cardinality of members added to HyperLogLog structure.
16
+ #
17
+ # If called with multiple keys, returns the approximate cardinality of the
18
+ # union of the HyperLogLogs contained in the keys.
19
+ #
20
+ # @param [String, Array<String>] keys
21
+ # @return [Integer]
22
+ def pfcount(*keys)
23
+ send_command([:pfcount] + keys)
24
+ end
25
+
26
+ # Merge multiple HyperLogLog values into an unique value that will approximate the cardinality of the union of
27
+ # the observed Sets of the source HyperLogLog structures.
28
+ #
29
+ # @param [String] dest_key destination key
30
+ # @param [String, Array<String>] source_key source key, or array of keys
31
+ # @return [Boolean]
32
+ def pfmerge(dest_key, *source_key)
33
+ send_command([:pfmerge, dest_key, *source_key], &BoolifySet)
34
+ end
35
+ end
36
+ end
37
+ end