Class: Bullet::Detector::NPlusOneQuery

Inherits:
Association show all
Extended by:
Bullet::Dependency
Defined in:
lib/bullet/detector/n_plus_one_query.rb

Class Method Summary collapse

Methods included from Bullet::Dependency

active_record30?, active_record31?, active_record32?, active_record3?, active_record40?, active_record41?, active_record42?, active_record4?, active_record?, active_record_version, mongoid2x?, mongoid3x?, mongoid4x?, mongoid?, mongoid_version, rails?

Methods inherited from Association

add_call_object_associations, add_object_associations, impossible_objects, possible_objects

Class Method Details

.add_impossible_object(object) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/bullet/detector/n_plus_one_query.rb', line 34

def add_impossible_object(object)
  return unless Bullet.start?
  return unless Bullet.n_plus_one_query_enable?
  return unless object.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



43
44
45
46
47
48
49
50
# File 'lib/bullet/detector/n_plus_one_query.rb', line 43

def add_inversed_object(object, association)
  return unless Bullet.start?
  return unless Bullet.n_plus_one_query_enable?
  return unless object.primary_key_value

  Bullet.debug("Detector::NPlusOneQuery#add_inversed_object", "object: #{object.bullet_key}, association: #{association}")
  inversed_objects.add object.bullet_key, association
end

.add_possible_objects(object_or_objects) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/bullet/detector/n_plus_one_query.rb', line 24

def add_possible_objects(object_or_objects)
  return unless Bullet.start?
  return unless Bullet.n_plus_one_query_enable?
  objects = Array(object_or_objects)
  return if objects.map(&:primary_key_value).compact.empty?

  Bullet.debug("Detector::NPlusOneQuery#add_possible_objects", "objects: #{objects.map(&:bullet_key).join(', ')}")
  objects.each { |object| possible_objects.add object.bullet_key }
end

.association?(object, associations) ⇒ Boolean

check if object => associations already exists in object_associations.

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bullet/detector/n_plus_one_query.rb', line 66

def association?(object, associations)
  value = object_associations[object.bullet_key]
  if value
    value.each do |v|
      # associations == v comparision 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.has_key?(associations) : associations == v
      return true if result
    end
  end

  false
end

.call_association(object, associations) ⇒ Object

executed when object.assocations 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.


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bullet/detector/n_plus_one_query.rb', line 11

def call_association(object, associations)
  return unless Bullet.start?
  return unless object.primary_key_value
  return if inversed_objects.include?(object.bullet_key, associations)
  add_call_object_associations(object, associations)

  Bullet.debug("Detector::NPlusOneQuery#call_association", "object: #{object.bullet_key}, associations: #{associations}")
  if conditions_met?(object, associations)
    Bullet.debug("detect n + 1 query", "object: #{object.bullet_key}, associations: #{associations}")
    create_notification caller_in_project, object.class.to_s, associations
  end
end

.conditions_met?(object, associations) ⇒ Boolean

decide whether the object.associations is unpreloaded or not.

Returns:

  • (Boolean)


53
54
55
# File 'lib/bullet/detector/n_plus_one_query.rb', line 53

def conditions_met?(object, associations)
  possible?(object) && !impossible?(object) && !association?(object, associations)
end

.impossible?(object) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/bullet/detector/n_plus_one_query.rb', line 61

def impossible?(object)
  impossible_objects.include? object.bullet_key
end

.possible?(object) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/bullet/detector/n_plus_one_query.rb', line 57

def possible?(object)
  possible_objects.include? object.bullet_key
end