Skip to content

Commit

Permalink
Merge branch 'master' into current-page-cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
GCorbel authored Sep 4, 2024
2 parents 6262836 + a725ea5 commit 2c3fc20
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
### 2.0.0 (Next)

* [#38](https://github.com/mongoid/mongoid-scroll/pull/38): Allow to reverse the scroll - [@GCorbel](https://github.com/GCorbel).
* [#43](https://github.com/mongoid/mongoid-scroll/pull/43): Add a current cursor - [@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).
* [#43](https://github.com/mongoid/mongoid-scroll/pull/43): Add `current_cursor` - [@GCorbel](https://github.com/GCorbel).
* Your contribution here.

### 1.0.1 (2023/03/15)
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Feed::Item.desc(:position).limit(5).scroll(saved_iterator.previous_cursor) do |r
end
```

Use `saved_iterator.current_cursor` to loop over the same records again.
Use `saved_iterator.first_cursor` to loop over the first records or `saved_iterator.current_cursor` to loop over the same records again.

The iteration finishes when no more records are available. You can also finish iterating over the remaining records by omitting the query limit.

Expand Down Expand Up @@ -161,7 +161,14 @@ end

## Cursors

You can use `Mongoid::Scroll::Cursor.from_record` to generate a cursor. A cursor points at the last record of the previous iteration and unlike MongoDB cursors will not expire.
You can use `Mongoid::Scroll::Cursor.from_record` to generate a cursor. A cursor points at the last record of the






iteration and unlike MongoDB cursors will not expire.

```ruby
record = Feed::Item.desc(:position).limit(3).last
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 @@ -9,6 +9,10 @@ def initialize(previous_cursor:, current_cursor:, next_cursor:)
@current_cursor = current_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 @@ -138,6 +138,17 @@

expect(records.limit(2).scroll(current_cursor, field_type: field_type).to_a).to eq(records.skip(4).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 @@ -191,6 +191,15 @@

expect(Feed::Item.asc(field_name).limit(2).scroll(current_cursor).to_a).to eq(Feed::Item.asc(field_name).skip(4).limit(2).to_a)
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 2c3fc20

Please sign in to comment.