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

records

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: 6 Imported by: 0

Documentation

Overview

Package records defines the ordered record store an IndexedDB object store provides, with a memory implementation that simulates power loss.

Every call is one IndexedDB transaction and one round trip. A commit is atomic. A durable commit maps to the strict durability hint and returns once it and every earlier commit are durable; any other commit maps to the relaxed hint, and a crash may lose it along with every later commit.

Index

Constants

This section is empty.

Variables

View Source
var ErrCrashed = errors.New("record store crashed")

ErrCrashed reports a call on a memory store after its injected crash.

Functions

This section is empty.

Types

type Memory

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

Memory is an in-memory Store that simulates power loss. It keeps the durable records as of the last durable commit and the commits made since, so PowerLoss can keep any prefix of them.

func NewMemory

func NewMemory() *Memory

NewMemory returns an empty memory store that never crashes.

func (*Memory) Calls

func (m *Memory) Calls() int

Calls returns the number of Commit calls made.

func (*Memory) Commit

func (m *Memory) Commit(ctx context.Context, ops []Op, durable bool) error

Commit applies ops atomically, injecting the crash when it is due. The crashing commit joins the pending ones, so PowerLoss may keep it.

func (*Memory) CrashAfter

func (m *Memory) CrashAfter(n int)

CrashAfter crashes the store after n further commits succeed. The crashing commit may or may not apply, and every later call fails with ErrCrashed until PowerLoss. A negative n never crashes.

func (*Memory) Get

func (m *Memory) Get(ctx context.Context, keys [][]byte) ([][]byte, error)

Get reads the records of keys, nil for an absent key.

func (*Memory) Has

func (m *Memory) Has(ctx context.Context, keys [][]byte) ([]bool, error)

Has reports which keys have records.

func (*Memory) PowerLoss

func (m *Memory) PowerLoss(rng *rand.Rand)

PowerLoss replaces the records with an image a power loss could leave: the durable records and a prefix of the later commits, as rng chooses. The store then accepts calls again.

func (*Memory) Scan

func (m *Memory) Scan(ctx context.Context, prefix []byte, fn func(key, value []byte) error) error

Scan calls fn with each record whose key has prefix, in key order, over the records as of the call.

func (*Memory) Stats

func (m *Memory) Stats() Stats

Stats returns the counts so far.

type Op

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

Op is one change of a commit.

type Stats

type Stats struct {
	// Reads counts Get, Has, and Scan calls.
	Reads int
	// Commits counts Commit calls.
	Commits int
	// Durable counts durable Commit calls.
	Durable int
	// Bytes counts the key and value bytes committed.
	Bytes int64
}

Stats counts a store's transactions.

type Store

type Store interface {
	// Get reads the records of keys, nil for an absent key.
	Get(ctx context.Context, keys [][]byte) ([][]byte, error)
	// Has reports which keys have records without reading them.
	Has(ctx context.Context, keys [][]byte) ([]bool, error)
	// Scan calls fn with each record whose key has prefix, in key order. fn
	// may retain the key and value.
	Scan(ctx context.Context, prefix []byte, fn func(key, value []byte) error) error
	// Commit applies ops atomically in order, durably if durable is set. The
	// store does not retain ops.
	Commit(ctx context.Context, ops []Op, durable bool) error
}

Store is an ordered record store.

Directories

Path Synopsis
Package recordstest checks a records.Store against its contract, so native tests and browser fixtures run the same checks on every implementation.
Package recordstest checks a records.Store against its contract, so native tests and browser fixtures run the same checks on every implementation.

Jump to

Keyboard shortcuts

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