Skip to content

Instantly share code, notes, and snippets.

@andrershov
Last active February 21, 2017 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrershov/d0ebb8fd111eca013b302f8abaf14445 to your computer and use it in GitHub Desktop.
Save andrershov/d0ebb8fd111eca013b302f8abaf14445 to your computer and use it in GitHub Desktop.
Riak CRDT register test
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.datatypes.*;
import com.basho.riak.client.core.RiakCluster;
import com.basho.riak.client.core.RiakNode;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;
/**
* Created by andrershov on 21/02/2017.
*/
public class CrdtTest {
public static RiakCluster setUpCluster() throws UnknownHostException {
// This example will use only one node listening on localhost:10017
RiakNode node = new RiakNode.Builder()
.withRemoteAddress("127.0.0.1")
.withRemotePort(8087)
.build();
// This cluster object takes our one node as an argument
RiakCluster cluster = new RiakCluster.Builder(node)
.build();
// The cluster must be started to work, otherwise you will see errors
cluster.start();
return cluster;
}
public static void main(String[] args) throws UnknownHostException, ExecutionException, InterruptedException {
RiakCluster cluster = TasteOfRiak.setUpCluster();
RiakClient client = new RiakClient(cluster);
Namespace namespace = new Namespace("maps", "customers");
Location ahmedMap = new Location(namespace, "ahmed_info");
RegisterUpdate ru1 = new RegisterUpdate("Ahmed");
RegisterUpdate ru2 = new RegisterUpdate("123");
MapUpdate mu = new MapUpdate()
.update("first_name", ru1)
.update("phone", ru2);
UpdateMap update = new UpdateMap.Builder(ahmedMap, mu).build();
client.execute(update);
Context ctx = getPhone(client, ahmedMap);
updatePhone(client, ahmedMap, ctx, "456");
getPhone(client, ahmedMap).getValue();
updatePhone(client, ahmedMap, ctx, "789");
getPhone(client, ahmedMap);
cluster.shutdown();
}
private static void updatePhone(RiakClient client, Location ahmedMap, Context ctx, String phone) throws ExecutionException, InterruptedException {
MapUpdate mu;
UpdateMap update;
mu = new MapUpdate()
.update("phone", new RegisterUpdate(phone));
update = new UpdateMap.Builder(ahmedMap, mu).withContext(ctx).build();
client.execute(update);
}
private static Context getPhone(RiakClient client, Location ahmedMap) throws ExecutionException, InterruptedException {
FetchMap fetch = new FetchMap.Builder(ahmedMap).build();
FetchMap.Response response = client.execute(fetch);
Context ctx = response.getContext();
System.out.println(response.getDatatype().getRegister("phone"));
return ctx;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment