Help us shape the future of Go! Take the 2026 Go Developer Survey

memtable

package
v0.57.21 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package memtable is an in-memory ordered key-value table with copy-on-write snapshots, which its owner persists through a commit function.

Published tables are never changed, so a read transaction sees one snapshot for its whole life without locks. A write transaction holds the writer lock and changes its own copy, recording each change for the owner to persist.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommitFunc

type CommitFunc func(ctx context.Context, next Snapshot, ops []Op, ordered bool) error

CommitFunc persists ops, which turn the published table into next, before the table publishes next. With ordered set it may persist them with write ordering only, as kvtx.OrderedCommitTx describes. Calls never overlap.

type Op

type Op struct {
	// Key is the key.
	Key []byte
	// Value is the value a set stores.
	Value []byte
	// Delete deletes the key.
	Delete bool
}

Op is one change a write transaction made.

type Snapshot

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

Snapshot is one immutable version of the table.

func (Snapshot) Len

func (s Snapshot) Len() int

Len returns the number of keys.

func (Snapshot) Scan

func (s Snapshot) Scan(fn func(key, value []byte) bool)

Scan calls fn with each key and value in key order until fn returns false. The key and value must not be changed.

type Table

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

Table is the key-value table.

func New

func New(commit CommitFunc) *Table

New returns an empty table that persists write transactions with commit.

func (*Table) Apply

func (t *Table) Apply(ops []Op)

Apply applies ops to the published table. It serves recovery before the table is shared.

func (*Table) Load

func (t *Table) Load(key, value []byte)

Load sets key to value in the published table, keeping both slices. It serves recovery before the table is shared and is fastest in key order.

func (*Table) NewTransaction

func (t *Table) NewTransaction(ctx context.Context, write bool) (kvtx.Tx, error)

NewTransaction opens a transaction on the published table. A write transaction holds the table's writer lock until Commit or Discard.

type Tx

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

Tx is a transaction on one table snapshot. A write transaction changes its own copy of the table and records each change for the commit function.

func (*Tx) Commit

func (t *Tx) Commit(ctx context.Context) error

Commit persists a write transaction's changes and publishes them. A write transaction without changes skips the commit function.

func (*Tx) CommitOrdered

func (t *Tx) CommitOrdered(ctx context.Context) error

CommitOrdered persists a write transaction's changes with write ordering only and publishes them.

func (*Tx) Delete

func (t *Tx) Delete(ctx context.Context, key []byte) error

Delete deletes key.

func (*Tx) Discard

func (t *Tx) Discard()

Discard ends the transaction, dropping uncommitted changes.

func (*Tx) Exists

func (t *Tx) Exists(ctx context.Context, key []byte) (bool, error)

Exists reports whether key is set.

func (*Tx) Get

func (t *Tx) Get(ctx context.Context, key []byte) ([]byte, bool, error)

Get returns the value of key. The value must not be changed.

func (*Tx) Iterate

func (t *Tx) Iterate(ctx context.Context, prefix []byte, sort, reverse bool) kvtx.Iterator

Iterate returns a sorted iterator over the keys with prefix.

func (*Tx) ScanPrefix

func (t *Tx) ScanPrefix(ctx context.Context, prefix []byte, cb func(key, value []byte) error) error

ScanPrefix calls cb with each key with prefix and its value in key order.

func (*Tx) ScanPrefixKeys

func (t *Tx) ScanPrefixKeys(ctx context.Context, prefix []byte, cb func(key []byte) error) error

ScanPrefixKeys calls cb with each key with prefix in key order.

func (*Tx) Set

func (t *Tx) Set(ctx context.Context, key, value []byte) error

Set sets key to a copy of value.

func (*Tx) Size

func (t *Tx) Size(ctx context.Context) (uint64, error)

Size returns the number of keys.

Jump to

Keyboard shortcuts

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