storage

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package storage is the leaf descriptor package of the format plugins: what a format's rows in the generic package tables (packages, package_versions, package_files and their side tables) need from the subsystems that work on every format the same way: trash, retention cleanup, federation, the artifact detail, promotion, backup and the recovery journal.

It imports nothing from the rest of the repository (only the standard library and internal/registry), so internal/database, the workers, search, promote, federation, backup and recovery can all consult it without an import cycle. internal/format/plugin fills the registry from each format's registration at startup (formats.Register); a format that is not registered has no storage descriptor and the generic engines reject it.

Docker keeps dedicated tables and is handled by the engines explicitly; it has no Storage value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Names

func Names() []string

Names returns the registered format names, sorted. The slice is shared and must not be modified.

func Register

func Register(format string, s Storage)

Register installs the storage descriptor of format. It panics when format is empty or already registered.

func TrashFormatsOfKind

func TrashFormatsOfKind(kind string) []string

TrashFormatsOfKind returns the registered formats whose trash items carry kind, in registry order; none for an empty or unknown kind. The slice is shared and must not be modified, and it costs a map lookup, so it is safe on a hot path.

func TrashKindOf

func TrashKindOf(format string) (string, bool)

TrashKindOf returns the trash kind the format registers, and whether the format has a storage descriptor at all.

Types

type Backup

type Backup struct {
	// Companions are the repository-scoped tables the format's side tables
	// reference (the deb distribution graph). They are archived before the
	// package rows and restored first, parents before children.
	Companions []Companion
	// Regen is the generated metadata a restored hosted repository needs queued
	// or marked stale, because archives carry rows and never the derived
	// indexes. Empty for formats whose indexes are computed live.
	Regen RegenKind
}

Backup is how backup and restore carry a format's rows beyond the generic tables. Every format's packages, package_versions, package_files and package_tags rows and its SideTables are archived without a descriptor; a format states only what restore needs on top of them.

type Cleanup

type Cleanup struct {
	// Partition is the list of expressions that tell "the same package" apart:
	// versions are only ranked against versions of the same partition, so a
	// newer arm64 build never evicts the only amd64 one. Each element is a
	// complete SQL expression (it may contain commas). Empty means
	// p.id, f.arch, f.part.
	Partition []string
	// VersionExpr is the text expression identifying one version inside a
	// partition. Empty means v.version_key.
	VersionExpr string
	// ProtectExpr is a boolean expression; rows for which it holds still occupy
	// their rank but are never selected (npm versions a dist-tag points at, the
	// newest build of a maven snapshot). It may use window functions. Empty
	// means nothing is protected.
	ProtectExpr string
}

Cleanup is how the retention worker ranks and protects the versions of a format. The candidates are the live, listed package_files rows of one repository; the worker ranks them with

DENSE_RANK() OVER (PARTITION BY <Partition> ORDER BY <newest created_at of the
                   version group> DESC, <VersionExpr> DESC)

so the rows that share a (Partition, VersionExpr) group (the jar and pom of a maven release, the sdist and wheel of a pypi release) count as one version for keep_last_n. Every fragment is a compile-time constant of the format, never user input, and sees the aliases f (package_files), v (package_versions) and p (packages). The zero value is the generic default.

func (Cleanup) PartitionExprs

func (c Cleanup) PartitionExprs() []string

PartitionExprs is Partition with the generic default applied.

func (Cleanup) ProtectSQL

func (c Cleanup) ProtectSQL() string

ProtectSQL is ProtectExpr with the generic default applied.

func (Cleanup) VersionSQL

func (c Cleanup) VersionSQL() string

VersionSQL is VersionExpr with the generic default applied.

type Companion

type Companion struct {
	// Name is the table name (a plain identifier).
	Name string
	// Scope is a compile-time constant SQL condition over the alias t that
	// selects the rows of repository $1, e.g. "t.repository_id = $1".
	Scope string
	// RepoColumn is the column that holds the repository id (set to the new
	// repository on restore); empty for a table scoped through a parent.
	RepoColumn string
	// Refs lists the uuid columns that reference the id of another companion
	// (debian_components.distribution_id).
	Refs []string
}

Companion is a repository-scoped table that side-table rows point at.

Restore gives every companion row a new id derived from the archived one and the new repository id, so rows of a restored repository never collide with the ids of the repository it was copied from, and rows that point at each other (Refs, SideTable.Refs) are rewritten the same way.

type Detail

type Detail struct {
	Summary     Field
	Description Field
	License     Field
	Homepage    Field
	// MD5 and SHA1 are the digests shown beside the blob's SHA-256.
	MD5  Field
	SHA1 Field
	// Info are the format-specific display entries.
	Info []InfoField
	// Deps is the raw JSON the dependencies are decoded from, and Decoder the name
	// of the decoder (registered by the API layer) that turns it into a list; an
	// empty Decoder means the format shows no dependencies.
	Deps    Field
	Decoder string
}

Detail is how the artifact detail endpoint reads a format's metadata out of the generic tables. Every field is data; the zero Detail (a format with no descriptive metadata) shows only what the generic columns hold. Text values that are missing, null, empty or not a scalar or an array of scalars read as "" and are dropped from Info.

func (Detail) Resolve

func (d Detail) Resolve(doc Document) Resolved

Resolve reads the fields of d out of doc.

type Document

type Document struct {
	Package   json.RawMessage `json:"p"`
	Version   json.RawMessage `json:"v"`
	File      json.RawMessage `json:"f"`
	Checksums json.RawMessage `json:"c"`
	Side      json.RawMessage `json:"s"`
	Columns   json.RawMessage `json:"k"`
	Sibling   json.RawMessage `json:"m"`
}

Document is the JSON the detail query loads for one artifact, one slot per Source. A slot is nil when the row has no such part (no side table, no sibling file).

type Federation

type Federation struct {
	// Unit is the grain of an artifact unit; the zero value is UnitVersion.
	Unit FederationUnit
	// Tags makes the format's package_tags rows (with a unit_key) ref units that
	// point at a version unit (npm dist-tags).
	Tags bool
	// TouchPackage makes applying or deleting a unit bump packages.updated_at in
	// the same transaction: the format uses it as a validator (the npm packument
	// ETag).
	TouchPackage bool
	// StaleAttr is the key of packages.attrs the format's metadata worker reads
	// as "regenerate me" (npm's packument_stale). When set, every applied or
	// deleted unit sets it to true on its package in the same transaction, so
	// the regeneration survives a crash between the commit and the notification.
	StaleAttr string
	// LocalPackageAttrs are the keys of packages.attrs that hold state local to
	// each instance (worker output such as npm's latest_version and
	// packument_stale). They are never replicated: a package created by a
	// replicated unit starts without them.
	LocalPackageAttrs []string
	// QuarantineKeys lists the artifact paths under which a scan of the unit
	// with the given unit key may have been recorded: the spellings the format's
	// own download handler checks. Nil means the unit key is the path.
	QuarantineKeys func(unitKey string) []string
}

Federation describes how a federated format's units live in the generic tables; the federation engine drives its one generic codec from it. The unit keys themselves are written by the format's store (package_files.unit_key on the primary file of a unit, package_tags.unit_key on a tag) and are the keys of docs/design/federation.md; the capture triggers of the baseline log a unit whenever such a row changes, with the blob digest of the keyed file (or the tag's version) as the content reference.

type FederationUnit

type FederationUnit uint8

FederationUnit is the grain of a federated unit of a format: what one unit key stands for in the generic tables.

const (
	// UnitVersion is the default: a unit is a version with all its live files
	// (a Go module: the zip and the go.mod). The unit key is carried by the
	// version's primary file (package_files.unit_key).
	UnitVersion FederationUnit = iota
	// UnitFile makes a unit one file: it travels with its package and version
	// rows, but the version's other files are units of their own (a PyPI
	// release holds several distribution files, a Maven version several
	// artifacts, a raw path is one file). The unit key is the file's
	// package_files.unit_key.
	UnitFile
)

type Field

type Field struct {
	Source Source
	Path   string
	// Join separates the elements of an array value ", " when empty.
	Join string
}

Field is the location of one value in a Document: a source and a dotted key path inside it ("manifest_json.description"). The zero Field names nothing.

func (Field) Raw

func (f Field) Raw(doc Document) []byte

Raw returns the JSON value at the field, nil when it is absent or null.

func (Field) Text

func (f Field) Text(doc Document) string

Text returns the value at the field as display text: a string as is, a number or boolean in its JSON spelling and an array of scalars joined by Join.

type InfoField

type InfoField struct {
	Key  string
	From Field
}

InfoField is one display entry of the detail: Key is the label the API returns, the value is read from the Field.

type Promotion

type Promotion struct {
	// WholeVersion copies every live file of the matched file's version, not only
	// the matched file: a Go module is a zip and its go.mod, a conan recipe
	// revision is all its files and binary packages.
	WholeVersion bool
	// SharedVersion says a version holds independent files (maven classifiers,
	// pypi wheels): adding one file to a destination version that already has
	// others is legitimate. By default a destination version that has a live
	// file is a collision with the incoming one.
	SharedVersion bool
	// Tags copies the package_tags of the source package that name a copied
	// version (npm dist-tags), keeping existing destination tags.
	Tags bool
	// Regen is the generated metadata the destination needs refreshed after a copy.
	Regen RegenKind
	// Licenses is a SQL expression over sv and s yielding text[]: the licenses a
	// copied version declares, checked against the destination's license policies.
	Licenses string
	// PackageAttrs is a SQL expression over sp yielding the jsonb attrs of a
	// destination package the copy creates; the default is sp.attrs.
	PackageAttrs string
	// VersionKey is a SQL expression over s, sv, sp yielding the version_key of the
	// destination version; the default is sv.version_key. It exists for keys that
	// embed ids of the source repository (deb: the component id).
	VersionKey string
	// Part is a SQL expression over s, sv, sp yielding package_files.part of the
	// copy; the default is s.part. It exists for partitions that embed ids of
	// the source repository (deb: the distribution id).
	Part string
	// Collide is an optional SQL predicate over s, sv, sp, d, dv, dp that is true
	// when destination file d serves the same download as source file s although
	// its path differs (deb and rpm resolve a download by file name alone).
	Collide string
	// Prepare lists statements run once per source file before the destination is
	// probed, with $1 = destination repository id and $2 = source file id: they
	// create the destination rows the copy hangs on (deb distributions).
	Prepare []string
	// AfterCopy lists statements run once per destination version a copy wrote
	// to, with $1 = destination repository id, $2 = destination package id and
	// $3 = destination version id: format state that depends on the copy (the
	// npm latest tag and packument stamp, the rpm stale flag).
	AfterCopy []string
	// SideSet maps a side table name to a SQL expression over x (the source side
	// row), s and $1 yielding a jsonb object of columns to override on the copied
	// side row (deb: the destination's distribution and component ids).
	SideSet map[string]string
}

Promotion is how the promotion engine copies a format's rows between two repositories. The engine finds the live source files that store a bundle item's content and copies their package, version and file rows (and side rows) into the destination; the zero value is a format where one file is one version and nothing else needs doing.

Every SQL string is a compile-time constant; request data only ever travels as bind parameters. Fragments are spliced into statements of the engine and may use the aliases and parameters their field documents:

s, sv, sp   the source file, its version and its package
d, dv, dp   a destination file, its version and its package
$1          the destination repository id (uuid)

A Prepare or AfterCopy statement binds only the parameters it names: leaving out the trailing ones is fine.

type RegenKind

type RegenKind string

RegenKind names generated metadata (indexes a worker renders from the rows) that a bulk write makes stale. A format whose indexes are computed live from the tables has none.

const (
	RegenDeb    RegenKind = "deb"
	RegenRPM    RegenKind = "rpm"
	RegenApk    RegenKind = "apk"
	RegenMaven  RegenKind = "maven"
	RegenNPM    RegenKind = "npm"
	RegenPacman RegenKind = "pacman"
)

The generated-metadata kinds.

type Resolved

type Resolved struct {
	Summary, Description, License, Homepage string
	MD5, SHA1                               string
	Info                                    map[string]string
	// Deps is the raw JSON of the dependency source, nil when absent.
	Deps []byte
}

Resolved is a Detail applied to a Document.

type SideTable

type SideTable struct {
	// Name is the table name. Descriptors are compile-time constants; engines
	// splice the name into SQL after checking it is a plain identifier.
	Name string
	// Exclude lists the columns the recovery journal does not record because
	// they are volatile or derivable from the blob. Backup and promotion always
	// carry every column: a restore must not lose a derived value that costs a
	// blob scan to rebuild (rpm_extra, pacman file_list).
	Exclude []string
	// Refs lists the uuid columns that reference rows of the format's
	// Backup.Companions (deb_files.distribution_id, component_id). Restore
	// derives their new values together with the companion rows' own ids.
	Refs []string
}

SideTable is a table that extends package_files for one format: one row per file, its primary key id references package_files(id) ON DELETE CASCADE. Every generic engine that copies, exports or journals file rows carries the side rows of the file with it (promotion copies them, backup archives and restores them, the recovery journal records them).

type Source

type Source string

Source names the JSON document of the artifact detail a Field is read from. The detail query loads one row of package_files (the artifact, its id is the listing id) with its version, package and side-table row and hands them to Detail.Resolve as a Document.

const (
	// FromPackage reads packages.attrs.
	FromPackage Source = "p"
	// FromVersion reads package_versions.metadata.
	FromVersion Source = "v"
	// FromFile reads package_files.attrs.
	FromFile Source = "f"
	// FromChecksums reads package_files.checksums.
	FromChecksums Source = "c"
	// FromSide reads the format's side-table row of the file (conan_files,
	// deb_files, maven_files, pacman_files, rpm_files) as a JSON object keyed by
	// column; bulk columns that no detail shows (pacman file_list, signature) are
	// not loaded.
	FromSide Source = "s"
	// FromColumns reads the plain columns the generic tables keep outside any JSON:
	// arch, download_count (numbers), name (packages.name) and version_key.
	FromColumns Source = "k"
	// FromSibling reads package_files.attrs of the unlisted sibling file of the
	// same version (go: the go.mod file next to the listed zip).
	FromSibling Source = "m"
)

type Storage

type Storage struct {
	// Trash is how the format's files are soft-deleted, restored and purged.
	Trash Trash
	// Cleanup is how the retention worker ranks and protects the format's versions.
	Cleanup Cleanup
	// Federation describes the federated units of the format, nil for formats that
	// are not federated (repotype.Spec.Federation is false).
	Federation *Federation
	// Detail is how the artifact detail endpoint decodes the format's metadata.
	Detail Detail
	// Promotion is how the promotion engine copies the format's rows between repositories.
	Promotion Promotion
	// Backup is how backup and restore carry the format's rows beyond the generic tables.
	Backup Backup
	// SideTables are the format's tables that extend package_files (one row per
	// file, primary key = package_files.id), journaled and backed up with the
	// generic tables.
	SideTables []SideTable
}

Storage is the per-format description of how the generic engines treat the format's package rows. The zero value of every field is the generic default, so a format only states what differs.

func Lookup

func Lookup(format string) (Storage, bool)

Lookup returns a copy of the descriptor registered for format.

type Trash

type Trash struct {
	// Kind is the string the API shows as the trash kind of the format's items
	// (models.TrashKind*); one per format. It must be unique across the registry.
	Kind string
	// Scope is ScopeFile (zero value) or ScopeVersion.
	Scope TrashScope
	// NameExpr is the text expression the trash list shows as the item name.
	// Empty means p.name.
	NameExpr string
	// VersionExpr is the text expression the trash list shows as the item
	// version. Empty means v.version.
	VersionExpr string
	// RegenKeys is a select list of two text expressions (for example
	// "p.attrs->>'group_id', p.attrs->>'artifact_id'") naming what a metadata
	// regeneration after a restore or purge needs to know about the item. Empty
	// means the format has no keyed regeneration and both keys read as ”.
	RegenKeys string
	// OnTrash are statements run once per newly trashed trash unit, in the
	// transaction that stamps it, after the files and version are stamped.
	// Each statement takes one parameter: $1 = the id of the listed file.
	OnTrash []string
	// OnRestore are statements run once per restored trash unit, after the
	// files are un-stamped; $1 = the id of the listed file.
	OnRestore []string
	// OnPurge are statements run once per purged trash unit BEFORE its files are
	// deleted (so they can still read the version); $1 = the id of the listed
	// file. The retention worker runs them too when it deletes a live version
	// for good.
	OnPurge []string
}

Trash is how a format's files are soft-deleted, restored and purged by the generic trash engine. The trash unit is the LISTED package_files row of the format (an unlisted companion file is never an item of its own); its id is the reference id the API and the purge worker use.

Every SQL fragment is a compile-time constant of the format, never user input. Fragments see the aliases f (package_files), v (package_versions) and p (packages) of the item.

func (Trash) NameSQL

func (t Trash) NameSQL() string

NameSQL is NameExpr with the generic default applied.

func (Trash) RegenKeysSQL

func (t Trash) RegenKeysSQL() string

RegenKeysSQL is RegenKeys with the default (two empty keys) applied.

func (Trash) VersionSQL

func (t Trash) VersionSQL() string

VersionSQL is VersionExpr with the generic default applied.

type TrashScope

type TrashScope uint8

TrashScope says which package_files rows one trash item stands for.

const (
	// ScopeFile is the default: the trash item is one listed package_files row
	// (its id is the trash reference id) and nothing else is stamped with it.
	ScopeFile TrashScope = iota
	// ScopeVersion makes the item the whole version: trashing, restoring or
	// purging the listed file does the same to every other file of its version
	// that was trashed with it (gomod: the zip drags the unlisted go.mod along).
	ScopeVersion
)

Jump to

Keyboard shortcuts

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