Skip to main content

General characteristics

Juno defines a database as an object that can store key-value pairs in a persistent way. Keys and values are arrays of bytes, that permit storing any value that can be serialized as a batch of bytes, and the same happens with the keys. Juno does not define the way the database must be initialized but defines what are the operations a database needs, which are:

  • Has: checks if the key already exists on the database.
  • Get: returns the associated value to the given key if the key exists on the database.
  • Put: stores a new value associated with the given key. If the key already exists, the value is overwritten.
  • Delete: deletes the given key from the database and its associated value.
  • NumberOfItems: returns the number of items (keys) on the database.

All of those operations are described by this interface in the code:

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)
}

Juno defines a database as the union of the above operations and a Close function to cleanly close the database, described by this interface in the code:

type Database interface {
DatabaseOperations
Close()
}

Transactions

A transactional database is a database that supports all the database functionalities and a new functionality called RunTxn to run a batch of operations in the database inside a unique transaction. This behavior is described by this interface in the code:

type DatabaseTransactional interface {
Database
RunTxn(DatabaseTxOp) error
}

type DatabaseTxOp func(txn DatabaseOperations) error

RunTXn receives a function that has a unique param that can do all the database operations, those operations are committed only if the DatabaseTxOp function ends without error.

MDBX Database

Now Juno uses an MDBX database to store all its data. An MDBX database is an environment (a database file) that can have multiple scopes or named databases. With that characteristic, Juno has only one MDBX environment and multiple named databases, one for each database on the application.

To know more about the MDBX Database and the package Juno uses, see these links: