Documentation
¶
Overview ¶
Package db provides a simple interface to a database. The base behavior is described by the DatabaseOperations, Database and DatabaseTransactional interfaces, any database implementation that match with these interfaces can be used as a Juno database.
Juno uses MDBX as its current database implementation. MDBX has a global environment (the database file) and within it a group of isolated named databases. The environment is initialized with the InitializeMDBXEnv function, then databases can be created with the NewMDBXDatabase function, an MDBXDatabse is a named database within the initialized environment. If the database already exists, the database is opened, otherwise, it is created.
Index ¶
- Variables
- func GetMDBXEnv() (*mdbx.Env, error)
- func InitializeMDBXEnv(path string, optMaxDB uint64, flags uint) (err error)
- func NewMDBXEnv(path string, optMaxDB uint64, flags uint) (*mdbx.Env, error)
- type Database
- type DatabaseOperations
- type DatabaseTransactional
- type DatabaseTxOp
- type MDBXDatabase
- func (x *MDBXDatabase) Close()
- func (x *MDBXDatabase) Delete(key []byte) error
- func (x *MDBXDatabase) Get(key []byte) ([]byte, error)
- func (x *MDBXDatabase) Has(key []byte) (bool, error)
- func (x *MDBXDatabase) NumberOfItems() (uint64, error)
- func (x *MDBXDatabase) Put(key, val []byte) error
- func (x *MDBXDatabase) RunTxn(op DatabaseTxOp) error
- type MDBXTransaction
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInternal represents an internal database error. ErrInternal = errors.New("internal error") // ErrNotFound is used when a key is not found in the database. ErrNotFound = errors.New("not found error") // ErrTxn is returned when a transaction fails for some reason. ErrTxn = errors.New("transaction error") )
var ErrEnvNotInitialized = errors.New("environment is not initialized")
Functions ¶
func GetMDBXEnv ¶
GetMDBXEnv returns the Juno MDBX environment. If the environment is not initialized then ErrEnvNotInitialized is returned.
func InitializeMDBXEnv ¶
InitializeMDBXEnv initializes the Juno MDBX environment.
Types ¶
type Database ¶
type Database interface {
DatabaseOperations
Close()
}
Database represents a database behavior.
type DatabaseOperations ¶
type DatabaseOperations interface {
Has(key []byte) (bool, error)
Get(key []byte) ([]byte, error)
Put(key, value []byte) error
Delete(key []byte) error
NumberOfItems() (uint64, error)
}
DatabaseOperations represents all the core operations needed to store and search values on a key-value database.
type DatabaseTransactional ¶
type DatabaseTransactional interface {
Database
RunTxn(DatabaseTxOp) error
}
DatabaseTransactional represents a database with the possibility of make transactions.
type DatabaseTxOp ¶
type DatabaseTxOp func(txn DatabaseOperations) error
DatabaseTxOp executes all the operations inside the txn function. If the functions returns an error then the transaction is aborted, on another case the transaction is committed.
type MDBXDatabase ¶
type MDBXDatabase struct {
// contains filtered or unexported fields
}
MDBXDatabase is a named database (isolated collection stored on the same file) of a certain environment (database file). One environment can have many named databases. The environment must be initialized and configured with the correct number of named databases before creating the MDBXDatabase instances.
func NewMDBXDatabase ¶
func NewMDBXDatabase(env *mdbx.Env, name string) (*MDBXDatabase, error)
NewMDBXDatabase creates a new MDBXDatabase instance with the given environment and name. If the environment does not have a named database with the given name, it is created and returned, otherwise the existing database is returned.
func (*MDBXDatabase) Close ¶
func (x *MDBXDatabase) Close()
Close closes the database. Notice this function does not close the environment.
func (*MDBXDatabase) Delete ¶
func (x *MDBXDatabase) Delete(key []byte) error
Delete deletes the given key and its value.
func (*MDBXDatabase) Get ¶
func (x *MDBXDatabase) Get(key []byte) ([]byte, error)
Get returns the associated value with the given key. If the key does not exist it returns an ErrNotFound.
func (*MDBXDatabase) Has ¶
func (x *MDBXDatabase) Has(key []byte) (bool, error)
Has searches on the database if the key already exists.
func (*MDBXDatabase) NumberOfItems ¶
func (x *MDBXDatabase) NumberOfItems() (uint64, error)
NumberOfItems returns the number of keys on the database.
func (*MDBXDatabase) Put ¶
func (x *MDBXDatabase) Put(key, val []byte) error
Put store/override the given value on the given key.
func (*MDBXDatabase) RunTxn ¶
func (x *MDBXDatabase) RunTxn(op DatabaseTxOp) error
RunTxn runs a function on a database transaction. If the function returns an error then the transaction is aborted. To guarantee the transaction is thread-safe, the function calls runtime.LockOSThread at the start and runtime.UnlockOSThread at the end.
type MDBXTransaction ¶
type MDBXTransaction struct {
// contains filtered or unexported fields
}
MDBXTransaction represents a transaction of the MDBXDatabase.
func (MDBXTransaction) Delete ¶
func (tx MDBXTransaction) Delete(key []byte) error
Delete deletes the given key and its value.
func (MDBXTransaction) Get ¶
func (tx MDBXTransaction) Get(key []byte) ([]byte, error)
Get returns the associated value with the given key. If the key does not exist the returns ErrNotFound.
func (MDBXTransaction) Has ¶
func (tx MDBXTransaction) Has(key []byte) (bool, error)
Has searches on the database if the key already exists.
func (MDBXTransaction) NumberOfItems ¶
func (tx MDBXTransaction) NumberOfItems() (uint64, error)
func (MDBXTransaction) Put ¶
func (tx MDBXTransaction) Put(key, val []byte) error
Put store/override the given value on the given key.