keydb

package
v0.0.0-...-24e5678 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: LGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package keydb came from buntdb that implements a low-level in-memory key/value store in pure Go. It persists to disk, is ACID compliant, and uses locking for multiple readers and a single writer. Bunt is ideal for projects that need a dependable database, and favor speed over data size.

Package keydb came from buntdb that implements a low-level in-memory key/value store in pure Go. It persists to disk, is ACID compliant, and uses locking for multiple readers and a single writer. Bunt is ideal for projects that need a dependable database, and favor speed over data size.

Package keydb came from buntdb that implements a low-level in-memory key/value store in pure Go. It persists to disk, is ACID compliant, and uses locking for multiple readers and a single writer. Bunt is ideal for projects that need a dependable database, and favor speed over data size.

Index

Constants

View Source
const (
	// Never is used to disable syncing data to disk.
	// The faster and less safe method.
	Never SyncPolicy = 0
	// EverySecond is used to sync data to disk every second.
	// It's pretty fast and you can lose 1 second of data if there
	// is a disaster.
	// This is the recommended setting.
	EverySecond = 1
	// Always is used to sync data after every write to disk.
	// Slow. Very safe.
	Always = 2
)

consts

Variables

View Source
var (
	// ErrTxNotWritable is returned when performing a write operation on a
	// read-only transaction.
	ErrTxNotWritable = errors.New("tx not writable")

	// ErrTxClosed is returned when committing or rolling back a transaction
	// that has already been committed or rolled back.
	ErrTxClosed = errors.New("tx closed")

	// ErrNotFound is returned when an item or index is not in the database.
	ErrNotFound = errors.New("not found")

	// ErrInvalidDatabase is returned when the database file is an invalid format.
	ErrInvalidDatabase = errors.New("invalid database")

	// ErrDatabaseClosed is returned when the database is closed.
	ErrDatabaseClosed = errors.New("database closed")

	// ErrInvalidOperation is returned when an operation cannot be completed.
	ErrInvalidOperation = errors.New("invalid operation")

	// ErrInvalidSyncPolicy is returned for an invalid SyncPolicy value.
	ErrInvalidSyncPolicy = errors.New("invalid sync policy")

	// ErrShrinkInProcess is returned when a shrink operation is in-process.
	ErrShrinkInProcess = errors.New("shrink is in-process")

	// ErrTxIterating is returned when Set or Delete are called while iterating.
	ErrTxIterating = errors.New("tx is iterating")

	ErrNotExistUnmarshaler = errors.New("not exist unmarshaler")
)

errors

Functions

This section is empty.

Types

type Config

type Config struct {
	// SyncPolicy adjusts how often the data is synced to disk.
	// This value can be Never, EverySecond, or Always.
	// The default is EverySecond.
	SyncPolicy SyncPolicy

	// AutoShrinkPercentage is used by the background process to trigger
	// a shrink of the aof file when the size of the file is larger than the
	// percentage of the result of the previous shrunk file.
	// For example, if this value is 100, and the last shrink process
	// resulted in a 100mb file, then the new aof file must be 200mb before
	// a shrink is triggered.
	AutoShrinkPercentage int

	// AutoShrinkMinSize defines the minimum size of the aof file before
	// an automatic shrink can occur.
	AutoShrinkMinSize int

	// AutoShrinkDisabled turns off automatic background shrinking
	AutoShrinkDisabled bool
}

Config represents database configuration options. These options are used to change various behaviors of the database.

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB represents a collection of key-value pairs that persist on disk. Transactions are used for all forms of data access to the DB.

func Open

func Open(path string, fn Unmarshaler) (*DB, error)

Open opens a database at the provided path. If the file does not exist then it will be created automatically.

func (*DB) Begin

func (db *DB) Begin(writable bool) (*Tx, error)

Begin opens a new transaction. Multiple read-only transactions can be opened at the same time but there can only be one read/write transaction at a time. Attempting to open a read/write transactions while another one is in progress will result in blocking until the current read/write transaction is completed.

All transactions must be closed by calling Commit() or Rollback() when done.

func (*DB) Close

func (db *DB) Close() error

Close releases all database resources. All transactions must be closed before closing the database.

func (*DB) ReadConfig

func (db *DB) ReadConfig(config *Config) error

ReadConfig returns the database configuration.

func (*DB) SetConfig

func (db *DB) SetConfig(config Config) error

SetConfig updates the database configuration.

func (*DB) Shrink

func (db *DB) Shrink() error

Shrink will make the database file smaller by removing redundant log entries. This operation does not block the database.

func (*DB) Update

func (db *DB) Update(fn func(tx *Tx) error) error

Update executes a function within a managed read/write transaction. The transaction has been committed when no error is returned. In the event that an error is returned, the transaction will be rolled back. When a non-nil error is returned from the function, the transaction will be rolled back and the that error will be return to the caller of Update().

Executing a manual commit or rollback from inside the function will result in a panic.

func (*DB) View

func (db *DB) View(fn func(tx *Tx) error) error

View executes a function within a managed read-only transaction. When a non-nil error is returned from the function that error will be return to the caller of View().

Executing a manual commit or rollback from inside the function will result in a panic.

type SyncPolicy

type SyncPolicy int

SyncPolicy represents how often data is synced to disk.

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx represents a transaction on the database. This transaction can either be read-only or read/write. Read-only transactions can be used for retrieving values for keys and iterating through keys and values. Read/write transactions can set and delete keys.

All transactions must be committed or rolled-back when done.

func (*Tx) Ascend

func (tx *Tx) Ascend(iterator func(key []byte, value interface{}) bool) error

Ascend calls the iterator for every item in the database within the range [first, last], until iterator returns false. The results will be ordered by the item key.

func (*Tx) AscendEqual

func (tx *Tx) AscendEqual(pivot []byte,
	iterator func(key []byte, value interface{}) bool) error

AscendEqual calls the iterator for every item in the database that equals pivot, until iterator returns false.

func (*Tx) AscendGreaterOrEqual

func (tx *Tx) AscendGreaterOrEqual(pivot []byte,
	iterator func(key []byte, value interface{}) bool) error

AscendGreaterOrEqual calls the iterator for every item in the database within the range [pivot, last], until iterator returns false. The results will be ordered by the item key.

func (*Tx) AscendLessThan

func (tx *Tx) AscendLessThan(pivot []byte,
	iterator func(key []byte, value interface{}) bool) error

AscendLessThan calls the iterator for every item in the database within the range [first, pivot), until iterator returns false. The results will be ordered by the item key.

func (*Tx) AscendRange

func (tx *Tx) AscendRange(greaterOrEqual, lessThan []byte,
	iterator func(key []byte, value interface{}) bool) error

AscendRange calls the iterator for every item in the database within the range [greaterOrEqual, lessThan), until iterator returns false. The results will be ordered by the item key.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit writes all changes to disk. An error is returned when a write error occurs, or when a Commit() is called from a read-only transaction.

func (*Tx) Delete

func (tx *Tx) Delete(key []byte) error

Delete removes an item from the database based on the item's key. If the item does not exist then ErrNotFound is returned.

Only a writable transaction can be used for this operation. This operation is not allowed during iterations such as Ascend* & Descend*.

func (*Tx) DeleteAll

func (tx *Tx) DeleteAll() error

DeleteAll deletes all items from the database.

func (*Tx) Descend

func (tx *Tx) Descend(iterator func(key []byte, value interface{}) bool) error

Descend calls the iterator for every item in the database within the range [last, first], until iterator returns false. The results will be ordered by the item key.

func (*Tx) DescendEqual

func (tx *Tx) DescendEqual(pivot []byte,
	iterator func(key []byte, value interface{}) bool) error

DescendEqual calls the iterator for every item in the database that equals pivot, until iterator returns false.

func (*Tx) DescendGreaterThan

func (tx *Tx) DescendGreaterThan(pivot []byte,
	iterator func(key []byte, value interface{}) bool) error

DescendGreaterThan calls the iterator for every item in the database within the range [last, pivot), until iterator returns false. The results will be ordered by the item key.

func (*Tx) DescendLessOrEqual

func (tx *Tx) DescendLessOrEqual(pivot []byte,
	iterator func(key []byte, value interface{}) bool) error

DescendLessOrEqual calls the iterator for every item in the database within the range [pivot, first], until iterator returns false. The results will be ordered by the item key.

func (*Tx) DescendRange

func (tx *Tx) DescendRange(lessOrEqual, greaterThan []byte,
	iterator func(key []byte, value interface{}) bool) error

DescendRange calls the iterator for every item in the database within the range [lessOrEqual, greaterThan), until iterator returns false. The results will be ordered by the item key.

func (*Tx) Get

func (tx *Tx) Get(key []byte) (val interface{}, err error)

Get returns a value for a key. If the item does not exist then ErrNotFound is returned

func (*Tx) Iterate

func (tx *Tx) Iterate(prefix []byte, fn func(key []byte, value interface{}) error) error

Iterate iterates all elements has prefix

func (*Tx) Len

func (tx *Tx) Len() (int, error)

Len returns the number of items in the database

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback closes the transaction and reverts all mutable operations that were performed on the transaction such as Set() and Delete().

Read-only transactions can only be rolled back, not committed.

func (*Tx) Set

func (tx *Tx) Set(key []byte, value interface{}, data []byte) error

Set inserts or replaces an item in the database based on the key. The opt params may be used for additional functionality such as forcing the item to be evicted at a specified time. When the return value for err is nil the operation succeeded. When the return value of replaced is true, then the operaton replaced an existing item whose value will be returned through the previousValue variable. The results of this operation will not be available to other transactions until the current transaction has successfully committed.

Only a writable transaction can be used with this operation. This operation is not allowed during iterations such as Ascend* & Descend*.

type Unmarshaler

type Unmarshaler func(key []byte, data []byte) (interface{}, error)

Unmarshaler needs to unmarshal structures

Jump to

Keyboard shortcuts

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