]> git.openstreetmap.org Git - nominatim.git/commitdiff
move analyse function into indexinf function
authorSarah Hoffmann <lonvia@denofr.de>
Mon, 19 Apr 2021 15:34:26 +0000 (17:34 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Tue, 20 Apr 2021 12:08:37 +0000 (14:08 +0200)
nominatim/indexer/indexer.py

index fa40334b7851b617f6069c0932c3f8c6b0a310d8..ebc9803870f3748cec0e28fa8a8f3943ec62964c 100644 (file)
@@ -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):