inmemdb

package module
v0.0.0-...-fccb716 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2017 License: MIT Imports: 6 Imported by: 1

README

InMemory DB is a fork from BuntDB. It's a a low-level, in-memory, key/value store in pure Go, supports generic interfaces and custom indexes.

License

InMemDB source code is available under the MIT License.

Documentation

Overview

Package buntdb 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

This section is empty.

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

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

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

	// ErrIndexExists is returned when an index already exists in the database.
	ErrIndexExists = errors.New("index exists")

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

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

Functions

func Desc

func Desc(less func(a, b interface{}, fields []string) bool) func(a, b interface{}, fields []string) bool

Desc is a helper function that changes the order of an index.

func IndexFloat64

func IndexFloat64(ia, ib interface{}, fields []string) bool

IndexFloat64 is a helper function that returns true if 'a' is less than 'b'. This compares float64s that are added to the database using the Float() conversion function.

func IndexIString

func IndexIString(ia, ib interface{}, fields []string) bool

IndexString is a helper function that return true if 'a' is less than 'b'. This is a case-insensitive comparison. Use the IndexBinary() for comparing case-sensitive strings.

func IndexInt

func IndexInt(ia, ib interface{}, fields []string) bool

IndexInt is a helper function that returns true if 'a' is less than 'b'.

func IndexString

func IndexString(ia, ib interface{}, fields []string) bool

IndexInt is a helper function that returns true if 'a' is less than 'b'.

func IndexUint64

func IndexUint64(ia, ib interface{}, fields []string) bool

IndexUint64 is a helper function that returns true if 'a' is less than 'b'. This compares uint64s that are added to the database using the Uint() conversion function.

Types

type Config

type Config struct {
	// OnExpired is used to custom handle the deletion option when a key
	// has been expired.
	OnExpired func(keys []string)
}

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

type DB

type DB struct {
	Name string // A given name, not used internally
	// 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(name string) (*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) 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 IndexOptions

type IndexOptions struct {
	// CaseInsensitiveKeyMatching allow for case-insensitive
	// matching on keys when setting key/values.
	CaseInsensitiveKeyMatching bool
}

IndexOptions provides an index with additional features or alternate functionality.

type SetOptions

type SetOptions struct {
	// Expires indicates that the Set() key-value will expire
	Expires bool
	// TTL is how much time the key-value will exist in the database
	// before being evicted. The Expires field must also be set to true.
	// TTL stands for Time-To-Live.
	TTL time.Duration
}

SetOptions represents options that may be included with the Set() command.

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(index string,
	iterator func(value interface{}) bool) error

Ascend calls the iterator for every item in the database within the range [first, last], until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the less() function of the defined index. When an index is not provided, the results will be ordered by the item key. An invalid index will return an error.

func (*Tx) AscendGreaterOrEqual

func (tx *Tx) AscendGreaterOrEqual(index string, pivot interface{},
	iterator func(value interface{}) bool) error

AscendGreaterOrEqual calls the iterator for every item in the database within the range [pivot, last], until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the less() function of the defined index. When an index is not provided, the results will be ordered by the item key. An invalid index will return an error.

func (*Tx) AscendLessThan

func (tx *Tx) AscendLessThan(index string, pivot interface{},
	iterator func(value interface{}) bool) error

AscendLessThan calls the iterator for every item in the database within the range [first, pivot), until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the less() function of the defined index. When an index is not provided, the results will be ordered by the item key. An invalid index will return an error.

func (*Tx) AscendRange

func (tx *Tx) AscendRange(index string, greaterOrEqual, lessThan interface{},
	iterator func(value interface{}) bool) error

AscendRange calls the iterator for every item in the database within the range [greaterOrEqual, lessThan), until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the less() function of the defined index. When an index is not provided, the results will be ordered by the item key. An invalid index will return an error.

func (*Tx) Commit

func (tx *Tx) Commit() error

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

func (*Tx) CreateIndex

func (tx *Tx) CreateIndex(name string, fields []string,
	lessers ...func(a, b interface{}, fields []string) bool) error

CreateIndex builds a new index and populates it with items. The items are ordered in an b-tree and can be retrieved using the Ascend* and Descend* methods. An error will occur if an index with the same name already exists.

When a pattern is provided, the index will be populated with keys that match the specified pattern. This is a very simple pattern match where '*' matches on any number characters and '?' matches on any one character. The less function compares if string 'a' is less than string 'b'. It allows for indexes to create custom ordering. It's possible that the strings may be textual or binary. It's up to the provided less function to handle the content format and comparison. There are some default less function that can be used such as IndexString, IndexBinary, etc.

func (*Tx) CreateIndexOptions

func (tx *Tx) CreateIndexOptions(name string, fields []string,
	opts *IndexOptions,
	lessers []func(a, b interface{}, fields []string) bool) error

CreateIndexOptions is the same as CreateIndex except that it allows for additional options.

func (*Tx) Delete

func (tx *Tx) Delete(key string) (val interface{}, err error)

Delete removes an item from the database based on the item's key. If the item does not exist or if the item has expired 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(index string,
	iterator func(value interface{}) bool) error

Descend calls the iterator for every item in the database within the range [last, first], until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the less() function of the defined index. When an index is not provided, the results will be ordered by the item key. An invalid index will return an error.

func (*Tx) DescendGreaterThan

func (tx *Tx) DescendGreaterThan(index string, pivot interface{},
	iterator func(value interface{}) bool) error

DescendGreaterThan calls the iterator for every item in the database within the range [last, pivot), until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the less() function of the defined index. When an index is not provided, the results will be ordered by the item key. An invalid index will return an error.

func (*Tx) DescendLessOrEqual

func (tx *Tx) DescendLessOrEqual(index string, pivot interface{},
	iterator func(value interface{}) bool) error

DescendLessOrEqual calls the iterator for every item in the database within the range [pivot, first], until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the less() function of the defined index. When an index is not provided, the results will be ordered by the item key. An invalid index will return an error.

func (*Tx) DescendRange

func (tx *Tx) DescendRange(index string, lessOrEqual, greaterThan interface{},
	iterator func(value interface{}) bool) error

DescendRange calls the iterator for every item in the database within the range [lessOrEqual, greaterThan), until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the less() function of the defined index. When an index is not provided, the results will be ordered by the item key. An invalid index will return an error.

func (*Tx) DropIndex

func (tx *Tx) DropIndex(name string) error

DropIndex removes an index.

func (*Tx) Get

func (tx *Tx) Get(key string) (val interface{}, err error)

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

func (*Tx) GetLess

func (tx *Tx) GetLess(index string) (func(a, b interface{}, fields []string) bool, error)

GetLess returns the less function for an index. This is handy for doing ad-hoc compares inside a transaction. Returns ErrNotFound if the index is not found or there is no less function bound to the index

func (*Tx) Indexes

func (tx *Tx) Indexes() ([]string, error)

Indexes returns a list of index names.

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 string, value interface{}, opts *SetOptions) (previousValue interface{},
	replaced bool, err 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*.

func (*Tx) TTL

func (tx *Tx) TTL(key string) (time.Duration, error)

TTL returns the remaining time-to-live for an item. A negative duration will be returned for items that do not have an expiration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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