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 2023/08/03 15:54:16 UTC

[couchdb] branch main updated: Fix pending count for the reverse changes feed

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

vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/main by this push:
     new 64faef7a5 Fix pending count for the reverse changes feed
64faef7a5 is described below

commit 64faef7a54bdfce9e745f1d6e3796383be0117f5
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Wed Aug 2 23:37:53 2023 -0400

    Fix pending count for the reverse changes feed
    
    Previously, pending changes count for the `descending=true` didn't indicate the
    number of pending changes. Instead, it returned the negative value of the
    already emitted rows. So for `descending=true&limit=2`, it would return -2; for
    `descending=true&limit=5`, it would return -5. That wasn't a very useful value,
    so let's fix it to return the actual number of pending changes.
    
    When traversing the changes feed backwards, the number of pending changes until
    the start will be equal the total number of changes minus the number of changes
    from the current start sequence until the end.
    
    Issue: https://github.com/apache/couchdb/issues/4714
---
 src/chttpd/test/eunit/chttpd_changes_test.erl | 12 ++++++------
 src/fabric/src/fabric_rpc.erl                 | 11 ++++++++++-
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/src/chttpd/test/eunit/chttpd_changes_test.erl b/src/chttpd/test/eunit/chttpd_changes_test.erl
index f780118c6..d0590a83a 100644
--- a/src/chttpd/test/eunit/chttpd_changes_test.erl
+++ b/src/chttpd/test/eunit/chttpd_changes_test.erl
@@ -282,7 +282,7 @@ t_reverse({_, DbUrl}) ->
     Params = "?descending=true",
     {Seq, Pending, Rows} = changes(DbUrl, Params),
     ?assertEqual(5, Seq),
-    ?assertEqual(-3, Pending),
+    ?assertEqual(0, Pending),
     ?assertEqual(
         [
             {7, {?DDOC2, <<"2-c">>}, ?LEAFREV},
@@ -296,7 +296,7 @@ t_continuous_reverse({_, DbUrl}) ->
     Params = "?feed=continuous&timeout=10&descending=true",
     {Seq, Pending, Rows} = changes(DbUrl, Params),
     ?assertEqual(5, Seq),
-    ?assertEqual(-3, Pending),
+    ?assertEqual(0, Pending),
     ?assertEqual(
         [
             {7, {?DDOC2, <<"2-c">>}, ?LEAFREV},
@@ -310,7 +310,7 @@ t_reverse_q8({_, DbUrl}) ->
     Params = "?descending=true",
     {Seq, Pending, Rows} = changes(DbUrl, Params),
     ?assertEqual(7, Seq),
-    ?assertEqual(-3, Pending),
+    ?assertEqual(0, Pending),
     {Seqs, Revs, _Deleted} = lists:unzip3(Rows),
     ?assertEqual(
         [
@@ -324,12 +324,12 @@ t_reverse_q8({_, DbUrl}) ->
 
 t_reverse_limit_zero({_, DbUrl}) ->
     Params = "?descending=true&limit=0",
-    ?assertEqual({7, 0, []}, changes(DbUrl, Params)).
+    ?assertEqual({7, 3, []}, changes(DbUrl, Params)).
 
 t_reverse_limit_one({_, DbUrl}) ->
     Params = "?descending=true&limit=1",
     ?assertEqual(
-        {7, -1, [
+        {7, 2, [
             {7, {?DDOC2, <<"2-c">>}, ?LEAFREV}
         ]},
         changes(DbUrl, Params)
@@ -338,7 +338,7 @@ t_reverse_limit_one({_, DbUrl}) ->
 t_reverse_limit_one_q8({_, DbUrl}) ->
     Params = "?descending=true&limit=1",
     ?assertMatch(
-        {7, -1, [
+        {7, 2, [
             {_, {<<_/binary>>, <<_/binary>>}, _}
         ]},
         changes(DbUrl, Params)
diff --git a/src/fabric/src/fabric_rpc.erl b/src/fabric/src/fabric_rpc.erl
index 5c1ddd573..fa6ea5116 100644
--- a/src/fabric/src/fabric_rpc.erl
+++ b/src/fabric/src/fabric_rpc.erl
@@ -92,12 +92,21 @@ changes(DbName, Options, StartVector, DbOptions) ->
             StartSeq = calculate_start_seq(Db, node(), StartVector),
             Enum = fun changes_enumerator/2,
             Opts = [{dir, Dir}],
+            Pending0 =
+                case Dir of
+                    fwd ->
+                        couch_db:count_changes_since(Db, StartSeq);
+                    rev ->
+                        ChangesTotal = couch_db:count_changes_since(Db, 0),
+                        ChangesToEnd = couch_db:count_changes_since(Db, StartSeq),
+                        ChangesTotal - ChangesToEnd
+                end,
             Acc0 = #fabric_changes_acc{
                 db = Db,
                 seq = StartSeq,
                 args = Args,
                 options = Options,
-                pending = couch_db:count_changes_since(Db, StartSeq),
+                pending = Pending0,
                 epochs = couch_db:get_epochs(Db)
             },
             try