database

package
v0.2.1-beta.2 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package database defines interfaces to key value type databases used by various components in go-spacemesh node

Index

Constants

View Source
const IdealBatchSize = 100 * 1024

IdealBatchSize is the best batch size Code using batches should try to add this much data to the batch. The value was determined empirically.

Variables

View Source
var Create = DBC.CreateRealDB

Create is the actual function that is used to create a DB without the need to know which DB specifically, this can be determined by the context

View Source
var DBC = ContextDBCreator{
	Path:    "../tmp/test/" + time.Now().String(),
	Context: "",
}

DBC is the global context that determines what db will be created and at what path.

View Source
var ErrNotFound = errors.ErrNotFound

ErrNotFound is special type error for not found in DB

Functions

func SwitchCreationContext added in v0.1.15

func SwitchCreationContext(path, context string)

SwitchCreationContext switches real DB creation context to allow creating the same DB for multiple nodes in one process

func SwitchToMemCreationContext added in v0.1.15

func SwitchToMemCreationContext()

SwitchToMemCreationContext switches global DB creation function to create memory DBs (this function should be called in tests 'init' function)

Types

type Batch

type Batch interface {
	Putter
	Deleter
	ValueSize() int // amount of data in the batch
	Write() error
	// Reset resets the batch for reuse
	Reset()
}

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

func NewTableBatch

func NewTableBatch(db Database, prefix string) Batch

NewTableBatch returns a Batch object which prefixes all keys with a given string.

type ContextDBCreator added in v0.1.15

type ContextDBCreator struct {
	Create  func(file string, cache int, handles int, logger log.Log) (Database, error)
	Path    string
	Context string
}

ContextDBCreator is a global structure that toggles creation of real dbs and memory dbs for tests

func (ContextDBCreator) CreateMemDB added in v0.1.15

func (c ContextDBCreator) CreateMemDB(file string, cache int, handles int, logger log.Log) (Database, error)

CreateMemDB is a wrapper function that creates a memory database to be used only in tests

func (ContextDBCreator) CreateRealDB added in v0.1.15

func (c ContextDBCreator) CreateRealDB(file string, cache int, handles int, logger log.Log) (Database, error)

CreateRealDB is a wrapper function that creates a leveldb database

type Database

type Database interface {
	Putter
	Deleter
	Get(key []byte) ([]byte, error)
	Has(key []byte) (bool, error)
	Close()
	NewBatch() Batch
	Find(key []byte) Iterator
}

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

func NewTable

func NewTable(db Database, prefix string) Database

NewTable returns a Database object that prefixes all keys with a given string.

type Deleter

type Deleter interface {
	Delete(key []byte) error
}

Deleter wraps the database delete operation supported by both batches and regular databases.

type Getter

type Getter interface {
	Get(key []byte) ([]byte, error)
}

Getter wraps the database read operation.

type Iterator

type Iterator interface {
	iterator.IteratorSeeker
	Key() []byte
	Value() []byte
}

Iterator defined basic iterator interface

type LDBDatabase

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

LDBDatabase is a wrapper for leveldb database with concurrent access

func NewLDBDatabase

func NewLDBDatabase(file string, cache int, handles int, logger log.Log) (*LDBDatabase, error)

NewLDBDatabase returns a LevelDB wrapped object.

func (*LDBDatabase) Close

func (db *LDBDatabase) Close()

Close closes database, flushing writes and denying all new write requests

func (*LDBDatabase) Delete

func (db *LDBDatabase) Delete(key []byte) error

Delete deletes the key from the queue and database

func (*LDBDatabase) Find

func (db *LDBDatabase) Find(key []byte) Iterator

Find returns iterator to iterate over values with given prefix key

func (*LDBDatabase) Get

func (db *LDBDatabase) Get(key []byte) ([]byte, error)

Get returns the given key if it's present.

func (*LDBDatabase) Has

func (db *LDBDatabase) Has(key []byte) (bool, error)

Has returns whether the db contains the key

func (*LDBDatabase) Iterator

func (db *LDBDatabase) Iterator() iterator.Iterator

Iterator returns iterator iterating over all database keys

func (*LDBDatabase) LDB

func (db *LDBDatabase) LDB() *leveldb.DB

LDB returns the actual inner leveldb struct refrence

func (*LDBDatabase) Meter

func (db *LDBDatabase) Meter(prefix string)

Meter configures the database metrics collectors and

func (*LDBDatabase) NewBatch

func (db *LDBDatabase) NewBatch() Batch

NewBatch creates a new batch write struct, able to add multiple values in a single operation

func (*LDBDatabase) NewIterator

func (db *LDBDatabase) NewIterator() iterator.Iterator

NewIterator creates a new leveldb iterator struct compliant with Iterator interface

func (*LDBDatabase) NewIteratorWithPrefix

func (db *LDBDatabase) NewIteratorWithPrefix(prefix []byte) iterator.Iterator

NewIteratorWithPrefix returns a iterator to iterate over subset of database content with a particular prefix.

func (*LDBDatabase) Path

func (db *LDBDatabase) Path() string

Path returns the path to the database directory.

func (*LDBDatabase) Put

func (db *LDBDatabase) Put(key []byte, value []byte) error

Put puts the given key / value to the queue

type MemDatabase

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

MemDatabase is a test memory database. Do not use for any production it does not get persisted

func NewMemDatabase

func NewMemDatabase() *MemDatabase

NewMemDatabase returns a memory database instance

func (*MemDatabase) Close

func (db *MemDatabase) Close()

Close closes the database

func (*MemDatabase) Delete

func (db *MemDatabase) Delete(key []byte) error

Delete removes the key from db

func (*MemDatabase) Find

func (db *MemDatabase) Find(key []byte) Iterator

Find returns iterator iterating items with given key as prefix

func (*MemDatabase) Get

func (db *MemDatabase) Get(key []byte) ([]byte, error)

Get gets the value for the given key, returns an error if key wasn't found

func (*MemDatabase) Has

func (db *MemDatabase) Has(key []byte) (bool, error)

Has returns a boolean if key is in db or not

func (*MemDatabase) Iterator

func (db *MemDatabase) Iterator() Iterator

Iterator returns iterator for memory database iterating all items in database

func (*MemDatabase) Keys

func (db *MemDatabase) Keys() [][]byte

Keys returns all keys found in database

func (*MemDatabase) Len

func (db *MemDatabase) Len() int

Len returns number of items in database

func (*MemDatabase) NewBatch

func (db *MemDatabase) NewBatch() Batch

NewBatch returns batch object to aggregate writes to db

func (*MemDatabase) NewMemDatabaseIterator

func (db *MemDatabase) NewMemDatabaseIterator() *MemDatabaseIterator

NewMemDatabaseIterator iterator for memory database iterating all items in database

func (*MemDatabase) Put

func (db *MemDatabase) Put(key []byte, value []byte) error

Put inserts value value by provided key

type MemDatabaseIterator

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

MemDatabaseIterator is an iterator for memory database

func (*MemDatabaseIterator) Error

func (iter *MemDatabaseIterator) Error() error

Error is a stub to comply with DB interface

func (*MemDatabaseIterator) First

func (iter *MemDatabaseIterator) First() bool

First moves the iterator to first object

func (*MemDatabaseIterator) Key

func (iter *MemDatabaseIterator) Key() []byte

Key returns the key of the current item iterator is pointing at

func (*MemDatabaseIterator) Last

func (iter *MemDatabaseIterator) Last() bool

Last moves the iterator to last object

func (*MemDatabaseIterator) Next

func (iter *MemDatabaseIterator) Next() bool

Next advances iterator to next item

func (*MemDatabaseIterator) Prev

func (iter *MemDatabaseIterator) Prev() bool

Prev moves the iterator one item back

func (*MemDatabaseIterator) Release

func (iter *MemDatabaseIterator) Release()

Release is a stub to comply with DB interface

func (*MemDatabaseIterator) Seek

func (iter *MemDatabaseIterator) Seek(key []byte) bool

Seek returns true if key is found in iterator object

func (*MemDatabaseIterator) Value

func (iter *MemDatabaseIterator) Value() []byte

Value returns the value of the current item iterator is pointing at

type Putter

type Putter interface {
	Put(key []byte, value []byte) error
}

Putter wraps the database write operation supported by both batches and regular databases.

type Store

type Store interface {
	Putter
	Getter
}

Store wraps basic get and put operations, All methods are safe for concurrent use.

Jump to

Keyboard shortcuts

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