midx

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 13 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 (
	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 ErrCorrupt = errors.New("midx: corrupt layer")

Functions

This section is empty.

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 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) 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"`
}

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 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.

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
}

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"`
}

Pack names a pack of a layer: the hex SHA-256 of its bytes, and its size.

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
}

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

Jump to

Keyboard shortcuts

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