| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2018-12-15 15:04:51 +0100 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2018-12-15 15:04:51 +0100 |
| commit | e8134a4e64a215a6f76cf24c52d4f67d3bfdcb71 (patch) | |
| tree | aee45dc3045ff1439f1f78e62f0adf1d672db93e | |
| parent | 0d7af0558cefec3041cb31e58afb09871c28d747 (diff) | |
...
| -rw-r--r-- | src/ataxia_lock.erl | 6 | ||||
| -rw-r--r-- | src/ataxia_server.erl | 224 |
2 files changed, 172 insertions, 58 deletions
diff --git a/src/ataxia_lock.erl b/src/ataxia_lock.erl index b6ce2c6..eb5176a 100644 --- a/src/ataxia_lock.erl +++ b/src/ataxia_lock.erl @@ -22,7 +22,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -export_type([type/0]). --export([locked/2, unlocked/0, allows/2]). +-export([locked/2, unlocked/0, can_access/2]). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -49,8 +49,8 @@ unlocked () -> time = ataxia_time:now() }. --spec allows (ataxia_security:user(), type()) -> boolean(). -allows (User, Lock) -> +-spec can_access (ataxia_security:user(), type()) -> boolean(). +can_access (User, Lock) -> case Lock#lock.status of none -> true; locked -> diff --git a/src/ataxia_server.erl b/src/ataxia_server.erl index eabcd8b..2d48a30 100644 --- a/src/ataxia_server.erl +++ b/src/ataxia_server.erl @@ -10,12 +10,24 @@ -export ( [ - read/4, - remove/3, + add_at/5, + add/4, reserve/3, - insert_at/5, - insert/4, - query/1 + + fetch/3, + update/4, + update_and_fetch/4, + remove/3, + + fetch_any/3, + update_any/4, + update_and_fetch_any/4, + remove_any/3, + + fetch_all/3, + update_all/4, + update_and_fetch_all/4, + remove_all/3 ] ). @@ -23,16 +35,16 @@ %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec query_transaction (shr_db_query:type()) -> 'ok'. -query_transaction (Query) -> - DB = shr_db_query:get_database(Query), - ID = shr_db_query:get_entry_id(Query), - [Item] = mnesia:read(DB, ID), - {ok, UpdatedItem} = shr_db_query:apply_to(Query, Item), - - mnesia:write(DB, UpdatedItem, sticky_write), - - ok. +%-spec query_transaction (shr_db_query:type()) -> 'ok'. +%query_transaction (Query) -> +% DB = shr_db_query:get_database(Query), +% ID = shr_db_query:get_entry_id(Query), +% [Item] = mnesia:read(DB, ID), +% {ok, UpdatedItem} = shr_db_query:apply_to(Query, Item), +% +% mnesia:write(DB, UpdatedItem, sticky_write), +% +% ok. -spec add_new_item (atom(), ataxia_entry:type()) -> 'ok'. add_new_item (DB, Item) -> @@ -47,30 +59,7 @@ add_new_item (DB, Item) -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec read - ( - atom(), - ataxia_security:user(), - ataxic:type(), - ataxia_id:type() - ) - -> ({'aborted', any()} | {'ok', any()} | 'not_found'). -read (DB, User, Selector, ID) -> - case mnesia:transaction(fun mnesia:read/2, [DB, ID]) of - {'atomic', []} -> 'not_found'; - {'atomic', [Entry]} -> - true = - ataxia_security:can_access - ( - User, - ataxia_entry:get_read_permission(Entry) - ), - {ok, ataxic:apply_to(Selector, ataxia_entry:get_value(Entry))}; - - Other -> {'aborted', Other} - end. - --spec insert_at +-spec add_at ( ataxia_id:type(), ataxia_security:permission(), @@ -79,31 +68,27 @@ read (DB, User, Selector, ID) -> atom() ) -> ({'aborted', any()} | 'ok'). -insert_at (DB, ID, ReadPerm, WritePerm, Value) -> +add_at (DB, ID, ReadPerm, WritePerm, Value) -> Item = ataxia_entry:new(ID, ReadPerm, WritePerm, Value), case mnesia:transaction(fun add_new_item/2, [DB, Item]) of - {'atomic', 'ok'} -> 'ok'; + {atomic, ok} -> ok; {aborted, Val} -> {aborted, Val} end. --spec insert +-spec add ( atom(), ataxia_security:permission(), ataxia_security:permission(), any()) -> ({'aborted', any()} | {'ok', ataxia_id:type()}). -insert (DB, ReadPerm, WritePerm, Value) -> +add (DB, ReadPerm, WritePerm, Value) -> ID = db_item_ids_manager:allocate(DB), - case insert_at(DB, ID, ReadPerm, WritePerm, Value) of - {'atomic', 'ok'} -> {'ok', ID}; + case add_at(DB, ID, ReadPerm, WritePerm, Value) of + {atomic, ok} -> {ok, ID}; {aborted, Val} -> {aborted, Val} end. --spec query (shr_db_query:type()) -> ({'aborted', any()} | {'atomic', 'ok'}). -query (Query) -> - mnesia:transaction(fun query_transaction/1, [Query]). - -spec reserve ( atom(), @@ -112,7 +97,7 @@ query (Query) -> ) -> ({'aborted', any()} | 'ok'). reserve (DB, User, ID) -> - insert_at + add_at ( DB, ID, @@ -124,15 +109,144 @@ reserve (DB, User, ID) -> } ). +-spec fetch + ( + atom(), + ataxia_security:user(), + ataxia_id:type() + ) + -> ({'aborted', any()} | {'ok', any()} | 'not_found'). +fetch (DB, User, ID) -> + case mnesia:transaction(fun mnesia:read/2, [DB, ID]) of + {atomic, []} -> not_found; + {atomic, [Entry]} -> + true = + ( + ataxia_lock:can_access(User, ataxia_entry:get_lock(Entry)) + and + ataxia_security:can_access + ( + User, + ataxia_entry:get_read_permission(Entry) + ) + ), + {ok, ataxia_entry:get_value(Entry)}; + + Other -> {aborted, Other} + end. + +-spec fetch_any + ( + atom(), + ataxia_security:user(), + ataxic:basic() + ) + -> ({'aborted', any()} | {'ok', any(), ataxia_id:type()} | 'not_found'). +fetch_any (_DB, _User, _Cond) -> + {aborted, unimplemented}. + +-spec fetch_all + ( + atom(), + ataxia_security:user(), + ataxic:basic() + ) + -> ({'aborted', any()} | {'ok', any(), ataxia_id:type()} | 'not_found'). +fetch_all (_DB, _User, _Cond) -> + {aborted, unimplemented}. + +-spec update + ( + atom(), + ataxia_security:user(), + ataxic:meta(), + ataxia_id:type() + ) + -> ({'aborted', any()} | 'ok'). +update (_DB, _User, _Update, _ID) -> + {aborted, unimplemented}. + +-spec update_any + ( + atom(), + ataxia_security:user(), + ataxic:meta(), + ataxic:basic() + ) + -> ({'aborted', any()} | {'ok', ataxia_id:type()}). +update_any (_DB, _User, _Update, _Cond) -> + {aborted, unimplemented}. + +-spec update_all + ( + atom(), + ataxia_security:user(), + ataxic:meta(), + ataxic:basic() + ) + -> ({'aborted', any()} | {'ok', list(ataxia_id:type())}). +update_all (_DB, _User, _Update, _Cond) -> + {aborted, unimplemented}. + +-spec update_and_fetch + ( + atom(), + ataxia_security:user(), + ataxic:meta(), + ataxia_id:type() + ) + -> ({'aborted', any()} | {'ok', any()}). +update_and_fetch (_DB, _User, _Update, _ID) -> + {aborted, unimplemented}. + +-spec update_and_fetch_any + ( + atom(), + ataxia_security:user(), + ataxic:meta(), + ataxic:basic() + ) + -> ({'aborted', any()} | {'ok', any(), ataxia_id:type()}). +update_and_fetch_any (_DB, _User, _Update, _Cond) -> + {aborted, unimplemented}. + +-spec update_and_fetch_all + ( + atom(), + ataxia_security:user(), + ataxic:meta(), + ataxic:basic() + ) + -> ({'aborted', any()} | {'ok', list({any(), ataxia_id:type()})}). +update_and_fetch_all (_DB, _User, _Update, _Cond) -> + {aborted, unimplemented}. + -spec remove ( atom(), ataxia_security:user(), ataxia_id:type() ) - -> ({'aborted', any()} | 'ok' | 'not_found'). + -> ({'aborted', any()} | 'ok'). remove (_DB, _User, _ID) -> - %% TODO [FUNCTION: db][MEDIUM]: unimplemented - %% Don't forget to checkt that Cred has write access before removing the - %% value. - {'aborted', 'unimplemented'}. + {aborted, unimplemented}. + +-spec remove_any + ( + atom(), + ataxia_security:user(), + ataxic:basic() + ) + -> ({'aborted', any()} | {'ok', ataxia_id:type()}). +remove_any (_DB, _User, _Cond) -> + {aborted, unimplemented}. + +-spec remove_all + ( + atom(), + ataxia_security:user(), + ataxic:basic() + ) + -> ({'aborted', any()} | {'ok', list(ataxia_id:type())}). +remove_all (_DB, _User, _Cond) -> + {aborted, unimplemented}. |


