-
Notifications
You must be signed in to change notification settings - Fork 284
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
Implement limit method for MongoDB driver #499
Conversation
If you have the time, I'd like to suggest some improvements. In particular, the amount of returned documents per chunk should also be limited to the given limit and the cursor should be discarded when the limit is reached. I'll make some inline comments for this. |
@@ -196,6 +217,10 @@ private class MongoCursorData { | |||
addSpecial("$orderby", order); | |||
} | |||
|
|||
void limit(size_t count) { | |||
m_limit = count; | |||
} |
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.
An additional if (m_nret <= 0 || count < m_nret) m_nret = min(count, 1024);
should limit the amount of results per chunk here.
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 might be wrong but I think it should be:
if (count != m_nret)
m_nret = min(count, 1024);
m_limit = count;
Because according to MongoDB docs:
"A limit() value of 0 (e.g. “.limit(0)”) is equivalent to setting no limit."
So such code:
db["users"].find().sort(Bson(["title": Bson(-1)])).limit(3).limit(0);
Should reset limit and also rising limit should be possible, for example:
db["paste"].find().sort(Bson(["title": Bson(-1)])).limit(3).limit(10);
@s-ludwig 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.
I don't know about the last two use cases, I think it's ok and expected if limit
... limits (i.e. it can only lower the limit and not raise it). And it should still be possible to specify a chunk size that is lower than the limit somehow, which cannot work if limit
always adjusts m_nret
.
But you are definitely right about the zero case (note that the if
statement in the empty
method also needs to be adjusted to cope with m_limit == 0
.
@s-ludwig updated, hope I understood your correctly. |
Looks good, except the zero case, which I would rather handle like this:
This would be more in line with the tought that each limit call successively limits the output further. |
@s-ludwig updated. Hope I got it correctly this time 👻 |
Thanks a lot! Merging now. |
Implement limit method for MongoDB driver
Just discovered that |
Yeah I left limit(0) case as "limit-reset" option. |
My first naive implementation.
Seems to work good but I am still new to vibe.d internals so I am not sure if it's the best approach.