-
Notifications
You must be signed in to change notification settings - Fork 1.4k
MONGOID-5391 - Support Criteria#pluck_each method #5332
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
Conversation
This feature works brilliantly. So fast! 🚀 |
This is ready for review. |
@neilshweky please review this PR, think it will be useful for your work on |
database_field_names | ||
else | ||
@fields.map {|f| @klass.cleanse_localized_field_names(f) } | ||
end |
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.
Your implementation of normalized_field_names
differs slightly to the one I had previously.
Can you add a test in which we pluck both a localized field, and it's _transaltions hash?
For example:
Dictionary.pluck(:description, :description_translations)
and verify that this new implementation correctly returns
[["t", {"en" => "t", "de" => "u" }]]
for current locale being :en
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.
@neilshweky this test is already in this PR - criteria_spec.rb line 2619 for both #pluck
and #pluck_each
, legacy and non-legacy cases (I copied all the tests you wrote for #pluck.)
it 'returns both' do
expect(plucked_translations_both.first).to eq([{"de"=>"deutsch-text", "en"=>"english-text"}, "deutsch-text"])
end
Github collapses the diff for criteria_spec.rb
because it is large so you may have missed it.
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.
Okay great!
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.
Can you please answer my comments regarding the noramlzied_field_names and add a test?
@neilshweky I responded. |
@p-mongo this PR is ready for review |
c82fdf6
to
4978c55
Compare
This is an optimization for a narrow use case that does not need to exist in Mongoid. To iterate result set in batches you should use the normal |
@p-mongo (I made this PR to "scratch a real-world itch" re: performance of standard queries) Given the benefit, this refactor has a pretty low "complexity" cost; extracting out |
To not construct Mongoid model instances when issuing queries from Mongoid we have https://jira.mongodb.org/browse/MONGOID-5411. If your object allocations are coming from somewhere else please elaborate. |
@p-mongo I believe MONGOID-5411 and this PR serve similar, but slightly different purposes. Suppose I have a model User with 100 different fields and some embeds (in the below examples "addresses" will be an embedded field.) In MONGOID-5411, I'm assuming it adds a criteria modifier (lets call it
(Please correct me if this is not how you're intending MONGOID-5411 to work) #pluck_each, like #pluck, can actually do even fewer allocations, especially when working with embedded docs where you can use dot-notation path traversal:
Array creation is about twice the speed of Hash creation with lower memory footprint. In summary, I think both #pluck_each and MONGOID-5411 are useful and not mutually exclusive. It's good for users to have a choose here, just like |
|
@p-mongo that sounds reasonable. If MONGOID-5411:
Then there wouldn't be any need for this PR. (I do think the refactor here makes the code cleaner regardless for |
Closing per the above comments. I extracted pluck-related changes to #5399. |
Fine to close. I do think this is still useful to have (syntax is more terse than what is being proposed in MONGOID-5411) but can revisit later. |
Resolves MONGOID-5391
This PR extracts out some code into
Mongoid::Contextual::Mongo::PluckEnumerator
. This class is used for both #pluck and #pluck_each (#pluck is simplypluck_each(fields).to_a
.) There has been no change to the logic of #pluck; all existing #pluck cases pass without modification.