From df38ecb4975f250f7a93b7327f8244d618d5ab99 Mon Sep 17 00:00:00 2001 From: Adam Kocoloski Date: Fri, 23 Oct 2009 16:49:52 +0000 Subject: improved speed and concurrency of config lookups git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@829123 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_config.erl | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/couchdb/couch_config.erl b/src/couchdb/couch_config.erl index 5d911455..d8473e08 100644 --- a/src/couchdb/couch_config.erl +++ b/src/couchdb/couch_config.erl @@ -50,7 +50,8 @@ all() -> get(Section) when is_binary(Section) -> ?MODULE:get(?b2l(Section)); get(Section) -> - gen_server:call(?MODULE, {get, Section}). + Matches = ets:match(?MODULE, {{Section, '$1'}, '$2'}), + [{Key, Value} || [Key, Value] <- Matches]. get(Section, Key) -> ?MODULE:get(Section, Key, undefined). @@ -58,8 +59,10 @@ get(Section, Key) -> get(Section, Key, Default) when is_binary(Section) and is_binary(Key) -> ?MODULE:get(?b2l(Section), ?b2l(Key), Default); get(Section, Key, Default) -> - gen_server:call(?MODULE, {get, Section, Key, Default}). - + case ets:lookup(?MODULE, {Section, Key}) of + [] -> Default; + [{_, Match}] -> Match + end. set(Section, Key, Value) -> ?MODULE:set(Section, Key, Value, true). @@ -89,7 +92,7 @@ register(Fun, Pid) -> init(IniFiles) -> - ets:new(?MODULE, [named_table, set, private]), + ets:new(?MODULE, [named_table, set, protected]), lists:map(fun(IniFile) -> {ok, ParsedIniValues} = parse_ini_file(IniFile), ets:insert(?MODULE, ParsedIniValues) @@ -108,16 +111,6 @@ terminate(_Reason, _State) -> handle_call(all, _From, Config) -> Resp = lists:sort((ets:tab2list(?MODULE))), {reply, Resp, Config}; -handle_call({get, Section}, _From, Config) -> - Matches = ets:match(?MODULE, {{Section, '$1'}, '$2'}), - Resp = [{Key, Value} || [Key, Value] <- Matches], - {reply, Resp, Config}; -handle_call({get, Section, Key, Default}, _From, Config) -> - Resp = case ets:lookup(?MODULE, {Section, Key}) of - [] -> Default; - [{_, Match}] -> Match - end, - {reply, Resp, Config}; handle_call({set, Sec, Key, Val, Persist}, _From, Config) -> true = ets:insert(?MODULE, {{Sec, Key}, Val}), case {Persist, Config#config.write_filename} of -- cgit v1.2.3