batch

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyEmpty           = errors.New("key cannot be empty")
	ErrBatchClosed        = errors.New("batch closed")
	ErrValueTooLarge      = errors.New("value exceeds inline threshold; use SetPointer")
	ErrMissingValueReader = errors.New("value reader unavailable")
)

Functions

func BuildApplyPlanFromEntries added in v0.6.0

func BuildApplyPlanFromEntries(entries []Entry, hasDeleteRanges bool) ([]Entry, []DeleteRange)

BuildApplyPlanFromEntries canonicalizes an ordered mixed operation stream into sorted final point ops and merged range tombstones over the base tree.

func DeleteRangeContainsKey added in v0.6.0

func DeleteRangeContainsKey(r DeleteRange, key []byte) bool

DeleteRangeContainsKey reports whether key is inside r's half-open range.

func DeleteRangeOverlapsSpan added in v0.6.0

func DeleteRangeOverlapsSpan(r DeleteRange, low, high []byte) bool

DeleteRangeOverlapsSpan reports whether r intersects the half-open child span [low, high). Nil high means unbounded upper; nil r bounds are unbounded.

func DeleteRangesContainKey added in v0.6.0

func DeleteRangesContainKey(ranges []DeleteRange, key []byte) bool

DeleteRangesContainKey reports whether key is covered by any merged or unmerged range in ranges.

func IsDeleteRangeNoop added in v0.6.0

func IsDeleteRangeNoop(start, end []byte) bool

IsDeleteRangeNoop reports whether [start,end) cannot delete any valid TreeDB key. Nil means unbounded; a non-nil end <= non-nil start is empty/reversed.

func Release

func Release(b *Batch)

Release resets and returns the batch to the pool.

Types

type Batch

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

Batch accumulates writes and deletes before committing them.

func Acquire

func Acquire(reader ValueReader, threshold int) *Batch

Acquire returns a reusable Batch from the pool.

func AcquireRetainingLargeEntries added in v0.6.0

func AcquireRetainingLargeEntries(reader ValueReader, threshold int) *Batch

AcquireRetainingLargeEntries returns a reusable Batch from the large-entry pool. See NewRetainingLargeEntries.

func New

func New(reader ValueReader, threshold int) *Batch

New creates a new Batch.

func NewRetainingLargeEntries added in v0.6.0

func NewRetainingLargeEntries(reader ValueReader, threshold int) *Batch

NewRetainingLargeEntries returns a batch from a separate pool that can retain larger entry slices. Use this only for repeated internal materializations with known large entry counts; ordinary batches should use New/Acquire.

func (*Batch) AppendDeleteViewTrustedSortedUnique added in v0.6.0

func (b *Batch) AppendDeleteViewTrustedSortedUnique(key []byte) error

AppendDeleteViewTrustedSortedUnique records a Delete without copying key bytes or invalidating sorted/compacted state. Nil keys are canonicalized to the empty key. Caller must guarantee key remains immutable until commit/close, and appended keys are strictly increasing with no duplicates.

func (*Batch) AppendDeleteViewTrustedSortedUniqueWithRevision added in v0.6.1

func (b *Batch) AppendDeleteViewTrustedSortedUniqueWithRevision(key []byte, revision page.EntryRevision) error

func (*Batch) AppendPointerViewNoTouchTrustedSorted added in v0.4.0

func (b *Batch) AppendPointerViewNoTouchTrustedSorted(key []byte, ptr page.ValuePtr)

AppendPointerViewNoTouchTrustedSorted appends a pointer Put without input validation, touched-segment tracking, or key-order checks. Caller must guarantee: batch is open, key is canonicalized if nil, ptr is a value-log pointer, and appended keys are already non-decreasing.

func (*Batch) AppendPointerViewNoTouchTrustedSortedWithRevision added in v0.6.1

func (b *Batch) AppendPointerViewNoTouchTrustedSortedWithRevision(key []byte, ptr page.ValuePtr, revision page.EntryRevision)

func (*Batch) AppendPointerViewTrustedSortedUnique added in v0.6.0

func (b *Batch) AppendPointerViewTrustedSortedUnique(key []byte, ptr page.ValuePtr) error

AppendPointerViewTrustedSortedUnique records a pointer Put without copying key bytes or invalidating sorted/compacted state. Nil keys are canonicalized to the empty key. Caller must guarantee key remains immutable until commit/close, ptr is a value-log pointer, and appended keys are strictly increasing with no duplicates.

func (*Batch) AppendPointerViewTrustedSortedUniqueWithRevision added in v0.6.1

func (b *Batch) AppendPointerViewTrustedSortedUniqueWithRevision(key []byte, ptr page.ValuePtr, revision page.EntryRevision) error

func (*Batch) AppendViewTrustedSortedUnique added in v0.6.0

func (b *Batch) AppendViewTrustedSortedUnique(key, value []byte) error

AppendViewTrustedSortedUnique records a Put without copying key/value bytes or invalidating the sorted/compacted state. Nil keys/values are canonicalized to zero-length byte strings. Caller must guarantee key/value remain immutable until commit/close, and appended keys are strictly increasing with no duplicates.

func (*Batch) AppendViewTrustedSortedUniqueWithRevision added in v0.6.1

func (b *Batch) AppendViewTrustedSortedUniqueWithRevision(key, value []byte, revision page.EntryRevision) error

func (*Batch) ApplyPlan added in v0.6.0

func (b *Batch) ApplyPlan() ([]Entry, []DeleteRange)

ApplyPlan canonicalizes an ordered mixed batch into sorted final point ops and merged range tombstones over the base tree. The point-only SortedEntries path remains allocation-free and is preferred when HasDeleteRanges is false.

func (*Batch) AssignLegacyPointRevisions added in v0.6.1

func (b *Batch) AssignLegacyPointRevisions(revision page.EntryRevision) page.EntryRevision

AssignLegacyPointRevisions assigns revision to point entries that do not already carry explicit revision metadata. It returns the maximum point revision observed after assignment.

func (*Batch) ByteSize

func (b *Batch) ByteSize() int

ByteSize returns the approximate size of the batch.

func (*Batch) Close

func (b *Batch) Close() error

Close returns the batch to the pool.

func (*Batch) Delete

func (b *Batch) Delete(key []byte) error

Delete adds or replaces a delete operation.

func (*Batch) DeleteRange added in v0.6.0

func (b *Batch) DeleteRange(start, end []byte) error

DeleteRange records a half-open range delete [start, end). Nil bounds are unbounded. Empty or reversed bounded ranges are no-ops.

func (*Batch) DeleteRangeView added in v0.6.0

func (b *Batch) DeleteRangeView(start, end []byte) error

DeleteRangeView records a range delete without copying bound bytes. Callers must treat start/end as immutable until the batch is committed or closed.

func (*Batch) DeleteView

func (b *Batch) DeleteView(key []byte) error

DeleteView is an internal-performance helper that records a Delete without copying the key bytes. Callers must treat key as immutable until the batch is committed (Write/WriteSync) or closed.

func (*Batch) DeleteViewWithRevision added in v0.6.1

func (b *Batch) DeleteViewWithRevision(key []byte, revision page.EntryRevision) error

func (*Batch) DeleteWithRevision added in v0.6.1

func (b *Batch) DeleteWithRevision(key []byte, revision page.EntryRevision) error

func (*Batch) EntriesCap added in v0.4.0

func (b *Batch) EntriesCap() int

EntriesCap returns the current capacity of the internal entry buffer.

func (*Batch) HasDeleteRanges added in v0.6.0

func (b *Batch) HasDeleteRanges() bool

HasDeleteRanges reports whether this batch contains any non-empty range deletes. Point-only callers can keep using SortedEntries' compacted fast path.

func (*Batch) HasValueLogPointers added in v0.4.0

func (b *Batch) HasValueLogPointers() bool

HasValueLogPointers reports whether this batch contains any value-log pointer operations.

func (*Batch) IsEmpty added in v0.6.0

func (b *Batch) IsEmpty() bool

IsEmpty reports whether the batch currently has no queued operations.

func (*Batch) Len added in v0.6.0

func (b *Batch) Len() int

Len returns the number of queued logical operations.

func (*Batch) MaxPointRevision added in v0.6.1

func (b *Batch) MaxPointRevision() page.EntryRevision

MaxPointRevision returns the highest explicit point-entry revision without mutating the batch.

func (*Batch) NoteTouchedValueLogFileID added in v0.4.0

func (b *Batch) NoteTouchedValueLogFileID(fileID uint32)

NoteTouchedValueLogFileID is an internal helper that records a touched value-log segment without appending a batch entry.

func (*Batch) Ops

func (b *Batch) Ops() map[string]Entry

Ops returns the map of operations. WARNING: This constructs the map on demand. Duplicate keys in the batch are resolved so the last one wins.

func (*Batch) OrderedEntries added in v0.6.0

func (b *Batch) OrderedEntries() []Entry

OrderedEntries returns the submission-order operation stream. The returned slice aliases batch-owned storage and must be treated as read-only.

func (*Batch) PointRevisionStats added in v0.6.1

func (b *Batch) PointRevisionStats() (page.EntryRevision, bool)

PointRevisionStats returns the highest point-entry revision and whether any point entry is still legacy-versioned.

func (*Batch) Replay

func (b *Batch) Replay(fn func(Entry) error) error

Replay iterates over the batch entries.

func (*Batch) Reserve added in v0.2.0

func (b *Batch) Reserve(n int)

Reserve grows internal buffers to accommodate roughly n entries without reallocation. It is intended as a best-effort performance hint for high-throughput internal callers (e.g. flush).

func (*Batch) Reset

func (b *Batch) Reset()

Reset clears the batch for reuse without releasing its internal buffers.

func (*Batch) Set

func (b *Batch) Set(key, value []byte) error

Set adds or replaces a key/value operation.

func (*Batch) SetInlineThresholdResolver added in v0.4.0

func (b *Batch) SetInlineThresholdResolver(resolver func([]byte) int)

SetInlineThresholdResolver installs an optional per-key threshold resolver used by Set/SetView/SetOps inline-size checks.

func (*Batch) SetOps

func (b *Batch) SetOps(ops []Entry) error

SetOps merges a slice of operations into the batch.

func (*Batch) SetPointer

func (b *Batch) SetPointer(key []byte, ptr page.ValuePtr) error

SetPointer adds a pointer directly to the batch (used by Compaction).

func (*Batch) SetPointerView added in v0.2.0

func (b *Batch) SetPointerView(key []byte, ptr page.ValuePtr) error

SetPointerView is an internal-performance helper that records a pointer Put without copying the key bytes. Callers must treat key as immutable until the batch is committed (Write/WriteSync) or closed.

This is intentionally not part of the public batch.Interface; it is a best-effort optimization used by higher-level layers (e.g. cached flush streaming).

func (*Batch) SetPointerViewNoTouch added in v0.4.0

func (b *Batch) SetPointerViewNoTouch(key []byte, ptr page.ValuePtr) error

SetPointerViewNoTouch is an internal-performance helper that records a pointer Put without copying key bytes and without tracking touched value-log segments. Callers must separately provide touched segment hints when commit publication needs them.

func (*Batch) SetPointerViewNoTouchWithRevision added in v0.6.1

func (b *Batch) SetPointerViewNoTouchWithRevision(key []byte, ptr page.ValuePtr, revision page.EntryRevision) error

func (*Batch) SetPointerViewWithRevision added in v0.6.1

func (b *Batch) SetPointerViewWithRevision(key []byte, ptr page.ValuePtr, revision page.EntryRevision) error

func (*Batch) SetPointerWithRevision added in v0.6.1

func (b *Batch) SetPointerWithRevision(key []byte, ptr page.ValuePtr, revision page.EntryRevision) error

func (*Batch) SetView

func (b *Batch) SetView(key, value []byte) error

SetView is an internal-performance helper that records a Put without copying key/value bytes. Callers must treat key/value as immutable until the batch is committed (Write/WriteSync) or closed.

func (*Batch) SetViewWithRevision added in v0.6.1

func (b *Batch) SetViewWithRevision(key, value []byte, revision page.EntryRevision) error

func (*Batch) SetWithRevision added in v0.6.1

func (b *Batch) SetWithRevision(key, value []byte, revision page.EntryRevision) error

func (*Batch) SortedEntries

func (b *Batch) SortedEntries() []Entry

SortedEntries returns the operations sorted by key. Duplicate keys are resolved (last write wins). This modifies the internal entries slice (sorts and compacts). The returned slice aliases batch-owned storage and must be treated as read-only. It remains valid only until the batch is mutated, reset, released, or closed.

func (*Batch) TouchedValueLogSegments added in v0.4.0

func (b *Batch) TouchedValueLogSegments() []uint32

TouchedValueLogSegments reports the value-log segments that were touched by pointer puts in this batch. The returned slice is sorted for deterministic commit/publish behavior.

In the small-set fast path, the returned slice aliases internal batch storage and remains valid only until the batch is mutated, reset, or released. Callers must treat the result as read-only.

type DeleteRange added in v0.6.0

type DeleteRange struct {
	Start []byte
	End   []byte
}

DeleteRange describes a half-open range delete [Start, End). Nil Start or End are unbounded on that side.

func MergeDeleteRanges added in v0.6.0

func MergeDeleteRanges(ranges []DeleteRange) []DeleteRange

MergeDeleteRanges returns sorted, non-overlapping ranges equivalent to the union of ranges. It does not copy bound bytes; callers must keep them alive.

type Entry

type Entry struct {
	Type     OpType
	Key      []byte        // Put/Delete key, or DeleteRange start bound (nil means unbounded)
	Value    []byte        // Inline value, or DeleteRange exclusive end bound (nil means unbounded)
	ValuePtr page.ValuePtr // For large values
	IsPtr    bool          // True if ValuePtr is valid
	Revision page.EntryRevision
}

Entry represents a single operation in the batch.

type Interface

type Interface interface {
	Set(key, value []byte) error
	Delete(key []byte) error
	DeleteRange(start, end []byte) error
	SetOps(ops []Entry) error
	Write() error
	WriteSync() error
	Close() error
	Replay(func(Entry) error) error
	GetByteSize() (int, error)
}

Interface defines the contract for a batch operation.

type OpType

type OpType uint8

OpType represents the type of operation recorded in a batch.

const (
	OpPut OpType = iota
	OpDelete
	OpDeleteRange
)

type ValueReader added in v0.3.0

type ValueReader interface {
	Read(ptr page.ValuePtr) ([]byte, error)
	ReadUnsafe(ptr page.ValuePtr) ([]byte, error)
}

ValueReader resolves value pointers for Replay callers that require full values.

Jump to

Keyboard shortcuts

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