Skip to content

Commit b881172

Browse files
author
Luke Bakken
committed
Add ListException to BucketMapReduce
1 parent f513522 commit b881172

File tree

2 files changed

+99
-21
lines changed

2 files changed

+99
-21
lines changed

src/main/java/com/basho/riak/client/api/commands/mapreduce/BucketMapReduce.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.basho.riak.client.api.commands.mapreduce;
22

3+
import com.basho.riak.client.api.ListException;
34
import com.basho.riak.client.core.query.Namespace;
45
import com.basho.riak.client.api.commands.mapreduce.filters.KeyFilter;
56

@@ -13,9 +14,14 @@
1314
*/
1415
public class BucketMapReduce extends MapReduce
1516
{
16-
protected BucketMapReduce(BucketInput input, Builder builder)
17+
protected BucketMapReduce(BucketInput input, Builder builder) throws ListException
1718
{
1819
super(input, builder);
20+
21+
if (builder.allowListing == false)
22+
{
23+
throw new ListException();
24+
}
1925
}
2026

2127
/**
@@ -25,13 +31,29 @@ public static class Builder extends MapReduce.Builder<Builder>
2531
{
2632
private Namespace namespace;
2733
private final List<KeyFilter> filters = new ArrayList<>();
34+
private boolean allowListing;
2835

2936
@Override
3037
protected Builder self()
3138
{
3239
return this;
3340
}
3441

42+
/**
43+
* Allow this listing command
44+
* <p>
45+
* Bucket and key list operations are expensive and should not
46+
* be used in production, however using this method will allow
47+
* the command to be built.
48+
* </p>
49+
* @return a reference to this object.
50+
*/
51+
public Builder withAllowListing()
52+
{
53+
this.allowListing = true;
54+
return this;
55+
}
56+
3557
public Builder withNamespace(Namespace namespace)
3658
{
3759
this.namespace = namespace;
@@ -44,7 +66,7 @@ public Builder withKeyFilter(KeyFilter filter)
4466
return this;
4567
}
4668

47-
public BucketMapReduce build()
69+
public BucketMapReduce build() throws ListException
4870
{
4971
if (namespace == null)
5072
{

src/test/java/com/basho/riak/client/api/commands/itest/ITestBucketMapReduce.java

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.basho.riak.client.api.commands.itest;
1818

19+
import com.basho.riak.client.api.ListException;
1920
import com.basho.riak.client.api.RiakClient;
2021
import com.basho.riak.client.core.RiakFuture;
2122
import com.basho.riak.client.api.commands.buckets.StoreBucketProperties;
@@ -42,6 +43,7 @@
4243
import static org.junit.Assert.assertEquals;
4344
import static org.junit.Assert.assertFalse;
4445
import static org.junit.Assert.assertTrue;
46+
import static org.junit.Assert.fail;
4547

4648
/**
4749
*
@@ -134,13 +136,21 @@ public void erlangBucketMRTestTypeStreaming() throws InterruptedException, Execu
134136
private void erlangBucketMR(String bucketType) throws InterruptedException, ExecutionException
135137
{
136138
Namespace ns = new Namespace(bucketType, mrBucketName);
137-
BucketMapReduce bmr =
138-
new BucketMapReduce.Builder()
139+
BucketMapReduce bmr = null;
140+
try
141+
{
142+
bmr = new BucketMapReduce.Builder()
143+
.withAllowListing()
139144
.withNamespace(ns)
140145
.withMapPhase(Function.newErlangFunction("riak_kv_mapreduce", "map_object_value"), false)
141146
.withReducePhase(Function.newErlangFunction("riak_kv_mapreduce", "reduce_string_to_integer"), false)
142147
.withReducePhase(Function.newErlangFunction("riak_kv_mapreduce", "reduce_sort"), true)
143148
.build();
149+
}
150+
catch (ListException ex)
151+
{
152+
fail(ex.getMessage());
153+
}
144154

145155
MapReduce.Response response = client.execute(bmr);
146156

@@ -157,13 +167,21 @@ private void erlangBucketMR(String bucketType) throws InterruptedException, Exec
157167
private void streamingErlangBucketMR(String bucketType) throws InterruptedException, ExecutionException
158168
{
159169
Namespace ns = new Namespace(bucketType, mrBucketName);
160-
BucketMapReduce bmr =
161-
new BucketMapReduce.Builder()
162-
.withNamespace(ns)
163-
.withMapPhase(Function.newErlangFunction("riak_kv_mapreduce", "map_object_value"), false)
164-
.withReducePhase(Function.newErlangFunction("riak_kv_mapreduce", "reduce_string_to_integer"), false)
165-
.withReducePhase(Function.newErlangFunction("riak_kv_mapreduce", "reduce_sort"), true)
166-
.build();
170+
BucketMapReduce bmr = null;
171+
try
172+
{
173+
bmr = new BucketMapReduce.Builder()
174+
.withAllowListing()
175+
.withNamespace(ns)
176+
.withMapPhase(Function.newErlangFunction("riak_kv_mapreduce", "map_object_value"), false)
177+
.withReducePhase(Function.newErlangFunction("riak_kv_mapreduce", "reduce_string_to_integer"), false)
178+
.withReducePhase(Function.newErlangFunction("riak_kv_mapreduce", "reduce_sort"), true)
179+
.build();
180+
}
181+
catch (ListException ex)
182+
{
183+
fail(ex.getMessage());
184+
}
167185

168186
final RiakFuture<MapReduce.Response, BinaryValue> streamingFuture =
169187
client.executeAsyncStreaming(bmr, 10);
@@ -231,12 +249,20 @@ public void JsBucketMRTestType() throws InterruptedException, ExecutionException
231249
private void JsBucketMR(String bucketType) throws InterruptedException, ExecutionException
232250
{
233251
Namespace ns = new Namespace(bucketType, mrBucketName);
234-
BucketMapReduce bmr =
235-
new BucketMapReduce.Builder()
252+
BucketMapReduce bmr = null;
253+
try
254+
{
255+
bmr = new BucketMapReduce.Builder()
256+
.withAllowListing()
236257
.withNamespace(ns)
237258
.withMapPhase(Function.newNamedJsFunction("Riak.mapValuesJson"), false)
238259
.withReducePhase(Function.newNamedJsFunction("Riak.reduceNumericSort"), true)
239260
.build();
261+
}
262+
catch (ListException ex)
263+
{
264+
fail(ex.getMessage());
265+
}
240266

241267
RiakFuture<MapReduce.Response, BinaryValue> future = client.executeAsync(bmr);
242268

@@ -259,12 +285,20 @@ private void JsBucketMR(String bucketType) throws InterruptedException, Executio
259285
public void multiPhaseResult() throws InterruptedException, ExecutionException
260286
{
261287
Namespace ns = new Namespace(Namespace.DEFAULT_BUCKET_TYPE, mrBucketName);
262-
BucketMapReduce bmr =
263-
new BucketMapReduce.Builder()
288+
BucketMapReduce bmr = null;
289+
try
290+
{
291+
bmr = new BucketMapReduce.Builder()
292+
.withAllowListing()
264293
.withNamespace(ns)
265294
.withMapPhase(Function.newNamedJsFunction("Riak.mapValuesJson"), true)
266295
.withReducePhase(Function.newNamedJsFunction("Riak.reduceNumericSort"), true)
267296
.build();
297+
}
298+
catch (ListException ex)
299+
{
300+
fail(ex.getMessage());
301+
}
268302

269303
RiakFuture<MapReduce.Response, BinaryValue> future = client.executeAsync(bmr);
270304

@@ -288,15 +322,23 @@ public void multiPhaseResult() throws InterruptedException, ExecutionException
288322
public void keyFilter() throws InterruptedException, ExecutionException
289323
{
290324
Namespace ns = new Namespace(Namespace.DEFAULT_BUCKET_TYPE, mrBucketName);
291-
BucketMapReduce bmr =
292-
new BucketMapReduce.Builder()
325+
BucketMapReduce bmr = null;
326+
try
327+
{
328+
bmr = new BucketMapReduce.Builder()
329+
.withAllowListing()
293330
.withNamespace(ns)
294331
.withMapPhase(Function.newNamedJsFunction("Riak.mapValuesJson"))
295332
.withReducePhase(Function.newErlangFunction("riak_kv_mapreduce", "reduce_sort"),true)
296333
.withKeyFilter(new TokenizeFilter("_",3))
297334
.withKeyFilter(new StringToIntFilter())
298335
.withKeyFilter(new LogicalAndFilter(new LessThanFilter<>(50), new GreaterThanFilter<>(45)))
299336
.build();
337+
}
338+
catch (ListException ex)
339+
{
340+
fail(ex.getMessage());
341+
}
300342

301343
RiakFuture<MapReduce.Response, BinaryValue> future = client.executeAsync(bmr);
302344

@@ -315,8 +357,10 @@ public void differentBucketType() throws InterruptedException, ExecutionExceptio
315357
Assume.assumeTrue(testBucketType);
316358

317359
Namespace ns = new Namespace(mapReduceBucketType.toString(), mrBucketName);
318-
BucketMapReduce bmr =
319-
new BucketMapReduce.Builder()
360+
BucketMapReduce bmr = null;
361+
try
362+
{
363+
bmr = new BucketMapReduce.Builder()
320364
.withNamespace(ns)
321365
.withMapPhase(Function.newAnonymousJsFunction(
322366
"function(value, keydata, arg) {" +
@@ -327,6 +371,11 @@ public void differentBucketType() throws InterruptedException, ExecutionExceptio
327371
" return[];" +
328372
"}"), true)
329373
.build();
374+
}
375+
catch (ListException ex)
376+
{
377+
fail(ex.getMessage());
378+
}
330379

331380
MapReduce.Response response = client.execute(bmr);
332381

@@ -339,8 +388,10 @@ public void differentBucketTypeWithFilter() throws InterruptedException, Executi
339388
Assume.assumeTrue(testBucketType);
340389

341390
Namespace ns = new Namespace(mapReduceBucketType.toString(), mrBucketName);
342-
BucketMapReduce bmr =
343-
new BucketMapReduce.Builder()
391+
BucketMapReduce bmr = null;
392+
try
393+
{
394+
bmr = new BucketMapReduce.Builder()
344395
.withNamespace(ns)
345396
.withKeyFilter(new TokenizeFilter("_",3))
346397
.withKeyFilter(new StringToIntFilter())
@@ -351,6 +402,11 @@ public void differentBucketTypeWithFilter() throws InterruptedException, Executi
351402
" return [data];" +
352403
"}"), true)
353404
.build();
405+
}
406+
catch (ListException ex)
407+
{
408+
fail(ex.getMessage());
409+
}
354410

355411
RiakFuture<MapReduce.Response, BinaryValue> future = client.executeAsync(bmr);
356412

0 commit comments

Comments
 (0)