-
Notifications
You must be signed in to change notification settings - Fork 1
perf: improve array/hash operation #247
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
DayTF
wants to merge
3
commits into
main
Choose a base branch
from
perf/various-loop-improvements
base: main
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -240,31 +240,37 @@ def re_project_in_place(caller, records, projection) | |
|
|
||
| def re_project_relation_in_place(caller, records, name, projection) | ||
| field_schema = schema[:fields][name] | ||
|
|
||
| return if field_schema.type == 'PolymorphicManyToOne' | ||
|
|
||
| association = datasource.get_collection(field_schema.foreign_collection) | ||
|
|
||
| if !@relations[name] | ||
| association = datasource.get_collection(field_schema.foreign_collection) | ||
| association.re_project_in_place(caller, records.filter_map { |r| r[name] }, projection) | ||
| elsif field_schema.type == 'ManyToOne' | ||
| ids = records.filter_map { |record| record[field_schema.foreign_key] }.uniq | ||
| sub_filter = Filter.new(condition_tree: ConditionTreeLeaf.new(field_schema.foreign_key_target, 'In', ids)) | ||
| sub_records = association.list(caller, sub_filter, projection.union([field_schema.foreign_key_target])) | ||
|
|
||
| records.each do |record| | ||
| record[name] = sub_records.find { |sr| sr[field_schema.foreign_key_target] == record[field_schema.foreign_key] } | ||
| end | ||
| assign_many_to_one_records(caller, records, name, field_schema, projection) | ||
| elsif ['OneToOne', 'OneToMany'].include?(field_schema.type) | ||
| ids = records.filter_map { |record| record[field_schema.origin_key_target] }.uniq | ||
| sub_filter = Filter.new(condition_tree: ConditionTreeLeaf.new(field_schema.origin_key, 'In', ids)) | ||
| sub_records = association.list(caller, sub_filter, projection.union([field_schema.origin_key])) | ||
|
|
||
| records.each do |record| | ||
| record[name] = sub_records.find { |sr| sr[field_schema.origin_key] == record[field_schema.origin_key_target] } | ||
| end | ||
| assign_to_one_or_many_records(caller, records, name, field_schema, projection) | ||
| end | ||
| end | ||
|
|
||
| def assign_many_to_one_records(caller, records, name, field_schema, projection) | ||
| association = datasource.get_collection(field_schema.foreign_collection) | ||
| ids = records.each_with_object(Set.new) { |record, set| set << record[field_schema.foreign_key] }.delete(nil).to_a | ||
| sub_filter = Filter.new(condition_tree: ConditionTreeLeaf.new(field_schema.foreign_key_target, 'In', ids)) | ||
| sub_records = association.list(caller, sub_filter, projection.union([field_schema.foreign_key_target])) | ||
| sub_records_by_key = sub_records.to_h { |sr| [sr[field_schema.foreign_key_target], sr] } | ||
|
|
||
| records.each { |record| record[name] = sub_records_by_key[record[field_schema.foreign_key]] } | ||
| end | ||
|
|
||
| def assign_to_one_or_many_records(caller, records, name, field_schema, projection) | ||
|
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. |
||
| association = datasource.get_collection(field_schema.foreign_collection) | ||
| ids = records.each_with_object(Set.new) { |record, set| set << record[field_schema.origin_key_target] }.delete(nil).to_a | ||
| sub_filter = Filter.new(condition_tree: ConditionTreeLeaf.new(field_schema.origin_key, 'In', ids)) | ||
| sub_records = association.list(caller, sub_filter, projection.union([field_schema.origin_key])) | ||
| sub_records_by_key = sub_records.to_h { |sr| [sr[field_schema.origin_key], sr] } | ||
|
|
||
| records.each { |record| record[name] = sub_records_by_key[record[field_schema.origin_key_target]] } | ||
| end | ||
| 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
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 |
|---|---|---|
|
|
@@ -4,33 +4,35 @@ module Validation | |
| include ForestAdminDatasourceToolkit::Components::Query::ConditionTree | ||
| def get_validations(model, column) | ||
| return [] if column.is_a?(Hash) | ||
| return [] if before_validation_callback?(model) | ||
| return [] unless model._validators? && model._validators[column.name.to_sym].size.positive? | ||
|
|
||
| validations = [] | ||
| # NOTICE: Do not consider validations if a before_validation Active Records | ||
| # Callback is detected. | ||
| parse_column_validators(model._validators[column.name.to_sym], column) | ||
| end | ||
|
|
||
| def before_validation_callback?(model) | ||
| default_callback_excluded = [:normalize_changed_in_place_attributes] | ||
| return validations if model._validation_callbacks | ||
| .reject { |callback| default_callback_excluded.include?(callback.filter) } | ||
| .map(&:kind).include?(:before) | ||
| model._validation_callbacks.any? do |callback| | ||
| !default_callback_excluded.include?(callback.filter) && callback.kind == :before | ||
| end | ||
| end | ||
|
|
||
| if model._validators? && model._validators[column.name.to_sym].size.positive? | ||
| model._validators[column.name.to_sym].each do |validator| | ||
| # NOTICE: Do not consider conditional validations | ||
| next if validator.options[:if] || validator.options[:unless] || validator.options[:on] | ||
| def parse_column_validators(validators, column) | ||
| validations = [] | ||
| validators.each do |validator| | ||
| next if validator.options[:if] || validator.options[:unless] || validator.options[:on] | ||
|
|
||
| case validator.class.to_s | ||
| when Mongoid::Validatable::PresenceValidator.to_s | ||
| validations << { operator: Operators::PRESENT } | ||
| when ActiveModel::Validations::NumericalityValidator.to_s | ||
| validations = parse_numericality_validator(validator, validations) | ||
| when Mongoid::Validatable::LengthValidator.to_s | ||
| validations = parse_length_validator(validator, validations, column) | ||
| when Mongoid::Validatable::FormatValidator.to_s | ||
| validations = parse_format_validator(validator, validations) | ||
| end | ||
| case validator.class.to_s | ||
| when Mongoid::Validatable::PresenceValidator.to_s | ||
| validations << { operator: Operators::PRESENT } | ||
| when ActiveModel::Validations::NumericalityValidator.to_s | ||
| validations = parse_numericality_validator(validator, validations) | ||
| when Mongoid::Validatable::LengthValidator.to_s | ||
| validations = parse_length_validator(validator, validations, column) | ||
| when Mongoid::Validatable::FormatValidator.to_s | ||
| validations = parse_format_validator(validator, validations) | ||
| end | ||
| end | ||
|
|
||
| validations | ||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
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. |
||
| 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
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
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.
Function with many parameters (count = 5): assign_many_to_one_records [qlty:function-parameters]