From 4b5e0a20aa087dd26df644c0432627aa3e5826d4 Mon Sep 17 00:00:00 2001 From: "Damien F. Katz" Date: Wed, 6 May 2009 19:11:10 +0000 Subject: First cut at _changes api. Update the by_id and by_seq indexes to contain update seq numbers and pointers to bodies on disk, for use in the _changes api. This is a new file version, but the code can continue to serve the old 0.9 version without problems, though certain features in the _changes api will not be able to work. Upgrade to new file version (from 1 to 2) by compacting the file. Also fixed bugs with how the stats api tracks open databases. git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@772406 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_doc.erl | 50 +++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) (limited to 'src/couchdb/couch_doc.erl') diff --git a/src/couchdb/couch_doc.erl b/src/couchdb/couch_doc.erl index 307f7372..4d2affa4 100644 --- a/src/couchdb/couch_doc.erl +++ b/src/couchdb/couch_doc.erl @@ -226,34 +226,28 @@ to_doc_info(FullDocInfo) -> {DocInfo, _Path} = to_doc_info_path(FullDocInfo), DocInfo. -to_doc_info_path(#full_doc_info{id=Id,update_seq=Seq,rev_tree=Tree}) -> - LeafRevs = couch_key_tree:get_all_leafs(Tree), - SortedLeafRevs = - lists:sort(fun({{IsDeletedA, _}, {StartA, [RevIdA|_]}}, {{IsDeletedB, _}, {StartB, [RevIdB|_]}}) -> - % sort descending by {not deleted, then Depth, then RevisionId} - A = {not IsDeletedA, StartA, RevIdA}, - B = {not IsDeletedB, StartB, RevIdB}, - A > B - end, - LeafRevs), - - [{{IsDeleted, SummaryPointer}, {Start, [RevId|_]}=Path} | Rest] = SortedLeafRevs, - {ConflictRevTuples, DeletedConflictRevTuples} = - lists:splitwith(fun({{IsDeleted1, _Sp}, _}) -> - not IsDeleted1 - end, Rest), - - ConflictRevs = [{Start1, RevId1} || {_, {Start1, [RevId1|_]}} <- ConflictRevTuples], - DeletedConflictRevs = [{Start1, RevId1} || {_, {Start1, [RevId1|_]}} <- DeletedConflictRevTuples], - DocInfo = #doc_info{ - id=Id, - update_seq=Seq, - rev = {Start, RevId}, - summary_pointer = SummaryPointer, - conflict_revs = ConflictRevs, - deleted_conflict_revs = DeletedConflictRevs, - deleted = IsDeleted}, - {DocInfo, Path}. +max_seq([], Max) -> + Max; +max_seq([#rev_info{seq=Seq}|Rest], Max) -> + max_seq(Rest, if Max > Seq -> Max; true -> Seq end). + +to_doc_info_path(#full_doc_info{id=Id,rev_tree=Tree}) -> + RevInfosAndPath = + [{#rev_info{deleted=Del,body_sp=Bp,seq=Seq,rev={Pos,RevId}}, Path} || + {{Del, Bp, Seq},{Pos, [RevId|_]}=Path} <- + couch_key_tree:get_all_leafs(Tree)], + SortedRevInfosAndPath = lists:sort( + fun({#rev_info{deleted=DeletedA,rev=RevA}, _PathA}, + {#rev_info{deleted=DeletedB,rev=RevB}, _PathB}) -> + % sort descending by {not deleted, rev} + {not DeletedA, RevA} > {not DeletedB, RevB} + end, RevInfosAndPath), + [{_RevInfo, WinPath}|_] = SortedRevInfosAndPath, + RevInfos = [RevInfo || {RevInfo, _Path} <- SortedRevInfosAndPath], + {#doc_info{id=Id, high_seq=max_seq(RevInfos, 0), revs=RevInfos}, WinPath}. + + + bin_foldl(Bin, Fun, Acc) when is_binary(Bin) -> case Fun(Bin, Acc) of -- cgit v1.2.3