index

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package index has the order-preserving key encoding and the in-memory secondary index (a B-tree of key + id). Segment indexes (M04) implement the same Index interface.

Index

Constants

View Source
const (
	// MaxFields is the most fields of one compound index.
	MaxFields = 4

	// MaxTupleKeys is the most keys one document may have in one compound
	// index (the pairs of the elements of its arrays).
	MaxTupleKeys = 1024
)

Variables

This section is empty.

Functions

func CheckName added in v1.20.0

func CheckName(name string) error

CheckName tells if name is a valid index name: one field, or 2 to MaxFields different fields. The id field is not allowed in a compound index (every key ends with the id already).

func Compound added in v1.20.0

func Compound(name string) bool

Compound tells if name is the name of a compound index.

func DocKeys added in v1.20.0

func DocKeys(doc map[string]any, name, timeField string) [][]byte

DocKeys are the keys of doc in the index name: Keys for one field, the tuple keys for a compound index. timeField is the time field of the collection ("" for none).

func Encode

func Encode(v query.Value) []byte

Encode returns bytes that sort like query.Compare. The encoding is prefix-free, so key + id never mixes with a longer key.

number: tag + 8 bytes (float64 bits, flipped so bytes sort as numbers)
time:   tag + 8 bytes (unix nanoseconds, sign bit flipped)
string, object, array: tag + bytes with 0x00 → 0x00 0xFF, then 0x00 0x01

func Fields added in v1.20.0

func Fields(name string) []string

Fields are the field paths of an index name.

func Keys

func Keys(doc map[string]any, path string, isTime bool) [][]byte

Keys are the distinct encoded keys of a document field: one per array element, none when the field is missing.

func PrefixEnd added in v1.20.0

func PrefixEnd(prefix []byte) []byte

PrefixEnd is the first key after every key that starts with prefix: the keys that start with prefix are those in [prefix, PrefixEnd(prefix)). It is nil when no key is after them.

Types

type Index

type Index interface {
	// Ascend calls fn with each id in key order until fn returns false.
	Ascend(r Range, fn func(id string) bool)
	Len() int
}

Index answers range lookups with document ids.

type Mem

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

Mem is the memtable index. It is not safe for concurrent use: the collection locks around it.

func NewMem

func NewMem() *Mem

func (*Mem) Add

func (m *Mem) Add(key []byte, id string)

func (*Mem) Ascend

func (m *Mem) Ascend(r Range, fn func(id string) bool)

func (*Mem) Len

func (m *Mem) Len() int

func (*Mem) Remove

func (m *Mem) Remove(key []byte, id string)

type Range

type Range struct {
	Lo, Hi         []byte
	LoIncl, HiIncl bool
}

Range is a span of encoded keys. A nil bound is open.

func Cmp

func Cmp(op string, v query.Value) Range

Cmp is the range of $gt, $gte, $lt or $lte. Comparisons stay inside the type of v, like the evaluator does.

func Eq

func Eq(v query.Value) Range

Eq is the range of one value.

func Intersect added in v1.20.0

func Intersect(a, b Range) Range

Intersect is the keys in both a and b. Both are ranges of tuples: the low end is in, the high end is out (as PrefixRange and TupleCmp give them).

func Overflow added in v1.20.0

func Overflow() Range

Overflow is the range of the overflow documents of a compound index.

func PrefixRange added in v1.20.0

func PrefixRange(prefix []byte) Range

PrefixRange is the range of the tuples that start with prefix.

func TupleCmp added in v1.20.0

func TupleCmp(prefix []byte, op string, v query.Value) Range

TupleCmp is the range of the tuples that start with prefix and whose next field passes one comparison (gt, gte, lt or lte) with v. Like Cmp, it stays inside the type of v.

Jump to

Keyboard shortcuts

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