Add a changeset cache to stop us looking up the same changeset over
authorTom Hughes <tom@compton.nu>
Tue, 28 Apr 2009 13:40:30 +0000 (13:40 +0000)
committerTom Hughes <tom@compton.nu>
Tue, 28 Apr 2009 13:40:30 +0000 (13:40 +0000)
and over again when processing a map call.

app/controllers/api_controller.rb
app/controllers/relation_controller.rb
app/controllers/search_controller.rb
app/models/node.rb
app/models/relation.rb
app/models/way.rb

index 1af1484..c0af0d3 100644 (file)
@@ -157,11 +157,12 @@ class ApiController < ApplicationController
     end
 
     visible_nodes = {}
+    changeset_cache = {}
     user_display_name_cache = {}
 
     @nodes.each do |node|
       if node.visible?
-        doc.root << node.to_xml_node(user_display_name_cache)
+        doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
         visible_nodes[node.id] = node
       end
     end
@@ -169,7 +170,7 @@ class ApiController < ApplicationController
     way_ids = Array.new
     ways.each do |way|
       if way.visible?
-        doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
+        doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
         way_ids << way.id
       end
     end 
@@ -187,7 +188,7 @@ class ApiController < ApplicationController
     # this "uniq" may be slightly inefficient; it may be better to first collect and output
     # all node-related relations, then find the *not yet covered* way-related ones etc.
     relations.uniq.each do |relation|
-      doc.root << relation.to_xml_node(user_display_name_cache)
+      doc.root << relation.to_xml_node(changeset_cache, user_display_name_cache)
     end
 
     response.headers["Content-Disposition"] = "attachment; filename=\"map.osm\""
index a249e42..440c695 100644 (file)
@@ -120,26 +120,27 @@ class RelationController < ApplicationController
         # create XML.
         doc = OSM::API.new.get_xml_doc
         visible_nodes = {}
+        changeset_cache = {}
         user_display_name_cache = {}
 
         nodes.each do |node|
           if node.visible? # should be unnecessary if data is consistent.
-            doc.root << node.to_xml_node(user_display_name_cache)
+            doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
             visible_nodes[node.id] = node
           end
         end
         ways.each do |way|
           if way.visible? # should be unnecessary if data is consistent.
-            doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
+            doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
           end
         end
         relations.each do |rel|
           if rel.visible? # should be unnecessary if data is consistent.
-            doc.root << rel.to_xml_node(user_display_name_cache)
+            doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
           end
         end
         # finally add self and output
-        doc.root << relation.to_xml_node(user_display_name_cache)
+        doc.root << relation.to_xml_node(changeset_cache, user_display_name_cache)
         render :text => doc.to_s, :content_type => "text/xml"
 
       else
index 17326db..b411a70 100644 (file)
@@ -111,19 +111,20 @@ class SearchController < ApplicationController
 
     # Print
     visible_nodes = {}
+    changeset_cache = {}
     user_display_name_cache = {}
     doc = OSM::API.new.get_xml_doc
     nodes.each do |node|
-      doc.root << node.to_xml_node(user_display_name_cache)
+      doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
       visible_nodes[node.id] = node
     end
 
     ways.each do |way|
-      doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
+      doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
     end 
 
     relations.each do |rel|
-      doc.root << rel.to_xml_node(user_display_name_cache)
+      doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
     end 
     render :text => doc.to_s, :content_type => "text/xml"
   end
index d5967c2..bd51d6c 100644 (file)
@@ -188,27 +188,33 @@ class Node < ActiveRecord::Base
     return doc
   end
 
-  def to_xml_node(user_display_name_cache = nil)
+  def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
     el1 = XML::Node.new 'node'
     el1['id'] = self.id.to_s
     el1['lat'] = self.lat.to_s
     el1['lon'] = self.lon.to_s
     el1['version'] = self.version.to_s
     el1['changeset'] = self.changeset_id.to_s
-    
-    user_display_name_cache = {} if user_display_name_cache.nil?
 
-    if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
+    if changeset_cache.key?(self.changeset_id)
+      # use the cache if available
+    else
+      changeset_cache[self.changeset_id] = self.changeset.user_id
+    end
+
+    user_id = changeset_cache[self.changeset_id]
+
+    if user_display_name_cache.key?(user_id)
       # use the cache if available
     elsif self.changeset.user.data_public?
-      user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
+      user_display_name_cache[user_id] = self.changeset.user.display_name
     else
-      user_display_name_cache[self.changeset.user_id] = nil
+      user_display_name_cache[user_id] = nil
     end
 
-    if not user_display_name_cache[self.changeset.user_id].nil?
-      el1['user'] = user_display_name_cache[self.changeset.user_id]
-      el1['uid'] = self.changeset.user_id.to_s
+    if not user_display_name_cache[user_id].nil?
+      el1['user'] = user_display_name_cache[user_id]
+      el1['uid'] = user_id.to_s
     end
 
     self.tags.each do |k,v|
index f9bba98..d2602f4 100644 (file)
@@ -87,7 +87,7 @@ class Relation < ActiveRecord::Base
     return doc
   end
 
-  def to_xml_node(user_display_name_cache = nil)
+  def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
     el1 = XML::Node.new 'relation'
     el1['id'] = self.id.to_s
     el1['visible'] = self.visible.to_s
@@ -95,19 +95,25 @@ class Relation < ActiveRecord::Base
     el1['version'] = self.version.to_s
     el1['changeset'] = self.changeset_id.to_s
 
-    user_display_name_cache = {} if user_display_name_cache.nil?
-    
-    if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
+    if changeset_cache.key?(self.changeset_id)
+      # use the cache if available
+    else
+      changeset_cache[self.changeset_id] = self.changeset.user_id
+    end
+
+    user_id = changeset_cache[self.changeset_id]
+
+    if user_display_name_cache.key?(user_id)
       # use the cache if available
     elsif self.changeset.user.data_public?
-      user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
+      user_display_name_cache[user_id] = self.changeset.user.display_name
     else
-      user_display_name_cache[self.changeset.user_id] = nil
+      user_display_name_cache[user_id] = nil
     end
 
-    if not user_display_name_cache[self.changeset.user_id].nil?
-      el1['user'] = user_display_name_cache[self.changeset.user_id]
-      el1['uid'] = self.changeset.user_id.to_s
+    if not user_display_name_cache[user_id].nil?
+      el1['user'] = user_display_name_cache[user_id]
+      el1['uid'] = user_id.to_s
     end
 
     self.relation_members.each do |member|
index 2b6f562..52d2802 100644 (file)
@@ -89,7 +89,7 @@ class Way < ActiveRecord::Base
     return doc
   end
 
-  def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
+  def to_xml_node(visible_nodes = nil, changeset_cache = {}, user_display_name_cache = {})
     el1 = XML::Node.new 'way'
     el1['id'] = self.id.to_s
     el1['visible'] = self.visible.to_s
@@ -97,19 +97,25 @@ class Way < ActiveRecord::Base
     el1['version'] = self.version.to_s
     el1['changeset'] = self.changeset_id.to_s
 
-    user_display_name_cache = {} if user_display_name_cache.nil?
+    if changeset_cache.key?(self.changeset_id)
+      # use the cache if available
+    else
+      changeset_cache[self.changeset_id] = self.changeset.user_id
+    end
+
+    user_id = changeset_cache[self.changeset_id]
 
-    if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
+    if user_display_name_cache.key?(user_id)
       # use the cache if available
     elsif self.changeset.user.data_public?
-      user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
+      user_display_name_cache[user_id] = self.changeset.user.display_name
     else
-      user_display_name_cache[self.changeset.user_id] = nil
+      user_display_name_cache[user_id] = nil
     end
 
-    if not user_display_name_cache[self.changeset.user_id].nil?
-      el1['user'] = user_display_name_cache[self.changeset.user_id]
-      el1['uid'] = self.changeset.user_id.to_s
+    if not user_display_name_cache[user_id].nil?
+      el1['user'] = user_display_name_cache[user_id]
+      el1['uid'] = user_id.to_s
     end
 
     # make sure nodes are output in sequence_id order