couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1172309 - in /couchdb/trunk/src: couch_index/src/ couch_mrview/src/ couchdb/
Date Sun, 18 Sep 2011 17:57:17 GMT
Author: davisp
Date: Sun Sep 18 17:57:16 2011
New Revision: 1172309

URL: http://svn.apache.org/viewvc?rev=1172309&view=rev
Log:
Use #doc_info{} optimization for _all_docs.

During the refactor I accidentally undid the optimization from
COUCHDB-1061 which uses a #doc_info{} to get a #doc{} from disk
which avoids a lookup in the id btree.


Modified:
    couchdb/trunk/src/couch_index/src/couch_index_util.erl
    couchdb/trunk/src/couch_mrview/src/couch_mrview.erl
    couchdb/trunk/src/couch_mrview/src/couch_mrview_compactor.erl
    couchdb/trunk/src/couch_mrview/src/couch_mrview_util.erl
    couchdb/trunk/src/couchdb/couch_changes.erl
    couchdb/trunk/src/couchdb/couch_httpd_db.erl
    couchdb/trunk/src/couchdb/couch_util.erl

Modified: couchdb/trunk/src/couch_index/src/couch_index_util.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couch_index/src/couch_index_util.erl?rev=1172309&r1=1172308&r2=1172309&view=diff
==============================================================================
--- couchdb/trunk/src/couch_index/src/couch_index_util.erl (original)
+++ couchdb/trunk/src/couch_index/src/couch_index_util.erl Sun Sep 18 17:57:16 2011
@@ -13,7 +13,7 @@
 -module(couch_index_util).
 
 -export([root_dir/0, index_dir/2, index_file/3]).
--export([load_doc/4, sort_lib/1, hexsig/1]).
+-export([load_doc/3, sort_lib/1, hexsig/1]).
 
 -include("couch_db.hrl").
 
@@ -36,32 +36,34 @@ index_file(Module, DbName, FileName) ->
     filename:join(index_dir(Module, DbName), FileName).
 
 
-load_doc(Db, Id, {Props}, Opts) ->
-    DocId = couch_util:get_value(<<"_id">>, Props, Id),
-    Rev = case couch_util:get_value(<<"_rev">>, Props, undefined) of
-        Rev0 when is_binary(Rev0) -> couch_doc:parse_rev(Rev0);
-        _ -> nil
-    end,
-    load_doc_int(Db, DocId, Rev, Opts);
-load_doc(Db, Id, _Value, Opts) ->
-    load_doc_int(Db, Id, nil, Opts).
+load_doc(Db, #doc_info{}=DI, Opts) ->
+    Deleted = lists:member(deleted, Opts),
+    case (catch couch_db:open_doc(Db, DI, Opts)) of
+        {ok, #doc{deleted=false}=Doc} -> Doc;
+        {ok, #doc{deleted=true}=Doc} when Deleted -> Doc;
+        _Else -> null
+    end;
+load_doc(Db, {DocId, Rev}, Opts) ->
+    case (catch load_doc(Db, DocId, Rev, Opts)) of
+        #doc{deleted=false} = Doc -> Doc;
+        _ -> null
+    end.
 
 
-load_doc_int(Db, DocId, Rev, Options) ->
-    JsonDoc = case Rev of
+load_doc(Db, DocId, Rev, Options) ->
+    case Rev of
         nil -> % open most recent rev
-            case couch_db:open_doc(Db, DocId, Options) of
-                {ok, Doc} -> couch_doc:to_json_obj(Doc, Options);
+            case (catch couch_db:open_doc(Db, DocId, Options)) of
+                {ok, Doc} -> Doc;
                 _Error -> null
             end;
         _ -> % open a specific rev (deletions come back as stubs)
-            case couch_db:open_doc_revs(Db, DocId, [Rev], Options) of
-                {ok, [{ok, Doc}]} -> couch_doc:to_json_obj(Doc, Options);
+            case (catch couch_db:open_doc_revs(Db, DocId, [Rev], Options)) of
+                {ok, [{ok, Doc}]} -> Doc;
                 {ok, [{{not_found, missing}, Rev}]} -> null;
                 {ok, [_Else]} -> null
             end
-    end,
-    {doc, JsonDoc}.
+    end.
 
 
 sort_lib({Lib}) ->

Modified: couchdb/trunk/src/couch_mrview/src/couch_mrview.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couch_mrview/src/couch_mrview.erl?rev=1172309&r1=1172308&r2=1172309&view=diff
==============================================================================
--- couchdb/trunk/src/couch_mrview/src/couch_mrview.erl (original)
+++ couchdb/trunk/src/couch_mrview/src/couch_mrview.erl Sun Sep 18 17:57:16 2011
@@ -29,6 +29,7 @@
     limit,
     skip,
     group_level,
+    doc_info,
     callback,
     user_acc,
     last_go=ok,
@@ -117,7 +118,7 @@ cleanup(Db) ->
     couch_mrview_cleanup:run(Db).
 
 
-all_docs_fold(Db, Args, Callback, UAcc) ->
+all_docs_fold(Db, #mrargs{keys=undefined}=Args, Callback, UAcc) ->
     {ok, Info} = couch_db:get_db_info(Db),
     Total = couch_util:get_value(doc_count, Info),
     UpdateSeq = couch_db:get_update_seq(Db),
@@ -132,38 +133,47 @@ all_docs_fold(Db, Args, Callback, UAcc) 
         update_seq=UpdateSeq,
         args=Args
     },
-    {Reds, FinalAcc} = case Args#mrargs.keys of
-        Keys when is_list(Keys) ->
-            KeyFoldFun = case Args#mrargs.direction of
-                fwd -> fun lists:foldl/3;
-                rev -> fun lists:foldr/3
-            end,
-            FoldFun = fun(Key, Acc0) ->
-                DocInfo = (catch couch_db:get_doc_info(Db, Key)),
-                Doc = case DocInfo of
-                    {ok, #doc_info{id=Id, revs=[RevInfo | _RestRevs]}} ->
-                        Rev = couch_doc:rev_to_str(RevInfo#rev_info.rev),
-                        Props = [{rev, Rev}] ++ case RevInfo#rev_info.deleted of
-                            true -> [{deleted, true}];
-                            false -> []
-                        end,
-                        {{Id, Id}, {Props}};
-                    not_found ->
-                        {{Key, error}, not_found}
+    [Opts] = couch_mrview_util:all_docs_key_opts(Args),
+    {ok, Offset, FinalAcc} = couch_db:enum_docs(Db, fun map_fold/3, Acc, Opts),
+    finish_fold(FinalAcc, [{total, Total}, {offset, Offset}]);
+all_docs_fold(Db, #mrargs{direction=Dir, keys=Keys0}=Args, Callback, UAcc) ->
+    {ok, Info} = couch_db:get_db_info(Db),
+    Total = couch_util:get_value(doc_count, Info),
+    UpdateSeq = couch_db:get_update_seq(Db),
+    Acc = #mracc{
+        db=Db,
+        total_rows=Total,
+        limit=Args#mrargs.limit,
+        skip=Args#mrargs.skip,
+        callback=Callback,
+        user_acc=UAcc,
+        reduce_fun=fun couch_mrview_util:all_docs_reduce_to_count/1,
+        update_seq=UpdateSeq,
+        args=Args
+    },
+    % Backwards compatibility hack. The old _all_docs iterates keys
+    % in reverse if descending=true was passed. Here we'll just
+    % reverse the list instead.
+    Keys = if Dir =:= fwd -> Keys0; true -> lists:reverse(Keys0) end,
+
+    FoldFun = fun(Key, Acc0) ->
+        DocInfo = (catch couch_db:get_doc_info(Db, Key)),
+        {Doc, Acc1} = case DocInfo of
+            {ok, #doc_info{id=Id, revs=[RevInfo | _RestRevs]}=DI} ->
+                Rev = couch_doc:rev_to_str(RevInfo#rev_info.rev),
+                Props = [{rev, Rev}] ++ case RevInfo#rev_info.deleted of
+                    true -> [{deleted, true}];
+                    false -> []
                 end,
-                {_, Acc1} = map_fold(Doc, {[], [{0, 0, 0}]}, Acc0),
-                Acc1
-            end,
-            KeyAcc = KeyFoldFun(FoldFun, Acc, Keys),
-            {{[], [{0, 0, 0}]}, KeyAcc};
-        _ ->
-            [Opts] = couch_mrview_util:all_docs_key_opts(Args),
-            Bt = Db#db.fulldocinfo_by_id_btree,
-            {ok, R, A} = couch_btree:fold(Bt, fun map_fold/3, Acc, Opts),
-            {R, A}
+                {{{Id, Id}, {Props}}, Acc0#mracc{doc_info=DI}};
+            not_found ->
+                {{{Key, error}, not_found}, Acc0}
+        end,
+        {_, Acc2} = map_fold(Doc, {[], [{0, 0, 0}]}, Acc1),
+        Acc2
     end,
-    Offset = couch_mrview_util:all_docs_reduce_to_count(Reds),
-    finish_fold(FinalAcc, [{total, Total}, {offset, Offset}]).
+    FinalAcc = lists:foldl(FoldFun, Acc, Keys),
+    finish_fold(FinalAcc, [{total, Total}]).
 
 
 map_fold(Db, View, Args, Callback, UAcc) ->
@@ -191,9 +201,9 @@ map_fold(Db, View, Args, Callback, UAcc)
 map_fold(#full_doc_info{} = FullDocInfo, OffsetReds, Acc) ->
     % matches for _all_docs and translates #full_doc_info{} -> KV pair
     case couch_doc:to_doc_info(FullDocInfo) of
-        #doc_info{id=Id, revs=[#rev_info{deleted=false, rev=Rev}|_]} ->
+        #doc_info{id=Id, revs=[#rev_info{deleted=false, rev=Rev}|_]} = DI ->
             Value = {[{rev, couch_doc:rev_to_str(Rev)}]},
-            map_fold({{Id, Id}, Value}, OffsetReds, Acc);
+            map_fold({{Id, Id}, Value}, OffsetReds, Acc#mracc{doc_info=DI});
         #doc_info{revs=[#rev_info{deleted=true}|_]} ->
             {ok, Acc}
     end;
@@ -222,14 +232,23 @@ map_fold({{Key, Id}, Val}, _Offset, Acc)
     #mracc{
         db=Db,
         limit=Limit,
+        doc_info=DI,
         callback=Callback,
         user_acc=UAcc0,
         args=Args
     } = Acc,
-    Doc = couch_mrview_util:maybe_load_doc(Db, Id, Val, Args),
+    Doc = case DI of
+        #doc_info{} -> couch_mrview_util:maybe_load_doc(Db, DI, Args);
+        _ -> couch_mrview_util:maybe_load_doc(Db, Id, Val, Args)
+    end,
     Row = [{id, Id}, {key, Key}, {val, Val}] ++ Doc,
     {Go, UAcc1} = Callback({row, Row}, UAcc0),
-    {Go, Acc#mracc{limit=Limit-1, user_acc=UAcc1, last_go=Go}}.
+    {Go, Acc#mracc{
+        limit=Limit-1,
+        doc_info=undefined,
+        user_acc=UAcc1,
+        last_go=Go
+    }}.
 
 
 red_fold(Db, {_Nth, _Lang, View}=RedView, Args, Callback, UAcc) ->

Modified: couchdb/trunk/src/couch_mrview/src/couch_mrview_compactor.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couch_mrview/src/couch_mrview_compactor.erl?rev=1172309&r1=1172308&r2=1172309&view=diff
==============================================================================
--- couchdb/trunk/src/couch_mrview/src/couch_mrview_compactor.erl (original)
+++ couchdb/trunk/src/couch_mrview/src/couch_mrview_compactor.erl Sun Sep 18 17:57:16 2011
@@ -161,8 +161,7 @@ update_task(#acc{changes = Changes, tota
 swap_compacted(OldState, NewState) ->
     #mrst{
         sig=Sig,
-        db_name=DbName,
-        idx_name=IdxName
+        db_name=DbName
     } = NewState,
 
     link(NewState#mrst.fd),

Modified: couchdb/trunk/src/couch_mrview/src/couch_mrview_util.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couch_mrview/src/couch_mrview_util.erl?rev=1172309&r1=1172308&r2=1172309&view=diff
==============================================================================
--- couchdb/trunk/src/couch_mrview/src/couch_mrview_util.erl (original)
+++ couchdb/trunk/src/couch_mrview/src/couch_mrview_util.erl Sun Sep 18 17:57:16 2011
@@ -23,7 +23,7 @@
 -export([temp_view_to_ddoc/1]).
 -export([calculate_data_size/2]).
 -export([validate_args/1]).
--export([maybe_load_doc/4]).
+-export([maybe_load_doc/3, maybe_load_doc/4]).
 
 -define(MOD, couch_mrview_index).
 
@@ -659,12 +659,37 @@ expand_dups([KV | Rest], Acc) ->
     expand_dups(Rest, [KV | Acc]).
 
 
+maybe_load_doc(_Db, _DI, #mrargs{include_docs=false}) ->
+    [];
+maybe_load_doc(Db, #doc_info{}=DI, #mrargs{conflicts=true}) ->
+    doc_row(couch_index_util:load_doc(Db, DI, [conflicts]));
+maybe_load_doc(Db, #doc_info{}=DI, _Args) ->
+    doc_row(couch_index_util:load_doc(Db, DI, [])).
+
+
 maybe_load_doc(_Db, _Id, _Val, #mrargs{include_docs=false}) ->
     [];
 maybe_load_doc(Db, Id, Val, #mrargs{conflicts=true}) ->
-    [couch_index_util:load_doc(Db, Id, Val, [conflicts])];
+    doc_row(couch_index_util:load_doc(Db, docid_rev(Id, Val), [conflicts]));
 maybe_load_doc(Db, Id, Val, _Args) ->
-    [couch_index_util:load_doc(Db, Id, Val, [])].
+    doc_row(couch_index_util:load_doc(Db, docid_rev(Id, Val), [])).
+
+
+doc_row(null) ->
+    [{doc, null}];
+doc_row(Doc) ->
+    [{doc, couch_doc:to_json_obj(Doc, [])}].
+
+
+docid_rev(Id, {Props}) ->
+    DocId = couch_util:get_value(<<"_id">>, Props, Id),
+    Rev = case couch_util:get_value(<<"_rev">>, Props, nil) of
+        nil -> nil;
+        Rev0 -> couch_doc:parse_rev(Rev0)
+    end,
+    {DocId, Rev};
+docid_rev(Id, _) ->
+    {Id, nil}.
 
 
 index_of(Key, List) ->

Modified: couchdb/trunk/src/couchdb/couch_changes.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_changes.erl?rev=1172309&r1=1172308&r2=1172309&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_changes.erl (original)
+++ couchdb/trunk/src/couchdb/couch_changes.erl Sun Sep 18 17:57:16 2011
@@ -365,8 +365,15 @@ changes_row(Results, DocInfo, Acc) ->
     {[{<<"seq">>, Seq}, {<<"id">>, Id}, {<<"changes">>,
Results}] ++
         deleted_item(Del) ++ case IncDoc of
             true ->
-                Options = if Conflicts -> [conflicts]; true -> [] end,
-                couch_util:doc_member(Db, DocInfo, Options);
+                Opts = case Conflicts of
+                    true -> [deleted, conflicts];
+                    false -> [deleted]
+                end,
+                Doc = couch_index_util:load_doc(Db, DocInfo, Opts),
+                case Doc of
+                    null -> [{doc, null}];
+                    _ ->  [{doc, couch_doc:to_json_obj(Doc, [])}]
+                end;
             false ->
                 []
         end}.

Modified: couchdb/trunk/src/couchdb/couch_httpd_db.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_httpd_db.erl?rev=1172309&r1=1172308&r2=1172309&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_httpd_db.erl (original)
+++ couchdb/trunk/src/couchdb/couch_httpd_db.erl Sun Sep 18 17:57:16 2011
@@ -561,7 +561,7 @@ all_docs_view_row_obj(Db, {_KeyDocId, Do
     #doc_info{revs = [#rev_info{deleted = true} | _]} ->
         {all_docs_row(DocInfo) ++ [{doc, null}]};
     _ ->
-        {all_docs_row(DocInfo) ++ couch_util:doc_member(
+        {all_docs_row(DocInfo) ++ couch_index_util:load_doc(
             Db, DocInfo, if Conflicts -> [conflicts]; true -> [] end)}
     end;
 all_docs_view_row_obj(_Db, {_KeyDocId, DocInfo}, _IncludeDocs, _Conflicts) ->

Modified: couchdb/trunk/src/couchdb/couch_util.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_util.erl?rev=1172309&r1=1172308&r2=1172309&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_util.erl (original)
+++ couchdb/trunk/src/couchdb/couch_util.erl Sun Sep 18 17:57:16 2011
@@ -28,7 +28,6 @@
 -export([url_strip_password/1]).
 -export([encode_doc_id/1]).
 -export([with_db/2]).
--export([doc_member/3]).
 
 -include("couch_db.hrl").
 
@@ -446,24 +445,3 @@ with_db(DbName, Fun) ->
         Else ->
             throw(Else)
     end.
-
-
-% Rescued from couch_httpd_view.erl, this should really go
-% somewhere else but I can't decide where.
-doc_member(Db, #doc_info{id=Id, revs=[#rev_info{rev=Rev} | _]} = Info, Opts) ->
-    ?LOG_DEBUG("Include Doc: ~p ~p", [Id, Rev]),
-    case couch_db:open_doc(Db, Info, [deleted | Opts]) of
-    {ok, Doc} ->
-        [{doc, couch_doc:to_json_obj(Doc, [])}];
-    _ ->
-        [{doc, null}]
-    end;
-doc_member(Db, {DocId, Rev}, Opts) ->
-    ?LOG_DEBUG("Include Doc: ~p ~p", [DocId, Rev]),
-    case (catch couch_httpd_db:couch_doc_open(Db, DocId, Rev, Opts)) of
-    #doc{} = Doc ->
-        JsonDoc = couch_doc:to_json_obj(Doc, []),
-        [{doc, JsonDoc}];
-    _Else ->
-        [{doc, null}]
-    end.



Mime
View raw message