badger

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package badger is the durable kvstore.KV driver (ADR-0066): a pure-Go Badger instance (separate from the metastore's, ADR-0065) with a single-writer group-commit gateway and per-store DropPrefix teardown. It also defines the Backup/CDC extension seams (ADR-0067/0068) — both absent by default, so the base driver writes no change-log and runs no background loop.

Index

Constants

View Source
const Reserved = "\x00"

Reserved is the NUL-prefixed namespace for the driver's internal keys (the CDC outbox + its cursors, ADR-0068). NUL cannot occur in a facade tenant key (<namespace>/<binding>/<key>), so List excludes it without ever hiding a real key. The CDC seam writes under this prefix.

Variables

This section is empty.

Functions

func Open

func Open(dir string, opts ...Option) (kvstore.KV, error)

Open opens (creating if absent) a durable Badger-backed kvstore.KV at dir, starting the single-writer group-commit gateway and the value-log GC. Close drains the gateway, stops GC, and releases the DB.

func RunBackup

func RunBackup(ctx context.Context, b Backup, logger *slog.Logger)

RunBackup drives the incremental + re-baseline cadence of a backup from NewBackup until ctx is cancelled (the daemon starts it in a goroutine when DR is enabled). A nil/non-*backup b is a no-op.

func RunCDC

func RunCDC(ctx context.Context, c CDC, logger *slog.Logger)

RunCDC drives the tailer until ctx is cancelled (the daemon starts it in a goroutine when CDC is enabled). A failed pass (e.g. a transient sink publish error) is logged and the tailer restarts from the durable cursor after cdcRetryDelay, so delivery resumes with no loss. A nil/non-*cdc c is a no-op.

Types

type Backup

type Backup interface {
	Ship(ctx context.Context) (cursor uint64, err error)
	Restore(ctx context.Context) error
}

Backup is the OPT-IN DR seam (ADR-0067). nil/absent unless DR is configured.

func NewBackup

func NewBackup(db *badger.DB, bucket blob.Bucket, cfg BackupConfig) (Backup, error)

NewBackup builds the DR backup over an opened KV Badger instance. A nil bucket means DR was enabled without a target — fault.Invalid (never a silent half-configured backup).

type BackupConfig

type BackupConfig struct {
	Interval   time.Duration
	Rebaseline time.Duration
	ChunkBytes int
}

BackupConfig configures the opt-in DR backup (ADR-0067). Zero ChunkBytes ⇒ 64 MiB.

type CDC

type CDC interface {
	OnWrite(txn *badger.Txn, key string, op Op) error
	Tail(ctx context.Context) error
}

CDC is the OPT-IN change-feed seam (ADR-0068). OnWrite is the write-hook the gateway calls INSIDE the data txn so the change-log entry commits atomically with the data; Tail publishes from a durable cursor.

func NewCDC

func NewCDC(db *badger.DB, sink bus.Bus, cfg CDCConfig) (CDC, error)

NewCDC builds the change-feed over an opened KV Badger instance. A nil sink or empty subject means CDC was enabled without a sink — fault.Invalid (never a silent half-configured feed).

type CDCConfig

type CDCConfig struct {
	Subject   bus.Subject
	Retention time.Duration
}

CDCConfig configures the opt-in change-feed (ADR-0068). Subject is the bus subject to publish to (the configured sink); zero Retention ⇒ 24h.

type Op

type Op uint8

Op is the mutation kind a CDC change-log entry records.

const (
	OpPut Op = iota
	OpDelete
)

type Option

type Option func(*config)

Option tunes the driver.

func WithBackup

func WithBackup(b Backup) Option

WithBackup wires the opt-in DR seam (ADR-0067). Absent ⇒ no backup.

func WithCDC

func WithCDC(cdc CDC) Option

WithCDC wires the opt-in change-feed seam (ADR-0068). Absent ⇒ no _cdc/ writes.

func WithSyncWrites

func WithSyncWrites(s bool) Option

WithSyncWrites toggles fsync-on-commit (default true).

func WithValueLogGCInterval

func WithValueLogGCInterval(d time.Duration) Option

WithValueLogGCInterval sets the value-log GC cadence (default 5m; 0 disables).

type Seams

type Seams struct {
	Backup Backup // nil unless kvstore.backup.enabled
	CDC    CDC    // nil unless kvstore.cdc.enabled
}

Seams holds the optional DR + CDC the daemon wires when configured (ADR-0067/0068). A nil field means that seam is off — the base driver then writes no change-log and runs no DR loop.

func OpenWithSeams

func OpenWithSeams(
	dir string,
	buildBackup func(*badger.DB) (Backup, error),
	buildCDC func(*badger.DB) (CDC, error),
	opts ...Option,
) (kvstore.KV, Seams, error)

OpenWithSeams opens the durable KV driver (ADR-0066) and wires the opt-in DR/CDC seams to the SAME Badger instance. It resolves the bootstrap order the per-seam constructors imply — NewBackup/NewCDC need the *badger.DB that Open creates — by opening the db first, building each seam (when its builder is non-nil) over that db, then starting the gateway with the seams already attached (so CDC's OnWrite fires on the very first write, with no attach-after-start race). With both builders nil it is exactly Open. The seams' background loops (Backup.Ship cadence, CDC.Tail) are started by the caller.

func OpenWithSeamsFor

func OpenWithSeamsFor(
	dir string,
	bucket blob.Bucket, bcfg BackupConfig,
	sink bus.Bus, ccfg CDCConfig,
	opts ...Option,
) (kvstore.KV, Seams, error)

OpenWithSeamsFor is the daemon-facing opener: it wires the opt-in seams from plain values, hiding the *badger.DB. A non-nil bucket enables DR backup (ADR-0067); a non-nil sink enables CDC (ADR-0068). Start the seams' loops with RunBackup / RunCDC on the returned Seams.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL