2020-01-21 23:57:07 +03:00
|
|
|
#! /usr/bin/env python3
|
2020-01-19 15:37:45 +03:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# nominatim - [description]
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Indexing tool for the Nominatim database.
|
|
|
|
#
|
|
|
|
# Based on C version by Brian Quinion
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
from argparse import ArgumentParser, RawDescriptionHelpFormatter, ArgumentTypeError
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import getpass
|
|
|
|
from datetime import datetime
|
2020-01-19 23:56:37 +03:00
|
|
|
import select
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
from indexer.progress import ProgressLogger
|
2020-08-14 11:33:58 +03:00
|
|
|
from indexer.db import DBConnection, make_connection
|
2020-07-29 17:00:20 +03:00
|
|
|
|
2020-01-19 15:37:45 +03:00
|
|
|
log = logging.getLogger()
|
|
|
|
|
2020-01-21 00:19:33 +03:00
|
|
|
class RankRunner(object):
|
|
|
|
""" Returns SQL commands for indexing one rank within the placex table.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, rank):
|
|
|
|
self.rank = rank
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return "rank {}".format(self.rank)
|
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
def sql_count_objects(self):
|
|
|
|
return """SELECT count(*) FROM placex
|
2020-01-21 00:19:33 +03:00
|
|
|
WHERE rank_search = {} and indexed_status > 0
|
2020-07-29 17:00:20 +03:00
|
|
|
""".format(self.rank)
|
2020-01-21 00:19:33 +03:00
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
def sql_get_objects(self):
|
2020-01-21 00:19:33 +03:00
|
|
|
return """SELECT place_id FROM placex
|
|
|
|
WHERE indexed_status > 0 and rank_search = {}
|
|
|
|
ORDER BY geometry_sector""".format(self.rank)
|
|
|
|
|
2020-07-29 18:20:30 +03:00
|
|
|
def sql_index_place(self, ids):
|
|
|
|
return "UPDATE placex SET indexed_status = 0 WHERE place_id IN ({})"\
|
|
|
|
.format(','.join((str(i) for i in ids)))
|
2020-01-21 00:19:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
class InterpolationRunner(object):
|
|
|
|
""" Returns SQL commands for indexing the address interpolation table
|
|
|
|
location_property_osmline.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return "interpolation lines (location_property_osmline)"
|
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
def sql_count_objects(self):
|
|
|
|
return """SELECT count(*) FROM location_property_osmline
|
|
|
|
WHERE indexed_status > 0"""
|
2020-01-21 00:19:33 +03:00
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
def sql_get_objects(self):
|
2020-01-21 00:19:33 +03:00
|
|
|
return """SELECT place_id FROM location_property_osmline
|
|
|
|
WHERE indexed_status > 0
|
|
|
|
ORDER BY geometry_sector"""
|
|
|
|
|
2020-07-29 18:20:30 +03:00
|
|
|
def sql_index_place(self, ids):
|
2020-01-21 00:19:33 +03:00
|
|
|
return """UPDATE location_property_osmline
|
2020-07-29 18:20:30 +03:00
|
|
|
SET indexed_status = 0 WHERE place_id IN ({})"""\
|
|
|
|
.format(','.join((str(i) for i in ids)))
|
2020-01-21 00:19:33 +03:00
|
|
|
|
|
|
|
|
2020-01-19 15:37:45 +03:00
|
|
|
class Indexer(object):
|
2020-01-21 00:19:33 +03:00
|
|
|
""" Main indexing routine.
|
|
|
|
"""
|
2020-01-19 15:37:45 +03:00
|
|
|
|
|
|
|
def __init__(self, options):
|
2020-01-21 00:19:33 +03:00
|
|
|
self.minrank = max(0, options.minrank)
|
|
|
|
self.maxrank = min(30, options.maxrank)
|
2020-01-19 15:37:45 +03:00
|
|
|
self.conn = make_connection(options)
|
2020-01-21 00:19:33 +03:00
|
|
|
self.threads = [DBConnection(options) for i in range(options.threads)]
|
2020-01-19 15:37:45 +03:00
|
|
|
|
|
|
|
def run(self):
|
2020-01-21 00:19:33 +03:00
|
|
|
""" Run indexing over the entire database.
|
|
|
|
"""
|
2020-01-21 01:34:56 +03:00
|
|
|
log.warning("Starting indexing rank ({} to {}) using {} threads".format(
|
2020-01-21 00:19:33 +03:00
|
|
|
self.minrank, self.maxrank, len(self.threads)))
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-01-21 00:19:33 +03:00
|
|
|
for rank in range(self.minrank, self.maxrank):
|
2020-01-19 15:37:45 +03:00
|
|
|
self.index(RankRunner(rank))
|
|
|
|
|
2020-01-21 00:19:33 +03:00
|
|
|
if self.maxrank == 30:
|
2020-07-29 18:20:30 +03:00
|
|
|
self.index(InterpolationRunner(), 20)
|
2020-01-22 13:18:21 +03:00
|
|
|
|
2020-07-29 18:20:30 +03:00
|
|
|
self.index(RankRunner(self.maxrank), 20)
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-07-29 18:20:30 +03:00
|
|
|
def index(self, obj, batch=1):
|
2020-01-21 00:19:33 +03:00
|
|
|
""" Index a single rank or table. `obj` describes the SQL to use
|
2020-07-29 18:20:30 +03:00
|
|
|
for indexing. `batch` describes the number of objects that
|
|
|
|
should be processed with a single SQL statement
|
2020-01-21 00:19:33 +03:00
|
|
|
"""
|
2020-01-21 01:34:56 +03:00
|
|
|
log.warning("Starting {}".format(obj.name()))
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute(obj.sql_count_objects())
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
total_tuples = cur.fetchone()[0]
|
|
|
|
log.debug("Total number of rows: {}".format(total_tuples))
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
cur.close()
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-01-19 23:56:37 +03:00
|
|
|
next_thread = self.find_free_thread()
|
2020-07-29 17:00:20 +03:00
|
|
|
progress = ProgressLogger(obj.name(), total_tuples)
|
2020-01-21 01:17:52 +03:00
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
cur = self.conn.cursor(name='places')
|
|
|
|
cur.execute(obj.sql_get_objects())
|
2020-01-21 01:17:52 +03:00
|
|
|
|
2020-07-29 18:20:30 +03:00
|
|
|
while True:
|
|
|
|
places = [p[0] for p in cur.fetchmany(batch)]
|
|
|
|
if len(places) == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
log.debug("Processing places: {}".format(places))
|
2020-07-29 17:00:20 +03:00
|
|
|
thread = next(next_thread)
|
2020-07-29 16:24:57 +03:00
|
|
|
|
2020-07-29 18:20:30 +03:00
|
|
|
thread.perform(obj.sql_index_place(places))
|
|
|
|
progress.add(len(places))
|
2020-01-19 15:37:45 +03:00
|
|
|
|
|
|
|
cur.close()
|
|
|
|
|
2020-01-19 23:56:37 +03:00
|
|
|
for t in self.threads:
|
|
|
|
t.wait()
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-07-29 17:00:20 +03:00
|
|
|
progress.done()
|
2020-01-19 15:37:45 +03:00
|
|
|
|
2020-01-19 23:56:37 +03:00
|
|
|
def find_free_thread(self):
|
2020-01-21 00:19:33 +03:00
|
|
|
""" Generator that returns the next connection that is free for
|
|
|
|
sending a query.
|
|
|
|
"""
|
2020-01-20 02:05:28 +03:00
|
|
|
ready = self.threads
|
2020-02-09 15:57:36 +03:00
|
|
|
command_stat = 0
|
2020-01-19 23:56:37 +03:00
|
|
|
|
|
|
|
while True:
|
2020-01-20 02:05:28 +03:00
|
|
|
for thread in ready:
|
2020-01-19 23:56:37 +03:00
|
|
|
if thread.is_done():
|
2020-02-09 15:57:36 +03:00
|
|
|
command_stat += 1
|
2020-01-19 23:56:37 +03:00
|
|
|
yield thread
|
|
|
|
|
2020-02-09 15:57:36 +03:00
|
|
|
# refresh the connections occasionaly to avoid potential
|
|
|
|
# memory leaks in Postgresql.
|
|
|
|
if command_stat > 100000:
|
|
|
|
for t in self.threads:
|
|
|
|
while not t.is_done():
|
2020-05-15 19:48:01 +03:00
|
|
|
t.wait()
|
2020-02-09 15:57:36 +03:00
|
|
|
t.connect()
|
|
|
|
command_stat = 0
|
|
|
|
ready = self.threads
|
|
|
|
else:
|
|
|
|
ready, _, _ = select.select(self.threads, [], [])
|
2020-01-19 23:56:37 +03:00
|
|
|
|
2020-04-19 18:59:49 +03:00
|
|
|
assert False, "Unreachable code"
|
2020-01-19 15:37:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def nominatim_arg_parser():
|
|
|
|
""" Setup the command-line parser for the tool.
|
|
|
|
"""
|
|
|
|
def h(s):
|
|
|
|
return re.sub("\s\s+" , " ", s)
|
|
|
|
|
2020-01-21 00:19:33 +03:00
|
|
|
p = ArgumentParser(description="Indexing tool for Nominatim.",
|
2020-01-19 15:37:45 +03:00
|
|
|
formatter_class=RawDescriptionHelpFormatter)
|
|
|
|
|
|
|
|
p.add_argument('-d', '--database',
|
|
|
|
dest='dbname', action='store', default='nominatim',
|
|
|
|
help='Name of the PostgreSQL database to connect to.')
|
|
|
|
p.add_argument('-U', '--username',
|
|
|
|
dest='user', action='store',
|
|
|
|
help='PostgreSQL user name.')
|
|
|
|
p.add_argument('-W', '--password',
|
|
|
|
dest='password_prompt', action='store_true',
|
|
|
|
help='Force password prompt.')
|
|
|
|
p.add_argument('-H', '--host',
|
|
|
|
dest='host', action='store',
|
|
|
|
help='PostgreSQL server hostname or socket location.')
|
|
|
|
p.add_argument('-P', '--port',
|
|
|
|
dest='port', action='store',
|
|
|
|
help='PostgreSQL server port')
|
|
|
|
p.add_argument('-r', '--minrank',
|
|
|
|
dest='minrank', type=int, metavar='RANK', default=0,
|
|
|
|
help='Minimum/starting rank.')
|
|
|
|
p.add_argument('-R', '--maxrank',
|
|
|
|
dest='maxrank', type=int, metavar='RANK', default=30,
|
|
|
|
help='Maximum/finishing rank.')
|
|
|
|
p.add_argument('-t', '--threads',
|
|
|
|
dest='threads', type=int, metavar='NUM', default=1,
|
|
|
|
help='Number of threads to create for indexing.')
|
|
|
|
p.add_argument('-v', '--verbose',
|
|
|
|
dest='loglevel', action='count', default=0,
|
|
|
|
help='Increase verbosity')
|
|
|
|
|
|
|
|
return p
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(stream=sys.stderr, format='%(levelname)s: %(message)s')
|
|
|
|
|
|
|
|
options = nominatim_arg_parser().parse_args(sys.argv[1:])
|
|
|
|
|
|
|
|
log.setLevel(max(3 - options.loglevel, 0) * 10)
|
|
|
|
|
|
|
|
options.password = None
|
|
|
|
if options.password_prompt:
|
|
|
|
password = getpass.getpass("Database password: ")
|
|
|
|
options.password = password
|
|
|
|
|
|
|
|
Indexer(options).run()
|