db/fact_store.ts

PG-backed HeldFactStore implementation.

Wraps the raw queries in db/fact_queries.ts with the lifecycle the HeldFactStore interface promises:

  • sync hash on put; put_stream hashes as it streams
  • idempotent insert (ON CONFLICT DO NOTHING in the queries layer)
  • refs are exactly the caller's options.refs — the bytes are never scanned for hashes, whatever the content_type
  • verify-on-read for disk-backed content; embedded reads skip verify because PG storage IS the hash table
  • mismatched disk bytes return null + log warning (treat as unavailable; GC / repair is a separate concern)

Embedded vs disk split: writes route by size. Bytes <= embedded_threshold land in the PG bytes column; larger bytes go to the disk CAS at <facts_dir>/<shard>/<rest> (db/fact_disk_storage.ts) and the row records a file:<shard>/<rest> external_url. put takes fully-buffered bytes; put_stream is the bounded-memory streaming twin (hash BLAKE3 as it streams, spill past the threshold, enforce max_bytes / ENOSPC). put needs disk_root + fs (the runtime/*Deps) for the over-threshold path only: without them the store has nowhere to put bytes it cannot embed, and an oversize put throws. put_stream needs fs for a body of any size — with none it throws before reading the stream — and disk_root for the over-threshold spill: with none, a body that passes the threshold throws FactPayloadTooLargeError.

Every external_url the store writes is a file:<shard>/<rest> URL into its own disk CAS, and that CAS is the only place get reads external bytes from — the store makes no network request. The column is free text (query_put_fact inserts what it is given), so get treats a row whose external_url has any other shape, and any external row on a store with no disk CAS configured, as unavailable: a warning and null. A file: URL naming another fact's file (file_fact_url_addresses) is refused the same way, logged as an error, before the file is read.

Holders

Every put names its holder (HeldFactPutOptions.holder): the actor whose request carried the bytes, or null for a system put (seeds, server-side imports). A named holder gets a fact_holder row on every put — a deduplicated put included, since bringing identical bytes is possession. The fact row, its refs, and the holder row commit in one transaction. Cell ref admission (auth/cell_ref_admission.ts) reads the holder rows. The holder is a spine concept, so PgFactStore implements HeldFactStore — the @fuzdev/fuz_util FactStore reads plus puts that take HeldFactPutOptions — rather than the backend-agnostic FactStore, whose puts name no actor.

view source

Declarations
#

6 declarations

FACT_EMBEDDED_THRESHOLD_DEFAULT
#

db/fact_store.ts view source

number import {FACT_EMBEDDED_THRESHOLD_DEFAULT} from '@fuzdev/fuz_app/db/fact_store.js';

Default embedded-vs-referenced cutoff (1 MiB).

HeldFactPutOptions
#

db/fact_store.ts view source

HeldFactPutOptions import type {HeldFactPutOptions} from '@fuzdev/fuz_app/db/fact_store.js';

Options for HeldFactStore.put / put_stream: the @fuzdev/fuz_util FactPutOptions plus the required holder.

holder is the actor that brought these bytes, recorded in fact_holder whether or not the fact already existed. null is a system put (seeds, server-side imports) that records no holder — a cell write can newly reference such a fact only as a global admin or as an actor that puts the same bytes itself (refs a cell already carries stay admitted through update and clone). Required, so a put on behalf of a caller can't silently skip the possession record. Twin of the Rust PutOptions::holder.

inheritance

extends: FactPutOptions

holder

type Branded | null

HeldFactStore
#

db/fact_store.ts view source

HeldFactStore import type {HeldFactStore} from '@fuzdev/fuz_app/db/fact_store.js';

A fact store whose puts record possession: the @fuzdev/fuz_util FactStore reads, plus put / put_stream taking HeldFactPutOptions with its required holder.

The holder is required — a store typed by the generic FactStore cannot record possession, so cell ref admission (auth/cell_ref_admission.ts) could never admit a ref to a fact put through it. AppDeps.fact_store is typed by this interface; PgFactStore implements it.

inheritance

extends: Omit<FactStore, 'put' | 'put_stream'>

put

Idempotent like FactStore.put; records options.holder (when non-null) on a fresh insert and a dedup hit alike.

type (bytes: Uint8Array<ArrayBufferLike>, options: HeldFactPutOptions): Promise<FactHash>

bytes

type Uint8Array<ArrayBufferLike>

options

returns Promise<FactHash>

put_stream

Idempotent like FactStore.put_stream; records options.holder (when non-null) on a fresh insert and a dedup hit alike.

type (stream: ReadableStream<Uint8Array<ArrayBufferLike>>, max_bytes: number, options: HeldFactPutOptions): Promise<...>

stream

type ReadableStream<Uint8Array<ArrayBufferLike>>

max_bytes

type number

options

returns Promise<PutStreamOutcome>

PgFactStore
#

db/fact_store.ts view source

import {PgFactStore} from '@fuzdev/fuz_app/db/fact_store.js';

PG-backed fact store. Delegates to db/fact_queries.ts for I/O and adds the lifecycle layer described in the module doc. Implements HeldFactStore: the @fuzdev/fuz_util FactStore reads, puts taking HeldFactPutOptions.

inheritance

implements: HeldFactStore

constructor

type new (options: PgFactStoreDeps): PgFactStore

options

put

Store fully-buffered bytes, routing by size: <= embedded_threshold into the PG bytes column; larger into the disk CAS (when disk_root + fs are configured) at <facts_dir>/<shard>/<rest> with a file: URL. Oversize without a disk root throws: the store has nowhere to put bytes it cannot embed. Idempotent — ON CONFLICT DO NOTHING + content-addressed disk filenames make a re-write harmless (it renames verified bytes over the same file). Refs are exactly options.refs (none when omitted). options.holder, when set, is recorded as a holder on a fresh insert and a dedup hit alike.

type (bytes: Uint8Array<ArrayBufferLike>, options: HeldFactPutOptions): Promise<FactHash>

bytes

type Uint8Array<ArrayBufferLike>

options

returns Promise<FactHash>

mutates

  • fact_holder — records `options.holder` (when non-null) in the transaction that writes the `fact` row

put_stream

Stream bytes into the store with bounded memory, returning the fact hash + size. Delegates the byte path to stream_fact_to_disk (hash BLAKE3 as it streams, buffer to the embedded threshold, spill to the disk CAS), then inserts the fact row by placement — embedded bytes go to the PG bytes column, disk-spilled bytes record the file: external_url. The cap is enforced mid-stream (FactPayloadTooLargeError); a disk-full mid- stream throws FactStorageFullError.

Refs are exactly options.refs (none when omitted), as on put.

Requires fs (and, for the over-threshold spill, disk_root) to be configured. The streaming twin of put; mirrors the Rust FactStore::put_stream. Records options.holder the way put does.

type (stream: ReadableStream<Uint8Array<ArrayBufferLike>>, max_bytes: number, options: HeldFactPutOptions): Promise<...>

stream

type ReadableStream<Uint8Array<ArrayBufferLike>>

max_bytes

type number

options

returns Promise<PutStreamOutcome>

mutates

  • fact_holder — records `options.holder` (when non-null) in the transaction that writes the `fact` row

get

Retrieve bytes. Embedded reads return PG bytes directly. An external read comes from the disk CAS and nowhere else: the bytes at the row's file:<shard>/<rest> URL are read and verified against the hash.

Returns null, with a warning log, for an external row the store cannot serve — an external_url that is not a file:<shard>/<rest> URL, no disk CAS configured (disk_root + fs), a failed disk read, or bytes that don't match the hash — and, with an error log, for a URL that names another fact's file. Never makes a network request.

type (hash: FactHash): Promise<Uint8Array<ArrayBufferLike> | null>

hash

type FactHash
returns Promise<Uint8Array<ArrayBufferLike> | null>

has

type (hash: FactHash): Promise<boolean>

hash

type FactHash
returns Promise<boolean>

get_meta

type (hash: FactHash): Promise<FactMeta | null>

hash

type FactHash
returns Promise<FactMeta | null>

get_refs

type (hash: FactHash): Promise<FactHash[]>

hash

type FactHash
returns Promise<FactHash[]>

delete

Drop a fact row. fact_ref rows referencing this hash as a source cascade via the FK; fact_ref targeting this hash do not — they remain as dangling pointers, consistent with the federation model where target_hash is intentionally not a FK.

Idempotent: deleting an absent fact returns null. The store does NOT verify the fact is unreferenced — that policy lives one layer up (the orphan-fact admin surface in the consumer; a future GC walker).

The disk file of a disk-backed fact is NOT unlinked — that is the caller's responsibility, using the returned external_url (when non-null): a file:<shard>/<rest> URL names <disk_root>/<shard>/<rest>. The value is the row's free text — parse it with parse_file_fact_url before joining a path.

type (hash: FactHash): Promise<{ size: number; external_url: string | null; } | null>

hash

type FactHash
returns Promise<{ size: number; external_url: string | null; } | null>

{size, external_url} for the deleted row, or null if no row matched the hash.

PgFactStoreDeps
#

db/fact_store.ts view source

PgFactStoreDeps import type {PgFactStoreDeps} from '@fuzdev/fuz_app/db/fact_store.js';

Construction-time deps for PgFactStore.

embedded_threshold (bytes) is the inline-vs-external cutoff: payloads at or under it store embedded in the fact row, larger ones route to the disk CAS. Defaults to FACT_EMBEDDED_THRESHOLD_DEFAULT (1 MiB). Consumers tune it per workload — e.g. a much lower bound (~16 KiB) keeps only small JSON inline and routes image originals + thumbnails to disk.

disk_root is the facts directory backing the <shard>/<rest> disk CAS; fs supplies the filesystem capabilities (a RuntimeDeps satisfies it). When both are set, oversize put + put_stream write to disk and get reads disk-backed facts from it. When either is unset the store writes embedded rows only and get returns null for an external row: an oversize put throws; put_stream throws for a body of any size when fs is unset, and with fs but no disk_root throws FactPayloadTooLargeError for a body that passes the threshold. log is optional — its call sites are the warnings on the paths where get returns null for an external row.

deps

type QueryDeps

embedded_threshold?

type number

disk_root?

type string

fs?

type FactDiskStorageDeps

log?

type Logger

to_uint8
#

db/fact_store.ts view source

(value: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike> import {to_uint8} from '@fuzdev/fuz_app/db/fact_store.js';

Coerce whatever the driver returns for BYTEA into a Uint8Array.

pg returns Buffer (a Uint8Array subclass), pglite already returns Uint8Array. Wrapping Buffer in a fresh Uint8Array keeps the downstream type honest without a copy.

value

a BYTEA column value as the driver returned it

type Uint8Array<ArrayBufferLike>

returns

Uint8Array<ArrayBufferLike>

a plain Uint8Array over the same bytes

Depends on
#

Imported by
#