You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2020/01/31 20:27:36 UTC

[couchdb] branch multi-transactional-iterators updated: Use multi-transactional iterators

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

vatamane pushed a commit to branch multi-transactional-iterators
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/multi-transactional-iterators by this push:
     new 02fcfa8  Use multi-transactional iterators
02fcfa8 is described below

commit 02fcfa87841a1cd4746fac293c6119f1fd09e8e8
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Fri Jan 31 14:58:41 2020 -0500

    Use multi-transactional iterators
---
 src/fabric/src/fabric2_db.erl  |  2 +-
 src/fabric/src/fabric2_fdb.erl | 53 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/src/fabric/src/fabric2_db.erl b/src/fabric/src/fabric2_db.erl
index 6d015df..dd97cf7 100644
--- a/src/fabric/src/fabric2_db.erl
+++ b/src/fabric/src/fabric2_db.erl
@@ -755,7 +755,7 @@ fold_docs(Db, UserFun, UserAcc) ->
 
 
 fold_docs(Db, UserFun, UserAcc0, Options) ->
-    fabric2_fdb:transactional(Db, fun(TxDb) ->
+    fabric2_fdb:with_iter(Db, fun(TxDb) ->
         try
             #{
                 db_prefix := DbPrefix
diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index 6abe1f6..4e7f386 100644
--- a/src/fabric/src/fabric2_fdb.erl
+++ b/src/fabric/src/fabric2_fdb.erl
@@ -24,6 +24,11 @@
     delete/1,
     exists/1,
 
+    create_iter/1,
+    destroy_iter/1,
+    checkpoint_iter/1,
+    with_iter/2,
+
     get_dir/1,
 
     list_dbs/4,
@@ -307,6 +312,54 @@ exists(#{name := DbName} = Db) when is_binary(DbName) ->
     end.
 
 
+create_iter(#{tx := undefined} = Db) ->
+    try
+        Db1 = refresh(Db),
+
+        Reopen = maps:get(reopen, Db1, false),
+        Db2 = maps:remove(reopen, Db1),
+
+        Fdb = get_db_handle(),
+        Tx = erlfdb:reate_read_only_transaction(Fdb),
+        Db3 = case Reopen of
+            true -> reopen(Db2#{tx => Tx});
+            false -> Db2#{tx => Tx}
+        end,
+
+        % Here we might throw `reopen`
+        Db4 = ensure_current(Db3),
+
+        % This part might update the Db cache
+        ok = run_on_commit_fun(Tx),
+        erase({?PDICT_ON_COMMIT_FUN, Tx}),
+
+        Db4#{tx => erlfdb:create_iter(Fdb, Tx)}
+    catch throw:{?MODULE, reopen} ->
+        create_iter(Db#{reopen => true})
+    end.
+
+
+destroy_iter(#{tx := {erfdb_transaction, _}} = Db) ->
+    #{tx := Tx} = Db,
+    erlfdb:destroy_iter(Tx),
+    Db#{tx := undefined}.
+
+
+checkpoint_iter(#{tx := {erlfdb_transaction, _}} = Db) ->
+    #{tx := Tx} = Db,
+    erlfdb:checkpoint_iter(Tx),
+    Db.
+
+
+with_iter(#{tx := undefined} = Db, Fun) when is_function(Fun, 1) ->
+    IterDb = create_iter(Db),
+    try
+        Fun(IterDb)
+    after
+        destroy_iter(IterDb)
+    end.
+
+
 get_dir(Tx) ->
     Root = erlfdb_directory:root(),
     Dir = fabric2_server:fdb_directory(),