From acb9dd3220a3edcac93aa11d1d74d008e2fb23ed Mon Sep 17 00:00:00 2001 From: nsensfel Date: Wed, 11 Jul 2018 18:02:26 +0200 Subject: "sh_" -> "shr_". --- src/shared/io/sh_database.erl | 58 ----------- src/shared/io/sh_timed_cache.erl | 130 ------------------------ src/shared/io/sh_timed_caches_manager.erl | 152 ----------------------------- src/shared/io/shr_database.erl | 58 +++++++++++ src/shared/io/shr_timed_cache.erl | 130 ++++++++++++++++++++++++ src/shared/io/shr_timed_caches_manager.erl | 152 +++++++++++++++++++++++++++++ 6 files changed, 340 insertions(+), 340 deletions(-) delete mode 100644 src/shared/io/sh_database.erl delete mode 100644 src/shared/io/sh_timed_cache.erl delete mode 100644 src/shared/io/sh_timed_caches_manager.erl create mode 100644 src/shared/io/shr_database.erl create mode 100644 src/shared/io/shr_timed_cache.erl create mode 100644 src/shared/io/shr_timed_caches_manager.erl (limited to 'src/shared/io') diff --git a/src/shared/io/sh_database.erl b/src/shared/io/sh_database.erl deleted file mode 100644 index 233e37d..0000000 --- a/src/shared/io/sh_database.erl +++ /dev/null @@ -1,58 +0,0 @@ --module(sh_database). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --export -( - [ - insert/4, - fetch/2, - commit/1 - ] -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec get_db_node () -> node(). -get_db_node () -> list_to_atom("db_node@" ++ net_adm:localhost()). - --spec do_remote_operation (atom(), list(any())) -> - ( - {'badrpc', any()} - | {'aborted', any()} - | {'atomic', ({'ok', any()} | 'ok' | 'not_found')} - ). -do_remote_operation (Op, Params) -> - rpc:call(get_db_node(), db_access, Op, Params). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec insert (atom(), any(), sh_db_user:permission(), any()) -> 'ok'. -insert (DB, ObjectID, Permission, Value) -> - {atomic, _} = do_remote_operation(insert, [DB, ObjectID, Permission, Value]), - io:format - ( - "~nsh_database:insert(~p) -> ok.~n", - [{DB, ObjectID, Permission, Value}] - ), - - ok. - --spec fetch (atom(), any()) -> ({'ok', any()} | 'not_found'). -fetch (DB, ObjectID) -> - {atomic, Reply} = do_remote_operation(read, [DB, ObjectID]), - io:format("~nsh_database:fetch(~p) -> ~p.~n", [{DB, ObjectID}, Reply]), - Reply. - --spec commit (sh_db_query:type()) -> 'ok'. -commit (Query) -> - {atomic, ok} = do_remote_operation(query, [Query]), - io:format("~nsh_database:commit(~p) -> ok.~n", [Query]), - ok. diff --git a/src/shared/io/sh_timed_cache.erl b/src/shared/io/sh_timed_cache.erl deleted file mode 100644 index 6f3d973..0000000 --- a/src/shared/io/sh_timed_cache.erl +++ /dev/null @@ -1,130 +0,0 @@ --module(sh_timed_cache). --behavior(gen_server). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% 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 -( - [ - fetch/3, - update/4, - invalidate/3 - ] -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --spec add_to_cache (atom(), any(), any()) -> any(). -add_to_cache (DB, Owner, ObjectID) -> - {ok, TimerPID} = gen_server:start(?MODULE, {DB, {Owner, ObjectID}}, []), - {ok, Data} = sh_database:fetch(DB, ObjectID), - ets:insert(DB, {{Owner, ObjectID}, TimerPID, Data}), - Data. - --spec add_update_to_cache (atom(), any(), any(), any()) -> 'ok'. -add_update_to_cache (DB, Owner, ObjectID, Data) -> - {ok, TimerPID} = gen_server:start(?MODULE, {DB, {Owner, ObjectID}}, []), - ets:insert(DB, {{Owner, ObjectID}, TimerPID, Data}), - ok. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%% 'gen_server' functions -init ({DB, ObjectID}) -> - io:format("~nCache entry added: ~p.~n", [{DB, ObjectID}]), - {ok, {DB, ObjectID}, sh_timed_caches_manager:get_timeout()}. - -handle_call (invalidate, _, State) -> - {stop, normal, State}; -handle_call (ping, _, State) -> - {noreply, State, sh_timed_caches_manager:get_timeout()}. - -handle_cast (invalidate, State) -> - {stop, normal, State}; -handle_cast (ping, State) -> - {noreply, State, sh_timed_caches_manager:get_timeout()}. - -terminate (_, {DB, ObjectID}) -> - io:format - ( - "~nCache entry timed out or was invalidated: ~p.~n", - [{DB, ObjectID}] - ), - ets:delete(DB, ObjectID). - -code_change (_, State, _) -> - {ok, State}. - -format_status (_, [_, State]) -> - [{data, [{"State", State}]}]. - -handle_info(timeout, State) -> - {stop, normal, State}; -handle_info(_, {DB, ObjectID}) -> - {noreply, {DB, ObjectID}, sh_timed_caches_manager:get_timeout()}. - -%%%% Interface Functions --spec fetch (atom(), any(), any()) -> any(). -fetch (DB, Owner, ObjectID) -> - io:format("~nfetch from cache: ~p.~n", [{DB, {Owner, ObjectID}}]), - case ets:lookup(DB, {Owner, ObjectID}) of - [] -> add_to_cache(DB, Owner, ObjectID); - - [{_, TimerPID, Data}] -> - gen_server:cast(TimerPID, ping), - Data - end. - --spec update (atom(), any(), any(), any()) -> 'ok'. -update (DB, Owner, ObjectID, Data) -> - io:format("~nUpdating cache: ~p.~n", [{DB, {Owner, ObjectID}}]), - case ets:lookup(DB, {Owner, ObjectID}) of - [] -> ok; - - [{_OwnerID, TimerPID, _Data}] -> - gen_server:stop(TimerPID) - end, - add_update_to_cache(DB, Owner, ObjectID, Data). - --spec invalidate (atom(), any(), any()) -> 'ok'. -invalidate (DB, Owner, ObjectID) -> - case ets:lookup(DB, {Owner, ObjectID}) of - [] -> - io:format - ( - "~nInvalidation request on non-stored entry: ~p.~n", - [{DB, Owner, ObjectID}] - ), - ok; - - [{_, TimerPID, _}] -> - io:format - ( - "~nInvalidation request on stored entry: ~p.~n", - [{DB, Owner, ObjectID}] - ), - gen_server:stop(TimerPID), - ok - end. diff --git a/src/shared/io/sh_timed_caches_manager.erl b/src/shared/io/sh_timed_caches_manager.erl deleted file mode 100644 index ea92c08..0000000 --- a/src/shared/io/sh_timed_caches_manager.erl +++ /dev/null @@ -1,152 +0,0 @@ --module(sh_timed_caches_manager). --behavior(gen_server). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%% 'gen_server' Exports --export( - [ - init/1, - handle_cast/2, - handle_call/3, - terminate/2, - code_change/3, - format_status/2, - handle_info/2 - ] -). - -%%%% Actual Interface --export( - [ - start/0, - new_cache/3, - delete_cache/2, - get_timeout/0 - ] -) -. -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -remove_cache (DB) -> - ets:delete(DB). - -add_cache (DB, none) -> - io:format("~nTimed Caches Manager added a new cache. ~n"), - ets:new( - DB, - [ - set, - public, - named_table, - {keypos, 1}, - {read_concurrency, true}, - {heir, none} - ] - ); -add_cache (DB, Heir) -> - io:format("~nTimed Caches Manager added a new cache. ~n"), - ets:new( - DB, - [ - set, - public, - named_table, - {keypos, 1}, - {read_concurrency, true}, - {heir, Heir, DB} - ] - ). - -inherit_cache (CacheList, DB, Heir) -> - case lists:member(DB, CacheList) of - true -> - ets:setopts(DB, {heir, Heir, DB}), - CacheList; - - false -> - [DB|CacheList] - end. - -remove_cache (CacheList, DB) -> - case lists:member(DB, CacheList) of - true -> - remove_cache(DB), - lists:delete(DB, CacheList); - false -> - CacheList - end. - -add_cache (CacheList, DB, Heir) -> - case lists:member(DB, CacheList) of - true when (Heir =:= none) -> - CacheList; - - true -> - ets:setopts(DB, {heir, Heir, DB}), - CacheList; - - false -> - add_cache(DB, Heir), - [DB|CacheList] - end. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%% 'gen_server' functions -init (CacheList) -> - io:format("~nStarting Timed Caches Manager..."), - {ok, CacheList}. - -handle_call ({remove, CacheName}, _Caller, State) -> - {noreply, remove_cache(State, CacheName)}; -handle_call ({add, CacheName, Heir}, _Caller, State)-> - {noreply, add_cache(State, CacheName, Heir)}; -handle_call ({inherit, CacheName, Heir}, _Caller, State)-> - {noreply, inherit_cache(State, CacheName, Heir)}; -handle_call (terminate, _, State) -> - {stop, normal, State}. - -handle_cast ({remove, CacheName}, State) -> - {noreply, remove_cache(State, CacheName)}; -handle_cast ({add, CacheName, Heir}, State)-> - {noreply, add_cache(State, CacheName, Heir)}; -handle_cast ({inherit, CacheName, Heir}, State)-> - {noreply, inherit_cache(State, CacheName, Heir)}; -handle_cast (terminate, State) -> - {stop, normal, State}. - -terminate (_Reason, []) -> - ok; -terminate (Reason, [CacheName|OtherCaches]) -> - remove_cache(CacheName), - terminate(Reason, OtherCaches). - -code_change (_, State, _) -> - {ok, State}. - -format_status (_, [_, State]) -> - [{data, [{"State", State}]}]. - -handle_info(_, State) -> - {noreply, State}. - -%%%% Interface Functions -start () -> - gen_server:start(?MODULE, [], []). - -new_cache (ManagerPid, DB, Heir) -> - gen_server:cast(ManagerPid, {add, DB, Heir}). - -delete_cache (ManagerPid, DB) -> - gen_server:cast(ManagerPid, {remove, DB}). - -get_timeout () -> - 120000. % 2min. diff --git a/src/shared/io/shr_database.erl b/src/shared/io/shr_database.erl new file mode 100644 index 0000000..bffcb9f --- /dev/null +++ b/src/shared/io/shr_database.erl @@ -0,0 +1,58 @@ +-module(shr_database). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-export +( + [ + insert/4, + fetch/2, + commit/1 + ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec get_db_node () -> node(). +get_db_node () -> list_to_atom("db_node@" ++ net_adm:localhost()). + +-spec do_remote_operation (atom(), list(any())) -> + ( + {'badrpc', any()} + | {'aborted', any()} + | {'atomic', ({'ok', any()} | 'ok' | 'not_found')} + ). +do_remote_operation (Op, Params) -> + rpc:call(get_db_node(), db_access, Op, Params). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec insert (atom(), any(), shr_db_user:permission(), any()) -> 'ok'. +insert (DB, ObjectID, Permission, Value) -> + {atomic, _} = do_remote_operation(insert, [DB, ObjectID, Permission, Value]), + io:format + ( + "~nshr_database:insert(~p) -> ok.~n", + [{DB, ObjectID, Permission, Value}] + ), + + ok. + +-spec fetch (atom(), any()) -> ({'ok', any()} | 'not_found'). +fetch (DB, ObjectID) -> + {atomic, Reply} = do_remote_operation(read, [DB, ObjectID]), + io:format("~nshr_database:fetch(~p) -> ~p.~n", [{DB, ObjectID}, Reply]), + Reply. + +-spec commit (shr_db_query:type()) -> 'ok'. +commit (Query) -> + {atomic, ok} = do_remote_operation(query, [Query]), + io:format("~nshr_database:commit(~p) -> ok.~n", [Query]), + ok. diff --git a/src/shared/io/shr_timed_cache.erl b/src/shared/io/shr_timed_cache.erl new file mode 100644 index 0000000..b89de48 --- /dev/null +++ b/src/shared/io/shr_timed_cache.erl @@ -0,0 +1,130 @@ +-module(shr_timed_cache). +-behavior(gen_server). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% 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 +( + [ + fetch/3, + update/4, + invalidate/3 + ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec add_to_cache (atom(), any(), any()) -> any(). +add_to_cache (DB, Owner, ObjectID) -> + {ok, TimerPID} = gen_server:start(?MODULE, {DB, {Owner, ObjectID}}, []), + {ok, Data} = shr_database:fetch(DB, ObjectID), + ets:insert(DB, {{Owner, ObjectID}, TimerPID, Data}), + Data. + +-spec add_update_to_cache (atom(), any(), any(), any()) -> 'ok'. +add_update_to_cache (DB, Owner, ObjectID, Data) -> + {ok, TimerPID} = gen_server:start(?MODULE, {DB, {Owner, ObjectID}}, []), + ets:insert(DB, {{Owner, ObjectID}, TimerPID, Data}), + ok. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% 'gen_server' functions +init ({DB, ObjectID}) -> + io:format("~nCache entry added: ~p.~n", [{DB, ObjectID}]), + {ok, {DB, ObjectID}, shr_timed_caches_manager:get_timeout()}. + +handle_call (invalidate, _, State) -> + {stop, normal, State}; +handle_call (ping, _, State) -> + {noreply, State, shr_timed_caches_manager:get_timeout()}. + +handle_cast (invalidate, State) -> + {stop, normal, State}; +handle_cast (ping, State) -> + {noreply, State, shr_timed_caches_manager:get_timeout()}. + +terminate (_, {DB, ObjectID}) -> + io:format + ( + "~nCache entry timed out or was invalidated: ~p.~n", + [{DB, ObjectID}] + ), + ets:delete(DB, ObjectID). + +code_change (_, State, _) -> + {ok, State}. + +format_status (_, [_, State]) -> + [{data, [{"State", State}]}]. + +handle_info(timeout, State) -> + {stop, normal, State}; +handle_info(_, {DB, ObjectID}) -> + {noreply, {DB, ObjectID}, shr_timed_caches_manager:get_timeout()}. + +%%%% Interface Functions +-spec fetch (atom(), any(), any()) -> any(). +fetch (DB, Owner, ObjectID) -> + io:format("~nfetch from cache: ~p.~n", [{DB, {Owner, ObjectID}}]), + case ets:lookup(DB, {Owner, ObjectID}) of + [] -> add_to_cache(DB, Owner, ObjectID); + + [{_, TimerPID, Data}] -> + gen_server:cast(TimerPID, ping), + Data + end. + +-spec update (atom(), any(), any(), any()) -> 'ok'. +update (DB, Owner, ObjectID, Data) -> + io:format("~nUpdating cache: ~p.~n", [{DB, {Owner, ObjectID}}]), + case ets:lookup(DB, {Owner, ObjectID}) of + [] -> ok; + + [{_OwnerID, TimerPID, _Data}] -> + gen_server:stop(TimerPID) + end, + add_update_to_cache(DB, Owner, ObjectID, Data). + +-spec invalidate (atom(), any(), any()) -> 'ok'. +invalidate (DB, Owner, ObjectID) -> + case ets:lookup(DB, {Owner, ObjectID}) of + [] -> + io:format + ( + "~nInvalidation request on non-stored entry: ~p.~n", + [{DB, Owner, ObjectID}] + ), + ok; + + [{_, TimerPID, _}] -> + io:format + ( + "~nInvalidation request on stored entry: ~p.~n", + [{DB, Owner, ObjectID}] + ), + gen_server:stop(TimerPID), + ok + end. diff --git a/src/shared/io/shr_timed_caches_manager.erl b/src/shared/io/shr_timed_caches_manager.erl new file mode 100644 index 0000000..8303cb8 --- /dev/null +++ b/src/shared/io/shr_timed_caches_manager.erl @@ -0,0 +1,152 @@ +-module(shr_timed_caches_manager). +-behavior(gen_server). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% 'gen_server' Exports +-export( + [ + init/1, + handle_cast/2, + handle_call/3, + terminate/2, + code_change/3, + format_status/2, + handle_info/2 + ] +). + +%%%% Actual Interface +-export( + [ + start/0, + new_cache/3, + delete_cache/2, + get_timeout/0 + ] +) +. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +remove_cache (DB) -> + ets:delete(DB). + +add_cache (DB, none) -> + io:format("~nTimed Caches Manager added a new cache. ~n"), + ets:new( + DB, + [ + set, + public, + named_table, + {keypos, 1}, + {read_concurrency, true}, + {heir, none} + ] + ); +add_cache (DB, Heir) -> + io:format("~nTimed Caches Manager added a new cache. ~n"), + ets:new( + DB, + [ + set, + public, + named_table, + {keypos, 1}, + {read_concurrency, true}, + {heir, Heir, DB} + ] + ). + +inherit_cache (CacheList, DB, Heir) -> + case lists:member(DB, CacheList) of + true -> + ets:setopts(DB, {heir, Heir, DB}), + CacheList; + + false -> + [DB|CacheList] + end. + +remove_cache (CacheList, DB) -> + case lists:member(DB, CacheList) of + true -> + remove_cache(DB), + lists:delete(DB, CacheList); + false -> + CacheList + end. + +add_cache (CacheList, DB, Heir) -> + case lists:member(DB, CacheList) of + true when (Heir =:= none) -> + CacheList; + + true -> + ets:setopts(DB, {heir, Heir, DB}), + CacheList; + + false -> + add_cache(DB, Heir), + [DB|CacheList] + end. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% 'gen_server' functions +init (CacheList) -> + io:format("~nStarting Timed Caches Manager..."), + {ok, CacheList}. + +handle_call ({remove, CacheName}, _Caller, State) -> + {noreply, remove_cache(State, CacheName)}; +handle_call ({add, CacheName, Heir}, _Caller, State)-> + {noreply, add_cache(State, CacheName, Heir)}; +handle_call ({inherit, CacheName, Heir}, _Caller, State)-> + {noreply, inherit_cache(State, CacheName, Heir)}; +handle_call (terminate, _, State) -> + {stop, normal, State}. + +handle_cast ({remove, CacheName}, State) -> + {noreply, remove_cache(State, CacheName)}; +handle_cast ({add, CacheName, Heir}, State)-> + {noreply, add_cache(State, CacheName, Heir)}; +handle_cast ({inherit, CacheName, Heir}, State)-> + {noreply, inherit_cache(State, CacheName, Heir)}; +handle_cast (terminate, State) -> + {stop, normal, State}. + +terminate (_Reason, []) -> + ok; +terminate (Reason, [CacheName|OtherCaches]) -> + remove_cache(CacheName), + terminate(Reason, OtherCaches). + +code_change (_, State, _) -> + {ok, State}. + +format_status (_, [_, State]) -> + [{data, [{"State", State}]}]. + +handle_info(_, State) -> + {noreply, State}. + +%%%% Interface Functions +start () -> + gen_server:start(?MODULE, [], []). + +new_cache (ManagerPid, DB, Heir) -> + gen_server:cast(ManagerPid, {add, DB, Heir}). + +delete_cache (ManagerPid, DB) -> + gen_server:cast(ManagerPid, {remove, DB}). + +get_timeout () -> + 120000. % 2min. -- cgit v1.2.3-70-g09d2