midx

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 17 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 midx is the layered multi-pack index of a repository: one sorted lookup structure over many packs, the manifest that lists the layers of a repository, and the geometric rules that merge layers and compact packs.

Index

Constants

View Source
const (

	// MaxGraphCommits bounds the commits of one graph file, so a file is at most 16 MiB and a few files fit the layer
	// cache together. MaxGraphParents bounds the parents of a commit that a graph holds.
	MaxGraphCommits = 1 << 17
	MaxGraphParents = 64
	// MaxGraphOverflow bounds the overflow list of one graph file, in bytes.
	MaxGraphOverflow = 3 << 20
)
View Source
const (
	ClassMeta = "m"
	ClassBlob = "b"
)

The classes of a layer. Metadata is commits, trees and tags.

View Source
const (
	// BlockRecords is the number of records that one ranged read of a layer returns.
	BlockRecords = 256
	// RecordSize is the size of one record: name, pack number and offset.
	RecordSize = 28
	// MaxOffset is the largest pack offset that a record holds.
	MaxOffset = 1<<32 - 1
)
View Source
const Factor = 2

Factor is the geometric ratio. A run takes the next older layer while that layer is at most Factor times the size of the run, so a layer that stays is more than Factor times as large as the layer after it, and a repository of N objects has about log N layers.

Variables

View Source
var (
	ErrCorruptGraph  = errors.New("midx: corrupt commit graph")
	ErrGraphTooLarge = errors.New("midx: too many commits for one graph")
)
View Source
var ErrCorrupt = errors.New("midx: corrupt layer")
View Source
var ErrCorruptSizes = errors.New("midx: corrupt size file")

Functions

func BlockValues added in v0.16.0

func BlockValues(i int, block []byte) ([]byte, error)

BlockValues checks the CRC of a block read from the size file and returns its values.

func BuildGraph added in v0.15.0

func BuildGraph(commits []Commit) ([]byte, error)

BuildGraph returns the commit graph file of commits: the records sorted by name, then the parents after the second of each commit with more than two parents, then the SHA-256 of all of it. A commit that repeats an earlier one is kept once.

func BuildSizes added in v0.16.0

func BuildSizes(vals []uint32) []byte

BuildSizes returns the size file of a layer: a header, the blocks of 256 values in the order of the layer, each with a CRC-32 of its number and its values, and the SHA-256 of all of it. It returns nil when no value is known.

func DecodeSize added in v0.16.0

func DecodeSize(v uint32) (int64, bool)

DecodeSize returns the size that v holds, and false when it is not known.

func EncodeSize added in v0.16.0

func EncodeSize(n int64) uint32

A size is stored as its value plus one in four bytes, and 0 means that it is not known, so an empty object and an object of more than 4 GiB less two bytes are both expressible.

func MergeGraphs added in v0.15.0

func MergeGraphs(gs []*Graph) ([]byte, error)

MergeGraphs returns the graph file that holds the commits of gs. A commit that two graphs hold takes the larger generation, because 0 means that it is not known.

func SizesBlockRange added in v0.16.0

func SizesBlockRange(i, n int) (off, length int64)

SizesBlockRange returns the offset and length in the size file of block i of a layer of n records, with its CRC. The blocks are those of the layer file.

func SizesFileLen added in v0.16.0

func SizesFileLen(n int) int64

SizesFileLen is the length of the size file of a layer of n records.

func ValueAt added in v0.16.0

func ValueAt(values []byte, j int) uint32

ValueAt returns the stored value at place j of the values of a block.

Types

type Builder

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

Builder assembles a layer from records in order.

func NewBuilder

func NewBuilder(packs []Pack, maxObjects int) *Builder

NewBuilder returns a Builder for a layer over packs with at most maxObjects records.

func (*Builder) Add

func (b *Builder) Add(r Record) error

Add appends r. A record that equals the last one is dropped. A record before it is an error.

func (*Builder) Finish

func (b *Builder) Finish() (Layer, error)

Finish returns the layer file. Its key is the SHA-256 of its bytes, so equal layers have one key.

type Commit added in v0.15.0

type Commit struct {
	SHA, Tree SHA
	Time      int64
	Gen       uint32
	Parents   []SHA
}

Commit is what a walk of history needs of a commit: its tree, its parents and its commit time. Gen is the length of the longest path from the commit to a root, counting the commit, and 0 when it is not known.

type Dir

type Dir struct {
	Packs []Pack
	N     int
	// contains filtered or unexported fields
}

Dir is the part of a layer that a reader keeps in memory: the pack list, the first record of each block, and the filter. A lookup reads one block of records from the object store.

func Parse

func Parse(tail []byte, size int64) (*Dir, error)

Parse reads the tail of a layer file of size bytes, which is its last dir bytes.

func (*Dir) BlockRange

func (d *Dir) BlockRange(i int) (off, n int64)

BlockRange returns the offset and length of block i in the layer file.

func (*Dir) Blocks

func (d *Dir) Blocks() int

Blocks is the number of blocks of records.

func (*Dir) Lookup

func (d *Dir) Lookup(ctx context.Context, s SHA, ord int, fetch Fetch) (Record, bool, error)

Lookup finds a record with name s, in the pack ord, or in any pack when ord is negative. A lookup reads at most one block.

func (*Dir) LookupMany added in v0.12.0

func (d *Dir) LookupMany(ctx context.Context, shas []SHA, fetch FetchMany) ([]Record, []bool, error)

LookupMany is Lookup of every name of shas in any pack. shas are sorted and distinct. It tests the filter and walks the block fences once for the whole list, and asks fetch for each needed block once.

func (*Dir) LookupManyPos added in v0.16.0

func (d *Dir) LookupManyPos(ctx context.Context, shas []SHA, fetch FetchMany) ([]Record, []bool, []int, error)

LookupManyPos is LookupMany that also returns the position of each record in the layer.

func (*Dir) MayContain

func (d *Dir) MayContain(s SHA) bool

MayContain is false when the filter proves that no record has the name s.

func (*Dir) Resident

func (d *Dir) Resident() int

Resident is the memory that the Dir holds.

func (*Dir) Size

func (d *Dir) Size() int64

Size is the length of the whole layer file.

type Entry

type Entry struct {
	Key     string `json:"k"`
	Objects int    `json:"n"`
	// Bytes is the total size of the packs of the layer.
	Bytes int64 `json:"b"`
	// NPacks is the number of packs of the layer.
	NPacks int `json:"np"`
	// Size and Dir are the length of a layer file and of its directory tail. They are zero for an index layer.
	Size int64 `json:"sz,omitempty"`
	Dir  int   `json:"d,omitempty"`
	// Packs lists the pack of an index layer.
	Packs []Pack `json:"p,omitempty"`
	// Class is ClassMeta or ClassBlob when every pack of the layer holds only that class of object, and empty when a
	// pack may hold both.
	Class string `json:"c,omitempty"`
	// Graph is the key of the commit graph file of the layer, Commits the number of commits that it holds and GraphSize
	// the length of the file. All three are empty for a layer that has no graph.
	Graph     string `json:"g,omitempty"`
	Commits   int    `json:"gn,omitempty"`
	GraphSize int64  `json:"gs,omitempty"`
	// Sizes is the key of the size file of the layer: the inflated size of each of its records. A layer that has none
	// has no key, and a release that does not know the field ignores it.
	Sizes string `json:"z,omitempty"`
}

Entry is one layer of a manifest. A key that ends in ".idx" names the v2 index of one pack, which is a layer of one pack. A key under "midx/" names a layer file of this package.

func (Entry) IsIndex

func (e Entry) IsIndex() bool

IsIndex reports whether the layer is the v2 index of one pack.

type Fetch

type Fetch func(ctx context.Context, i int) ([]byte, error)

Fetch returns the records of block i.

type FetchMany added in v0.12.0

type FetchMany func(ctx context.Context, idx []int) ([][]byte, error)

FetchMany returns the records of the blocks in idx, in the same order.

type Graph added in v0.15.0

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

Graph is a parsed commit graph file. It holds the bytes of the file.

func ParseGraph added in v0.15.0

func ParseGraph(b []byte) (*Graph, error)

ParseGraph checks the structure and the checksum of a graph file before it trusts a byte of it.

func (*Graph) Commits added in v0.15.0

func (g *Graph) Commits() iter.Seq[Commit]

Commits returns every commit of the graph in the order of their names.

func (*Graph) Len added in v0.15.0

func (g *Graph) Len() int

Len is the number of commits.

func (*Graph) Lookup added in v0.15.0

func (g *Graph) Lookup(sha SHA) (Commit, bool)

Lookup returns the commit named sha.

type Iter

type Iter interface {
	Next() (Record, bool, error)
}

Iter yields records in order. It returns false at the end.

func NewIter

func NewIter(ctx context.Context, d *Dir, read ReadFunc) Iter

NewIter reads the records of a layer in order, in chunks, and checks the checksum of the layer at the end.

func WithSizes added in v0.16.0

func WithSizes(it Iter, s *Sizes) Iter

WithSizes sets the size of each record that it ends from the values of s, by position.

type Layer

type Layer struct {
	Key  string
	Data []byte
	// Dir is the length of the tail of Data that Parse reads.
	Dir     int
	Objects int
	// Sizes is the size file of the layer, and is nil when the builder knew no size.
	Sizes []byte
}

Layer is a finished layer file.

func Merge

func Merge(ctx context.Context, ins []Iter, packs []Pack, maxObjects int, remap func(in int, r Record) (Record, error)) (Layer, int, error)

Merge writes one layer over packs from the records of ins, in order. remap turns a record of input in into its record in the new layer, and its result must keep each input in order. maxObjects bounds the records of all inputs. It returns the number of records that the inputs gave, before it drops repeats, so a caller can check that none was lost.

type Manifest

type Manifest struct {
	Version int     `json:"v"`
	Layers  []Entry `json:"l"`
}

Manifest is the consistent set of layers of one repository, oldest layer first.

func DecodeManifest

func DecodeManifest(b []byte) (Manifest, error)

DecodeManifest reads the stored form.

func (Manifest) Encode

func (m Manifest) Encode() ([]byte, error)

Encode returns the stored form of m.

type Pack

type Pack struct {
	ID    string `json:"id"`
	Bytes int64  `json:"b"`
	Key   string `json:"k,omitempty"`
	Idx   string `json:"i,omitempty"`
}

Pack names a pack of a layer: the hex SHA-256 of its bytes, and its size. Key and Idx are the bucket keys of the pack file and of its index file, below the prefix of the repository. A manifest written before the keys were recorded has neither, and the keys are then derived from the id.

func (Pack) FileKey added in v0.20.0

func (p Pack) FileKey() string

FileKey is the bucket key of the pack file.

func (Pack) IdxKey added in v0.20.0

func (p Pack) IdxKey() string

IdxKey is the bucket key of the index file of the pack.

func (Pack) Keyed added in v0.20.0

func (p Pack) Keyed() Pack

Keyed returns p with its derived keys recorded.

type Plan

type Plan struct{ Lo, Hi int }

Plan is a run of the newest layers to rewrite as one.

func PlanCompact

func PlanCompact(layers []Entry, maxBytes int64, maxObjects, fanIn int) (Plan, bool)

PlanCompact picks the newest window of layers whose packs to rewrite as one pack, by pack bytes. The new pack has at most maxBytes and at most maxObjects objects, and the window holds at least two packs. A single layer that holds several packs, which merges of the index alone made, is a window.

func PlanMerge

func PlanMerge(layers []Entry, maxObjects, fanIn int, compactBytes int64) (Plan, bool)

PlanMerge picks the newest window of layers to merge into one layer file, by object count. The merged layer holds at most maxObjects records and the window at most fanIn layers. A window whose packs hold more than compactBytes, with an average pack under half of it, is not merged: a layer that big is never compacted, so its small packs would stay.

type ReadFunc

type ReadFunc func(ctx context.Context, off, n int64) ([]byte, error)

ReadFunc reads n bytes at off of one layer file.

type Record

type Record struct {
	SHA SHA
	Ord uint32
	Off uint32
	// Size is the stored size of the object (see EncodeSize). The layer file does not hold it: the size file does.
	Size uint32
}

Record places one object: the pack that holds it, as an index of the pack list of the layer, and its offset. A layer keeps a record for each copy of an object, ordered by name and then by pack number.

type SHA

type SHA = [20]byte

type Sizes added in v0.16.0

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

Sizes is a parsed size file.

func ParseSizes added in v0.16.0

func ParseSizes(b []byte, n int) (*Sizes, error)

ParseSizes checks a size file of a layer of n records before it trusts a byte of it.

func (*Sizes) At added in v0.16.0

func (s *Sizes) At(i int) uint32

At returns the stored value of record i.

func (*Sizes) Len added in v0.16.0

func (s *Sizes) Len() int

Len is the number of records.

Jump to

Keyboard shortcuts

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