Commit cd8db653189648b8bb8246333afa09da706a041f

Authored by AntonioTerceiro
1 parent 6eae175f

ActionItem466: use eager loading in map_traversal

instead of having O(n) SQL queries, let's do only O(log(n)).


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2058 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing 1 changed file with 6 additions and 8 deletions   Show diff stats
lib/acts_as_filesystem.rb
... ... @@ -158,15 +158,13 @@ module ActsAsFileSystem
158 158 end
159 159  
160 160 def map_traversal(&block)
161   - stack = []
162 161 result = []
163   - stack.push(self)
164   - while !stack.empty?
165   - element = stack.pop
166   - result.push(element)
167   - element.children.reverse.each do |item|
168   - stack.push(item)
169   - end
  162 + current_level = [self]
  163 +
  164 + while !current_level.empty?
  165 + result += current_level
  166 + ids = current_level.map(&:id)
  167 + current_level = self.class.find(:all, :conditions => { :parent_id => ids})
170 168 end
171 169 block ||= (lambda { |x| x })
172 170 result.map(&block)
... ...