X-Git-Url: https://git.openstreetmap.org./chef.git/blobdiff_plain/fb1f1d56e8d8a6a86b79c71546e6c114fec2640c..307adfed3045a1673b09bccce74acaaf530d3c9b:/cookbooks/tile/files/default/ruby/expire.rb?ds=inline diff --git a/cookbooks/tile/files/default/ruby/expire.rb b/cookbooks/tile/files/default/ruby/expire.rb index 7757386c5..3d693ad5b 100755 --- a/cookbooks/tile/files/default/ruby/expire.rb +++ b/cookbooks/tile/files/default/ruby/expire.rb @@ -1,11 +1,10 @@ #!/usr/bin/ruby -require 'rubygems' -require 'proj4' -require 'xml/libxml' -require 'set' -require 'time' -require 'mmap' +require "rubygems" +require "proj4" +require "xml/libxml" +require "set" +require "time" module Expire # projection object to go from latlon -> spherical mercator @@ -19,9 +18,9 @@ module Expire # the size of the meta tile blocks METATILE = 8 # the directory root for meta tiles - HASH_ROOT = "/tiles/default/" + HASH_ROOT = "/tiles/default/".freeze # node cache file - NODE_CACHE_FILE = "/store/database/nodes" + NODE_CACHE_FILE = "/store/database/nodes".freeze # turns a spherical mercator coord into a tile coord def self.tile_from_merc(point, zoom) @@ -50,8 +49,8 @@ module Expire # generate the path hash_path = (0..4).collect do |i| (((x >> 4 * i) & 0xf) << 4) | ((y >> 4 * i) & 0xf) - end.reverse.join('/') - z.to_s + '/' + hash_path + ".meta" + end.reverse.join("/") + z.to_s + "/" + hash_path + ".meta" end # time to reset to, some very stupidly early time, before OSM started @@ -102,17 +101,13 @@ module Expire # we put all the nodes into the hash, as it doesn't matter whether the node was # added, deleted or modified - the tile will need updating anyway. - doc.find('//node').each do |node| - lat = node['lat'].to_f - if lat < -85 - lat = -85 - end - if lat > 85 - lat = 85 - end - point = Proj4::Point.new(Math::PI * node['lon'].to_f / 180, + doc.find("//node").each do |node| + lat = node["lat"].to_f + lat = -85 if lat < -85 + lat = 85 if lat > 85 + point = Proj4::Point.new(Math::PI * node["lon"].to_f / 180, Math::PI * lat / 180) - nodes[node['id'].to_i] = tile_from_latlon(point, max_zoom) + nodes[node["id"].to_i] = tile_from_latlon(point, max_zoom) end # now we look for all the ways that have changed and put all of their nodes into @@ -124,18 +119,19 @@ module Expire # itself deleted and the coverage of the point set isn't enough to encompass the # change. node_cache = NodeCache.new(NODE_CACHE_FILE) - doc.find('//way/nd').each do |node| - node_id = node['ref'].to_i + doc.find("//way/nd").each do |node| + node_id = node["ref"].to_i next if nodes.include? node_id # this is a node referenced but not added, modified or deleted, so it should # still be in the node cache. - if entry = node_cache[node_id] + if (entry = node_cache[node_id]) point = Proj4::Point.new(entry.lon, entry.lat) nodes[node_id] = tile_from_merc(point, max_zoom) end end + node_cache.close # create a set of all the tiles at the maximum zoom level which are touched by # any of the nodes we've collected. we'll create the tiles at other zoom levels @@ -143,7 +139,7 @@ module Expire set = Set.new nodes.values # expire tiles and shrink to the set of parents - (max_zoom).downto(min_zoom) do |_| + max_zoom.downto(min_zoom) do |_| # allow the block to work on the set, returning the set at the next # zoom level set = yield set @@ -164,12 +160,17 @@ module Expire # open the cache def initialize(filename) - @cache = Mmap.new(filename) + @cache = File.new(filename, "r") + + throw "Unexpected format" unless @cache.sysread(4).unpack("l").first == 1 + throw "Unexpected ID size" unless @cache.sysread(4).unpack("l").first == 8 - throw "Unexpected format" unless @cache[0..3].unpack("l").first == 1 - throw "Unexpected ID size" unless @cache[4..7].unpack("l").first == 8 + @max_id = @cache.sysread(8).unpack("q").first + end - @max_id = @cache[8..15].unpack("q").first + # close the cache + def close + @cache.close end # lookup a node @@ -177,11 +178,11 @@ module Expire if id <= @max_id offset = 16 + id * 8 - lon, lat = @cache[offset..offset + 7].unpack("ll") + @cache.sysseek(offset) - if lon != -2147483648 && lat != -2147483648 - node = Node.new(lon, lat) - end + lon, lat = @cache.sysread(8).unpack("ll") + + node = Node.new(lon, lat) if lon != -2147483648 && lat != -2147483648 end node