/
couch_config.erl
82 lines (69 loc) · 2.61 KB
/
couch_config.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
%%%-------------------------------------------------------------------
%%% @author James Aimonetti <james@2600hz.org>
%%% @copyright (C) 2011, VoIP INC
%%% @doc
%%% Access and cache config options for Whistle Couch
%%% It is important to realize that configs are stored
%%% with the key as part of the value.
%%%
%%% So {foo, bar, baz} is internally stored as:
%%% { foo, {foo, bar, baz} }
%%% fetch/1,2,3 will use foo as the key, and return the full tuple.
%%% @end
%%% Created : 28 Oct 2011 by James Aimonetti <james@2600hz.org>
%%%-------------------------------------------------------------------
-module(couch_config).
-export([start_link/0, ready/0]).
-export([load_config/1, write_config/1]).
-export([fetch/1, fetch/2]).
-export([store/2, store/3]).
-include("wh_couch.hrl").
-define(CONFIG_CAT, <<"whistle_couch">>).
-spec start_link/0 :: () -> 'ignore'.
start_link() ->
_ = load_config(?CONFIG_FILE_PATH),
?LOG("loaded couch configs"),
ignore.
-spec load_config/1 :: (file:name()) -> 'ok' | {'error', 'enoent'}.
load_config(Path) ->
?LOG("loading ~s", [Path]),
case file:consult(Path) of
{ok, Startup} ->
_ = [cache_from_file(T) || T <- Startup],
ok;
{error, enoent}=E ->
E
end.
%% convert 3..n-tuples to 2 tuples with the value being (3..n)-1 tuples
%% so {key, v1, v2, v3} becomes {key, {v1, v2, v3}}
%% subsequent writes back to the file will store in the new format
cache_from_file({Key, Value}) ->
store(Key, Value);
cache_from_file(T) when is_tuple(T) ->
[Key|V] = erlang:tuple_to_list(T),
Value = erlang:list_to_tuple(V),
store(Key, Value).
-spec write_config/1 :: (file:name()) -> 'ok' | {'error', file:posix() | 'badarg' | 'terminated' | 'system_limit'}.
write_config(Path) ->
Contents = lists:foldl(fun(I, Acc) -> [io_lib:format("~p.~n", [I]) | Acc] end
, "", whapps_config:get_all_kvs(?CONFIG_CAT)),
file:write_file(Path, Contents).
ready() ->
whapps_config:couch_ready().
fetch(Key) ->
fetch(Key, undefined).
fetch(Key, Cache) when is_pid(Cache) ->
fetch(Key, undefined, Cache);
fetch(Key, Default) ->
whapps_config:get(?CONFIG_CAT, Key, Default).
fetch(Key, Default, Cache) ->
case wh_cache:fetch_local(Cache, {?MODULE, Key}) of
{error, not_found} -> Default;
{ok, V} -> V
end.
-spec store/2 :: (term(), term()) -> 'ok'.
store(Key, Value) ->
whapps_config:set(?CONFIG_CAT, Key, Value).
-spec store/3 :: (term(), term(), pid()) -> 'ok'.
store(Key, Value, Cache) when is_pid(Cache) ->
wh_cache:store_local(Cache, {?MODULE, Key}, Value, ?MILLISECONDS_IN_DAY).