db

package
v0.2.70 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 13 Imported by: 5

Documentation

Overview

package db implements Bolt based database which can be encrypted on the fly and which supports automatic backups. It offers very simple API and hides all the complex stuff behind it. It's thread safe. More information see the Cfg struct.

Index

Constants

View Source
const MEM_PREFIX = "MEMORY_"

Variables

View Source
var ErrDisabledDB = errors.New("database is turned off")

Functions

func AddKeyValueToBucket

func AddKeyValueToBucket(bucket []byte, keyValue, index *Data) (err error)

AddKeyValueToBucket add value to bucket pointed by the index. keyValue and index use Data type's operators to encrypt and hash data on the fly.

func Backup

func Backup() (did bool, err error)

Backup takes backup copy of the database. Before backup the database is closed automatically and only dirty databases are backed up.

func BackupTicker

func BackupTicker(interval time.Duration) (done chan<- struct{})

BackupTicker creates a backup ticker which takes backup copy of the database file specified by the interval. Ticker can be stopped with returned done channel.

func Close

func Close() (err error)

Close closes the database. It can be used after that if wanted. Transactions opens the database when needed.

func GetAllValuesFromBucket added in v0.1.31

func GetAllValuesFromBucket(
	bucket []byte,
	transforms ...Filter,
) (
	values [][]byte,
	err error,
)

GetAllValuesFromBucket returns all entries from the bucket. Note: - Order is not guaranteed. - The returned slice contains only the values as byte arrays. Keys are excluded. Transform functions can be used e.g. to decrypt the data. They are applied in the provided order. Errors will return only if it cannot perform the transaction successfully.

func GetKeyValueFromBucket

func GetKeyValueFromBucket(
	bucket []byte,
	index, keyValue *Data,
) (
	found bool,
	err error,
)

GetKeyValueFromBucket writes keyValue data by the index from a bucket. It returns `found` if key value exists. Errors will return only if it cannot perform the transaction successfully.

func GracefulStop added in v0.2.25

func GracefulStop()

GracefulStop closes all database instances immediately.

func Init

func Init(cfg Cfg) (err error)

Init initializes managed version of the encrypted database. Database is ready to use after this call. See more information of Cfg struct.

func RmKeyValueFromBucket added in v0.1.21

func RmKeyValueFromBucket(bucket []byte, index *Data) (err error)

RmKeyValueFromBucket removes value pointed by the index from the bucket. The index uses Data type's operators to encrypt and hash data on the fly.

func Wipe

func Wipe() (err error)

Wipe removes the whole database and its master file.

Types

type Cfg

type Cfg struct {
	// Filename is full path file name of the DB file. Note, if the base
	// of the filename starts with MEM_PREFIX, the memory database is created.
	// That's useful e.g. testing and profiling.
	Filename string

	// Base part of the backup file names. Date and time is added.
	BackupName string

	// Buckets is slice of the bucket names that are in byte slice.
	Buckets [][]byte
}

Cfg is configuration needed to create and open managed database that is implemented with Bolt DB or by memory maps for testing and profiling. See Filename for more information.

type Data

type Data struct {
	Data  []byte
	Read  Filter
	Write Filter
	Use
	Result interface{}
}

Data is general data element for encrypted database. It offers placeholders for read, write, and use operators to over write.

type Filter

type Filter func(value []byte) (k []byte)

type Handle added in v0.1.47

type Handle interface {
	AddKeyValueToBucket(bucket []byte, keyValue, index *Data) (err error)
	RmKeyValueFromBucket(bucket []byte, index *Data) (err error)
	GetKeyValueFromBucket(bucket []byte, index, keyValue *Data) (found bool, err error)
	GetAllValuesFromBucket(bucket []byte, transforms ...Filter) (values [][]byte, err error)
	BackupTicker(interval time.Duration) (done chan<- struct{})
	Backup() (did bool, err error)
	Wipe() (err error)
	Close() (err error)
	SetStatusFn(f OnFn)
}

func New added in v0.1.21

func New(cfg Cfg) Handle

New creates a new managed and encrypted database. This is a preferred way to use the managed database package. There is also the alternated Init function when you don't need to store the Mgd instance by yourself. It's for the cases when only one managed database is needed per a process or an application. Database is ready to use after this call. You don't need to open it and backup can be taken during the run. See more information of Cfg struct.

func NewMemDB added in v0.1.47

func NewMemDB(bucketNames [][]byte, a ...string) Handle

NewMemDB creates new memory database. The memory DB has same interface (Handle) as the normal Bolt DB, but instead of writing data to file it leaves into the memory. The DB is meant for the tests and performance measurements.

type Mgd

type Mgd struct {
	Cfg
	// contains filtered or unexported fields
}

Mgd is a managed and encrypted (option, can be pre-procession as well) DB.

func (*Mgd) AddKeyValueToBucket added in v0.1.21

func (db *Mgd) AddKeyValueToBucket(bucket []byte, keyValue, index *Data) (err error)

AddKeyValueToBucket add value to bucket pointed by the index. keyValue and index use Data type's operators to encrypt and hash data on the fly.

func (*Mgd) Backup added in v0.1.21

func (db *Mgd) Backup() (did bool, err error)

Backup takes backup copy of the database. Before backup the database is closed automatically and only dirty databases are backed up.

func (*Mgd) BackupTicker added in v0.1.21

func (db *Mgd) BackupTicker(interval time.Duration) (done chan<- struct{})

BackupTicker creates a backup ticker which takes backup copy of the database file specified by the interval. Ticker can be stopped with returned done channel.

func (*Mgd) Close added in v0.1.21

func (db *Mgd) Close() (err error)

Close closes the database. It can be used after that if wanted. Transactions opens the database when needed.

func (*Mgd) GetAllValuesFromBucket added in v0.1.31

func (db *Mgd) GetAllValuesFromBucket(
	bucket []byte,
	transforms ...Filter,
) (
	values [][]byte,
	err error,
)

GetAllValuesFromBucket returns all entries from the bucket. Note: - Order is not guaranteed. - The returned slice contains only the values as byte arrays. Keys are excluded. Transform functions can be used e.g. to decrypt the data. They are applied in the provided order. Errors will return only if it cannot perform the transaction successfully.

func (*Mgd) GetKeyValueFromBucket added in v0.1.21

func (db *Mgd) GetKeyValueFromBucket(
	bucket []byte,
	index, keyValue *Data,
) (
	found bool,
	err error,
)

GetKeyValueFromBucket writes keyValue data by the index from a bucket. It returns `found` if key value exists. Errors will return only if it cannot perform the transaction successfully.

func (*Mgd) RmKeyValueFromBucket added in v0.1.21

func (db *Mgd) RmKeyValueFromBucket(bucket []byte, index *Data) (err error)

RmKeyValueFromBucket removes value pointed by the index from the bucket. The index uses Data type's operators to encrypt and hash data on the fly.

func (*Mgd) SetStatusFn added in v0.2.25

func (db *Mgd) SetStatusFn(f OnFn)

func (*Mgd) Wipe added in v0.1.21

func (db *Mgd) Wipe() (err error)

Wipe removes the whole database and its master file.

type OnFn added in v0.2.25

type OnFn func() bool

OnFn is call back to status of the database: on or off.

type Use

type Use func(value []byte) interface{}

Jump to

Keyboard shortcuts

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