-
Notifications
You must be signed in to change notification settings - Fork 12
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
Gives the previous cursor in the scroll block #38
Conversation
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 is far from being perfect, but it is backward compatible and works for the case I need.
Improvements can be done, as give an hash in the block as :
Feed::Item.asc(:name).limit(2).scroll do |record, meta_data|
meta_data[:next_cursor]
meta_data[:previous_cursor]
meta_data[:total]
# ...
end
But it is not backward compatible and needs more work.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
tokens changed because previous: false is encoded.
lib/mongoid/scroll/base_cursor.rb
Outdated
@@ -1,7 +1,7 @@ | |||
module Mongoid | |||
module Scroll | |||
class BaseCursor | |||
attr_accessor :value, :tiebreak_id, :field_type, :field_name, :direction, :include_current | |||
attr_accessor :value, :tiebreak_id, :field_type, :field_name, :direction, :include_current, :previous |
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.
I'm not sure about the naming of the previous flag.
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.
Per my comment in #38 (comment), this could be an optional cursor itself, previous_cursor
and it would be deserialized into a BaseCursor
.
Impressive that you got it working :) Let's start by adding to README? I'll do a thorough review when I get a moment. |
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.
I took a detailed look. I could be missing something but I think this is a different feature of scrolling backwards. I understand how that yields the previous cursor, but I believe this is more complicated than necessary. I am not sure of the value of scrolling backwards by making inverse queries, a caller can simply inverse the order by which to scroll.
So, a super inefficient and naive scroll backwards reaches the end saving the list of cursors, then yielding them in reverse order. In some way we want that for the previous cursor minus the inefficiencies of making all the queries.
A possibly much simpler implementation:
- Start scroll, the starting cursor is
nil
. - Every page we may have the
current
cursor that we unpack and make a view with, and generate acursor
(aka a link to the next page with the last item in our page). - Include the information about the
current
cursor in the generated next page one. - Add a property to the unpacked cursor/page called
previous
that returns (3), now you can make queries with it.
Basically, instead of previous
being a boolean, make it the actual previous cursor "as is".
WDYT?
Thanks for the fast answer. I can explain what I need more in details. ATM, In an API payload, we return the hash of a cursor as :
We can take the hash in Then we take this param as :
My need is to be able to do the same, but with the records before the records listed. Given that, as we don't loop over each record, your proposal does not work. |
|
.... actually now that I think about it more, my suggestion might not work - we'd need to store the entire history of cursors for every cursor, and that's going to be ... a lot 🤦♂️ |
So, a possibly simpler idea - |
ok so, to be consistent, we should also have |
It means we will have an extra query each time, right ? |
I tried very rapidly to implement a method Anyway, as it require an extra call each time to use Let me know what you think. |
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.
Ok, I think you had it right the first time, your implementation is better. Thanks for hanging in here with me.
You're right to ask about naming previous
. It's confusing because it's about the nature of the cursor. How about we introduce a type
and give it a number (0 for next, 1 for previous)? Other possibility is to all this azimuth
or navigation
- I'd use a symbol or a number either way. Who knows what kind of other cursors we'll come up with :)
I will try with
I may need to have a "firstPageToken" so I may add a "first_cursor". That's for another PR. |
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.
Looks great. Some typo-things below.
Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
Typos are fixed. Thanks. |
Merged, 👏 Now that you wrote half of this library, would you like to join in as a (co)maintainer? Maybe make the next release? If yes, drop me a note to dblock[at]dblock[dot]org with your rugygems username. |
Thanks for the merge. I enjoy working with you. I'm not very interested in being a maintainer, unless you don't want to be anymore. |
Too bad :( I don't mind, just trying to reduce bus factor. Appreciate any help you can give like on a few issues I opened. |
In addition to the next cursor, blocks in
Mongo::Scrollable#scroll
andMongoid::Criteria::Scrollable
gives an additional cursor with allow to fetch records before the first one returned.The previous cursor is created based on the first record with a
previous
flag set totrue
. For example, if we haveFeed::Item
with a name from 0 to 3, we have this behavior :