Skip to content

Commit

Permalink
Add a cursor to go to the first page (#42)
Browse files Browse the repository at this point in the history
* Add a cursor to go to the first page

* small changes

* add a line in CHANGELOG

* add specs for mongo

* Update lib/mongoid/criteria/scrollable/iterator.rb

Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>

* minor changes

---------

Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
  • Loading branch information
GCorbel and dblock authored Sep 4, 2024
1 parent 56f04f5 commit a725ea5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
* [#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)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/mongoid/criteria/scrollable/iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def initialize(previous_cursor:, next_cursor:)
@previous_cursor = previous_cursor
@next_cursor = next_cursor
end

def first_cursor
@first_cursor ||= next_cursor.class.new(nil, next_cursor.sort_options)
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/mongo/collection_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/mongoid/criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@
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
first_cursor = nil

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(first_cursor).to_a).to eq(Feed::Item.asc(field_name).limit(2).to_a)
end
end
end
end
Expand Down

0 comments on commit a725ea5

Please sign in to comment.