go-memdb: github.com/hashicorp/go-memdb Index | Files | Directories

package memdb

import "github.com/hashicorp/go-memdb"

Package memdb provides an in-memory database that supports transactions and MVCC.

Index

Package Files

filter.go index.go memdb.go schema.go txn.go watch.go watch_few.go

Variables

var (
    // ErrNotFound is returned when the requested item is not found
    ErrNotFound = fmt.Errorf("not found")
)
var MapType = reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf("")).Kind()

func IsUintType Uses

func IsUintType(k reflect.Kind) (size int, okay bool)

IsUintType returns whether the passed type is a type of uint and the number of bytes needed to encode the type.

type CompoundIndex Uses

type CompoundIndex struct {
    Indexes []Indexer

    // AllowMissing results in an index based on only the indexers
    // that return data. If true, you may end up with 2/3 columns
    // indexed which might be useful for an index scan. Otherwise,
    // the CompoundIndex requires all indexers to be satisfied.
    AllowMissing bool
}

CompoundIndex is used to build an index using multiple sub-indexes Prefix based iteration is supported as long as the appropriate prefix of indexers support it. All sub-indexers are only assumed to expect a single argument.

func (*CompoundIndex) FromArgs Uses

func (c *CompoundIndex) FromArgs(args ...interface{}) ([]byte, error)

func (*CompoundIndex) FromObject Uses

func (c *CompoundIndex) FromObject(raw interface{}) (bool, []byte, error)

func (*CompoundIndex) PrefixFromArgs Uses

func (c *CompoundIndex) PrefixFromArgs(args ...interface{}) ([]byte, error)

type ConditionalIndex Uses

type ConditionalIndex struct {
    Conditional ConditionalIndexFunc
}

ConditionalIndex builds an index based on a condition specified by a passed user function. This function may examine the passed object and return a boolean to encapsulate an arbitrarily complex conditional.

func (*ConditionalIndex) FromArgs Uses

func (c *ConditionalIndex) FromArgs(args ...interface{}) ([]byte, error)

func (*ConditionalIndex) FromObject Uses

func (c *ConditionalIndex) FromObject(obj interface{}) (bool, []byte, error)

type ConditionalIndexFunc Uses

type ConditionalIndexFunc func(obj interface{}) (bool, error)

ConditionalIndexFunc is the required function interface for a ConditionalIndex.

type DBSchema Uses

type DBSchema struct {
    // Tables is the set of tables within this database. The key is the
    // table name and must match the Name in TableSchema.
    Tables map[string]*TableSchema
}

DBSchema is the schema to use for the full database with a MemDB instance.

MemDB will require a valid schema. Schema validation can be tested using the Validate function. Calling this function is recommended in unit tests.

func (*DBSchema) Validate Uses

func (s *DBSchema) Validate() error

Validate validates the schema.

type FieldSetIndex Uses

type FieldSetIndex struct {
    Field string
}

FieldSetIndex is used to extract a field from an object using reflection and builds an index on whether the field is set by comparing it against its type's nil value.

func (*FieldSetIndex) FromArgs Uses

func (f *FieldSetIndex) FromArgs(args ...interface{}) ([]byte, error)

func (*FieldSetIndex) FromObject Uses

func (f *FieldSetIndex) FromObject(obj interface{}) (bool, []byte, error)

type FilterFunc Uses

type FilterFunc func(interface{}) bool

FilterFunc is a function that takes the results of an iterator and returns whether the result should be filtered out.

type FilterIterator Uses

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

FilterIterator is used to wrap a ResultIterator and apply a filter over it.

func NewFilterIterator Uses

func NewFilterIterator(wrap ResultIterator, filter FilterFunc) *FilterIterator

func (*FilterIterator) Next Uses

func (f *FilterIterator) Next() interface{}

Next returns the next non-filtered result from the wrapped iterator

func (*FilterIterator) WatchCh Uses

func (f *FilterIterator) WatchCh() <-chan struct{}

WatchCh returns the watch channel of the wrapped iterator.

type IndexSchema Uses

type IndexSchema struct {
    // Name of the index. This must be unique among a tables set of indexes.
    // This must match the key in the map of Indexes for a TableSchema.
    Name string

    // AllowMissing if true ignores this index if it doesn't produce a
    // value. For example, an index that extracts a field that doesn't
    // exist from a structure.
    AllowMissing bool

    Unique  bool
    Indexer Indexer
}

IndexSchema is the schema for an index. An index defines how a table is queried.

func (*IndexSchema) Validate Uses

func (s *IndexSchema) Validate() error

type Indexer Uses

type Indexer interface {
    // FromArgs is called to build the exact index key from a list of arguments.
    FromArgs(args ...interface{}) ([]byte, error)
}

Indexer is an interface used for defining indexes. Indexes are used for efficient lookup of objects in a MemDB table. An Indexer must also implement one of SingleIndexer or MultiIndexer.

Indexers are primarily responsible for returning the lookup key as a byte slice. The byte slice is the key data in the underlying data storage.

type MemDB Uses

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 Uses

func NewMemDB(schema *DBSchema) (*MemDB, error)

NewMemDB creates a new MemDB with the given schema

func (*MemDB) Snapshot Uses

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 Uses

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 MultiIndexer Uses

type MultiIndexer interface {
    // FromObject extracts index values from an object. The return values
    // are the same as a SingleIndexer except there can be multiple index
    // values.
    FromObject(raw interface{}) (bool, [][]byte, error)
}

MultiIndexer is an interface used for defining indexes that generate multiple values per object. Each value is stored as a seperate index pointing to the same object.

For example, an index that extracts the first and last name of a person and allows lookup based on eitherd would be a MultiIndexer. The FromObject of this example would split the first and last name and return both as values.

type PrefixIndexer Uses

type PrefixIndexer interface {
    // PrefixFromArgs is the same as FromArgs for an Indexer except that
    // the index value returned should return all prefix-matched values.
    PrefixFromArgs(args ...interface{}) ([]byte, error)
}

PrefixIndexer is an optional interface on top of an Indexer that allows indexes to support prefix-based iteration.

type ResultIterator Uses

type ResultIterator interface {
    WatchCh() <-chan struct{}
    Next() interface{}
}

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

type SingleIndexer Uses

type SingleIndexer interface {
    // FromObject extracts the index value from an object. The return values
    // are whether the index value was found, the index value, and any error
    // while extracting the index value, respectively.
    FromObject(raw interface{}) (bool, []byte, error)
}

SingleIndexer is an interface used for defining indexes that generate a single value per object

type StringFieldIndex Uses

type StringFieldIndex struct {
    Field     string
    Lowercase bool
}

StringFieldIndex is used to extract a field from an object using reflection and builds an index on that field.

func (*StringFieldIndex) FromArgs Uses

func (s *StringFieldIndex) FromArgs(args ...interface{}) ([]byte, error)

func (*StringFieldIndex) FromObject Uses

func (s *StringFieldIndex) FromObject(obj interface{}) (bool, []byte, error)

func (*StringFieldIndex) PrefixFromArgs Uses

func (s *StringFieldIndex) PrefixFromArgs(args ...interface{}) ([]byte, error)

type StringMapFieldIndex Uses

type StringMapFieldIndex struct {
    Field     string
    Lowercase bool
}

StringMapFieldIndex is used to extract a field of type map[string]string from an object using reflection and builds an index on that field.

func (*StringMapFieldIndex) FromArgs Uses

func (s *StringMapFieldIndex) FromArgs(args ...interface{}) ([]byte, error)

func (*StringMapFieldIndex) FromObject Uses

func (s *StringMapFieldIndex) FromObject(obj interface{}) (bool, [][]byte, error)

type StringSliceFieldIndex Uses

type StringSliceFieldIndex struct {
    Field     string
    Lowercase bool
}

StringSliceFieldIndex builds an index from a field on an object that is a string slice ([]string). Each value within the string slice can be used for lookup.

func (*StringSliceFieldIndex) FromArgs Uses

func (s *StringSliceFieldIndex) FromArgs(args ...interface{}) ([]byte, error)

func (*StringSliceFieldIndex) FromObject Uses

func (s *StringSliceFieldIndex) FromObject(obj interface{}) (bool, [][]byte, error)

func (*StringSliceFieldIndex) PrefixFromArgs Uses

func (s *StringSliceFieldIndex) PrefixFromArgs(args ...interface{}) ([]byte, error)

type TableSchema Uses

type TableSchema struct {
    // Name of the table. This must match the key in the Tables map in DBSchema.
    Name string

    // Indexes is the set of indexes for querying this table. The key
    // is a unique name for the index and must match the Name in the
    // IndexSchema.
    Indexes map[string]*IndexSchema
}

TableSchema is the schema for a single table.

func (*TableSchema) Validate Uses

func (s *TableSchema) Validate() error

Validate is used to validate the table schema

type Txn Uses

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 Uses

func (txn *Txn) Abort()

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

func (*Txn) Commit Uses

func (txn *Txn) Commit()

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

func (*Txn) Defer Uses

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 Uses

func (txn *Txn) Delete(table string, obj interface{}) error

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

func (*Txn) DeleteAll Uses

func (txn *Txn) DeleteAll(table, index string, args ...interface{}) (int, error)

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

func (*Txn) DeletePrefix Uses

func (txn *Txn) DeletePrefix(table string, prefix_index string, prefix string) (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 Uses

func (txn *Txn) First(table, index string, args ...interface{}) (interface{}, error)

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

func (*Txn) FirstWatch Uses

func (txn *Txn) FirstWatch(table, index string, args ...interface{}) (<-chan struct{}, 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 Uses

func (txn *Txn) Get(table, index string, args ...interface{}) (ResultIterator, error)

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

func (*Txn) Insert Uses

func (txn *Txn) Insert(table string, obj interface{}) error

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

func (*Txn) LongestPrefix Uses

func (txn *Txn) LongestPrefix(table, index string, args ...interface{}) (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.

type UUIDFieldIndex Uses

type UUIDFieldIndex struct {
    Field string
}

UUIDFieldIndex is used to extract a field from an object using reflection and builds an index on that field by treating it as a UUID. This is an optimization to using a StringFieldIndex as the UUID can be more compactly represented in byte form.

func (*UUIDFieldIndex) FromArgs Uses

func (u *UUIDFieldIndex) FromArgs(args ...interface{}) ([]byte, error)

func (*UUIDFieldIndex) FromObject Uses

func (u *UUIDFieldIndex) FromObject(obj interface{}) (bool, []byte, error)

func (*UUIDFieldIndex) PrefixFromArgs Uses

func (u *UUIDFieldIndex) PrefixFromArgs(args ...interface{}) ([]byte, error)

type UintFieldIndex Uses

type UintFieldIndex struct {
    Field string
}

UintFieldIndex is used to extract a uint field from an object using reflection and builds an index on that field.

func (*UintFieldIndex) FromArgs Uses

func (u *UintFieldIndex) FromArgs(args ...interface{}) ([]byte, error)

func (*UintFieldIndex) FromObject Uses

func (u *UintFieldIndex) FromObject(obj interface{}) (bool, []byte, error)

type WatchSet Uses

type WatchSet map[<-chan struct{}]struct{}

WatchSet is a collection of watch channels.

func NewWatchSet Uses

func NewWatchSet() WatchSet

NewWatchSet constructs a new watch set.

func (WatchSet) Add Uses

func (w WatchSet) Add(watchCh <-chan struct{})

Add appends a watchCh to the WatchSet if non-nil.

func (WatchSet) AddWithLimit Uses

func (w WatchSet) AddWithLimit(softLimit int, watchCh <-chan struct{}, altCh <-chan struct{})

AddWithLimit appends a watchCh to the WatchSet if non-nil, and if the given softLimit hasn't been exceeded. Otherwise, it will watch the given alternate channel. It's expected that the altCh will be the same on many calls to this function, so you will exceed the soft limit a little bit if you hit this, but not by much.

This is useful if you want to track individual items up to some limit, after which you watch a higher-level channel (usually a channel from start start of an iterator higher up in the radix tree) that will watch a superset of items.

func (WatchSet) Watch Uses

func (w WatchSet) Watch(timeoutCh <-chan time.Time) bool

Watch is used to wait for either the watch set to trigger or a timeout. Returns true on timeout.

func (WatchSet) WatchCtx Uses

func (w WatchSet) WatchCtx(ctx context.Context) error

WatchCtx is used to wait for either the watch set to trigger or for the context to be cancelled. Watch with a timeout channel can be mimicked by creating a context with a deadline. WatchCtx should be preferred over Watch.

Directories

PathSynopsis
watch-genThis tool generates the special-case code for a small number of watchers which runs all the watches in a single select vs.

Package memdb imports 12 packages (graph) and is imported by 272 packages. Updated 2018-11-08. Refresh now. Tools for package owners.