169 lines
7.8 KiB
TypeScript
169 lines
7.8 KiB
TypeScript
/**
|
|
* Backend-facing vocabulary of the storage hub: a backend owns one medium
|
|
* (a file-tree root, a database file) and exposes operation groups over it.
|
|
* This module defines the normative contract text for backend implementers; the shared
|
|
* conformance suite in `tests/contract.ts` checks every rule.
|
|
* @module @deepseek-ai/dsh-storage/src/backend
|
|
*/
|
|
|
|
/** Allowed format for unit and table names: safe as a file name and as a SQL identifier segment without escaping. */
|
|
export const UNIT_NAME_RE = /^[a-z][a-z0-9_]*$/
|
|
|
|
/**
|
|
* One registered backend. A backend owns exactly one medium and shares its
|
|
* lifecycle across all facets; facets are optional members — a backend that
|
|
* cannot serve a data kind simply omits it, and resolution fails loud instead.
|
|
*/
|
|
export interface StorageBackend {
|
|
/** Key-value operations; absent when this backend cannot serve them. */
|
|
readonly kv?: KvFacet
|
|
|
|
/**
|
|
* Drain in-flight writes across all open units and release the medium.
|
|
* Idempotent; concurrent and repeated calls resolve once teardown finishes.
|
|
* @returns resolution after the medium is released.
|
|
*/
|
|
close(): Promise<void>
|
|
}
|
|
|
|
/** The key-value data shape: whole-unit snapshots plus per-record durable writes. */
|
|
export interface KvFacet {
|
|
/**
|
|
* Open one unit, creating it when the medium holds no trace of it yet
|
|
* (materialization may defer to the first write, but {@link KvUnit.loadAll}
|
|
* must immediately serve the empty shape). Backends with a unit-wide stamp
|
|
* reject an existing version outside `descriptor.version` and
|
|
* `compatibleVersions` with `version-mismatch`; document-based per-record
|
|
* layouts read an unaccepted document as absent. A medium that cannot be
|
|
* parsed as this unit rejects with `malformed-medium`. Opening the same unit name twice without closing
|
|
* is a caller bug and rejects.
|
|
* @param descriptor - Static identity and shape of the unit to open.
|
|
* @returns the opened unit.
|
|
*/
|
|
open(descriptor: KvUnitDescriptor): Promise<KvUnit>
|
|
}
|
|
|
|
/** Static identity and shape of one KV unit, projected from its owner's spec. */
|
|
export interface KvUnitDescriptor {
|
|
/** Unit name; must match {@link UNIT_NAME_RE}. Also the file-name / SQL-identifier segment. */
|
|
readonly name: string
|
|
/** Current unit format version; a non-negative integer stamped on initial materialization and subsequent mutations. */
|
|
readonly version: number
|
|
/** Table names; each must match {@link UNIT_NAME_RE}. */
|
|
readonly tables: readonly string[]
|
|
/** Whether this unit carries the global singleton slot. */
|
|
readonly hasGlobal: boolean
|
|
/**
|
|
* Document layout. `single` (the default) keeps the whole unit in one
|
|
* document; `per-record` keeps each record in its own document, so a unit
|
|
* whose records are large or sparse never rewrites the rest on one write,
|
|
* and an unaccepted document reads as absent instead of
|
|
* rejecting the whole unit. Row stores retain unit-wide versioning for
|
|
* either layout request.
|
|
*/
|
|
readonly layout?: 'single' | 'per-record'
|
|
/**
|
|
* Older unit versions whose stored records are also readable under the
|
|
* declaring owner's current schemas (the owner vouches for that —
|
|
* typically by declaring the fields old records lack as optional). Both
|
|
* layouts accept listed versions without restamping ordinary reads. Whole-unit
|
|
* mutations atomically stamp {@link version} with their data; per-record
|
|
* replacements stamp their document. Untouched records keep their saved
|
|
* values, so the current schemas must accept mixed generations.
|
|
* The legacy per-record bootstrap also accepts listed versions.
|
|
*/
|
|
readonly compatibleVersions?: readonly number[]
|
|
}
|
|
|
|
/**
|
|
* One opened unit. Values are opaque JSON to this layer: no schema, no
|
|
* events, no domain meaning. The unit does NOT serialize concurrent writes —
|
|
* write ordering is the caller's responsibility (the domain layer runs one
|
|
* write chain per unit). Whole-document and row-store calls are atomic on the
|
|
* medium; a per-record batch interrupted before resolution can be partially
|
|
* applied. Resolved writes are durable; per-record deletions sync directory
|
|
* changes on POSIX before resolving. Any call after {@link close} rejects with
|
|
* `closed`.
|
|
*/
|
|
export interface KvUnit {
|
|
/**
|
|
* Read the full current snapshot.
|
|
* @returns every table's records keyed by table name, plus the global
|
|
* singleton (`null` when never written or not declared).
|
|
*/
|
|
loadAll(): Promise<{ tables: Record<string, Record<string, unknown>>; global: unknown }>
|
|
|
|
/**
|
|
* Upsert one record durably. Overwrite semantics: an existing key is replaced.
|
|
* @param table - Declared table name.
|
|
* @param key - Record key. In the `per-record` layout a key becomes a path
|
|
* segment and must match `[a-zA-Z0-9_-]+` (an unsafe key rejects); in the
|
|
* `single` layout keys stay opaque.
|
|
* @param value - Opaque JSON-serializable record.
|
|
* @returns resolution after durability.
|
|
*/
|
|
putRecord(table: string, key: string, value: unknown): Promise<void>
|
|
|
|
/**
|
|
* Delete one record durably. Idempotent: a missing key is a no-op and does
|
|
* not advance the version stamp.
|
|
* @param table - Declared table name.
|
|
* @param key - Record key.
|
|
* @returns resolution after durability.
|
|
*/
|
|
deleteRecord(table: string, key: string): Promise<void>
|
|
|
|
/**
|
|
* Delete several records durably as one batch. Idempotent: missing keys and
|
|
* empty batches are no-ops and do not advance the version stamp. Resolves
|
|
* once every listed record is absent from the readable
|
|
* state and the backend accepted the batch. A rejection normally leaves the
|
|
* batch unapplied, so no listed record was removed. A backend that cannot
|
|
* establish or restore the batch's state rejects with `unknown-state`
|
|
* instead: the readable or durable state is then unknown, every later operation on that
|
|
* unit rejects with the same error until {@link close} releases it, and
|
|
* calls after close reject `closed`. On the `single` layout a
|
|
* directory-fsync failure can arrive after the atomic replacement: the new
|
|
* file may already hold the batch while the caller sees a rejection. Close
|
|
* the backend and its domains, then reopen to inspect saved state before
|
|
* retrying. Per-record batches are not crash-atomic before resolution.
|
|
* A backend whose medium can apply a whole batch
|
|
* in one operation (the `single` document, a row store) must do that instead
|
|
* of repeating {@link deleteRecord} per key.
|
|
* @param table - Declared table name.
|
|
* @param keys - Record keys; duplicates are harmless. In the `per-record`
|
|
* layout every key becomes a path segment and must match `[a-zA-Z0-9_-]+`
|
|
* (an unsafe key rejects before any deletion); in the `single` layout keys
|
|
* stay opaque.
|
|
* @returns resolution after the backend accepted the batch.
|
|
*/
|
|
deleteRecords(table: string, keys: readonly string[]): Promise<void>
|
|
|
|
/**
|
|
* Move one record's stored document out of the unit's readable set,
|
|
* preserving its bytes for inspection instead of deleting them. Backends
|
|
* whose medium has no per-record document to move (the `single` layout, a
|
|
* row store) omit this member, and the caller falls back to its
|
|
* reject-loud path. Absent after the move: a later {@link loadAll} reads
|
|
* the key as missing and a later {@link putRecord} recreates it fresh.
|
|
* @param table - Declared table name.
|
|
* @param key - Record key.
|
|
* @returns the medium location the document was moved to (diagnostics).
|
|
*/
|
|
backupRecord?(table: string, key: string): Promise<string>
|
|
|
|
/**
|
|
* Write the global singleton durably. Only valid when the descriptor
|
|
* declared `hasGlobal`.
|
|
* @param value - Opaque JSON-serializable value.
|
|
* @returns resolution after durability.
|
|
*/
|
|
setGlobal(value: unknown): Promise<void>
|
|
|
|
/**
|
|
* Drain this unit's in-flight writes and release it. Idempotent.
|
|
* @returns resolution after the unit is released.
|
|
*/
|
|
close(): Promise<void>
|
|
}
|