summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Vatamaniuc <vatamane@gmail.com>2021-04-22 13:05:48 -0400
committerNick Vatamaniuc <nickva@users.noreply.github.com>2021-04-22 16:16:16 -0400
commitbf9b451689482ea3f046a4551300af73fbcba616 (patch)
tree599a5d58edbffedd056cfa201d85fdf85351100e
parent2e1fbc2f7d5568cacc299c199fad2d9df5afa830 (diff)
downloadcouchdb-bf9b451689482ea3f046a4551300af73fbcba616.tar.gz
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.
-rw-r--r--src/fabric/src/fabric2_fdb.erl15
-rw-r--r--src/fabric/test/fabric2_changes_fold_tests.erl26
2 files changed, 38 insertions, 3 deletions
diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index bccf1d635..a4c3f89dd 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 fa79f25f0..a8578f96b 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",