Skip to content

Instantly share code, notes, and snippets.

@ricardobcl
Last active December 13, 2015 23:39
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 ricardobcl/4992839 to your computer and use it in GitHub Desktop.
Save ricardobcl/4992839 to your computer and use it in GitHub Desktop.
Little experiment to test the difference between Version Vectors and Dotted Version Vectors Sets.
%% Experiment to test the difference between Version Vectors (VV) and Dotted Version Vector Sets (DVVSet).
%% The Riak version using VV is here: https://github.com/ricardobcl/riak_kv/tree/master
%% The Riak version using DVVset is here: https://github.com/ricardobcl/riak_kv/tree/dvvset
%%
%% To reproduce, have a riak node running on port 10017 for protocol buffers, and do:
%%
%% $ git clone git://github.com/basho/riak-erlang-client.git
%% $ cd riak-erlang-client
%% $ make
%%
%% Copy this file to that folder, and:
%%
%% $ erlc sib.erl; erl sib -pa ebin deps/*/ebin
%% Erlang R15B02 (erts-5.9.2) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false] [dtrace]
%%
%% Eshell V5.9.2 (abort with ^G)
%%
%% 1> sib:run1(100). % with Version Vectors
%%
%% Value Count: 100
%% Values: v31 v90 v37 v30 v55 v21 v47 v89 v32 v65 v74 v6 v62 v79 v84 v58 v68 v18 v23 v15 v13 v61 v38 v71 v12 v14 v50 v2 v72 v46 v56 v45 v44 v63 v22 v48 v25 v51 v66 v4 v5 v96 v28 v82 v52 v85 v75 v98 v8 v95 v94 v40 v73 v41 v42 v67 v11 v10 v53 v77 v20 v92 v80 v76 v81 v54 v43 v59 v9 v39 v19 v87 v64 v60 v49 v35 v17 v88 v36 v24 v57 v16 v29 v78 v26 v34 v97 v86 v1 v99 v33 v91 v27 v100 v93 v69 v70 v3 v7 v83!
%% ok
%%
%%
%% 2> sib:run1(100). % with Dotted Version Vector Sets
%%
%% Value Count: 3
%% Values: v100 v99 v98!
%% ok
%% 3>
-module(sib).
-compile(export_all).
-compile({no_auto_import,[get/1,put/2]}).
run1(N) ->
Pid = start(),
Values = create_values(N),
loop1(Pid, Values),
print_sibs(Pid),
stop(Pid).
run2(N) ->
Pid = start(),
Values = create_values(N),
loop2(Pid, Values),
print_sibs(Pid),
stop(Pid).
create_values(N) ->
[ list_to_atom("v" ++ integer_to_list(I)) || I <- lists:seq(1, N)].
loop1(Pid, [Value|Rest]) ->
ok = put(Pid, Value),
O1 = get(Pid),
loop([put, upd], Pid, Rest, [dummy,O1]).
loop2(Pid, [V1|[V2|Rest]]) ->
ok = put(Pid, V1),
O1 = get(Pid),
ok = put(Pid, V2),
O2 = get(Pid),
loop([upd, upd], Pid, Rest, [O1,O2]).
loop(_,_,[],_) -> ok;
loop([put, Next], Pid, [Value|Rest], [Dummy,O2]) ->
ok = put(Pid, Value),
loop([Next, put], Pid, Rest, [O2,Dummy]);
loop([upd, Next], Pid, [Value|Rest], [O1, O2]) ->
ok = update(Pid, O1, Value),
O3 = get(Pid),
loop([Next, upd], Pid, Rest, [O2,O3]).
put(Pid, Value) ->
allow_mult(Pid),
Bucket = <<"test">>,
Key = <<"foo">>,
Data = atom_to_binary(Value, utf8),
NewO = riakc_obj:new(Bucket, Key, Data),
riakc_pb_socket:put(Pid, NewO).
update(Pid, Obj, Value) ->
allow_mult(Pid),
Bucket = <<"test">>,
Key = <<"foo">>,
Data = atom_to_binary(Value, utf8),
NewO = riakc_obj:new(Bucket, Key, Data),
VV = riakc_obj:vclock(Obj),
NewO1 = riakc_obj:set_vclock(NewO,VV),
riakc_pb_socket:put(Pid, NewO1).
allow_mult(Pid) ->
Bucket = <<"test">>,
Options = [{allow_mult,true}],
riakc_pb_socket:set_bucket(Pid, Bucket, Options).
start() ->
{ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 10017),
Bucket = <<"test">>,
Key = <<"foo">>,
riakc_pb_socket:delete(Pid, Bucket, Key),
Pid.
stop(Pid) ->
Bucket = <<"test">>,
Key = <<"foo">>,
riakc_pb_socket:delete(Pid, Bucket, Key),
riakc_pb_socket:stop(Pid).
get(Pid) ->
Bucket = <<"test">>,
Key = <<"foo">>,
{ok, R} = riakc_pb_socket:get(Pid, Bucket, Key),
R.
print_sibs(Pid) ->
Obj = get(Pid),
Count = riakc_obj:value_count(Obj),
io:format("~nSiblings: ~p~nValues:",[Count]),
Values = [ binary_to_atom(V, utf8) || V <- riakc_obj:get_values(Obj)],
[ io:format(" ~p",[V]) || V <- Values],
io:format("!~n~n").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment