Class: Bullet::Detector::NPlusOneQuery
- Inherits:
-
Association
- Object
- Base
- Association
- Bullet::Detector::NPlusOneQuery
- Extended by:
- Bullet::Dependency, StackTraceFilter
- Defined in:
- lib/bullet/detector/n_plus_one_query.rb
Constant Summary
Constants included from StackTraceFilter
Class Method Summary collapse
- .add_impossible_object(object) ⇒ Object
- .add_inversed_object(object, association) ⇒ Object
- .add_possible_objects(object_or_objects) ⇒ Object
-
.association?(object, associations) ⇒ Boolean
check if object => associations already exists in object_associations.
-
.call_association(object, associations, caller_stack = nil) ⇒ Object
executed when object.associations is called.
-
.conditions_met?(object, associations) ⇒ Boolean
decide whether the object.associations is unpreloaded or not.
- .impossible?(object) ⇒ Boolean
- .possible?(object) ⇒ Boolean
- .update_inversed_object(object) ⇒ Object
Methods included from Bullet::Dependency
active_record40?, active_record41?, active_record42?, active_record4?, active_record50?, active_record51?, active_record52?, active_record5?, active_record60?, active_record61?, active_record6?, active_record70?, active_record71?, active_record72?, active_record7?, active_record80?, active_record81?, active_record8?, active_record?, active_record_version, mongoid4x?, mongoid5x?, mongoid6x?, mongoid7x?, mongoid8x?, mongoid9x?, mongoid?, mongoid_version
Methods included from StackTraceFilter
caller_in_project, excluded_stacktrace_path?
Methods inherited from Association
add_call_object_associations, add_object_associations, impossible_objects, possible_objects
Class Method Details
.add_impossible_object(object) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 68 def add_impossible_object(object) return unless Bullet.start? return unless Bullet.n_plus_one_query_enable? return unless object.bullet_primary_key_value Bullet.debug('Detector::NPlusOneQuery#add_impossible_object', "object: #{object.bullet_key}") impossible_objects.add object.bullet_key end |
.add_inversed_object(object, association) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 77 def add_inversed_object(object, association) return unless Bullet.start? return unless Bullet.n_plus_one_query_enable? object_key = object.bullet_primary_key_value ? object.bullet_key : object.object_id Bullet.debug( 'Detector::NPlusOneQuery#add_inversed_object', "object: #{object_key}, association: #{association}" ) inversed_objects.add object_key, association end |
.add_possible_objects(object_or_objects) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 44 def add_possible_objects(object_or_objects) return unless Bullet.start? return unless Bullet.n_plus_one_query_enable? objects = Array.wrap(object_or_objects) class_names_match_regex = true primary_key_values_are_empty = true keys_joined = objects.map do |obj| unless obj.class.name =~ /^HABTM_/ class_names_match_regex = false end unless obj.bullet_primary_key_value.nil? primary_key_values_are_empty = false end obj.bullet_key end.join(", ") unless class_names_match_regex || primary_key_values_are_empty Bullet.debug('Detector::NPlusOneQuery#add_possible_objects', "objects: #{keys_joined}") objects.each { |object| possible_objects.add object.bullet_key } end end |
.association?(object, associations) ⇒ Boolean
check if object => associations already exists in object_associations.
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 113 def association?(object, associations) value = object_associations[object.bullet_key] value&.each do |v| # associations == v comparison order is important here because # v variable might be a squeel node where :== method is redefined, # so it does not compare values at all and return unexpected results result = v.is_a?(Hash) ? v.key?(associations) : associations == v return true if result end false end |
.call_association(object, associations, caller_stack = nil) ⇒ Object
executed when object.associations is called. first, it keeps this method call for object.association. then, it checks if this associations call is unpreload.
if it is, keeps this unpreload associations and caller.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 16 def call_association(object, associations, caller_stack = nil) return unless Bullet.start? return unless Bullet.n_plus_one_query_enable? return unless object.bullet_primary_key_value return if inversed_objects.include?(object.bullet_key, associations) # AR short-circuits a polymorphic belongs_to read to nil when the # *_type column is nil — no SQL is issued, so this cannot be an N+1. # Still record the call so a present preload isn't flagged as unused. if optional_polymorphic_belongs_to_with_nil_type?(object, associations) add_call_object_associations(object, associations) call_stacks.add(object.bullet_key, caller_stack) if caller_stack return end add_call_object_associations(object, associations) call_stacks.add(object.bullet_key, caller_stack) if caller_stack Bullet.debug( 'Detector::NPlusOneQuery#call_association', "object: #{object.bullet_key}, associations: #{associations}" ) if !excluded_stacktrace_path? && conditions_met?(object, associations) Bullet.debug('detect n + 1 query', "object: #{object.bullet_key}, associations: #{associations}") create_notification(caller_in_project(object.bullet_key), object.class.to_s, associations) end end |
.conditions_met?(object, associations) ⇒ Boolean
decide whether the object.associations is unpreloaded or not.
100 101 102 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 100 def conditions_met?(object, associations) possible?(object) && !impossible?(object) && !association?(object, associations) end |
.impossible?(object) ⇒ Boolean
108 109 110 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 108 def impossible?(object) impossible_objects.include? object.bullet_key end |
.possible?(object) ⇒ Boolean
104 105 106 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 104 def possible?(object) possible_objects.include? object.bullet_key end |
.update_inversed_object(object) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/bullet/detector/n_plus_one_query.rb', line 89 def update_inversed_object(object) if inversed_objects&.key?(object.object_id) Bullet.debug( 'Detector::NPlusOneQuery#update_inversed_object', "object from #{object.object_id} to #{object.bullet_key}" ) inversed_objects.add(object.bullet_key, inversed_objects[object.object_id].to_a) end end |