Skip to content

Conversation

@mikebaldry
Copy link
Contributor

@mikebaldry mikebaldry commented Apr 14, 2022

Related to https://jira.mongodb.org/browse/MONGOID-5285

MongoDB treats a limit of 0 as having no limit at all but the query caching does not take this in to consideration, returning 0 records, nor using existing cached items with a 0 limit when they could be used.

MongoDB treats a negative limit the same as a positive limit, except it signifies that if there aren't enough documents in the batch, the client will not call getMore. The query cache was using ruby negative array indexes giving back weird things

@mikebaldry mikebaldry changed the title Treat 0 limit as no limit in query caching Treat 0 limit as no limit and negative limit as positive limit in query caching Apr 14, 2022
Copy link
Contributor

@p-mongo p-mongo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very thorough, thank you for your work @mikebaldry

end
end

def normalized_limit(limit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be more at home under Cursor somewhere, thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though a limit doesn't require creation of a cursor.

I would prefer collection to not reference QueryCache for implementation parts.

Copy link
Contributor Author

@mikebaldry mikebaldry Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did consider CachingCursor exposing a method to get a limited set of values and handling the normalization internally there, some how. Didn't get that far though, happy to attempt it

limit_for_cached_query = respond_to?(:limit) ? QueryCache.normalized_limit(limit) : nil
end

if block_given?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't comment on line 89 because it's not in the diff but I'll ask on this one..

If we call each without a block, it'll just return an Enumerable, which won't have the cached limit applied.

This case probably should be handled by always doing the positive case, and then calling to_enum on the result of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 79 could end up being something like

result = if use_query_cache?
  @cursor.limited_to(limit) # is there a case where limit doesn't exist? CachingCursor inherits Cursor which implements limit, so don't see why we have the `respond_to?(:limit)` check
else
  @cursor
end

if block_given? 
  # iterate result and yield 
else
  result.to_enum
end

Just ideas at them moment, let me know what you think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @p-mongo (not sure if you'd normally get notifications for these comments on someone elses PR)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do @mikebaldry , but thank you for the highlight. We'll discuss this PR with the team to figure out the best course of action.

@comandeo
Copy link
Contributor

The issue in now under the driver project - https://jira.mongodb.org/browse/RUBY-2961

1 similar comment
@comandeo
Copy link
Contributor

The issue in now under the driver project - https://jira.mongodb.org/browse/RUBY-2961

@comandeo comandeo changed the title Treat 0 limit as no limit and negative limit as positive limit in query caching RUBY-2961 Treat 0 limit as no limit and negative limit as positive limit in query caching Apr 19, 2022
Copy link
Contributor

@p-mongo p-mongo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to merge as is per today's discussion.

@comandeo comandeo merged commit 43f23a3 into mongodb:master Apr 25, 2022
comandeo pushed a commit that referenced this pull request Apr 25, 2022
comandeo pushed a commit that referenced this pull request Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants