couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kocol...@apache.org
Subject [1/3] couchdb-mango git commit: Use a record for callback accumulator
Date Wed, 22 Jul 2015 21:05:03 GMT
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 <adam@cloudant.com>
Authored: Mon Jul 20 17:26:12 2015 -0400
Committer: Adam Kocoloski <adam@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}}.


Mime
View raw message