Documentation
¶
Overview ¶
Package storage implements the vec storage engine (spec 04): fixed-stride columnar vector segments, a separate columnar metadata store, and an id-map that mediates between stable application point ids and dense engine positions.
The engine owns the single copy of every vector (spec 04 §2.1, invariant I-1): one slot in one segment backs every index and every flat scan. Indexes ([07], [08]) operate on positions and call back through FetchVector/ScanVectors to read that single copy; they never persist their own vector bytes.
This build keeps segments, columns, and the id-map resident in memory with the exact stride math and position addressing the spec mandates (spec 04 §3.2-§3.4), so the layout is faithful and the seam to the pager ([05]) is a later slice. The in-memory column slices preserve the columnar access pattern (scan one column without touching vector bytes, spec 04 §5.1) that the on-disk format also gives.
Index ¶
- Variables
- type And
- type CmpOp
- type ColID
- type ColType
- type CollectionDef
- type CollectionStats
- type ColumnDef
- type ColumnStats
- type Compare
- type ElemType
- type Engine
- func (e *Engine) Analyze(collID uint64) error
- func (e *Engine) Begin(write bool) Txn
- func (e *Engine) Close() error
- func (e *Engine) CollectionStats(collID uint64) (CollectionStats, error)
- func (e *Engine) ColumnStats(collID uint64, colID ColID) (ColumnStats, error)
- func (e *Engine) Compact(collID uint64, lo, hi uint32) error
- func (e *Engine) CreateCollection(def CollectionDef) error
- func (e *Engine) Delete(txn Txn, collID uint64, id PointID) error
- func (e *Engine) Fetch(collID uint64, P uint32, proj []ColID, snap Snapshot) (PointRecord, error)
- func (e *Engine) FetchBatch(collID uint64, positions []uint32, proj []ColID, snap Snapshot) ([]PointRecord, error)
- func (e *Engine) FetchVector(collID uint64, P uint32, buf []float32) error
- func (e *Engine) IdMapStats(collID uint64) (IdMapStats, error)
- func (e *Engine) Insert(txn Txn, collID uint64, id PointID, vec []float32, meta MetaRow) (uint32, error)
- func (e *Engine) InsertBatch(txn Txn, collID uint64, points []Point) ([]uint32, error)
- func (e *Engine) LookupID(collID uint64, id PointID) (uint32, error)
- func (e *Engine) LookupPos(collID uint64, P uint32) (PointID, error)
- func (e *Engine) MetadataFilter(collID uint64, pred Predicate, snap Snapshot) (*PositionBitmap, error)
- func (e *Engine) ScanVectors(collID uint64, snap Snapshot, cb func(pos uint32, vec []float32) bool) error
- func (e *Engine) SealSegment(collID uint64) error
- func (e *Engine) SetRepointHook(h func(collID uint64, rp []Repoint) error)
- func (e *Engine) ShouldCompact(collID uint64) bool
- func (e *Engine) Snapshot() Snapshot
- func (e *Engine) UpdateMeta(txn Txn, collID uint64, id PointID, meta MetaRow) error
- func (e *Engine) Upsert(txn Txn, collID uint64, id PointID, vec []float32, meta MetaRow) (uint32, bool, error)
- func (e *Engine) VectorSegments(collID uint64) ([]VectorSegment, error)
- func (e *Engine) WarmCache(collID uint64) error
- type HistogramBucket
- type IdMapStats
- type IsNullPred
- type MetaRow
- type Not
- type Or
- type Point
- type PointID
- type PointRecord
- type PositionBitmap
- func (b *PositionBitmap) And(other *PositionBitmap)
- func (b *PositionBitmap) Clear(p uint32)
- func (b *PositionBitmap) Contains(p uint32) bool
- func (b *PositionBitmap) Count() int
- func (b *PositionBitmap) Len() uint32
- func (b *PositionBitmap) Or(other *PositionBitmap)
- func (b *PositionBitmap) Set(p uint32)
- func (b *PositionBitmap) SetAll()
- type Predicate
- type Repoint
- type Snapshot
- type StorageEngine
- type True
- type Txn
- type Value
- type ValueKind
- type VectorSegment
- type ZoneBlock
- type ZoneMap
- type ZoneMapStats
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("vec: point id not found") ErrDuplicateID = errors.New("vec: duplicate point id") ErrDeleted = errors.New("vec: position is tombstoned") ErrNotVisible = errors.New("vec: position not visible in snapshot") ErrPositionOutOfRange = errors.New("vec: position out of range") ErrSegmentCorrupt = errors.New("vec: segment CRC mismatch") ErrDatabaseCorrupt = errors.New("vec: database requires manual recovery") ErrDimensionMismatch = errors.New("vec: vector dimension does not match collection") ErrSchemaMismatch = errors.New("vec: metadata schema does not match collection") ErrCompactionActive = errors.New("vec: compaction already in progress for collection") // ErrUnknownCollection is returned when a collID has no catalog entry. ErrUnknownCollection = errors.New("vec: unknown collection") // ErrUnknownColumn is returned when a ColID is not part of the collection schema. ErrUnknownColumn = errors.New("vec: unknown column") // ErrTxnClosed is returned when a committed or aborted transaction is reused. ErrTxnClosed = errors.New("vec: transaction already finished") )
Engine error set (spec 04 §15.5). All errors are wrappable; callers use errors.Is and errors.As. The engine never panics on bad input, it returns one of these; panics are reserved for structural invariant violations (spec 04 §25.3), raised inline rather than through a separate assert package.
var NullValue = Value{Kind: KindNull}
NullValue is the absent / NULL cell (spec 04 §5.3).
Functions ¶
This section is empty.
Types ¶
type CmpOp ¶
type CmpOp uint8
CmpOp is a scalar comparison operator in a metadata predicate (spec 04 §10.5).
type ColType ¶
type ColType uint8
ColType is the logical type of a metadata column (spec 04 §5, spec 02 §4).
type CollectionDef ¶
type CollectionDef struct {
ID uint64
Name string
Dims uint32
Elem ElemType
Metric distance.Metric
Columns []ColumnDef
SegmentCapacity uint32
// Int8Scale is the symmetric scale used to dequantize ElemInt8 segments on read
// (spec 04 §18.5). Ignored for other element types; 0 defaults to 1.0.
Int8Scale float32
}
CollectionDef declares the physical parameters of a collection (spec 04 §22.1). SegmentCapacity is the per-segment point capacity; 0 derives it from the default 256 MB segment target divided by the stride (spec 04 §3.5).
type CollectionStats ¶
type CollectionStats struct {
TotalPoints uint64
LivePoints uint64
TombstoneCount uint64
SegmentCount int
Dims uint32
Stride uint32
StalenessScore float64 // 0 fresh, 1 very stale (spec 04 §21.2)
WritesSinceAnalyze uint64
}
CollectionStats are the aggregate statistics the planner consumes (spec 04 §12.2).
type ColumnStats ¶
type ColumnStats struct {
ColID ColID
NullFraction float64
DistinctCount uint64
Min Value
Max Value
Histogram []HistogramBucket
}
ColumnStats are per-column statistics for selectivity estimation (spec 04 §12.3).
type Compare ¶
Compare is a leaf predicate: column Col compared by Op against literal Lit (spec 04 §10.5). A comparison involving NULL is false (SQL UNKNOWN treated as not-passing for filter purposes).
type ElemType ¶
type ElemType uint8
ElemType is the stored element representation of a vector segment (spec 04 §3.3).
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the storage engine for one database (spec 04 §15.1). It owns the collections, the MVCC clock, and the single-writer lock. Reads take the read lock and resolve through MVCC snapshots; writes serialize on the writer lock and publish atomically at commit (spec 04 §13.1).
func (*Engine) Begin ¶
Begin starts a transaction (spec 04 §13.1). A write transaction takes the engine's writer lock; it must be released by Commit or Abort.
func (*Engine) CollectionStats ¶
func (e *Engine) CollectionStats(collID uint64) (CollectionStats, error)
CollectionStats returns aggregate statistics for the planner (spec 04 §12.2).
func (*Engine) ColumnStats ¶
func (e *Engine) ColumnStats(collID uint64, colID ColID) (ColumnStats, error)
ColumnStats returns per-column statistics, computing them on demand if Analyze has not cached them (spec 04 §12.3).
func (*Engine) Compact ¶
Compact rewrites the collection's live points into a fresh, gap-free segment directory and reclaims tombstoned slots (spec 04 §9.4). It always performs a full-collection compaction in this build, so it produces a complete repoint table covering every live point; the lo/hi range is advisory. The repoint table is handed to the Index SPI through the registered hook so indexes renumber their positions in lockstep (spec 04 §9.5, §15.6).
func (*Engine) CreateCollection ¶
func (e *Engine) CreateCollection(def CollectionDef) error
CreateCollection registers a new collection (spec 04 §22.1). It is a DDL operation, not part of a data transaction in this build.
func (*Engine) Fetch ¶
Fetch resolves a position to a full point record (spec 04 §7.1). proj selects metadata columns; nil means all.
func (*Engine) FetchBatch ¶
func (e *Engine) FetchBatch(collID uint64, positions []uint32, proj []ColID, snap Snapshot) ([]PointRecord, error)
FetchBatch resolves many positions (spec 04 §7.5). Positions that are out of range, tombstoned, or not visible are skipped; the result holds the resolvable records in the input order.
func (*Engine) FetchVector ¶
FetchVector fills buf with the full-precision vector at position P (spec 04 §15.1). Used by the Index SPI for reranking; buf must hold dims floats.
func (*Engine) IdMapStats ¶
func (e *Engine) IdMapStats(collID uint64) (IdMapStats, error)
IdMapStats returns id-map statistics for a collection (spec 04 §15.3).
func (*Engine) Insert ¶
func (e *Engine) Insert(txn Txn, collID uint64, id PointID, vec []float32, meta MetaRow) (uint32, error)
Insert inserts a new point and returns its collection-level position (spec 04 §8.1). Returns ErrDuplicateID if the id is already live.
func (*Engine) InsertBatch ¶
InsertBatch inserts many points as one transaction (spec 04 §8.3).
func (*Engine) LookupID ¶
LookupID resolves a point id to its collection-level position (spec 04 §15.1).
func (*Engine) LookupPos ¶
LookupPos resolves a position to the point id at that position (spec 04 §15.1).
func (*Engine) MetadataFilter ¶
func (e *Engine) MetadataFilter(collID uint64, pred Predicate, snap Snapshot) (*PositionBitmap, error)
MetadataFilter evaluates pred against all live, snapshot-visible positions and returns a bitmap of the matches (spec 04 §10.5). Zone maps skip whole blocks the predicate provably rejects (spec 04 §5.4).
func (*Engine) ScanVectors ¶
func (e *Engine) ScanVectors(collID uint64, snap Snapshot, cb func(pos uint32, vec []float32) bool) error
ScanVectors calls cb for every live, snapshot-visible position in ascending position order (spec 04 §10.2). The flat index and IVF training use this.
func (*Engine) SealSegment ¶
SealSegment manually seals the open segment (spec 04 §15.1).
func (*Engine) SetRepointHook ¶
SetRepointHook registers the compaction repoint callback (spec 04 §9.5).
func (*Engine) ShouldCompact ¶
ShouldCompact reports whether a collection's tombstone fraction has crossed the compaction threshold (spec 04 §9.3).
func (*Engine) Snapshot ¶
Snapshot returns a read snapshot at the current commit point (spec 06 §2.1).
func (*Engine) UpdateMeta ¶
UpdateMeta updates only the metadata of an existing point (spec 04 §8.7).
func (*Engine) Upsert ¶
func (e *Engine) Upsert(txn Txn, collID uint64, id PointID, vec []float32, meta MetaRow) (uint32, bool, error)
Upsert inserts or replaces the point (spec 04 §8.5). Replacing tombstones the old slot and appends a new one; the returned bool reports whether the id was new.
func (*Engine) VectorSegments ¶
func (e *Engine) VectorSegments(collID uint64) ([]VectorSegment, error)
VectorSegments returns the collection's segments as the VectorSegment interface (spec 04 §15.2), for the index build loop and diagnostics.
type HistogramBucket ¶
HistogramBucket is one equi-depth bucket of a numeric column histogram (spec 04 §21.2): values in [Lo, Hi] cover roughly Count points.
type IdMapStats ¶
IdMapStats reports id-map size (spec 04 §15.3).
type IsNullPred ¶
IsNullPred passes when the column is NULL (spec 04 §5.3).
type MetaRow ¶
MetaRow is the metadata payload of a point, keyed by column id (spec 04 §15.1). A missing column is treated as NULL. Columns not in the schema are rejected.
type PointID ¶
PointID is the stable, application-supplied identity of a point (spec 04 §6.1). It never changes and is never reused after delete. The id-map translates it to a dense engine position, which an ANN graph can store in a fixed 4 bytes per edge (spec 04 §20.4).
type PointRecord ¶
PointRecord is the resolved, snapshot-visible record returned by Fetch (spec 04 §7.1): the id, the full-precision vector, and the projected metadata.
type PositionBitmap ¶
type PositionBitmap struct {
// contains filtered or unexported fields
}
PositionBitmap is a dense bitset over collection-level positions (spec 04 §10.6). The filter path produces one (1 = position passes the predicate) and the Index SPI consumes it as the pre-filter mask. It satisfies the index.Bitmap seam (Contains/Count) without the storage package importing index.
func NewPositionBitmap ¶
func NewPositionBitmap(n uint32) *PositionBitmap
NewPositionBitmap allocates a bitmap sized for n positions, all clear.
func (*PositionBitmap) And ¶
func (b *PositionBitmap) And(other *PositionBitmap)
And intersects in place with other (positions set in both survive).
func (*PositionBitmap) Contains ¶
func (b *PositionBitmap) Contains(p uint32) bool
Contains reports whether position p is set (satisfies index.Bitmap).
func (*PositionBitmap) Count ¶
func (b *PositionBitmap) Count() int
Count returns the number of set positions (satisfies index.Bitmap).
func (*PositionBitmap) Len ¶
func (b *PositionBitmap) Len() uint32
Len returns the bitmap capacity (logical position count).
func (*PositionBitmap) Or ¶
func (b *PositionBitmap) Or(other *PositionBitmap)
Or unions other into b in place.
func (*PositionBitmap) Set ¶
func (b *PositionBitmap) Set(p uint32)
Set marks position p (no-op if out of range).
func (*PositionBitmap) SetAll ¶
func (b *PositionBitmap) SetAll()
SetAll marks every position in [0, n).
type Predicate ¶
type Predicate interface {
// contains filtered or unexported methods
}
Predicate is a boolean metadata filter the engine evaluates against each live position (spec 04 §10.5, §21.1). The executor builds these from the WHERE clause ([11], [13]); the engine evaluates them and prunes whole zone blocks where it can.
type Repoint ¶
Repoint describes one position mapping change during compaction (spec 04 §15.6). The slice handed to ApplyRepoint and to the Index SPI is sorted by OldPos ascending so position-sorted index structures can binary-search it.
type Snapshot ¶
Snapshot is an MVCC read snapshot (spec 04 §13.2, spec 06). A read sees only versions committed at or before its watermark; in-flight writes are invisible.
type StorageEngine ¶
type StorageEngine interface {
Fetch(collID uint64, P uint32, proj []ColID, snap Snapshot) (PointRecord, error)
FetchBatch(collID uint64, positions []uint32, proj []ColID, snap Snapshot) ([]PointRecord, error)
FetchVector(collID uint64, P uint32, buf []float32) error
ScanVectors(collID uint64, snap Snapshot, cb func(pos uint32, vec []float32) bool) error
Insert(txn Txn, collID uint64, id PointID, vec []float32, meta MetaRow) (uint32, error)
InsertBatch(txn Txn, collID uint64, points []Point) ([]uint32, error)
Delete(txn Txn, collID uint64, id PointID) error
Upsert(txn Txn, collID uint64, id PointID, vec []float32, meta MetaRow) (uint32, bool, error)
UpdateMeta(txn Txn, collID uint64, id PointID, meta MetaRow) error
MetadataFilter(collID uint64, pred Predicate, snap Snapshot) (*PositionBitmap, error)
LookupID(collID uint64, id PointID) (uint32, error)
LookupPos(collID uint64, P uint32) (PointID, error)
CollectionStats(collID uint64) (CollectionStats, error)
ColumnStats(collID uint64, colID ColID) (ColumnStats, error)
Analyze(collID uint64) error
Compact(collID uint64, lo, hi uint32) error
SealSegment(collID uint64) error
WarmCache(collID uint64) error
Close() error
}
StorageEngine is the central object the executor and Index SPI call (spec 04 §15.1). One StorageEngine exists per open database.
type Txn ¶
type Txn = *txn
Txn is the transaction handle the engine API takes (spec 04 §15.1). It is a pointer so the same handle threads through a multi-statement transaction.
type Value ¶
Value is a single metadata cell: a small tagged union covering the scalar and variable-length column types (spec 04 §5.2, §5.5). NullValue is the zero value.
type VectorSegment ¶
type VectorSegment interface {
SeqNum() uint32
ElemType() ElemType
Dims() uint32
Stride() uint32
Capacity() uint32
LiveCount() uint32
TombstoneCount() uint32
IsSealed() bool
FetchVector(slotPos uint32, buf []float32) error
IsTombstoned(slotPos uint32) bool
VersionAt(slotPos uint32) uint64
BackwardMapLookup(slotPos uint32) (PointID, error)
Scan(cb func(slotPos uint32, vec []float32) bool) error
Pages() (firstPgno uint32, count uint32)
}
VectorSegment represents one sealed or open segment of vector data (spec 04 §15.2). Callers obtain segments through the engine; they do not create them.
type ZoneBlock ¶
type ZoneBlock struct {
Min Value
Max Value
NullCount uint32
Count uint32 // non-null live values folded into Min/Max
}
ZoneBlock summarizes one run of positions in a column segment (spec 04 §5.4). Min and Max bound the live, non-null values; a block with Count == 0 holds no summarizable value and is never skippable. Invariant I-5 (spec 04 §25.1): the zone MAY be wider than the true range but MUST NOT be narrower.
type ZoneMap ¶
type ZoneMap struct {
Blocks []ZoneBlock
}
ZoneMap is the per-column-segment array of zone blocks (spec 04 §5.4).
type ZoneMapStats ¶
ZoneMapStats summarize zone map effectiveness for a column (spec 04 §12.4).