archive

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package archive moves cold rows out of the live database into zstd compressed NDJSON bundles in a local directory, and can list, browse, restore and verify them.

Index

Constants

View Source
const DirName = "archives"

DirName is the bundle directory's name under the data dir.

Variables

View Source
var (
	// ErrInvalidID means the id does not match the bundle name pattern.
	ErrInvalidID = errors.New("invalid archive id")
	// ErrNotFound means no bundle with that id exists.
	ErrNotFound = errors.New("archive not found")
	// ErrNothingToArchive means no row is old enough to leave the live tables.
	ErrNothingToArchive = errors.New("nothing to archive")
	// ErrChecksum means a bundle no longer matches its recorded checksum.
	ErrChecksum = errors.New("archive checksum mismatch")
)

Kinds lists every archivable kind.

Functions

This section is empty.

Types

type Bundle

type Bundle struct {
	ID         string         `json:"id"`
	Kind       Kind           `json:"kind"`
	CreatedAt  time.Time      `json:"created_at"`
	Cutoff     time.Time      `json:"cutoff"`
	Rows       map[string]int `json:"rows"`
	TotalRows  int            `json:"total_rows"`
	SizeBytes  int64          `json:"size_bytes"`
	SHA256     string         `json:"sha256"`
	VerifiedAt *time.Time     `json:"verified_at,omitempty"`
	VerifiedOK *bool          `json:"verified_ok,omitempty"`
	RestoredAt *time.Time     `json:"restored_at,omitempty"`
}

Bundle describes one archive bundle.

type Kind

type Kind string

Kind names a group of tables archived together.

const (
	KindAudit     Kind = "audit"
	KindDeploys   Kind = "deploys"
	KindPipelines Kind = "pipelines"
)

Archive kinds.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager creates, lists, restores and prunes bundles in one directory.

func New

func New(db *sql.DB, dataDir string) *Manager

New returns a Manager storing bundles under dataDir/archives.

func (*Manager) Browse

func (m *Manager) Browse(id, table string, offset, limit int) (Rows, error)

Browse returns up to limit rows of table starting at offset, without touching the live database.

func (*Manager) Create

func (m *Manager) Create(ctx context.Context, kind Kind, cutoff time.Time) (Bundle, error)

Create archives every cold row of kind older than cutoff into one bundle and removes those rows from the live tables once the bundle has been re-read and matches. It returns ErrNothingToArchive when no row qualifies.

func (*Manager) Delete

func (m *Manager) Delete(id string) error

Delete removes a bundle and its metadata.

func (*Manager) Get

func (m *Manager) Get(id string) (Bundle, error)

Get returns one bundle.

func (*Manager) HotRows

func (m *Manager) HotRows(ctx context.Context) map[string]int

HotRows counts the live rows per kind, to show what is still hot.

func (*Manager) List

func (m *Manager) List() ([]Bundle, error)

List returns every bundle, newest first.

func (*Manager) Path

func (m *Manager) Path(id string) (string, error)

Path returns the bundle file path for download after confirming it exists.

func (*Manager) Prune

func (m *Manager) Prune(keepDays int) (int, error)

Prune deletes bundles older than keepDays (0 keeps everything).

func (*Manager) Restore

func (m *Manager) Restore(ctx context.Context, id string) (RestoreResult, error)

Restore puts a bundle's rows back into the live tables. Rows already present are left alone; pipeline runs whose pipeline was deleted are skipped.

func (*Manager) Run

Run archives every kind per the policy, then prunes expired bundles.

func (*Manager) Verify

func (m *Manager) Verify(ctx context.Context, id string) (Bundle, error)

Verify checks the bundle's hash and row counts, then restores it into a scratch database built from the current schema and compares row counts.

type RestoreResult

type RestoreResult struct {
	Restored map[string]int `json:"restored"`
	Existing int            `json:"already_present"`
	Skipped  int            `json:"skipped_missing_parent"`
}

RestoreResult reports how many rows came back per table.

type Rows

type Rows struct {
	Table string           `json:"table"`
	Total int              `json:"total"`
	Rows  []map[string]any `json:"rows"`
}

Rows is one page of a bundle's rows for one table.

type RunResult

type RunResult struct {
	Created []Bundle `json:"created"`
	Pruned  int      `json:"pruned"`
}

RunResult summarises one archive pass.

Jump to

Keyboard shortcuts

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