1
1
% % -*- coding: utf-8 -*-
2
2
% % --------------------------------------------------------------------------
3
3
% %
4
- % % crdt_bcounter : A convergent, replicated, operation based bounded counter.
4
+ % % antidote_crdt_counter_b : A convergent, replicated, operation- based bounded counter.
5
5
% %
6
6
% % --------------------------------------------------------------------------
7
7
12
12
% % All operations on this CRDT are monotonic and do not keep extra tombstones.
13
13
% % @end
14
14
15
- -module (antidote_crdt_bcounter ).
15
+ -module (antidote_crdt_counter_b ).
16
16
17
17
-behaviour (antidote_crdt ).
18
18
40
40
-include_lib (" eunit/include/eunit.hrl" ).
41
41
-endif .
42
42
43
- -export_type ([bcounter / 0 , binary_bcounter / 0 , bcounter_op / 0 , id / 0 ]).
43
+ -export_type ([antidote_crdt_counter_b / 0 , binary_antidote_crdt_counter_b / 0 , antidote_crdt_counter_b_op / 0 , id / 0 ]).
44
44
45
- -opaque bcounter () :: {orddict :orddict (), orddict :orddict ()}.
46
- -type binary_bcounter () :: binary ().
47
- -type bcounter_op () :: bcounter_anon_op () | bcounter_src_op ().
48
- -type bcounter_anon_op () :: {transfer , {pos_integer (), id (), id ()}} |
45
+ -opaque antidote_crdt_counter_b () :: {orddict :orddict (), orddict :orddict ()}.
46
+ -type binary_antidote_crdt_counter_b () :: binary ().
47
+ -type antidote_crdt_counter_b_op () :: antidote_crdt_counter_b_anon_op () | antidote_crdt_counter_b_src_op ().
48
+ -type antidote_crdt_counter_b_anon_op () :: {transfer , {pos_integer (), id (), id ()}} |
49
49
{increment , {pos_integer (), id ()}} | {decrement , {pos_integer (), id ()}}.
50
- -type bcounter_src_op () :: {bcounter_anon_op (), id ()}.
50
+ -type antidote_crdt_counter_b_src_op () :: {antidote_crdt_counter_b_anon_op (), id ()}.
51
51
-opaque id () :: term . % % A replica's identifier.
52
52
53
- % % @doc Return a new, empty `bcounter ()'.
54
- -spec new () -> bcounter ().
53
+ % % @doc Return a new, empty `antidote_crdt_counter_b ()'.
54
+ -spec new () -> antidote_crdt_counter_b ().
55
55
new () ->
56
56
{orddict :new (), orddict :new ()}.
57
57
58
- % % @doc Return the available permissions of replica `Id' in a `bcounter ()'.
59
- -spec localPermissions (id (), bcounter ()) -> non_neg_integer ().
58
+ % % @doc Return the available permissions of replica `Id' in a `antidote_crdt_counter_b ()'.
59
+ -spec localPermissions (id (), antidote_crdt_counter_b ()) -> non_neg_integer ().
60
60
localPermissions (Id , {P , D }) ->
61
61
Received = lists :foldl (
62
62
fun (
@@ -88,8 +88,8 @@ localPermissions(Id, {P, D}) ->
88
88
Received - Granted
89
89
end .
90
90
91
- % % @doc Return the total available permissions in a `bcounter ()'.
92
- - spec permissions (bcounter ()) -> non_neg_integer ().
91
+ % % @doc Return the total available permissions in a `antidote_crdt_counter_b ()'.
92
+ - spec permissions (antidote_crdt_counter_b ()) -> non_neg_integer ().
93
93
permissions ({P , D }) ->
94
94
TotalIncrements = orddict :fold (
95
95
fun
@@ -105,22 +105,22 @@ permissions({P, D}) ->
105
105
end , 0 , D ),
106
106
TotalIncrements - TotalDecrements .
107
107
108
- % % @doc Return the read value of a given `bcounter ()', itself.
109
- - spec value (bcounter ()) -> bcounter ().
108
+ % % @doc Return the read value of a given `antidote_crdt_counter_b ()', itself.
109
+ - spec value (antidote_crdt_counter_b ()) -> antidote_crdt_counter_b ().
110
110
value (Counter ) -> Counter .
111
111
112
112
% % @doc Generate a downstream operation.
113
113
% % The first parameter is either `{increment, pos_integer()}' or `{decrement, pos_integer()}',
114
114
% % which specify the operation and amount, or `{transfer, pos_integer(), id()}'
115
115
% % that additionally specifies the target replica.
116
116
% % The second parameter is an `actor()' who identifies the source replica,
117
- % % and the third parameter is a `bcounter ()' which holds the current snapshot.
117
+ % % and the third parameter is a `antidote_crdt_counter_b ()' which holds the current snapshot.
118
118
% %
119
119
% % Return a tuple containing the operation and source replica.
120
120
% % This operation fails and returns `{error, no_permissions}'
121
121
% % if it tries to consume resources unavailable to the source replica
122
122
% % (which prevents logging of forbidden attempts).
123
- - spec downstream (bcounter_op (), bcounter ()) -> {ok , term ()} | {error , no_permissions }.
123
+ - spec downstream (antidote_crdt_counter_b_op (), antidote_crdt_counter_b ()) -> {ok , term ()} | {error , no_permissions }.
124
124
downstream ({increment , {V , Actor }}, _Counter ) when is_integer (V ), V > 0 ->
125
125
{ok , {{increment , V }, Actor }};
126
126
downstream ({decrement , {V , Actor }}, Counter ) when is_integer (V ), V > 0 ->
@@ -134,11 +134,11 @@ generate_downstream_check(Op, Actor, Counter, V) ->
134
134
Available < V -> {error , no_permissions }
135
135
end .
136
136
137
- % % @doc Update a `bcounter ()' with a downstream operation,
137
+ % % @doc Update a `antidote_crdt_counter_b ()' with a downstream operation,
138
138
% % usually created with `generate_downstream'.
139
139
% %
140
- % % Return the resulting `bcounter ()' after applying the operation.
141
- - spec update (term (), bcounter ()) -> {ok , bcounter ()}.
140
+ % % Return the resulting `antidote_crdt_counter_b ()' after applying the operation.
141
+ - spec update (term (), antidote_crdt_counter_b ()) -> {ok , antidote_crdt_counter_b ()}.
142
142
update ({{increment , V }, Id }, Counter ) ->
143
143
increment (Id , V , Counter );
144
144
update ({{decrement , V }, Id }, Counter ) ->
@@ -158,12 +158,12 @@ decrement(Id, V, {P, D}) ->
158
158
transfer (From , To , V , {P , D }) ->
159
159
{ok , {orddict :update_counter ({From , To }, V , P ), D }}.
160
160
161
- % % doc Return the binary representation of a `bcounter ()'.
162
- - spec to_binary (bcounter ()) -> binary ().
161
+ % % doc Return the binary representation of a `antidote_crdt_counter_b ()'.
162
+ - spec to_binary (antidote_crdt_counter_b ()) -> binary ().
163
163
to_binary (C ) -> term_to_binary (C ).
164
164
165
- % % doc Return a `bcounter ()' from its binary representation.
166
- - spec from_binary (binary ()) -> {ok , bcounter ()}.
165
+ % % doc Return a `antidote_crdt_counter_b ()' from its binary representation.
166
+ - spec from_binary (binary ()) -> {ok , antidote_crdt_counter_b ()}.
167
167
from_binary (<<B /binary >>) -> {ok , binary_to_term (B )}.
168
168
169
169
% % @doc The following operation verifies
@@ -199,7 +199,7 @@ apply_op(Op, Counter) ->
199
199
{ok , NewCounter } = update (OP_DS , Counter ),
200
200
NewCounter .
201
201
202
- % % Tests creating a new `bcounter ()'.
202
+ % % Tests creating a new `antidote_crdt_counter_b ()'.
203
203
new_test () ->
204
204
? assertEqual ({[], []}, new ()).
205
205
@@ -271,22 +271,22 @@ transfer_test() ->
271
271
272
272
% % Tests the function `value()'.
273
273
value_test () ->
274
- % % Test on `bcounter ()' resulting from applying all kinds of operation.
274
+ % % Test on `antidote_crdt_counter_b ()' resulting from applying all kinds of operation.
275
275
Counter0 = new (),
276
276
Counter1 = apply_op ({increment , {10 , r1 }}, Counter0 ),
277
277
Counter2 = apply_op ({decrement , {6 , r1 }}, Counter1 ),
278
278
Counter3 = apply_op ({transfer , {2 , r2 , r1 }}, Counter2 ),
279
- % % Assert `value()' returns `bcounter ()' itself.
279
+ % % Assert `value()' returns `antidote_crdt_counter_b ()' itself.
280
280
? assertEqual (Counter3 , value (Counter3 )).
281
281
282
282
% % Tests serialization functions `to_binary()' and `from_binary()'.
283
283
binary_test () ->
284
- % % Test on `bcounter ()' resulting from applying all kinds of operation.
284
+ % % Test on `antidote_crdt_counter_b ()' resulting from applying all kinds of operation.
285
285
Counter0 = new (),
286
286
Counter1 = apply_op ({increment , {10 , r1 }}, Counter0 ),
287
287
Counter2 = apply_op ({decrement , {6 , r1 }}, Counter1 ),
288
288
Counter3 = apply_op ({transfer , {2 , r2 , r1 }}, Counter2 ),
289
- % % Assert marshaling and unmarshaling holds the same `bcounter ()'.
289
+ % % Assert marshaling and unmarshaling holds the same `antidote_crdt_counter_b ()'.
290
290
B = to_binary (Counter3 ),
291
291
? assertEqual ({ok , Counter3 }, from_binary (B )).
292
292
0 commit comments