postings

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package postings is the inverted index: for each (name, value) attribute — identified by **interned symbol ids**, not strings — it keeps the sorted list of [signal.SeriesID]s that carry it, and composes those lists with lazy set-op iterators (Intersect/Merge/Without) to resolve label matchers to series.

Keys are uint32 symbol ids (from index/symbols), so the index is zero-alloc and memory-compact, and it preserves attribute types: the value id is interned from the value's type-tagged encoding, so int 5, "5" and 5.0 are distinct buckets — not a stringified collapse. Matching is **callback-based and typed at the edges**: MemPostings.Select hands a predicate the candidate value id, and the caller decodes it to a signal.Value and applies any rule (regexp, numeric range, exact). Storage knows nothing about a query language's operators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToSlice

func ToSlice(p Postings) ([]signal.SeriesID, error)

ToSlice fully drains a Postings into a slice (for tests and materialization).

Types

type Matcher

type Matcher struct {
	NameID uint32
	Match  func(valueID uint32) bool
}

Matcher is a single label condition: the value ids of NameID that satisfy the Match predicate. The callback form keeps storage language- and type-neutral — the caller decodes the value id to a typed value and supplies the rule.

type MemPostings

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

MemPostings is the in-memory inverted index: nameID → valueID → sorted series list, plus the set of all series. The zero value is not usable; create one with NewMemPostings. Not safe for concurrent use; callers own synchronization (the first read sorts the lists in place).

func NewMemPostings

func NewMemPostings() *MemPostings

NewMemPostings returns an empty index.

func (*MemPostings) Add

func (p *MemPostings) Add(series signal.SeriesID, nameID, valueID uint32)

Add records that series carries the attribute nameID=valueID (both interned symbol ids). Re-adding the same triple is fine; duplicates are removed on the first read.

func (*MemPostings) All

func (p *MemPostings) All() Postings

All returns a Postings over every series in the index.

func (*MemPostings) EnsureSorted added in v0.2.0

func (p *MemPostings) EnsureSorted()

EnsureSorted sorts and deduplicates the index in place (idempotent; a no-op once sorted). It is the exported form of the read-triggered lazy sort: a caller holding the index's **exclusive** lock calls it after writes so that subsequent **concurrent** reads (which only inspect the already-sorted lists) never trigger the in-place mutation. See MemPostings.Sorted.

func (*MemPostings) ForName

func (p *MemPostings) ForName(nameID uint32) Postings

ForName returns the series that carry the attribute nameID with any value.

func (*MemPostings) Get

func (p *MemPostings) Get(nameID, valueID uint32) Postings

Get returns the series carrying exactly nameID=valueID.

func (*MemPostings) LabelValues

func (p *MemPostings) LabelValues(nameID uint32) []uint32

LabelValues returns, sorted, the distinct value ids seen for nameID.

func (*MemPostings) Resolve

func (p *MemPostings) Resolve(ms ...Matcher) Postings

Resolve returns the series matching all matchers (their intersection). With no matchers it returns every series. Negation and absent-label semantics are composed by the caller with Without / MemPostings.WithoutName.

func (*MemPostings) Select

func (p *MemPostings) Select(nameID uint32, match func(valueID uint32) bool) Postings

Select returns the union of the series whose value id for nameID satisfies match. The predicate receives the candidate value id; the caller resolves it to a typed signal.Value (via the symbol table) and applies any rule, so storage stays free of query-language operators. For exact equality prefer MemPostings.Get (an O(1) lookup).

func (*MemPostings) Sorted added in v0.2.0

func (p *MemPostings) Sorted() bool

Sorted reports whether the index is already sorted (no read will mutate it). A caller that owns synchronization checks this under a read lock to decide whether it must upgrade to an exclusive lock and call MemPostings.EnsureSorted before issuing concurrent reads.

func (*MemPostings) WithoutName

func (p *MemPostings) WithoutName(nameID uint32) Postings

WithoutName returns the series that do not carry the attribute nameID at all.

type Postings

type Postings interface {
	Next() bool
	Seek(v signal.SeriesID) bool
	At() signal.SeriesID
	Err() error
}

Postings is a forward iterator over a sorted, deduplicated stream of signal.SeriesID. Next advances; Seek jumps to the first id ≥ v; At returns the current id; Err reports a terminal error. The set-op constructors (Intersect, Merge, Without) compose Postings lazily, so resolving a matcher never materializes intermediate lists.

func Empty

func Empty() Postings

Empty returns a Postings with no entries.

func FromSlice

func FromSlice(s []signal.SeriesID) Postings

FromSlice returns a Postings over a sorted, deduplicated slice (not copied).

func Intersect

func Intersect(ps ...Postings) Postings

Intersect returns the set intersection (AND) of the inputs.

func Merge

func Merge(ps ...Postings) Postings

Merge returns the set union (OR) of the inputs, deduplicated.

func Without

func Without(a, b Postings) Postings

Without returns the set difference a \ b (a AND NOT b).

Jump to

Keyboard shortcuts

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