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/12 07:16:33 UTC

[1/2] couch-dbupdates commit: updated refs/heads/import-master to 12ed398

Updated Branches:
  refs/heads/import-master [created] 12ed398bd


Import couch_dbupdates from rcouch.

This creates a new top level API endpoint: `/_db_updates`
that returns a line of JSON for each database event along
with the database name.

A database event is one of `created`, `updated`, `deleted`.

The API endpoint supports a `?feed=` parameter with the
options: `longpoll`, `continuous` and `eventsource`.

A second parameter `timeout=` specifies when the server should
close the connection.

`longpoll` closes the connection after a single notification.
It is the default option.

`continuous` keeps a socket open until the specified `timeout`
or 60 seconds by default.

`heartbeat` decides whether to send a newline character on
`timeout` to avoid clients closing the connection prematurely.

`eventsource` works like continuous, but sends the data in
EventSource format. See http://dev.w3.org/html5/eventsource/

The parameters are modelled after the existing `/_changes` API
endpoint. Note that `/_db_updates` does not support resuming
of notifications via a sequence ID.

This is a port of the existing DbUpdateNotification interface
to the HTTP API.

Functional changes compared to rcouch:

 - make _db_updates an admin-only resource

Docs:

 - updated api/misc to include basic info on `/_db_updates`

License:

  Apache 2 license, updated LICENSE.

Notice:

  (c) 2012 Benoit Chesneau, updated NOTICE.

Tests:

 - only manual testing of the various API differences due to
   complications with asynchronous HTTP requests in the JS
   test suite and total annoyance of overly complicated
   ibrowse/httpc modules for writing etap tests.

Recommendation to ship this as EXPERIMENTAL until we have tests.


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/dea80e1a
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/tree/dea80e1a
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/diff/dea80e1a

Branch: refs/heads/import-master
Commit: dea80e1a97788bd665bf4c81522e0f9f096c1496
Parents: 
Author: Jan Lehnardt <ja...@apache.org>
Authored: Mon Mar 4 12:02:24 2013 +0100
Committer: Jan Lehnardt <ja...@apache.org>
Committed: Mon Jul 22 12:28:51 2013 +0200

----------------------------------------------------------------------
 LICENSE                       | 22 ++++++++++++
 Makefile.am                   | 33 ++++++++++++++++++
 NOTICE                        |  6 ++++
 README.md                     | 32 ++++++++++++++++++
 src/couch_dbupdates.app.src   | 11 ++++++
 src/couch_dbupdates.erl       | 46 +++++++++++++++++++++++++
 src/couch_dbupdates_httpd.erl | 69 ++++++++++++++++++++++++++++++++++++++
 7 files changed, 219 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/dea80e1a/LICENSE
----------------------------------------------------------------------
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a089916
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+2009-2012 (c) Benoît Chesneau <be...@e-engura.org>
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/dea80e1a/Makefile.am
----------------------------------------------------------------------
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..d131b9b
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,33 @@
+## 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.
+
+couch_dbupdateslibdir = $(localerlanglibdir)/couch_dbupdates-0.1
+couch_dbupdatesebindir = $(couch_dbupdateslibdir)/ebin
+
+couch_dbupdatesebin_DATA = $(compiled_files)
+
+EXTRA_DIST = $(source_files)
+CLEANFILES = $(compiled_files)
+
+source_files = \
+    src/couch_dbupdates.erl \
+    src/couch_dbupdates.app.src \
+    src/couch_dbupdates_httpd.erl
+
+compiled_files = \
+    ebin/couch_dbupdates.beam \
+    ebin/couch_dbupdates_httpd.beam
+
+ebin/%.beam: src/%.erl
+	@mkdir -p ebin/
+	$(ERLC) -I$(top_srcdir)/src/couchdb -o ebin/ $(ERLC_FLAGS) ${TEST} $<;
+

http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/dea80e1a/NOTICE
----------------------------------------------------------------------
diff --git a/NOTICE b/NOTICE
new file mode 100644
index 0000000..ee00a85
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,6 @@
+couch_dbupdates
+---------------
+
+2012 (c) Benoît Chesneau <be...@refuge.io>
+
+couch_dbupdates is released under the Apache License 2.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/dea80e1a/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c683102
--- /dev/null
+++ b/README.md
@@ -0,0 +1,32 @@
+# couch_dbupdates
+
+`couch_dbupdates` is a simple couchdb modules to receive databases
+events in couchdb node.
+
+It's actually **supported by all the [refuge](http://refuge.io) projects**:
+
+- [refuge](https://github.com/refuge/refuge)
+- [rcouch](https://github.com/refuge/rcouch)
+- [rcouch_template](https://github.com/refuge/rcouch_template)
+
+
+## HTTP API
+
+To get db events, do a GET to `/_db_updates` .
+
+You can pass an optional query parameters:
+
+* `feed` The feed can be `longpoll` (default) for longpolling, `eventsource`
+  for event stream or `continuous` for continuous feed.
+* `timeout`: timeout before the longpolling connection close or when the
+  heartbeat is emitted.
+* `heartbeat`: true, or false. an empty line is emittend when the
+  timeout occurs to maintain the connection active.
+
+
+## Example of usage
+
+    $ curl http://127.0.0.1:5984/_db_updates?feed=continuous
+    {"type":"created","db_name":"testdb"}
+    {"type":"updated","db_name":"testdb"}
+    {"type":"deleted","db_name":"testdb"}

http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/dea80e1a/src/couch_dbupdates.app.src
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates.app.src b/src/couch_dbupdates.app.src
new file mode 100644
index 0000000..c420283
--- /dev/null
+++ b/src/couch_dbupdates.app.src
@@ -0,0 +1,11 @@
+{application, couch_dbupdates,
+ [
+  {description, ""},
+  {vsn, "@version@"},
+  {registered, []},
+  {applications, [
+                  kernel,
+                  stdlib
+                 ]},
+  {env, []}
+ ]}.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/dea80e1a/src/couch_dbupdates.erl
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates.erl b/src/couch_dbupdates.erl
new file mode 100644
index 0000000..e37362f
--- /dev/null
+++ b/src/couch_dbupdates.erl
@@ -0,0 +1,46 @@
+-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/dea80e1a/src/couch_dbupdates_httpd.erl
----------------------------------------------------------------------
diff --git a/src/couch_dbupdates_httpd.erl b/src/couch_dbupdates_httpd.erl
new file mode 100644
index 0000000..96058d3
--- /dev/null
+++ b/src/couch_dbupdates_httpd.erl
@@ -0,0 +1,69 @@
+-module(couch_dbupdates_httpd).
+
+-export([handle_req/1]).
+
+-include_lib("couch_db.hrl").
+
+-record(state, {resp, feed}).
+
+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")
+    ),
+
+    Heartbeat0 = proplists:get_value("heartbeat", Qs),
+    Heartbeat = case {Feed, Heartbeat0} of
+        {"longpoll", _} -> false;
+        {_, "false"} -> false;
+        _ -> true
+    end,
+
+    Options = [{timeout, Timeout}, {heartbeat, Heartbeat}],
+
+    {ok, Resp} = case Feed of
+        "eventsource" ->
+            Headers = [
+                {"Content-Type", "text/event-stream"},
+                {"Cache-Control", "no-cache"}
+            ],
+            couch_httpd:start_json_response(Req, 200, Headers);
+        _ ->
+            couch_httpd:start_json_response(Req, 200)
+    end,
+
+    State = #state{resp=Resp, feed=Feed},
+    couch_dbupdates:handle_dbupdates(fun handle_update/2,
+                                     State, Options).
+
+handle_req(Req, _Db) ->
+    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"}) ->
+    {Props} = event_obj(Event),
+    JsonObj = {[{<<"ok">>, true} | Props]},
+    couch_httpd:send_chunk(Resp, ?JSON_ENCODE(JsonObj)),
+    stop.
+
+event_obj({Type, DbName}) ->
+    {[{<<"type">>, couch_util:to_binary(Type)},
+      {<<"db_name">>, couch_util:to_binary(DbName)}]}.


[2/2] couch-dbupdates commit: updated refs/heads/import-master to 12ed398

Posted by da...@apache.org.
Remove couchdb_updates files (content already in top-level LICENSE/NOTICE).


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/12ed398b
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/tree/12ed398b
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/diff/12ed398b

Branch: refs/heads/import-master
Commit: 12ed398bdf3fc84ed75c568f473bf431cf632110
Parents: dea80e1
Author: Dirkjan Ochtman <dj...@apache.org>
Authored: Wed Aug 7 18:00:37 2013 +0200
Committer: Dirkjan Ochtman <dj...@apache.org>
Committed: Fri Aug 23 16:32:19 2013 +0200

----------------------------------------------------------------------
 LICENSE   | 22 ----------------------
 NOTICE    |  6 ------
 README.md | 32 --------------------------------
 3 files changed, 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/12ed398b/LICENSE
----------------------------------------------------------------------
diff --git a/LICENSE b/LICENSE
deleted file mode 100644
index a089916..0000000
--- a/LICENSE
+++ /dev/null
@@ -1,22 +0,0 @@
-2009-2012 (c) Benoît Chesneau <be...@e-engura.org>
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/12ed398b/NOTICE
----------------------------------------------------------------------
diff --git a/NOTICE b/NOTICE
deleted file mode 100644
index ee00a85..0000000
--- a/NOTICE
+++ /dev/null
@@ -1,6 +0,0 @@
-couch_dbupdates
----------------
-
-2012 (c) Benoît Chesneau <be...@refuge.io>
-
-couch_dbupdates is released under the Apache License 2.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-dbupdates/blob/12ed398b/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
deleted file mode 100644
index c683102..0000000
--- a/README.md
+++ /dev/null
@@ -1,32 +0,0 @@
-# couch_dbupdates
-
-`couch_dbupdates` is a simple couchdb modules to receive databases
-events in couchdb node.
-
-It's actually **supported by all the [refuge](http://refuge.io) projects**:
-
-- [refuge](https://github.com/refuge/refuge)
-- [rcouch](https://github.com/refuge/rcouch)
-- [rcouch_template](https://github.com/refuge/rcouch_template)
-
-
-## HTTP API
-
-To get db events, do a GET to `/_db_updates` .
-
-You can pass an optional query parameters:
-
-* `feed` The feed can be `longpoll` (default) for longpolling, `eventsource`
-  for event stream or `continuous` for continuous feed.
-* `timeout`: timeout before the longpolling connection close or when the
-  heartbeat is emitted.
-* `heartbeat`: true, or false. an empty line is emittend when the
-  timeout occurs to maintain the connection active.
-
-
-## Example of usage
-
-    $ curl http://127.0.0.1:5984/_db_updates?feed=continuous
-    {"type":"created","db_name":"testdb"}
-    {"type":"updated","db_name":"testdb"}
-    {"type":"deleted","db_name":"testdb"}