Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2ACED1058B for ; Sun, 9 Feb 2014 08:49:27 +0000 (UTC) Received: (qmail 60428 invoked by uid 500); 9 Feb 2014 08:49:26 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 60304 invoked by uid 500); 9 Feb 2014 08:49:15 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 60295 invoked by uid 99); 9 Feb 2014 08:49:13 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 09 Feb 2014 08:49:13 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 37D0192100D; Sun, 9 Feb 2014 08:49:13 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: benoitc@apache.org To: commits@couchdb.apache.org Message-Id: <54033d875aad45f1b3e751c19540cf3a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: couchdb commit: updated refs/heads/1994-merge-rcouch to b5066d8 Date: Sun, 9 Feb 2014 08:49:13 +0000 (UTC) Updated Branches: refs/heads/1994-merge-rcouch c24654992 -> b5066d85e couch_index: don't try to get the config each time in the indexer Rather than fetching the threshold and refresh interval settings each time we need them, register the process to couch_config events so we can update the config only when needed. Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/b5066d85 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/b5066d85 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/b5066d85 Branch: refs/heads/1994-merge-rcouch Commit: b5066d85e3983df148f7fd0b5b12dae4afe6d762 Parents: c246549 Author: Benoit Chesneau Authored: Sun Feb 9 09:47:24 2014 +0100 Committer: Benoit Chesneau Committed: Sun Feb 9 09:47:24 2014 +0100 ---------------------------------------------------------------------- apps/couch_index/src/couch_index_indexer.erl | 38 +++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/b5066d85/apps/couch_index/src/couch_index_indexer.erl ---------------------------------------------------------------------- diff --git a/apps/couch_index/src/couch_index_indexer.erl b/apps/couch_index/src/couch_index_indexer.erl index 4af85cf..727c8dd 100644 --- a/apps/couch_index/src/couch_index_indexer.erl +++ b/apps/couch_index/src/couch_index_indexer.erl @@ -19,6 +19,8 @@ -record(state, {index, dbname, + threshold, + refresh_interval, db_updates=0, tref=nil, notifier=nil, @@ -30,10 +32,25 @@ start_link(Index, DbName) -> init({Index, DbName}) -> process_flag(trap_exit, true), + %% register to config events + Self = self(), + ok = couch_config:register(fun + ("couch_index", "threshold") -> + gen_server:cast(Self, config_threshold); + ("couch_index", "refresh_interval") -> + gen_server:cast(Self, config_refresh) + end), + + %% get defaults + Threshold = get_db_threshold(), + Refresh = get_refresh_interval(), + %% delay background index indexing self() ! start_indexing, {ok, #state{index=Index, dbname=DbName, + threshold=Threshold, + refresh_interval=Refresh, locks=dict:new()}}. handle_call({acquire, Pid}, _From, #state{locks=Locks}=State) -> @@ -66,9 +83,24 @@ handle_call({release, Pid}, _From, #state{locks=Locks}=State) -> handle_call(stop, _From, State) -> {stop, normal, ok, State}. + +handle_cast(config_threshold, State) -> + Threshold = get_db_threshold(), + {noreply, State#state{threshold=Threshold}}; +handle_cast(config_refresh, #state{tref=TRef}=State) -> + R = get_refresh_interval(), + %% stop the old timee + if TRef /= nil -> + erlang:cancel_timer(TRef); + true -> ok + end, + %% start the new timer + NTRef = erlang:start_timer(R, self(), refresh_index), + {noreply, State#state{refresh_interval=R, tref=NTRef}}; + handle_cast(updated, #state{index=Index, dbname=DbName, + threshold=Threshold, db_updates=Updates}=State) -> - Threshold = get_db_threshold(), NUpdates = Updates + 1, %% we only update if the number of updates is greater than the @@ -85,12 +117,12 @@ handle_cast(updated, #state{index=Index, dbname=DbName, handle_cast(_Msg, State) -> {noreply, State}. -handle_info(start_indexing, #state{dbname=DbName}=State) -> +handle_info(start_indexing, #state{dbname=DbName, + refresh_interval=R}=State) -> %% start the db notifier to watch db update events {ok, NotifierPid} = start_db_notifier(DbName), %% start the timer - R = get_refresh_interval(), TRef = erlang:start_timer(R, self(), refresh_index), {noreply, State#state{tref=TRef, notifier=NotifierPid}};