forked from fogfish/uid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uid.erl
362 lines (295 loc) · 7.62 KB
/
uid.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
%%
%% Copyright 2012 Dmitry Kolesnikov, All Rights Reserved
%%
%% Licensed 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 express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @description
%% k-ordered unique identity
-module(uid).
-include("uid.hrl").
%% k-order interface
-export([
z/0
, l/0
, l/1
, g/0
, g/1
, encode/1
, encode/2
, decode/1
, decode/2
, encode64/1
, encode64/2
, decode64/1
, decode64/2
]).
%% k-order utility
-export([
gtol/1
, d/2
, t/1
]).
%% v-clock interface
-export([
vclock/0
, vclock/1
, join/2
, descend/2
, descend/3
, diff/2
]).
%%
%% data types
-export_type([uid/0, l/0, g/0, vclock/0]).
-type uid() :: l() | g().
-type l() :: {uid, t(), seq()}.
-type g() :: {uid, id(), t(), seq()}.
-type t() :: {integer(), integer(), integer()}.
-type seq() :: <<_:14>>.
-type id() :: node() | <<_:32>>.
-type vclock() :: [g()].
%%%----------------------------------------------------------------------------
%%%
%%% k-order interface
%%%
%%%----------------------------------------------------------------------------
%%
%%
-spec z() -> l().
z() ->
{uid, {0,0,0}, 0}.
%%
%% @doc
%% generate locally unique 64-bit k-order identifier
-spec l() -> l().
-spec l(g()) -> l().
l() ->
{uid, t(), seq()}.
l({uid, _, _} = Uid) ->
Uid;
l({uid, Node, T, Seq})
when is_binary(Node) ->
case hid(erlang:node()) of
Node ->
{uid, T, Seq};
_ ->
exit(badarg)
end;
l({uid, Node, T, Seq})
when is_atom(Node) ->
case erlang:node() of
Node ->
{uid, T, Seq};
_ ->
exit(badarg)
end.
%%
%% @doc
%% generate globally unique 96-bit k-order identifier
-spec g() -> g().
-spec g(l()) -> g().
g() ->
{uid, erlang:node(), t(), seq()}.
g({uid, T, Seq}) ->
{uid, erlang:node(), T, Seq};
g({uid, _, _, _} = Uid) ->
Uid.
%%
%% @doc
%% encode k-order number to binary format
-spec encode(l() | g()) -> binary().
-spec encode(integer(), l() | g()) -> binary().
encode(Uid) ->
encode(?CONFIG_DRIFT, Uid).
encode(_Bits, {uid, T, Seq}) ->
<<(encode_t(T))/bits, Seq:14>>;
encode(Bits, {uid, Node, T, Seq}) ->
<<(encode_t(Bits, Node, T))/bits, Seq:14>>.
encode_t({A, B, C}) ->
<<T:50/bits, _/bits>> = <<A:20, B:20, C:20>>,
T.
encode_t(Bits, Node, {A, B, C})
when is_binary(Node), Bits >= ?CONFIG_DRIFT_LOW, Bits =< ?CONFIG_DRIFT_HIGH ->
L0 = 20 - Bits,
L1 = Bits,
B0 = B bsr Bits,
B1 = B band ((1 bsl Bits) - 1),
<<T:82/bits, _/bits>> = <<A:20, B0:L0, Node/binary, B1:L1, C:20>>,
T;
encode_t(Bits, Node, T) ->
encode_t(Bits, hid(Node), T).
%%
%% @doc
%% decode k-order number from binary format
-spec decode(binary()) -> l() | g().
-spec decode(integer(), binary()) -> l() | g().
decode(Uid) ->
decode(?CONFIG_DRIFT, Uid).
decode(_Bits, <<Prefix:50/bits, Seq:14>>) ->
{uid, decode_lt(Prefix), Seq};
decode(Bits, <<Prefix:82/bits, Seq:14>>) ->
{Node, T} = decode_gt(Bits, Prefix),
{uid, Node, T, Seq}.
decode_lt(<<A:20, B:20, C:10>> ) ->
{A, B, C bsl 10}.
decode_gt(Bits, T) ->
L0 = 20 - Bits,
L1 = Bits,
<<A:20, B:L0, Node:4/binary, C:L1, D:10>> = T,
case hid(erlang:node()) of
Node ->
{erlang:node(), {A, B bsl Bits + C, D bsl 10}};
_ ->
{Node, {A, B bsl Bits + C, D bsl 10}}
end.
%%
%% @doc
%% encode k-order number to base64 url
-spec encode64(l() | g()) -> binary().
-spec encode64(integer(), l() | g()) -> binary().
encode64(Uid) ->
encode64(?CONFIG_DRIFT, Uid).
encode64(Drift, Uid) ->
uid_b64:encode( encode(Drift, Uid) ).
%%
%% @doc
%% decode k-order number from base64 url
-spec decode64(binary()) -> l() | g().
-spec decode64(integer(), binary()) -> l() | g().
decode64(Uid) ->
decode64(?CONFIG_DRIFT, Uid).
decode64(Drift, Uid) ->
decode(Drift, uid_b64:decode(Uid)).
%%%----------------------------------------------------------------------------
%%%
%%% k-order utility
%%%
%%%----------------------------------------------------------------------------
%%
%% @doc
%% cast with force global to local k-order identifier
%% the operation do not guarantee uniqueness of the result
-spec gtol(g()) -> l().
gtol({uid, _, T, Seq}) ->
{uid, T, Seq}.
%%
%% @doc
%% approximate distance between k-order values
-spec d(uid(), uid()) -> uid().
d({uid, A, Sa}, {uid, B, Sb}) ->
{uid, d(A, B), Sa - Sb};
d({uid, Node, A, Sa}, {uid, Node, B, Sb}) ->
{uid, Node, d(A, B), Sa - Sb};
d({A2, A1, A0}, {B2, B1, B0}) ->
X = timer:now_diff({A2, A1, A0}, {B2, B1, B0}),
C = X rem ?BASE,
Y = X div ?BASE,
B = Y rem ?BASE,
A = Y div ?BASE,
{A, B, C}.
%%
%% @doc
%% helper function to extract time-stamp in milliseconds from k-order value
-spec t(l() | g()) -> integer().
t({uid, {A, B, C}, _}) ->
(A * ?BASE + B) * 1000 + C div 1000;
t({uid, _, {A, B, C}, _}) ->
(A * ?BASE + B) * 1000 + C div 1000.
%%%----------------------------------------------------------------------------
%%%
%%% v-clock interface
%%%
%%%----------------------------------------------------------------------------
%%
%% create new v-clock
-spec vclock() -> vclock().
vclock() ->
[g()].
%%
%% increment v-clock
-spec vclock(vclock()) -> vclock().
vclock(Vclock) ->
case lists:keytake(erlang:node(), 2, Vclock) of
false ->
[g() | Vclock];
{value, _, List} ->
[g() | List]
end.
%%
%% join two v-clock
-spec join(vclock(), vclock()) -> vclock().
join(A, B) ->
do_join(lists:keysort(2, A), lists:keysort(2, B)).
do_join([{uid, NodeA, _, _}=X|A], [{uid, NodeB, _, _}|_]=B)
when NodeA < NodeB ->
[X | do_join(A, B)];
do_join([{uid, NodeA, _, _}|_]=A, [{uid, NodeB, _, _}=X|B])
when NodeA > NodeB ->
[X | do_join(A, B)];
do_join([X|A], [Y|B]) ->
[erlang:max(X, Y) | do_join(A, B)];
do_join([], B) ->
B;
do_join(A, []) ->
A.
%%
%% return true if A v-clock is descend of B v-clock : A -> B
-spec descend(vclock(), vclock()) -> boolean().
descend(_, []) ->
true;
descend(A, [{uid, Node, _, _} = X|B]) ->
case lists:keyfind(Node, 2, A) of
false ->
false;
Y ->
(X =< Y) andalso descend(A, B)
end.
%%
%% return true if A clock is descend B with an exception to given peer
%% the method allows to discover local conflicts
-spec descend(node(), vclock(), vclock()) -> boolean().
descend(Node, A, B)
when is_atom(Node) ->
descend(lists:keydelete(Node, 2, A), lists:keydelete(Node, 2, B)).
%%
%% return difference of A clock to compare with B
-spec diff(vclock(), vclock()) -> [node()].
diff(_, []) ->
[];
diff(A, [{uid, Node, _, _} = X|B]) ->
case lists:keyfind(Node, 2, A) of
false ->
[X | diff(A, B)];
Y when X =< Y ->
diff(A, B);
Y ->
[d(X, Y) | diff(A, B)]
end.
%%%----------------------------------------------------------------------------
%%%
%%% private
%%%
%%%----------------------------------------------------------------------------
%%
%% local time stamp
t() ->
{A, B, C} = os:timestamp(),
{A, B, C band 16#ffc00}.
%%
%% locally unique sequential number 14-bit length
seq() ->
erlang:unique_integer([monotonic, positive]) band 16#3fff.
%%
%% host unique identifier
hid(Node) ->
<<(erlang:phash(Node, 1 bsl 32)):32>>.