db

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2025 License: Apache-2.0, UNKNOWN, UNKNOWN not legal advice Imports: 0 Imported by: 0

README

DB

Database abstractions to be used in applications. These abstractions are not only meant to be used in applications built on Tendermint, but can be used in a variety of applications.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InternalRegisterDBCreator

func InternalRegisterDBCreator(backend BackendType, creator dbCreator, force bool)

InternalRegisterDBCreator is used by the init functions of imported databases to register their own dbCreators.

This function is not meant for usage outside of db/.

func IsKeyInDomain

func IsKeyInDomain(key, start, end []byte) bool

See DB interface documentation for more information.

Types

type BackendType

type BackendType string
const (
	// GoLevelDBBackend represents goleveldb (github.com/syndtr/goleveldb - most
	// popular implementation)
	//   - stable
	GoLevelDBBackend BackendType = "goleveldb"

	// PebbleDBBackend represents pebble (github.com/cockroachdb/pebble)
	//   - stable
	PebbleDBBackend BackendType = "pebbledb"

	// MemDBBackend represents in-memory key value store, which is mostly used
	// for testing.
	MemDBBackend BackendType = "memdb"

	// BoltDBBackend represents bolt (uses etcd's fork of bolt -
	// go.etcd.io/bbolt)
	//   - EXPERIMENTAL
	//   - may be faster is some use-cases (random reads - indexer)
	BoltDBBackend BackendType = "boltdb"
)

These are valid backend types.

The backends themselves must be imported to be used (ie. using the blank import, `import _ "github.com/gnolang/gno/tm2/pkg/db/goleveldb"`). To allow for end-user customization at build time, the package "github.com/gnolang/gno/tm2/pkg/db/_tags" can be imported -- this package will import each database depending on whether its build tag is provided.

This can be used in conjunction with specific to provide defaults, for instance:

package main

import (
	"github.com/gnolang/gno/tm2/pkg/db"
	_ "github.com/gnolang/gno/tm2/pkg/db/_tags" // allow user to customize with build tags
	_ "github.com/gnolang/gno/tm2/pkg/db/memdb" // always support memdb
)

func main() {
	db.NewDB("mydb", db.BackendType(userProvidedBackend), "./data")
}

func BackendList

func BackendList() []BackendType

BackendList returns a list of available db backends. The list is sorted.

func (BackendType) String

func (b BackendType) String() string

type Batch

type Batch interface {
	// Set sets a key/value pair.
	// CONTRACT: key, value readonly []byte
	Set(key, value []byte) error

	// Delete deletes a key/value pair.
	// CONTRACT: key readonly []byte
	Delete(key []byte) error

	// Write writes the batch, possibly without flushing to disk. Only Close() can be called after,
	// other methods will error.
	Write() error

	// WriteSync writes the batch and flushes it to disk. Only Close() can be called after, other
	// methods will error.
	WriteSync() error

	// Close closes the batch. It is idempotent, but calls to other methods afterwards will error.
	Close() error

	// GetByteSize that returns the current size of the batch in bytes. Depending on the implementation,
	// this may return the size of the underlying LSM batch, including the size of additional metadata
	// on top of the expected key and value total byte count.
	GetByteSize() (int, error)
}

Batch represents a group of writes. They may or may not be written atomically depending on the backend. Callers must call Close on the batch when done.

Batch Close must be called when the program no longer needs the object.

type DB

type DB interface {
	// Get returns nil iff key doesn't exist.
	// A nil key is interpreted as an empty byteslice.
	// CONTRACT: key, value readonly []byte
	Get([]byte) ([]byte, error)

	// Has checks if a key exists.
	// A nil key is interpreted as an empty byteslice.
	// CONTRACT: key, value readonly []byte
	Has(key []byte) (bool, error)

	// Set sets the key.
	// A nil key is interpreted as an empty byteslice.
	// CONTRACT: key, value readonly []byte
	Set([]byte, []byte) error
	SetSync([]byte, []byte) error

	// Delete deletes the key.
	// A nil key is interpreted as an empty byteslice.
	// CONTRACT: key readonly []byte
	Delete([]byte) error
	DeleteSync([]byte) error

	// Iterate over a domain of keys in ascending order. End is exclusive.
	// Start must be less than end, or the Iterator is invalid.
	// A nil start is interpreted as an empty byteslice.
	// If end is nil, iterates up to the last item (inclusive).
	// CONTRACT: No writes may happen within a domain while an iterator exists over it.
	// CONTRACT: start, end readonly []byte
	Iterator(start, end []byte) (Iterator, error)

	// Iterate over a domain of keys in descending order. End is exclusive.
	// Start must be less than end, or the Iterator is invalid.
	// If start is nil, iterates up to the first/least item (inclusive).
	// If end is nil, iterates from the last/greatest item (inclusive).
	// CONTRACT: No writes may happen within a domain while an iterator exists over it.
	// CONTRACT: start, end readonly []byte
	ReverseIterator(start, end []byte) (Iterator, error)

	// Closes the connection.
	Close() error

	// Creates a batch for atomic updates.
	NewBatch() Batch

	// NewBatchWithSize create a new batch for atomic updates, but with pre-allocated size.
	// This will does the same thing as NewBatch if the batch implementation doesn't support pre-allocation.
	NewBatchWithSize(int) Batch

	// For debugging
	Print() error

	// Stats returns a map of property values for all keys and the size of the cache.
	Stats() map[string]string
}

DBs are goroutine safe.

func NewDB

func NewDB(name string, backend BackendType, dir string) (DB, error)

NewDB creates a new database of type backend with the given name. NOTE: function panics if:

  • backend is unknown (not registered)
  • creator function, provided during registration, returns error

type ImmutableDB

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

func NewImmutableDB

func NewImmutableDB(db DB) *ImmutableDB

NewImmutableDB wraps a db to make it immutable. ImmutableDB panics on mutation operations.

func (*ImmutableDB) Close

func (idb *ImmutableDB) Close() error

Implements DB.

func (*ImmutableDB) Delete

func (idb *ImmutableDB) Delete(key []byte) error

Implements DB.

func (*ImmutableDB) DeleteSync

func (idb *ImmutableDB) DeleteSync(key []byte) error

Implements DB.

func (*ImmutableDB) Get

func (idb *ImmutableDB) Get(key []byte) ([]byte, error)

Implements DB.

func (*ImmutableDB) Has

func (idb *ImmutableDB) Has(key []byte) (bool, error)

Implements DB.

func (*ImmutableDB) Iterator

func (idb *ImmutableDB) Iterator(start, end []byte) (Iterator, error)

Implements DB.

func (*ImmutableDB) NewBatch

func (idb *ImmutableDB) NewBatch() Batch

Implements DB.

func (*ImmutableDB) NewBatchWithSize

func (idb *ImmutableDB) NewBatchWithSize(_ int) Batch

Implements DB.

func (*ImmutableDB) Print

func (idb *ImmutableDB) Print() error

Implements DB.

func (*ImmutableDB) ReverseIterator

func (idb *ImmutableDB) ReverseIterator(start, end []byte) (Iterator, error)

Implements DB.

func (*ImmutableDB) Set

func (idb *ImmutableDB) Set(key []byte, value []byte) error

Implements DB.

func (*ImmutableDB) SetSync

func (idb *ImmutableDB) SetSync(key []byte, value []byte) error

Implements DB.

func (*ImmutableDB) Stats

func (idb *ImmutableDB) Stats() map[string]string

Implements DB.

type Iterator

type Iterator interface {
	// Domain returns the start (inclusive) and end (exclusive) limits of the iterator.
	Domain() (start, end []byte)

	// Valid returns whether the current iterator is valid. Once invalid, the Iterator remains
	// invalid forever.
	Valid() bool

	// Next moves the iterator to the next key in the database, as defined by order of iteration.
	// If Valid returns false, this method will panic.
	Next()

	// Key returns the key at the current position. Panics if the iterator is invalid.
	// Note, the key returned should be a copy and thus safe for modification.
	Key() []byte

	// Value returns the value at the current position. Panics if the iterator is
	// invalid.
	// Note, the value returned should be a copy and thus safe for modification.
	Value() []byte

	// Error returns the last error encountered by the iterator, if any.
	Error() error

	// Close closes the iterator, releasing any allocated resources.
	Close() error
}

Usage:

var itr Iterator = ... defer itr.Close()

for ; itr.Valid(); itr.Next() {
	k, v := itr.Key(); itr.Value()
	// ...
}

Iterator represents an iterator over a domain of keys. Callers must call Close when done. No writes can happen to a domain while there exists an iterator over it. Some backends may take out database locks to ensure this will not happen.

Callers must make sure the iterator is valid before calling any methods on it, otherwise these methods will panic.

func IteratePrefix

func IteratePrefix(db DB, prefix []byte) Iterator

IteratePrefix is a convenience function for iterating over a key domain restricted by prefix.

type PrefixDB

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

func NewPrefixDB

func NewPrefixDB(db DB, prefix []byte) *PrefixDB

NewPrefixDB lets you namespace multiple DBs within a single DB.

func (*PrefixDB) Close

func (pdb *PrefixDB) Close() error

Implements DB.

func (*PrefixDB) Delete

func (pdb *PrefixDB) Delete(key []byte) error

Implements DB.

func (*PrefixDB) DeleteSync

func (pdb *PrefixDB) DeleteSync(key []byte) error

Implements DB.

func (*PrefixDB) Get

func (pdb *PrefixDB) Get(key []byte) ([]byte, error)

Implements DB.

func (*PrefixDB) Has

func (pdb *PrefixDB) Has(key []byte) (bool, error)

Implements DB.

func (*PrefixDB) Iterator

func (pdb *PrefixDB) Iterator(start, end []byte) (Iterator, error)

Implements DB.

func (*PrefixDB) Mutex

func (pdb *PrefixDB) Mutex() *sync.Mutex

Implements atomicSetDeleter.

func (*PrefixDB) NewBatch

func (pdb *PrefixDB) NewBatch() Batch

Implements DB. Panics if the underlying DB is not an atomicSetDeleter.

func (*PrefixDB) NewBatchWithSize

func (pdb *PrefixDB) NewBatchWithSize(size int) Batch

Implements DB. Panics if the underlying DB is not an atomicSetDeleter.

func (*PrefixDB) Print

func (pdb *PrefixDB) Print() error

Implements DB.

func (*PrefixDB) ReverseIterator

func (pdb *PrefixDB) ReverseIterator(start, end []byte) (Iterator, error)

Implements DB.

func (*PrefixDB) Set

func (pdb *PrefixDB) Set(key []byte, value []byte) error

Implements DB.

func (*PrefixDB) SetSync

func (pdb *PrefixDB) SetSync(key []byte, value []byte) error

Implements DB.

func (*PrefixDB) Stats

func (pdb *PrefixDB) Stats() map[string]string

Implements DB.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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