pycsamt.forward.maxwell.cache#
Content-addressed cache for canonical Maxwell forward results.
Entries are keyed by MaxwellProblem.problem_hash. Each result archive has
a SHA-256 sidecar, is written by atomic replacement, and is validated against
the requested problem when read. Lock files coordinate independent workers.
Classes
|
Describe one complete cache entry. |
|
Summarize the current on-disk cache state. |
|
Manage a validated, content-addressed result cache. |
Exceptions
Indicate that a cached archive failed integrity validation. |
|
Indicate that a cache-key lock could not be acquired in time. |
- exception pycsamt.forward.maxwell.cache.CacheCorruptionError[source]
Bases:
RuntimeErrorIndicate that a cached archive failed integrity validation.
Examples
>>> isinstance(CacheCorruptionError("bad checksum"), RuntimeError) True
- exception pycsamt.forward.maxwell.cache.CacheLockTimeoutError[source]
Bases:
TimeoutErrorIndicate that a cache-key lock could not be acquired in time.
Examples
>>> isinstance(CacheLockTimeoutError("busy"), TimeoutError) True
- class pycsamt.forward.maxwell.cache.CacheEntry(key, archive_path, size_bytes, modified_time_s)[source]
Bases:
objectDescribe one complete cache entry.
- Parameters:
key (str) – Problem SHA-256 digest.
archive_path (pathlib.Path) – Result archive location.
size_bytes (int) – Combined archive and checksum size.
modified_time_s (float) – Archive modification time as Unix seconds.
Examples
>>> entry = CacheEntry("0" * 64, Path("result.npz"), 10, 1.0) >>> entry.size_bytes 10
- key: str
- archive_path: Path
- size_bytes: int
- modified_time_s: float
- class pycsamt.forward.maxwell.cache.CacheStatistics(entry_count, total_bytes, orphan_count, corrupt_count)[source]
Bases:
objectSummarize the current on-disk cache state.
- Parameters:
entry_count (int) – Counts and storage for normal, incomplete, and quarantined files.
total_bytes (int) – Counts and storage for normal, incomplete, and quarantined files.
orphan_count (int) – Counts and storage for normal, incomplete, and quarantined files.
corrupt_count (int) – Counts and storage for normal, incomplete, and quarantined files.
Examples
>>> CacheStatistics(2, 100, 0, 1).entry_count 2
- entry_count: int
- total_bytes: int
- orphan_count: int
- corrupt_count: int
- class pycsamt.forward.maxwell.cache.MaxwellResultCache(root, *, lock_timeout_s=300.0, poll_interval_s=0.05, stale_lock_s=3600.0, quarantine_corrupt=True)[source]
Bases:
objectManage a validated, content-addressed result cache.
- Parameters:
root (str or pathlib.Path) – Dedicated cache directory. It is created when absent.
lock_timeout_s (float, default=300) – Maximum wait for another worker holding the same problem key.
poll_interval_s (float, default=0.05) – Delay between lock acquisition attempts.
stale_lock_s (float, default=3600) – Age after which an abandoned lock can be removed.
quarantine_corrupt (bool, default=True) – Move corrupt files under
root/corrupt. When false, reads raiseCacheCorruptionErrorand leave the entry untouched.
Examples
>>> from tempfile import TemporaryDirectory >>> with TemporaryDirectory() as directory: ... cache = MaxwellResultCache(directory) ... cache.statistics().entry_count 0
- property root: Path[source]
Return the resolved cache root.
- Returns:
Dedicated cache directory.
- Return type:
Examples
The returned path is always absolute.
- contains(problem)[source]
Return whether a complete entry exists for a problem.
- Parameters:
problem (MaxwellProblem) – Problem whose content hash identifies the entry.
- Returns:
True when both archive and checksum sidecar exist.
- Return type:
Examples
A newly created cache contains no problems.
- get(problem)[source]
Load and validate a cached result.
- Parameters:
problem (MaxwellProblem) – Exact problem expected by the caller.
- Returns:
Valid result, or None when no complete entry exists. Corruption is quarantined and treated as a miss when configured.
- Return type:
ForwardResult or None
- Raises:
CacheCorruptionError – If validation fails and quarantine is disabled.
Examples
Cache misses return None rather than raising
KeyError.
- put(problem, result, *, overwrite=False)[source]
Validate and atomically store one result.
- Parameters:
problem (MaxwellProblem) – Exact simulation input.
result (ForwardResult) – Canonical result matching
problem.overwrite (bool, default=False) – Replace an existing complete entry. Otherwise the validated existing entry is retained.
- Returns:
Metadata for the stored or retained entry.
- Return type:
Examples
Invalid problem/result pairs are rejected before writing an archive.
- get_or_solve(problem, backend)[source]
Return a hit or solve and cache one problem under a key lock.
- Parameters:
problem (MaxwellProblem) – Simulation input and cache identity.
backend (MaxwellBackend) – Conforming backend used only after a cache miss.
- Returns:
Valid cached or newly computed result.
- Return type:
Notes
The second read after locking prevents duplicate concurrent work.
Examples
Backend invocation is skipped whenever a validated hit exists.
- entry(key)[source]
Return filesystem metadata for a complete entry.
- Parameters:
key (str) – Problem SHA-256 digest.
- Returns:
Entry paths, size, and modification time.
- Return type:
- Raises:
KeyError – If archive or checksum sidecar is missing.
Examples
This method does not deserialize the result archive.
- entries()[source]
Return complete entries sorted by problem key.
- Returns:
Stable snapshot of complete cache entries.
- Return type:
tuple of CacheEntry
Examples
An empty cache returns an empty tuple.
- remove(problem)[source]
Remove one problem entry under its key lock.
- Parameters:
problem (MaxwellProblem) – Exact problem identifying the entry.
- Returns:
True when at least one entry file was removed.
- Return type:
Examples
Removing an absent problem is a no-op returning False.
- prune(maximum_bytes)[source]
Remove oldest entries until storage is within a byte budget.
- Parameters:
maximum_bytes (int) – Non-negative archive and checksum budget.
- Returns:
Entries removed, oldest first.
- Return type:
tuple of CacheEntry
Examples
prune(0)removes every complete entry but leaves infrastructure.
- statistics()[source]
Inspect complete, orphaned, and quarantined cache files.
- Returns:
Current cache counts and complete-entry storage.
- Return type:
Examples
Statistics inspect metadata without deserializing archives.