summaryrefslogtreecommitdiff
blob: 60327a5d60e63aa664c01e736ab60b7d3275039b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-module(shr_database).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export
(
   [
      insert/5,
      fetch/3,
      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(),
      shr_db_user:permission(),
      any()
   )
   -> 'ok'.
insert (DB, ObjectID, ReadPerm, WritePerm, Value) ->
   {atomic, _} =
      do_remote_operation(insert, [DB, ObjectID, ReadPerm, WritePerm, Value]),

   io:format
   (
      "~nshr_database:insert(~p) -> ok.~n",
      [{DB, ObjectID, ReadPerm, WritePerm, Value}]
   ),

   ok.

-spec fetch
   (
      atom(),
      any(),
      shr_db_user:user()
   )
   -> ({'ok', any()} | 'not_found').
fetch (DB, ObjectID, Cred) ->
   {atomic, Reply} = do_remote_operation(read, [DB, ObjectID, Cred]),
   io:format
   (
      "~nshr_database:fetch(~p) -> ~p.~n",
      [{DB, ObjectID, Cred}, 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.