forked from mongoid/mongoid-scroll
-
Notifications
You must be signed in to change notification settings - Fork 1
Gives the previous cursor in the scroll block #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GCorbel
wants to merge
15
commits into
FabienChaynes:master
Choose a base branch
from
GCorbel:prev-cursor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
01eef8d
Gives the previous cursor in the scroll block
GCorbel 772010b
apply changes for mongo
GCorbel b582458
encrypt with the previous option
GCorbel 76287e8
take values before the first page
GCorbel 3ca3244
keep the ordering when fetching previous records
GCorbel 089a518
keep the ordering when fetching previous records
GCorbel e50e73d
add entry to the change log
GCorbel 9787664
Fix with Ruby 2.6
GCorbel 3e9475a
Update the README and CHANGELOG
GCorbel 0d7637f
minor changes
GCorbel a1766fb
change to use type
GCorbel 0a76df8
change to use an iterator object
GCorbel 9c20b52
refactor
GCorbel aa019aa
Fix typos
GCorbel 7f13aca
throw an error when type is unsupported
GCorbel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ def initialize(value, options = {}) | |
field_name: parsed['field_name'], | ||
direction: parsed['direction'], | ||
include_current: parsed['include_current'], | ||
tiebreak_id: parsed['tiebreak_id'] && !parsed['tiebreak_id'].empty? ? BSON::ObjectId.from_string(parsed['tiebreak_id']) : nil | ||
tiebreak_id: parsed['tiebreak_id'] && !parsed['tiebreak_id'].empty? ? BSON::ObjectId.from_string(parsed['tiebreak_id']) : nil, | ||
previous: parsed['previous'] | ||
} | ||
else | ||
super nil, options | ||
|
@@ -32,7 +33,8 @@ def to_s | |
field_name: field_name, | ||
direction: direction, | ||
include_current: include_current, | ||
tiebreak_id: tiebreak_id && tiebreak_id.to_s | ||
tiebreak_id: tiebreak_id && tiebreak_id.to_s, | ||
previous: previous | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about the naming of the |
||
}.to_json) | ||
end | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,18 @@ | |
describe Mongoid::Scroll::Base64EncodedCursor do | ||
context 'new' do | ||
context 'an empty cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6bnVsbCwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOm51bGx9' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6bnVsbCwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOm51bGwsInByZXZpb3VzIjpmYWxzZX0=' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tokens changed because |
||
subject do | ||
Mongoid::Scroll::Base64EncodedCursor.new base64_string | ||
end | ||
its(:tiebreak_id) { should be_nil } | ||
its(:value) { should be_nil } | ||
its(:criteria) { should eq({}) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'a string field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6ImEgc3RyaW5nIiwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2M2RmODA5NDQzNDE3YzdkMmIxMDIifQ==' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6ImEgc3RyaW5nIiwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2M2RmODA5NDQzNDE3YzdkMmIxMDIiLCJwcmV2aW91cyI6ZmFsc2V9' } | ||
let(:a_value) { 'a string' } | ||
let(:tiebreak_id) { BSON::ObjectId.from_string('64063df809443417c7d2b102') } | ||
let(:criteria) do | ||
|
@@ -32,10 +33,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'an id field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6IjY0MDY0NTg0MDk0NDM0MjgxZmE3MWFiMiIsImZpZWxkX3R5cGUiOiJCU09OOjpPYmplY3RJZCIsImZpZWxkX25hbWUiOiJpZCIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2NDU4NDA5NDQzNDI4MWZhNzFhYjIifQ==' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6IjY0MDY0NTg0MDk0NDM0MjgxZmE3MWFiMiIsImZpZWxkX3R5cGUiOiJCU09OOjpPYmplY3RJZCIsImZpZWxkX25hbWUiOiJpZCIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2NDU4NDA5NDQzNDI4MWZhNzFhYjIiLCJwcmV2aW91cyI6ZmFsc2V9' } | ||
let(:a_value) { BSON::ObjectId('64064584094434281fa71ab2') } | ||
let(:tiebreak_id) { a_value } | ||
let(:criteria) do | ||
|
@@ -52,10 +54,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'an integer field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTAsImZpZWxkX3R5cGUiOiJJbnRlZ2VyIiwiZmllbGRfbmFtZSI6ImFfaW50ZWdlciIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2M2RmODA5NDQzNDE3YzdkMmIxMDgifQ==' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTAsImZpZWxkX3R5cGUiOiJJbnRlZ2VyIiwiZmllbGRfbmFtZSI6ImFfaW50ZWdlciIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2M2RmODA5NDQzNDE3YzdkMmIxMDgiLCJwcmV2aW91cyI6ZmFsc2V9' } | ||
let(:a_value) { 10 } | ||
let(:tiebreak_id) { BSON::ObjectId('64063df809443417c7d2b108') } | ||
let(:criteria) do | ||
|
@@ -74,10 +77,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'a date/time field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzU5MDEyMy4wLCJmaWVsZF90eXBlIjoiRGF0ZVRpbWUiLCJmaWVsZF9uYW1lIjoiYV9kYXRldGltZSIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2NDNhNzA5NDQzNDIzOWYyZGJmODYifQ==' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzU5MDEyMy4wLCJmaWVsZF90eXBlIjoiRGF0ZVRpbWUiLCJmaWVsZF9uYW1lIjoiYV9kYXRldGltZSIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOiI2NDA2NDNhNzA5NDQzNDIzOWYyZGJmODYiLCJwcmV2aW91cyI6ZmFsc2V9' } | ||
let(:a_value) { DateTime.new(2013, 12, 21, 1, 42, 3, 'UTC') } | ||
let(:tiebreak_id) { BSON::ObjectId('640643a7094434239f2dbf86') } | ||
let(:criteria) do | ||
|
@@ -94,10 +98,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'a date field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzU4NDAwMCwiZmllbGRfdHlwZSI6IkRhdGUiLCJmaWVsZF9uYW1lIjoiYV9kYXRlIiwiZGlyZWN0aW9uIjoxLCJpbmNsdWRlX2N1cnJlbnQiOmZhbHNlLCJ0aWVicmVha19pZCI6IjY0MDY0MmM5MDk0NDM0MjEyYzRkNDQyMCJ9' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzU4NDAwMCwiZmllbGRfdHlwZSI6IkRhdGUiLCJmaWVsZF9uYW1lIjoiYV9kYXRlIiwiZGlyZWN0aW9uIjoxLCJpbmNsdWRlX2N1cnJlbnQiOmZhbHNlLCJ0aWVicmVha19pZCI6IjY0MDY0MmM5MDk0NDM0MjEyYzRkNDQyMCIsInByZXZpb3VzIjpmYWxzZX0=' } | ||
let(:tiebreak_id) { BSON::ObjectId('640642c9094434212c4d4420') } | ||
let(:a_value) { Date.new(2013, 12, 21) } | ||
let(:criteria) do | ||
|
@@ -114,10 +119,11 @@ | |
its(:value) { should eq a_value } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'a time field cursor' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzYwNTcyMy4wLCJmaWVsZF90eXBlIjoiVGltZSIsImZpZWxkX25hbWUiOiJhX3RpbWUiLCJkaXJlY3Rpb24iOjEsImluY2x1ZGVfY3VycmVudCI6ZmFsc2UsInRpZWJyZWFrX2lkIjoiNjQwNjNkNGEwOTQ0MzQxNjZiZDA1M2VkIn0=' } | ||
let(:base64_string) { 'eyJ2YWx1ZSI6MTM4NzYwNTcyMy4wLCJmaWVsZF90eXBlIjoiVGltZSIsImZpZWxkX25hbWUiOiJhX3RpbWUiLCJkaXJlY3Rpb24iOjEsImluY2x1ZGVfY3VycmVudCI6ZmFsc2UsInRpZWJyZWFrX2lkIjoiNjQwNjNkNGEwOTQ0MzQxNjZiZDA1M2VkIiwicHJldmlvdXMiOmZhbHNlfQ==' } | ||
let(:item_id) { BSON::ObjectId('640636f209443407333b46d4') } | ||
let(:a_value) { Time.new(2013, 12, 21, 6, 2, 3, '+00:00').utc } | ||
let(:tiebreak_id) { BSON::ObjectId('64063d4a094434166bd053ed') } | ||
|
@@ -136,6 +142,7 @@ | |
its(:tiebreak_id) { tiebreak_id } | ||
its(:tiebreak_id) { should eq tiebreak_id } | ||
its(:criteria) { should eq(criteria) } | ||
its(:previous) { should be_falsy } | ||
its(:to_s) { should eq(base64_string) } | ||
end | ||
context 'an invalid field cursor' do | ||
|
@@ -229,5 +236,22 @@ | |
end.to raise_error Mongoid::Scroll::Errors::UnsupportedFieldTypeError, /The type of the field 'a_array' is not supported: Array./ | ||
end | ||
end | ||
|
||
it 'encode and decode previous option' do | ||
feed_item = Feed::Item.create! | ||
cursor = Mongoid::Scroll::Base64EncodedCursor.from_record feed_item, field_name: 'id', field_type: BSON::ObjectId, previous: true | ||
expect(Mongoid::Scroll::Base64EncodedCursor.new(cursor.to_s).previous).to be_truthy | ||
end | ||
context 'a cursor with previous set to true' do | ||
let(:field_type) { BSON::ObjectId } | ||
let(:field_name) { 'id' } | ||
let(:feed_item) { Feed::Item.create! } | ||
subject do | ||
Mongoid::Scroll::Base64EncodedCursor.from_record feed_item, field_name: field_name, field_type: field_type, previous: true | ||
end | ||
its(:value) { should eq feed_item._id } | ||
its(:field_type) { should eq field_type.to_s } | ||
its(:previous) { should be_truthy } | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This :
limit
Given that, if we have this list :
We want to have 2 records before the cursor and keep the order. Returned values have to be
3, 4
.To do that, we first select only records be the cursor :
We reverse the selection :
Take only the two first values :
And finally revert the sorting :
To avoid using aggregation, another solution was possible by sorting first with mongo and the reverse the records to display in the correct order
Another possible solution was to do the same in pure Ruby :
Even if this version is simpler, I chose to use aggregates for performance reason.