2 Functions for setting up and importing a new Nominatim database.
9 from ..db.connection import connect
10 from ..db.async_connection import DBConnection
11 from ..db.sql_preprocessor import SQLPreprocessor
13 # pylint: disable=R0912
14 # pylint: disable=R0914,R0915,W0702
16 LOG = logging.getLogger()
19 def add_tiger_data(dsn, data_dir, threads, config, sqllib_dir):
20 """ Import tiger data from directory or tar file
22 # Handling directory or tarball file.
24 if data_dir.endswith('.tar.gz'):
26 tar = tarfile.open(data_dir)
27 sql_files = [i for i in tar.getmembers() if i.name.endswith('.sql')]
28 LOG.warning("Found %d SQL files in tarfile with path %s", len(sql_files), data_dir)
30 LOG.warning("Tiger data import selected but no files in tarfile's path %s", data_dir)
33 files = os.listdir(data_dir)
34 sql_files = [i for i in files if i.endswith('.sql')]
35 LOG.warning("Found %d SQL files in path %s", len(sql_files), data_dir)
37 LOG.warning("Tiger data import selected but no files found in path %s", data_dir)
40 with connect(dsn) as conn:
41 sql = SQLPreprocessor(conn, config, sqllib_dir)
42 sql.run_sql_file(conn, 'tiger_import_start.sql')
44 # Reading sql_files and then for each file line handling
45 # sql_query in <threads - 1> chunks.
46 sel = selectors.DefaultSelector()
47 place_threads = max(1, threads - 1)
48 for sql_file in sql_files:
50 file_path = os.path.join(data_dir, sql_file)
51 file = open(file_path)
53 file = tar.extractfile(sql_file)
56 total_used_threads = place_threads
60 for imod in range(place_threads):
61 conn = DBConnection(dsn)
64 sql_query = file.readline()
69 total_used_threads = imod
72 conn.perform(sql_query)
73 sel.register(conn, selectors.EVENT_READ, conn)
76 print('. ', end='', flush=True)
79 todo = min(place_threads, total_used_threads)
81 for key, _ in sel.select(1):
94 LOG.warning("Creating indexes on Tiger data")
95 with connect(dsn) as conn:
96 sql = SQLPreprocessor(conn, config, sqllib_dir)
97 sql.run_sql_file(conn, 'tiger_import_finish.sql')