row2

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: 0BSD Imports: 10 Imported by: 2 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 row2 reads and writes the Fast2 and Compact2 KVO row formats. A Field or View borrows at most one byte span. The external schema supplies signedness and field meanings. Select the codec at the table boundary: these bytes cannot be decoded as Fast1 word-map data.

Index

Examples

Constants

View Source
const MaxBytes = 64 << 20

MaxBytes limits the encoded size of one container.

View Source
const MaxEntries = 1 << 20

MaxEntries limits the number of entries in one container.

Variables

View Source
var (
	ErrFormat = errors.New("row2: invalid row")
	ErrLayout = errors.New("row2: invalid layout")
	ErrKind   = errors.New("row2: wrong container kind")
	ErrType   = errors.New("row2: incompatible value")
	ErrOrder  = errors.New("row2: keys must be ordered and unique")
	ErrLimit  = errors.New("row2: container exceeds limit")
	ErrValue  = errors.New("row2: invalid input")
	ErrIndex  = errors.New("row2: index out of bounds")
)

Codec errors distinguish invalid input, incompatible operations, and size limits.

Functions

func PayloadSize

func PayloadSize(v values.Value) uint64

PayloadSize bounds a scalar or immutable byte-span payload before encoding.

Types

type Editor

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

Editor holds a borrowed row until the first write. Snapshot freezes the current bytes, including for backends which retain a Put until commit. Use one editor by pointer. Do not copy an editor after its first write.

func Edit

func Edit(base View) *Editor

Edit creates an editor starting from base.

func (*Editor) Get

func (e *Editor) Get(path []Step, enc Encoding) values.Value

Get borrows from the current edit until the next Set. Snapshot the row before taking a view that must survive another write.

func (*Editor) Set

func (e *Editor) Set(path []Step, value values.Value, enc Encoding)

Set replaces a leaf. A scalar with an existing Fast cell changes eight bytes in owned storage. Changes in shape or Compact width stream through the writer; siblings, including unknown fields, retain their raw payloads.

func (*Editor) SetWithWriter

func (e *Editor) SetWithWriter(path []Step, value values.Value, enc Encoding, w *Writer)

SetWithWriter reuses metadata scratch across edits. The caller can share w across editors used sequentially. Each rebuilt row owns its output bytes.

func (*Editor) Snapshot

func (e *Editor) Snapshot() View

Snapshot returns the current view. Later edits preserve the returned snapshot.

type Encoding

type Encoding uint8

Encoding specifies how a typed runtime value maps to row2 payload bytes. Strings and immutable byte spans share Data; Float32 uses the historical float64 wire encoding, while runtime values retain float32 bits.

const (
	Word Encoding = iota
	Signed
	Float32
	Data
	Timestamp
)

Field encodings select how a logical value is represented in a row.

func (Encoding) String

func (e Encoding) String() string

String returns the diagnostic name of the encoding.

type Field

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

Field is raw codec metadata, not a runtime value. Payloads borrow the source container; inline words retain their full width when copied without a model. Decode requires the schema's encoding. Copying unknown fields uses PutRaw.

func (Field) Container

func (v Field) Container() View

Container interprets the payload as an already trusted nested row.

func (Field) Decode

func (v Field) Decode(enc Encoding) values.Value

Decode interprets the stored field using its model's scalar encoding.

func (Field) IsZero

func (v Field) IsZero() bool

IsZero reports whether the field carries a zero word or an empty payload.

func (Field) Payload

func (v Field) Payload() []byte

Payload returns the borrowed bytes for a payload field. A nonzero inline word has no payload.

type Iterator

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

Iterator tracks one pass through the encoded entries of a container.

func (*Iterator) Next

func (it *Iterator) Next() (key uint64, text []byte, value Field, ok bool)

Next advances to the next raw field and returns false at the end.

type KeyAccessOptions

type KeyAccessOptions uint8

KeyAccessOptions controls property access independently of the field ID.

const (
	// Compact selects compact storage when creating a child.
	Compact KeyAccessOptions = 1 << iota
	// Present preserves explicit zero values and present, empty children.
	Present
)

type Kind

type Kind int

Kind identifies a row2 container. Its values are part of the wire format.

const (
	Object     Kind = 0
	Dictionary Kind = 1
	List       Kind = 2
)

Container kinds distinguish numeric fields, dictionary entries, and ordered elements.

func (Kind) String

func (k Kind) String() string

String returns the wire format name for diagnostics.

type Layout

type Layout uint8

Layout selects the physical row2 container layout. The values match the low trailer bit: Compact2 is zero and Fast2 is one. They are not EDB's StorageFormat IDs, which also include other codecs.

const (
	Compact2 Layout = 0
	Fast2    Layout = 1
)

Layout constants select compact or random-access container encoding.

func (Layout) String

func (l Layout) String() string

String returns the wire format name for diagnostics.

type Step

type Step struct {
	Key     uint64
	Options KeyAccessOptions
}

Step names a schema property and its access options.

type View

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

View borrows one encoded container. Raw and child spans are read-only.

func Empty

func Empty(layout Layout) View

Empty returns an empty object encoded in the specified layout.

func Open

func Open(raw []byte) View

Open panics on error. Use TryOpen to handle errors explicitly.

func Trusted

func Trusted(b []byte) View

Trusted borrows bytes produced by the codec or validated at ingestion. The bytes and any borrowed children must outlive the view.

func TryOpen

func TryOpen(raw []byte) (v View, err error)

TryOpen validates a single bounded container. Spans are opaque; for untrusted input, open a modeled child separately before trusting its contents.

func (View) At

func (v View) At(index int, enc Encoding) values.Value

At decodes the list entry at the supplied index using enc.

func (View) Child

func (v View) Child(s Step) View

Child returns a missing view for an absent field. A presence-significant field can represent a present, empty object using an explicit zero.

func (View) Get

func (v View) Get(key uint64, enc Encoding) values.Value

Get decodes one property using its declared encoding; it does not decode a row.

func (View) GetFast

func (v View) GetFast(key uint64) values.Value

GetFast borrows a field from Fast2, whose words and spans already have the runtime layout. The caller must ensure Indexed() and handle Float32 fields through Get(key, Float32), since those retain float64 bits on disk.

func (View) GetString

func (v View) GetString(key string, enc Encoding) values.Value

GetString looks up a dictionary entry by string key and decodes it using enc.

func (View) Has

func (v View) Has(key uint64) bool

Has reports whether the object contains the specified key.

func (View) HasString

func (v View) HasString(key string) bool

HasString distinguishes a missing dictionary entry from a present zero value.

func (View) Iter

func (v View) Iter() Iterator

Iter starts a cursor over the raw fields in encoded order.

func (View) Kind

func (v View) Kind() Kind

Kind reads the container kind from the encoded trailer.

func (View) Layout

func (v View) Layout() Layout

Layout returns Compact2 for a missing view; use Missing to test presence.

func (View) Len

func (v View) Len() int

Len returns the number of entries in the container.

func (View) Missing

func (v View) Missing() bool

Missing reports whether this view has no encoded bytes.

func (View) Raw

func (v View) Raw() []byte

Raw returns the borrowed encoded container bytes.

func (View) RawAt

func (v View) RawAt(i int) Field

RawAt reads a list field by index and panics with ErrIndex for an invalid index.

func (View) RawField

func (v View) RawField(key uint64) Field

RawField looks up a numeric object key without decoding the field payload.

func (View) RawString

func (v View) RawString(key string) Field

RawString looks up a raw dictionary entry. A missing key returns the zero value.

type Writer

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

Writer streams nested containers into one destination. Keys within an object or dictionary must arrive in strictly increasing order.

Example
package main

import (
	"fmt"

	"github.com/andreyvit/evm/kvo/codec/row2"
	"github.com/andreyvit/evm/values"
)

func main() {
	w := row2.AcquireWriter(nil)
	defer w.Release()
	w.Begin(0, "", row2.Object, row2.Fast2)
	w.Put(1, "", values.Uint(42), row2.Word)
	w.End()
	row := row2.Open(w.BytesWritten())
	fmt.Println(row.Get(1, row2.Word).AsUint())
}
Output:
42

func AcquireWriter

func AcquireWriter(dst []byte) *Writer

AcquireWriter returns a writer with reusable directory scratch. dst and the encoded bytes belong to the caller, including after Release.

func (*Writer) Begin

func (w *Writer) Begin(key uint64, text string, kind Kind, layout Layout)

Begin opens a container, nested under the given key when a parent is open.

func (*Writer) BytesWritten

func (w *Writer) BytesWritten() []byte

BytesWritten returns the destination bytes after all containers have been closed. It panics if a frame remains open.

func (*Writer) Count

func (w *Writer) Count() int

Count reports entries emitted in the currently open container.

func (*Writer) End

func (w *Writer) End()

End finishes the innermost container using normal empty-child omission.

func (*Writer) Finish

func (w *Writer) Finish(keepEmpty bool)

Finish closes a container, optionally preserving an explicitly empty child.

func (*Writer) Put

func (w *Writer) Put(key uint64, text string, v values.Value, enc Encoding)

Put writes a field using enc and the container's normal zero-omission rules.

func (*Writer) PutPresent

func (w *Writer) PutPresent(key uint64, text string, v values.Value, enc Encoding)

PutPresent writes a field even when its value is zero.

func (*Writer) PutRaw

func (w *Writer) PutRaw(key uint64, text string, v Field)

PutRaw preserves an unknown field's representation, including explicit zero.

func (*Writer) Release

func (w *Writer) Release()

Release an acquired writer exactly once. It returns directory scratch to the pool. Discard unusually large workspaces so one wide or deeply nested row cannot pin them between writes.

func (*Writer) ReserveEntries

func (w *Writer) ReserveEntries(n int)

ReserveEntries is optional capacity advice, not a count or framing promise. A source slice/model can supply it without sizing or encoding any child.

func (*Writer) Reset

func (w *Writer) Reset(dst []byte)

Reset discards partial frames and starts appending to dst.

Jump to

Keyboard shortcuts

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