summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2018-12-12 18:04:54 +0100
committernsensfel <SpamShield0@noot-noot.org>2018-12-12 18:04:54 +0100
commitd38c68a02d61abe0dfaf45a85747524a2d61c5ca (patch)
tree1493290911bd494ec4fd76f1d0e2bbb7917285a5
parent6b2fd0aae5067469598fa404f9163ab2ac87f69e (diff)
Separates ID logic from the ID manager.
-rw-r--r--src/ataxia_id.erl63
-rw-r--r--src/ataxia_id_manager.erl50
2 files changed, 71 insertions, 42 deletions
diff --git a/src/ataxia_id.erl b/src/ataxia_id.erl
new file mode 100644
index 0000000..c809b90
--- /dev/null
+++ b/src/ataxia_id.erl
@@ -0,0 +1,63 @@
+-module(ataxia_id).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-type type() :: binary().
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%% Actual Interface
+-export
+(
+ [
+ null/0,
+ next/1
+ ]
+).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-spec next_char (integer()) -> integer().
+next_char ($9) -> $a;
+next_char ($z) -> $A;
+next_char ($Z) -> $0;
+next_char (C) -> (C + 1).
+
+-spec next_id_internal
+ (
+ list(integer()),
+ boolean(),
+ list(integer())
+ )
+ -> list(integer()).
+next_id_internal ([], true, Result) -> [$0|Result];
+next_id_internal ([], false, Result) -> Result;
+next_id_internal ([$Z|Next], true, Result) ->
+ next_id_internal(Next, true, [$0|Result]);
+next_id_internal ([Char|Next], true, Result) ->
+ next_id_internal(Next, false, [next_char(Char)|Result]);
+next_id_internal ([Char|Next], false, Result) ->
+ next_id_internal(Next, false, [Char|Result]).
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-spec next (type()) -> type().
+next (ID) ->
+ list_to_binary
+ (
+ next_id_internal
+ (
+ lists:reverse(binary:bin_to_list(ID)),
+ true,
+ []
+ )
+ ).
+
+-spec null () -> type().
+null () -> <<"">>.
diff --git a/src/ataxia_id_manager.erl b/src/ataxia_id_manager.erl
index 9df15d6..6a8fdc2 100644
--- a/src/ataxia_id_manager.erl
+++ b/src/ataxia_id_manager.erl
@@ -8,8 +8,8 @@
(
entry,
{
- last_id :: binary(),
- freed_ids :: list(binary())
+ last_id :: ataxia_id:type(),
+ freed_ids :: list(ataxia_id:type())
}
).
@@ -48,49 +48,15 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--spec next_char (integer()) -> integer().
-next_char ($9) -> $a;
-next_char ($z) -> $A;
-next_char ($Z) -> $0;
-next_char (C) -> (C + 1).
-
--spec next_id_internal
- (
- list(integer()),
- boolean(),
- list(integer())
- )
- -> list(integer()).
-next_id_internal ([], true, Result) -> [$0|Result];
-next_id_internal ([], false, Result) -> Result;
-next_id_internal ([$Z|Next], true, Result) ->
- next_id_internal(Next, true, [$0|Result]);
-next_id_internal ([Char|Next], true, Result) ->
- next_id_internal(Next, false, [next_char(Char)|Result]);
-next_id_internal ([Char|Next], false, Result) ->
- next_id_internal(Next, false, [Char|Result]).
-
--spec next_id (binary()) -> binary().
-next_id (ID) ->
- list_to_binary
- (
- next_id_internal
- (
- lists:reverse(binary:bin_to_list(ID)),
- true,
- []
- )
- ).
-
-spec new_entry () -> entry().
new_entry () ->
#entry
{
- last_id = <<"">>,
+ last_id = ataxia_id:null(),
freed_ids = []
}.
--spec allocate_id_in_entry (entry()) -> {entry(), binary()}.
+-spec allocate_id_in_entry (entry()) -> {entry(), ataxia_id:type()}.
allocate_id_in_entry (Entry) ->
case Entry#entry.freed_ids of
[] ->
@@ -112,7 +78,7 @@ allocate_id_in_entry (Entry) ->
atom(),
dict:dict(atom(), entry())
)
- -> {dict:dict(atom(), entry()), binary()}.
+ -> {dict:dict(atom(), entry()), ataxia_id:type()}.
allocate_id (DB, State) ->
S0Entry =
case dict:find(DB, State) of
@@ -128,7 +94,7 @@ allocate_id (DB, State) ->
-spec free_id
(
- binary(),
+ ataxia_id:type(),
atom(),
dict:dict(atom(), entry())
)
@@ -185,7 +151,7 @@ handle_info(_, State) ->
{noreply, State}.
%%%% Interface Functions
--spec allocate (atom()) -> binary().
+-spec allocate (atom()) -> ataxia_id:type().
allocate (DB) ->
{allocated_id, Result} =
gen_server:call({global, ataxia_id_manager}, {allocate, DB}),
@@ -194,7 +160,7 @@ allocate (DB) ->
Result.
--spec free (binary(), atom()) -> 'ok'.
+-spec free (ataxia_id:type(), atom()) -> 'ok'.
free (ID, DB) ->
gen_server:cast({global, ataxia_id_manager}, {free, ID, DB}),