Compactedtypeno test imports ittype Compacted = { readonly epoch: number; readonly horizon: VersionVector; readonly collected: number; readonly retained: number; readonly blobs: number; readonly proposal: EpochProposal };What a collection did.
The collection of rows, and the epoch that makes it last. Rows are only ever added, so a database grows with every write and every delete; they can be collected only when every replica that matters has them, which is known only from what each has said it holds. The horizon of a roster that has all reported is, for each replica, the least number of its rows that any of the roster holds, and a row whose number is covered is dead unless the epoch keeps it: the heads of each path (what the files are made of) are kept, and a path whose only head is a delete is kept by nothing at all. The epoch is written down, and a trigger refuses a dead row that a SQL import brings back, so that an old dump replayed afterwards cannot raise a file that was deleted and collected. An epoch is a record that can be published and adopted by another replica that holds all it covers. A replica that will not return is retired: it is no longer trusted, and that is written down with who did it, when and why.
import { compactDatabase } from "./compact.ts";
// const done = await compactDatabase(port, ctx, { reports, roster });Compactedtypeno test imports ittype Compacted = { readonly epoch: number; readonly horizon: VersionVector; readonly collected: number; readonly retained: number; readonly blobs: number; readonly proposal: EpochProposal };What a collection did.
CompactInputtypeno test imports ittype CompactInput = { readonly reports: Readonly<Record<string, VersionVector>>; readonly roster: readonly ReplicaId[] };What a replica says of what it holds, for each replica of the roster.
EpochProposaltypeno test imports ittype EpochProposal = {
readonly format: 1;
readonly proposer: ReplicaId;
readonly horizon: VersionVector;
readonly roster: readonly ReplicaId[];
readonly retained: readonly string[];
readonly bound: string;
/** What the replica that made the epoch says of its identity, where it signs: not part of the identity. */
readonly signature?: string;
};An epoch as a record a replica can publish and another can adopt: who made it, what it covers, against which roster, what it keeps and the greatest id it collected.
Retiredtypeno test imports ittype Retired = { readonly replica: ReplicaId; readonly by: ReplicaId; readonly at: string; readonly reason: string };A replica that is no longer trusted: who retired it, when and why.
AdoptedFromMediumtypeno test imports ittype AdoptedFromMedium = { readonly adopted: readonly Compacted[]; readonly waiting: readonly string[]; readonly refused: readonly string[] };What taking the epochs of a medium did: the epochs adopted, the ones this replica is not yet caught up for, and the files refused.
adoptEpochfunctionno test imports itfunction adoptEpoch(port: SqlPort, c: Ctx, proposal: EpochProposal): Promise<Result<"nothing" | Compacted, string>>Takes an epoch another replica made: the proposer must be one the database takes changes from, and the database must hold every row the epoch covers (otherwise it is not caught up, and the refusal says which). The database collects as the epoch says, and keeps the heads it has itself as well, so that a path never loses what it has not yet seen replaced.
adoptPublishedfunctionfunction adoptPublished(medium: Store<"list" | "read">, port: SqlPort, c: Ctx): Promise<Result<AdoptedFromMedium, string>>Adopts the epochs a medium holds, oldest horizon first: each file must be the epoch its name says it is, and the database adopts it where it holds what it covers; an epoch it is not caught up for waits for a later synchronization, and one it refuses (not from whom it takes changes) is reported.
compactDatabasefunctionfunction compactDatabase(port: SqlPort, c: Ctx, input: CompactInput): Promise<Result<Compacted, string>>Collects the rows that every replica of a roster holds. The roster must be known to be complete: every replica of it must have reported what it holds, and the refusal says who has not. The database's own report is the one it makes now.
decodeEpochfunctionno test imports itfunction decodeEpoch(text: string): Result<EpochProposal, string>An epoch record from its text, or why it is not one.
encodeEpochfunctionno test imports itfunction encodeEpoch(p: EpochProposal): stringThe text of an epoch record: sorted, so that one epoch is one text.
encodeEpochFilefunctionno test imports itfunction encodeEpochFile(p: EpochProposal): stringThe text of an epoch record as it is kept in a medium: its identity's text and the signature where it has one.
signEpochfunctionno test imports itfunction signEpoch(p: EpochProposal, signer: Signer): Promise<EpochProposal>An epoch signed by the replica that made it: the signature is over the identity of the epoch, which is made without it.
epochIdconstno test imports itconst epochId: (p: EpochProposal) => Promise<string>The identity of an epoch record: the SHA-256 of its text.
epochPathfunctionno test imports itfunction epochPath(p: EpochProposal): Promise<Result<Path, string>>The path an epoch is published at: below epochs/, named by its identity, so that an epoch is one file wherever it is published.
epochsOfconstno test imports itconst epochsOf: (port: SqlPort, c: Ctx) => Promise<readonly EpochRow[]>The epochs of the database, oldest first.
publishEpochfunctionfunction publishEpoch(medium: Store<"write">, p: EpochProposal, signer?: Signer | undefined): Promise<Result<string, string>>Publishes an epoch to a medium, written once; where it is there already it is the same file.
retireReplicafunctionno test imports itfunction retireReplica(port: SqlPort, c: Ctx, replica: ReplicaId, reason: string, at: string): Promise<Result<Retired, string>>Retires a replica that will not return: it is no longer trusted (its rows are refused from now on, in a delta and by the trigger of an import), and the fact is written down. A replica cannot retire itself, and a reason is required.
retiredOffunctionno test imports itfunction retiredOf(port: SqlPort, c: Ctx): Promise<readonly Retired[]>The replicas that were retired, with who retired them and why.