Skip to content

Commit

Permalink
a more stable fix for slow SEL
Browse files Browse the repository at this point in the history
  • Loading branch information
somebee committed Sep 15, 2008
1 parent f690b23 commit 84ad212
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/dm-core/associations/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ def get_parent(child, parent = nil)
unless collection.empty?
collection.send(:lazy_load)
children.each do |c|
p = parent_identity_map[child_key.get(c)] || collection.get(*child_key.get(c))
c.send(association_accessor).instance_variable_set(:@parent, p)
c.send(association_accessor).instance_variable_set(:@parent, collection.get(*child_key.get(c)))
end
child.send(association_accessor).instance_variable_get(:@parent)
end
Expand Down
10 changes: 8 additions & 2 deletions lib/dm-core/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def reload(query = {})
def get(*key)
key = model.typecast_key(key)
if loaded?
# loop over the collection to find the matching resource
detect { |resource| resource.key == key }
# find indexed resource (create index first if it does not exist)
(@index||=map{|r| [r.key,r]}.to_hash)[key]
elsif query.limit || query.offset > 0
# current query is exclusive, find resource within the set

Expand Down Expand Up @@ -533,6 +533,7 @@ def add(resource)
def relate_resource(resource)
return unless resource
resource.collection = self
wipe_index
resource
end

Expand All @@ -541,6 +542,7 @@ def relate_resource(resource)
def orphan_resource(resource)
return unless resource
resource.collection = nil if resource.collection == self
wipe_index
resource
end

Expand Down Expand Up @@ -578,6 +580,10 @@ def keys
def identity_map
repository.identity_map(model)
end

def wipe_index
@index = nil
end

##
# @api private
Expand Down

0 comments on commit 84ad212

Please sign in to comment.