Skip to content

Instantly share code, notes, and snippets.

@binarytemple
Forked from potatosalad/map-example.erl
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save binarytemple/94553872a66dec8a65f4 to your computer and use it in GitHub Desktop.

Select an option

Save binarytemple/94553872a66dec8a65f4 to your computer and use it in GitHub Desktop.
%% riak_dt_map: OR-Set schema based multi CRDT container
M = riak_dt_map:new(),
%% Braxton adds key and val fields
{ok, M1B} = riak_dt_map:update({update, [
{add, {key, riak_dt_lwwreg}},
{add, {val, riak_dt_pncounter}}
]}, braxton, M),
%% Braxton sets key to 'a' and increments val by 1
{ok, M2B} = riak_dt_map:update({update, [
{update, {key, riak_dt_lwwreg}, {assign, a}},
{update, {val, riak_dt_pncounter}, {increment, 1}}
]}, braxton, M1B),
%% Andrew changes the key to 'b' and increments val by 100
{ok, M3A} = riak_dt_map:update({update, [
{update, {key, riak_dt_lwwreg}, {assign, b}},
{update, {val, riak_dt_pncounter}, {increment, 100}}
]}, andrew, M2B),
%% Tyler changes the key to 'c' and decrements val by 1
{ok, M3T} = riak_dt_map:update({update, [
{update, {key, riak_dt_lwwreg}, {assign, c}},
{update, {val, riak_dt_pncounter}, {decrement, 1}}
]}, tyler, M2B),
%% M3A and M3T have diverged from M2B
io:format(
"Braxton: ~p~n"
"(history): ~p~n~n"
"Andrew: ~p~n"
"(history): ~p~n~n"
"Tyler: ~p~n"
"(history): ~p~n~n",
[riak_dt_map:value(M2B), M2B, riak_dt_map:value(M3A), M3A, riak_dt_map:value(M3T), M3T]).
%
% Braxton: [{{val,riak_dt_pncounter},1},{{key,riak_dt_lwwreg},a}]
% (history): {[{braxton,4}],
% [{{key,riak_dt_lwwreg},{[{braxton,3}],{a,1387903891408760}}},
% {{val,riak_dt_pncounter},{[{braxton,4}],[{braxton,1,0}]}}]}
% Andrew: [{{val,riak_dt_pncounter},101},{{key,riak_dt_lwwreg},b}]
% (history): {[{andrew,2},{braxton,4}],
% [{{key,riak_dt_lwwreg},{[{andrew,1}],{b,1387903891408951}}},
% {{val,riak_dt_pncounter},
% {[{andrew,2}],[{andrew,100,0},{braxton,1,0}]}}]}
% Tyler: [{{val,riak_dt_pncounter},0},{{key,riak_dt_lwwreg},c}]
% (history): {[{tyler,2},{braxton,4}],
% [{{key,riak_dt_lwwreg},{[{tyler,1}],{c,1387903891409145}}},
% {{val,riak_dt_pncounter},
% {[{tyler,2}],[{tyler,0,1},{braxton,1,0}]}}]}
%
%% Merge together the 2 divergent maps:
MMerged = riak_dt_map:merge(M3A, M3T).
%% Show the result
io:format(
"Merged: ~p~n"
"(history): ~p~n~n",
[riak_dt_map:value(MMerged), MMerged]).
%
% Merged: [{{val,riak_dt_pncounter},100},{{key,riak_dt_lwwreg},c}]
% (history): {[{andrew,2},{braxton,4},{tyler,2}],
% [{{key,riak_dt_lwwreg},
% {[{andrew,1},{tyler,1}],{c,1387903891409145}}},
% {{val,riak_dt_pncounter},
% {[{andrew,2},{tyler,2}],
% [{andrew,100,0},{braxton,1,0},{tyler,0,1}]}}]}
%
%% This is an Ordered Set with a vclock
S = riak_dt_orswot:new(),
%% Braxton has already added a and b
{ok, S1B} = riak_dt_orswot:update({add, a}, braxton, S),
{ok, S2B} = riak_dt_orswot:update({add, b}, braxton, S1B),
%% Sharing this common ancestor, Andrew adds c & d, while Tyler adds c also
{ok, S3A} = riak_dt_orswot:update({add, c}, andrew, S2B),
{ok, S4A} = riak_dt_orswot:update({add, d}, andrew, S3A),
{ok, S3T} = riak_dt_orswot:update({add, c}, tyler, S2B),
%% Tyler then removes a
{ok, S4T} = riak_dt_orswot:update({remove, a}, tyler, S3T).
%% Taking a look inside S4A and S4T which have diverged from S2B
io:format(
"Braxton: ~p~n"
"(history): ~p~n~n"
"Andrew: ~p~n"
"(history): ~p~n~n"
"Tyler: ~p~n"
"(history): ~p~n~n",
[riak_dt_orswot:value(S2B), S2B, riak_dt_orswot:value(S4A), S4A, riak_dt_orswot:value(S4T), S4T]).
%
% Braxton: [a,b]
% (history): {[{braxton,2}],[{a,[{braxton,1}]},{b,[{braxton,2}]}]}
% Andrew: [a,b,c,d]
% (history): {[{andrew,2},{braxton,2}],
% [{a,[{braxton,1}]},
% {b,[{braxton,2}]},
% {c,[{andrew,1}]},
% {d,[{andrew,2}]}]}
% Tyler: [b,c]
% (history): {[{tyler,1},{braxton,2}],[{b,[{braxton,2}]},{c,[{tyler,1}]}]}
%
%% Merge together the 3 divergent ordered sets:
SMerged = riak_dt_orswot:merge(S2B, riak_dt_orswot:merge(S4A, S4T)).
%% Show the result
io:format(
"Merged: ~p~n"
"(history): ~p~n~n",
[riak_dt_orswot:value(SMerged), SMerged]).
%
% Merged: [b,c,d]
% (history): {[{andrew,2},{braxton,2},{tyler,1}],
% [{b,[{braxton,2}]},{c,[{andrew,1},{tyler,1}]},{d,[{andrew,2}]}]}
%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment