1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of classes for API access via libraries.
10 from typing import Mapping, Optional, Any, AsyncIterator
13 from pathlib import Path
15 import sqlalchemy as sa
16 import sqlalchemy.ext.asyncio as sa_asyncio
19 from nominatim.db.sqlalchemy_schema import SearchTables
20 from nominatim.config import Configuration
21 from nominatim.api.status import get_status, StatusResult
22 from nominatim.api.connection import SearchConnection
24 class NominatimAPIAsync:
25 """ API loader asynchornous version.
27 def __init__(self, project_dir: Path,
28 environ: Optional[Mapping[str, str]] = None) -> None:
29 self.config = Configuration(project_dir, environ)
30 self.server_version = 0
32 self._engine_lock = asyncio.Lock()
33 self._engine: Optional[sa_asyncio.AsyncEngine] = None
34 self._tables: Optional[SearchTables] = None
37 async def setup_database(self) -> None:
38 """ Set up the engine and connection parameters.
40 This function will be implicitly called when the database is
41 accessed for the first time. You may also call it explicitly to
42 avoid that the first call is delayed by the setup.
44 async with self._engine_lock:
48 dsn = self.config.get_database_params()
50 dburl = sa.engine.URL.create(
52 database=dsn.get('dbname'),
53 username=dsn.get('user'), password=dsn.get('password'),
54 host=dsn.get('host'), port=int(dsn['port']) if 'port' in dsn else None,
55 query={k: v for k, v in dsn.items()
56 if k not in ('user', 'password', 'dbname', 'host', 'port')})
57 engine = sa_asyncio.create_async_engine(
59 connect_args={'server_settings': {
60 'DateStyle': 'sql,european',
61 'max_parallel_workers_per_gather': '0'
65 async with engine.begin() as conn:
66 result = await conn.scalar(sa.text('SHOW server_version_num'))
67 self.server_version = int(result)
68 except asyncpg.PostgresError:
69 self.server_version = 0
71 if self.server_version >= 110000:
72 @sa.event.listens_for(engine.sync_engine, "connect") # type: ignore[misc]
73 def _on_connect(dbapi_con: Any, _: Any) -> None:
74 cursor = dbapi_con.cursor()
75 cursor.execute("SET jit_above_cost TO '-1'")
76 # Make sure that all connections get the new settings
79 self._tables = SearchTables(sa.MetaData(), engine.name) # pylint: disable=no-member
83 async def close(self) -> None:
84 """ Close all active connections to the database. The NominatimAPIAsync
85 object remains usable after closing. If a new API functions is
86 called, new connections are created.
88 if self._engine is not None:
89 await self._engine.dispose()
92 @contextlib.asynccontextmanager
93 async def begin(self) -> AsyncIterator[SearchConnection]:
94 """ Create a new connection with automatic transaction handling.
96 This function may be used to get low-level access to the database.
97 Refer to the documentation of SQLAlchemy for details how to use
98 the connection object.
100 if self._engine is None:
101 await self.setup_database()
103 assert self._engine is not None
104 assert self._tables is not None
106 async with self._engine.begin() as conn:
107 yield SearchConnection(conn, self._tables)
110 async def status(self) -> StatusResult:
111 """ Return the status of the database.
114 async with self.begin() as conn:
115 status = await get_status(conn)
116 except asyncpg.PostgresError:
117 return StatusResult(700, 'Database connection failed')
123 """ API loader, synchronous version.
126 def __init__(self, project_dir: Path,
127 environ: Optional[Mapping[str, str]] = None) -> None:
128 self._loop = asyncio.new_event_loop()
129 self._async_api = NominatimAPIAsync(project_dir, environ)
132 def close(self) -> None:
133 """ Close all active connections to the database. The NominatimAPIAsync
134 object remains usable after closing. If a new API functions is
135 called, new connections are created.
137 self._loop.run_until_complete(self._async_api.close())
141 def status(self) -> StatusResult:
142 """ Return the status of the database.
144 return self._loop.run_until_complete(self._async_api.status())