2 Functions for setting up and importing a new Nominatim database.
10 from ..db.connection import connect
11 from ..db.async_connection import DBConnection
12 from ..db.sql_preprocessor import SQLPreprocessor
14 LOG = logging.getLogger()
17 def add_tiger_data(dsn, data_dir, threads, config, sqllib_dir):
18 """ Import tiger data from directory or tar file
20 # Handling directory or tarball file.
22 if(data_dir.endswith('.tar.gz')):
24 tar = tarfile.open(data_dir)
25 sql_files = [i for i in tar.getmembers() if i.name.endswith('.sql')]
26 LOG.warning(f'Found {len(sql_files)} SQL files in tarfile with path {data_dir}')
27 if(not len(sql_files)):
28 LOG.warning(f'Tiger data import selected but no files found in tarfile with path {data_dir}')
31 files = os.listdir(data_dir)
32 sql_files = [i for i in files if i.endswith('.sql')]
33 LOG.warning(f'Found {len(sql_files)} SQL files in path {data_dir}')
34 if(not len(sql_files)):
35 LOG.warning(f'Tiger data import selected but no files found in path {data_dir}')
38 with connect(dsn) as conn:
39 sql = SQLPreprocessor(conn, config, sqllib_dir)
40 sql.run_sql_file(conn, 'tiger_import_start.sql')
42 # Reading sql_files and then for each file line handling
43 # sql_query in <threads - 1> chunks.
44 sel = selectors.DefaultSelector()
45 place_threads = max(1, threads - 1)
46 for sql_file in sql_files:
48 file_path = os.path.join(data_dir, sql_file)
49 file = open(file_path)
51 file = tar.extractfile(sql_file)
54 total_used_threads = place_threads
58 for imod in range(place_threads):
59 conn = DBConnection(dsn)
62 sql_query = file.readline()
67 total_used_threads = imod
70 conn.perform(sql_query)
71 sel.register(conn, selectors.EVENT_READ, conn)
74 print('. ', end='', flush=True)
77 todo = min(place_threads,total_used_threads)
79 for key, _ in sel.select(1):
92 LOG.warning("Creating indexes on Tiger data")
93 with connect(dsn) as conn:
94 sql = SQLPreprocessor(conn, config, sqllib_dir)
95 sql.run_sql_file(conn, 'tiger_import_finish.sql')