Documentation ¶
Overview ¶
Package database defines interfaces to key value type databases used by various components in go-spacemesh node
Index ¶
- Constants
- Variables
- func SwitchCreationContext(path, context string)
- func SwitchToMemCreationContext()
- type Batch
- type ContextDBCreator
- type Database
- type Deleter
- type Getter
- type Iterator
- type LDBDatabase
- func (db *LDBDatabase) Close()
- func (db *LDBDatabase) Delete(key []byte) error
- func (db *LDBDatabase) Find(key []byte) Iterator
- func (db *LDBDatabase) Get(key []byte) ([]byte, error)
- func (db *LDBDatabase) Has(key []byte) (bool, error)
- func (db *LDBDatabase) Iterator() iterator.Iterator
- func (db *LDBDatabase) LDB() *leveldb.DB
- func (db *LDBDatabase) Meter(prefix string)
- func (db *LDBDatabase) NewBatch() Batch
- func (db *LDBDatabase) NewIterator() iterator.Iterator
- func (db *LDBDatabase) NewIteratorWithPrefix(prefix []byte) iterator.Iterator
- func (db *LDBDatabase) Path() string
- func (db *LDBDatabase) Put(key []byte, value []byte) error
- type MemDatabase
- func (db *MemDatabase) Close()
- func (db *MemDatabase) Delete(key []byte) error
- func (db *MemDatabase) Find(key []byte) Iterator
- func (db *MemDatabase) Get(key []byte) ([]byte, error)
- func (db *MemDatabase) Has(key []byte) (bool, error)
- func (db *MemDatabase) Iterator() Iterator
- func (db *MemDatabase) Keys() [][]byte
- func (db *MemDatabase) Len() int
- func (db *MemDatabase) NewBatch() Batch
- func (db *MemDatabase) NewMemDatabaseIterator() *MemDatabaseIterator
- func (db *MemDatabase) Put(key []byte, value []byte) error
- type MemDatabaseIterator
- func (iter *MemDatabaseIterator) Error() error
- func (iter *MemDatabaseIterator) First() bool
- func (iter *MemDatabaseIterator) Key() []byte
- func (iter *MemDatabaseIterator) Last() bool
- func (iter *MemDatabaseIterator) Next() bool
- func (iter *MemDatabaseIterator) Prev() bool
- func (iter *MemDatabaseIterator) Release()
- func (iter *MemDatabaseIterator) Seek(key []byte) bool
- func (iter *MemDatabaseIterator) Value() []byte
- type Putter
- type Store
Constants ¶
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 ¶
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
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.
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 ¶
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.
type Deleter ¶
Deleter wraps the database delete operation supported by both batches and regular databases.
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 ¶
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.
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) 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) 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
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