couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject [11/12] couch-index commit: updated refs/heads/1994-merge-rcouch to 7c1666b
Date Thu, 13 Feb 2014 00:01:24 GMT
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-couch-index/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/commit/7c1666ba
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/tree/7c1666ba
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/diff/7c1666ba

Branch: refs/heads/1994-merge-rcouch
Commit: 7c1666ba73641efd0a8eab76399f1794082085e2
Parents: 814ed50
Author: benoitc <benoitc@apache.org>
Authored: Sun Feb 9 09:47:24 2014 +0100
Committer: Paul J. Davis <paul.joseph.davis@gmail.com>
Committed: Wed Feb 12 17:58:23 2014 -0600

----------------------------------------------------------------------
 src/couch_index_indexer.erl | 38 +++++++++++++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/blob/7c1666ba/src/couch_index_indexer.erl
----------------------------------------------------------------------
diff --git a/src/couch_index_indexer.erl b/src/couch_index_indexer.erl
index 4af85cf..727c8dd 100644
--- a/src/couch_index_indexer.erl
+++ b/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}};


Mime
View raw message