db

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2019 License: AGPL-3.0 Imports: 9 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when a key doesn't exist.
	ErrNotFound = errors.New("key does not exist")

	// ErrInvalidBatch is returned when a batch is invalid.
	ErrInvalidBatch = errors.New("batch is invalid")

	// ErrInvalidTransaction is returned when a transaction is invalid.
	ErrInvalidTransaction = errors.New("transaction is invalid")
)

Functions

This section is empty.

Types

type Batch

type Batch interface {
	// Put sets or overwrites the value of a key.
	Put(key, value []byte)

	// Delete removes a key.
	Delete(key []byte)
}

Batch can execute multiple write operations efficiently. It is only intended to be used once. When writing the batch to the database, the operations may execute concurrently, so order is not guaranteed.

type Batcher

type Batcher interface {
	// Batch creates a batch which can be used to execute multiple write
	// operations atomically and efficiently.
	Batch() Batch

	// Write executes all the operations in a batch atomically. It may
	// write concurrently.
	Write(Batch) error
}

Batcher can batch write.

type DB

type DB interface {
	Reader
	Ranger
	Writer
	Batcher
	Transactor

	// Close may close the underlying storage.
	Close() error
}

DB can read and write values to a key-value database.

Implementations should be concurrently safe.

func NewFileDB

func NewFileDB(filename string, o *opt.Options) (DB, error)

NewFileDB creates a new LevelDB key-value database from a LevelDB file.

Options can be nil. LevelDB has some interesting options to look at to improve performance, such as BloomFilter.

func NewLevelDB

func NewLevelDB(ldb *leveldb.DB) DB

NewLevelDB creates a new LevelDB key-value database from an existing LevelDB database instance.

func NewMemDB

func NewMemDB(o *opt.Options) (DB, error)

NewMemDB creates a new in-memory key-value database.

It can be used for testing or for storing small amounts of data, but it will run out of memory quickly otherwise.

type Diff

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

Diff implements ReadWriter and records changes that need to be applied to an underlying database. The recorded changes can then be applied atomically using a batch. It ensures only one write operation per key changed.

func NewDiff

func NewDiff(db ReadWriteBatcher) *Diff

NewDiff creates a new Diff with the given underlying database.

func (*Diff) Apply

func (d *Diff) Apply() error

Apply applies all the recorded changes to the underlying database. It also resets the recorded changes.

func (*Diff) Delete

func (d *Diff) Delete(key []byte) error

Delete records a deleted value.

func (*Diff) Get

func (d *Diff) Get(key []byte) ([]byte, error)

Get returns the recorded value or the value of the underlying database.

func (*Diff) Put

func (d *Diff) Put(key, value []byte) error

Put records an updated value.

func (*Diff) Reset

func (d *Diff) Reset()

Reset resets all the recorded changes.

type Iterator

type Iterator interface {
	// Next returns whether there are keys left.
	Next() (bool, error)

	// Key returns the key of the current entry.
	Key() []byte

	// Value returns the value of the current entry.
	Value() []byte

	// Release needs to be called to free the iterator.
	Release()
}

Iterator iterates over a range of keys in the key-value database.

type Ranger

type Ranger interface {
	// IterateRange creates an iterator that iterates from the given start
	// key (inclusive) up to the given stop key (exclusive). Remember to
	// call Release() on the iterator.
	IterateRange(start, stop []byte) Iterator

	// IteratePrefix creates an iterator that iterates over all the keys
	// that begin with the given prefix. Remember to call Release() on the
	// iterator.
	IteratePrefix(prefix []byte) Iterator
}

Ranger can iterate over ranges.

type ReadWriteBatcher

type ReadWriteBatcher interface {
	ReadWriter
	Batcher
}

ReadWriteBatcher can both read, write, and batch write values to a key-value database.

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

ReadWriter can both read and write values to a key-value database.

type Reader

type Reader interface {
	// Get returns the value of a key. If the key doesn't exist, it returns
	// ErrNotFound (which feels a bit safer than returning nil).
	Get(key []byte) ([]byte, error)
}

Reader reads values from a key-value database.

type Transaction

type Transaction interface {
	Reader
	Ranger
	Writer
	Batcher

	// Commit should be called to commit the transaction to the database.
	Commit() error

	// Discard should be called to discard the transaction.
	Discard()
}

Transaction can be used to execute multiple operations atomically. It should be closed exactly once by calling either Commit or Discard.

type Transactor

type Transactor interface {
	// Transaction creates a transaction which can be used to execute
	// multiple operations atomically.
	Transaction() (Transaction, error)
}

Transactor can create database transactions.

type Writer

type Writer interface {
	// Put sets or overwrites the value of a key.
	Put(key, value []byte) error

	// Delete removes a key. If the key doesn't exist, it is a NOP.
	Delete(key []byte) error
}

Writer writes values to a key-value database.

Jump to

Keyboard shortcuts

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