Optimise finding of relations, partially reversing change #8443 but in

a more rails like way.
This commit is contained in:
Tom Hughes 2008-07-16 23:42:59 +00:00
parent 66b0dd135e
commit d6e047d7d4
3 changed files with 23 additions and 5 deletions

View file

@ -128,8 +128,8 @@ class AmfController < ApplicationController
points = nodes_not_used_in_area.collect { |n| [n.id, n.lon, n.lat, n.tags_as_hash] } points = nodes_not_used_in_area.collect { |n| [n.id, n.lon, n.lat, n.tags_as_hash] }
# find the relations used by those nodes and ways # find the relations used by those nodes and ways
relations = nodes_in_area.collect { |node| node.containing_relations.visible }.flatten + relations = Relation.find_for_nodes(nodes_in_area.collect { |n| n.id }, :conditions => "visible = 1") +
way_ids.collect { |id| Way.find(id).containing_relations.visible }.flatten Relation.find_for_ways(way_ids, :conditions => "visible = 1")
relation_ids = relations.collect { |relation| relation.id }.uniq relation_ids = relations.collect { |relation| relation.id }.uniq
end end

View file

@ -176,15 +176,15 @@ class ApiController < ApplicationController
end end
end end
relations = visible_nodes.values.collect { |node| node.containing_relations.visible }.flatten + relations = Relation.find_for_nodes(visible_nodes.keys, :conditions => "visible = 1") +
way_ids.collect { |id| Way.find(id).containing_relations.visible }.flatten Relation.find_for_ways(way_ids, :conditions => "visible = 1")
# we do not normally return the "other" partners referenced by an relation, # we do not normally return the "other" partners referenced by an relation,
# e.g. if we return a way A that is referenced by relation X, and there's # e.g. if we return a way A that is referenced by relation X, and there's
# another way B also referenced, that is not returned. But we do make # another way B also referenced, that is not returned. But we do make
# an exception for cases where an relation references another *relation*; # an exception for cases where an relation references another *relation*;
# in that case we return that as well (but we don't go recursive here) # in that case we return that as well (but we don't go recursive here)
relations += relations.collect { |relation| relation.containing_relations.visible }.flatten relations += Relation.find_for_relations(relations.collect { |r| r.id }, :conditions => "visible = 1")
# this "uniq" may be slightly inefficient; it may be better to first collect and output # 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. # all node-related relations, then find the *not yet covered* way-related ones etc.

View file

@ -105,6 +105,24 @@ class Relation < ActiveRecord::Base
return el1 return el1
end end
def self.find_for_nodes(ids, options = {})
self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'node' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
return self.find(:all, options)
end
end
def self.find_for_ways(ids, options = {})
self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'way' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
return self.find(:all, options)
end
end
def self.find_for_relations(ids, options = {})
self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'relation' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
return self.find(:all, options)
end
end
# FIXME is this really needed? # FIXME is this really needed?
def members def members
unless @members unless @members