caching

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBatchClosed = fmt.Errorf("batch has been written or closed")
View Source
var ErrKeyEmpty = fmt.Errorf("key cannot be empty")
View Source
var ErrMemtableFull = fmt.Errorf("memtable full")
View Source
var ErrMemtableValueLogPointers = errors.New("memtable value-log pointers require WAL/value-log enabled")
View Source
var ErrSplitValueLog = errors.New("split value log requires WAL/value-log enabled")
View Source
var ErrUnsafeOptions = fmt.Errorf("unsafe options require AllowUnsafe")
View Source
var ErrValueNil = fmt.Errorf("value cannot be nil")

Functions

func SetIteratorDebug

func SetIteratorDebug(enabled bool)

SetIteratorDebug toggles attaching debug metadata to iterators returned by CachingDB.Iterator. It is intended for benchmarking/diagnostics.

Types

type BackendDB

type BackendDB interface {
	Get(key []byte) ([]byte, error)
	GetUnsafe(key []byte) ([]byte, error)
	GetAppend(key, dst []byte) ([]byte, error)
	Has(key []byte) (bool, error)
	Iterator(start, end []byte) (iterator.UnsafeIterator, error)
	ReverseIterator(start, end []byte) (iterator.UnsafeIterator, error)
	NewBatch() batch.Interface
	Close() error
	Print() error
	Stats() map[string]string
}

BackendDB defines the subset of treedb.DB needed by CachingDB.

type Batch

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

func (*Batch) Close

func (b *Batch) Close() error

func (*Batch) Delete

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

func (*Batch) DeleteView

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

DeleteView records a Delete without copying key bytes. Callers must treat key as immutable until the batch is written or closed.

func (*Batch) GetByteSize

func (b *Batch) GetByteSize() (int, error)

func (*Batch) Replay

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

func (*Batch) Reset

func (b *Batch) Reset()

Reset clears the batch for reuse without closing it.

This intentionally keeps internal buffers to avoid per-batch allocations in callers that frequently reset (e.g. geth benchmarks).

func (*Batch) Set

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

func (*Batch) SetOps

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

func (*Batch) SetView

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

SetView records a Put without copying key/value bytes. Callers must treat key/value as immutable until the batch is written or closed.

func (*Batch) Write

func (b *Batch) Write() error

func (*Batch) WriteSync

func (b *Batch) WriteSync() error

type DB

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

func Open

func Open(dir string, backend BackendDB, opts Options) (*DB, error)

func (*DB) Checkpoint

func (db *DB) Checkpoint() error

Checkpoint forces a durable backend boundary and trims the WAL so long-running cached-mode runs do not accumulate unbounded `wal/` growth.

It blocks writers while it:

  • rotates the current mutable memtable (if non-empty),
  • rotates to a fresh WAL segment,
  • flushes all queued memtables with backend sync,
  • forces a backend sync boundary (even if the queue is empty),
  • removes all older WAL segments (keeping only the currently-open one).

func (*DB) Close

func (db *DB) Close() error

func (*DB) CompactionAssist

func (db *DB) CompactionAssist()

CompactionAssist performs bounded flush work when backpressure triggers. It is intended to be called by background maintenance (e.g. slab compaction) so that flush debt does not grow unbounded in the absence of foreground writes.

func (*DB) Delete

func (db *DB) Delete(key []byte) error

func (*DB) DeleteRange

func (db *DB) DeleteRange(start, end []byte) error

DeleteRange deletes all keys in the range [start, end).

When WAL is disabled and the backend is empty, a full-range delete can be satisfied by clearing the in-memory layers without enumerating keys.

func (*DB) DeleteSync

func (db *DB) DeleteSync(key []byte) error

func (*DB) Drain

func (db *DB) Drain() error

Drain flushes all currently buffered writes (mutable + queued memtables) to the backend. It is intended for maintenance operations that require a fully materialized backend state (e.g. index vacuum).

Drain does not provide mutual exclusion against concurrent writers; callers should ensure no writes occur concurrently if they require a fully drained state.

func (*DB) Get

func (db *DB) Get(key []byte) ([]byte, error)

Get returns a safe copy of the value.

func (*DB) GetAppend

func (db *DB) GetAppend(key, dst []byte) ([]byte, error)

GetAppend appends the value for the key to dst and returns the new slice. If the key is not found, it returns dst and ErrKeyNotFound.

func (*DB) GetUnsafe

func (db *DB) GetUnsafe(key []byte) ([]byte, error)

GetUnsafe returns a safe copy of the value.

func (*DB) Has

func (db *DB) Has(key []byte) (bool, error)

func (*DB) Iterator

func (db *DB) Iterator(start, end []byte) (merging.Iterator, error)

Iterator implements DB.Iterator

func (*DB) NewBatch

func (db *DB) NewBatch() *Batch

func (*DB) NewBatchWithSize

func (db *DB) NewBatchWithSize(size int) *Batch

func (*DB) Print

func (db *DB) Print() error

func (*DB) QueueBacklogBytes

func (db *DB) QueueBacklogBytes() int64

QueueBacklogBytes returns the current queued memtable backlog in bytes.

func (*DB) ReverseIterator

func (db *DB) ReverseIterator(start, end []byte) (merging.Iterator, error)

func (*DB) Set

func (db *DB) Set(key, value []byte) error

func (*DB) SetSync

func (db *DB) SetSync(key, value []byte) error

func (*DB) StartAutoCheckpoint

func (db *DB) StartAutoCheckpoint(interval time.Duration, maxWALBytes int64, idleInterval time.Duration)

StartAutoCheckpoint enables a background loop that periodically forces a durable boundary and trims cached-mode WAL segments. When idleInterval > 0, it also triggers an opportunistic checkpoint after a period of write-idleness.

interval > 0 enables periodic checkpoints. maxWALBytes is a safety cap: if > 0, the loop will attempt to checkpoint when the effective WAL bytes exceed this cap. maxWALBytes <= 0 disables the size trigger.

This does not make each individual write durable; it bounds the window of unsynced writes for long-running workloads.

func (*DB) Stats

func (db *DB) Stats() map[string]string

func (*DB) TriggerAutoCheckpoint

func (db *DB) TriggerAutoCheckpoint()

TriggerAutoCheckpoint schedules a best-effort immediate auto-checkpoint pass.

func (*DB) TriggerFlush

func (db *DB) TriggerFlush()

TriggerFlush schedules a background flush pass (best-effort).

type Options

type Options struct {
	FlushThreshold int64

	// MemtableMode selects the in-memory write buffer implementation.
	// Supported: "skiplist", "hash_sorted", "btree", "adaptive".
	// Use "adaptive" or "adaptive:<mode>" to switch per-rotation based on workload.
	MemtableMode string

	// MemtableShards controls the number of mutable memtable shards. Values <= 0
	// use a default derived from GOMAXPROCS. The count is rounded down to a power
	// of two.
	MemtableShards int

	// Legacy backpressure knob: queue length limit.
	// 0 uses the default (4). <0 disables writer backpressure entirely.
	MaxQueuedMemtables int

	// Adaptive backpressure knobs (seconds/bytes). If any of these are non-zero,
	// the caching layer uses backlog-bytes thresholds instead of queue length.
	SlowdownBacklogSeconds float64
	StopBacklogSeconds     float64
	MaxBacklogBytes        int64

	// Writer flush assist limits when backpressure triggers.
	WriterFlushMaxMemtables int
	WriterFlushMaxDuration  time.Duration

	// FlushBuildConcurrency controls how many goroutines may be used to build a
	// combined flush batch from multiple immutable memtables. Values <= 1 disable
	// parallelism.
	FlushBuildConcurrency int

	// DisableWAL disables the Write-Ahead Log.
	DisableWAL bool
	// DisableValueLog forces the cached WAL to remain in legacy mode (no value-log pointers).
	DisableValueLog bool
	// SplitValueLog stores WAL records in wal/ while large values go to vlog/
	// segments, and WAL entries reference them via pointers.
	SplitValueLog bool
	// WALMaxSegmentBytes caps the size of a single WAL segment payload.
	// 0 uses the default limit.
	WALMaxSegmentBytes int64
	// RelaxedSync disables fsync on Sync operations.
	RelaxedSync bool
	// MemtableValueLogPointers avoids storing large values in the memtable and
	// serves them by pointer from the value log (WAL/vlog). Requires WAL/value-log.
	MemtableValueLogPointers bool
	// ValueLogPointerThreshold controls when WAL/vlog pointers are used.
	// Values <= 0 use the default inline threshold (256 bytes).
	ValueLogPointerThreshold int
	// DisableReadChecksum skips CRC verification on value-log reads.
	DisableReadChecksum bool
	// AllowUnsafe acknowledges unsafe durability options.
	// When false, Open will reject DisableWAL or RelaxedSync.
	AllowUnsafe bool
	// MaxValueLogRetainedBytes emits a warning when retained value-log bytes exceed
	// this threshold (0 disables warnings).
	MaxValueLogRetainedBytes int64
	// MaxValueLogRetainedBytesHard disables value-log pointers for new large
	// values once retained bytes exceed this threshold (0 disables the cap).
	MaxValueLogRetainedBytesHard int64

	// NotifyError is an optional hook for background maintenance failures.
	NotifyError func(error)
}

Jump to

Keyboard shortcuts

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