couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robertkowal...@apache.org
Subject [34/50] [abbrv] couchdb-mango git commit: Allow cursors to add KVs to the find response
Date Tue, 03 Feb 2015 15:13:41 GMT
Allow cursors to add KVs to the find response

This change allows cursors to set top level key/value pairs in the
response to _find requests. This is ground work for Search based indexes
that will need to set a bookmark value for clients.

BugzId: 33294


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

Branch: refs/heads/master
Commit: 9ed13044727875534b2abd763a0ec67410b32034
Parents: d1d13e6
Author: Paul J. Davis <paul.joseph.davis@gmail.com>
Authored: Thu Jan 8 17:48:41 2015 -0600
Committer: Paul J. Davis <paul.joseph.davis@gmail.com>
Committed: Fri Jan 16 13:32:49 2015 -0600

----------------------------------------------------------------------
 src/mango_httpd.erl | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/9ed13044/src/mango_httpd.erl
----------------------------------------------------------------------
diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl
index ef4cc5b..02e26ad 100644
--- a/src/mango_httpd.erl
+++ b/src/mango_httpd.erl
@@ -123,8 +123,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, _}} = run_find(Resp0, Db, Sel, Opts),
-    end_find_resp(Resp1);
+    {ok, {Resp1, _, KVs}} = run_find(Resp0, Db, Sel, Opts),
+    end_find_resp(Resp1, KVs);
 
 handle_find_req(Req, _Db) ->
     chttpd:send_method_not_allowed(Req, "POST").
@@ -157,16 +157,25 @@ start_find_resp(Req) ->
     chttpd:start_delayed_json_response(Req, 200, [], "{\"docs\":[").
 
 
-end_find_resp(Resp0) ->
-    {ok, Resp1} = chttpd:send_delayed_chunk(Resp0, "\r\n]}"),
+end_find_resp(Resp0, KVs) ->
+    FinalAcc = lists:foldl(fun({K, V}, Acc) ->
+        JK = ?JSON_ENCODE(K),
+        JV = ?JSON_ENCODE(V),
+        [JV, ": ", JK, ",\r\n" | Acc]
+    end, ["\r\n]"], KVs),
+    Chunk = lists:reverse(FinalAcc, ["}\r\n"]),
+    {ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk),
     chttpd:end_delayed_json_response(Resp1).
 
 
 run_find(Resp, Db, Sel, Opts) ->
-    mango_crud:find(Db, Sel, fun handle_doc/2, {Resp, "\r\n"}, Opts).
+    mango_crud:find(Db, Sel, fun handle_doc/2, {Resp, "\r\n", []}, Opts).
 
 
-handle_doc({row, Doc}, {Resp0, Prepend}) ->
+handle_doc({add_key, Key, Value}, {Resp, Prepend, KVs}) ->
+    NewKVs = lists:keystore(Key, 1, KVs, {Key, Value}),
+    {ok, {Resp, Prepend, NewKVs}};
+handle_doc({row, Doc}, {Resp0, Prepend, KVs}) ->
     Chunk = [Prepend, ?JSON_ENCODE(Doc)],
     {ok, Resp1} = chttpd:send_delayed_chunk(Resp0, Chunk),
-    {ok, {Resp1, ",\r\n"}}.
+    {ok, {Resp1, ",\r\n", KVs}}.


Mime
View raw message