You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by be...@apache.org on 2014/02/09 09:49:13 UTC

couchdb commit: updated refs/heads/1994-merge-rcouch to b5066d8

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 <be...@apache.org>
Authored: Sun Feb 9 09:47:24 2014 +0100
Committer: Benoit Chesneau <be...@apache.org>
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}};