-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_db.erl
55 lines (46 loc) · 961 Bytes
/
my_db.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
-module(my_db).
-export([start/0,stop/0]).
-export([write/2,delete/1,read/1,match/1]).
-export([init/0]).
start()->
register(my_db,spawn(?MODULE,init,[])).
stop()->
my_db!{stop,self()},
receive ok->ok end.
init()->
Db=db:new(),
loop(Db).
reply(To,Msg)->
To!{reply,Msg}.
call(Msg)->
my_db!{request,self(),Msg},
receive
{reply,Reply}->Reply
end.
loop(Db)->
receive
{request,From,Msg}->
{Reply,NewDb}=handle_msg(Msg,Db),
reply(From,Reply),
loop(NewDb);
{stop,From}->
reply(From,terminate(Db))
end.
write(Key,Element)->
call({write,{Key,Element}}).
delete(Key)->
call({delete,Key}).
read(Key)->
call({read,Key}).
match(Element)->
call({match,Element}).
handle_msg({write,{Key,Element}},Db)->
{ok,db:write(Key,Element,Db)};
handle_msg({delete,Key},Db)->
{ok,db:delete(Key,Db)};
handle_msg({read,Key},Db)->
{db:read(Key,Db),Db};
handle_msg({match,Element},Db)->
{db:match(Element,Db),Db}.
terminate(Db)->
db:destroy(Db).