db

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

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

Go to latest
Published: Oct 24, 2016 License: ISC Imports: 12 Imported by: 2

README

DB GoDoc Build Status

Package db implements an immutable, consistent, in-memory key/value store. DB uses an immutable Left-Leaning Red-Black tree (LLRB) internally and supports snapshotting. 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.

For the underlying immutable LLRB trees, see llrb.

Example

package main

import (
	"fmt"
	"log"

	"github.com/azmodb/db"
)

func main() {
	db := db.New()
	tx := db.Txn()
	for i := 0; i < 100; 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)
	}
}

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

Examples

Constants

View Source
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

func Load(path string, timeout time.Duration, opts ...backend.Option) (*DB, error)

Load reloads the immutable, consistent, in-memory key/value database from the underlying backend.

func New

func New() *DB

New returns an immutable, consistent, in-memory key/value database.

func (*DB) Get

func (db *DB) Get(key []byte, rev int64, equal bool) (interface{}, int64, int64, error)

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

func (db *DB) Range(from, to []byte, rev int64, limit int32) (*Notifier, int64, error)

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.

func (*DB) Rev

func (db *DB) Rev() int64

Rev returns the current revision of the database.

func (*DB) Snapshot

func (db *DB) Snapshot() (int64, error)

Snapshot writes the entire in-memory database to the underlying backend.

func (*DB) Txn

func (db *DB) Txn() *Txn

Txn starts a new batch transaction. Only one batch transaction can be used at a time. Starting multiple batch transactions will cause the calls to block and be serialized until the current transaction finishes.

func (*DB) Watch

func (db *DB) Watch(key []byte) (*Notifier, int64, error)

Watch returns a notifier for a key. If the key does not exist it returns an error.

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.

func (Event) Err

func (e Event) Err() error

Err returns an error if any.

type Notifier

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

Notifier represents the database event notifier.

func (*Notifier) Cancel

func (n *Notifier) Cancel()

Cancel cancel and close the notifier. It should not be reused.

func (*Notifier) Recv

func (n *Notifier) Recv() <-chan Event

Recv returns the receiving channel part.

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

func (tx *Txn) Delete(key []byte) int64

Delete removes a key/value pair and returns the current revision of the database.

func (*Txn) Put

func (tx *Txn) Put(key []byte, data interface{}, tombstone bool) (int64, error)

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.

func (*Txn) Update

func (tx *Txn) Update(key []byte, up Updater, tombstone bool) (int64, error)

Update updates 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.

type Updater

type Updater func(data interface{}) interface{}

Updater is a function that operates on a key/value pair

Directories

Path Synopsis
Package backend implements the persistent AzmoDB backend key/value database.
Package backend implements the persistent AzmoDB backend key/value database.

Jump to

Keyboard shortcuts

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