Skip to content
Browse files

Change vclock:merge to give deterministic result for an id with same …

…counters.
  • Loading branch information...
1 parent a7e2a86 commit bc17d412786e5b6cf3540a7cd2fa6e336544acd8 @jonmeredith jonmeredith committed
Showing with 10 additions and 8 deletions.
  1. +10 −8 src/vclock.erl
View
18 src/vclock.erl
@@ -82,19 +82,21 @@ merge([], NClock) -> NClock;
merge([AClock|VClocks],NClock) ->
merge(VClocks, merge(lists:keysort(1, AClock), NClock, [])).
+merge([], [], AccClock) -> lists:reverse(AccClock);
merge([], Left, AccClock) -> lists:reverse(AccClock, Left);
merge(Left, [], AccClock) -> lists:reverse(AccClock, Left);
-merge(V=[{Node1,{Ctr1,TS1}}|VClock],
- N=[{Node2,{Ctr2,TS2}}|NClock], AccClock) ->
+merge(V=[{Node1,{Ctr1,TS1}=CT1}|VClock],
+ N=[{Node2,{Ctr2,TS2}=CT2}|NClock], AccClock) ->
if Node1 < Node2 ->
- merge(VClock, N, [{Node1,{Ctr1,TS1}}|AccClock]);
+ merge(VClock, N, [{Node1,CT1}|AccClock]);
Node1 > Node2 ->
- merge(V, NClock, [{Node2,{Ctr2,TS2}}|AccClock]);
+ merge(V, NClock, [{Node2,CT2}|AccClock]);
true ->
- ({_Ctr,_TS} = C1) = if Ctr1 > Ctr2 -> {Ctr1,TS1};
- true -> {Ctr2,TS2}
- end,
- merge(VClock, NClock, [{Node1,C1}|AccClock])
+ ({_Ctr,_TS} = CT) = if Ctr1 > Ctr2 -> CT1;
+ Ctr1 < Ctr2 -> CT2;
+ true -> {Ctr1, erlang:max(TS1,TS2)}
+ end,
+ merge(VClock, NClock, [{Node1,CT}|AccClock])
end.
% @doc Get the counter value in VClock set from Node.

0 comments on commit bc17d41

Please sign in to comment.
Something went wrong with that request. Please try again.