summaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2018-12-15 19:22:30 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2018-12-15 19:22:30 +0100
commitd50ae4d7700c4cb083e907b3d3e4ee67b92a6459 (patch)
tree1a19779b083002f6e9a29f33b8f7ffdb06fa855e /src/db
parent2c3aa52b642858b85ba756df927ff5730f5ee73d (diff)
Get debug to okay the src files.
Diffstat (limited to 'src/db')
-rw-r--r--src/db/db_node.erl18
-rw-r--r--src/db/logic/db_access.erl132
-rw-r--r--src/db/struct/db_item_ids_manager.erl207
-rw-r--r--src/db/struct/db_model.erl81
4 files changed, 9 insertions, 429 deletions
diff --git a/src/db/db_node.erl b/src/db/db_node.erl
index 30a29e2..763653a 100644
--- a/src/db/db_node.erl
+++ b/src/db/db_node.erl
@@ -25,16 +25,16 @@ wait_for_stop () ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec start () -> 'ok'.
start () ->
- Mnesia = db_model:new("/tmp/to_db_node.mnesia", []),
- db_model:start(Mnesia),
- db_model:add_db(battle_db, Mnesia),
- db_model:add_db(login_db, Mnesia),
- db_model:add_db(map_db, Mnesia),
- db_model:add_db(player_db, Mnesia),
- db_model:add_db(roster_db, Mnesia),
- db_model:add_db(inventory_db, Mnesia),
+ Mnesia = ataxia_admin:new("/tmp/to_db_node.mnesia", []),
+ ataxia_admin:start(Mnesia),
+ ataxia_admin:add_db(battle_db, Mnesia),
+ ataxia_admin:add_db(login_db, Mnesia),
+ ataxia_admin:add_db(map_db, Mnesia),
+ ataxia_admin:add_db(player_db, Mnesia),
+ ataxia_admin:add_db(roster_db, Mnesia),
+ ataxia_admin:add_db(inventory_db, Mnesia),
- ok = db_item_ids_manager:start(),
+ ok = ataxia_id_manager:start(),
wait_for_stop(),
ok.
diff --git a/src/db/logic/db_access.erl b/src/db/logic/db_access.erl
deleted file mode 100644
index 39019a6..0000000
--- a/src/db/logic/db_access.erl
+++ /dev/null
@@ -1,132 +0,0 @@
--module(db_access).
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--export
-(
- [
- read/3,
- remove/3,
- reserve/3,
- insert_at/5,
- insert/4,
- query/1
- ]
-).
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% 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 add_new_item (atom(), shr_db_item:type()) -> 'ok'.
-add_new_item (DB, Item) ->
- ID = shr_db_item:get_id(Item),
- [] = mnesia:read(DB, ID),
-
- mnesia:write(DB, Item, sticky_write),
-
- ok.
-
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--spec read
- (
- atom(),
- binary(),
- shr_db_user:user()
- )
- -> ({'aborted', any()} | {'atomic', ({'ok', any()} | 'not_found')}).
-read (DB, ID, Cred) ->
- case mnesia:transaction(fun mnesia:read/2, [DB, ID]) of
- {'atomic', []} -> {'atomic', 'not_found'};
- {'atomic', [Item]} ->
- true =
- shr_db_user:can_access
- (
- shr_db_item:get_read_permission(Item),
- Cred
- ),
- {'atomic', {ok, shr_db_item:get_value(Item)}};
-
- Other -> {'aborted', Other}
- end.
-
--spec insert_at
- (
- atom(),
- binary(),
- shr_db_user:permission(),
- shr_db_user:permission(),
- any())
- -> ({'aborted', any()} | {'atomic', 'ok'}).
-insert_at (DB, ID, ReadPerm, WritePerm, Value) ->
- Item = shr_db_item:new(ID, ReadPerm, WritePerm, Value),
- mnesia:transaction(fun add_new_item/2, [DB, Item]).
-
--spec insert
- (
- atom(),
- shr_db_user:permission(),
- shr_db_user:permission(),
- any())
- -> ({'aborted', any()} | {'atomic', {'ok', binary()}}).
-insert (DB, ReadPerm, WritePerm, Value) ->
- ID = db_item_ids_manager:allocate(DB),
- case insert_at(DB, ID, ReadPerm, WritePerm, Value) of
- {'atomic', 'ok'} -> {'atomic', {'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(),
- binary(),
- shr_db_user:user()
- )
- -> ({'aborted', any()} | {'atomic', 'ok'}).
-reserve (DB, ID, Cred) ->
- insert_at
- (
- DB,
- ID,
- [Cred],
- [Cred],
- {
- reserved,
- <<"?">> %% TODO [FUNCTION: db][LOW]: timestamp
- }
- ).
-
--spec remove
- (
- atom(),
- binary(),
- shr_db_user:user()
- )
- -> ({'aborted', any()} | {'atomic', ({'ok', any()} | 'not_found')}).
-remove (_DB, _ID, _Cred) ->
- %% TODO [FUNCTION: db][MEDIUM]: unimplemented
- %% Don't forget to checkt that Cred has write access before removing the
- %% value.
- {'aborted', 'unimplemented'}.
diff --git a/src/db/struct/db_item_ids_manager.erl b/src/db/struct/db_item_ids_manager.erl
deleted file mode 100644
index e2af8a6..0000000
--- a/src/db/struct/db_item_ids_manager.erl
+++ /dev/null
@@ -1,207 +0,0 @@
--module(db_item_ids_manager).
--behavior(gen_server).
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--record
-(
- entry,
- {
- last_id :: binary(),
- freed_ids :: list(binary())
- }
-).
-
--type entry() :: #entry{}.
-
-%% FIXME: IDs should be generated in a decentralized fashion.
-%% TODO: How to handle IDs management with the redundancy DBs?
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%% 'gen_server' Exports
--export
-(
- [
- init/1,
- handle_cast/2,
- handle_call/3, %% No reply will ever be given.
- terminate/2,
- code_change/3,
- format_status/2,
- handle_info/2
- ]
-).
-
-%%%% Actual Interface
--export
-(
- [
- allocate/1,
- free/2,
- start/0
- ]
-).
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% 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 = <<"">>,
- freed_ids = []
- }.
-
--spec allocate_id_in_entry (entry()) -> {entry(), binary()}.
-allocate_id_in_entry (Entry) ->
- case Entry#entry.freed_ids of
- [] ->
- NewID = next_id(Entry#entry.last_id),
- {
- Entry#entry{ last_id = NewID },
- NewID
- };
-
- [OldID|OtherOldIDs] ->
- {
- Entry#entry{ freed_ids = OtherOldIDs },
- OldID
- }
- end.
-
--spec allocate_id
- (
- atom(),
- dict:dict(atom(), entry())
- )
- -> {dict:dict(atom(), entry()), binary()}.
-allocate_id (DB, State) ->
- S0Entry =
- case dict:find(DB, State) of
- {ok, Value} -> Value;
- error -> new_entry()
- end,
-
- {S1Entry, NewID} = allocate_id_in_entry(S0Entry),
-
- S0State = dict:store(DB, S1Entry, State),
-
- {S0State, NewID}.
-
--spec free_id
- (
- binary(),
- atom(),
- dict:dict(atom(), entry())
- )
- -> dict:dict(atom(), entry()).
-free_id (ID, DB, State) ->
- Entry =
- case dict:find(DB, State) of
- {ok, Value} -> Value;
- error -> new_entry()
- end,
-
- EntryFreedIDs = Entry#entry.freed_ids,
-
- case lists:member(ID, EntryFreedIDs) of
- true -> State;
- false ->
- Result =
- dict:store
- (
- DB,
- Entry#entry{ freed_ids = [ID|EntryFreedIDs] },
- State
- ),
-
- Result
- end.
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%% 'gen_server' functions
-init (_) -> {ok, dict:new()}.
-
-handle_call ({allocate, DB}, _, State) ->
- {NewState, NewID} = allocate_id(DB, State),
- {reply, {allocated_id, NewID}, NewState};
-handle_call ({free, ID, DB}, _, State) ->
- {noreply, free_id(ID, DB, State)}.
-
-handle_cast ({allocate, _DB}, State) ->
- {noreply, State};
-handle_cast ({free, ID, DB}, State) ->
- {noreply, free_id(ID, DB, State)}.
-
-terminate (_, _) -> ok.
-
-code_change (_, State, _) ->
- {ok, State}.
-
-format_status (_, [_, State]) ->
- [{data, [{"State", State}]}].
-
-handle_info(_, State) ->
- {noreply, State}.
-
-%%%% Interface Functions
--spec allocate (atom()) -> binary().
-allocate (DB) ->
- {allocated_id, Result} =
- gen_server:call({global, db_item_ids_manager}, {allocate, DB}),
-
- io:format("~n[DB: ~p] Item ID ~p allocated.~n", [DB, Result]),
-
- Result.
-
--spec free (binary(), atom()) -> 'ok'.
-free (ID, DB) ->
- gen_server:cast({global, db_item_ids_manager}, {free, ID, DB}),
-
- ok.
-
--spec start () -> 'ok'.
-start () ->
- {ok, _} = gen_server:start({global, db_item_ids_manager}, ?MODULE, none, []),
-
- ok.
diff --git a/src/db/struct/db_model.erl b/src/db/struct/db_model.erl
deleted file mode 100644
index 2cd58d4..0000000
--- a/src/db/struct/db_model.erl
+++ /dev/null
@@ -1,81 +0,0 @@
--module(db_model).
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
--record
-(
- db_model,
- {
- store_file :: string(),
- neighbors :: list(node())
- }
-).
-
--type type() :: #db_model{}.
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--export_type([type/0]).
-
--export
-(
- [
- new/2,
- add_db/2,
- start/1
- ]
-).
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--spec new (string(), list(node())) -> type().
-new (StorageFile, Neighbors) ->
- #db_model
- {
- store_file = StorageFile,
- neighbors = Neighbors
- }.
-
--spec start(type()) -> 'ok'.
-start (Model) ->
- StorageFile = Model#db_model.store_file,
- Neighbors = Model#db_model.neighbors,
-
- ok = application:set_env(mnesia, dir, StorageFile),
-
- case mnesia:create_schema([node()|Neighbors]) of
- {error, {Name, {already_exists, Name}}} -> ok;
- ok -> ok
- end,
-
- ok = mnesia:start(),
-
- ok.
-
--spec add_db (atom(), type()) -> 'ok'.
-add_db (DBName, Model) ->
- Neighbors = Model#db_model.neighbors,
-
- mnesia:create_table
- (
- DBName,
- [
- {record_name, shr_db_item:get_record_name()},
- {attributes, shr_db_item:get_record_info()},
- {disc_copies, [node()|Neighbors]},
- {disc_only_copies, []},
- {ram_copies, []},
- {type, ordered_set},
- {local_content, false}
- ]
- ),
-
- ok.