Permalink
Browse files

Refactor out association identity. Pass the entire reflection through…

… association! so association type can be recorded for further use
  • Loading branch information...
1 parent aab8082 commit b8f15dacc2ec485385b19bc51a4843df26072ad3 @sdsykes sdsykes committed Mar 30, 2009
Showing with 24 additions and 10 deletions.
  1. +2 −1 lib/callsite.rb
  2. +21 −8 lib/optimizations/associations/association_set.rb
  3. +1 −1 lib/optimizations/associations/macro.rb
View
@@ -24,9 +24,10 @@ def column!( column )
end
# Flag an association as seen
+ # association should be an AssociationReflection object
#
def association!(association, record_id)
- if preloadable_association?(association)
+ if preloadable_association?(association.name)
associations.register(association, record_id)
end
end
@@ -3,7 +3,8 @@ module Optimizations
module Associations
# Keeps track of how a result set is used to access associations
- # Each callsite will contain one of these objects.
+ # Each callsite where associations are accessed will contain one of
+ # these objects.
# Each thread will collect data, and we check this data before each
# fetch from the database, adding any associations that are needed
# which are returned when to_preload is called.
@@ -53,16 +54,14 @@ def initialize
end
def reset
- @associations = Set.new
- @accessed_via = {}
+ @associations = {}
@result_set_size = 0
end
def register(association, record_id)
if @result_set_size > 1
- @associations << association
- @accessed_via[association] ||= []
- @accessed_via[association] << record_id
+ assoc = (@associations[association.name] ||= AssociationIdentity.new(association))
+ assoc.register(record_id)
end
end
@@ -71,7 +70,7 @@ def register_result_set(result_set)
end
def to_preload
- @associations.select { |association| preload_this_assoc?(association) }
+ @associations.values.select { |association| preload_this_assoc?(association) }.map(&:name)
end
private
@@ -87,10 +86,24 @@ def preload_this_assoc?(association)
if @result_set_size <= 1
false
else
- @accessed_via[association].size > @result_set_size / 4
+ association.accessed_via.size > @result_set_size / 4
end
end
end
+
+ class AssociationIdentity
+ attr_reader :name, :type, :accessed_via
+
+ def initialize(association)
+ @name = association.name
+ @type = association.macro
+ @accessed_via = []
+ end
+
+ def register(record_id)
+ @accessed_via << record_id
+ end
+ end
end
end
end
@@ -73,7 +73,7 @@ def self.included( base )
# do collections at the moment anyway
#
def load_target_with_scrooge
- scrooge_seen_association!(@reflection.name)
+ scrooge_seen_association!(@reflection)
load_target_without_scrooge
end

0 comments on commit b8f15da

Please sign in to comment.