1 #! /usr/bin/env python3
2 #-----------------------------------------------------------------------------
3 # nominatim - [description]
4 #-----------------------------------------------------------------------------
6 # Indexing tool for the Nominatim database.
8 # Based on C version by Brian Quinion
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #-----------------------------------------------------------------------------
25 from argparse import ArgumentParser, RawDescriptionHelpFormatter, ArgumentTypeError
30 from datetime import datetime
32 from psycopg2.extras import wait_select
35 log = logging.getLogger()
37 def make_connection(options, asynchronous=False):
38 return psycopg2.connect(dbname=options.dbname, user=options.user,
39 password=options.password, host=options.host,
40 port=options.port, async_=asynchronous)
43 class RankRunner(object):
44 """ Returns SQL commands for indexing one rank within the placex table.
47 def __init__(self, rank):
51 return "rank {}".format(self.rank)
53 def sql_index_sectors(self):
54 return """SELECT geometry_sector, count(*) FROM placex
55 WHERE rank_search = {} and indexed_status > 0
56 GROUP BY geometry_sector
57 ORDER BY geometry_sector""".format(self.rank)
59 def sql_nosector_places(self):
60 return """SELECT place_id FROM placex
61 WHERE indexed_status > 0 and rank_search = {}
62 ORDER BY geometry_sector""".format(self.rank)
64 def sql_sector_places(self):
65 return """SELECT place_id FROM placex
66 WHERE indexed_status > 0 and rank_search = {}
67 and geometry_sector = %s""".format(self.rank)
69 def sql_index_place(self):
70 return "UPDATE placex SET indexed_status = 0 WHERE place_id = %s"
73 class InterpolationRunner(object):
74 """ Returns SQL commands for indexing the address interpolation table
75 location_property_osmline.
79 return "interpolation lines (location_property_osmline)"
81 def sql_index_sectors(self):
82 return """SELECT geometry_sector, count(*) FROM location_property_osmline
83 WHERE indexed_status > 0
84 GROUP BY geometry_sector
85 ORDER BY geometry_sector"""
87 def sql_nosector_places(self):
88 return """SELECT place_id FROM location_property_osmline
89 WHERE indexed_status > 0
90 ORDER BY geometry_sector"""
92 def sql_sector_places(self):
93 return """SELECT place_id FROM location_property_osmline
94 WHERE indexed_status > 0 and geometry_sector = %s
95 ORDER BY geometry_sector"""
97 def sql_index_place(self):
98 return """UPDATE location_property_osmline
99 SET indexed_status = 0 WHERE place_id = %s"""
102 class DBConnection(object):
103 """ A single non-blocking database connection.
106 def __init__(self, options):
107 self.current_query = None
108 self.current_params = None
114 if self.conn is not None:
118 self.conn = make_connection(options, asynchronous=True)
121 self.cursor = self.conn.cursor()
124 """ Block until any pending operation is done.
126 wait_select(self.conn)
127 self.current_query = None
129 def perform(self, sql, args=None):
130 """ Send SQL query to the server. Returns immediately without
133 self.current_query = sql
134 self.current_params = args
135 self.cursor.execute(sql, args)
138 """ File descriptor to wait for. (Makes this class select()able.)
140 return self.conn.fileno()
143 """ Check if the connection is available for a new query.
145 Also checks if the previous query has run into a deadlock.
146 If so, then the previous query is repeated.
148 if self.current_query is None:
152 if self.conn.poll() == psycopg2.extensions.POLL_OK:
153 self.current_query = None
155 except psycopg2.extensions.TransactionRollbackError as e:
156 if e.pgcode == '40P01':
157 log.info("Deadlock detected (params = {}), retry.".format(self.current_params))
158 self.cursor.execute(self.current_query, self.current_params)
165 class Indexer(object):
166 """ Main indexing routine.
169 def __init__(self, options):
170 self.minrank = max(0, options.minrank)
171 self.maxrank = min(30, options.maxrank)
172 self.conn = make_connection(options)
173 self.threads = [DBConnection(options) for i in range(options.threads)]
176 """ Run indexing over the entire database.
178 log.warning("Starting indexing rank ({} to {}) using {} threads".format(
179 self.minrank, self.maxrank, len(self.threads)))
181 for rank in range(self.minrank, self.maxrank):
182 self.index(RankRunner(rank))
184 if self.maxrank == 30:
185 self.index(InterpolationRunner())
187 self.index(RankRunner(self.maxrank))
189 def index(self, obj):
190 """ Index a single rank or table. `obj` describes the SQL to use
193 log.warning("Starting {}".format(obj.name()))
195 cur = self.conn.cursor(name='main')
196 cur.execute(obj.sql_index_sectors())
201 log.debug("Total number of rows; {}".format(total_tuples))
203 cur.scroll(0, mode='absolute')
205 next_thread = self.find_free_thread()
207 rank_start_time = datetime.now()
209 sector_sql = obj.sql_sector_places()
210 index_sql = obj.sql_index_place()
211 min_grouped_tuples = total_tuples - len(self.threads) * 1000
213 next_info = 100 if log.isEnabledFor(logging.INFO) else total_tuples + 1
218 # Should we do the remaining ones together?
219 do_all = done_tuples > min_grouped_tuples
221 pcur = self.conn.cursor(name='places')
224 pcur.execute(obj.sql_nosector_places())
226 pcur.execute(sector_sql, (sector, ))
230 log.debug("Processing place {}".format(place_id))
231 thread = next(next_thread)
233 thread.perform(index_sql, (place_id,))
236 if done_tuples >= next_info:
238 done_time = (now - rank_start_time).total_seconds()
239 tuples_per_sec = done_tuples / done_time
240 log.info("Done {} in {} @ {:.3f} per second - {} ETA (seconds): {:.2f}"
241 .format(done_tuples, int(done_time),
242 tuples_per_sec, obj.name(),
243 (total_tuples - done_tuples)/tuples_per_sec))
244 next_info += int(tuples_per_sec)
253 for t in self.threads:
256 rank_end_time = datetime.now()
257 diff_seconds = (rank_end_time-rank_start_time).total_seconds()
259 log.warning("Done {}/{} in {} @ {:.3f} per second - FINISHED {}\n".format(
260 done_tuples, total_tuples, int(diff_seconds),
261 done_tuples/diff_seconds, obj.name()))
263 def find_free_thread(self):
264 """ Generator that returns the next connection that is free for
276 # refresh the connections occasionaly to avoid potential
277 # memory leaks in Postgresql.
278 if command_stat > 100000:
279 for t in self.threads:
280 while not t.is_done():
286 ready, _, _ = select.select(self.threads, [], [])
288 assert(False, "Unreachable code")
291 def nominatim_arg_parser():
292 """ Setup the command-line parser for the tool.
295 return re.sub("\s\s+" , " ", s)
297 p = ArgumentParser(description="Indexing tool for Nominatim.",
298 formatter_class=RawDescriptionHelpFormatter)
300 p.add_argument('-d', '--database',
301 dest='dbname', action='store', default='nominatim',
302 help='Name of the PostgreSQL database to connect to.')
303 p.add_argument('-U', '--username',
304 dest='user', action='store',
305 help='PostgreSQL user name.')
306 p.add_argument('-W', '--password',
307 dest='password_prompt', action='store_true',
308 help='Force password prompt.')
309 p.add_argument('-H', '--host',
310 dest='host', action='store',
311 help='PostgreSQL server hostname or socket location.')
312 p.add_argument('-P', '--port',
313 dest='port', action='store',
314 help='PostgreSQL server port')
315 p.add_argument('-r', '--minrank',
316 dest='minrank', type=int, metavar='RANK', default=0,
317 help='Minimum/starting rank.')
318 p.add_argument('-R', '--maxrank',
319 dest='maxrank', type=int, metavar='RANK', default=30,
320 help='Maximum/finishing rank.')
321 p.add_argument('-t', '--threads',
322 dest='threads', type=int, metavar='NUM', default=1,
323 help='Number of threads to create for indexing.')
324 p.add_argument('-v', '--verbose',
325 dest='loglevel', action='count', default=0,
326 help='Increase verbosity')
330 if __name__ == '__main__':
331 logging.basicConfig(stream=sys.stderr, format='%(levelname)s: %(message)s')
333 options = nominatim_arg_parser().parse_args(sys.argv[1:])
335 log.setLevel(max(3 - options.loglevel, 0) * 10)
337 options.password = None
338 if options.password_prompt:
339 password = getpass.getpass("Database password: ")
340 options.password = password
342 Indexer(options).run()