From ccb9c48b8e333a824c2523e06a695efb90016865 Mon Sep 17 00:00:00 2001 From: GCorbel Date: Tue, 3 Sep 2024 15:50:33 -0400 Subject: [PATCH 1/6] Add a cursor to go to the first page --- lib/mongoid/criteria/scrollable/iterator.rb | 4 ++++ spec/mongoid/criteria_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/mongoid/criteria/scrollable/iterator.rb b/lib/mongoid/criteria/scrollable/iterator.rb index fb11b91..995b0ae 100644 --- a/lib/mongoid/criteria/scrollable/iterator.rb +++ b/lib/mongoid/criteria/scrollable/iterator.rb @@ -8,6 +8,10 @@ def initialize(previous_cursor:, next_cursor:) @previous_cursor = previous_cursor @next_cursor = next_cursor end + + def first_page_cursor + next_cursor.class.new(nil, next_cursor.sort_options) + end end end end diff --git a/spec/mongoid/criteria_spec.rb b/spec/mongoid/criteria_spec.rb index 26e5cd4..32578e6 100644 --- a/spec/mongoid/criteria_spec.rb +++ b/spec/mongoid/criteria_spec.rb @@ -181,6 +181,16 @@ expect(Feed::Item.asc(field_name).limit(2).scroll(second_iterator.previous_cursor)).to eq(records.limit(2)) expect(Feed::Item.asc(field_name).limit(2).scroll(third_iterator.previous_cursor)).to eq(records.skip(2).limit(2)) end + it 'can loop over the first records with the first page cursor' do + records = [] + iterator = nil + + Feed::Item.asc(field_name).limit(2).scroll(cursor_type) do |_, iterator| + iterator = iterator + end + + expect(Feed::Item.asc(field_name).limit(2).scroll(iterator.first_page_cursor).to_a).to eq(Feed::Item.asc(field_name).limit(2).to_a) + end end end end From 84d71109aace2e7d5c04d05803025f5ef8a4d261 Mon Sep 17 00:00:00 2001 From: GCorbel Date: Tue, 3 Sep 2024 16:03:00 -0400 Subject: [PATCH 2/6] small changes --- lib/mongoid/criteria/scrollable/iterator.rb | 2 +- spec/mongoid/criteria_spec.rb | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/mongoid/criteria/scrollable/iterator.rb b/lib/mongoid/criteria/scrollable/iterator.rb index 995b0ae..be08eff 100644 --- a/lib/mongoid/criteria/scrollable/iterator.rb +++ b/lib/mongoid/criteria/scrollable/iterator.rb @@ -9,7 +9,7 @@ def initialize(previous_cursor:, next_cursor:) @next_cursor = next_cursor end - def first_page_cursor + def first_cursor next_cursor.class.new(nil, next_cursor.sort_options) end end diff --git a/spec/mongoid/criteria_spec.rb b/spec/mongoid/criteria_spec.rb index 32578e6..53f5cef 100644 --- a/spec/mongoid/criteria_spec.rb +++ b/spec/mongoid/criteria_spec.rb @@ -182,14 +182,13 @@ expect(Feed::Item.asc(field_name).limit(2).scroll(third_iterator.previous_cursor)).to eq(records.skip(2).limit(2)) end it 'can loop over the first records with the first page cursor' do - records = [] - iterator = nil + first_cursor = nil - Feed::Item.asc(field_name).limit(2).scroll(cursor_type) do |_, iterator| - iterator = iterator + Feed::Item.asc(field_name).limit(2).scroll(cursor_type) do |_, it| + first_cursor = it.first_cursor end - expect(Feed::Item.asc(field_name).limit(2).scroll(iterator.first_page_cursor).to_a).to eq(Feed::Item.asc(field_name).limit(2).to_a) + expect(Feed::Item.asc(field_name).limit(2).scroll(first_cursor).to_a).to eq(Feed::Item.asc(field_name).limit(2).to_a) end end end From 7a32a782def4d3edf19a063009db1197def64004 Mon Sep 17 00:00:00 2001 From: GCorbel Date: Tue, 3 Sep 2024 16:37:19 -0400 Subject: [PATCH 3/6] add a line in CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94006e5..d48f6d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 2.0.0 (Next) * [#38](https://github.com/mongoid/mongoid-scroll/pull/38): Allow to reverse the scroll - [@GCorbel](https://github.com/GCorbel). +* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add a cursor to go to the first page - [@GCorbel](https://github.com/GCorbel). * Your contribution here. ### 1.0.1 (2023/03/15) From 243357d46d5aacc036f9fde6fb47d848f8f48643 Mon Sep 17 00:00:00 2001 From: GCorbel Date: Tue, 3 Sep 2024 17:07:03 -0400 Subject: [PATCH 4/6] add specs for mongo --- spec/mongo/collection_view_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/mongo/collection_view_spec.rb b/spec/mongo/collection_view_spec.rb index 2f329e3..323140e 100644 --- a/spec/mongo/collection_view_spec.rb +++ b/spec/mongo/collection_view_spec.rb @@ -127,6 +127,17 @@ expect(Mongoid.default_client['feed_items'].find.sort(field_name => 1).limit(2).scroll(second_iterator.previous_cursor, field_type: field_type).to_a).to eq(records.limit(2).to_a) expect(Mongoid.default_client['feed_items'].find.sort(field_name => 1).limit(2).scroll(third_iterator.previous_cursor, field_type: field_type).to_a).to eq(records.skip(2).limit(2).to_a) end + it 'can loop over the first records with the first cursor' do + first_cursor = nil + records = Mongoid.default_client['feed_items'].find.sort(field_name => 1) + cursor = cursor_type.from_record records.skip(4).first, field_name: field_name, field_type: field_type, include_current: true + + records.limit(2).scroll(cursor, field_type: field_type) do |_, iterator| + first_cursor = iterator.first_cursor + end + + expect(records.limit(2).scroll(first_cursor, field_type: field_type).to_a).to eq(records.limit(2).to_a) + end end end end From bdcd3b21a3d567c3504d250d1ed03a73cb8f9b59 Mon Sep 17 00:00:00 2001 From: Guirec Corbel Date: Wed, 4 Sep 2024 10:08:10 -0400 Subject: [PATCH 5/6] Update lib/mongoid/criteria/scrollable/iterator.rb Co-authored-by: Daniel (dB.) Doubrovkine --- lib/mongoid/criteria/scrollable/iterator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoid/criteria/scrollable/iterator.rb b/lib/mongoid/criteria/scrollable/iterator.rb index be08eff..8cad507 100644 --- a/lib/mongoid/criteria/scrollable/iterator.rb +++ b/lib/mongoid/criteria/scrollable/iterator.rb @@ -10,7 +10,7 @@ def initialize(previous_cursor:, next_cursor:) end def first_cursor - next_cursor.class.new(nil, next_cursor.sort_options) + @first_cursor ||= next_cursor.class.new(nil, next_cursor.sort_options) end end end From 239301e2111cd9de3910927afdb23df1be72cde9 Mon Sep 17 00:00:00 2001 From: GCorbel Date: Wed, 4 Sep 2024 10:12:30 -0400 Subject: [PATCH 6/6] minor changes --- CHANGELOG.md | 4 ++-- README.md | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d48f6d5..a79e0c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ### 2.0.0 (Next) -* [#38](https://github.com/mongoid/mongoid-scroll/pull/38): Allow to reverse the scroll - [@GCorbel](https://github.com/GCorbel). -* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add a cursor to go to the first page - [@GCorbel](https://github.com/GCorbel). +* [#38](https://github.com/mongoid/mongoid-scroll/pull/38): Add `previous_cursor` - [@GCorbel](https://github.com/GCorbel). +* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add `first_cursor` - [@GCorbel](https://github.com/GCorbel). * Your contribution here. ### 1.0.1 (2023/03/15) diff --git a/README.md b/README.md index 6657577..c55fa61 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ Feed::Item.desc(:position).limit(5).scroll(saved_iterator.previous_cursor) do |r end ``` +Use `saved_iterator.first_cursor` to loop over the first records. + The iteration finishes when no more records are available. You can also finish iterating over the remaining records by omitting the query limit. ```ruby