summaryrefslogtreecommitdiff |
diff options
author | nsensfel <SpamShield0@noot-noot.org> | 2018-06-05 16:13:56 +0200 |
---|---|---|
committer | nsensfel <SpamShield0@noot-noot.org> | 2018-06-05 16:13:56 +0200 |
commit | 97f7511e61cebae3676a83aa9c0dc2efb15d8d8c (patch) | |
tree | e1a72ea2778569fa87b999ef4ac7c4559bfd7e58 /src/db | |
parent | 983ed9d0e8da15cb8c304fea62c25d3b053e712b (diff) |
Kind of hacked around to get the db working.
It does seem to work, though.
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/include/db_query.hrl | 10 | ||||
-rw-r--r-- | src/db/src/logic/storage_access.erl | 34 | ||||
-rw-r--r-- | src/db/src/struct/db_item.erl | 8 | ||||
-rw-r--r-- | src/db/src/struct/db_model.erl | 4 | ||||
-rw-r--r-- | src/db/src/struct/db_query.erl | 29 |
5 files changed, 63 insertions, 22 deletions
diff --git a/src/db/include/db_query.hrl b/src/db/include/db_query.hrl index ac7745a..7e5a5b0 100644 --- a/src/db/include/db_query.hrl +++ b/src/db/include/db_query.hrl @@ -40,6 +40,14 @@ -record ( + set_val, + { + val :: any() + } +). + +-record +( db_query, { db :: atom(), @@ -50,6 +58,6 @@ ). -type db_query_op() :: (#set_field{} | #add_to_field{} | #update_indexed{}). --type db_query_master_op() :: (db_query_op() | #set_perm{}). +-type db_query_master_op() :: (db_query_op() | #set_perm{} | #set_val{}). -type db_query() :: #db_query{}. diff --git a/src/db/src/logic/storage_access.erl b/src/db/src/logic/storage_access.erl index c58f26d..437294f 100644 --- a/src/db/src/logic/storage_access.erl +++ b/src/db/src/logic/storage_access.erl @@ -3,7 +3,6 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --record(regval, {id, owner, val}). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -13,34 +12,43 @@ [ read/2, insert/4, - update/3 + query/1 ] ). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% get_value ([]) -> not_found; -get_value ([Regval]) -> {ok, Regval}. +get_value ([Regval]) -> {ok, db_item:get_value(Regval)}. -get_value(DB, ID) -> get_value(mnesia:read(DB, ID)). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -read (DB, ID) -> +read_transaction (DB, ID) -> get_value(mnesia:read(DB, ID)). -insert (DB, ID, Owner, Value) -> - StoredItem = #regval{ id = ID, owner = Owner, val = Value }, +insert_transaction (DB, ID, Perm, Value) -> + StoredItem = db_item:new(ID, Perm, Value), % FIXME: handle return value, mnesia:write -> (transaction abort | ok). % FIXME: is this an atomic OP? Is the lock freed afterwards? mnesia:write(DB, StoredItem, sticky_write), ok. -update (DB, ID, Query) -> - {ok, Item} = get_value(DB, ID), +query_transaction (Query) -> + DB = db_query:get_database(Query), + ID = db_query:get_entry_id(Query), + [Item] = mnesia:read(DB, ID), {ok, UpdatedItem} = db_query:apply_to(Query, Item), % FIXME: handle return value, mnesia:write -> (transaction abort | ok). % FIXME: is this an atomic OP? Is the lock freed afterwards? mnesia:write(DB, UpdatedItem, sticky_write), ok. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +read (DB, ID) -> + mnesia:transaction(fun read_transaction/2, [DB, ID]). + +insert (DB, ID, Perm, Value) -> + mnesia:transaction(fun insert_transaction/4, [DB, ID, Perm, Value]). + +query (Query) -> + mnesia:transaction(fun query_transaction/1, [Query]). diff --git a/src/db/src/struct/db_item.erl b/src/db/src/struct/db_item.erl index 6ec79d0..0ce7630 100644 --- a/src/db/src/struct/db_item.erl +++ b/src/db/src/struct/db_item.erl @@ -24,7 +24,9 @@ set_permission/2, set_value/2, - get_id_field/0 + get_id_field/0, + get_record_info/0, + get_record_name/0 ] ). @@ -62,3 +64,7 @@ set_value (Value, Item) -> Item#db_item{ val = Value }. -spec get_id_field () -> non_neg_integer(). get_id_field () -> #db_item.id. +get_record_info () -> record_info(fields, db_item). + +get_record_name () -> db_item. + diff --git a/src/db/src/struct/db_model.erl b/src/db/src/struct/db_model.erl index 0f8c872..1b21629 100644 --- a/src/db/src/struct/db_model.erl +++ b/src/db/src/struct/db_model.erl @@ -3,7 +3,6 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --include("../../include/db_item.hrl"). -record ( @@ -61,7 +60,8 @@ start (Model) -> ( DBName, [ - {attributes, record_info(fields, db_item)}, + {record_name, db_item:get_record_name()}, + {attributes, db_item:get_record_info()}, {disc_copies, [node()|Neighbors]}, {disc_only_copies, []}, {ram_copies, []}, diff --git a/src/db/src/struct/db_query.erl b/src/db/src/struct/db_query.erl index 18d0a12..f8ad310 100644 --- a/src/db/src/struct/db_query.erl +++ b/src/db/src/struct/db_query.erl @@ -8,6 +8,13 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-export +( + [ + get_database/1, + get_entry_id/1 + ] +). -export([apply_to/2]). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -25,7 +32,8 @@ apply_update_indexed (Op, Elem) -> IndexedFieldValue = element(FieldNumber, Elem), ArrayValue = array:get(IX, IndexedFieldValue), UpdatedArrayValue = lists:foldl(fun apply_op_to/2, ArrayValue, Ops), - UpdatedIndexedFieldValue = array:set(IX, UpdatedArrayValue), + UpdatedIndexedFieldValue = + array:set(IX, UpdatedArrayValue, IndexedFieldValue), setelement(FieldNumber, Elem, UpdatedIndexedFieldValue). @@ -52,11 +60,11 @@ apply_set_field (Op, Elem) -> setelement(FieldNumber, Elem, NewValue). -spec apply_op_to (db_query_op(), any()) -> any(). -apply_op_to (Op, Elem) when is_record(Elem, set_field) -> +apply_op_to (Op, Elem) when is_record(Op, set_field) -> apply_set_field(Op, Elem); -apply_op_to (Op, Elem) when is_record(Elem, add_to_field) -> +apply_op_to (Op, Elem) when is_record(Op, add_to_field) -> apply_add_to_field(Op, Elem); -apply_op_to (Op, Elem) when is_record(Elem, update_indexed) -> +apply_op_to (Op, Elem) when is_record(Op, update_indexed) -> apply_update_indexed(Op, Elem). -spec apply_master_op_to @@ -69,6 +77,10 @@ apply_master_op_to (MOp, Elem) when is_record(MOp, set_perm) -> NewPerm = MOp#set_perm.perm, db_item:set_perm(NewPerm, Elem); +apply_master_op_to (MOp, Elem) when is_record(MOp, set_val) -> + NewVal = MOp#set_val.val, + + db_item:set_value(NewVal, Elem); apply_master_op_to (MOp, Elem) -> OldValue = db_item:get_value(Elem), NewValue = apply_op_to(MOp, OldValue), @@ -78,6 +90,12 @@ apply_master_op_to (MOp, Elem) -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec get_database (db_query()) -> atom(). +get_database (#db_query{ db = Result }) -> Result. + +-spec get_entry_id (db_query()) -> any(). +get_entry_id (#db_query{ id = Result }) -> Result. + -spec apply_to ( db_query(), @@ -86,5 +104,6 @@ apply_master_op_to (MOp, Elem) -> -> ({'ok', db_item:type()} | 'error'). apply_to (DBQuery, DBItem) -> true = db_user:can_access(db_item:get_permission(DBItem), get_user(DBQuery)), - {ok, apply_master_op_to(DBQuery, DBItem)}. + MOps = DBQuery#db_query.ops, + {ok, lists:foldl(fun apply_master_op_to/2, DBItem, MOps)}. |