-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage_manager.erl
84 lines (68 loc) · 2.17 KB
/
storage_manager.erl
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
%%%-------------------------------------------------------------------
%%% @author Burak OGUZ <burak@medratech.com>
%%% @copyright 2010, Burak OGUZ
%%% @doc Storage Manager for BSSE Web Application.
%%% @end
%%%-------------------------------------------------------------------
-module(storage_manager).
-author("burak@medratech.com").
-behaviour(gen_server).
%% API
-export([start_link/0,
stop/0,
get_objects/1,
run_transaction/1,
insert_object/1,
update_object/1,
delete_object/1]).
%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
-include("bsse_records.hrl").
-record(state, {id=0}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() ->
gen_server:call({local,?MODULE}, stop).
get_objects(Function) ->
gen_server:call(?MODULE, {get_objects, Function}).
run_transaction(Function) ->
gen_server:call(?MODULE, {run_transaction, Function}).
insert_object(Function) ->
gen_server:call(?MODULE, {run_transaction, Function}).
update_object(Function) ->
gen_server:call(?MODULE, {run_transaction, Function}).
delete_object(Function) ->
gen_server:call(?MODULE, {run_transaction, Function}).
init([]) ->
mnesia:create_schema([node()]),
mnesia:start(),
try
mnesia:table_info(user, type)
catch
exit: _->
mnesia:create_table(user,[{attributes, record_info(fields, user)},
{type, bag},
{disc_copies, [node()]}])
end,
{ok, #state{}}.
handle_call({get_objects, Function}, _From, State) ->
{atomic, Users} = mnesia:transaction(Function),
{reply, Users, State};
handle_call({run_transaction, Function}, _From, State) ->
mnesia:transaction(Function),
{reply, [], State};
handle_call(stop, _From, State) ->
{stop, normalStop, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Msg, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.