Coverage for python / lsst / daf / butler / _standalone_datastore.py: 42%
34 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-18 08:43 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-18 08:43 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28from __future__ import annotations
30__all__ = ("instantiate_standalone_datastore",)
32from typing import Any
34from . import ddl
35from ._butler_config import ButlerConfig
36from .datastore import Datastore
37from .dimensions import DimensionUniverse
38from .registry.bridge.monolithic import MonolithicDatastoreRegistryBridgeManager
39from .registry.databases.sqlite import SqliteDatabase
40from .registry.interfaces import Database, DatastoreRegistryBridgeManager, OpaqueTableStorageManager
41from .registry.opaque import ByNameOpaqueTableStorageManager
44def instantiate_standalone_datastore(
45 butler_config: ButlerConfig,
46 dimensions: DimensionUniverse,
47 filename: str | None = None,
48 OpaqueManagerClass: type[OpaqueTableStorageManager] | None = None,
49 BridgeManagerClass: type[DatastoreRegistryBridgeManager] | None = None,
50) -> tuple[Datastore, Database]:
51 """Initialize a `Datastore` instance without an associated `Registry`.
53 Parameters
54 ----------
55 butler_config : `ButlerConfig`
56 Butler configuration that defines the configuration for the `Datastore`
57 to be initialized.
58 dimensions : `DimensionUniverse`
59 Dimension universe used by the Butler repository backing the
60 `Datastore`.
61 filename : `str`, optional
62 Name for the SQLite database that will back the `Datastore`; defaults
63 to an in-memory database.
64 OpaqueManagerClass : `type`, optional
65 A subclass of `OpaqueTableStorageManager` to use for datastore
66 opaque records. Default is a SQL-backed implementation.
67 BridgeManagerClass : `type`, optional
68 A subclass of `DatastoreRegistryBridgeManager` to use for datastore
69 location records. Default is a SQL-backed implementation.
71 Returns
72 -------
73 datastore_and_database : `tuple` [ `Datastore` , `Database` ]
74 The temporary datastore, and the database instance backing it.
76 Notes
77 -----
78 This allows files to be written and read without access to the Butler
79 database. It is primarily used by `QuantumBackedButler`.
80 """
81 if filename is None:
82 filename = ":memory:"
83 if OpaqueManagerClass is None:
84 OpaqueManagerClass = ByNameOpaqueTableStorageManager
85 if BridgeManagerClass is None:
86 BridgeManagerClass = MonolithicDatastoreRegistryBridgeManager
88 butler_root = butler_config.get("root", butler_config.configDir)
89 db = SqliteDatabase.fromUri(f"sqlite:///{filename}", origin=0)
90 with db.declareStaticTables(create=True) as context:
91 opaque_manager = OpaqueManagerClass.initialize(db, context)
92 bridge_manager = BridgeManagerClass.initialize(
93 db,
94 context,
95 opaque=opaque_manager,
96 # MyPy can tell it's a fake, but we know it shouldn't care.
97 datasets=_DatasetRecordStorageManagerDatastoreConstructionMimic, # type: ignore
98 universe=dimensions,
99 )
101 datastore = Datastore.fromConfig(butler_config, bridge_manager, butler_root)
102 return (datastore, db)
105class _DatasetRecordStorageManagerDatastoreConstructionMimic:
106 """A partial implementation of `DatasetRecordStorageManager` that exists
107 only to allow a `DatastoreRegistryBridgeManager` (and hence a `Datastore`)
108 to be constructed without a full `Registry`.
109 """
111 @classmethod
112 def getIdColumnType(cls) -> type:
113 # Docstring inherited.
114 return ddl.GUID
116 @classmethod
117 def addDatasetForeignKey(
118 cls,
119 tableSpec: ddl.TableSpec,
120 *,
121 name: str = "dataset",
122 constraint: bool = True,
123 onDelete: str | None = None,
124 **kwargs: Any,
125 ) -> ddl.FieldSpec:
126 # Docstring inherited.
127 idFieldSpec = ddl.FieldSpec(f"{name}_id", dtype=ddl.GUID, **kwargs)
128 tableSpec.fields.add(idFieldSpec)
129 return idFieldSpec