forked from helium/erlang-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibp2p_cache.erl
148 lines (129 loc) · 4.46 KB
/
libp2p_cache.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
%%%-------------------------------------------------------------------
%% @doc
%% == Libp2p Cache ==
%% @end
%%%-------------------------------------------------------------------
-module(libp2p_cache).
-behavior(gen_server).
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export([
start_link/1,
insert/3,
lookup/2, lookup/3,
delete/2
]).
%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------
-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3
]).
-record(state, {
dets :: dets:tab_name()
}).
-define(MIGRATE, #{
tcp_listen_addrs => tcp_local_listen_addrs
}).
%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
start_link(TID) ->
gen_server:start_link(reg_name(TID), ?MODULE, [TID], []).
reg_name(TID)->
{local,libp2p_swarm:reg_name_from_tid(TID, ?MODULE)}.
%%--------------------------------------------------------------------
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec insert(pid(), any(), any()) -> ok | {error, any()}.
insert(Pid, Key, Value) ->
gen_server:call(Pid, {insert, Key, Value}, 30000).
%%--------------------------------------------------------------------
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec lookup(pid(), any()) -> undefined | any().
lookup(Pid, Key) ->
lookup(Pid, Key, undefined).
-spec lookup(pid(), any(), any()) -> undefined | any().
lookup(Pid, Key, Default) ->
gen_server:call(Pid, {lookup, Key, Default}).
%%--------------------------------------------------------------------
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec delete(pid(), any()) -> ok | {error, any()}.
delete(Pid, Key) ->
gen_server:call(Pid, {delete, Key}).
%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------
init([TID]) ->
erlang:process_flag(trap_exit, true),
libp2p_swarm_auxiliary_sup:register_cache(TID),
SwarmName = libp2p_swarm:name(TID),
DataDir = libp2p_config:base_dir(TID),
Opts = [{file, filename:join([DataDir, SwarmName, "cache.dets"])}],
{ok, Dets} = dets:open_file(SwarmName, Opts),
_ = migrate(Dets),
{ok, #state{dets=Dets}}.
handle_call({insert, Key, Value}, _From, #state{dets=Dets}=State) ->
Result = dets:insert(Dets, {Key, Value}),
{reply, Result, State};
handle_call({lookup, Key, Default}, _From, #state{dets=Dets}=State) ->
Result = case dets:lookup(Dets, Key) of
[] -> Default;
[{Key, Value}] -> Value;
[{Key, Value}|_]-> Value
end,
{reply, Result, State};
handle_call({delete, Key}, _From, #state{dets=Dets}=State) ->
Result = dets:delete(Dets, Key),
{reply, Result, State};
handle_call(_Msg, _From, State) ->
lager:warning("rcvd unknown call msg: ~p from: ~p", [_Msg, _From]),
{reply, ok, State}.
handle_cast(_Msg, State) ->
lager:warning("rcvd unknown cast msg: ~p", [_Msg]),
{noreply, State}.
handle_info(_Msg, State) ->
lager:warning("rcvd unknown info msg: ~p", [_Msg]),
{noreply, State}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
terminate(_Reason, #state{dets=Name}) ->
ok = dets:close(Name),
ok.
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
migrate(Dets) ->
maps:fold(
fun(Key, undefined, _) ->
dets:delete(Dets, Key);
(Key0, Key1, _) ->
case dets:lookup(Dets, Key0) of
[] ->
ok;
[{Key0, Value}] ->
dets:insert(Dets, {Key1, Value});
[{Key0, Value}|_]->
dets:insert(Dets, {Key1, Value})
end,
dets:delete(Dets, Key0)
end,
ok,
?MIGRATE
).
%% ------------------------------------------------------------------
%% EUNIT Tests
%% ------------------------------------------------------------------
-ifdef(TEST).
-endif.