Skip to content
This repository was archived by the owner on May 27, 2022. It is now read-only.

Commit 7177373

Browse files
Merge pull request #1 from balegas/master
Export check permissions function.
2 parents a7021de + cc28319 commit 7177373

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/antidote_crdt_bcounter.erl

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
%% @doc
99
%% An operation based implementation of the bounded counter CRDT.
1010
%% This counter is able to maintain a non-negative value by
11-
%% explicitly exchanging permissions to execute decrement operations.
11+
%% explicitly exchanging permissions to execute decrement operations.
1212
%% All operations on this CRDT are monotonic and do not keep extra tombstones.
1313
%% @end
1414

@@ -27,7 +27,8 @@
2727
to_binary/1,
2828
from_binary/1,
2929
is_operation/1,
30-
require_state_downstream/1
30+
require_state_downstream/1,
31+
generate_downstream_check/4
3132
]).
3233

3334
%% API
@@ -44,7 +45,7 @@
4445
-opaque bcounter() :: {orddict:orddict(),orddict:orddict()}.
4546
-type binary_bcounter() :: binary().
4647
-type bcounter_op() :: bcounter_anon_op() | bcounter_src_op().
47-
-type bcounter_anon_op() :: {transfer, {pos_integer(), id(), id()}} |
48+
-type bcounter_anon_op() :: {transfer, {pos_integer(), id(), id()}} |
4849
{increment, {pos_integer(), id()}} | {decrement, {pos_integer(), id()}}.
4950
-type bcounter_src_op() :: {bcounter_anon_op(), id()}.
5051
-opaque id() :: term. %% A replica's identifier.
@@ -56,46 +57,46 @@ new() ->
5657

5758
%% @doc Return the available permissions of replica `Id' in a `bcounter()'.
5859
-spec localPermissions(id(),bcounter()) -> non_neg_integer().
59-
localPermissions(Id,{P,D}) ->
60+
localPermissions(Id,{P,D}) ->
6061
Received = lists:foldl(
6162
fun(
6263
{_,V},Acc) ->
63-
Acc + V
64+
Acc + V
6465
end,
6566
0, orddict:filter(
6667
fun(
6768
{_,To},_) when To == Id ->
68-
true;
69+
true;
6970
(_,_) ->
70-
false
71+
false
7172
end, P)),
7273
Granted = lists:foldl(
7374
fun
7475
({_,V},Acc) ->
75-
Acc + V
76+
Acc + V
7677
end, 0, orddict:filter(
7778
fun
7879
({From,To},_) when From == Id andalso To /= Id ->
7980
true;
8081
(_,_) ->
81-
false
82+
false
8283
end, P)),
8384
case orddict:find(Id,D) of
84-
{ok, Decrements} ->
85+
{ok, Decrements} ->
8586
Received - Granted - Decrements;
86-
error ->
87+
error ->
8788
Received - Granted
8889
end.
8990

9091
%% @doc Return the total available permissions in a `bcounter()'.
9192
-spec permissions(bcounter()) -> non_neg_integer().
92-
permissions({P,D}) ->
93+
permissions({P,D}) ->
9394
TotalIncrements = orddict:fold(
9495
fun
9596
({K,K},V,Acc) ->
96-
V + Acc;
97+
V + Acc;
9798
(_,_,Acc) ->
98-
Acc
99+
Acc
99100
end, 0, P),
100101
TotalDecrements = orddict:fold(
101102
fun
@@ -146,15 +147,15 @@ update({{transfer, V,To},From}, Counter) ->
146147
transfer(From,To,V,Counter).
147148

148149
%% Add a given amount of permissions to a replica.
149-
increment(Id,V,{P,D}) ->
150+
increment(Id,V,{P,D}) ->
150151
{ok,{orddict:update_counter({Id,Id},V,P),D}}.
151152

152153
%% Consume a given amount of permissions from a replica.
153-
decrement(Id,V,{P,D}) ->
154+
decrement(Id,V,{P,D}) ->
154155
{ok, {P,orddict:update_counter(Id,V,D)}}.
155156

156157
%% Transfer a given amount of permissions from one replica to another.
157-
transfer(From,To,V,{P,D}) ->
158+
transfer(From,To,V,{P,D}) ->
158159
{ok, {orddict:update_counter({From,To},V,P),D}}.
159160

160161
%% doc Return the binary representation of a `bcounter()'.
@@ -221,7 +222,7 @@ localPermisisons_test() ->
221222
?assertEqual(10, localPermissions(r1,Counter1)),
222223
%% Test nonexistent replica.
223224
?assertEqual(0, localPermissions(r2,Counter1)).
224-
225+
225226
%% Tests decrement operations.
226227
decrement_test() ->
227228
Counter0 = new(),

0 commit comments

Comments
 (0)