From: Sarah Hoffmann Date: Mon, 19 Apr 2021 15:34:26 +0000 (+0200) Subject: move analyse function into indexinf function X-Git-Tag: v4.0.0~106^2~3 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/18705b3f18f61daea3897db11a860997bf4bd014 move analyse function into indexinf function --- diff --git a/nominatim/indexer/indexer.py b/nominatim/indexer/indexer.py index fa40334b..ebc98038 100644 --- a/nominatim/indexer/indexer.py +++ b/nominatim/indexer/indexer.py @@ -13,12 +13,6 @@ from nominatim.db.async_connection import DBConnection LOG = logging.getLogger() -def _analyse_db_if(conn, condition): - if condition: - with conn.cursor() as cur: - cur.execute('ANALYSE') - - class Indexer: """ Main indexing routine. """ @@ -51,26 +45,31 @@ class Indexer: database will be analysed at the appropriate places to ensure that database statistics are updated. """ - conn = psycopg2.connect(self.dsn) - conn.autocommit = True + with psycopg2.connect(self.dsn) as conn: + conn.autocommit = True + + if analyse: + def _analyse(): + with conn.cursor() as cur: + cur.execute('ANALYSE') + else: + def _analyse(): + pass - try: self.index_by_rank(0, 4) - _analyse_db_if(conn, analyse) + _analyse() self.index_boundaries(0, 30) - _analyse_db_if(conn, analyse) + _analyse() self.index_by_rank(5, 25) - _analyse_db_if(conn, analyse) + _analyse() self.index_by_rank(26, 30) - _analyse_db_if(conn, analyse) + _analyse() self.index_postcodes() - _analyse_db_if(conn, analyse) - finally: - conn.close() + _analyse() def index_boundaries(self, minrank, maxrank):