summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_config.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/couchdb/couch_config.erl')
-rw-r--r--src/couchdb/couch_config.erl86
1 files changed, 26 insertions, 60 deletions
diff --git a/src/couchdb/couch_config.erl b/src/couchdb/couch_config.erl
index 48386949..6822dc5c 100644
--- a/src/couchdb/couch_config.erl
+++ b/src/couchdb/couch_config.erl
@@ -23,10 +23,9 @@
-export([start_link/1, init/1,
handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
--export([store/2, register/1, register/2,
- get/1, get/2,
- lookup_match/1, lookup_match/2,
- all/0, unset/1, load_ini_file/1]).
+-export([set/3, register/1, register/2,
+ get/1, get/2, get/3,
+ all/0, delete/2, load_ini_file/1]).
-record(config,
{notify_funs=[],
@@ -39,44 +38,30 @@
start_link(IniFiles) -> gen_server:start_link({local, ?MODULE}, ?MODULE, IniFiles, []).
-%% @spec store(Key::any(), Value::any()) -> {ok, Tab::etsatable()}
-%% @doc Public API function that triggers storage of a Key/Value pair into the
-%% local ets table and writes it to the storage ini file.
-store(Key, Value) -> gen_server:call(?MODULE, {store, [{Key, Value}]}).
-
-%% @spec get(Key::any()) -> Value::any() | undefined
-%% @doc Returns the value that is stored under key::any() or undefined::atom() if no
-%% such Key exists.
-get(Key) ->
- ?MODULE:get(Key, undefined).
-
-%% @spec get(Key::any(), Default::any()) -> Value::any() | Default
-%% @doc Returns the value that is stored under key::any() or Default::any() if
-%% no such Key exists.
-get(Key, Default) ->
- fix_lookup_result(ets:lookup(?MODULE, Key), Default).
-
-%% @spec lookup_match(Key::any()) -> Value::any() | undefined:atom()
-%% @doc Lets you look for a Key's Value specifying a pattern that gets passed
-%% to ets::match(). Returns undefined::atom() if no Key is found.
-lookup_match(Key) -> gen_server:call(?MODULE, {lookup_match, Key}).
-
-%% @spec lookup_match(Key::any(), Default::any()) -> Value::any() | Default
-%% @doc Lets you look for a Key's Value specifying a pattern that gets passed
-%% to ets::match(). Returns Default::any() if no Key is found
-lookup_match(Key, Default) -> gen_server:call(?MODULE, {lookup_match, Key, Default}).
+set(Section, Key, Value) ->
+ gen_server:call(?MODULE, {set, [{{Section, Key}, Value}]}).
-all() -> gen_server:call(?MODULE, all).
+delete(Section, Key) ->
+ gen_server:call(?MODULE, {delete, {Section, Key}}).
-register(Fun) -> gen_server:call(?MODULE, {register, Fun, self()}).
+get(Section, Key) ->
+ get(Section, Key, undefined).
+get(Section, Key, Default) ->
+ case ets:lookup(?MODULE, {Section, Key}) of
+ [] -> Default;
+ [{_,Result}] -> Result
+ end.
+
+get(Section) ->
+ Matches = ets:match(?MODULE, {{Section, '$1'}, '$2'}),
+ [{Key, Value} || [Key, Value] <- Matches].
-register(Fun, Pid) -> gen_server:call(?MODULE, {register, Fun, Pid}).
+all() -> gen_server:call(?MODULE, all).
-%% @spec unset(Key::any) -> ok
-%% @doc Public API call to remove the configuration entry from the internal
-%% ets table. This change is _not_ written to the storage ini file.
-unset(Key) -> gen_server:call(?MODULE, {unset, Key}).
+register(Fun) -> ?MODULE:register(Fun, self()).
+
+register(Fun, Pid) -> gen_server:call(?MODULE, {register, Fun, Pid}).
%% Private API %%
@@ -87,42 +72,23 @@ init(IniFiles) ->
[ok = load_ini_file(IniFile) || IniFile <- IniFiles],
{ok, #config{writeback_filename=lists:last(IniFiles)}}.
-%% @doc see store/2
-handle_call({store, KVs}, _From, Config) ->
+handle_call({set, KVs}, _From, Config) ->
[ok = insert_and_commit(Config, KV) || KV <- KVs],
{reply, ok, Config};
-
-%% @doc See init_value/2
-handle_call({init_value, Key, Value}, _From, Config) ->
- Reply = ets:insert(?MODULE, {Key, Value}),
- {reply, Reply, Config};
-
-%% @doc See unset/1
-handle_call({unset, Key}, _From, Config) ->
+handle_call({delete, Key}, _From, Config) ->
ets:delete(?MODULE, Key),
{reply, ok, Config};
-
-%% @doc See lookup_match/2
-handle_call({lookup_match, Key, Default}, _From, Config) ->
- {reply, fix_lookup_result(ets:match(?MODULE, Key), Default), Config};
-
handle_call(all, _From, Config) ->
{reply, lists:sort(ets:tab2list(?MODULE)), Config};
-%% @doc See register/2
handle_call({register, Fun, Pid}, _From, #config{notify_funs=PidFuns}=Config) ->
erlang:monitor(process, Pid),
{reply, ok, Config#config{notify_funs=[{Pid, Fun}|PidFuns]}}.
-fix_lookup_result([{_Key, Value}], _Default) ->
- Value;
-fix_lookup_result([], Default) ->
- Default;
-fix_lookup_result(Values, _Default) ->
- [list_to_tuple(Value) || Value <- Values].
+
%% @spec insert_and_commit(Tab::etstable(), Config::any()) -> ok
%% @doc Inserts a Key/Value pair into the ets table, writes it to the storage
@@ -130,7 +96,7 @@ fix_lookup_result(Values, _Default) ->
insert_and_commit(Config, KV) ->
true = ets:insert(?MODULE, KV),
% notify funs
- %[catch Fun(KV) || {_Pid, Fun} <- Config#config.notify_funs],
+ [catch Fun(KV) || {_Pid, Fun} <- Config#config.notify_funs],
couch_config_writer:save_to_file(KV, Config#config.writeback_filename).
%% @spec load_ini_file(IniFile::filename()) -> ok