db

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: AGPL-3.0 Imports: 3 Imported by: 27

Documentation

Index

Constants

View Source
const DefaultBatchMaxSize uint = 1024 * 1024

DefaultBatchMaxSize is the default value for maxSize used in NewBatch. Assuming keys and values of 64 bytes, which should be common in our use case, this value allows for 128 MiB of key-values which should be a nice tradeoff between performance and memory consumption.

View Source
const (
	// TypePebble defines the type of db that uses PebbleDB
	TypePebble = "pebble"
)

Variables

View Source
var ErrConflict = fmt.Errorf("txn conflict")

ErrConflict is returned when a transaction conflicts with another transaction. This can happen if the read rows had been updated concurrently by another transaction.

View Source
var ErrKeyNotFound = fmt.Errorf("key not found")

ErrKeysNotFound is used to indicate that a key does not exist in the db.

View Source
var ErrTxnTooBig = fmt.Errorf("txn too big")

ErrTxnTooBig is used to indicate that a WriteTx is too big and can't include more writes.

Functions

This section is empty.

Types

type Batch

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

Batch wraps a WriteTx to automatically commit when it becomes too big (either the Tx write operation returns ErrTxnTooBig or the write count reaches the specified maxSize), causing a new internal WriteTx to be created.

func NewBatch added in v1.3.0

func NewBatch(db Database) *Batch

NewBatch creates a new Batch from a Database with a default maxSize.

func NewBatchMaxSize added in v1.3.0

func NewBatchMaxSize(db Database, maxSize uint) *Batch

NewBatchMaxSize creates a new Batch from a Database with the specified maxSize.

func (*Batch) Apply added in v1.3.0

func (t *Batch) Apply(other WriteTx) (err error)

Apply implements the WriteTx.Apply interface method

func (*Batch) Commit added in v1.3.0

func (t *Batch) Commit() error

Commit implements the WriteTx.Commit interface method. Notice that this method also commits the wrapped WriteTx. Notice that this method will only commit the writes of the last interal tx: if the tx previously has become too big a commit will have already been applied to the previous tx transparently during a Set or Delete.

func (*Batch) Delete added in v1.3.0

func (t *Batch) Delete(key []byte) (err error)

Delete implements the WriteTx.Delete interface method. If during this operation, the internal tx becomes too big, all the pending writes will be committed and a new WriteTx will be created to continue with this and future writes.

func (*Batch) Discard added in v1.3.0

func (t *Batch) Discard()

Discard implements the ReadTx.Discard interface method. Notice that this method will only discard the writes of the last interal tx: if the tx previously has become too big a commit will have been applied that can't be discarded.

func (*Batch) Get added in v1.3.0

func (t *Batch) Get(key []byte) ([]byte, error)

Get implements the WriteTx.Get interface method

func (*Batch) Set added in v1.3.0

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

Set implements the WriteTx.Set interface method. If during this operation, the internal tx becomes too big, all the pending writes will be committed and a new WriteTx will be created to continue with this and future writes.

func (*Batch) Unwrap added in v1.3.0

func (t *Batch) Unwrap() WriteTx

Unwrap returns the wrapped WriteTx

type Database

type Database interface {
	io.Closer

	// ReadTx creates a new read transaction.
	ReadTx() ReadTx
	// WriteTx creates a new write transaction.
	WriteTx() WriteTx

	// Iterate calls callback with all key-value pairs in the database whose key
	// starts with prefix. The calls are ordered lexicographically by key.
	//
	// The iteration is stopped early when the callback function returns false.
	//
	// It is not safe to use the key or value slices after the callback returns.
	// To use the values for longer, make a copy.
	Iterate(prefix []byte, callback func(key, value []byte) bool) error
}

Database wraps all database operations. All methods are safe for concurrent use.

type Options added in v1.3.0

type Options struct {
	Path string
}

Options defines generic parameters for creating a new Database.

type ReadTx added in v1.3.0

type ReadTx interface {
	// Get retrieves the value for the given key. If the key does not
	// exist, returns the error ErrKeyNotFound
	Get(key []byte) ([]byte, error)
	// Discard discards the transaction. This method can be called always,
	// even if previously the Tx has been Commited (for the WriteTx case).
	// So it's a good practice to `defer tx.Discard()` just after creating
	// the tx.
	Discard()
}

type WriteTx added in v1.3.0

type WriteTx interface {
	ReadTx

	// Set adds a key-value pair. If the key already exists, its value is
	// updated.
	Set(key []byte, value []byte) error
	// Delete deletes a key and its value.
	Delete(key []byte) error
	// Apply applies the value-passed WriteTx into the given WriteTx,
	// copying the key-values from the original WriteTx into the one from
	// which the method is called.
	// TODO Review once generics are ready: WriteTx is an interface, so
	// Apply internally needs type assertions, revisit this once generics
	// are ready.
	Apply(WriteTx) error
	// Commit commits the transaction into the db
	Commit() error
}

func UnwrapWriteTx added in v1.3.0

func UnwrapWriteTx(tx WriteTx) WriteTx

UnwrapWriteTx unwraps (if possible) the WriteTx using Unwrap method

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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