You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ga...@apache.org on 2020/04/23 17:24:54 UTC

[couchdb] 01/02: Refactor fetching rev code in fabric2_fdb

This is an automated email from the ASF dual-hosted git repository.

garren pushed a commit to branch prototype/fdb-layer
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit f522b880b68109feb5960d6d8244ac034990059e
Author: Garren Smith <ga...@gmail.com>
AuthorDate: Wed Apr 22 15:17:20 2020 +0200

    Refactor fetching rev code in fabric2_fdb
    
    Use a common `get_revs_future` and `get_revs_wait` for fetching
    winning revs and all revs.
---
 src/couch_views/src/couch_views_indexer.erl |  2 +-
 src/fabric/src/fabric2_db.erl               |  2 +-
 src/fabric/src/fabric2_fdb.erl              | 36 ++++++++++++++---------------
 3 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/src/couch_views/src/couch_views_indexer.erl b/src/couch_views/src/couch_views_indexer.erl
index ab5aaad..bd1bd4d 100644
--- a/src/couch_views/src/couch_views_indexer.erl
+++ b/src/couch_views/src/couch_views_indexer.erl
@@ -384,7 +384,7 @@ fetch_docs(Db, Changes) ->
     RevFutures = maps:keys(RevState),
     BodyState = lists:foldl(fun(RevFuture, Acc) ->
         {Id, Change} = maps:get(RevFuture, RevState),
-        Revs = fabric2_fdb:get_winning_revs_wait(Db, RevFuture),
+        Revs = fabric2_fdb:get_revs_wait(Db, RevFuture),
 
         % I'm assuming that in this changes transaction that the winning
         % doc body exists since it is listed in the changes feed as not deleted
diff --git a/src/fabric/src/fabric2_db.erl b/src/fabric/src/fabric2_db.erl
index 6e629f7..15694cd 100644
--- a/src/fabric/src/fabric2_db.erl
+++ b/src/fabric/src/fabric2_db.erl
@@ -1534,7 +1534,7 @@ update_doc_interactive(Db, Doc0, Options) ->
 
 
 update_doc_interactive(Db, Doc0, Future, _Options) ->
-    RevInfos = fabric2_fdb:get_winning_revs_wait(Db, Future),
+    RevInfos = fabric2_fdb:get_revs_wait(Db, Future),
     {Winner, SecondPlace} = case RevInfos of
         [] -> {not_found, not_found};
         [WRI] -> {WRI, not_found};
diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index 03f3bad..b1ada52 100644
--- a/src/fabric/src/fabric2_fdb.erl
+++ b/src/fabric/src/fabric2_fdb.erl
@@ -42,9 +42,10 @@
     incr_stat/4,
 
     get_all_revs/2,
+    get_all_revs_future/2,
     get_winning_revs/3,
     get_winning_revs_future/3,
-    get_winning_revs_wait/2,
+    get_revs_wait/2,
     get_non_deleted_rev/3,
 
     get_doc_body/3,
@@ -602,46 +603,45 @@ incr_stat(#{} = Db, Section, Key, Increment) when is_integer(Increment) ->
 get_all_revs(#{} = Db, DocId) ->
     DbName = maps:get(name, Db, undefined),
     with_span('db.get_all_revs', #{'db.name' => DbName, 'doc.id' => DocId}, fun() ->
-        #{
-            tx := Tx,
-            db_prefix := DbPrefix
-        } = ensure_current(Db),
-
-        Prefix = erlfdb_tuple:pack({?DB_REVS, DocId}, DbPrefix),
-        Options = [{streaming_mode, want_all}],
-        Future = erlfdb:get_range_startswith(Tx, Prefix, Options),
-        lists:map(fun({K, V}) ->
-            Key = erlfdb_tuple:unpack(K, DbPrefix),
-            Val = erlfdb_tuple:unpack(V),
-            fdb_to_revinfo(Key, Val)
-        end, erlfdb:wait(Future))
+        Future = get_all_revs_future(Db, DocId),
+        get_revs_wait(Db, Future)
     end).
 
 
+get_all_revs_future(#{} = Db, DocId) ->
+    Options = [{streaming_mode, want_all}],
+    get_revs_future(Db, DocId, Options).
+
+
 get_winning_revs(Db, DocId, NumRevs) ->
     DbName = maps:get(name, Db, undefined),
     with_span('db.get_winning_revs', #{'db.name' => DbName, 'doc.id' => DocId}, fun() ->
         Future = get_winning_revs_future(Db, DocId, NumRevs),
-        get_winning_revs_wait(Db, Future)
+        get_revs_wait(Db, Future)
     end).
 
 
 get_winning_revs_future(#{} = Db, DocId, NumRevs) ->
+    Options = [{reverse, true}, {limit, NumRevs}],
+    get_revs_future(Db, DocId, Options).
+
+
+get_revs_future(#{} = Db, DocId, Options) ->
     #{
         tx := Tx,
         db_prefix := DbPrefix
     } = ensure_current(Db),
 
     {StartKey, EndKey} = erlfdb_tuple:range({?DB_REVS, DocId}, DbPrefix),
-    Options = [{reverse, true}, {limit, NumRevs}],
     erlfdb:fold_range_future(Tx, StartKey, EndKey, Options).
 
 
-get_winning_revs_wait(#{} = Db, RangeFuture) ->
+get_revs_wait(#{} = Db, RangeFuture) ->
     #{
         tx := Tx,
         db_prefix := DbPrefix
     } = ensure_current(Db),
+
     RevRows = erlfdb:fold_range_wait(Tx, RangeFuture, fun({K, V}, Acc) ->
         Key = erlfdb_tuple:unpack(K, DbPrefix),
         Val = erlfdb_tuple:unpack(V),
@@ -1185,7 +1185,7 @@ load_validate_doc_funs(#{} = Db) ->
             id := DDocId,
             rev_info := RevInfoFuture
         } = Info,
-        [RevInfo] = get_winning_revs_wait(Db, RevInfoFuture),
+        [RevInfo] = get_revs_wait(Db, RevInfoFuture),
         #{deleted := Deleted} = RevInfo,
         if Deleted -> []; true ->
             [Info#{