C · Snapshots

Snapshots

#include <ustore/db.h> 1.0.0-rc.1 · d6d40961

A snapshot pins a point-in-time view of the whole database, and reads select it through ustore_snapshot_view, so a read can name a transaction or a snapshot but never both:

ustore_snapshot_t snapshot = NULL;
ustore_snapshot_init(&(ustore_snapshot_init_t){
    .snapshot = &snapshot,
    .database = database,
    .outcome = &outcome,
});

ustore_blobs_read(&(ustore_blobs_read_t){
    .database = database, .outcome = &outcome, .view = ustore_snapshot_view(snapshot), /* ... */});

ustore_snapshot_free(snapshot);

ustore_snapshot_init can export its read_stamp, the stamp the snapshot reads at, which LevelDB, keeping its sequence numbers private, reports as zero. A snapshot lives no longer than the process that took it, and holds its database open until freed, as a transaction does, so ustore_database_free closes the database only once the last of them is freed. ustore_snapshot_save writes one out as a new database of the same engine, holding exactly the collections and names the snapshot records, which that engine’s open then serves, so a save is how a view outlives the process. RocksDB and LevelDB create its path, the ram engine writes its image into the directory path names, and NVMe takes path as the manifest of the new database. Persistent snapshots, recorded in a system collection under UUIDv7 identifiers and kept across restarts until dropped, are planned.

API

Types

ustore_snapshot_tAn opaque handle to a point-in-time state of the whole database, which reads address through ustore_snapshot_view.
db.h:29 ↗
typedef struct ustore_snapshot_handle_t * ustore_snapshot_t;

Short-lived: it pins the versions its reads need and holds its database open until ustore_snapshot_free.

Functions

ustore_snapshot_initPins a point-in-time state of the database until ustore_snapshot_free releases it.
db.h:809 ↗
void ustore_snapshot_init(ustore_snapshot_init_t *arguments);

Parameters

snapshotustore_snapshot_t *Output for the new snapshot, which the caller frees with ustore_snapshot_free; NULL on failure.
databaseustore_database_tAn open database.
executorustore_executor_tExecutor that runs the call, where NULL runs it on the calling thread.
outcomeustore_outcome_t *Output for the outcome, final once its status leaves ustore_status_deferred_k.
completionustore_completion_tWhether the call returns final, the default, or once submitted.
read_stampustore_stamp_t *Optional output for the stamp the snapshot reads at; zeroed on failure.

Statuses

ustore_snapshot_saveWrites a snapshot out as a new database of the same engine, which that engine's open then serves.
db.h:859 ↗
void ustore_snapshot_save(ustore_snapshot_save_t *arguments);

The copy holds exactly the collections and names the snapshot records, whatever changed since. RocksDB and LevelDB create path, while the RAM engine writes its image into the directory path names. Its progress counts the bytes of the keys and values saved, and a save its progress callback cancels removes what it wrote, as a failed one does. The NVMe engine names the SHA-256 of every run in the saved manifest, which the first open of the copy checks before it reads and then drops from every manifest it publishes.

Parameters

snapshotustore_snapshot_tThe snapshot to save, which holds its database open.
executorustore_executor_tExecutor that runs the call, where NULL runs it on the calling thread.
outcomeustore_outcome_t *Output for the outcome, final once its status leaves ustore_status_deferred_k.
completionustore_completion_tWhether the call returns final, the default, or once submitted.
pathustore_bytes_view_tDirectory that receives the saved database, or on NVMe its manifest, <database>.ustore.json, or the raw address of its first slot.
progressustore_progress_callback_tOptional callback reporting the bytes saved.
progress_statevoid *Opaque pointer passed to progress.

Statuses

ustore_snapshot_viewNames a snapshot as the view a read sees, without allocating.
db.h:866 ↗
ustore_view_t ustore_snapshot_view(ustore_snapshot_t snapshot);

Parameters

snapshotinA snapshot, which the view dies with.

Returns

The view, or NULL, the head, for a NULL snapshot.

ustore_snapshot_freeWaits until every deferred call naming the snapshot finishes, then releases it, unpinning the versions only it kept, and frees its memory with every view of it once no cursor reads through it.
db.h:874 ↗
void ustore_snapshot_free(ustore_snapshot_t snapshot);

Parameters

snapshotinoutThe snapshot to be freed; NULL is safe.