-
Notifications
You must be signed in to change notification settings - Fork 89
/
antidote_crdt_flag_ew.erl
129 lines (105 loc) · 3.73 KB
/
antidote_crdt_flag_ew.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
%% -------------------------------------------------------------------
%%
%% Copyright <2013-2018> <
%% Technische Universität Kaiserslautern, Germany
%% Université Pierre et Marie Curie / Sorbonne-Université, France
%% Universidade NOVA de Lisboa, Portugal
%% Université catholique de Louvain (UCL), Belgique
%% INESC TEC, Portugal
%% >
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either expressed or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% List of the contributors to the development of Antidote: see AUTHORS file.
%% Description and complete License: see LICENSE file.
%% -------------------------------------------------------------------
%% @doc
%% An operation-based Enable-wins Flag CRDT.
%% @end
-module(antidote_crdt_flag_ew).
%% Callbacks
-export([
new/0,
value/1,
downstream/2,
update/2,
equal/2,
to_binary/1,
from_binary/1,
is_operation/1,
is_bottom/1,
require_state_downstream/1
]).
-behaviour(antidote_crdt).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-define(TAG, 77).
-define(V1_VERS, 1).
-type flag_ew() :: antidote_crdt_flag_helper:tokens().
%% SeenTokens, NewTokens
-type downstream_op() :: {antidote_crdt_flag_helper:tokens(), antidote_crdt_flag_helper:tokens()}.
-spec new() -> flag_ew().
new() ->
[].
-spec value(flag_ew()) -> boolean().
value(EnableTokens) ->
EnableTokens =/= [].
-spec downstream(antidote_crdt_flag_helper:op(), flag_ew()) -> {ok, downstream_op()}.
downstream({disable, {}}, Tokens) ->
{ok, {Tokens, []}};
downstream({enable, {}}, Tokens) ->
{ok, {Tokens, [antidote_crdt_flag_helper:unique()]}};
downstream({reset, {}}, Tokens) ->
{ok, {Tokens, []}}.
-spec update(downstream_op(), flag_ew()) -> {ok, flag_ew()}.
update({SeenTokens, NewTokens}, CurrentTokens) ->
FinalTokens = (CurrentTokens ++ NewTokens) -- SeenTokens,
{ok, FinalTokens}.
-spec equal(flag_ew(), flag_ew()) -> boolean().
equal(Flag1, Flag2) ->
% Everything inside is ordered, so this should work
Flag1 == Flag2.
-spec to_binary(flag_ew()) -> antidote_crdt_flag_helper:binary_flag().
to_binary(Flag) ->
%% @TODO something smarter
<<?TAG:8/integer, ?V1_VERS:8/integer, (term_to_binary(Flag))/binary>>.
from_binary(<<?TAG:8/integer, ?V1_VERS:8/integer, Bin/binary>>) ->
%% @TODO something smarter
{ok, binary_to_term(Bin)}.
is_operation(A) -> antidote_crdt_flag_helper:is_operation(A).
is_bottom(Flag) ->
Flag == new().
require_state_downstream(A) -> antidote_crdt_flag_helper:require_state_downstream(A).
%% ===================================================================
%% EUnit tests
%% ===================================================================
-ifdef(TEST).
prop1_test() ->
Flag0 = new(),
% DC1 add a
{ok, Enable1Effect} = downstream({enable, {}}, Flag0),
{ok, Flag1a} = update(Enable1Effect, Flag0),
% DC1 reset
{ok, Reset1Effect} = downstream({reset, {}}, Flag1a),
{ok, Flag1b} = update(Reset1Effect, Flag1a),
io:format("Reset1Effect = ~p~n", [Reset1Effect]),
io:format("Enable1Effect = ~p~n", [Enable1Effect]),
io:format("Flag1a = ~p~n", [Flag1a]),
io:format("Flag1b = ~p~n", [Flag1b]),
?assertEqual(false, value(Flag0)),
?assertEqual(true, value(Flag1a)),
?assertEqual(false, value(Flag1b)).
-endif.