You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ko...@apache.org on 2015/07/22 23:05:03 UTC

[1/3] couchdb-mango git commit: Use a record for callback accumulator

Repository: couchdb-mango
Updated Branches:
  refs/heads/master 7fa976394 -> 4abd4748f


Use a record for callback accumulator

This allows us to extend the state stored in the accumulator more
easily.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/1231788a
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/1231788a
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/1231788a

Branch: refs/heads/master
Commit: 1231788aab2c09bfa0b5750200256175f6eadb92
Parents: a0cac18
Author: Adam Kocoloski <ad...@cloudant.com>
Authored: Mon Jul 20 17:26:12 2015 -0400
Committer: Adam Kocoloski <ad...@cloudant.com>
Committed: Mon Jul 20 17:26:12 2015 -0400

----------------------------------------------------------------------
 src/mango_httpd.erl | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/1231788a/src/mango_httpd.erl
----------------------------------------------------------------------
diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl
index d3fde9c..54510fb 100644
--- a/src/mango_httpd.erl
+++ b/src/mango_httpd.erl
@@ -22,6 +22,11 @@
 -include("mango.hrl").
 -include("mango_idx.hrl").
 
+-record(vacc, {
+    resp,
+    prepend,
+    kvs
+}).
 
 handle_req(#httpd{} = Req, Db0) ->
     try
@@ -163,8 +168,8 @@ handle_find_req(#httpd{method='POST'}=Req, Db) ->
     {ok, Opts0} = mango_opts:validate_find(chttpd:json_body_obj(Req)),
     {value, {selector, Sel}, Opts} = lists:keytake(selector, 1, Opts0),
     {ok, Resp0} = start_find_resp(Req),
-    {ok, {Resp1, _, KVs}} = run_find(Resp0, Db, Sel, Opts),
-    end_find_resp(Resp1, KVs);
+    {ok, AccOut} = run_find(Resp0, Db, Sel, Opts),
+    end_find_resp(AccOut);
 
 handle_find_req(Req, _Db) ->
     chttpd:send_method_not_allowed(Req, "POST").
@@ -213,7 +218,8 @@ start_find_resp(Req) ->
     chttpd:start_delayed_json_response(Req, 200, [], "{\"docs\":[").
 
 
-end_find_resp(Resp0, KVs) ->
+end_find_resp(Acc) ->
+    #vacc{resp=Resp0, kvs=KVs} = Acc,
     FinalAcc = lists:foldl(fun({K, V}, Acc) ->
         JK = ?JSON_ENCODE(K),
         JV = ?JSON_ENCODE(V),
@@ -225,13 +231,20 @@ end_find_resp(Resp0, KVs) ->
 
 
 run_find(Resp, Db, Sel, Opts) ->
-    mango_crud:find(Db, Sel, fun handle_doc/2, {Resp, "\r\n", []}, Opts).
+    Acc0 = #vacc{
+        resp = Resp,
+        prepend = "\r\n",
+        kvs = []
+    },
+    mango_crud:find(Db, Sel, fun handle_doc/2, Acc0, Opts).
 
 
-handle_doc({add_key, Key, Value}, {Resp, Prepend, KVs}) ->
+handle_doc({add_key, Key, Value}, Acc0) ->
+    #vacc{resp=Resp, prepend=Prepend, kvs=KVs} = Acc0,
     NewKVs = lists:keystore(Key, 1, KVs, {Key, Value}),
     {ok, {Resp, Prepend, NewKVs}};
-handle_doc({row, Doc}, {Resp0, Prepend, KVs}) ->
+handle_doc({row, Doc}, Acc0) ->
+    #vacc{resp=Resp0, prepend=Prepend, kvs=KVs} = Acc0,
     Chunk = [Prepend, ?JSON_ENCODE(Doc)],
     {ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk),
     {ok, {Resp1, ",\r\n", KVs}}.


[3/3] couchdb-mango git commit: Merge branch '2724-chunked-buffering'

Posted by ko...@apache.org.
Merge branch '2724-chunked-buffering'


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/4abd4748
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/4abd4748
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/4abd4748

Branch: refs/heads/master
Commit: 4abd4748f5897f7416de2e5e297790bff96f7429
Parents: 7fa9763 47ee021
Author: Adam Kocoloski <ad...@cloudant.com>
Authored: Wed Jul 22 17:03:50 2015 -0400
Committer: Adam Kocoloski <ad...@cloudant.com>
Committed: Wed Jul 22 17:03:50 2015 -0400

----------------------------------------------------------------------
 src/mango_httpd.erl | 51 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 41 insertions(+), 10 deletions(-)
----------------------------------------------------------------------



[2/3] couchdb-mango git commit: Buffer rows to reduce number of chunks

Posted by ko...@apache.org.
Buffer rows to reduce number of chunks

This patch reduces the number of chunks in an HTTP chunked response body
by coalescing multiple rows into a single transmission. The default
value is chosen to fill a standard Ethernet frame and can be configured
by setting

     [httpd]
     chunked_response_buffer = 1490

Note that the same setting is several other streaming responses (e.g.
_changes, _all_docs, _views).

COUCHDB-2724


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/47ee0216
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/47ee0216
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/47ee0216

Branch: refs/heads/master
Commit: 47ee02167aa8f2cb302b741659508d81e5de5e4f
Parents: 1231788
Author: Adam Kocoloski <ad...@cloudant.com>
Authored: Mon Jul 20 17:35:00 2015 -0400
Committer: Adam Kocoloski <ad...@cloudant.com>
Committed: Wed Jul 22 17:03:27 2015 -0400

----------------------------------------------------------------------
 src/mango_httpd.erl | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/47ee0216/src/mango_httpd.erl
----------------------------------------------------------------------
diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl
index 54510fb..566ff09 100644
--- a/src/mango_httpd.erl
+++ b/src/mango_httpd.erl
@@ -25,7 +25,10 @@
 -record(vacc, {
     resp,
     prepend,
-    kvs
+    kvs,
+    buffer = [],
+    bufsize = 0,
+    threshold = 1490
 }).
 
 handle_req(#httpd{} = Req, Db0) ->
@@ -218,13 +221,14 @@ start_find_resp(Req) ->
     chttpd:start_delayed_json_response(Req, 200, [], "{\"docs\":[").
 
 
-end_find_resp(Acc) ->
-    #vacc{resp=Resp0, kvs=KVs} = Acc,
+end_find_resp(Acc0) ->
+    #vacc{resp=Resp00, buffer=Buf, kvs=KVs, threshold=Max} = Acc0,
+    {ok, Resp0} = chttpd:close_delayed_json_object(Resp00, Buf, "\r\n]}", Max),
     FinalAcc = lists:foldl(fun({K, V}, Acc) ->
         JK = ?JSON_ENCODE(K),
         JV = ?JSON_ENCODE(V),
         [JV, ": ", JK, ",\r\n" | Acc]
-    end, ["\r\n]"], KVs),
+    end, [], KVs),
     Chunk = lists:reverse(FinalAcc, ["}\r\n"]),
     {ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk),
     chttpd:end_delayed_json_response(Resp1).
@@ -234,20 +238,34 @@ run_find(Resp, Db, Sel, Opts) ->
     Acc0 = #vacc{
         resp = Resp,
         prepend = "\r\n",
-        kvs = []
+        kvs = [],
+        threshold = chttpd:chunked_response_buffer_size()
     },
     mango_crud:find(Db, Sel, fun handle_doc/2, Acc0, Opts).
 
 
 handle_doc({add_key, Key, Value}, Acc0) ->
-    #vacc{resp=Resp, prepend=Prepend, kvs=KVs} = Acc0,
+    #vacc{kvs=KVs} = Acc0,
     NewKVs = lists:keystore(Key, 1, KVs, {Key, Value}),
-    {ok, {Resp, Prepend, NewKVs}};
+    {ok, Acc0#vacc{kvs = NewKVs}};
 handle_doc({row, Doc}, Acc0) ->
-    #vacc{resp=Resp0, prepend=Prepend, kvs=KVs} = Acc0,
+    #vacc{prepend=Prepend} = Acc0,
     Chunk = [Prepend, ?JSON_ENCODE(Doc)],
-    {ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk),
-    {ok, {Resp1, ",\r\n", KVs}}.
+    maybe_flush_response(Acc0, Chunk, iolist_size(Chunk)).
+
+maybe_flush_response(#vacc{bufsize=Size, threshold=Max} = Acc, Data, Len)
+        when Size > 0 andalso (Size + Len) > Max ->
+    #vacc{buffer = Buffer, resp = Resp} = Acc,
+    {ok, R1} = chttpd:send_delayed_chunk(Resp, Buffer),
+    {ok, Acc#vacc{prepend = ",\r\n", buffer = Data, bufsize = Len, resp = R1}};
+maybe_flush_response(Acc0, Data, Len) ->
+    #vacc{buffer = Buf, bufsize = Size} = Acc0,
+    Acc = Acc0#vacc{
+        prepend = ",\r\n",
+        buffer = [Buf | Data],
+        bufsize = Size + Len
+    },
+    {ok, Acc}.
 
 
 parse_index_param("limit", Value) ->