# SPDX-License-Identifier: MIT
# Copyright (c) 2001-{{ COPYRIGHT_END_YEAR }} JCMwave GmbH
"""Semi-public advanced operations exposed through ``Resultbag.management``."""
from __future__ import annotations
from collections.abc import Callable, Mapping, Sequence
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .internal.resultbag.store import _ResultbagStore
[docs]
class ResultbagManagement:
"""Provide advanced Resultbag maintenance and solve operations."""
def __init__(self, store: _ResultbagStore) -> None:
self._store: _ResultbagStore = store
[docs]
def add(
self,
*,
keys: Mapping[str, object],
result: object,
log: object | None = None,
jcmt_pattern: str | None = None,
) -> None:
"""Add or replace one Resultbag entry.
Parameters
----------
keys : Mapping[str, object]
project keys that identify the Resultbag entry.
result : object
Versioned raw result dictionary to store. Other pickleable values
remain available through this low-level management surface. Older
result objects are converted before persistence.
log : object | None, optional
Log to store with the result.
jcmt_pattern : str | None, optional
JCMT pattern that identifies the result. `None` identifies the
unsuffixed result.
Returns
-------
None
This method has no return value. The method adds or replaces the
Resultbag entry.
"""
self._store.add(
keys=keys,
result=result,
log=log,
jcmt_pattern=jcmt_pattern,
)
[docs]
def add_attempt_log(
self,
*,
keys: Mapping[str, object],
attempt_index: int,
payload: object,
jcmt_pattern: str | None = None,
) -> None:
"""Store the log from one solve attempt.
Parameters
----------
keys : Mapping[str, object]
project keys that identify the Resultbag entry.
attempt_index : int
Zero-based attempt index.
payload : object
Attempt log to store.
jcmt_pattern : str | None, optional
JCMT pattern that identifies the result. `None` identifies the
unsuffixed result.
Returns
-------
None
This method has no return value. The method stores the attempt log.
Raises
------
InvalidConfigurationError
If `attempt_index` is negative.
"""
self._store.add_attempt_log(
keys=keys,
attempt_index=attempt_index,
payload=payload,
jcmt_pattern=jcmt_pattern,
)
[docs]
def check_result(
self,
keys: Mapping[str, object],
*,
jcmt_pattern: str | None = None,
) -> bool:
"""Check whether one Resultbag entry exists.
Parameters
----------
keys : Mapping[str, object]
project keys that identify the Resultbag entry.
jcmt_pattern : str | None, optional
JCMT pattern that identifies the result. `None` identifies the
unsuffixed result.
Returns
-------
bool
`True` if the exact Resultbag entry exists. Otherwise, `False`.
"""
return self._store._check_result(keys, jcmt_pattern=jcmt_pattern)
[docs]
def check_source_files(self, files: Sequence[str | Path]) -> bool:
"""Check whether source files are unchanged.
Parameters
----------
files : Sequence[str | Path]
Source files to compare with the stored file information.
Returns
-------
bool
`True` if the files match or source-file checks are ignored.
Otherwise, `False`.
"""
return self._store.check_source_files(files)
[docs]
def clear_attempt_logs(
self,
keys: Mapping[str, object],
*,
jcmt_pattern: str | None = None,
) -> None:
"""Remove the solve-attempt logs for one Resultbag entry.
Parameters
----------
keys : Mapping[str, object]
project keys that identify the Resultbag entry.
jcmt_pattern : str | None, optional
JCMT pattern that identifies the result. `None` identifies the
unsuffixed result.
Returns
-------
None
This method has no return value. The method removes all matching
attempt logs.
"""
self._store.clear_attempt_logs(keys, jcmt_pattern=jcmt_pattern)
[docs]
def get_attempt_logs(
self,
keys: Mapping[str, object],
*,
jcmt_pattern: str | None = None,
) -> tuple[object, ...]:
"""Return the stored solve-attempt logs for one Resultbag entry.
Parameters
----------
keys : Mapping[str, object]
project keys that identify the Resultbag entry.
jcmt_pattern : str | None, optional
JCMT pattern that identifies the result. `None` identifies the
unsuffixed result.
Returns
-------
tuple[object, ...]
Attempt logs in order of their `attempt_index` values.
Notes
-----
Only load attempt logs from a Resultbag file that you trust. This
method uses Python pickle to load stored objects.
"""
return self._store.get_attempt_logs(keys, jcmt_pattern=jcmt_pattern)
[docs]
def get_result(
self,
keys: Mapping[str, object],
*,
jcmt_pattern: str | None = None,
) -> object:
"""Return the stored result for one set of project keys.
Parameters
----------
keys : Mapping[str, object]
project keys that identify the Resultbag entry.
jcmt_pattern : str | None, optional
JCMT pattern that identifies the result. `None` identifies the
unsuffixed result.
Returns
-------
object
Stored raw result dictionary or other management data.
Raises
------
InvalidConfigurationError
If the entry does not exist.
Notes
-----
Only load results from a Resultbag file that you trust. This method
uses serialized storage data to load the raw result data.
"""
return self._store._get_result(keys, jcmt_pattern=jcmt_pattern)
[docs]
def get_log(
self,
keys: Mapping[str, object],
*,
jcmt_pattern: str | None = None,
) -> object:
"""Return the stored log for one set of project keys.
Parameters
----------
keys : Mapping[str, object]
project keys that identify the Resultbag entry.
jcmt_pattern : str | None, optional
JCMT pattern that identifies the result. `None` identifies the
unsuffixed result.
Returns
-------
object
Stored log.
Raises
------
InvalidConfigurationError
If the entry does not exist.
Notes
-----
Only load logs from a Resultbag file that you trust. This method uses
Python pickle to load the stored object.
"""
return self._store.get_log(keys, jcmt_pattern=jcmt_pattern)
[docs]
def remove(self, filter_function: Callable[[dict[str, object]], bool]) -> None:
"""Remove entries selected by a filter function.
Parameters
----------
filter_function : Callable[[dict[str, object]], bool]
Filter function that receives project keys. The function returns
`True` for each Resultbag entry to remove.
Examples
--------
Remove entries whose `material` project key is `"Si"`:
>>> resultbag.management.remove(
... filter_function=lambda project_keys: project_keys.get("material") == "Si"
... )
Returns
-------
None
This method has no return value. The method removes matching
entries, suffix variants, and attempt logs.
"""
self._store.remove(filter_function)
[docs]
def remove_result(
self,
keys: Mapping[str, object],
*,
jcmt_pattern: str | None = None,
) -> None:
"""Remove one Resultbag entry.
Parameters
----------
keys : Mapping[str, object]
project keys that identify the Resultbag entry.
jcmt_pattern : str | None, optional
JCMT pattern that identifies the result. `None` identifies the
unsuffixed result.
Returns
-------
None
This method has no return value. The method removes the matching
Resultbag entry and its attempt logs.
"""
self._store.remove_result(keys, jcmt_pattern=jcmt_pattern)
[docs]
def reset(self) -> None:
"""Remove all stored entries and attempt logs.
Returns
-------
None
This method has no return value. Resultbag settings remain stored.
"""
self._store.reset()
[docs]
def set_source_files(self, files: Sequence[str | Path]) -> None:
"""Store information about the Resultbag source files.
Parameters
----------
files : Sequence[str | Path]
Source files that determine whether stored results can be reused.
Returns
-------
None
This method has no return value. The method stores each file path
and a value that detects file changes.
Raises
------
InvalidConfigurationError
If a path is missing or is not a regular file.
"""
self._store.set_source_files(files)
[docs]
def ignore_source_files(self, ignore: bool = True) -> None:
"""Enable or disable source-file consistency checks.
Parameters
----------
ignore : bool, optional
If `True`, do not check source files. If `False`, check the source
files before reusing results.
Returns
-------
None
This method has no return value. The method stores the setting.
"""
self._store.ignore_source_files(ignore)
[docs]
def bind_job(
self,
*,
keys: Mapping[str, object],
job_id: int,
jcmt_pattern: str | None,
) -> None:
"""Associate a daemon submission with project keys.
Parameters
----------
keys : Mapping[str, object]
project keys for the daemon submission.
job_id : int
Daemon submission identifier.
jcmt_pattern : str | None
JCMT pattern for the result. `None` identifies the unsuffixed
result.
Returns
-------
None
This method has no return value. The Resultbag stores the
association until the daemon result is available.
"""
self._store._set_job_id(keys, job_id, jcmt_pattern=jcmt_pattern)
[docs]
def store_job_result(self, *, job_id: int, result: object, log: object) -> None:
"""Store the result for an associated daemon submission.
Parameters
----------
job_id : int
Daemon submission identifier previously passed to `bind_job(...)`.
result : object
Result to store.
log : object
Log to store with the result.
Returns
-------
None
This method has no return value. The method stores the result and
removes the daemon submission association.
"""
self._store._add_for_job(
id=job_id,
result=result,
log=log,
)
self.release_job(job_id)
[docs]
def release_job(self, job_id: int) -> None:
"""Release a daemon submission association.
Parameters
----------
job_id : int
Daemon submission identifier to release.
Returns
-------
None
This method has no return value. If the association exists, the
method removes it.
"""
self._store._release(job_id)