elldb

package
v0.1.8-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2019 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package elldb provides LevelDB database utility.

Index

Constants

View Source
const (
	// KeyPrefixSeparator is used to separate prefix and key
	KeyPrefixSeparator = "@@"
)

Variables

This section is empty.

Functions

func MakeKey

func MakeKey(key []byte, prefixes ...[]byte) []byte

MakeKey construct a key from the key and prefixes

func MakePrefix

func MakePrefix(prefixes ...[]byte) (result []byte)

MakePrefix creates a prefix string

Types

type DB

type DB interface {

	// Open opens the database
	Open(namespace string) error

	// Close closes the database
	Close() error

	// Put writes many objects to the database in one atomic request
	Put([]*KVObject) error

	// GetByPrefix returns valyes matching a prefix
	GetByPrefix([]byte) (result []*KVObject)

	// GetFirstOrLast returns one value matching a prefix.
	// Set first to return the first value we find or false if the last.
	GetFirstOrLast(prefix []byte, first bool) *KVObject

	// Iterate finds a set of objects by prefix and passes them ro iterFunc
	// for further processing. If iterFunc returns true, the iterator is immediately released.
	// If first is set to true, it begins from the first item, otherwise, the last
	Iterate(prefix []byte, first bool, iterFunc func(kv *KVObject) bool) error

	// DeleteByPrefix deletes one or many records by prefix
	DeleteByPrefix([]byte) error

	// Truncate removes all items
	Truncate() error

	// NewTx creates a transaction
	NewTx() (Tx, error)
}

DB describes the database access, model and available functionalities

type KVObject

type KVObject struct {
	Key    []byte `json:"key"`
	Value  []byte `json:"value"`
	Prefix []byte `json:"prefix"`
}

KVObject represents an item in the elldb

func FromKeyValue

func FromKeyValue(key []byte, value []byte) *KVObject

FromKeyValue takes a key and creates a KVObject

func NewKVObject

func NewKVObject(key, value []byte, prefixes ...[]byte) *KVObject

NewKVObject creates a key value object. The prefixes provided is joined together and prepended to the key before insertion.

func (*KVObject) Equal

func (kv *KVObject) Equal(other *KVObject) bool

Equal performs equality check with another KVObject

func (*KVObject) GetKey

func (kv *KVObject) GetKey() []byte

GetKey creates and returns the key

func (*KVObject) IsEmpty

func (kv *KVObject) IsEmpty() bool

IsEmpty checks whether the object is empty

func (*KVObject) Scan

func (kv *KVObject) Scan(dest interface{}) error

Scan marshals the value into dest

type LevelDB

type LevelDB struct {
	sync.Mutex
	// contains filtered or unexported fields
}

LevelDB provides local data storage and functionalities for various purpose. It implements DB interface

func NewDB

func NewDB(dataDir string) *LevelDB

NewDB creates a new instance of the Ellcrys DB

func (*LevelDB) Close

func (db *LevelDB) Close() error

Close closes the database

func (*LevelDB) DeleteByPrefix

func (db *LevelDB) DeleteByPrefix(prefix []byte) error

DeleteByPrefix deletes items with the matching prefix

func (*LevelDB) GetByPrefix

func (db *LevelDB) GetByPrefix(prefix []byte) []*KVObject

GetByPrefix returns keys matching a prefix. Their key and value are returned

func (*LevelDB) GetFirstOrLast

func (db *LevelDB) GetFirstOrLast(prefix []byte, first bool) *KVObject

GetFirstOrLast returns one value matching a prefix. Set first to return the first value we find or false if the last.

func (*LevelDB) Iterate

func (db *LevelDB) Iterate(prefix []byte, first bool, iterFunc func(kv *KVObject) bool) error

Iterate finds a set of objects and passes them to iterFunc for further processing. If iterFunc returns true, the iteration is discontinued. If first is set to true, iteration begins from the first item, or the last if set to false

func (*LevelDB) NewTx

func (db *LevelDB) NewTx() (Tx, error)

NewTx creates a new transaction

func (*LevelDB) Open

func (db *LevelDB) Open(namespace string) error

Open opens the database. namespace is used as a suffix on the database name

func (*LevelDB) Put

func (db *LevelDB) Put(objs []*KVObject) error

Put writes many objects in one request.

func (*LevelDB) Truncate

func (db *LevelDB) Truncate() error

Truncate deletes all items

type Transaction

type Transaction struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Transaction defines interface for working with a database transaction

func (*Transaction) Commit

func (tx *Transaction) Commit() error

Commit the transaction

func (*Transaction) DeleteByPrefix

func (tx *Transaction) DeleteByPrefix(prefix []byte) error

DeleteByPrefix deletes items with the matching prefix

func (*Transaction) Discard

func (tx *Transaction) Discard()

Discard the transaction

func (*Transaction) GetByPrefix

func (tx *Transaction) GetByPrefix(prefix []byte) []*KVObject

GetByPrefix get objects by prefix

func (*Transaction) Iterate

func (tx *Transaction) Iterate(prefix []byte, first bool, iterFunc func(kv *KVObject) bool)

Iterate finds a set of objects by prefix and passes them ro iterFunc for further processing. If iterFunc returns true, the iterator is immediately released. If first is set to true, it begins from the first item, otherwise, the last

func (*Transaction) Put

func (tx *Transaction) Put(objs []*KVObject) error

Put adds a key and value

func (*Transaction) Rollback

func (tx *Transaction) Rollback()

Rollback discards the transaction

type Tx

type Tx interface {

	// Put puts one or more objects
	Put([]*KVObject) error

	// GetByPrefix gets objects by prefix
	GetByPrefix([]byte) (result []*KVObject)

	// Iterate finds a set of objects by prefix and passes them to iterFunc
	// for further processing. If iterFunc returns true, the iterator is immediately released.
	// If first is set to true, it begins from the first item, otherwise, the last
	Iterate(prefix []byte, first bool, iterFunc func(kv *KVObject) bool)

	// DeleteByPrefix deletes one or many records by prefix
	DeleteByPrefix([]byte) error

	// Commit commits the transaction
	Commit() error

	// Rollback roles back the transaction
	Rollback()

	// Discard the transaction. Do not call
	// functions in the transaction after this.
	Discard()
}

Tx represents a database transaction instance

type TxCreator

type TxCreator interface {
	// NewTx creates a transaction
	NewTx() (Tx, error)
}

TxCreator defines an interface for creating database transaction

Jump to

Keyboard shortcuts

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