riak-client-noenc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gitignore +42 -0
  4. data/.rspec +1 -0
  5. data/Gemfile +17 -0
  6. data/Guardfile +20 -0
  7. data/LICENSE.md +16 -0
  8. data/README.markdown +640 -0
  9. data/RELEASE_NOTES.md +392 -0
  10. data/Rakefile +119 -0
  11. data/lib/riak.rb +22 -0
  12. data/lib/riak/bucket.rb +297 -0
  13. data/lib/riak/bucket_properties.rb +74 -0
  14. data/lib/riak/bucket_type.rb +77 -0
  15. data/lib/riak/bucket_typed/bucket.rb +121 -0
  16. data/lib/riak/client.rb +433 -0
  17. data/lib/riak/client/beefcake/bucket_properties_operator.rb +178 -0
  18. data/lib/riak/client/beefcake/crdt/counter_loader.rb +18 -0
  19. data/lib/riak/client/beefcake/crdt/map_loader.rb +64 -0
  20. data/lib/riak/client/beefcake/crdt/set_loader.rb +18 -0
  21. data/lib/riak/client/beefcake/crdt_loader.rb +84 -0
  22. data/lib/riak/client/beefcake/crdt_operator.rb +223 -0
  23. data/lib/riak/client/beefcake/footer +4 -0
  24. data/lib/riak/client/beefcake/header +6 -0
  25. data/lib/riak/client/beefcake/message_codes.rb +89 -0
  26. data/lib/riak/client/beefcake/message_overlay.rb +87 -0
  27. data/lib/riak/client/beefcake/messages.rb +772 -0
  28. data/lib/riak/client/beefcake/object_methods.rb +112 -0
  29. data/lib/riak/client/beefcake/protocol.rb +105 -0
  30. data/lib/riak/client/beefcake/socket.rb +260 -0
  31. data/lib/riak/client/beefcake_protobuffs_backend.rb +538 -0
  32. data/lib/riak/client/decaying.rb +36 -0
  33. data/lib/riak/client/feature_detection.rb +120 -0
  34. data/lib/riak/client/instrumentation.rb +19 -0
  35. data/lib/riak/client/node.rb +49 -0
  36. data/lib/riak/client/protobuffs_backend.rb +143 -0
  37. data/lib/riak/client/search.rb +27 -0
  38. data/lib/riak/client/yokozuna.rb +52 -0
  39. data/lib/riak/conflict.rb +13 -0
  40. data/lib/riak/core_ext.rb +7 -0
  41. data/lib/riak/core_ext/blank.rb +53 -0
  42. data/lib/riak/core_ext/deep_dup.rb +13 -0
  43. data/lib/riak/core_ext/extract_options.rb +7 -0
  44. data/lib/riak/core_ext/json.rb +15 -0
  45. data/lib/riak/core_ext/slice.rb +18 -0
  46. data/lib/riak/core_ext/stringify_keys.rb +10 -0
  47. data/lib/riak/core_ext/symbolize_keys.rb +10 -0
  48. data/lib/riak/core_ext/to_param.rb +31 -0
  49. data/lib/riak/counter.rb +101 -0
  50. data/lib/riak/crdt.rb +21 -0
  51. data/lib/riak/crdt/base.rb +183 -0
  52. data/lib/riak/crdt/batch_counter.rb +19 -0
  53. data/lib/riak/crdt/batch_map.rb +41 -0
  54. data/lib/riak/crdt/counter.rb +82 -0
  55. data/lib/riak/crdt/inner_counter.rb +81 -0
  56. data/lib/riak/crdt/inner_flag.rb +42 -0
  57. data/lib/riak/crdt/inner_map.rb +75 -0
  58. data/lib/riak/crdt/inner_register.rb +26 -0
  59. data/lib/riak/crdt/inner_set.rb +102 -0
  60. data/lib/riak/crdt/map.rb +121 -0
  61. data/lib/riak/crdt/operation.rb +19 -0
  62. data/lib/riak/crdt/set.rb +166 -0
  63. data/lib/riak/crdt/typed_collection.rb +181 -0
  64. data/lib/riak/encoding.rb +6 -0
  65. data/lib/riak/errors/backend_creation.rb +9 -0
  66. data/lib/riak/errors/base.rb +9 -0
  67. data/lib/riak/errors/connection_error.rb +50 -0
  68. data/lib/riak/errors/crdt_error.rb +38 -0
  69. data/lib/riak/errors/failed_request.rb +58 -0
  70. data/lib/riak/errors/protobuffs_error.rb +11 -0
  71. data/lib/riak/errors/search_error.rb +35 -0
  72. data/lib/riak/i18n.rb +7 -0
  73. data/lib/riak/index_collection.rb +71 -0
  74. data/lib/riak/instrumentation.rb +6 -0
  75. data/lib/riak/json.rb +52 -0
  76. data/lib/riak/link.rb +96 -0
  77. data/lib/riak/list_buckets.rb +28 -0
  78. data/lib/riak/locale/en.yml +107 -0
  79. data/lib/riak/locale/fr.yml +51 -0
  80. data/lib/riak/map_reduce.rb +295 -0
  81. data/lib/riak/map_reduce/filter_builder.rb +103 -0
  82. data/lib/riak/map_reduce/phase.rb +98 -0
  83. data/lib/riak/map_reduce/results.rb +49 -0
  84. data/lib/riak/map_reduce_error.rb +7 -0
  85. data/lib/riak/multiget.rb +122 -0
  86. data/lib/riak/preflist_item.rb +7 -0
  87. data/lib/riak/rcontent.rb +173 -0
  88. data/lib/riak/robject.rb +222 -0
  89. data/lib/riak/search.rb +11 -0
  90. data/lib/riak/search/index.rb +87 -0
  91. data/lib/riak/search/query.rb +141 -0
  92. data/lib/riak/search/result_collection.rb +144 -0
  93. data/lib/riak/search/result_document.rb +129 -0
  94. data/lib/riak/search/schema.rb +65 -0
  95. data/lib/riak/secondary_index.rb +81 -0
  96. data/lib/riak/serializers.rb +73 -0
  97. data/lib/riak/stamp.rb +77 -0
  98. data/lib/riak/util/escape.rb +80 -0
  99. data/lib/riak/util/tcp_socket_extensions.rb +58 -0
  100. data/lib/riak/util/translation.rb +18 -0
  101. data/lib/riak/version.rb +3 -0
  102. data/lib/riak/walk_spec.rb +145 -0
  103. data/spec/failover/failover.rb +59 -0
  104. data/spec/fixtures/bitcask.txt +25 -0
  105. data/spec/fixtures/cat.jpg +0 -0
  106. data/spec/fixtures/multipart-basic-conflict.txt +15 -0
  107. data/spec/fixtures/multipart-blank.txt +7 -0
  108. data/spec/fixtures/multipart-mapreduce.txt +10 -0
  109. data/spec/fixtures/multipart-with-body.txt +16 -0
  110. data/spec/fixtures/multipart-with-marked-tombstones.txt +17 -0
  111. data/spec/fixtures/multipart-with-unmarked-tombstone.txt +16 -0
  112. data/spec/fixtures/server.cert.crt +15 -0
  113. data/spec/fixtures/server.cert.key +15 -0
  114. data/spec/fixtures/test.pem +1 -0
  115. data/spec/fixtures/yz_schema_template.xml +18 -0
  116. data/spec/integration/riak/bucket_types_spec.rb +270 -0
  117. data/spec/integration/riak/conflict_resolution_spec.rb +96 -0
  118. data/spec/integration/riak/counters_spec.rb +36 -0
  119. data/spec/integration/riak/crdt/configuration_spec.rb +37 -0
  120. data/spec/integration/riak/crdt_search_spec.rb +176 -0
  121. data/spec/integration/riak/crdt_spec.rb +250 -0
  122. data/spec/integration/riak/crdt_validation/map_spec.rb +63 -0
  123. data/spec/integration/riak/crdt_validation/set_spec.rb +122 -0
  124. data/spec/integration/riak/preflist_spec.rb +31 -0
  125. data/spec/integration/riak/properties_spec.rb +69 -0
  126. data/spec/integration/riak/protobuffs/interrupted_request_spec.rb +33 -0
  127. data/spec/integration/riak/protobuffs_backends_spec.rb +40 -0
  128. data/spec/integration/riak/search_spec.rb +104 -0
  129. data/spec/integration/riak/secondary_index_spec.rb +72 -0
  130. data/spec/integration/riak/security_spec.rb +100 -0
  131. data/spec/integration/riak/threading_spec.rb +150 -0
  132. data/spec/integration/yokozuna/index_spec.rb +61 -0
  133. data/spec/integration/yokozuna/queries_spec.rb +115 -0
  134. data/spec/integration/yokozuna/schema_spec.rb +49 -0
  135. data/spec/riak/beefcake_protobuffs_backend/bucket_properties_operator_spec.rb +247 -0
  136. data/spec/riak/beefcake_protobuffs_backend/crdt_operator_spec.rb +222 -0
  137. data/spec/riak/beefcake_protobuffs_backend/object_methods_spec.rb +23 -0
  138. data/spec/riak/beefcake_protobuffs_backend/protocol_spec.rb +189 -0
  139. data/spec/riak/beefcake_protobuffs_backend_spec.rb +162 -0
  140. data/spec/riak/bucket_properties_spec.rb +135 -0
  141. data/spec/riak/bucket_spec.rb +275 -0
  142. data/spec/riak/bucket_type_spec.rb +50 -0
  143. data/spec/riak/bucket_typed/bucket_spec.rb +62 -0
  144. data/spec/riak/client_spec.rb +246 -0
  145. data/spec/riak/core_ext/to_param_spec.rb +15 -0
  146. data/spec/riak/counter_spec.rb +122 -0
  147. data/spec/riak/crdt/counter_spec.rb +55 -0
  148. data/spec/riak/crdt/inner_counter_spec.rb +21 -0
  149. data/spec/riak/crdt/inner_flag_spec.rb +39 -0
  150. data/spec/riak/crdt/inner_map_spec.rb +47 -0
  151. data/spec/riak/crdt/inner_register_spec.rb +40 -0
  152. data/spec/riak/crdt/inner_set_spec.rb +33 -0
  153. data/spec/riak/crdt/map_spec.rb +78 -0
  154. data/spec/riak/crdt/set_spec.rb +61 -0
  155. data/spec/riak/crdt/shared_examples.rb +74 -0
  156. data/spec/riak/crdt/typed_collection_spec.rb +225 -0
  157. data/spec/riak/escape_spec.rb +72 -0
  158. data/spec/riak/feature_detection_spec.rb +77 -0
  159. data/spec/riak/index_collection_spec.rb +53 -0
  160. data/spec/riak/instrumentation_spec.rb +124 -0
  161. data/spec/riak/link_spec.rb +85 -0
  162. data/spec/riak/list_buckets_spec.rb +41 -0
  163. data/spec/riak/map_reduce/filter_builder_spec.rb +32 -0
  164. data/spec/riak/map_reduce/phase_spec.rb +142 -0
  165. data/spec/riak/map_reduce_spec.rb +434 -0
  166. data/spec/riak/multiget_spec.rb +81 -0
  167. data/spec/riak/node_spec.rb +26 -0
  168. data/spec/riak/robject_spec.rb +496 -0
  169. data/spec/riak/search/index_spec.rb +72 -0
  170. data/spec/riak/search/query_spec.rb +88 -0
  171. data/spec/riak/search/result_collection_spec.rb +89 -0
  172. data/spec/riak/search/result_document_spec.rb +106 -0
  173. data/spec/riak/search/schema_spec.rb +63 -0
  174. data/spec/riak/search_spec.rb +107 -0
  175. data/spec/riak/secondary_index_spec.rb +225 -0
  176. data/spec/riak/serializers_spec.rb +121 -0
  177. data/spec/riak/stamp_spec.rb +54 -0
  178. data/spec/riak/walk_spec_spec.rb +203 -0
  179. data/spec/spec_helper.rb +66 -0
  180. data/spec/support/certs/README.md +13 -0
  181. data/spec/support/certs/ca.crt +21 -0
  182. data/spec/support/certs/client.crl +13 -0
  183. data/spec/support/certs/client.crt +94 -0
  184. data/spec/support/certs/client.csr +18 -0
  185. data/spec/support/certs/client.key +27 -0
  186. data/spec/support/certs/empty_ca.crt +21 -0
  187. data/spec/support/certs/server.crl +13 -0
  188. data/spec/support/certs/server.crt +94 -0
  189. data/spec/support/certs/server.key +27 -0
  190. data/spec/support/crdt_search_config.rb +112 -0
  191. data/spec/support/crdt_search_fixtures.rb +42 -0
  192. data/spec/support/integration_setup.rb +10 -0
  193. data/spec/support/search_config.rb +83 -0
  194. data/spec/support/search_corpus_setup.rb +39 -0
  195. data/spec/support/test_client.rb +46 -0
  196. data/spec/support/test_client.yml.example +10 -0
  197. data/spec/support/unified_backend_examples.rb +380 -0
  198. data/spec/support/version_filter.rb +12 -0
  199. data/spec/support/wait_until.rb +20 -0
  200. metadata +511 -0
@@ -0,0 +1,434 @@
1
+ require 'spec_helper'
2
+
3
+ describe Riak::MapReduce do
4
+
5
+ let(:backend){ double 'Backend' }
6
+ let(:client) do
7
+ Riak::Client.new.tap do |c|
8
+ allow(c).to receive(:backend).and_yield(backend)
9
+ end
10
+ end
11
+ let(:mr) { Riak::MapReduce.new(client) }
12
+
13
+ let(:bucket_type){ client.bucket_type 'type' }
14
+ let(:typed_bucket){ bucket_type.bucket 'bucket' }
15
+ let(:typed_object){ Riak::RObject.new typed_bucket, 'key' }
16
+
17
+ let(:default_type){ client.bucket_type Riak::BucketType::DEFAULT_NAME }
18
+ let(:default_bucket){ default_type.bucket 'bucket' }
19
+ let(:default_object){ Riak::RObject.new default_bucket, 'key' }
20
+
21
+ it "requires a client" do
22
+ expect { Riak::MapReduce.new }.to raise_error
23
+ expect { Riak::MapReduce.new(client) }.not_to raise_error
24
+ end
25
+
26
+ it "initializes the inputs and query to empty arrays" do
27
+ expect(mr.inputs).to eq([])
28
+ expect(mr.query).to eq([])
29
+ end
30
+
31
+ it "yields itself when given a block on initializing" do
32
+ mapred2 = nil
33
+ mapred = Riak::MapReduce.new(client) do |mr|
34
+ mapred2 = mr
35
+ end
36
+ expect(mapred2).to eq(mapred)
37
+ end
38
+
39
+ describe "adding inputs" do
40
+ it "returns self for chaining" do
41
+ expect(mr.add("foo", "bar")).to eq(mr)
42
+ end
43
+
44
+ it "adds bucket/key pairs to the inputs" do
45
+ mr.add("foo", "bar")
46
+ expect(mr.inputs).to eq([%w(foo bar)])
47
+ end
48
+
49
+ it "adds an array containing a bucket/key pair to the inputs" do
50
+ mr.add(%w(foo bar))
51
+ expect(mr.inputs).to eq([%w(foo bar)])
52
+ end
53
+
54
+ it "adds an object to the inputs by its bucket and key" do
55
+ bucket = Riak::Bucket.new(client, "foo")
56
+ obj = Riak::RObject.new(bucket, "bar")
57
+ mr.add(obj)
58
+ expect(mr.inputs).to eq([%w(foo bar)])
59
+ end
60
+
61
+ it 'adds a bucket-typed object to the inputs' do
62
+ mr.add typed_object
63
+ expect(mr.inputs).to eq [[typed_bucket.name,
64
+ typed_object.key,
65
+ '',
66
+ typed_bucket.type.name
67
+ ]]
68
+ end
69
+
70
+ it "adds an array containing a bucket/key/key-data triple to the inputs" do
71
+ mr.add(["foo", "bar", 1000])
72
+ expect(mr.inputs).to eq([["foo", "bar", 1000]])
73
+ end
74
+
75
+ it "uses a bucket name as the single input" do
76
+ mr.add(Riak::Bucket.new(client, "foo"))
77
+ expect(mr.inputs).to eq("foo")
78
+ mr.add("docs")
79
+ expect(mr.inputs).to eq("docs")
80
+ end
81
+
82
+ it 'accepts a bucket typed bucket as a single input' do
83
+ mr.add typed_bucket
84
+ expect(mr.inputs).to eq([bucket_type.name, typed_bucket.name ])
85
+ end
86
+
87
+ it "doesn't pass a default bucket type name" do
88
+ mr.add default_bucket
89
+ expect(mr.inputs).to eq default_bucket.name
90
+ end
91
+
92
+ it "accepts a list of key-filters along with a bucket" do
93
+ mr.add("foo",
94
+ [[:tokenize, "-", 3],
95
+ [:string_to_int],
96
+ [:between, 2009, 2010]])
97
+ expect(mr.inputs).to eq({bucket: "foo",
98
+ key_filters: [[:tokenize, "-", 3],
99
+ [:string_to_int],
100
+ [:between, 2009, 2010]]})
101
+ end
102
+
103
+ it 'accepts a list of key-filters along with a bucket-typed bucket' do
104
+ filters = [
105
+ [:tokenize, '-', 3],
106
+ [:string_to_int],
107
+ [:between, 2009, 2010]
108
+ ]
109
+
110
+ mr.add(typed_bucket, filters)
111
+
112
+ expect(mr.inputs).to eq(
113
+ bucket: [typed_bucket.type.name,
114
+ typed_bucket.name],
115
+ key_filters: filters
116
+ )
117
+ end
118
+
119
+ it "adds a bucket and filter list via a builder block" do
120
+ mr.filter("foo") do
121
+ tokenize "-", 3
122
+ string_to_int
123
+ between 2009, 2010
124
+ end
125
+ expect(mr.inputs).to eq(bucket: "foo",
126
+ key_filters: [[:tokenize, "-", 3],
127
+ [:string_to_int],
128
+ [:between, 2009, 2010]])
129
+ end
130
+
131
+ context "using secondary indexes as inputs" do
132
+ it "set the inputs for a bucket-typed bucket" do
133
+ expect(mr.index(typed_bucket, "email_bin", "sean@basho.com")).to eq(mr)
134
+ expect(mr.inputs).to eq(bucket: [typed_bucket.type.name, typed_bucket.name],
135
+ index: "email_bin",
136
+ key: "sean@basho.com")
137
+ end
138
+
139
+ it "sets the inputs for equality" do
140
+ expect(mr.index("foo", "email_bin", "sean@basho.com")).to eq(mr)
141
+ expect(mr.inputs).to eq(bucket: "foo",
142
+ index: "email_bin",
143
+ key: "sean@basho.com")
144
+ end
145
+
146
+ it "sets the inputs for a range" do
147
+ expect(mr.index("foo", "rank_int", 10..20)).to eq(mr)
148
+ expect(mr.inputs).to eq(bucket: "foo",
149
+ index: "rank_int",
150
+ start: 10,
151
+ end: 20)
152
+ end
153
+
154
+ it "raises an error when given an invalid query" do
155
+ expect do
156
+ mr.index("foo", "rank_int", 1.0348)
157
+ end.to raise_error(ArgumentError)
158
+ expect do
159
+ mr.index("foo", "rank_int", Range.new(1.03, 1.05))
160
+ end.to raise_error(ArgumentError)
161
+ end
162
+ end
163
+
164
+ describe "escaping" do
165
+ before { @oldesc, Riak.escaper = Riak.escaper, CGI }
166
+ after { Riak.escaper = @oldesc }
167
+
168
+ context "when url_decoding is false" do
169
+ before { @urldecode, Riak.url_decoding = Riak.url_decoding, false }
170
+ after { Riak.url_decoding = @urldecode }
171
+
172
+ it "adds bucket/key pairs to the inputs with bucket and key escaped" do
173
+ mr.add("[foo]", "(bar)")
174
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
175
+ end
176
+
177
+ it "adds an escaped array containing a bucket/key pair to the inputs" do
178
+ mr.add(["[foo]", "(bar)"])
179
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
180
+ end
181
+
182
+ it "adds an object to the inputs by its escaped bucket and key" do
183
+ bucket = Riak::Bucket.new(client, "[foo]")
184
+ obj = Riak::RObject.new(bucket, "(bar)")
185
+ mr.add(obj)
186
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
187
+ end
188
+
189
+ it "adds an escaped array containing a bucket/key/key-data triple to the inputs" do
190
+ mr.add(["[foo]", "(bar)", "[]()"])
191
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29", "[]()"]])
192
+ end
193
+
194
+ it "uses an escaped bucket name as the single input" do
195
+ mr.add(Riak::Bucket.new(client, "[foo]"))
196
+ expect(mr.inputs).to eq("%5Bfoo%5D")
197
+ mr.add("docs")
198
+ expect(mr.inputs).to eq("docs")
199
+ end
200
+ end
201
+
202
+ context "when url_decoding is true" do
203
+ before { @urldecode, Riak.url_decoding = Riak.url_decoding, true }
204
+ after { Riak.url_decoding = @urldecode }
205
+
206
+ it "adds bucket/key pairs to the inputs with bucket and key unescaped" do
207
+ mr.add("[foo]", "(bar)")
208
+ expect(mr.inputs).to eq([["[foo]", "(bar)"]])
209
+ end
210
+
211
+ it "adds an unescaped array containing a bucket/key pair to the inputs" do
212
+ mr.add(["[foo]", "(bar)"])
213
+ expect(mr.inputs).to eq([["[foo]", "(bar)"]])
214
+ end
215
+
216
+ it "adds an object to the inputs by its unescaped bucket and key" do
217
+ bucket = Riak::Bucket.new(client, "[foo]")
218
+ obj = Riak::RObject.new(bucket, "(bar)")
219
+ mr.add(obj)
220
+ expect(mr.inputs).to eq([["[foo]", "(bar)"]])
221
+ end
222
+
223
+ it "adds an unescaped array containing a bucket/key/key-data triple to the inputs" do
224
+ mr.add(["[foo]", "(bar)", "[]()"])
225
+ expect(mr.inputs).to eq([["[foo]", "(bar)", "[]()"]])
226
+ end
227
+
228
+ it "uses an unescaped bucket name as the single input" do
229
+ mr.add(Riak::Bucket.new(client, "[foo]"))
230
+ expect(mr.inputs).to eq("[foo]")
231
+ mr.add("docs")
232
+ expect(mr.inputs).to eq("docs")
233
+ end
234
+ end
235
+ end
236
+
237
+ context "escaping" do
238
+ before { @oldesc, Riak.escaper = Riak.escaper, CGI }
239
+ after { Riak.escaper = @oldesc }
240
+
241
+ it "adds bucket/key pairs to the inputs with bucket and key escaped" do
242
+ mr.add("[foo]", "(bar)")
243
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
244
+ end
245
+
246
+ it "adds an escaped array containing a bucket/key pair to the inputs" do
247
+ mr.add(["[foo]", "(bar)"])
248
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
249
+ end
250
+
251
+ it "adds an object to the inputs by its escaped bucket and key" do
252
+ bucket = Riak::Bucket.new(client, "[foo]")
253
+ obj = Riak::RObject.new(bucket, "(bar)")
254
+ mr.add(obj)
255
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
256
+ end
257
+
258
+ it "adds an escaped array containing a bucket/key/key-data triple to the inputs" do
259
+ mr.add(["[foo]", "(bar)", "[]()"])
260
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29", "[]()"]])
261
+ end
262
+
263
+ it "uses an escaped bucket name as the single input" do
264
+ mr.add(Riak::Bucket.new(client, "[foo]"))
265
+ expect(mr.inputs).to eq("%5Bfoo%5D")
266
+ mr.add("docs")
267
+ expect(mr.inputs).to eq("docs")
268
+ end
269
+ end
270
+
271
+ context "when adding an input that will result in full-bucket mapreduce" do
272
+ before { Riak.disable_list_keys_warnings = false }
273
+ after { Riak.disable_list_keys_warnings = true }
274
+
275
+ it "warns about list-keys on buckets" do
276
+ expect(mr).to receive(:warn).twice
277
+ mr.add("foo")
278
+ mr.add(Riak::Bucket.new(client, "foo"))
279
+ end
280
+
281
+ it "warns about list-keys on key-filters" do
282
+ expect(mr).to receive(:warn)
283
+ mr.filter("foo") { matches "bar" }
284
+ end
285
+ end
286
+ end
287
+
288
+ [:map, :reduce].each do |type|
289
+ describe "adding #{type} phases" do
290
+ it "returns self for chaining" do
291
+ expect(mr.send(type, "function(){}")).to eq(mr)
292
+ end
293
+
294
+ it "accepts a function string" do
295
+ mr.send(type, "function(){}")
296
+ expect(mr.query.size).to eq(1)
297
+ phase = mr.query.first
298
+ expect(phase.function).to eq("function(){}")
299
+ expect(phase.type).to eq(type)
300
+ end
301
+
302
+ it "accepts a function and options" do
303
+ mr.send(type, "function(){}", :keep => true)
304
+ expect(mr.query.size).to eq(1)
305
+ phase = mr.query.first
306
+ expect(phase.function).to eq("function(){}")
307
+ expect(phase.type).to eq(type)
308
+ expect(phase.keep).to be_truthy
309
+ end
310
+
311
+ it "accepts a module/function pair" do
312
+ mr.send(type, %w(riak mapsomething))
313
+ expect(mr.query.size).to eq(1)
314
+ phase = mr.query.first
315
+ expect(phase.function).to eq(%w(riak mapsomething))
316
+ expect(phase.type).to eq(type)
317
+ expect(phase.language).to eq("erlang")
318
+ end
319
+
320
+ it "accepts a module/function pair with extra options" do
321
+ mr.send(type, %w(riak mapsomething), :arg => [1000])
322
+ expect(mr.query.size).to eq(1)
323
+ phase = mr.query.first
324
+ expect(phase.function).to eq(%w(riak mapsomething))
325
+ expect(phase.type).to eq(type)
326
+ expect(phase.language).to eq("erlang")
327
+ expect(phase.arg).to eq([1000])
328
+ end
329
+ end
330
+ end
331
+
332
+ describe "adding link phases" do
333
+ it "returns self for chaining" do
334
+ expect(mr.link({})).to eq(mr)
335
+ end
336
+
337
+ it "accepts a WalkSpec" do
338
+ mr.link(Riak::WalkSpec.new(:tag => "next"))
339
+ expect(mr.query.size).to eq(1)
340
+ phase = mr.query.first
341
+ expect(phase.type).to eq(:link)
342
+ expect(phase.function).to be_kind_of(Riak::WalkSpec)
343
+ expect(phase.function.tag).to eq("next")
344
+ end
345
+
346
+ it "accepts a WalkSpec and a hash of options" do
347
+ mr.link(Riak::WalkSpec.new(:bucket => "foo"), :keep => true)
348
+ expect(mr.query.size).to eq(1)
349
+ phase = mr.query.first
350
+ expect(phase.type).to eq(:link)
351
+ expect(phase.function).to be_kind_of(Riak::WalkSpec)
352
+ expect(phase.function.bucket).to eq("foo")
353
+ expect(phase.keep).to be_truthy
354
+ end
355
+
356
+ it "accepts a hash of options intermingled with the walk spec options" do
357
+ mr.link(:tag => "snakes", :arg => [1000])
358
+ expect(mr.query.size).to eq(1)
359
+ phase = mr.query.first
360
+ expect(phase.arg).to eq([1000])
361
+ expect(phase.function).to be_kind_of(Riak::WalkSpec)
362
+ expect(phase.function.tag).to eq("snakes")
363
+ end
364
+ end
365
+
366
+ describe "converting to JSON for the job" do
367
+ it "includes the inputs and query keys" do
368
+ expect(mr.to_json).to match(/"inputs":/)
369
+ end
370
+
371
+ it "maps phases to their JSON equivalents" do
372
+ phase = Riak::MapReduce::Phase.new(:type => :map, :function => "function(){}")
373
+ mr.query << phase
374
+ expect(mr.to_json).to include('"source":"function(){}"')
375
+ expect(mr.to_json).to include('"query":[{"map":{')
376
+ end
377
+
378
+ it "emits only the bucket name when the input is the whole bucket" do
379
+ mr.add("foo")
380
+ expect(mr.to_json).to include('"inputs":"foo"')
381
+ end
382
+
383
+ it "emits an array of inputs when there are multiple inputs" do
384
+ mr.add("foo", "bar", 1000).add("foo", "baz")
385
+ expect(mr.to_json).to include('"inputs":[["foo","bar",1000],["foo","baz"]]')
386
+ end
387
+
388
+ it "adds the timeout value when set" do
389
+ mr.timeout(50000)
390
+ expect(mr.to_json).to include('"timeout":50000')
391
+ end
392
+ end
393
+
394
+ it "returns self from setting the timeout" do
395
+ expect(mr.timeout(5000)).to eq(mr)
396
+ end
397
+
398
+ describe "executing the map reduce job" do
399
+ before :each do
400
+ mr.map("Riak.mapValues", :keep => true)
401
+ end
402
+
403
+ it "submits the query to the backend" do
404
+ expect(backend).to receive(:mapred).with(mr).and_return([])
405
+ expect(mr.run).to eq([])
406
+ end
407
+
408
+ it "passes the given block to the backend for streaming" do
409
+ arr = []
410
+ expect(backend).to receive(:mapred).with(mr).and_yield("foo").and_yield("bar")
411
+ mr.run {|v| arr << v }
412
+ expect(arr).to eq(%w(foo bar))
413
+ end
414
+
415
+ it "interprets failed requests with JSON content-types as map reduce errors" do
416
+ allow(backend).to receive(:mapred).
417
+ and_raise(Riak::ProtobuffsFailedRequest.new(:server_error, '{"error":"syntax error"}'))
418
+ expect{ mr.run }.to raise_error(Riak::MapReduceError)
419
+ begin
420
+ mr.run
421
+ rescue Riak::MapReduceError => mre
422
+ expect(mre.message).to include('{"error":"syntax error"}')
423
+ else
424
+ fail "No exception raised!"
425
+ end
426
+ end
427
+
428
+ it "re-raises non-JSON error responses" do
429
+ allow(backend).to receive(:mapred).
430
+ and_raise(Riak::ProtobuffsFailedRequest.new(:server_error, 'Oops, you bwoke it.'))
431
+ expect { mr.run }.to raise_error(Riak::FailedRequest)
432
+ end
433
+ end
434
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Riak::Multiget do
4
+ before :each do
5
+ @client = Riak::Client.new
6
+ @bucket = Riak::Bucket.new(@client, 'foo')
7
+ @pairs = [[@bucket, 'key1'], [@bucket, 'key2']]
8
+ end
9
+
10
+ describe "initialization" do
11
+ it "accepts a client and an array of bucket/key pairs" do
12
+ expect { Riak::Multiget.new(@client, @pairs) }.not_to raise_error
13
+ end
14
+ end
15
+
16
+ describe "operation" do
17
+ it "fetches both keys from the bucket" do
18
+ expect(@bucket).to receive(:[]).with('key1')
19
+ expect(@bucket).to receive(:[]).with('key2')
20
+
21
+ @multiget = Riak::Multiget.new(@client, @pairs)
22
+ @multiget.fetch
23
+ @multiget.wait_for_finish
24
+ end
25
+
26
+ it "fetches asynchronously" do
27
+ # make fetches slow
28
+ @slow_mtx = Mutex.new
29
+ @slow_mtx.lock
30
+
31
+ # set up fetch process to wait on key2
32
+ expect(@bucket).to receive(:[]) { |key|
33
+ next if key == 'key1'
34
+
35
+ # wait for test process
36
+ @slow_mtx.lock
37
+ }.twice
38
+
39
+ # start fetch process
40
+ @multiget = Riak::Multiget.new(@client, @pairs)
41
+ @multiget.fetch
42
+
43
+ expect(@multiget.finished?).to be_falsey
44
+
45
+ # allow fetch
46
+ @slow_mtx.unlock
47
+
48
+ @results = @multiget.results
49
+ expect(@multiget.finished?).to be_truthy
50
+ expect(@results).to be_a Hash
51
+ end
52
+
53
+ it "returns found objects when only some objects are found" do
54
+ expect(@bucket).to receive(:[]).
55
+ with('key1').
56
+ and_raise(Riak::ProtobuffsFailedRequest.new(:not_found, "not found"))
57
+
58
+ expect(@bucket).to receive(:[]).
59
+ with('key2').
60
+ and_return(true)
61
+
62
+ @results = Riak::Multiget.get_all @client, @pairs
63
+
64
+ expect(@results[[@bucket, 'key1']]).to be_nil
65
+ expect(@results[[@bucket, 'key2']]).to be_truthy
66
+ end
67
+ end
68
+
69
+ describe "results" do
70
+ it "returns a hash of pairs to values" do
71
+ expect(@bucket).to receive(:[]).with('key1')
72
+ expect(@bucket).to receive(:[]).with('key2')
73
+
74
+ @multiget = Riak::Multiget.new(@client, @pairs)
75
+ @multiget.fetch
76
+ @results = @multiget.results
77
+
78
+ expect(@results).to be_a Hash
79
+ end
80
+ end
81
+ end