]> git.openstreetmap.org Git - rails.git/commitdiff
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 1af14845585ee6b39e387cf6a5ab7071c509625e..c0af0d3e5358487e92d6826110f62c76e0e3b897 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 a249e42a02f9b1b4142b2993e0abef9e3c0fe765..440c69550afb309d89dada9c78163b47ec9c225f 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 17326db07e70f79c66fc64733057b4dcc47f1f9e..b411a70a29f417d4598b2314530f95695f9fa702 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 d5967c2fa1caab02bcd54dbc1b8f0380b7eac075..bd51d6c5013712835e8e811fee50520f230aecdf 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 f9bba98539b719fb69dc1c88a0be362d8d88516e..d2602f497270c455d3d3a4b44062670eaac689bd 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 2b6f562be6c2fbbc95c290a70a4728d701dc2c5e..52d28020969f4e1d1ed51a70e21144ad2cf54eac 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