Skip to content

Commit

Permalink
Use cached internal connection cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git committed Feb 27, 2023
1 parent 519eea3 commit de27b18
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ def password(self):
# 'password': LAYMAN_PG_PASSWORD,
# }
PG_CONN = {}
PG_URI_STR = str()
18 changes: 11 additions & 7 deletions src/db/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask import g

import crs as crs_def
from . import PG_CONN
from . import PG_CONN, PG_URI_STR
from .error import Error

logger = logging.getLogger(__name__)
Expand All @@ -24,12 +24,16 @@ def create_connection_cursor(db_uri_str=None, encapsulate_exception=True):
return connection, cursor


def get_connection_cursor():
key = FLASK_CONN_CUR_KEY
if key not in g:
conn_cur = create_connection_cursor()
g.setdefault(key, conn_cur)
return g.get(key)
def get_connection_cursor(db_uri_str=None, encapsulate_exception=True):
if db_uri_str is None or db_uri_str == PG_URI_STR:
key = FLASK_CONN_CUR_KEY
if key not in g:
conn_cur = create_connection_cursor(encapsulate_exception=encapsulate_exception)
g.setdefault(key, conn_cur)
result = g.get(key)
else:
result = create_connection_cursor(db_uri_str=db_uri_str, encapsulate_exception=encapsulate_exception)
return result


def run_query(query, data=None, conn_cur=None, encapsulate_exception=True, log_query=False):
Expand Down
2 changes: 1 addition & 1 deletion src/layman/common/prime_db_schema/schema_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def ensure_schema(db_schema):
db_util.run_statement(model.CREATE_SCHEMA_SQL)
db_util.run_statement(model.setup_codelists_data())
except BaseException as exc:
db_util.run_statement(model.DROP_SCHEMA_SQL, conn_cur=db_util.create_connection_cursor())
db_util.run_statement(model.DROP_SCHEMA_SQL, conn_cur=db_util.get_connection_cursor())
raise exc
else:
logger.info(f"Layman DB schema already exists, schema_name={db_schema}")
Expand Down
2 changes: 1 addition & 1 deletion src/layman/geoserver_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def ensure_attributes_in_db(attributes_by_db):
(schema, table, attr): (workspace, layer, attr)
for workspace, layer, attr, schema, table in attr_tuples
}
conn_cur = db_util.create_connection_cursor(db_uri_str=db_uri_str)
conn_cur = db_util.get_connection_cursor(db_uri_str=db_uri_str)
db_attr_tuples = list(db_layman_attr_mapping.keys())
created_db_attr_tuples = db.ensure_attributes(db_attr_tuples, conn_cur)
all_created_attr_tuples.update({db_layman_attr_mapping[a] for a in created_db_attr_tuples})
Expand Down
2 changes: 1 addition & 1 deletion src/layman/layer/db/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_layer_info(workspace, layername,):
conn_cur = db_util.get_connection_cursor()
else:
try:
conn_cur = db_util.create_connection_cursor(db_uri_str=table_uri.db_uri_str,)
conn_cur = db_util.get_connection_cursor(db_uri_str=table_uri.db_uri_str,)
except BaseException:
result['db'] = {
'schema': table_uri.schema,
Expand Down
2 changes: 1 addition & 1 deletion src/layman/layer/micka/csw.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def get_template_path_and_values(workspace, layername, http_method):
if geodata_type == settings.GEODATA_TYPE_VECTOR:
table_uri = publ_info['_table_uri']
table_name = table_uri.table
conn_cur = db_util.create_connection_cursor(db_uri_str=table_uri.db_uri_str)
conn_cur = db_util.get_connection_cursor(db_uri_str=table_uri.db_uri_str)
try:
languages = db.get_text_languages(table_uri.schema, table_name, table_uri.primary_key_column,
conn_cur=conn_cur)
Expand Down
2 changes: 1 addition & 1 deletion src/layman/layer/prime_db_schema/file_data_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def patch_after_feature_change(
assert geodata_type == settings.GEODATA_TYPE_VECTOR

table_uri = info['_table_uri']
conn_cur = db_util.create_connection_cursor(db_uri_str=table_uri.db_uri_str)
conn_cur = db_util.get_connection_cursor(db_uri_str=table_uri.db_uri_str)
bbox = db_get_bbox(table_uri.schema, table_uri.table, conn_cur=conn_cur, column=table_uri.geo_column)

if self.is_aborted():
Expand Down
2 changes: 1 addition & 1 deletion src/layman/layer/prime_db_schema/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def refresh_file_data(
# because for compressed files sent with chunks file_type would be UNKNOWN and table_uri not set
publ_info = layman_util.get_publication_info(username, LAYER_TYPE, layername, context={'keys': ['table_uri']})
table_uri = publ_info['_table_uri']
conn_cur = db_util.create_connection_cursor(db_uri_str=table_uri.db_uri_str)
conn_cur = db_util.get_connection_cursor(db_uri_str=table_uri.db_uri_str)
bbox = db_get_bbox(table_uri.schema, table_uri.table, conn_cur=conn_cur, column=table_uri.geo_column)
crs = get_table_crs(table_uri.schema, table_uri.table, conn_cur=conn_cur, column=table_uri.geo_column, use_internal_srid=True)
elif geodata_type == settings.GEODATA_TYPE_RASTER:
Expand Down
2 changes: 1 addition & 1 deletion src/layman/layer/qgis/wms.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def save_qgs_file(workspace, layer):
layer_bbox = layer_bbox if not bbox_util.is_empty(layer_bbox) else crs_def.CRSDefinitions[crs].default_bbox
qml = util.get_original_style_xml(workspace, layer)
qml_geometry = util.get_qml_geometry_from_qml(qml)
conn_cur = db_util.create_connection_cursor(db_uri_str=table_uri.db_uri_str)
conn_cur = db_util.get_connection_cursor(db_uri_str=table_uri.db_uri_str)
db_types = db.get_geometry_types(db_schema, table_name, conn_cur=conn_cur)
db_cols = [
col for col in db.get_all_column_infos(db_schema, table_name, conn_cur=conn_cur, omit_geometry_columns=True)
Expand Down
2 changes: 1 addition & 1 deletion src/layman/layer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def parse_and_validate_external_table_uri_str(external_table_uri_str):
})

try:
conn_cur = db_util.create_connection_cursor(db_uri_str, encapsulate_exception=False)
conn_cur = db_util.get_connection_cursor(db_uri_str, encapsulate_exception=False)
except psycopg2.OperationalError as exc:
raise LaymanError(2, {
'parameter': 'external_table_uri',
Expand Down
1 change: 1 addition & 0 deletions src/layman_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class EnumOriginalDataSource(Enum):
}
db.PG_CONN = PG_CONN
PG_URI_STR = f"postgresql://{LAYMAN_PG_USER}:{LAYMAN_PG_PASSWORD}@{LAYMAN_PG_HOST}:{LAYMAN_PG_PORT}/{LAYMAN_PG_DBNAME}"
db.PG_URI_STR = PG_URI_STR

GEOSERVER_ADMIN_USER = 'admin'
GEOSERVER_ADMIN_PASSWORD = os.getenv('GEOSERVER_ADMIN_PASSWORD', None)
Expand Down
12 changes: 6 additions & 6 deletions test_tools/external_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def ensure_db():
with app.app_context():
db_util.run_statement(statement)

conn_cur = db_util.create_connection_cursor(URI_STR)
conn_cur = db_util.get_connection_cursor(URI_STR)
statement = f"""
CREATE USER {READ_ONLY_USER} WITH PASSWORD '{READ_ONLY_PASSWORD}';
GRANT CONNECT ON DATABASE {EXTERNAL_DB_NAME} TO {READ_ONLY_USER};
Expand All @@ -36,7 +36,7 @@ def ensure_db():


def ensure_schema(schema):
conn_cur = db_util.create_connection_cursor(URI_STR)
conn_cur = db_util.get_connection_cursor(URI_STR)
statement = sql.SQL(f'CREATE SCHEMA IF NOT EXISTS {{schema}} AUTHORIZATION {settings.LAYMAN_PG_USER}').format(
schema=sql.Identifier(schema),
)
Expand Down Expand Up @@ -71,7 +71,7 @@ def ensure_table(schema, name, geo_column, *, primary_key_columns=None, other_co
table=sql.Identifier(schema, name),
columns=sql.SQL(',').join(columns),
)
conn_cur = db_util.create_connection_cursor(URI_STR)
conn_cur = db_util.get_connection_cursor(URI_STR)
db_util.run_statement(statement, conn_cur=conn_cur, log_query=True)


Expand Down Expand Up @@ -111,15 +111,15 @@ def import_table(input_file_path, *, table=None, schema='public', geo_column=set
assert return_code == 0 and not stdout and not stderr, f"return_code={return_code}, stdout={stdout}, stderr={stderr}"

if primary_key_column is None:
conn_cur = db_util.create_connection_cursor(URI_STR)
conn_cur = db_util.get_connection_cursor(URI_STR)
statement = sql.SQL("alter table {table} drop column {primary_key}").format(
table=sql.Identifier(schema, table),
primary_key=sql.Identifier(primary_key_to_later_drop),
)
db_util.run_statement(statement, conn_cur=conn_cur)

if additional_geo_column:
conn_cur = db_util.create_connection_cursor(URI_STR)
conn_cur = db_util.get_connection_cursor(URI_STR)
statement = sql.SQL("alter table {table} add column {geo_column_2} GEOMETRY").format(
table=sql.Identifier(schema, table),
geo_column_2=sql.Identifier(additional_geo_column),
Expand All @@ -141,5 +141,5 @@ def drop_table(schema, name, *, if_exists=False):
statement = sql.SQL(f'drop table {if_exists_str} {{table}}').format(
table=sql.Identifier(schema, name)
)
conn_cur = db_util.create_connection_cursor(URI_STR)
conn_cur = db_util.get_connection_cursor(URI_STR)
db_util.run_statement(statement, conn_cur=conn_cur)
2 changes: 1 addition & 1 deletion tests/asserts/final/publication/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def point_coordinates(workspace, publ_type, name, *, point_id, crs, exp_coordina
)
with app.app_context():
to_srid = db_util.get_internal_srid(crs)
coordinates = db_util.run_query(query, (to_srid, point_id), conn_cur=db_util.create_connection_cursor(table_uri.db_uri_str))
coordinates = db_util.run_query(query, (to_srid, point_id), conn_cur=db_util.get_connection_cursor(table_uri.db_uri_str))
assert len(coordinates) == 1, coordinates
coordinates = coordinates[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def test_layer(self, layer: Publication, rest_method, rest_args, params):
'primary_key_column': primary_key_column,
'additional_geo_column': params['additional_geo_column'],
})
conn_cur = db_util.create_connection_cursor(external_db.URI_STR)
conn_cur = db_util.get_connection_cursor(external_db.URI_STR)
query = f'''select type from geometry_columns where f_table_schema = %s and f_table_name = %s and f_geometry_column = %s'''
result = db_util.run_query(query, (schema, table, geo_column), conn_cur=conn_cur)
assert result[0][0] == params['exp_geometry_type']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_new_attribute(self, layer: Publication, rest_args, params, parametrizat
with app.app_context():
table_uri = get_publication_info(workspace, process_client.LAYER_TYPE, layer_name,
context={'keys': ['table_uri']})['_table_uri']
conn_cur = conn_cur or db_util.create_connection_cursor(table_uri.db_uri_str)
conn_cur = conn_cur or db_util.get_connection_cursor(table_uri.db_uri_str)
table_uris[layer_name] = table_uri
old_db_attributes[layer_name] = db.get_all_table_column_names(table_uri.schema, table_uri.table,
conn_cur=conn_cur)
Expand Down

0 comments on commit de27b18

Please sign in to comment.