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/08/17 05:14:26 UTC
[1/2] couch-dbupdates commit: updated refs/heads/windsor-merge to
05de5e1
Repository: couchdb-couch-dbupdates
Updated Branches:
refs/heads/windsor-merge b9acfa82e -> 05de5e1a7 (forced update)
Update couch_dbupdates.app.src
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/commit/05de5e1a
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/tree/05de5e1a
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/diff/05de5e1a
Branch: refs/heads/windsor-merge
Commit: 05de5e1a745da26d5d792da253d89be9108ed26d
Parents: 6a4d75d
Author: Paul J. Davis <pa...@gmail.com>
Authored: Sat Aug 16 22:06:31 2014 -0500
Committer: Paul J. Davis <pa...@gmail.com>
Committed: Sat Aug 16 22:14:17 2014 -0500
----------------------------------------------------------------------
src/couch_dbupdates.app.src | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/05de5e1a/src/couch_dbupdates.app.src
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates.app.src b/src/couch_dbupdates.app.src
index a5597ba..2e8ed6a 100644
--- a/src/couch_dbupdates.app.src
+++ b/src/couch_dbupdates.app.src
@@ -1,11 +1,12 @@
-{application, couch_dbupdates,
- [
- {description, ""},
- {vsn, git},
- {registered, []},
- {applications, [
- kernel,
- stdlib
- ]},
- {env, []}
- ]}.
+{application, couch_dbupdates, [
+ {description, "HTTP interface to CouchDB events."},
+ {vsn, git},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ couch_event,
+ couch
+ ]},
+ {env, []}
+]}.
[2/2] couch-dbupdates commit: updated refs/heads/windsor-merge to
05de5e1
Posted by da...@apache.org.
Rewrite couch_dbupdates to use couch_event
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/commit/6a4d75d8
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/tree/6a4d75d8
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/diff/6a4d75d8
Branch: refs/heads/windsor-merge
Commit: 6a4d75d8f6c7358c54a4fe702055216d4cdd9554
Parents: 690c8c2
Author: Paul J. Davis <pa...@gmail.com>
Authored: Sat Aug 16 22:04:35 2014 -0500
Committer: Paul J. Davis <pa...@gmail.com>
Committed: Sat Aug 16 22:14:17 2014 -0500
----------------------------------------------------------------------
src/couch_dbupdates.erl | 46 ---------------
src/couch_dbupdates_httpd.erl | 118 ++++++++++++++++++++++++++-----------
2 files changed, 84 insertions(+), 80 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/6a4d75d8/src/couch_dbupdates.erl
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates.erl b/src/couch_dbupdates.erl
deleted file mode 100644
index e37362f..0000000
--- a/src/couch_dbupdates.erl
+++ /dev/null
@@ -1,46 +0,0 @@
--module(couch_dbupdates).
-
--export([handle_dbupdates/3]).
-
-
-handle_dbupdates(Fun, Acc, Options) ->
- NotifierPid = db_update_notifier(),
- try
- loop(Fun, Acc, Options)
- after
- couch_db_update_notifier:stop(NotifierPid)
- end.
-
-
-loop(Fun, Acc, Options) ->
- [{timeout, Timeout}, {heartbeat, Heartbeat}] = Options,
- receive
- {db_updated, Event} ->
- case Fun(Event, Acc) of
- {ok, Acc1} ->
- loop(Fun, Acc1, Options);
- stop ->
- Fun(stop, Acc)
-
- end
- after Timeout ->
- case Heartbeat of
- true ->
- case Fun(heartbeat, Acc) of
- {ok, Acc1} ->
- loop(Fun, Acc1, Options);
- stop ->
- Fun(stop, Acc)
-
- end;
- _ ->
- Fun(stop, Acc)
- end
- end.
-
-db_update_notifier() ->
- Self = self(),
- {ok, Notifier} = couch_db_update_notifier:start_link(fun(Event) ->
- Self ! {db_updated, Event}
- end),
- Notifier.
http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/6a4d75d8/src/couch_dbupdates_httpd.erl
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates_httpd.erl b/src/couch_dbupdates_httpd.erl
index ec0c4d6..303d4f9 100644
--- a/src/couch_dbupdates_httpd.erl
+++ b/src/couch_dbupdates_httpd.erl
@@ -4,17 +4,19 @@
-include_lib("couch/include/couch_db.hrl").
--record(state, {resp, feed}).
+-record(st, {
+ resp,
+ feed,
+ heartbeat,
+ timeout
+}).
+
handle_req(#httpd{method='GET'}=Req) ->
ok = couch_httpd:verify_is_server_admin(Req),
Qs = couch_httpd:qs(Req),
Feed = proplists:get_value("feed", Qs, "longpoll"),
-
- Timeout = list_to_integer(
- proplists:get_value("timeout", Qs, "60000")
- ),
-
+ Timeout = list_to_integer(proplists:get_value("timeout", Qs, "60000")),
Heartbeat0 = proplists:get_value("heartbeat", Qs),
Heartbeat = case {Feed, Heartbeat0} of
{"longpoll", _} -> false;
@@ -22,8 +24,6 @@ handle_req(#httpd{method='GET'}=Req) ->
_ -> true
end,
- Options = [{timeout, Timeout}, {heartbeat, Heartbeat}],
-
{ok, Resp} = case Feed of
"eventsource" ->
Headers = [
@@ -35,35 +35,85 @@ handle_req(#httpd{method='GET'}=Req) ->
couch_httpd:start_json_response(Req, 200)
end,
- State = #state{resp=Resp, feed=Feed},
- couch_dbupdates:handle_dbupdates(fun handle_update/2,
- State, Options).
+ St1 = #st{
+ resp = Resp,
+ feed = Feed,
+ heartbeat = Heartbeat,
+ timeout = Timeout
+ },
+ {ok, St2} = run(St1, Timeout),
+ {ok, St2#st.resp};
-handle_req(Req, _Db) ->
+handle_req(Req) ->
couch_httpd:send_method_not_allowed(Req, "GET").
-handle_update(stop, #state{resp=Resp}) ->
- couch_httpd:end_json_response(Resp);
-handle_update(heartbeat, #state{resp=Resp}=State) ->
- {ok, Resp1} = couch_httpd:send_chunk(Resp, "\n"),
- {ok, State#state{resp=Resp1}};
-handle_update(Event, #state{resp=Resp, feed="eventsource"}=State) ->
- EventObj = event_obj(Event),
- {ok, Resp1} = couch_httpd:send_chunk(Resp, ["data: ",
- ?JSON_ENCODE(EventObj),
- "\n\n"]),
- {ok, State#state{resp=Resp1}};
-handle_update(Event, #state{resp=Resp, feed="continuous"}=State) ->
- EventObj = event_obj(Event),
- {ok, Resp1} = couch_httpd:send_chunk(Resp, [?JSON_ENCODE(EventObj) |
- "\n"]),
- {ok, State#state{resp=Resp1}};
-handle_update(Event, #state{resp=Resp, feed="longpoll"}) ->
+
+run(St, Timeout) ->
+ ok = couch_event:register_all(self()),
+ try
+ loop(St, Timeout)
+ after
+ ok = couch_event:unregister(self()),
+ drain_events()
+ end.
+
+
+loop(Timeout, St) ->
+ Event = receive
+ {'$couch_event', DbName, Ev} ->
+ {DbName, Ev}
+ after Timeout ->
+ timeout
+ end,
+ case handle_update(Event, St) of
+ {ok, NewSt} ->
+ loop(Timeout, NewSt);
+ {stop, NewSt} ->
+ {ok, NewSt}
+ end.
+
+
+drain_events() ->
+ receive
+ {'$couch_event', _, _} ->
+ drain_events()
+ after 0 ->
+ ok
+ end.
+
+
+handle_update(timeout, #st{heartbeat=true}=St) ->
+ {ok, Resp1} = couch_httpd:send_chunk(St#st.resp, "\n"),
+ {ok, St#st{resp=Resp1}};
+handle_update(timeout, #st{heartbeat=false}=St) ->
+ {ok, Resp1} = couch_httpd:end_json_response(St#st.resp),
+ {stop, St#st{resp=Resp1}};
+handle_update(Event, #st{feed="eventsource"}=St) ->
+ Chunk = ["data: ", ?JSON_ENCODE(event_obj(Event)), "\n\n"],
+ {ok, Resp1} = couch_httpd:send_chunk(St#st.resp, Chunk),
+ {ok, St#st{resp=Resp1}};
+handle_update(Event, #st{feed="continuous"}=St) ->
+ Chunk = [?JSON_ENCODE(event_obj(Event)), "\n"],
+ {ok, Resp1} = couch_httpd:send_chunk(St#st.resp, Chunk),
+ {ok, St#st{resp=Resp1}};
+handle_update(Event, #st{feed="longpoll"}=St) ->
{Props} = event_obj(Event),
JsonObj = {[{<<"ok">>, true} | Props]},
- couch_httpd:send_chunk(Resp, ?JSON_ENCODE(JsonObj)),
- stop.
+ {ok, Resp1} = couch_httpd:send_chunk(St#st.resp, ?JSON_ENCODE(JsonObj)),
+ {ok, Resp2} = couch_httpd:end_json_response(Resp1),
+ {stop, St#st{resp=Resp2}}.
+
+
+event_obj({DbName, Event}) when is_atom(Event) ->
+ {[
+ {<<"type">>, couch_util:to_binary(Event)},
+ {<<"db_name">>, couch_util:to_binary(DbName)}
+ ]};
+event_obj({DbName, {ddoc_updated, DDocId}}) ->
+ {[
+ {<<"type">>, <<"ddoc_updated">>},
+ {<<"db_name">>, couch_util:to_binary(DbName)},
+ {<<"ddoc_id">>, couch_util:to_binary(DDocId)}
+ ]}.
+
-event_obj({Type, DbName}) ->
- {[{<<"type">>, couch_util:to_binary(Type)},
- {<<"db_name">>, couch_util:to_binary(DbName)}]}.