CheckpointStore¶
Bases: _ReadableStore
A durable store in one directory: a canonical HDF5 file plus atomically written payload files and SWMR ledgers.
The store owns every checkpoint file name. Its canonical file carries a
commit marker (COMMIT_MARKER_ATTR on the file root): a JSON object
that maps each streamed attribute's canonical name to its committed
length.
With a layout that has a shared attribute, a payload references the objects of that attribute stored in the canonical file, and every read decodes the payload with them.
CheckpointStore ¶
CheckpointStore(directory: str | PathLike, layout: StoreLayout, *, canonical_filename: str | None = None) -> None
Attributes¶
| Name | Type | Value | Doc |
|---|---|---|---|
canonical_path |
pathlib.Path |
read-only property | The canonical file's path. |
layout(inherited) |
StoreLayout |
unset |
consolidate ¶
consolidate(*, encode_cache: dict | None = None) -> tuple[int, ...]
Merge every payload into the canonical file, then remove the merged payloads, the SWMR ledgers and orphaned temp files.
Run it only when no writer is live. An unreadable payload raises
CheckpointReadError and nothing is deleted. Returns the sorted
keys that got at least one attr appended. A shot payload is
decoded with the parent program stored in the canonical file, once
per call.
get¶
Inherited method from BaseCheckpointStore.get().
has_content ¶
has_content() -> bool
Whether the directory exists and holds any entry other than the canonical file's temp file.
That temp file is what a kill during the first snapshot leaves, and the next snapshot overwrites it, so it doesn't count as content.
iter_items¶
Inherited method from BaseCheckpointStore.iter_items().
keys¶
Inherited method from BaseCheckpointStore.keys().
ledger_paths ¶
ledger_paths() -> list[Path]
Existing ledger files, sorted by name, .tmp files excluded.
missing¶
Inherited method from BaseCheckpointStore.missing().
nested ¶
nested(key: int, attr: str = '_program_results', layout: StoreLayout = SHOT_LAYOUT) -> _NestedStore
A read-only store over the ProgramResults held at key in
attr, found through this store's cached key index.
payload_path ¶
payload_path(worker_id: str, first_key: int) -> Path
The payload file of a worker's batch starting at first_key.
payload_paths ¶
payload_paths() -> list[Path]
Existing payload files, sorted by name, .tmp files excluded.
payload_view ¶
payload_view(path: str | PathLike) -> _PayloadView
A read-only store over the one payload file at path.
The file is read as the store reads payloads. Raises ValueError
if path is not one of this store's payload file names.
put_batch ¶
put_batch(entries: Mapping[int, Mapping[str, Any]], *, worker_id: str | None = None, encode_cache: dict | None = None) -> BatchReceipt
Store a batch of entries.
With a worker_id the batch is one atomically written payload
file. Without one it is a canonical append: entries are added to
the canonical file in place and committed by rewriting its marker
last. encode_cache is only for canonical appends and changes only
when the batch commits.
For a layout with a shared attribute, a payload references the
objects of that attribute stored in the canonical file, instead of
embedding them, so the canonical file must exist
(FileNotFoundError otherwise). The process keeps one encode
cache seeded from it across batches, and the payload's IDs start
above the file's next-ID record.
read_attrs ¶
read_attrs(names: Iterable[str], *, decode_cache: Any = None) -> dict[str, Any]
Decode the named non-streamed attributes of the committed canonical file, without decoding any streamed entry.
Raises CheckpointReadError naming the file if the file, marker or
a name is missing or decoding fails, and ValueError for a
streamed attribute. A given decode_cache is re-rooted at the file.
read_config ¶
read_config() -> Serializable
The committed canonical file's object, rebuilt with its stored class and without its streamed attributes.
Raises CheckpointReadError naming the file if the file or marker
is missing or decoding fails. The canonical decode cache is left
alone.
shared_encode_cache ¶
shared_encode_cache() -> dict
A new encode cache seeded from the canonical file's shared attribute, for encoding objects that reference it.
Empty for a layout without a shared attribute. Raises
CheckpointReadError if the attribute can't be read.
worker_id_of ¶
worker_id_of(path: str | PathLike) -> str
The worker ID in a ledger or payload file name.
Raises ValueError on any other name. Worker IDs may contain
underscores, so the match is anchored on the name's suffix.
write_snapshot ¶
write_snapshot(obj: Serializable, *, keep_entries: bool = False, encode_cache: dict | None = None) -> None
Atomically write the canonical file from obj.
Before the rename, empty groups-format streamed attributes are
removed, fixed-size datasets made growable, and the commit marker
and next free cache ID (NEXT_CACHE_ID_ATTR) recorded.
With keep_entries, the existing canonical file must be readable;
its committed streamed entries (not any tail past its marker) are
copied in as stored, replacing obj's, and new cache IDs continue
above its own.
encode_cache (pass an empty dict) receives, once the file is in
place, an object-ID-free entry per object the new file holds, for
canonical appends to reference. A keep_entries rewrite adds none.