| summaryrefslogtreecommitdiff |
diff options
| author | nsensfel <SpamShield0@noot-noot.org> | 2019-01-10 18:48:54 +0100 |
|---|---|---|
| committer | nsensfel <SpamShield0@noot-noot.org> | 2019-01-10 18:48:54 +0100 |
| commit | 7f162e8824f3977dc5cc207dd5257ad0b39faa97 (patch) | |
| tree | b51ce99282f37cb86587b8268c3fb98a355e33c1 | |
| parent | 2dc910eab6e0c4cc415ed85f4cb85b66c2077ac5 (diff) | |
Allows the use of locks when creating entries.
| -rw-r--r-- | src/ataxia_client.erl | 169 | ||||
| -rw-r--r-- | src/ataxia_entry.erl | 7 | ||||
| -rw-r--r-- | src/ataxia_lock.erl | 2 | ||||
| -rw-r--r-- | src/ataxia_security.erl | 14 | ||||
| -rw-r--r-- | src/ataxia_server.erl | 28 |
5 files changed, 189 insertions, 31 deletions
diff --git a/src/ataxia_client.erl b/src/ataxia_client.erl index 739320a..71f0d63 100644 --- a/src/ataxia_client.erl +++ b/src/ataxia_client.erl @@ -10,10 +10,23 @@ -export ( [ + % Only the user provided + add/3, + add_at/4, + reserve/2, + reserve_at/3, + + % Both permissions provided add/4, add_at/5, reserve/3, + reserve_at/4, + + % Lock provided + add/5, + add_at/6, reserve/4, + reserve_at/5, fetch/3, update/4, @@ -42,23 +55,127 @@ get_debug_db_node () -> list_to_atom("db_node@" ++ net_adm:localhost()). get_random_db_node () -> get_debug_db_node(). --spec get_db_node_for (binary()) -> node(). +-spec get_db_node_for (ataxia_id:type()) -> node(). get_db_node_for (_ObjectID) -> get_debug_db_node(). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% ADD NEW ELEMENT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%% +%%%% Providing only the user +%%%% -spec add_at ( atom(), - binary(), + ataxia_id:type(), + ataxia_security:user(), + any() + ) + -> ({'aborted', any()} | 'ok'). +add_at (DB, ID, User, Value) -> + Permission = ataxia_permission:allow_only(User), + + add_at(DB, ID, Permission, Permission, ataxia_lock:unlocked(), Value). + +-spec add + ( + atom(), + ataxia_security:user(), + any() + ) + -> ({'ok', ataxia_id:type()} | {'aborted', any()}). +add (DB, User, Value) -> + Permission = ataxia_permission:allow_only(User), + + add(DB, Permission, Permission, ataxia_lock:unlocked(), Value). + +-spec reserve + ( + atom(), + ataxia_security:user() + ) + -> ({'ok', ataxia_id:type()} | {'aborted', any()}). +reserve (DB, User) -> + Permission = ataxia_permission:allow_only(User), + + reserve (DB, Permission, Permission, ataxia_lock:unlocked()). + +-spec reserve_at + ( + atom(), + ataxia_security:user(), + ataxia_id:type() + ) + -> ('ok' | {'aborted', any()}). +reserve_at (DB, User, ID) -> + Permission = ataxia_permission:allow_only(User), + + reserve_at (DB, Permission, Permission, ataxia_lock:unlocked(), ID). + +%%%% +%%%% Providing No Lock +%%%% +-spec add_at + ( + atom(), + ataxia_id:type(), ataxia_security:permission(), ataxia_security:permission(), any() ) -> ({'aborted', any()} | 'ok'). add_at (DB, ID, ReadPerm, WritePerm, Value) -> + add_at(DB, ID, ReadPerm, WritePerm, ataxia_lock:unlocked(), Value). + +-spec add + ( + atom(), + ataxia_security:permission(), + ataxia_security:permission(), + any() + ) + -> ({'ok', ataxia_id:type()} | {'aborted', any()}). +add (DB, ReadPerm, WritePerm, Value) -> + add(DB, ReadPerm, WritePerm, ataxia_lock:unlocked(), Value). + +-spec reserve + ( + atom(), + ataxia_security:permission(), + ataxia_security:permission() + ) + -> ({'ok', ataxia_id:type()} | {'aborted', any()}). +reserve (DB, ReadPerm, WritePerm) -> + reserve (DB, ReadPerm, WritePerm, ataxia_lock:unlocked()). + +-spec reserve_at + ( + atom(), + ataxia_security:permission(), + ataxia_security:permission(), + ataxia_id:type() + ) + -> ('ok' | {'aborted', any()}). +reserve_at (DB, ReadPerm, WritePerm, ID) -> + reserve_at (DB, ReadPerm, WritePerm, ataxia_lock:unlocked(), ID). + +%%%% +%%%% Providing everything +%%%% +-spec add_at + ( + atom(), + ataxia_id:type(), + ataxia_security:permission(), + ataxia_security:permission(), + ataxia_lock:type(), + any() + ) + -> ({'aborted', any()} | 'ok'). +add_at (DB, ID, ReadPerm, WritePerm, Lock, Value) -> DBNode = get_db_node_for(ID), Reply = @@ -67,13 +184,13 @@ add_at (DB, ID, ReadPerm, WritePerm, Value) -> DBNode, ataxia_server, add_at, - [DB, ID, ReadPerm, WritePerm, Value] + [DB, ID, ReadPerm, WritePerm, Lock, Value] ), io:format ( "~nataxia_client:add_at(~p) ! ~p -> ~p.~n", - [{DB, ID, ReadPerm, WritePerm, Value}, DBNode, Reply] + [{DB, ID, ReadPerm, WritePerm, Lock, Value}, DBNode, Reply] ), Reply. @@ -83,19 +200,26 @@ add_at (DB, ID, ReadPerm, WritePerm, Value) -> atom(), ataxia_security:permission(), ataxia_security:permission(), + ataxia_lock:type(), any() ) -> ({'ok', ataxia_id:type()} | {'aborted', any()}). -add (DB, ReadPerm, WritePerm, Value) -> +add (DB, ReadPerm, WritePerm, Lock, Value) -> DBNode = get_random_db_node(), Reply = - rpc:call(DBNode, ataxia_server, add, [DB, ReadPerm, WritePerm, Value]), + rpc:call + ( + DBNode, + ataxia_server, + add, + [DB, ReadPerm, WritePerm, Lock, Value] + ), io:format ( "~nataxia_client:add(~p) ! ~p -> ok.~n", - [{DB, ReadPerm, WritePerm, Value}, DBNode] + [{DB, ReadPerm, WritePerm, Lock, Value}, DBNode] ), Reply. @@ -104,40 +228,55 @@ add (DB, ReadPerm, WritePerm, Value) -> ( atom(), ataxia_security:permission(), - ataxia_security:permission() + ataxia_security:permission(), + ataxia_lock:type() ) -> ({'ok', ataxia_id:type()} | {'aborted', any()}). -reserve (DB, ReadPerm, WritePerm) -> +reserve (DB, ReadPerm, WritePerm, Lock) -> DBNode = get_random_db_node(), - Reply = rpc:call(DBNode, ataxia_server, reserve, [DB, ReadPerm, WritePerm]), + Reply = + rpc:call + ( + DBNode, + ataxia_server, + reserve, + [DB, ReadPerm, WritePerm, Lock] + ), io:format ( "~nataxia_client:reserve(~p) ! ~p -> ~p.~n", - [{DB, ReadPerm, WritePerm}, DBNode, Reply] + [{DB, ReadPerm, WritePerm, Lock}, DBNode, Reply] ), Reply. --spec reserve +-spec reserve_at ( atom(), ataxia_security:permission(), ataxia_security:permission(), + ataxia_lock:type(), ataxia_id:type() ) -> ('ok' | {'aborted', any()}). -reserve (DB, ReadPerm, WritePerm, ID) -> +reserve_at (DB, ReadPerm, WritePerm, Lock, ID) -> DBNode = get_db_node_for(ID), Reply = - rpc:call(DBNode, ataxia_server, reserve, [DB, ReadPerm, WritePerm, ID]), + rpc:call + ( + DBNode, + ataxia_server, + reserve_at, + [DB, ReadPerm, WritePerm, Lock, ID] + ), io:format ( "~nataxia_client:reserve(~p) ! ~p -> ~p.~n", - [{DB, ReadPerm, WritePerm, ID}, DBNode, Reply] + [{DB, ReadPerm, WritePerm, Lock, ID}, DBNode, Reply] ), Reply. diff --git a/src/ataxia_entry.erl b/src/ataxia_entry.erl index af0b240..6d1f223 100644 --- a/src/ataxia_entry.erl +++ b/src/ataxia_entry.erl @@ -25,7 +25,7 @@ -export ( [ - new/4, + new/5, get_id/1, get_read_permission/1, @@ -61,15 +61,16 @@ any(), ataxia_security:permission(), ataxia_security:permission(), + ataxia_lock:type(), any() ) -> type(). -new (ID, ReadPermission, WritePermission, Value) -> +new (ID, ReadPermission, WritePermission, Lock, Value) -> #entry { id = ID, read_perm = ReadPermission, write_perm = WritePermission, - lock = ataxia_lock:unlocked(), + lock = Lock, val = Value }. diff --git a/src/ataxia_lock.erl b/src/ataxia_lock.erl index b0bab59..fef131d 100644 --- a/src/ataxia_lock.erl +++ b/src/ataxia_lock.erl @@ -45,7 +45,7 @@ unlocked () -> #lock { status = none, - permission = ataxia_security:allow_only(ataxia_security:any()), + permission = ataxia_security:allow_none(), time = ataxia_time:now() }. diff --git a/src/ataxia_security.erl b/src/ataxia_security.erl index b1ec662..4088cd4 100644 --- a/src/ataxia_security.erl +++ b/src/ataxia_security.erl @@ -14,7 +14,16 @@ -export([janitor/0, any/0, admin/0, user_from_id/1]). --export([add_access/2, remove_access/2, allow_only/1, allow_any/0]). +-export +( + [ + add_access/2, + remove_access/2, + allow_only/1, + allow_any/0, + allow_none/0 + ] +). -export([can_access/2]). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -40,6 +49,9 @@ allow_only (User) -> allow_any () -> ordsets:add_element(any(), ordsets:new()). +-spec allow_none () -> permission(). +allow_none () -> ordsets:new(). + -spec user_from_id (any()) -> user(). user_from_id (ID) -> {user, ID}. diff --git a/src/ataxia_server.erl b/src/ataxia_server.erl index 39cdf16..d4d1671 100644 --- a/src/ataxia_server.erl +++ b/src/ataxia_server.erl @@ -10,10 +10,10 @@ -export ( [ - add_at/5, - add/4, - reserve/3, + add_at/6, + add/5, reserve/4, + reserve_at/5, fetch/3, update/4, @@ -200,11 +200,12 @@ fetch_if (DB, User, ID, Cond) -> ataxia_id:type(), ataxia_security:permission(), ataxia_security:permission(), + ataxia_lock:type(), any() ) -> ({'aborted', any()} | 'ok'). -add_at (DB, ID, ReadPerm, WritePerm, Value) -> - Item = ataxia_entry:new(ID, ReadPerm, WritePerm, Value), +add_at (DB, ID, ReadPerm, WritePerm, Lock, Value) -> + Item = ataxia_entry:new(ID, ReadPerm, WritePerm, Lock, Value), case mnesia:transaction(fun add_new_item/2, [DB, Item]) of {atomic, ok} -> ok; {aborted, Val} -> {aborted, Val} @@ -215,25 +216,27 @@ add_at (DB, ID, ReadPerm, WritePerm, Value) -> atom(), ataxia_security:permission(), ataxia_security:permission(), + ataxia_lock:type(), any() ) -> ({'aborted', any()} | {'ok', ataxia_id:type()}). -add (DB, ReadPerm, WritePerm, Value) -> +add (DB, ReadPerm, WritePerm, Lock, Value) -> ID = ataxia_id_manager:allocate(DB), - case add_at(DB, ID, ReadPerm, WritePerm, Value) of + case add_at(DB, ID, ReadPerm, WritePerm, Lock, Value) of ok -> {ok, ID}; {aborted, Val} -> {aborted, Val} end. --spec reserve +-spec reserve_at ( atom(), ataxia_security:permission(), ataxia_security:permission(), + ataxia_lock:type(), ataxia_id:type() ) -> ({'aborted', any()} | 'ok'). -reserve (DB, ReadPerm, WritePerm, ID) -> +reserve_at (DB, ReadPerm, WritePerm, Lock, ID) -> % TODO: spawn or inform janitor add_at ( @@ -241,6 +244,7 @@ reserve (DB, ReadPerm, WritePerm, ID) -> ID, ataxia_security:add_access(ataxia_security:janitor(), ReadPerm), ataxia_security:add_access(ataxia_security:janitor(), WritePerm), + Lock, % TODO: allow the janitor there. reserved ). @@ -248,16 +252,18 @@ reserve (DB, ReadPerm, WritePerm, ID) -> ( atom(), ataxia_security:permission(), - ataxia_security:permission() + ataxia_security:permission(), + ataxia_lock:type() ) -> ({'aborted', any()} | {'ok', ataxia_id:type()}). -reserve (DB, ReadPerm, WritePerm) -> +reserve (DB, ReadPerm, WritePerm, Lock) -> % TODO: spawn or inform janitor add ( DB, ataxia_security:add_access(ataxia_security:janitor(), ReadPerm), ataxia_security:add_access(ataxia_security:janitor(), WritePerm), + Lock, % TODO: allow the janitor there. reserved ). |


