You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2014/02/06 17:54:33 UTC

[14/22] couch-mrview commit: updated refs/heads/import-rcouch to 7258945

add index update events notifications


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/commit/5dbbdb5f
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/tree/5dbbdb5f
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/diff/5dbbdb5f

Branch: refs/heads/import-rcouch
Commit: 5dbbdb5f81e1abe0b18cc94fc78e5c24721d8d62
Parents: c296d04
Author: benoitc <be...@apache.org>
Authored: Thu Jan 30 09:21:06 2014 +0100
Committer: Paul J. Davis <pa...@gmail.com>
Committed: Thu Feb 6 10:51:40 2014 -0600

----------------------------------------------------------------------
 src/couch_index_event.erl     | 65 ++++++++++++++++++++++++++++++++++++++
 src/couch_index_event_sup.erl | 51 ++++++++++++++++++++++++++++++
 src/couch_index_sup.erl       |  7 +++-
 3 files changed, 122 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/5dbbdb5f/src/couch_index_event.erl
----------------------------------------------------------------------
diff --git a/src/couch_index_event.erl b/src/couch_index_event.erl
new file mode 100644
index 0000000..0cd0a6b
--- /dev/null
+++ b/src/couch_index_event.erl
@@ -0,0 +1,65 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_index_event).
+-behaviour(gen_event).
+
+-export([start_link/1]).
+-export([notify/1]).
+-export([stop/1]).
+
+%% gen_event callbacks
+-export([init/1, handle_event/2, handle_call/2, handle_info/2,
+         terminate/2, code_change/3]).
+
+start_link(Consumer) ->
+    HandlerId = {?MODULE, make_ref()},
+    couch_index_event_sup:start_link(couch_index_events, HandlerId,
+                                     Consumer).
+
+notify(Event) ->
+    gen_event:notify(couch_index_events, Event).
+
+stop(Pid) ->
+    couch_index_event_sup:stop(Pid).
+
+
+init(Consumer) ->
+    process_flag(trap_exit, true),
+    {ok, Consumer}.
+
+handle_event(Event, Consumer) ->
+    dispatch_event(Event, Consumer).
+
+handle_call(_Req, Consumer) ->
+    {reply, ok, Consumer}.
+
+handle_info({'EXIT', _, _}, _Consumer) ->
+    remove_handler;
+handle_info(_Info, Consumer)->
+    {ok, Consumer}.
+
+code_change(_OldVsn, Consumer, _Extra) ->
+    {ok, Consumer}.
+
+terminate(_Reason, _consumer) ->
+    ok.
+
+dispatch_event(Event, Fun) when is_function(Fun) ->
+    Fun(Event),
+    {ok, Fun};
+dispatch_event(Event, {Fun, Acc}) when is_function(Fun) ->
+    Acc2 = Fun(Event, Acc),
+    {ok, {Fun, Acc2}};
+dispatch_event(Event, Pid) when is_pid(Pid) ->
+    Pid ! Event,
+    {ok, Pid}.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/5dbbdb5f/src/couch_index_event_sup.erl
----------------------------------------------------------------------
diff --git a/src/couch_index_event_sup.erl b/src/couch_index_event_sup.erl
new file mode 100644
index 0000000..68cba43
--- /dev/null
+++ b/src/couch_index_event_sup.erl
@@ -0,0 +1,51 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_index_event_sup).
+
+-export([start_link/3]).
+-export([stop/1]).
+
+%% internal gen_server callbacks
+-export([init/1, terminate/2, handle_call/3, handle_cast/2,
+         handle_info/2,code_change/3]).
+
+start_link(EventMgr, EventHandler, Args) ->
+    gen_server:start_link(?MODULE, {EventMgr, EventHandler, Args}, []).
+
+stop(Pid) ->
+    gen_server:cast(Pid, stop).
+
+init({EventMgr, EventHandler, Args}) ->
+    case gen_event:add_sup_handler(EventMgr, EventHandler, Args) of
+    ok ->
+        {ok, {EventMgr, EventHandler}};
+    {stop, Error} ->
+        {stop, Error}
+    end.
+
+handle_call(_Whatever, _From, State) ->
+    {ok, State}.
+
+handle_cast(stop, State) ->
+    {stop, normal, State}.
+
+handle_info({gen_event_EXIT, _Handler, Reason}, State) ->
+    {stop, Reason, State}.
+
+code_change(_OldVsn, State, _Extra) ->
+    {ok, State}.
+
+terminate(_Reason, _State) ->
+    ok.
+
+

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/5dbbdb5f/src/couch_index_sup.erl
----------------------------------------------------------------------
diff --git a/src/couch_index_sup.erl b/src/couch_index_sup.erl
index fd97814..8e69016 100644
--- a/src/couch_index_sup.erl
+++ b/src/couch_index_sup.erl
@@ -26,4 +26,9 @@ start_link() ->
 
 init([]) ->
     Server = ?CHILD(couch_index_server),
-    {ok, {{one_for_one, 10, 3600}, [Server]}}.
+
+    EventSup = {couch_index_events,
+                {gen_event, start_link, [{local, couch_index_events}]},
+                permanent, brutal_kill, worker, dynamic},
+
+    {ok, {{one_for_one, 10, 3600}, [Server, EventSup]}}.