|
| 1 | +# Copyright (c) 2021-2023, Crate.io Inc. |
| 2 | +# Distributed under the terms of the AGPLv3 license, see LICENSE. |
| 3 | +import logging |
| 4 | +import typing as t |
| 5 | +import uuid |
| 6 | + |
| 7 | +import sqlalchemy as sa |
| 8 | +from sqlalchemy import MetaData, Result, Table |
| 9 | +from sqlalchemy.orm import Session |
| 10 | + |
| 11 | +from cratedb_toolkit.exception import TableNotFound |
| 12 | +from cratedb_toolkit.materialized.model import MaterializedView, MaterializedViewSettings |
| 13 | +from cratedb_toolkit.model import TableAddress |
| 14 | +from cratedb_toolkit.util.database import DatabaseAdapter |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class MaterializedViewStore: |
| 20 | + """ |
| 21 | + A wrapper around the materialized view management table. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, settings: MaterializedViewSettings): |
| 25 | + self.settings = settings |
| 26 | + |
| 27 | + logger.info( |
| 28 | + f"Connecting to database {self.settings.database.safe}, " |
| 29 | + f"table {self.settings.materialized_table.fullname}" |
| 30 | + ) |
| 31 | + |
| 32 | + # Set up generic database adapter. |
| 33 | + self.database: DatabaseAdapter = DatabaseAdapter(dburi=self.settings.database.dburi) |
| 34 | + |
| 35 | + # Set up SQLAlchemy Core adapter for materialized view management table. |
| 36 | + metadata = MetaData(schema=self.settings.materialized_table.schema) |
| 37 | + self.table = Table(self.settings.materialized_table.table, metadata, autoload_with=self.database.engine) |
| 38 | + |
| 39 | + def create(self, mview: MaterializedView, ignore: t.Optional[str] = None): |
| 40 | + """ |
| 41 | + Create a new materialized view, and return its identifier. |
| 42 | +
|
| 43 | + TODO: Generalize, see `RetentionPolicyStore`. |
| 44 | + """ |
| 45 | + |
| 46 | + # TODO: Sanity check, whether target table already exists? |
| 47 | + |
| 48 | + ignore = ignore or "" |
| 49 | + |
| 50 | + # Sanity checks. |
| 51 | + if mview.table_schema is None: |
| 52 | + raise ValueError("Table schema needs to be defined") |
| 53 | + if mview.table_name is None: |
| 54 | + raise ValueError("Table name needs to be defined") |
| 55 | + if self.exists(mview): |
| 56 | + if not ignore.startswith("DuplicateKey"): |
| 57 | + raise ValueError(f"Materialized view '{mview.table_schema}.{mview.table_name}' already exists") |
| 58 | + |
| 59 | + table = self.table |
| 60 | + # TODO: Add UUID as converter to CrateDB driver? |
| 61 | + identifier = str(uuid.uuid4()) |
| 62 | + data = mview.to_storage_dict(identifier=identifier) |
| 63 | + insertable = sa.insert(table).values(**data).returning(table.c.id) |
| 64 | + cursor = self.execute(insertable) |
| 65 | + identifier = cursor.one()[0] |
| 66 | + self.synchronize() |
| 67 | + return identifier |
| 68 | + |
| 69 | + def retrieve(self): |
| 70 | + """ |
| 71 | + Retrieve all records from database table. |
| 72 | +
|
| 73 | + TODO: Add filtering capabilities. |
| 74 | + TODO: Generalize, see `RetentionPolicyStore`. |
| 75 | + """ |
| 76 | + |
| 77 | + # Run SELECT statement, and return result. |
| 78 | + selectable = sa.select(self.table) |
| 79 | + records = self.query(selectable) |
| 80 | + return records |
| 81 | + |
| 82 | + def get_by_table(self, table_address: TableAddress) -> MaterializedView: |
| 83 | + """ |
| 84 | + Retrieve effective policies to process, by strategy and tags. |
| 85 | + """ |
| 86 | + table = self.table |
| 87 | + selectable = sa.select(table).where( |
| 88 | + table.c.table_schema == table_address.schema, |
| 89 | + table.c.table_name == table_address.table, |
| 90 | + ) |
| 91 | + logger.info(f"View definition DQL: {selectable}") |
| 92 | + try: |
| 93 | + record = self.query(selectable)[0] |
| 94 | + except IndexError: |
| 95 | + raise KeyError( |
| 96 | + f"Synthetic materialized table definition does not exist: {table_address.schema}.{table_address.table}" |
| 97 | + ) |
| 98 | + mview = MaterializedView.from_record(record) |
| 99 | + return mview |
| 100 | + |
| 101 | + def delete(self, identifier: str) -> int: |
| 102 | + """ |
| 103 | + Delete materialized view by identifier. |
| 104 | +
|
| 105 | + TODO: Generalize, see `RetentionPolicyStore`. |
| 106 | + """ |
| 107 | + table = self.table |
| 108 | + constraint = table.c.id == identifier |
| 109 | + deletable = sa.delete(table).where(constraint) |
| 110 | + result = self.execute(deletable) |
| 111 | + self.synchronize() |
| 112 | + if result.rowcount == 0: |
| 113 | + logger.warning(f"Materialized view not found with id: {identifier}") |
| 114 | + return result.rowcount |
| 115 | + |
| 116 | + def execute(self, statement) -> Result: |
| 117 | + """ |
| 118 | + Execute SQL statement, and return result object. |
| 119 | +
|
| 120 | + TODO: Generalize, see `RetentionPolicyStore`. |
| 121 | + """ |
| 122 | + with Session(self.database.engine) as session: |
| 123 | + result = session.execute(statement) |
| 124 | + session.commit() |
| 125 | + return result |
| 126 | + |
| 127 | + def query(self, statement) -> t.List[t.Dict]: |
| 128 | + """ |
| 129 | + Execute SQL statement, fetch result rows, and return them converted to dictionaries. |
| 130 | +
|
| 131 | + TODO: Generalize, see `RetentionPolicyStore`. |
| 132 | + """ |
| 133 | + cursor = self.execute(statement) |
| 134 | + rows = cursor.mappings().fetchall() |
| 135 | + records = [dict(row.items()) for row in rows] |
| 136 | + return records |
| 137 | + |
| 138 | + def exists(self, mview: MaterializedView): |
| 139 | + """ |
| 140 | + Check if retention policy for specific table already exists. |
| 141 | +
|
| 142 | + TODO: Generalize, see `RetentionPolicyStore`. |
| 143 | + """ |
| 144 | + table = self.table |
| 145 | + selectable = sa.select(table).where( |
| 146 | + table.c.table_schema == mview.table_schema, |
| 147 | + table.c.table_name == mview.table_name, |
| 148 | + ) |
| 149 | + result = self.query(selectable) |
| 150 | + return bool(result) |
| 151 | + |
| 152 | + def synchronize(self): |
| 153 | + """ |
| 154 | + Synchronize data by issuing `REFRESH TABLE` statement. |
| 155 | + """ |
| 156 | + sql = f"REFRESH TABLE {self.settings.materialized_table.fullname};" |
| 157 | + self.database.run_sql(sql) |
0 commit comments