You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2022/10/26 02:27:34 UTC

[GitHub] [couchdb] nickva commented on a diff in pull request #4238: Optimize _bulk_get endpoint

nickva commented on code in PR #4238:
URL: https://github.com/apache/couchdb/pull/4238#discussion_r1005148598


##########
src/chttpd/src/chttpd_db.erl:
##########
@@ -2363,6 +2288,175 @@ set_namespace(NS, #mrargs{} = Args) ->
 
 %% /db/_bulk_get stuff
 
+bulk_get_is_multipart(#httpd{mochi_req = MochiReq}) ->
+    Json = MochiReq:accepts_content_type("application/json"),
+    Mixed = MochiReq:accepts_content_type("multipart/mixed"),
+    Related = MochiReq:accepts_content_type("multipart/related"),
+    not Json andalso (Mixed orelse Related).
+
+bulk_get_docs(Db, #{} = ArgsMap, Options) ->
+    % Sort args by doc ID to hopefully make querying B-trees a bit faster
+    KeyFun = fun({Ref, {DocId, _, _}}) -> {DocId, Ref} end,
+    CmpFun = fun(A, B) -> KeyFun(A) =< KeyFun(B) end,
+    ArgsList = lists:sort(CmpFun, maps:to_list(ArgsMap)),
+    % Split out known errors. Later, before returning, recombine them back into
+    % the final result map.
+    PartFun = fun({_Ref, {_DocId, RevsOrError, _DocOpts}}) ->
+        case RevsOrError of
+            L when is_list(L) -> true;
+            all -> true;
+            {error, _} -> false
+        end
+    end,
+    {ValidArgs, ErrorArgs} = lists:partition(PartFun, ArgsList),
+    UseBatches = config:get_boolean("chttpd", "bulk_get_use_batches", true),
+    Responses =
+        case UseBatches of
+            true -> bulk_get_docs_batched(Db, ValidArgs, Options);
+            false -> bulk_get_docs_individually(Db, ValidArgs, Options)
+        end,
+    MapFun = fun({Ref, {DocId, Response, _}} = RespTuple) ->
+        case Response of
+            [] ->
+                % Remap empty reponses to errors. This is a peculiarity of the
+                % _bulk_get HTTP API. If revision was not specifed, `undefined`
+                % must be returned as the error revision ID.
+                #{Ref := {_, Revs, _}} = ArgsMap,
+                RevStr = bulk_get_rev_error(Revs),
+                Error = {RevStr, <<"not_found">>, <<"missing">>},
+                {Ref, {DocId, {error, Error}, []}};
+            [_ | _] = DocRevisions ->

Review Comment:
   [Yup](https://medium.com/erlang-battleground/ode-to-the-robot-butt-bbd69e69beb2)!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org