2
2
import com .basho .riak .client .api .commands .kv .DeleteValue ;
3
3
import com .basho .riak .client .api .commands .kv .FetchValue ;
4
4
import com .basho .riak .client .api .commands .kv .StoreValue ;
5
+ import com .basho .riak .client .api .commands .kv .UpdateValue ;
5
6
import com .basho .riak .client .core .RiakCluster ;
6
7
import com .basho .riak .client .core .RiakNode ;
7
8
import com .basho .riak .client .core .query .Location ;
@@ -21,6 +22,30 @@ public static class Book {
21
22
public Integer copiesOwned ;
22
23
}
23
24
25
+ // This will allow us to update the book object handling the
26
+ // entire fetch/modify/update cycle.
27
+ public static class BookUpdate extends UpdateValue .Update <Book > {
28
+ private final Book update ;
29
+ public BookUpdate (Book update ){
30
+ this .update = update ;
31
+ }
32
+
33
+ @ Override
34
+ public Book apply (Book t ) {
35
+ if (t == null ) {
36
+ t = new Book ();
37
+ }
38
+
39
+ t .author = update .author ;
40
+ t .body = update .body ;
41
+ t .copiesOwned = update .copiesOwned ;
42
+ t .isbn = update .isbn ;
43
+ t .title = update .title ;
44
+
45
+ return t ;
46
+ }
47
+ }
48
+
24
49
// This will create a client object that we can use to interact with Riak
25
50
private static RiakCluster setUpCluster () throws UnknownHostException {
26
51
// This example will use only one node listening on localhost:10017
@@ -84,6 +109,14 @@ public static void main( String[] args ) {
84
109
assert (fetchedObject .getValue ().equals (quoteObject .getValue ()));
85
110
System .out .println ("Success! The object we created and the object we fetched have the same value" );
86
111
112
+ // Now update the fetched object
113
+ fetchedObject .setValue (BinaryValue .create ("You can be my wingman any time." ));
114
+ StoreValue updateOp = new StoreValue .Builder (fetchedObject )
115
+ .withLocation (quoteObjectLocation )
116
+ .build ();
117
+ StoreValue .Response updateOpResp = client .execute (updateOp );
118
+ updateOpResp = client .execute (updateOp );
119
+
87
120
// And we'll delete the object
88
121
DeleteValue deleteOp = new DeleteValue .Builder (quoteObjectLocation )
89
122
.build ();
@@ -119,6 +152,14 @@ public static void main( String[] args ) {
119
152
assert (mobyDick .title .equals (fetchedBook .title ));
120
153
assert (mobyDick .author .equals (fetchedBook .author ));
121
154
// And so on...
155
+
156
+ // Now to update the book with additional copies
157
+ mobyDick .copiesOwned = 5 ;
158
+ BookUpdate updatedBook = new BookUpdate (mobyDick );
159
+ UpdateValue updateValue = new UpdateValue .Builder (mobyDickLocation )
160
+ .withUpdate (updatedBook ).build ();
161
+ UpdateValue .Response response = client .execute (updateValue );
162
+
122
163
System .out .println ("Success! All of our tests check out" );
123
164
124
165
// Now that we're all finished, we should shut our cluster object down
0 commit comments