Skip to content

Commit da63bd5

Browse files
authored
Merge pull request #701 from basho/features/lrb/gh-697_CLIENTS-1065
Implement exceptions for listing operations.
2 parents 33d3648 + 83f7779 commit da63bd5

11 files changed

Lines changed: 249 additions & 34 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.basho.riak.client.api;
2+
3+
public class ListException extends Exception
4+
{
5+
public ListException()
6+
{
7+
super("Bucket and key list operations are expensive and should not be used in production.");
8+
}
9+
10+
public ListException(Throwable cause)
11+
{
12+
super("Bucket and key list operations are expensive and should not be used in production.", cause);
13+
}
14+
}

src/main/java/com/basho/riak/client/api/commands/buckets/ListBuckets.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.basho.riak.client.api.commands.buckets;
1717

1818
import com.basho.riak.client.api.commands.ChunkedResponseIterator;
19+
import com.basho.riak.client.api.ListException;
1920
import com.basho.riak.client.api.StreamableRiakCommand;
2021
import com.basho.riak.client.core.FutureOperation;
2122
import com.basho.riak.client.core.StreamingRiakFuture;
@@ -68,10 +69,15 @@ public final class ListBuckets extends StreamableRiakCommand.StreamableRiakComma
6869
private final int timeout;
6970
private final BinaryValue type;
7071

71-
ListBuckets(Builder builder)
72+
ListBuckets(Builder builder) throws ListException
7273
{
7374
this.timeout = builder.timeout;
7475
this.type = builder.type;
76+
77+
if (!builder.allowListing)
78+
{
79+
throw new ListException();
80+
}
7581
}
7682

7783
@Override
@@ -170,6 +176,7 @@ public static class Builder
170176
{
171177
private int timeout;
172178
private final BinaryValue type;
179+
private boolean allowListing;
173180

174181
/**
175182
* Construct a Builder for a ListBuckets command.
@@ -180,6 +187,21 @@ public Builder(String type)
180187
this.type = BinaryValue.create(type);
181188
}
182189

190+
/**
191+
* Allow this listing command
192+
* <p>
193+
* Bucket and key list operations are expensive and should not
194+
* be used in production, however using this method will allow
195+
* the command to be built.
196+
* </p>
197+
* @return a reference to this object.
198+
*/
199+
public Builder withAllowListing()
200+
{
201+
this.allowListing = true;
202+
return this;
203+
}
204+
183205
/**
184206
* Construct a Builder for a ListBuckets command.
185207
* @param type the bucket type.
@@ -208,7 +230,7 @@ public Builder withTimeout(int timeout)
208230
* Construct a new ListBuckets command.
209231
* @return a new ListBuckets command.
210232
*/
211-
public ListBuckets build()
233+
public ListBuckets build() throws ListException
212234
{
213235
return new ListBuckets(this);
214236
}

src/main/java/com/basho/riak/client/api/commands/kv/ListKeys.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.basho.riak.client.api.commands.kv;
1717

18+
import com.basho.riak.client.api.ListException;
1819
import com.basho.riak.client.api.StreamableRiakCommand;
1920
import com.basho.riak.client.api.commands.ChunkedResponseIterator;
2021
import com.basho.riak.client.core.FutureOperation;
@@ -78,10 +79,15 @@ public final class ListKeys extends StreamableRiakCommand.StreamableRiakCommandW
7879
private final Namespace namespace;
7980
private final int timeout;
8081

81-
ListKeys(Builder builder)
82+
ListKeys(Builder builder) throws ListException
8283
{
8384
this.namespace = builder.namespace;
8485
this.timeout = builder.timeout;
86+
87+
if (!builder.allowListing)
88+
{
89+
throw new ListException();
90+
}
8591
}
8692

8793
@Override
@@ -163,6 +169,7 @@ public static class Builder
163169
{
164170
private final Namespace namespace;
165171
private int timeout;
172+
private boolean allowListing;
166173

167174
/**
168175
* Constructs a Builder for a ListKeys command.
@@ -177,6 +184,21 @@ public Builder(Namespace namespace)
177184
this.namespace = namespace;
178185
}
179186

187+
/**
188+
* Allow this listing command
189+
* <p>
190+
* Bucket and key list operations are expensive and should not
191+
* be used in production, however using this method will allow
192+
* the command to be built.
193+
* </p>
194+
* @return a reference to this object.
195+
*/
196+
public Builder withAllowListing()
197+
{
198+
this.allowListing = true;
199+
return this;
200+
}
201+
180202
/**
181203
* Set the Riak-side timeout value.
182204
* <p>
@@ -196,7 +218,7 @@ public Builder withTimeout(int timeout)
196218
* Construct the ListKeys command.
197219
* @return A ListKeys command.
198220
*/
199-
public ListKeys build()
221+
public ListKeys build() throws ListException
200222
{
201223
return new ListKeys(this);
202224
}

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)
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/main/java/com/basho/riak/client/api/commands/timeseries/ListKeys.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.timeseries;
22

3+
import com.basho.riak.client.api.ListException;
34
import com.basho.riak.client.api.AsIsRiakCommand;
45
import com.basho.riak.client.core.operations.ts.ListKeysOperation;
56
import com.basho.riak.client.core.query.timeseries.QueryResult;
@@ -17,10 +18,15 @@ public class ListKeys extends AsIsRiakCommand<QueryResult, String>
1718
private final String tableName;
1819
private final int timeout;
1920

20-
private ListKeys(ListKeys.Builder builder)
21+
private ListKeys(ListKeys.Builder builder) throws ListException
2122
{
2223
this.tableName = builder.tableName;
2324
this.timeout = builder.timeout;
25+
26+
if (!builder.allowListing)
27+
{
28+
throw new ListException();
29+
}
2430
}
2531

2632
@Override
@@ -43,6 +49,7 @@ public static class Builder
4349
{
4450
private final String tableName;
4551
private int timeout;
52+
private boolean allowListing;
4653

4754
/**
4855
* Construct a Builder for a Time Series ListKeys command.
@@ -53,6 +60,21 @@ public Builder(String tableName)
5360
this.tableName = tableName;
5461
}
5562

63+
/**
64+
* Allow this listing command
65+
* <p>
66+
* Bucket and key list operations are expensive and should not
67+
* be used in production, however using this method will allow
68+
* the command to be built.
69+
* </p>
70+
* @return a reference to this object.
71+
*/
72+
public Builder withAllowListing()
73+
{
74+
this.allowListing = true;
75+
return this;
76+
}
77+
5678
/**
5779
* Set the Riak-side timeout value.
5880
* <p>
@@ -77,7 +99,7 @@ public Builder withTimeout(int timeout)
7799
* Construct a Time Series ListKeys object.
78100
* @return a new Time Series ListKeys instance.
79101
*/
80-
public ListKeys build()
102+
public ListKeys build() throws ListException
81103
{
82104
return new ListKeys(this);
83105
}

src/test/java/com/basho/riak/client/api/commands/buckets/ListBucketsTest.java

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

3+
import com.basho.riak.client.api.ListException;
34
import com.basho.riak.client.api.RiakClient;
45
import com.basho.riak.client.core.*;
56
import com.basho.riak.client.core.operations.ListBucketsOperation;
@@ -56,7 +57,7 @@ public void init() throws Exception
5657
private void testListBuckets(String bucketType) throws Exception
5758
{
5859
final BinaryValue type = BinaryValue.createFromUtf8(bucketType);
59-
ListBuckets.Builder list = new ListBuckets.Builder(type);
60+
ListBuckets.Builder list = new ListBuckets.Builder(type).withAllowListing();
6061
client.execute(list.build());
6162

6263
ArgumentCaptor<FutureOperation> captor =
@@ -70,6 +71,13 @@ private void testListBuckets(String bucketType) throws Exception
7071
Assert.assertEquals(ByteString.copyFrom(type.unsafeGetValue()), builder.getType());
7172
}
7273

74+
@Test(expected = ListException.class)
75+
public void listBucketsThrowsListException() throws ListException
76+
{
77+
ListBuckets.Builder b = new ListBuckets.Builder("bucket_type");
78+
b.build();
79+
}
80+
7381
@Test
7482
public void bucketTypeBuiltCorrectly() throws Exception
7583
{

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

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

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

19+
import com.basho.riak.client.api.ListException;
1920
import com.basho.riak.client.api.RiakClient;
2021
import com.basho.riak.client.api.commands.buckets.ListBuckets;
2122
import com.basho.riak.client.api.commands.kv.StoreValue;
@@ -35,6 +36,7 @@
3536
import static org.junit.Assert.assertEquals;
3637
import static org.junit.Assert.assertFalse;
3738
import static org.junit.Assert.assertTrue;
39+
import static org.junit.Assert.fail;
3840
import static org.junit.Assume.assumeTrue;
3941

4042
/**
@@ -91,7 +93,15 @@ public void testListBucketsStreamingTestType() throws InterruptedException, Exec
9193

9294
private void testListBuckets(Namespace namespace) throws InterruptedException, ExecutionException
9395
{
94-
ListBuckets listBucketsCommand = new ListBuckets.Builder(namespace.getBucketType()).build();
96+
ListBuckets listBucketsCommand = null;
97+
try
98+
{
99+
listBucketsCommand = new ListBuckets.Builder(namespace.getBucketType()).withAllowListing().build();
100+
}
101+
catch (ListException ex)
102+
{
103+
fail(ex.getMessage());
104+
}
95105

96106
final ListBuckets.Response listResponse = client.execute(listBucketsCommand);
97107

@@ -109,7 +119,15 @@ private void testListBuckets(Namespace namespace) throws InterruptedException, E
109119

110120
private void testListBucketsStreaming(Namespace namespace) throws InterruptedException, ExecutionException
111121
{
112-
ListBuckets listBucketsCommand = new ListBuckets.Builder(namespace.getBucketType()).build();
122+
ListBuckets listBucketsCommand = null;
123+
try
124+
{
125+
listBucketsCommand = new ListBuckets.Builder(namespace.getBucketType()).withAllowListing().build();
126+
}
127+
catch (ListException ex)
128+
{
129+
fail(ex.getMessage());
130+
}
113131

114132
final RiakFuture<ListBuckets.Response, BinaryValue> streamingFuture =
115133
client.executeAsyncStreaming(listBucketsCommand, 500);

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import com.basho.riak.client.api.ListException;
1920
import com.basho.riak.client.api.RiakClient;
2021
import com.basho.riak.client.api.commands.kv.ListKeys;
2122
import com.basho.riak.client.core.RiakFuture;
@@ -34,6 +35,7 @@
3435

3536
import static org.junit.Assert.assertEquals;
3637
import static org.junit.Assert.assertTrue;
38+
import static org.junit.Assert.fail;
3739
import static org.junit.Assume.assumeTrue;
3840

3941
/**
@@ -69,7 +71,15 @@ public void testLargeStreamingListKeys() throws ExecutionException, InterruptedE
6971
{
7072
assumeTrue(testBucketType);
7173

72-
ListKeys lk = new ListKeys.Builder(typedNamespace).build();
74+
ListKeys lk = null;
75+
try
76+
{
77+
lk = new ListKeys.Builder(typedNamespace).withAllowListing().build();
78+
}
79+
catch (ListException ex)
80+
{
81+
fail(ex.getMessage());
82+
}
7383

7484
final RiakFuture<ListKeys.Response, Namespace> streamFuture =
7585
client.executeAsyncStreaming(lk, 200);

0 commit comments

Comments
 (0)