store

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package store defines the storage abstraction used by keeper. The interface mirrors bbolt's transaction model closely so the bbolt implementation is zero-overhead, while alternative backends (in-memory, etcd, Badger, …) can be swapped in for testing or production.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoltStore

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

BoltStore wraps a bbolt.DB and implements Store.

func Open

func Open(path string) (*BoltStore, error)

Open opens (or creates) a bbolt database at path with mode 0600. A 5-second timeout is applied so Open never blocks indefinitely when another process holds the file lock.

func OpenWithOptions

func OpenWithOptions(path string, opts *bolt.Options) (*BoltStore, error)

OpenWithOptions opens a bbolt database with caller-supplied options. Use this when you need custom page size, read-only mode, etc.

func (*BoltStore) Close

func (s *BoltStore) Close() error

Close implements Store.

func (*BoltStore) DB

func (s *BoltStore) DB() *bolt.DB

DB returns the underlying bbolt.DB for operations not covered by the interface (e.g. bolt.DB.Stats()). Use sparingly — callers that reach through this accessor cannot be swapped to another backend.

func (*BoltStore) Update

func (s *BoltStore) Update(fn func(Tx) error) error

Update implements Store.

func (*BoltStore) View

func (s *BoltStore) View(fn func(Tx) error) error

View implements Store.

type Bucket

type Bucket interface {
	// Get returns the value for key, or nil if the key does not exist.
	// The returned slice is only valid for the lifetime of the transaction.
	Get(key []byte) []byte

	// Put stores key → value.
	Put(key, value []byte) error

	// Delete removes key.
	Delete(key []byte) error

	// Bucket returns a nested bucket by name, or nil if it does not exist.
	Bucket(name []byte) Bucket

	// CreateBucketIfNotExists creates a nested bucket if absent.
	CreateBucketIfNotExists(name []byte) (Bucket, error)

	// DeleteBucket removes a nested bucket and all its contents.
	DeleteBucket(name []byte) error

	// ForEach iterates over all key/value pairs in the bucket.
	// Nested buckets appear with a nil value; use Bucket(k) to open them.
	ForEach(fn func(k, v []byte) error) error

	// NextSequence returns a monotonically increasing integer for the bucket.
	NextSequence() (uint64, error)
}

Bucket is a key/value namespace that may contain nested buckets.

type MemStore

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

MemStore is an in-memory Store implementation backed by nested maps. It is goroutine-safe and is designed for unit tests and embedding use-cases where no persistent file is required.

Limitations: No durable persistence across process restarts. NextSequence values are not snapshotted across transactions. ForEach iteration order is not guaranteed (map order).

func NewMemStore

func NewMemStore() *MemStore

NewMemStore creates an empty in-memory store.

func (*MemStore) Close

func (s *MemStore) Close() error

func (*MemStore) Update

func (s *MemStore) Update(fn func(Tx) error) error

func (*MemStore) View

func (s *MemStore) View(fn func(Tx) error) error

type Store

type Store interface {
	// Update executes fn in a read-write transaction.
	// The transaction is committed if fn returns nil, rolled back otherwise.
	Update(fn func(Tx) error) error

	// View executes fn in a read-only transaction.
	View(fn func(Tx) error) error

	// Close releases all resources held by the store.
	Close() error
}

Store is the top-level database handle.

type Tx

type Tx interface {
	// Bucket returns the named top-level bucket, or nil if it does not exist.
	Bucket(name []byte) Bucket

	// CreateBucketIfNotExists creates the named top-level bucket if absent.
	CreateBucketIfNotExists(name []byte) (Bucket, error)

	// DeleteBucket removes the named top-level bucket and all its contents.
	DeleteBucket(name []byte) error

	// ForEach iterates over all top-level buckets in the transaction.
	ForEach(fn func(name []byte, b Bucket) error) error
}

Tx is a transaction handle.

Jump to

Keyboard shortcuts

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