radixdb

package
v0.9.4 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when the requested item is not found
	ErrNotFound = fmt.Errorf("not found")
)

Functions

func FromObject

func FromObject(obj interface{}) ([]byte, error)

return the key from obj

func KVFromObject

func KVFromObject(obj interface{}) ([]byte, []byte, error)

Types

type MemDB

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

MemDB is an in-memory database.

MemDB provides a table abstraction to store objects (rows) with multiple indexes based on inserted values. The database makes use of immutable radix trees to provide transactions and MVCC.

func NewMemDB

func NewMemDB() (*MemDB, error)

NewMemDB creates a new MemDB with the given schema

func (*MemDB) Size

func (db *MemDB) Size() int

func (*MemDB) Snapshot

func (db *MemDB) Snapshot() *MemDB

Snapshot is used to capture a point-in-time snapshot of the database that will not be affected by any write operations to the existing DB.

func (*MemDB) Txn

func (db *MemDB) Txn(write bool) *Txn

Txn is used to start a new transaction, in either read or write mode. There can only be a single concurrent writer, but any number of readers.

type ResultIterator

type ResultIterator interface {
	WatchCh() <-chan struct{}
	// Next returns the next result from the iterator. If there are no more results
	// nil is returned.
	Next() ([]byte, interface{})
}

ResultIterator is used to iterate over a list of results from a query on a table.

When a ResultIterator is created from a write transaction, the results from Next will reflect a snapshot of the table at the time the ResultIterator is created. This means that calling Insert or Delete on a transaction while iterating is allowed, but the changes made by Insert or Delete will not be observed in the results returned from subsequent calls to Next. For example if an item is deleted from the index used by the iterator it will still be returned by Next. If an item is inserted into the index used by the iterator, it will not be returned by Next. However, an iterator created after a call to Insert or Delete will reflect the modifications.

When a ResultIterator is created from a write transaction, and there are already modifications to the index used by the iterator, the modification cache of the index will be invalidated. This may result in some additional allocations if the same node in the index is modified again.

type Txn

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

Txn is a transaction against a MemDB. This can be a read or write transaction.

func (*Txn) Abort

func (txn *Txn) Abort()

Abort is used to cancel this transaction. This is a noop for read transactions.

func (*Txn) Commit

func (txn *Txn) Commit()

Commit is used to finalize this transaction. This is a noop for read transactions.

func (*Txn) Defer

func (txn *Txn) Defer(fn func())

Defer is used to push a new arbitrary function onto a stack which gets called when a transaction is committed and finished. Deferred functions are called in LIFO order, and only invoked at the end of write transactions.

func (*Txn) Delete

func (txn *Txn) Delete(key []byte) error

Delete is used to delete a single object from the given table This object must already exist in the table

func (*Txn) DeleteAll

func (txn *Txn) DeleteAll() (int, error)

DeleteAll is used to delete all the objects in a given table matching the constraints on the index

func (*Txn) DeletePrefix

func (txn *Txn) DeletePrefix(prefix []byte) (bool, error)

DeletePrefix is used to delete an entire subtree based on a prefix. The given index must be a prefix index, and will be used to perform a scan and enumerate the set of objects to delete. These will be removed from all other indexes, and then a special prefix operation will delete the objects from the given index in an efficient subtree delete operation. This is useful when you have a very large number of objects indexed by the given index, along with a much smaller number of entries in the other indexes for those objects.

func (*Txn) First

func (txn *Txn) First(key []byte) ([]byte, interface{}, error)

First is used to return the first matching object for the given constraints on the index

func (*Txn) FirstWatch

func (txn *Txn) FirstWatch(key []byte) (<-chan struct{}, []byte, interface{}, error)

FirstWatch is used to return the first matching object for the given constraints on the index along with the watch channel

func (*Txn) Get

func (txn *Txn) Get(key []byte) (ResultIterator, error)

Get is used to construct a ResultIterator over all the rows that match the given constraints of an index.

See the documentation for ResultIterator to understand the behaviour of the returned ResultIterator.

func (*Txn) GetReverse

func (txn *Txn) GetReverse(key []byte) (ResultIterator, error)

GetReverse is used to construct a Reverse ResultIterator over all the rows that match the given constraints of an index. The returned ResultIterator's Next() will return the next Previous value.

See the documentation for ResultIterator to understand the behaviour of the returned ResultIterator.

func (*Txn) Insert

func (txn *Txn) Insert(key []byte, value []byte) error

Insert is used to add or update an object into the given table

func (*Txn) Last

func (txn *Txn) Last(key []byte) ([]byte, interface{}, error)

Last is used to return the last matching object for the given constraints on the index

func (*Txn) LastWatch

func (txn *Txn) LastWatch(key []byte) (<-chan struct{}, []byte, interface{}, error)

LastWatch is used to return the last matching object for the given constraints on the index along with the watch channel

func (*Txn) LongestPrefix

func (txn *Txn) LongestPrefix(key []byte) (interface{}, error)

LongestPrefix is used to fetch the longest prefix match for the given constraints on the index. Note that this will not work with the memdb StringFieldIndex because it adds null terminators which prevent the algorithm from correctly finding a match (it will get to right before the null and fail to find a leaf node). This should only be used where the prefix given is capable of matching indexed entries directly, which typically only applies to a custom indexer. See the unit test for an example.

func (*Txn) LowerBound

func (txn *Txn) LowerBound(key []byte) (ResultIterator, error)

LowerBound is used to construct a ResultIterator over all the the range of rows that have an index value greater than or equal to the provide args. Calling this then iterating until the rows are larger than required allows range scans within an index. It is not possible to watch the resulting iterator since the radix tree doesn't efficiently allow watching on lower bound changes. The WatchCh returned will be nill and so will block forever.

See the documentation for ResultIterator to understand the behaviour of the returned ResultIterator.

func (*Txn) ReverseLowerBound

func (txn *Txn) ReverseLowerBound(key []byte) (ResultIterator, error)

ReverseLowerBound is used to construct a Reverse ResultIterator over all the the range of rows that have an index value less than or equal to the provide args. Calling this then iterating until the rows are lower than required allows range scans within an index. It is not possible to watch the resulting iterator since the radix tree doesn't efficiently allow watching on lower bound changes. The WatchCh returned will be nill and so will block forever.

See the documentation for ResultIterator to understand the behaviour of the returned ResultIterator.

func (*Txn) Snapshot

func (txn *Txn) Snapshot() *Txn

Snapshot creates a snapshot of the current state of the transaction. Returns a new read-only transaction or nil if the transaction is already aborted or committed.

Jump to

Keyboard shortcuts

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