| 1 |
""" |
| 2 |
Helpers for dealing with obscore-published tables and its view. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import contextlib |
| 12 |
|
| 13 |
from gavo import base |
| 14 |
|
| 15 |
|
| 16 |
def createObscoreView(connection): |
| 17 |
"""create the (potentially materialised) obscore view. |
| 18 |
|
| 19 |
This is mainly called from stream in obscore.rd (see the |
| 20 |
create-obscore-view stream there). You can call it programatically |
| 21 |
if the obscore view is not materialised. Don't do it if it is, because |
| 22 |
this is probably an expensive operation, and you'll need to create |
| 23 |
indexes and such manually if you call this function. |
| 24 |
""" |
| 25 |
try: |
| 26 |
from gavo import rsc |
| 27 |
ocTable = rsc.TableForDef( |
| 28 |
base.resolveCrossId("//obscore#_obscoresources"), |
| 29 |
connection=connection) |
| 30 |
parts = ["(%s)"%row["sqlFragment"] |
| 31 |
for row in ocTable.iterQuery(ocTable.tableDef, "")] |
| 32 |
if parts: |
| 33 |
ocTable.dropTable("ivoa.obscore") |
| 34 |
|
| 35 |
if base.getConfig("ivoa", "materialiseObscore"): |
| 36 |
creation = "CREATE TABLE " |
| 37 |
else: |
| 38 |
creation = "CREATE VIEW" |
| 39 |
|
| 40 |
connection.execute( |
| 41 |
f"{creation} ivoa.ObsCore AS (%s)"%( |
| 42 |
" UNION ALL ".join(parts))) |
| 43 |
rsc.TableForDef( |
| 44 |
base.resolveCrossId("//obscore#ObsCore"), |
| 45 |
connection=connection).updateMeta() |
| 46 |
except: |
| 47 |
base.ui.notifyError( |
| 48 |
"-------- Try dachs imp //obscore recover to fix this -------") |
| 49 |
raise |
| 50 |
|
| 51 |
|
| 52 |
@contextlib.contextmanager |
| 53 |
def suspendFromObscore(rd, connection): |
| 54 |
"""a context manager to remove everything within rd from obscore in |
| 55 |
the controlled block. |
| 56 |
|
| 57 |
This is a good idea when doing larger operations in the RD, as a |
| 58 |
singled locked table (e.g., while clustering) in ivoa.obscore will lock the |
| 59 |
whole thing. |
| 60 |
|
| 61 |
This will commit the connection, once before entering the controlled |
| 62 |
block, and once after, if there actually are obscore-published tables |
| 63 |
in RD. |
| 64 |
|
| 65 |
For a materialised obscore clause, this is a no-op. |
| 66 |
""" |
| 67 |
if base.getConfig("ivoa", "materialiseObscore"): |
| 68 |
updateObscore = lambda _: None |
| 69 |
else: |
| 70 |
updateObscore = createObscoreView |
| 71 |
|
| 72 |
prevClauses = list(connection.queryToDicts( |
| 73 |
"SELECT tablename, sqlfragment, sourcerd FROM ivoa._obscoresources" |
| 74 |
" WHERE sourceRD=%(sourceRD)s", |
| 75 |
{"sourceRD": rd.sourceId})) |
| 76 |
|
| 77 |
if not prevClauses: |
| 78 |
yield |
| 79 |
|
| 80 |
else: |
| 81 |
connection.execute("DELETE FROM ivoa._obscoresources" |
| 82 |
" WHERE sourceRD=%(sourceRD)s", |
| 83 |
{"sourceRD": rd.sourceId}) |
| 84 |
updateObscore(connection) |
| 85 |
connection.commit() |
| 86 |
|
| 87 |
try: |
| 88 |
yield |
| 89 |
except: |
| 90 |
connection.rollback() |
| 91 |
raise |
| 92 |
|
| 93 |
finally: |
| 94 |
with connection.cursor() as c: |
| 95 |
c.executemany("INSERT INTO ivoa._obscoresources" |
| 96 |
" (tablename, sqlfragment, sourcerd)" |
| 97 |
" VALUES (%(tablename)s, %(sqlfragment)s, %(sourcerd)s)", |
| 98 |
prevClauses) |
| 99 |
updateObscore(connection) |
| 100 |
connection.commit() |
| 101 |
|
| 102 |
|
| 103 |
def restoreObscore(connection): |
| 104 |
"""sees if this system should have an obscore table and re-creates |
| 105 |
it if it's missing. |
| 106 |
""" |
| 107 |
q = base.UnmanagedQuerier(connection) |
| 108 |
if q.getTableType("ivoa._obscoresources") is not None: |
| 109 |
n = list(q.connection.query( |
| 110 |
"SELECT count(*) from ivoa._obscoresources"))[0][0] |
| 111 |
if n>1: # ivoa.emptyobscore doesn't count |
| 112 |
createObscoreView(connection) |
| 113 |
|