couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject [couchdb] 02/02: Pass a TxDb to fold functions
Date Thu, 02 May 2019 14:42:26 GMT
This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/rfc-001-revision-metadata-model
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 44bc5533ae1928c5e174065c0cf9b14113a5fd63
Author: Paul J. Davis <paul.joseph.davis@gmail.com>
AuthorDate: Thu May 2 09:42:06 2019 -0500

    Pass a TxDb to fold functions
---
 src/fabric/src/fabric2_fdb.erl                     | 17 ++++++++++-------
 src/fabric/test/fabric2_changes_fold_tests.erl     | 18 +++++++++---------
 src/fabric/test/fabric2_doc_fold_tests.erl         | 22 +++++++++++-----------
 src/fabric/test/fabric2_fdb_tx_retry_tests.erl     |  4 ++--
 src/fabric/test/fabric2_trace_doc_create_tests.erl |  3 +++
 5 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index 3666268..acb8b82 100644
--- a/src/fabric/src/fabric2_fdb.erl
+++ b/src/fabric/src/fabric2_fdb.erl
@@ -80,7 +80,10 @@ do_transaction(Fun, LayerPrefix) when is_function(Fun, 1) ->
     Db = get_db_handle(),
     try
         erlfdb:transactional(Db, fun(Tx) ->
-            erlfdb:set_option(Tx, transaction_logging_enable),
+            case get('erlfdb_trace') of
+                true -> erlfdb:set_option(Tx, transaction_logging_enable);
+                _ -> ok
+            end,
             case is_transaction_applied(Tx) of
                 true ->
                     get_previous_transaction_result();
@@ -567,7 +570,7 @@ fold_docs(#{} = Db, UserFun, UserAcc0, Options) ->
     DocCountBin = erlfdb:wait(erlfdb:get(Tx, DocCountKey)),
 
     try
-        UserAcc1 = maybe_stop(UserFun({meta, [
+        UserAcc1 = maybe_stop(UserFun(Db, {meta, [
             {total, ?bin2uint(DocCountBin)},
             {offset, null}
         ]}, UserAcc0)),
@@ -575,14 +578,14 @@ fold_docs(#{} = Db, UserFun, UserAcc0, Options) ->
         UserAcc2 = erlfdb:fold_range(Tx, Start, End, fun({K, V}, UserAccIn) ->
             {?DB_ALL_DOCS, DocId} = erlfdb_tuple:unpack(K, DbPrefix),
             RevId = erlfdb_tuple:unpack(V),
-            maybe_stop(UserFun({row, [
+            maybe_stop(UserFun(Db, {row, [
                 {id, DocId},
                 {key, DocId},
                 {value, couch_doc:rev_to_str(RevId)}
             ]}, UserAccIn))
         end, UserAcc1, [{reverse, Reverse}]),
 
-        {ok, maybe_stop(UserFun(complete, UserAcc2))}
+        {ok, maybe_stop(UserFun(Db, complete, UserAcc2))}
     catch throw:{stop, FinalUserAcc} ->
         {ok, FinalUserAcc}
     end.
@@ -619,7 +622,7 @@ fold_changes(#{} = Db, SinceSeq0, UserFun, UserAcc0, Options) ->
         <<51:8, FirstSeq:12/binary>> = erlfdb_tuple:pack({SinceSeq1}),
         put('$last_changes_seq', fabric2_util:to_hex(FirstSeq)),
 
-        UserAcc1 = maybe_stop(UserFun(start, UserAcc0)),
+        UserAcc1 = maybe_stop(UserFun(Db, start, UserAcc0)),
 
         UserAcc2 = erlfdb:fold_range(Tx, Start, End, fun({K, V}, UserAccIn) ->
             {?DB_CHANGES, UpdateSeq} = erlfdb_tuple:unpack(K, DbPrefix),
@@ -635,14 +638,14 @@ fold_changes(#{} = Db, SinceSeq0, UserFun, UserAcc0, Options) ->
                 [{deleted, true}]
             end,
 
-            maybe_stop(UserFun({change, {[
+            maybe_stop(UserFun(Db, {change, {[
                 {seq, SeqHex},
                 {id, DocId},
                 {changes, [{[{rev, couch_doc:rev_to_str(RevId)}]}]}
             ] ++ DelMember}}, UserAccIn))
         end, UserAcc1, [{reverse, Reverse}]),
 
-        UserFun({stop, get('$last_changes_seq'), null}, UserAcc2)
+        UserFun(Db, {stop, get('$last_changes_seq'), null}, UserAcc2)
     catch throw:{stop, FinalUserAcc} ->
         {ok, FinalUserAcc}
     after
diff --git a/src/fabric/test/fabric2_changes_fold_tests.erl b/src/fabric/test/fabric2_changes_fold_tests.erl
index 20f57a4..a8b3b1f 100644
--- a/src/fabric/test/fabric2_changes_fold_tests.erl
+++ b/src/fabric/test/fabric2_changes_fold_tests.erl
@@ -67,12 +67,12 @@ cleanup({Db, _DocIdRevs, Ctx}) ->
 
 
 fold_changes_basic({Db, DocRows, _}) ->
-    {ok, Rows} = fabric2_db:fold_changes(Db, 0, fun fold_fun/2, []),
+    {ok, Rows} = fabric2_db:fold_changes(Db, 0, fun fold_fun/3, []),
     ?assertEqual(lists:reverse(DocRows), Rows).
 
 
 fold_changes_since_now({Db, _, _}) ->
-    {ok, Rows} = fabric2_db:fold_changes(Db, now, fun fold_fun/2, []),
+    {ok, Rows} = fabric2_db:fold_changes(Db, now, fun fold_fun/3, []),
     ?assertEqual([], Rows).
 
 
@@ -81,20 +81,20 @@ fold_changes_since_seq({_, [], _}) ->
 
 fold_changes_since_seq({Db, [Row | RestRows], _}) ->
     #{seq := Since} = Row,
-    {ok, Rows} = fabric2_db:fold_changes(Db, Since, fun fold_fun/2, []),
+    {ok, Rows} = fabric2_db:fold_changes(Db, Since, fun fold_fun/3, []),
     ?assertEqual(lists:reverse(RestRows), Rows),
     fold_changes_since_seq({Db, RestRows, nil}).
 
 
 fold_changes_basic_rev({Db, _, _}) ->
     Opts = [{dir, rev}],
-    {ok, Rows} = fabric2_db:fold_changes(Db, 0, fun fold_fun/2, [], Opts),
+    {ok, Rows} = fabric2_db:fold_changes(Db, 0, fun fold_fun/3, [], Opts),
     ?assertEqual([], Rows).
 
 
 fold_changes_since_now_rev({Db, DocRows, _}) ->
     Opts = [{dir, rev}],
-    {ok, Rows} = fabric2_db:fold_changes(Db, now, fun fold_fun/2, [], Opts),
+    {ok, Rows} = fabric2_db:fold_changes(Db, now, fun fold_fun/3, [], Opts),
     ?assertEqual(DocRows, Rows).
 
 
@@ -104,15 +104,15 @@ fold_changes_since_seq_rev({_, [], _}) ->
 fold_changes_since_seq_rev({Db, DocRows, _}) ->
     #{seq := Since} = lists:last(DocRows),
     Opts = [{dir, rev}],
-    {ok, Rows} = fabric2_db:fold_changes(Db, Since, fun fold_fun/2, [], Opts),
+    {ok, Rows} = fabric2_db:fold_changes(Db, Since, fun fold_fun/3, [], Opts),
     ?assertEqual(DocRows, Rows),
     RestRows = lists:sublist(DocRows, length(DocRows) - 1),
     fold_changes_since_seq_rev({Db, RestRows, nil}).
 
 
-fold_fun(start, Acc) ->
+fold_fun(_Db, start, Acc) ->
     {ok, Acc};
-fold_fun({change, {Props}}, Acc) ->
+fold_fun(_Db, {change, {Props}}, Acc) ->
     [{[{rev, Rev}]}] = fabric2_util:get_value(changes, Props),
     Row = #{
         id => fabric2_util:get_value(id, Props),
@@ -121,5 +121,5 @@ fold_fun({change, {Props}}, Acc) ->
         rev => Rev
     },
     {ok, [Row | Acc]};
-fold_fun({stop, _LastSeq, null}, Acc) ->
+fold_fun(_Db, {stop, _LastSeq, null}, Acc) ->
     {ok, Acc}.
diff --git a/src/fabric/test/fabric2_doc_fold_tests.erl b/src/fabric/test/fabric2_doc_fold_tests.erl
index caa5f92..1ded7ba 100644
--- a/src/fabric/test/fabric2_doc_fold_tests.erl
+++ b/src/fabric/test/fabric2_doc_fold_tests.erl
@@ -61,14 +61,14 @@ cleanup({Db, _DocIdRevs, Ctx}) ->
 
 
 fold_docs_basic({Db, DocIdRevs, _}) ->
-    {ok, {?DOC_COUNT, Rows}} = fabric2_db:fold_docs(Db, fun fold_fun/2, []),
+    {ok, {?DOC_COUNT, Rows}} = fabric2_db:fold_docs(Db, fun fold_fun/3, []),
     ?assertEqual(DocIdRevs, lists:reverse(Rows)).
 
 
 fold_docs_rev({Db, DocIdRevs, _}) ->
     Opts = [{dir, rev}],
     {ok, {?DOC_COUNT, Rows}} =
-            fabric2_db:fold_docs(Db, fun fold_fun/2, [], Opts),
+            fabric2_db:fold_docs(Db, fun fold_fun/3, [], Opts),
     ?assertEqual(DocIdRevs, Rows).
 
 
@@ -76,7 +76,7 @@ fold_docs_with_start_key({Db, DocIdRevs, _}) ->
     {StartKey, _} = hd(DocIdRevs),
     Opts = [{start_key, StartKey}],
     {ok, {?DOC_COUNT, Rows}}
-            = fabric2_db:fold_docs(Db, fun fold_fun/2, [], Opts),
+            = fabric2_db:fold_docs(Db, fun fold_fun/3, [], Opts),
     ?assertEqual(DocIdRevs, lists:reverse(Rows)),
     if length(DocIdRevs) == 1 -> ok; true ->
         fold_docs_with_start_key({Db, tl(DocIdRevs), nil})
@@ -88,7 +88,7 @@ fold_docs_with_end_key({Db, DocIdRevs, _}) ->
     {EndKey, _} = hd(RevDocIdRevs),
     Opts = [{end_key, EndKey}],
     {ok, {?DOC_COUNT, Rows}} =
-            fabric2_db:fold_docs(Db, fun fold_fun/2, [], Opts),
+            fabric2_db:fold_docs(Db, fun fold_fun/3, [], Opts),
     ?assertEqual(RevDocIdRevs, Rows),
     if length(DocIdRevs) == 1 -> ok; true ->
         fold_docs_with_end_key({Db, lists:reverse(tl(RevDocIdRevs)), nil})
@@ -111,12 +111,12 @@ fold_docs_with_different_keys({Db, DocIdRevs, _}) ->
 check_all_combos(Db, StartKey, EndKey, Rows) ->
     Opts1 = make_opts(fwd, StartKey, EndKey, true),
     {ok, {?DOC_COUNT, Rows1}} =
-            fabric2_db:fold_docs(Db, fun fold_fun/2, [], Opts1),
+            fabric2_db:fold_docs(Db, fun fold_fun/3, [], Opts1),
     ?assertEqual(lists:reverse(Rows), Rows1),
 
     Opts2 = make_opts(fwd, StartKey, EndKey, false),
     {ok, {?DOC_COUNT, Rows2}} =
-            fabric2_db:fold_docs(Db, fun fold_fun/2, [], Opts2),
+            fabric2_db:fold_docs(Db, fun fold_fun/3, [], Opts2),
     Expect2 = if EndKey == undefined -> lists:reverse(Rows); true ->
         lists:reverse(all_but_last(Rows))
     end,
@@ -124,12 +124,12 @@ check_all_combos(Db, StartKey, EndKey, Rows) ->
 
     Opts3 = make_opts(rev, StartKey, EndKey, true),
     {ok, {?DOC_COUNT, Rows3}} =
-            fabric2_db:fold_docs(Db, fun fold_fun/2, [], Opts3),
+            fabric2_db:fold_docs(Db, fun fold_fun/3, [], Opts3),
     ?assertEqual(Rows, Rows3),
 
     Opts4 = make_opts(rev, StartKey, EndKey, false),
     {ok, {?DOC_COUNT, Rows4}} =
-            fabric2_db:fold_docs(Db, fun fold_fun/2, [], Opts4),
+            fabric2_db:fold_docs(Db, fun fold_fun/3, [], Opts4),
     Expect4 = if StartKey == undefined -> Rows; true ->
         tl(Rows)
     end,
@@ -197,13 +197,13 @@ pick_end_key(Rows) ->
     end.
 
 
-fold_fun({meta, Meta}, _Acc) ->
+fold_fun(_Db, {meta, Meta}, _Acc) ->
     Total = fabric2_util:get_value(total, Meta),
     {ok, {Total, []}};
-fold_fun({row, Row}, {Total, Rows}) ->
+fold_fun(_Db, {row, Row}, {Total, Rows}) ->
     RowId = fabric2_util:get_value(id, Row),
     RowId = fabric2_util:get_value(key, Row),
     RowRev = fabric2_util:get_value(value, Row),
     {ok, {Total, [{RowId, RowRev} | Rows]}};
-fold_fun(complete, Acc) ->
+fold_fun(_Db, complete, Acc) ->
     {ok, Acc}.
diff --git a/src/fabric/test/fabric2_fdb_tx_retry_tests.erl b/src/fabric/test/fabric2_fdb_tx_retry_tests.erl
index bfb6d25..c924ce5 100644
--- a/src/fabric/test/fabric2_fdb_tx_retry_tests.erl
+++ b/src/fabric/test/fabric2_fdb_tx_retry_tests.erl
@@ -100,7 +100,7 @@ run_on_first_try() ->
     meck:expect(erlfdb, get_last_error, fun() -> undefined end),
     meck:expect(erlfdb, clear, fun(_, _) -> ok end),
     meck:expect(erlfdb, is_read_only, fun(_) -> false end),
-    meck:expect(fabric2_txids, create, fun(_) -> <<"a txid">> end),
+    meck:expect(fabric2_txids, create, fun(_, _) -> <<"a txid">> end),
     meck:expect(erlfdb, set, fun(_, <<"a txid">>, <<>>) -> ok
end),
     meck:expect(fabric2_txids, remove, fun(<<"a txid">>) -> ok end),
 
@@ -120,7 +120,7 @@ retry_when_commit_conflict() ->
     meck:expect(erlfdb, get_last_error, fun() -> 1020 end),
     meck:expect(erlfdb, clear, fun(_, _) -> ok end),
     meck:expect(erlfdb, is_read_only, fun(_) -> false end),
-    meck:expect(fabric2_txids, create, fun(_) -> <<"a txid">> end),
+    meck:expect(fabric2_txids, create, fun(_, _) -> <<"a txid">> end),
     meck:expect(erlfdb, set, fun(_, <<"a txid">>, <<>>) -> ok
end),
     meck:expect(fabric2_txids, remove, fun(<<"a txid">>) -> ok end),
 
diff --git a/src/fabric/test/fabric2_trace_doc_create_tests.erl b/src/fabric/test/fabric2_trace_doc_create_tests.erl
index 21b9738..279533b 100644
--- a/src/fabric/test/fabric2_trace_doc_create_tests.erl
+++ b/src/fabric/test/fabric2_trace_doc_create_tests.erl
@@ -46,6 +46,7 @@ cleanup({Db, Ctx}) ->
 
 
 create_new_doc({Db, _}) ->
+    put(erlfdb_trace, true),
     Doc = #doc{
         id = fabric2_util:uuid(),
         body = {[{<<"foo">>, <<"bar">>}]}
@@ -54,6 +55,7 @@ create_new_doc({Db, _}) ->
 
 
 create_two_docs({Db, _}) ->
+    put(erlfdb_trace, true),
     Doc1 = #doc{
         id = fabric2_util:uuid(),
         body = {[{<<"bam">>, <<"baz">>}]}
@@ -66,6 +68,7 @@ create_two_docs({Db, _}) ->
 
 
 create_50_docs({Db, _}) ->
+    put(erlfdb_trace, true),
     Docs = lists:map(fun(Val) ->
         #doc{
             id = fabric2_util:uuid(),


Mime
View raw message