Skip to content

Commit 33d3648

Browse files
authored
Merge pull request #693 from basho/am/features/CLIENTS-1057-GSet-Support
GSet Support
2 parents cb678da + e9b358c commit 33d3648

11 files changed

Lines changed: 287 additions & 56 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,21 @@ com.basho.riak.yokozuna | true | Riak KV 2.0 Solr/Yokozuna Search Tests
9999
com.basho.riak.2i | true | Riak KV Secondary Index Tests
100100
com.basho.riak.mr | true | Riak KV MapReduce Tests
101101
com.basho.riak.crdt | true | Riak KV 2.0 Data Type Tests
102-
com.basho.riak.hlldt | true | Riak KV 2.2 HyperLogLog Data Type Tests
103102
com.basho.riak.lifecycle | true | Java Client Node/Cluster Lifecycle Tests
104103
com.basho.riak.timeseries | false | Riak TS TimeSeries Tests
105104
com.basho.riak.riakSearch | false | Riak KV 1.0 Legacy Search Tests
106105
com.basho.riak.coveragePlan | false | Riak KV/TS Coverage Plan Tests <br>(need cluster to run these )
107106
com.basho.riak.security | false | Riak Security Tests
108107
com.basho.riak.clientcert | false | Riak Security Tests with Certificates
109108

109+
To run the HyperLogLog or GSet Data Type tests, you must have two test bucket types setup as following:
110+
```
111+
riak-admin bucket-type create gsets '{"props":{"allow_mult":true, "datatype": "gset"}}'
112+
riak-admin bucket-type create hlls '{"props":{"allow_mult":true, "datatype": "hll"}}'
113+
riak-admin bucket-type activate gsets
114+
riak-admin bucket-type activate hlls
115+
```
116+
110117
Some tests may require more than one feature to run, so please check the test to see which ones are required before running.
111118

112119
Connection Options
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2016 Basho Technologies Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.basho.riak.client.api.commands.datatypes;
17+
18+
import com.basho.riak.client.core.query.crdt.ops.GSetOp;
19+
import com.basho.riak.client.core.query.crdt.ops.SetOp;
20+
import com.basho.riak.client.core.util.BinaryValue;
21+
22+
import java.util.HashSet;
23+
import java.util.Set;
24+
25+
/**
26+
* An update to a Riak grow-only set datatype.
27+
* <p>
28+
* When building an {@link UpdateSet} command
29+
* this class is used to encapsulate the update to be performed on a
30+
* Riak gset datatype.
31+
* </p>
32+
* @author Alex Moore <amoore at basho dot com>
33+
* @since 2.2
34+
*/
35+
public class GSetUpdate implements DatatypeUpdate
36+
{
37+
protected final Set<BinaryValue> adds = new HashSet<>();
38+
39+
/**
40+
* Constructs an empty GSetUpdate.
41+
*/
42+
public GSetUpdate()
43+
{
44+
}
45+
46+
/**
47+
* Add the provided value to the set in Riak.
48+
* @param value the value to be added.
49+
* @return a reference to this object.
50+
*/
51+
public GSetUpdate add(BinaryValue value)
52+
{
53+
this.adds.add(value);
54+
return this;
55+
}
56+
57+
/**
58+
* Add the provided value to the set in Riak.
59+
* @param value the value to be added.
60+
* @return a reference to this object.
61+
*/
62+
public GSetUpdate add(String value)
63+
{
64+
this.adds.add(BinaryValue.create(value));
65+
return this;
66+
}
67+
68+
/**
69+
* Get the set of additions contained in this update.
70+
* @return the set of additions.
71+
*/
72+
public Set<BinaryValue> getAdds()
73+
{
74+
return adds;
75+
}
76+
77+
/**
78+
* Returns the core update.
79+
* @return the update used by the client core.
80+
*/
81+
@Override
82+
public GSetOp getOp()
83+
{
84+
return new GSetOp(adds);
85+
}
86+
87+
@Override
88+
public String toString()
89+
{
90+
return "Add: " + adds;
91+
}
92+
}

src/main/java/com/basho/riak/client/api/commands/datatypes/SetUpdate.java

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
* @author Dave Rusek <drusek at basho dot com>
3232
* @since 2.0
3333
*/
34-
public class SetUpdate implements DatatypeUpdate
34+
public class SetUpdate extends GSetUpdate
3535
{
36-
private final Set<BinaryValue> adds = new HashSet<>();
3736
private final Set<BinaryValue> removes = new HashSet<>();
3837

3938
/**
@@ -43,28 +42,6 @@ public SetUpdate()
4342
{
4443
}
4544

46-
/**
47-
* Add the provided value to the set in Riak.
48-
* @param value the value to be added.
49-
* @return a reference to this object.
50-
*/
51-
public SetUpdate add(BinaryValue value)
52-
{
53-
this.adds.add(value);
54-
return this;
55-
}
56-
57-
/**
58-
* Add the provided value to the set in Riak.
59-
* @param value the value to be added.
60-
* @return a reference to this object.
61-
*/
62-
public SetUpdate add(String value)
63-
{
64-
this.adds.add(BinaryValue.create(value));
65-
return this;
66-
}
67-
6845
/**
6946
* Remove the provided value from the set in Riak.
7047
* @param value the value to be removed.
@@ -87,15 +64,6 @@ public SetUpdate remove(String value)
8764
return this;
8865
}
8966

90-
/**
91-
* Get the set of additions contained in this update.
92-
* @return the set of additions.
93-
*/
94-
public Set<BinaryValue> getAdds()
95-
{
96-
return adds;
97-
}
98-
9967
/**
10068
* Get the set of removes contained in this update.
10169
* @return the set of removes.
@@ -120,4 +88,5 @@ public String toString()
12088
{
12189
return "Add: " + adds + " Remove: " + removes;
12290
}
91+
12392
}

src/main/java/com/basho/riak/client/api/commands/datatypes/UpdateDatatype.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public T withTimeout(int timeout)
270270
*/
271271
public T withReturnDatatype(boolean returnDatatype)
272272
{
273-
withOption(Option.RETURN_BODY, true);
273+
withOption(Option.RETURN_BODY, returnDatatype);
274274
return self();
275275
}
276276

src/main/java/com/basho/riak/client/api/commands/datatypes/UpdateSet.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,24 @@ protected Response convertResponse(FutureOperation<DtUpdateOperation.Response, ?
7676
}
7777

7878
/**
79-
* Builder used to construct an UpdateSet command.
79+
* Builder used to construct an UpdateSet command..
8080
*/
8181
public static class Builder extends UpdateDatatype.Builder<Builder>
8282
{
83+
/**
84+
* Construct a Builder for an UpdateSet command.
85+
* @param location the location of the set in Riak.
86+
* @param update the update to apply to the set.
87+
*/
88+
public Builder(Location location, GSetUpdate update)
89+
{
90+
super(location, update);
91+
if (update == null)
92+
{
93+
throw new IllegalArgumentException("Update cannot be null");
94+
}
95+
}
96+
8397
/**
8498
* Construct a Builder for an UpdateSet command.
8599
* @param location the location of the set in Riak.
@@ -94,6 +108,26 @@ public Builder(Location location, SetUpdate update)
94108
}
95109
}
96110

111+
/**
112+
* Constructs a builder for an UpdateSet command with only a Namespace.
113+
* <p>
114+
* By providing only a Namespace with the update, Riak will create the
115+
* set, generate the key,
116+
* and return it in the response.
117+
* </p>
118+
* @param namespace the namespace to create the datatype.
119+
* @param update the update to apply
120+
* @see Response#getGeneratedKey()
121+
*/
122+
public Builder(Namespace namespace, GSetUpdate update)
123+
{
124+
super(namespace, update);
125+
if (update == null)
126+
{
127+
throw new IllegalArgumentException("Update cannot be null");
128+
}
129+
}
130+
97131
/**
98132
* Constructs a builder for an UpdateSet command with only a Namespace.
99133
* <p>

src/main/java/com/basho/riak/client/core/converters/CrdtResponseConverter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ else if (response.hasHllValue())
9696
{
9797
element = parseHll(response.getHllValue());
9898
}
99+
else if (response.getGsetValueCount() > 0)
100+
{
101+
element = parseSet(response.getGsetValueList());
102+
}
99103

100104
return element;
101105
}
@@ -117,6 +121,9 @@ public RiakDatatype convert(RiakDtPB.DtFetchResp response)
117121
case HLL:
118122
element = parseHll(response.getValue().getHllValue());
119123
break;
124+
case GSET:
125+
element = parseSet(response.getValue().getGsetValueList());
126+
break;
120127
default:
121128
throw new IllegalStateException("No known datatype returned");
122129
}

src/main/java/com/basho/riak/client/core/operations/DtUpdateOperation.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,18 @@ RiakDtPB.SetOp getSetOp(SetOp op)
293293
return setOpBuilder.build();
294294
}
295295

296+
RiakDtPB.GSetOp getGSetOp(GSetOp op)
297+
{
298+
RiakDtPB.GSetOp.Builder setOpBuilder = RiakDtPB.GSetOp.newBuilder();
299+
300+
for (BinaryValue element : op.getAdds())
301+
{
302+
setOpBuilder.addAdds(ByteString.copyFrom(element.unsafeGetValue()));
303+
}
304+
305+
return setOpBuilder.build();
306+
}
307+
296308
RiakDtPB.HllOp getHllOp(HllOp op)
297309
{
298310
RiakDtPB.HllOp.Builder hllOpBuilder = RiakDtPB.HllOp.newBuilder();
@@ -411,6 +423,10 @@ else if (op instanceof SetOp)
411423
{
412424
withOp((SetOp) op);
413425
}
426+
else if (op instanceof GSetOp)
427+
{
428+
withOp((GSetOp) op);
429+
}
414430
else if (op instanceof HllOp)
415431
{
416432
withOp((HllOp) op);
@@ -442,6 +458,12 @@ private Builder withOp(SetOp op)
442458
return this;
443459
}
444460

461+
private Builder withOp(GSetOp op)
462+
{
463+
reqBuilder.setOp(RiakDtPB.DtOp.newBuilder().setGsetOp(getGSetOp(op)));
464+
return this;
465+
}
466+
445467
private Builder withOp(HllOp op)
446468
{
447469
reqBuilder.setOp(RiakDtPB.DtOp.newBuilder()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2016 Basho Technologies Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.basho.riak.client.core.query.crdt.ops;
17+
18+
import com.basho.riak.client.core.util.BinaryValue;
19+
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
23+
public class GSetOp implements CrdtOp
24+
{
25+
protected final Set<BinaryValue> adds = new HashSet<>();
26+
27+
public GSetOp(Set<BinaryValue> adds)
28+
{
29+
this.adds.addAll(adds);
30+
}
31+
32+
public GSetOp() {}
33+
34+
public GSetOp add(BinaryValue element)
35+
{
36+
this.adds.add(element);
37+
return this;
38+
}
39+
40+
public Set<BinaryValue> getAdds()
41+
{
42+
return adds;
43+
}
44+
45+
@Override
46+
public String toString()
47+
{
48+
return "{Add: " + adds + "}";
49+
}
50+
}

src/main/java/com/basho/riak/client/core/query/crdt/ops/SetOp.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import java.util.HashSet;
2121
import java.util.Set;
2222

23-
public class SetOp implements CrdtOp
23+
public class SetOp extends GSetOp
2424
{
25-
private final Set<BinaryValue> adds = new HashSet<>();
2625
private final Set<BinaryValue> removes = new HashSet<>();
2726

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

3433
public SetOp() {}
3534

35+
@Override
3636
public SetOp add(BinaryValue element)
3737
{
38-
this.adds.add(element);
38+
super.add(element);
3939
return this;
4040
}
4141

@@ -45,11 +45,6 @@ public SetOp remove(BinaryValue element)
4545
return this;
4646
}
4747

48-
public Set<BinaryValue> getAdds()
49-
{
50-
return adds;
51-
}
52-
5348
public Set<BinaryValue> getRemoves()
5449
{
5550
return removes;

0 commit comments

Comments
 (0)