database

package
v1.7.10 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2022 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// If, when a batch is reset, the cap(batch)/len(batch) > MaxExcessCapacityFactor,
	// the underlying array's capacity will be reduced by a factor of capacityReductionFactor.
	// Higher value for MaxExcessCapacityFactor --> less aggressive array downsizing --> less memory allocations
	// but more unnecessary data in the underlying array that can't be garbage collected.
	// Higher value for CapacityReductionFactor --> more aggressive array downsizing --> more memory allocations
	// but less unnecessary data in the underlying array that can't be garbage collected.
	MaxExcessCapacityFactor = 4
	CapacityReductionFactor = 2
)

Variables

View Source
var (
	// Benchmarks is a list of all database benchmarks
	Benchmarks = []func(b *testing.B, db Database, name string, keys, values [][]byte){
		BenchmarkGet,
		BenchmarkPut,
		BenchmarkDelete,
		BenchmarkBatchPut,
		BenchmarkBatchDelete,
		BenchmarkBatchWrite,
		BenchmarkParallelGet,
		BenchmarkParallelPut,
		BenchmarkParallelDelete,
	}
	// BenchmarkSizes to use with each benchmark
	BenchmarkSizes = [][]int{

		{1024, 32, 32},
		{1024, 256, 256},
		{1024, 2 * units.KiB, 2 * units.KiB},
	}
)
View Source
var (
	ErrClosed   = errors.New("closed")
	ErrNotFound = errors.New("not found")
)

common errors

Tests is a list of all database tests

Functions

func BenchmarkBatchDelete added in v1.7.6

func BenchmarkBatchDelete(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkBatchDelete measures the time it takes to batch delete.

func BenchmarkBatchPut added in v1.7.6

func BenchmarkBatchPut(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkBatchPut measures the time it takes to batch put.

func BenchmarkBatchWrite added in v1.7.6

func BenchmarkBatchWrite(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkBatchWrite measures the time it takes to batch write.

func BenchmarkDelete added in v1.7.6

func BenchmarkDelete(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkDelete measures the time it takes to delete a (k, v) from a database.

func BenchmarkGet added in v1.7.6

func BenchmarkGet(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkGet measures the time it takes to get an operation from a database.

func BenchmarkParallelDelete added in v1.7.6

func BenchmarkParallelDelete(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkParallelDelete measures the time it takes to delete a (k, v) from the db.

func BenchmarkParallelGet added in v1.7.6

func BenchmarkParallelGet(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkParallelGet measures the time it takes to read in parallel.

func BenchmarkParallelPut added in v1.7.6

func BenchmarkParallelPut(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkParallelPut measures the time it takes to write to the db in parallel.

func BenchmarkPut added in v1.7.6

func BenchmarkPut(b *testing.B, db Database, name string, keys, values [][]byte)

BenchmarkPut measures the time it takes to write an operation to a database.

func Clear added in v1.7.6

func Clear(readerDB Iteratee, deleterDB KeyValueDeleter) error

func ClearPrefix added in v1.7.6

func ClearPrefix(readerDB Iteratee, deleterDB KeyValueDeleter, prefix []byte) error

func Count added in v1.7.6

func Count(db Iteratee) (int, error)

func GetBool added in v1.7.6

func GetBool(db KeyValueReader, key []byte) (bool, error)

func GetID added in v1.7.6

func GetID(db KeyValueReader, key []byte) (ids.ID, error)

func GetTimestamp added in v1.7.6

func GetTimestamp(db KeyValueReader, key []byte) (time.Time, error)

func GetUInt32 added in v1.7.6

func GetUInt32(db KeyValueReader, key []byte) (uint32, error)

func GetUInt64 added in v1.7.6

func GetUInt64(db KeyValueReader, key []byte) (uint64, error)

func PackUInt32 added in v1.7.6

func PackUInt32(val uint32) []byte

func PackUInt64 added in v1.7.6

func PackUInt64(val uint64) []byte

func ParseID added in v1.7.6

func ParseID(b []byte) (ids.ID, error)

func ParseTimestamp added in v1.7.6

func ParseTimestamp(b []byte) (time.Time, error)

func ParseUInt32 added in v1.7.6

func ParseUInt32(b []byte) (uint32, error)

func ParseUInt64 added in v1.7.6

func ParseUInt64(b []byte) (uint64, error)

func PutBool added in v1.7.6

func PutBool(db KeyValueWriter, key []byte, b bool) error

func PutID added in v1.7.6

func PutID(db KeyValueWriter, key []byte, val ids.ID) error

func PutTimestamp added in v1.7.6

func PutTimestamp(db KeyValueWriter, key []byte, val time.Time) error

func PutUInt32 added in v1.7.6

func PutUInt32(db KeyValueWriter, key []byte, val uint32) error

func PutUInt64 added in v1.7.6

func PutUInt64(db KeyValueWriter, key []byte, val uint64) error

func SetupBenchmark added in v1.7.6

func SetupBenchmark(b *testing.B, count int, keySize, valueSize int) ([][]byte, [][]byte)

Writes size data into the db in order to setup reads in subsequent tests.

func Size added in v1.7.6

func Size(db Iteratee) (int, error)

func TestBatchDelete

func TestBatchDelete(t *testing.T, db Database)

TestBatchDelete tests to make sure that batched deletes work as expected.

func TestBatchInner

func TestBatchInner(t *testing.T, db Database)

TestBatchInner tests to make sure that inner can be used to write to the database.

func TestBatchLargeSize added in v1.7.6

func TestBatchLargeSize(t *testing.T, db Database)

TestBatchLargeSize tests to make sure that the batch can support a large amount of entries.

func TestBatchPut

func TestBatchPut(t *testing.T, db Database)

TestBatchPut tests to make sure that batched writes work as expected.

func TestBatchReplay

func TestBatchReplay(t *testing.T, db Database)

TestBatchReplay tests to make sure that batches will correctly replay their contents.

func TestBatchReset

func TestBatchReset(t *testing.T, db Database)

TestBatchReset tests to make sure that a batch drops un-written operations when it is reset.

func TestBatchReuse

func TestBatchReuse(t *testing.T, db Database)

TestBatchReuse tests to make sure that a batch can be reused once it is reset.

func TestBatchRewrite

func TestBatchRewrite(t *testing.T, db Database)

TestBatchRewrite tests to make sure that write can be called multiple times on a batch and the values will be updated correctly.

func TestClear added in v1.7.6

func TestClear(t *testing.T, db Database)

TestClear tests to make sure the deletion helper works as expected.

func TestClearPrefix added in v1.7.6

func TestClearPrefix(t *testing.T, db Database)

TestClearPrefix tests to make sure prefix deletion works as expected.

func TestCompactNoPanic

func TestCompactNoPanic(t *testing.T, db Database)

TestCompactNoPanic tests to make sure compact never panics.

func TestIterator

func TestIterator(t *testing.T, db Database)

TestIterator tests to make sure the database iterates over the database contents lexicographically.

func TestIteratorClosed

func TestIteratorClosed(t *testing.T, db Database)

TestIteratorClosed tests to make sure that an iterator that was created with a closed database will report a closed error correctly.

func TestIteratorError added in v1.7.6

func TestIteratorError(t *testing.T, db Database)

TestIteratorError tests to make sure that an iterator on a database will report itself as being exhausted and return ErrClosed to indicate that the iteration was not successful. Additionally tests that an iterator that has already called Next() can still serve its current value after the underlying DB was closed.

func TestIteratorErrorAfterRelease added in v1.7.6

func TestIteratorErrorAfterRelease(t *testing.T, db Database)

TestIteratorErrorAfterRelease tests to make sure that an iterator that was released still reports the error correctly.

func TestIteratorMemorySafety added in v1.7.6

func TestIteratorMemorySafety(t *testing.T, db Database)

TestIteratorMemorySafety tests to make sure that keys can values are able to be modified from the returned iterator.

func TestIteratorPrefix

func TestIteratorPrefix(t *testing.T, db Database)

TestIteratorPrefix tests to make sure the iterator can be configured to skip keys missing the provided prefix.

func TestIteratorSnapshot added in v1.7.6

func TestIteratorSnapshot(t *testing.T, db Database)

TestIteratorSnapshot tests to make sure the database iterates over a snapshot of the database at the time of the iterator creation.

func TestIteratorStart

func TestIteratorStart(t *testing.T, db Database)

TestIteratorStart tests to make sure the the iterator can be configured to start mid way through the database.

func TestIteratorStartPrefix

func TestIteratorStartPrefix(t *testing.T, db Database)

TestIteratorStartPrefix tests to make sure that the iterator can start mid way through the database while skipping a prefix.

func TestKeyEmptyValue added in v1.7.6

func TestKeyEmptyValue(t *testing.T, db Database)

func TestMemorySafetyBatch added in v1.7.6

func TestMemorySafetyBatch(t *testing.T, db Database)

TestMemorySafetyDatabase ensures it is safe to modify a key after passing it to Batch.Put.

func TestMemorySafetyDatabase added in v1.7.6

func TestMemorySafetyDatabase(t *testing.T, db Database)

TestMemorySafetyDatabase ensures it is safe to modify a key after passing it to Database.Put and Database.Get.

func TestSimpleKeyValue

func TestSimpleKeyValue(t *testing.T, db Database)

TestSimpleKeyValue tests to make sure that simple Put + Get + Delete + Has calls return the expected values.

func TestSimpleKeyValueClosed

func TestSimpleKeyValueClosed(t *testing.T, db Database)

TestSimpleKeyValueClosed tests to make sure that Put + Get + Delete + Has calls return the correct error when the database has been closed.

func TestStatNoPanic

func TestStatNoPanic(t *testing.T, db Database)

TestStatNoPanic tests to make sure that Stat never panics.

Types

type Batch

type Batch interface {
	KeyValueWriterDeleter

	// Size retrieves the amount of data queued up for writing, this includes
	// the keys, values, and deleted keys.
	Size() int

	// Write flushes any accumulated data to disk.
	Write() error

	// Reset resets the batch for reuse.
	Reset()

	// Replay replays the batch contents in the same order they were written
	// to the batch.
	Replay(w KeyValueWriterDeleter) error

	// Inner returns a Batch writing to the inner database, if one exists. If
	// this batch is already writing to the base DB, then itself should be
	// returned.
	Inner() Batch
}

Batch is a write-only database that commits changes to its host database when Write is called. A batch cannot be used concurrently.

type Batcher

type Batcher interface {
	// NewBatch creates a write-only database that buffers changes to its host db
	// until a final write is called.
	NewBatch() Batch
}

Batcher wraps the NewBatch method of a backing data store.

type Compacter

type Compacter interface {
	// Compact the underlying DB for the given key range.
	// Specifically, deleted and overwritten versions are discarded,
	// and the data is rearranged to reduce the cost of operations
	// needed to access the data. This operation should typically only
	// be invoked by users who understand the underlying implementation.
	//
	// A nil start is treated as a key before all keys in the DB.
	// And a nil limit is treated as a key after all keys in the DB.
	// Therefore if both are nil then it will compact entire DB.
	Compact(start []byte, limit []byte) error
}

Compacter wraps the Compact method of a backing data store.

type Database

Database contains all the methods required to allow handling different key-value data stores backing the database.

type Iteratee

type Iteratee interface {
	// NewIterator creates an iterator over the entire keyspace contained within
	// the key-value database.
	NewIterator() Iterator

	// NewIteratorWithStart creates an iterator over a subset of database
	// content starting at a particular initial key.
	NewIteratorWithStart(start []byte) Iterator

	// NewIteratorWithPrefix creates an iterator over a subset of database
	// content with a particular key prefix.
	NewIteratorWithPrefix(prefix []byte) Iterator

	// NewIteratorWithStartAndPrefix creates an iterator over a subset of
	// database content with a particular key prefix starting at a specified
	// key.
	NewIteratorWithStartAndPrefix(start, prefix []byte) Iterator
}

Iteratee wraps the NewIterator methods of a backing data store.

type Iterator

type Iterator interface {
	// Next moves the iterator to the next key/value pair. It returns whether
	// the iterator successfully moved to a new key/value pair.
	// The iterator may return false if the underlying database has been closed
	// before the iteration has completed, in which case future calls to Error()
	// must return [ErrClosed].
	Next() bool

	// Error returns any accumulated error. Exhausting all the key/value pairs
	// is not considered to be an error.
	// Error should be called after all key/value pairs have been exhausted ie.
	// after Next() has returned false.
	Error() error

	// Key returns the key of the current key/value pair, or nil if done.
	Key() []byte

	// Value returns the value of the current key/value pair, or nil if done.
	Value() []byte

	// Release releases associated resources. Release should always succeed and
	// can be called multiple times without causing error.
	Release()
}

Iterator iterates over a database's key/value pairs.

When it encounters an error any seek will return false and will yield no key/ value pairs. The error can be queried by calling the Error method. Calling Release is still necessary.

An iterator must be released after use, but it is not necessary to read an iterator until exhaustion. An iterator is not safe for concurrent use, but it is safe to use multiple iterators concurrently.

type KeyValueDeleter added in v1.7.6

type KeyValueDeleter interface {
	// Delete removes the key from the key-value data store.
	Delete(key []byte) error
}

KeyValueDeleter wraps the Delete method of a backing data store.

type KeyValueReader

type KeyValueReader interface {
	// Has retrieves if a key is present in the key-value data store.
	Has(key []byte) (bool, error)

	// Get retrieves the given key if it's present in the key-value data store.
	Get(key []byte) ([]byte, error)
}

KeyValueReader wraps the Has and Get method of a backing data store.

type KeyValueReaderWriter added in v1.7.6

type KeyValueReaderWriter interface {
	KeyValueReader
	KeyValueWriter
}

KeyValueReaderWriter allows read/write acccess to a backing data store.

type KeyValueReaderWriterDeleter added in v1.7.6

type KeyValueReaderWriterDeleter interface {
	KeyValueReader
	KeyValueWriter
	KeyValueDeleter
}

KeyValueReaderWriterDeleter allows read/write/delete access to a backing data store.

type KeyValueWriter

type KeyValueWriter interface {
	// Put inserts the given value into the key-value data store.
	Put(key []byte, value []byte) error
}

KeyValueWriter wraps the Put method of a backing data store.

type KeyValueWriterDeleter added in v1.7.6

type KeyValueWriterDeleter interface {
	KeyValueWriter
	KeyValueDeleter
}

KeyValueWriterDeleter allows write/delete acccess to a backing data store.

type Stater

type Stater interface {
	// Stat returns a particular internal stat of the database.
	Stat(property string) (string, error)
}

Stater wraps the Stat method of a backing data store.

Directories

Path Synopsis
^ Only build this file if this computer is not Linux OR it's not AMD64 OR rocksdb is not allowed Copyright (C) 2019-2021, Ava Labs, Inc.
^ Only build this file if this computer is not Linux OR it's not AMD64 OR rocksdb is not allowed Copyright (C) 2019-2021, Ava Labs, Inc.

Jump to

Keyboard shortcuts

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