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 2021/04/22 17:48:57 UTC

[couchdb] 01/03: Fix fabric_fdb:next_vs/1 function

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

vatamane pushed a commit to branch add-batching-to-couch-jobs
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 618dd7de353da72412c4116612d5da42270c1b03
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Thu Apr 22 13:05:48 2021 -0400

    Fix fabric_fdb:next_vs/1 function
    
    Also, add a clause for the variant without a txid part.
    
    Previously, `next_vs/1` could overflow the batch or the txid field. The range
    of values for both those is [0..16#FFFF], so the correct check before
    incrementing each field should be `< 16#FFFF` instead of `=< 16#FFFF`. Since
    we're dealing with bytes and in other places in the file we use 16#FFFF for max
    values in the versionstamp fields, switch to hex constants.
    
    The tests were included in the fabric2_changes_fold_tests module as next_vs is
    relevant for the _changes feed since_seq calculation.
---
 src/fabric/src/fabric2_fdb.erl                 | 15 +++++++++++---
 src/fabric/test/fabric2_changes_fold_tests.erl | 27 ++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index bccf1d6..a4c3f89 100644
--- a/src/fabric/src/fabric2_fdb.erl
+++ b/src/fabric/src/fabric2_fdb.erl
@@ -1186,18 +1186,27 @@ seq_to_vs(Seq) when is_binary(Seq) ->
 
 
 next_vs({versionstamp, VS, Batch, TxId}) ->
-    {V, B, T} = case TxId =< 65535 of
+    {V, B, T} = case TxId < 16#FFFF of
         true ->
             {VS, Batch, TxId + 1};
         false ->
-            case Batch =< 65535 of
+            case Batch < 16#FFFF of
                 true ->
                     {VS, Batch + 1, 0};
                 false ->
                     {VS + 1, 0, 0}
             end
     end,
-    {versionstamp, V, B, T}.
+    {versionstamp, V, B, T};
+
+next_vs({versionstamp, VS, Batch}) ->
+    {V, B} = case Batch < 16#FFFF of
+        true ->
+            {VS, Batch + 1};
+        false ->
+            {VS + 1, 0}
+    end,
+    {versionstamp, V, B}.
 
 
 new_versionstamp(Tx) ->
diff --git a/src/fabric/test/fabric2_changes_fold_tests.erl b/src/fabric/test/fabric2_changes_fold_tests.erl
index fa79f25..154d69a 100644
--- a/src/fabric/test/fabric2_changes_fold_tests.erl
+++ b/src/fabric/test/fabric2_changes_fold_tests.erl
@@ -22,6 +22,32 @@
 -define(DOC_COUNT, 25).
 
 
+next_vs_function_with_txid_test() ->
+    Cases = [
+        {{0, 0, 1}, {0, 0, 0}},
+        {{0, 0, 2}, {0, 0, 1}},
+        {{0, 1, 0}, {0, 0, 16#FFFF}},
+        {{0, 2, 0}, {0, 1, 16#FFFF}},
+        {{1, 0, 0}, {0, 16#FFFF, 16#FFFF}},
+        {{2, 0, 0}, {1, 16#FFFF, 16#FFFF}}
+    ],
+    Next = fun({V, B, T}) -> fabric2_fdb:next_vs({versionstamp, V, B, T}) end,
+    [?assertEqual({versionstamp, RV, RB, RT}, Next({V, B, T})) ||
+        {{RV, RB, RT}, {V, B, T}} <- Cases].
+
+
+next_vs_function_without_txid_test() ->
+    Cases = [
+        {{0, 1}, {0, 0}},
+        {{0, 2}, {0, 1}},
+        {{1, 0}, {0, 16#FFFF}},
+        {{2, 0}, {1, 16#FFFF}}
+    ],
+    Next = fun({V, B}) -> fabric2_fdb:next_vs({versionstamp, V, B}) end,
+    [?assertEqual({versionstamp, RV, RB}, Next({V, B})) ||
+        {{RV, RB}, {V, B}} <- Cases].
+
+
 changes_fold_test_() ->
     {
         "Test changes fold operations",
@@ -52,6 +78,7 @@ changes_fold_test_() ->
     }.
 
 
+
 setup_all() ->
     Ctx = test_util:start_couch([fabric]),
     meck:new(erlfdb, [passthrough]),