Skip to content

Commit

Permalink
Merge pull request #701 from basho/features/lrb/gh-697_CLIENTS-1065
Browse files Browse the repository at this point in the history
Implement exceptions for listing operations.
  • Loading branch information
lukebakken authored Feb 21, 2017
2 parents 33d3648 + 83f7779 commit da63bd5
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 34 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/basho/riak/client/api/ListException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.basho.riak.client.api;

public class ListException extends Exception
{
public ListException()
{
super("Bucket and key list operations are expensive and should not be used in production.");
}

public ListException(Throwable cause)
{
super("Bucket and key list operations are expensive and should not be used in production.", cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.basho.riak.client.api.commands.buckets;

import com.basho.riak.client.api.commands.ChunkedResponseIterator;
import com.basho.riak.client.api.ListException;
import com.basho.riak.client.api.StreamableRiakCommand;
import com.basho.riak.client.core.FutureOperation;
import com.basho.riak.client.core.StreamingRiakFuture;
Expand Down Expand Up @@ -68,10 +69,15 @@ public final class ListBuckets extends StreamableRiakCommand.StreamableRiakComma
private final int timeout;
private final BinaryValue type;

ListBuckets(Builder builder)
ListBuckets(Builder builder) throws ListException
{
this.timeout = builder.timeout;
this.type = builder.type;

if (!builder.allowListing)
{
throw new ListException();
}
}

@Override
Expand Down Expand Up @@ -170,6 +176,7 @@ public static class Builder
{
private int timeout;
private final BinaryValue type;
private boolean allowListing;

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

/**
* Allow this listing command
* <p>
* Bucket and key list operations are expensive and should not
* be used in production, however using this method will allow
* the command to be built.
* </p>
* @return a reference to this object.
*/
public Builder withAllowListing()
{
this.allowListing = true;
return this;
}

/**
* Construct a Builder for a ListBuckets command.
* @param type the bucket type.
Expand Down Expand Up @@ -208,7 +230,7 @@ public Builder withTimeout(int timeout)
* Construct a new ListBuckets command.
* @return a new ListBuckets command.
*/
public ListBuckets build()
public ListBuckets build() throws ListException
{
return new ListBuckets(this);
}
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/com/basho/riak/client/api/commands/kv/ListKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.basho.riak.client.api.commands.kv;

import com.basho.riak.client.api.ListException;
import com.basho.riak.client.api.StreamableRiakCommand;
import com.basho.riak.client.api.commands.ChunkedResponseIterator;
import com.basho.riak.client.core.FutureOperation;
Expand Down Expand Up @@ -78,10 +79,15 @@ public final class ListKeys extends StreamableRiakCommand.StreamableRiakCommandW
private final Namespace namespace;
private final int timeout;

ListKeys(Builder builder)
ListKeys(Builder builder) throws ListException
{
this.namespace = builder.namespace;
this.timeout = builder.timeout;

if (!builder.allowListing)
{
throw new ListException();
}
}

@Override
Expand Down Expand Up @@ -163,6 +169,7 @@ public static class Builder
{
private final Namespace namespace;
private int timeout;
private boolean allowListing;

/**
* Constructs a Builder for a ListKeys command.
Expand All @@ -177,6 +184,21 @@ public Builder(Namespace namespace)
this.namespace = namespace;
}

/**
* Allow this listing command
* <p>
* Bucket and key list operations are expensive and should not
* be used in production, however using this method will allow
* the command to be built.
* </p>
* @return a reference to this object.
*/
public Builder withAllowListing()
{
this.allowListing = true;
return this;
}

/**
* Set the Riak-side timeout value.
* <p>
Expand All @@ -196,7 +218,7 @@ public Builder withTimeout(int timeout)
* Construct the ListKeys command.
* @return A ListKeys command.
*/
public ListKeys build()
public ListKeys build() throws ListException
{
return new ListKeys(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.basho.riak.client.api.commands.mapreduce;

import com.basho.riak.client.api.ListException;
import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.api.commands.mapreduce.filters.KeyFilter;

Expand All @@ -13,9 +14,14 @@
*/
public class BucketMapReduce extends MapReduce
{
protected BucketMapReduce(BucketInput input, Builder builder)
protected BucketMapReduce(BucketInput input, Builder builder) throws ListException
{
super(input, builder);

if (!builder.allowListing)
{
throw new ListException();
}
}

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

@Override
protected Builder self()
{
return this;
}

/**
* Allow this listing command
* <p>
* Bucket and key list operations are expensive and should not
* be used in production, however using this method will allow
* the command to be built.
* </p>
* @return a reference to this object.
*/
public Builder withAllowListing()
{
this.allowListing = true;
return this;
}

public Builder withNamespace(Namespace namespace)
{
this.namespace = namespace;
Expand All @@ -44,7 +66,7 @@ public Builder withKeyFilter(KeyFilter filter)
return this;
}

public BucketMapReduce build()
public BucketMapReduce build() throws ListException
{
if (namespace == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.basho.riak.client.api.commands.timeseries;

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

private ListKeys(ListKeys.Builder builder)
private ListKeys(ListKeys.Builder builder) throws ListException
{
this.tableName = builder.tableName;
this.timeout = builder.timeout;

if (!builder.allowListing)
{
throw new ListException();
}
}

@Override
Expand All @@ -43,6 +49,7 @@ public static class Builder
{
private final String tableName;
private int timeout;
private boolean allowListing;

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

/**
* Allow this listing command
* <p>
* Bucket and key list operations are expensive and should not
* be used in production, however using this method will allow
* the command to be built.
* </p>
* @return a reference to this object.
*/
public Builder withAllowListing()
{
this.allowListing = true;
return this;
}

/**
* Set the Riak-side timeout value.
* <p>
Expand All @@ -77,7 +99,7 @@ public Builder withTimeout(int timeout)
* Construct a Time Series ListKeys object.
* @return a new Time Series ListKeys instance.
*/
public ListKeys build()
public ListKeys build() throws ListException
{
return new ListKeys(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.basho.riak.client.api.commands.buckets;

import com.basho.riak.client.api.ListException;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.core.*;
import com.basho.riak.client.core.operations.ListBucketsOperation;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void init() throws Exception
private void testListBuckets(String bucketType) throws Exception
{
final BinaryValue type = BinaryValue.createFromUtf8(bucketType);
ListBuckets.Builder list = new ListBuckets.Builder(type);
ListBuckets.Builder list = new ListBuckets.Builder(type).withAllowListing();
client.execute(list.build());

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

@Test(expected = ListException.class)
public void listBucketsThrowsListException() throws ListException
{
ListBuckets.Builder b = new ListBuckets.Builder("bucket_type");
b.build();
}

@Test
public void bucketTypeBuiltCorrectly() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import com.basho.riak.client.api.ListException;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.buckets.ListBuckets;
import com.basho.riak.client.api.commands.kv.StoreValue;
Expand All @@ -35,6 +36,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

/**
Expand Down Expand Up @@ -91,7 +93,15 @@ public void testListBucketsStreamingTestType() throws InterruptedException, Exec

private void testListBuckets(Namespace namespace) throws InterruptedException, ExecutionException
{
ListBuckets listBucketsCommand = new ListBuckets.Builder(namespace.getBucketType()).build();
ListBuckets listBucketsCommand = null;
try
{
listBucketsCommand = new ListBuckets.Builder(namespace.getBucketType()).withAllowListing().build();
}
catch (ListException ex)
{
fail(ex.getMessage());
}

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

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

private void testListBucketsStreaming(Namespace namespace) throws InterruptedException, ExecutionException
{
ListBuckets listBucketsCommand = new ListBuckets.Builder(namespace.getBucketType()).build();
ListBuckets listBucketsCommand = null;
try
{
listBucketsCommand = new ListBuckets.Builder(namespace.getBucketType()).withAllowListing().build();
}
catch (ListException ex)
{
fail(ex.getMessage());
}

final RiakFuture<ListBuckets.Response, BinaryValue> streamingFuture =
client.executeAsyncStreaming(listBucketsCommand, 500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import com.basho.riak.client.api.ListException;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.kv.ListKeys;
import com.basho.riak.client.core.RiakFuture;
Expand All @@ -34,6 +35,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

/**
Expand Down Expand Up @@ -69,7 +71,15 @@ public void testLargeStreamingListKeys() throws ExecutionException, InterruptedE
{
assumeTrue(testBucketType);

ListKeys lk = new ListKeys.Builder(typedNamespace).build();
ListKeys lk = null;
try
{
lk = new ListKeys.Builder(typedNamespace).withAllowListing().build();
}
catch (ListException ex)
{
fail(ex.getMessage());
}

final RiakFuture<ListKeys.Response, Namespace> streamFuture =
client.executeAsyncStreaming(lk, 200);
Expand Down
Loading

0 comments on commit da63bd5

Please sign in to comment.