Documentation ¶
Overview ¶
Package db implements an immutable, consistent, in-memory key/value store. DB uses an immutable Left-Leaning Red-Black tree (LLRB) internally. The database provides Atomicity, Consistency and Isolation from ACID. Being that it is in-memory, it does not provide durability.
The database provides the following:
Multi-Version Concurrency Control (MVCC) - By leveraging immutable LLRB trees the database is able to support any number of concurrent readers without locking, and allows a writer to make progress.
Transaction Support - The database allows for rich transactions, in which multiple objects are inserted, updated or deleted. The database provides atomicity and isolation in ACID terminology, such that until commit the updates are not visible.
Example ¶
db := New() tx := db.Txn() for i := 0; i < 6; i++ { key := []byte(fmt.Sprintf("key%.3d", i)) _, err := tx.Put(key, i, false) if err != nil { log.Fatalf("creating %q: %v", key, err) } } tx.Commit() n, _, err := db.Range(nil, nil, 0, 0) if err != nil { log.Fatalf("range: %v", err) } defer n.Cancel() for ev := range n.Recv() { if ev.Err() != nil { break } fmt.Println(string(ev.Key), ev.Data, ev.Created, ev.Current) }
Output: key000 0 1 6 key001 1 2 6 key002 2 3 6 key003 3 4 6 key004 4 5 6 key005 5 6 6
Index ¶
- Constants
- type DB
- func (db *DB) Get(key []byte, rev int64, equal bool) (interface{}, int64, int64, error)
- func (db *DB) Range(from, to []byte, rev int64, limit int32) (*Notifier, int64, error)
- func (db *DB) Rev() int64
- func (db *DB) Snapshot() (int64, error)
- func (db *DB) Txn() *Txn
- func (db *DB) Watch(key []byte) (*Notifier, int64, error)
- type Event
- type Notifier
- type Txn
- type Updater
Examples ¶
Constants ¶
const ( // ErrRevisionNotFound is returned when trying to access a revision // that has not been created. ErrRevisionNotFound = perror("revision not found") // ErrKeyNotFound is returned when trying to access a key that has // not been created. ErrKeyNotFound = perror("key not found") // ErrIncompatibleValue is returned when trying create or delete a // value on an imcompatible key. ErrIncompatibleValue = perror("incompatible value") // PairDeleted is the error returned by a watcher when the // underlying is deleted. PairDeleted = perror("key/value pair deleted") // NotifierCanceled is the error returned when the watcher is // canceled. NotifierCanceled = perror("notifier is shut down") // ErrInvertedRange is returned when a inverted range is supplied. ErrInvertedRange = perror("inverted range") )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents an immutable, consistent, in-memory key/value database. All access is performed through a transaction which can be obtained through the database.
func Load ¶
Load reloads the immutable, consistent, in-memory key/value database from the underlying backend.
func (*DB) Get ¶
Get retrieves the value for a key at revision rev. If rev <= 0 it returns the current value for a key. If equal is true the value revision must match the supplied rev.
Get returns the revision of the key/value pair, the current revision of the database and an errors if any.
func (*DB) Range ¶
Range iterates over values stored in the database in the range at rev over the interval [from, to] from left to right. Limit limits the number of keys returned. If rev <= 0 Range gets the keys at the current revision of the database. From/To combination:
from == nil && to == nil: the request returns all keys in the database from != nil && to != nil: the request returns the keys in the interval from != nil && to == nil: the request returns the key (like Get)
Range returns a notifier, the current revision of the database and an error if any.
type Event ¶
type Event struct { Data interface{} Created int64 Current int64 Key []byte // contains filtered or unexported fields }
Event represents a database key or range search query result. This structure must be kept immutable.
type Notifier ¶
type Notifier struct {
// contains filtered or unexported fields
}
Notifier represents the database event notifier.
type Txn ¶
type Txn struct {
// contains filtered or unexported fields
}
Txn represents a batch transaction on the database.
func (*Txn) Commit ¶
func (tx *Txn) Commit()
Commit closes the transaction and writes all changes into the database.
func (*Txn) Delete ¶
Delete removes a key/value pair and returns the current revision of the database.
func (*Txn) Put ¶
Put sets the value for a key. If the key exists and tombstone is true then its previous versions will be overwritten. Supplied key and value must remain valid for the life of the database.
It the key exists and the value data type differ, it returns an error.
func (*Txn) Rollback ¶
func (tx *Txn) Rollback()
Rollback closes the transaction and ignores all previous updates.