db

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

Database Package

This package has all the managers, one for each type stored on the database. The managers are: abi, state and transaction. Each manager has a protobuf file (abi.proto) to describe the model stored on its database.

You will need protocol buffer compiler to generate the files. For linux, please run the following:

sudo apt install -y protobuf-compiler

After modifying the proto file of a certain manager, you need to generate the Go code for the models again. To do it exists two Make commands:

  • make clean: is used to clean all the files generated during the compilation, including the model files.
  • make generate: generate the files for database models. This command overrides the previously generated files, so make clean is not required.

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

Constants

This section is empty.

Variables

View Source
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")
)
View Source
var ErrEnvNotInitialized = errors.New("environment is not initialized")

Functions

func GetMDBXEnv

func GetMDBXEnv() (*mdbx.Env, error)

GetMDBXEnv returns the Juno MDBX environment. If the environment is not initialized then ErrEnvNotInitialized is returned.

func InitializeMDBXEnv

func InitializeMDBXEnv(path string, optMaxDB uint64, flags uint) (err error)

InitializeMDBXEnv initializes the Juno MDBX environment.

func NewMDBXEnv

func NewMDBXEnv(path string, optMaxDB uint64, flags uint) (*mdbx.Env, error)

NewMDBXEnv creates a new 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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