store

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 38 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 store keeps the git objects of a repository in packs and the operation objects of a workspace, in one object store. HTTP, grants and the SQL index are in other packages.

Index

Constants

View Source
const (
	// DefaultGrace is how long a retired file stays before it is deleted.
	DefaultGrace = time.Hour
	// CompactMaxBytes bounds the pack that one compaction writes.
	CompactMaxBytes = MaxPackBytes
	// MaxLayerObjects bounds the records of one layer file.
	MaxLayerObjects = 8 << 20
)
View Source
const (
	MaxOpBytes   = 8 << 20
	MaxPackBytes = 64 << 20
	MaxIdxBytes  = 8 << 20
	// MaxBlobBatchBytes bounds the blobs that one batch read returns.
	MaxBlobBatchBytes = 16 << 20
	// MaxBatch bounds the objects of one put.
	MaxBatch = 100000

	// MemoryBudget is the bytes that requests may hold while they work, apart from the bodies that are still being read.
	MemoryBudget = 512 << 20

	// LandBudget is the bytes that the leaders of lands may hold, apart from MemoryBudget. A leader that waits behind
	// fetches holds up every land of its ref.
	LandBudget = 128 << 20

	ReadAheadBytes = inFlightBytes + aheadKeep
)
View Source
const (
	// DefaultReadBudget is the bytes that the shared bucket reads of all requests may hold at once.
	DefaultReadBudget = 48 << 20
	// MinReadBudget is the least budget: a range that a prefetch plans is at most 1 MiB, and a read reserves three times
	// its length.
	MinReadBudget = 4 << 20
)
View Source
const DefaultRangeCacheBytes = 256 << 20

DefaultRangeCacheBytes is the memory of the range cache when the option and the configuration do not set it.

Variables

View Source
var (
	ErrNotFound = errors.New("store: object not found")
	ErrInvalid  = errors.New("store: invalid object name or content")
	ErrTooLarge = errors.New("store: object too large")
)
View Source
var ErrNoPath = errors.New("store: no such file")

ErrNoPath is the failure of File for a path that names no file of the commit.

Functions

func CopyPlaceBytes added in v0.16.0

func CopyPlaceBytes(n int) int64

CopyPlaceBytes is the most that the placement of a Copy of n objects holds in index blocks: placeBatch blocks.

func Phases added in v0.15.0

func Phases(ctx context.Context, prefix string) func(name string)

Phases returns a function that ends a phase of the request of ctx: it records a child span named prefix and name that runs from the end of the last phase, with what the request read from the store meanwhile. It returns nil when the request is not traced.

func StartRootSpan added in v0.15.0

func StartRootSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, func(err error, attrs ...attribute.KeyValue))

StartRootSpan is StartSpan for work that has no request, such as maintenance: the span is a root, so the sampling of a request that happens to run it does not drop it.

func StartSpan added in v0.15.0

func StartSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, func(err error, attrs ...attribute.KeyValue))

StartSpan starts a child span of ctx. The function that it returns ends the span with err and attrs, and adds what the request of ctx read from the store meanwhile. Under a parent that is not recording, it starts nothing.

func ValidWorkspaceID

func ValidWorkspaceID(id string) bool

ValidWorkspaceID reports whether id can name a workspace.

Types

type CacheStats added in v0.10.0

type CacheStats struct {
	Hits, Misses, Evictions int64
	OpenPacks, OpenBytes    int64
	IndexBytes              int64
	RangeReads, RangeBytes  int64
	IndexHits, IndexMisses  int64
	IndexEvictions          int64
	IndexResident           int64
	RangeCacheHits          int64
	RangeCacheMisses        int64
	RangeCacheBytes         int64
	RangeCacheEvictions     int64
}

CacheStats counts the open-pack cache, the layer cache and the bucket reads that fill them.

type CommitInfo added in v0.19.0

type CommitInfo struct {
	Tree    gitpack.SHA
	Parents []gitpack.SHA
	Time    time.Time
}

CommitInfo is the root tree, the parents and the committer time of a commit.

type Entry

type Entry struct {
	Name string
	Mode uint32
	SHA  gitpack.SHA
	Size int64
}

type MaintStats added in v0.10.0

type MaintStats struct {
	CompactRuns, CompactBytes, CompactObjects int64
	MergeRuns, MergeObjects                   int64
	Deleted, Failures                         int64
	Repos, LayersMax, LayersSum               int64
}

MaintStats counts the work of Maintain, and the layers of the repos that it has seen.

type OpKind

type OpKind string
const (
	OpOperation OpKind = "operations"
	OpView      OpKind = "views"
)

type Option added in v0.10.0

type Option func(*Service)

Option tunes a Service.

func WithClock added in v0.10.0

func WithClock(c clock.Clock) Option

WithClock sets the clock that stamps retired files. It defaults to the real clock.

func WithGrace added in v0.10.0

func WithGrace(d time.Duration) Option

WithGrace sets how long a retired file stays before Maintain deletes it. A request that began within the grace reads a consistent set. A value of zero or less keeps DefaultGrace.

func WithLogger added in v0.22.0

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger of the warnings of maintenance. It defaults to the default logger.

func WithRangeCache added in v0.14.0

func WithRangeCache(bytes int64) Option

WithRangeCache sets the memory of the cache of pack ranges that requests share. A value of zero or less turns the cache off. It defaults to DefaultRangeCacheBytes.

func WithReadBudget added in v0.14.0

func WithReadBudget(bytes int64) Option

WithReadBudget sets the bytes that the shared bucket reads of all requests may hold at once, at least MinReadBudget. It defaults to DefaultReadBudget.

func WithTracerProvider added in v0.22.0

func WithTracerProvider(tp trace.TracerProvider) Option

WithTracerProvider makes the service record its slow read spans with tp, not with the global tracer provider.

type PackIndex

type PackIndex interface {
	RetireFiles(ctx context.Context, repo string, keys []string, at time.Time) error
	RetiredFiles(ctx context.Context, repo string) ([]meta.Retired, error)
	ForgetRetired(ctx context.Context, repo string, keys []string) error
	BeginDelete(ctx context.Context, repo string, keys []string, now, staleBefore, retiredBefore time.Time) ([]string, error)
	Manifest(ctx context.Context, repo string) (meta.Manifest, error)
	SwapManifest(ctx context.Context, repo string, expect int64, body []byte) (int64, error)
}

PackIndex is the metadata that the store keeps about the packs of a repo: the manifest of layers and the retired files.

type Reader added in v0.6.0

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

Reader reads the objects of one repo against the pack list of the moment that Reader was called, so a request that reads many objects lists the packs once.

func (*Reader) Copy added in v0.16.0

func (r *Reader) Copy(ctx context.Context, shas []gitpack.SHA, ahead int64, before func(size int64) error, fn func(sha gitpack.SHA, raw *gitpack.Raw) error) error

Copy calls fn once for each of shas, with the stored pack entry of the object when a writer can copy it as it is, and with nil for an object that it cannot: a delta, an object that the commit and tree cache holds, and an object that no pack of the reader holds. It calls fn for the copyable objects first, in the physical order of the packs, and reads each pack once from front to back in runs of at most maxRun, reading the next runs while fn runs on the current one, with at most ahead bytes of runs read or being read. before is called with the size of an entry before the entry is read on its own. The entry that fn gets is valid until fn returns.

func (*Reader) Get added in v0.6.0

func (r *Reader) Get(ctx context.Context, sha gitpack.SHA) (gitpack.Type, []byte, error)

func (*Reader) GetSized added in v0.6.0

func (r *Reader) GetSized(ctx context.Context, sha gitpack.SHA, before func(size int64) error) (gitpack.Type, []byte, error)

GetSized is Get that calls before with the size of the object before it reads the object. An error of before stops the read.

func (*Reader) GraphCommit added in v0.15.0

func (r *Reader) GraphCommit(ctx context.Context, sha gitpack.SHA) (tree gitpack.SHA, parents []gitpack.SHA, when int64, ok bool, err error)

GraphCommit returns the tree, the parents and the commit time of a commit from the commit graph, with false when no graph of the repo holds it.

func (*Reader) Has added in v0.6.0

func (r *Reader) Has(ctx context.Context, shas []gitpack.SHA) ([]bool, error)

Has reports for each sha whether the repo holds it, from the pack indexes only.

func (*Reader) Packs added in v0.16.0

func (r *Reader) Packs() int

Packs is the number of packs that the layers of the reader list.

func (*Reader) Prefetch added in v0.11.0

func (r *Reader) Prefetch(ctx context.Context, shas []gitpack.SHA)

Prefetch reads the pack bytes that Get of shas will need, in few large ranges that it reads at most readWorkers at a time. It skips what it cannot place, and a failed read is left to the Get that needs it.

func (*Reader) PrefetchHeaders added in v0.13.0

func (r *Reader) PrefetchHeaders(ctx context.Context, shas []gitpack.SHA)

PrefetchHeaders is Prefetch for a caller that reads only the headers of the objects, as the sizes of the blobs of a tree: it keeps the first bytes of each object, not the whole object.

type Repo

type Repo struct{ Prefix, Path string }

Repo names a repository as <prefix>:<path>.

func ParseRepo

func ParseRepo(s string) (Repo, error)

ParseRepo reads the text form. It rejects a prefix or a path that the object keys cannot carry.

func (Repo) String

func (r Repo) String() string

type Service

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

Service stores the objects of every repo and workspace in one object store.

func New

func New(st bucket.Store, index PackIndex, opts ...Option) *Service

func (*Service) Blobs

func (s *Service) Blobs(ctx context.Context, repo Repo, shas []gitpack.SHA, grow func(size int64) error) ([][]byte, error)

Blobs returns the blobs of shas. Before it reads a blob, it calls grow, when it is set, with the bytes of the blobs read so far and the size that this one holds while it is read, taken from the pack.

func (*Service) CacheStats added in v0.10.0

func (s *Service) CacheStats() CacheStats

CacheStats returns the counters of the open-pack cache.

func (*Service) Commit

func (s *Service) Commit(ctx context.Context, repo Repo, sha gitpack.SHA, grow func(size int64) error) (CommitInfo, error)

Commit returns the tree, the parents and the committer time of a commit. It calls grow, when it is set, with the size of the commit before it reads it.

func (*Service) File added in v0.19.0

func (s *Service) File(ctx context.Context, repo Repo, commit gitpack.SHA, path string, max int64, grow func(size int64) error) ([]byte, error)

File returns the content of the file at path in the tree of a commit. It fails with ErrNoPath when a segment is missing or the path does not end at a file, and with ErrTooLarge when the file has more than max bytes. It calls grow with the bytes that it holds before it reads each object.

func (*Service) Get

func (s *Service) Get(ctx context.Context, repo Repo, sha gitpack.SHA) (gitpack.Type, []byte, error)

func (*Service) GetOp

func (s *Service) GetOp(ctx context.Context, ws string, kind OpKind, id string) ([]byte, error)

func (*Service) Has

func (s *Service) Has(ctx context.Context, repo Repo, shas []gitpack.SHA) ([]bool, error)

Has reports for each sha whether a pack of the repo holds it. It reads the pack indexes only.

func (*Service) ImportPack

func (s *Service) ImportPack(ctx context.Context, repo Repo, id string, idx, pack []byte) error

ImportPack stores a pack and its v2 index under the SHA-256 of the pack.

func (*Service) MaintStats added in v0.10.0

func (s *Service) MaintStats() MaintStats

MaintStats returns the counters of Maintain.

func (*Service) Maintain added in v0.10.0

func (s *Service) Maintain(ctx context.Context, repo Repo) error

Maintain does one bounded pass over repo: it adds the packs that the manifest lacks, compacts packs, merges layers and deletes the retired files whose grace has passed. It repeats the compactions and the merges until no run is left or maintainBudget has passed, so a burst of writes does not outrun it. Every step ends in one compare and swap of the manifest, so a crash between steps loses nothing, and a repeat finishes the work. Each step holds the repository lock alone, so a tail merge of a write can run between steps. It is safe to run on many replicas, which only repeat work.

func (*Service) PutObjects

func (s *Service) PutObjects(ctx context.Context, repo Repo, objs []gitpack.Object) ([]gitpack.SHA, error)

PutObjects stores objects as one pack and returns their names in order.

func (*Service) PutOp

func (s *Service) PutOp(ctx context.Context, ws string, kind OpKind, id string, data []byte) error

func (*Service) Reader added in v0.6.0

func (s *Service) Reader(ctx context.Context, repo Repo) (*Reader, error)

func (*Service) ReceivePack added in v0.6.0

func (s *Service) ReceivePack(ctx context.Context, repo Repo, pack []byte, held func(int64) error) error

ReceivePack stores the objects of a pushed pack as one pack of the repo. The pack may be thin: the service resolves the base of a ref delta from the repo, so the stored pack holds whole objects. A non-nil held is called with the inflated bytes that the unpack holds each time that count grows.

func (*Service) Reserve added in v0.5.0

func (s *Service) Reserve(ctx context.Context, bytes int64) (release func(), err error)

Reserve takes bytes of MemoryBudget, at most the whole budget, and waits while the budget is spent. The caller releases it.

func (*Service) ReserveLand added in v0.13.0

func (s *Service) ReserveLand(ctx context.Context, bytes int64) (release func(), err error)

ReserveLand takes bytes of LandBudget, at most the whole budget, and waits while the budget is spent. The caller releases it.

func (*Service) TreeBulk

func (s *Service) TreeBulk(ctx context.Context, repo Repo, sha gitpack.SHA, maxEntries int, grow func(treeBytes int64) error) (out []Tree, err error)

TreeBulk returns a tree and its subtrees breadth first, until the entries returned reach maxEntries. Blob entries carry their size. Before it reads a tree, it calls grow with the tree bytes of the trees read so far and this one, taken from the pack index. It reads the trees of a batch together, and then the headers of their blobs in groups, so a bulk read costs a few rounds of reads, not one for each tree.

func (*Service) Warm added in v0.8.0

func (s *Service) Warm(ctx context.Context, repos []Repo, parallel int) error

Warm loads the layers of repos, newest layer first, with at most parallel reads at a time. It stops loading when the layer cache first evicts, because a layer that it loads then only evicts another. It returns the first error.

type Stats

type Stats struct {
	Objects, Bytes, Layers, RangeReads, RangeBytes, RangeHits, RangeMisses, MaxDepth, SlotWaitNanos atomic.Int64
}

Stats counts what one request read from the store. A handler puts one in the context with WithStats and reads it when the request ends. A nil *Stats counts nothing.

func StatsFrom

func StatsFrom(ctx context.Context) *Stats

StatsFrom returns the Stats of a request, or nil.

func WithStats

func WithStats(ctx context.Context) (context.Context, *Stats)

WithStats returns ctx with a Stats that the reads of the request fill.

func (*Stats) AddSlotWait

func (s *Stats) AddSlotWait(d time.Duration)

AddSlotWait adds the time a request waited for a weighted slot.

func (*Stats) Attrs added in v0.15.0

func (s *Stats) Attrs() []attribute.KeyValue

Attrs returns the counts of the request as span attributes.

type Tree

type Tree struct {
	SHA     gitpack.SHA
	Entries []Entry
}

Jump to

Keyboard shortcuts

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