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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
-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'}.
|