Skip to content

Commit

Permalink
Merge pull request #693 from basho/am/features/CLIENTS-1057-GSet-Support
Browse files Browse the repository at this point in the history
GSet Support
  • Loading branch information
alexmoore authored Feb 14, 2017
2 parents cb678da + e9b358c commit 33d3648
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 56 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,21 @@ com.basho.riak.yokozuna | true | Riak KV 2.0 Solr/Yokozuna Search Tests
com.basho.riak.2i | true | Riak KV Secondary Index Tests
com.basho.riak.mr | true | Riak KV MapReduce Tests
com.basho.riak.crdt | true | Riak KV 2.0 Data Type Tests
com.basho.riak.hlldt | true | Riak KV 2.2 HyperLogLog Data Type Tests
com.basho.riak.lifecycle | true | Java Client Node/Cluster Lifecycle Tests
com.basho.riak.timeseries | false | Riak TS TimeSeries Tests
com.basho.riak.riakSearch | false | Riak KV 1.0 Legacy Search Tests
com.basho.riak.coveragePlan | false | Riak KV/TS Coverage Plan Tests <br>(need cluster to run these )
com.basho.riak.security | false | Riak Security Tests
com.basho.riak.clientcert | false | Riak Security Tests with Certificates

To run the HyperLogLog or GSet Data Type tests, you must have two test bucket types setup as following:
```
riak-admin bucket-type create gsets '{"props":{"allow_mult":true, "datatype": "gset"}}'
riak-admin bucket-type create hlls '{"props":{"allow_mult":true, "datatype": "hll"}}'
riak-admin bucket-type activate gsets
riak-admin bucket-type activate hlls
```

Some tests may require more than one feature to run, so please check the test to see which ones are required before running.

Connection Options
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2016 Basho Technologies Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.basho.riak.client.api.commands.datatypes;

import com.basho.riak.client.core.query.crdt.ops.GSetOp;
import com.basho.riak.client.core.query.crdt.ops.SetOp;
import com.basho.riak.client.core.util.BinaryValue;

import java.util.HashSet;
import java.util.Set;

/**
* An update to a Riak grow-only set datatype.
* <p>
* When building an {@link UpdateSet} command
* this class is used to encapsulate the update to be performed on a
* Riak gset datatype.
* </p>
* @author Alex Moore <amoore at basho dot com>
* @since 2.2
*/
public class GSetUpdate implements DatatypeUpdate
{
protected final Set<BinaryValue> adds = new HashSet<>();

/**
* Constructs an empty GSetUpdate.
*/
public GSetUpdate()
{
}

/**
* Add the provided value to the set in Riak.
* @param value the value to be added.
* @return a reference to this object.
*/
public GSetUpdate add(BinaryValue value)
{
this.adds.add(value);
return this;
}

/**
* Add the provided value to the set in Riak.
* @param value the value to be added.
* @return a reference to this object.
*/
public GSetUpdate add(String value)
{
this.adds.add(BinaryValue.create(value));
return this;
}

/**
* Get the set of additions contained in this update.
* @return the set of additions.
*/
public Set<BinaryValue> getAdds()
{
return adds;
}

/**
* Returns the core update.
* @return the update used by the client core.
*/
@Override
public GSetOp getOp()
{
return new GSetOp(adds);
}

@Override
public String toString()
{
return "Add: " + adds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
* @author Dave Rusek <drusek at basho dot com>
* @since 2.0
*/
public class SetUpdate implements DatatypeUpdate
public class SetUpdate extends GSetUpdate
{
private final Set<BinaryValue> adds = new HashSet<>();
private final Set<BinaryValue> removes = new HashSet<>();

/**
Expand All @@ -43,28 +42,6 @@ public SetUpdate()
{
}

/**
* Add the provided value to the set in Riak.
* @param value the value to be added.
* @return a reference to this object.
*/
public SetUpdate add(BinaryValue value)
{
this.adds.add(value);
return this;
}

/**
* Add the provided value to the set in Riak.
* @param value the value to be added.
* @return a reference to this object.
*/
public SetUpdate add(String value)
{
this.adds.add(BinaryValue.create(value));
return this;
}

/**
* Remove the provided value from the set in Riak.
* @param value the value to be removed.
Expand All @@ -87,15 +64,6 @@ public SetUpdate remove(String value)
return this;
}

/**
* Get the set of additions contained in this update.
* @return the set of additions.
*/
public Set<BinaryValue> getAdds()
{
return adds;
}

/**
* Get the set of removes contained in this update.
* @return the set of removes.
Expand All @@ -120,4 +88,5 @@ public String toString()
{
return "Add: " + adds + " Remove: " + removes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public T withTimeout(int timeout)
*/
public T withReturnDatatype(boolean returnDatatype)
{
withOption(Option.RETURN_BODY, true);
withOption(Option.RETURN_BODY, returnDatatype);
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ protected Response convertResponse(FutureOperation<DtUpdateOperation.Response, ?
}

/**
* Builder used to construct an UpdateSet command.
* Builder used to construct an UpdateSet command..
*/
public static class Builder extends UpdateDatatype.Builder<Builder>
{
/**
* Construct a Builder for an UpdateSet command.
* @param location the location of the set in Riak.
* @param update the update to apply to the set.
*/
public Builder(Location location, GSetUpdate update)
{
super(location, update);
if (update == null)
{
throw new IllegalArgumentException("Update cannot be null");
}
}

/**
* Construct a Builder for an UpdateSet command.
* @param location the location of the set in Riak.
Expand All @@ -94,6 +108,26 @@ public Builder(Location location, SetUpdate update)
}
}

/**
* Constructs a builder for an UpdateSet command with only a Namespace.
* <p>
* By providing only a Namespace with the update, Riak will create the
* set, generate the key,
* and return it in the response.
* </p>
* @param namespace the namespace to create the datatype.
* @param update the update to apply
* @see Response#getGeneratedKey()
*/
public Builder(Namespace namespace, GSetUpdate update)
{
super(namespace, update);
if (update == null)
{
throw new IllegalArgumentException("Update cannot be null");
}
}

/**
* Constructs a builder for an UpdateSet command with only a Namespace.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ else if (response.hasHllValue())
{
element = parseHll(response.getHllValue());
}
else if (response.getGsetValueCount() > 0)
{
element = parseSet(response.getGsetValueList());
}

return element;
}
Expand All @@ -117,6 +121,9 @@ public RiakDatatype convert(RiakDtPB.DtFetchResp response)
case HLL:
element = parseHll(response.getValue().getHllValue());
break;
case GSET:
element = parseSet(response.getValue().getGsetValueList());
break;
default:
throw new IllegalStateException("No known datatype returned");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ RiakDtPB.SetOp getSetOp(SetOp op)
return setOpBuilder.build();
}

RiakDtPB.GSetOp getGSetOp(GSetOp op)
{
RiakDtPB.GSetOp.Builder setOpBuilder = RiakDtPB.GSetOp.newBuilder();

for (BinaryValue element : op.getAdds())
{
setOpBuilder.addAdds(ByteString.copyFrom(element.unsafeGetValue()));
}

return setOpBuilder.build();
}

RiakDtPB.HllOp getHllOp(HllOp op)
{
RiakDtPB.HllOp.Builder hllOpBuilder = RiakDtPB.HllOp.newBuilder();
Expand Down Expand Up @@ -411,6 +423,10 @@ else if (op instanceof SetOp)
{
withOp((SetOp) op);
}
else if (op instanceof GSetOp)
{
withOp((GSetOp) op);
}
else if (op instanceof HllOp)
{
withOp((HllOp) op);
Expand Down Expand Up @@ -442,6 +458,12 @@ private Builder withOp(SetOp op)
return this;
}

private Builder withOp(GSetOp op)
{
reqBuilder.setOp(RiakDtPB.DtOp.newBuilder().setGsetOp(getGSetOp(op)));
return this;
}

private Builder withOp(HllOp op)
{
reqBuilder.setOp(RiakDtPB.DtOp.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2016 Basho Technologies Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.basho.riak.client.core.query.crdt.ops;

import com.basho.riak.client.core.util.BinaryValue;

import java.util.HashSet;
import java.util.Set;

public class GSetOp implements CrdtOp
{
protected final Set<BinaryValue> adds = new HashSet<>();

public GSetOp(Set<BinaryValue> adds)
{
this.adds.addAll(adds);
}

public GSetOp() {}

public GSetOp add(BinaryValue element)
{
this.adds.add(element);
return this;
}

public Set<BinaryValue> getAdds()
{
return adds;
}

@Override
public String toString()
{
return "{Add: " + adds + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import java.util.HashSet;
import java.util.Set;

public class SetOp implements CrdtOp
public class SetOp extends GSetOp
{
private final Set<BinaryValue> adds = new HashSet<>();
private final Set<BinaryValue> removes = new HashSet<>();

public SetOp(Set<BinaryValue> adds, Set<BinaryValue> removes)
Expand All @@ -33,9 +32,10 @@ public SetOp(Set<BinaryValue> adds, Set<BinaryValue> removes)

public SetOp() {}

@Override
public SetOp add(BinaryValue element)
{
this.adds.add(element);
super.add(element);
return this;
}

Expand All @@ -45,11 +45,6 @@ public SetOp remove(BinaryValue element)
return this;
}

public Set<BinaryValue> getAdds()
{
return adds;
}

public Set<BinaryValue> getRemoves()
{
return removes;
Expand Down
Loading

0 comments on commit 33d3648

Please sign in to comment.