+
+
+ def _setup_tables(self, config: Configuration, reverse_only: bool) -> None:
+ """ Set up the basic database layout: tables, indexes and functions.
+ """
+ from ..tools import database_import, refresh
+
+ with connect(config.get_libpq_dsn()) as conn:
+ LOG.warning('Create functions (1st pass)')
+ refresh.create_functions(conn, config, False, False)
+ LOG.warning('Create tables')
+ database_import.create_tables(conn, config, reverse_only=reverse_only)
+ refresh.load_address_levels_from_config(conn, config)
+ LOG.warning('Create functions (2nd pass)')
+ refresh.create_functions(conn, config, False, False)
+ LOG.warning('Create table triggers')
+ database_import.create_table_triggers(conn, config)
+ LOG.warning('Create partition tables')
+ database_import.create_partition_tables(conn, config)
+ LOG.warning('Create functions (3rd pass)')
+ refresh.create_functions(conn, config, False, False)
+
+
+ def _get_tokenizer(self, continue_at: Optional[str],
+ config: Configuration) -> AbstractTokenizer:
+ """ Set up a new tokenizer or load an already initialised one.
+ """
+ from ..tokenizer import factory as tokenizer_factory
+
+ if continue_at is None or continue_at == 'load-data':
+ # (re)initialise the tokenizer data
+ return tokenizer_factory.create_tokenizer(config)
+
+ # just load the tokenizer
+ return tokenizer_factory.get_tokenizer_for_db(config)
+
+
+ def _create_pending_index(self, conn: Connection, tablespace: str) -> None:
+ """ Add a supporting index for finding places still to be indexed.
+
+ This index is normally created at the end of the import process
+ for later updates. When indexing was partially done, then this
+ index can greatly improve speed going through already indexed data.
+ """
+ if conn.index_exists('idx_placex_pendingsector'):
+ return
+
+ with conn.cursor() as cur:
+ LOG.warning('Creating support index')
+ if tablespace:
+ tablespace = 'TABLESPACE ' + tablespace
+ cur.execute(f"""CREATE INDEX idx_placex_pendingsector
+ ON placex USING BTREE (rank_address,geometry_sector)
+ {tablespace} WHERE indexed_status > 0
+ """)
+ conn.commit()
+
+
+ def _finalize_database(self, dsn: str, offline: bool) -> None:
+ """ Determine the database date and set the status accordingly.
+ """
+ with connect(dsn) as conn:
+ if not offline:
+ try:
+ dbdate = status.compute_database_date(conn)
+ status.set_status(conn, dbdate)
+ LOG.info('Database is at %s.', dbdate)
+ except Exception as exc: # pylint: disable=broad-except
+ LOG.error('Cannot determine date of database: %s', exc)
+
+ properties.set_property(conn, 'database_version', version_str())