## KernelDB


Content-addressed SPICE kernel database.


Usage

``` python
KernelDB(
    db_path=None,
    read_only=False,
    kernel_roots=None,
    console=None,
)
```


Wraps a DuckDB database and provides scanning, lookup, metakernel tracking, downloading, rewriting and deduplication. Every kernel file is identified by its SHA-256 hash, not by its filename. Several locations (from different missions, or the same mission in different directories) can reference the same hash, which is how duplicates are detected.

The library is quiet. No method prints unless the instance was given a `rich.console.Console` (`KernelDB(console=...)`), and warnings go through the `spice_kernel_db` logger, so a script sees them only if it configures logging. The CLI is this API plus a console and one log handler.

For one operation against the configured database, the module-level functions [check](check.md#spice_kernel_db.check), [get](get.md#spice_kernel_db.get), [scan](scan.md#spice_kernel_db.scan) and the others open the database, do one thing and close it again. Keep a [KernelDB](KernelDB.md#spice_kernel_db.KernelDB) for several operations in a row. An instance is a context manager that closes the connection on exit.


## Parameters


`db_path: str | Path | None = None`  
The DuckDB file to open, created if it does not exist. `None` reads the path from `~/.config/spice-kernel-db/config.toml`. Without a config file it is `~/.local/share/spice-kernel-db/kernels.duckdb`.

`read_only: bool = ``False`  
Open the database for queries only, and skip the schema initialisation. Several read-only connections can be open at once. DuckDB's file lock means none can open while another process has the database open for writing, for example during [get](get.md#spice_kernel_db.get).

`kernel_roots: list[str | Path] | None = None`  
The kernel roots used to guess a mission from a path. `None` reads them from the configuration the first time they are needed. The resolved list is available as the [kernel_roots](kernel_roots.md#spice_kernel_db.kernel_roots) property. Pass it to keep a script independent of the user's configuration.

`console: rich.console.Console | None = None`  
The one place the instance writes to: status tables, notes, progress bars and the download question of [get_metakernel](KernelDB.get_metakernel.md#spice_kernel_db.KernelDB.get_metakernel). Without one nothing is printed, progress bars are off and every question is answered no, so a script passes `yes=True` where it wants a download. The CLI passes its own console.


## Attributes


`db_path: str`  
The expanded path of the open database file.

`read_only: bool`  
Whether the database was opened read-only.

`console: rich.console.Console | None`  
The console given at construction.

`kernel_roots: list[Path]`  
The kernel roots, as expanded, resolved paths. Read from the configuration on first use unless they were passed to the constructor.


## Raises


`LegacyDatabaseFound`  
`db_path` is `None`, there is no config file, and a database sits at the pre-0.19 default `~/.spice_kernels.duckdb` while the new default does not exist. The message names the move command. Before 0.21.0 that database was opened with a `FutureWarning`. Since 0.21.0 it is not, so that an empty database is not started quietly beside it.

`ConfigError`  
`db_path` is `None` and the config file does not parse.

`duckdb.IOException`  
`read_only` is `True` and another process holds the database open for writing.


## See Also

[check()](check.md#spice_kernel_db.check), [get()](get.md#spice_kernel_db.get), [scan()](scan.md#spice_kernel_db.scan), [resolve_metakernel()](resolve_metakernel.md#spice_kernel_db.resolve_metakernel), [metakernels_covering()](metakernels_covering.md#spice_kernel_db.metakernels_covering), [register_metakernel()](register_metakernel.md#spice_kernel_db.register_metakernel)  
The module-level functions that wrap one call each.


## Notes

The schema is created when the database is opened for writing. It has these tables:

`kernels` One row per unique file content: `sha256` (primary key), `filename` (the first name seen), `kernel_type`, `size_bytes` and `superseded_by`. `locations` Where files live on disk: `sha256` and `abs_path` (primary key), `mission`, `source_url` and `scanned_at`. References `kernels`. `kernel_aliases` A view of `locations`: every distinct on-disk basename a content hash appears under. `metakernel_entries` The kernel entries inside each `.tm` file: `mk_path` and `entry_index` (primary key), `raw_entry` and `filename`. `metakernel_registry` The tracked metakernel files: `mk_path` (primary key), `mission`, `source_url`, `filename`, `acquired_at` and `official`. `missions` The configured missions: `name` (primary key), `server_url`, `mk_dir_url`, `dedup` and `added_at`. `kernel_coverage` Cached SPK coverage intervals per body: `sha256`, `body_id` and `interval_index` (primary key), `et_start` and `et_end`.


## Examples

``` python
>>> from spice_kernel_db import KernelDB
>>> with KernelDB(read_only=True) as db:
...     path = db.resolve_metakernel("juice_crema_5_2.tm")
```
