Documentation
¶
Overview ¶
Package db provides a DB which manage collections
Index ¶
- Constants
- Variables
- func DefaultDecode(data []byte, value interface{}) error
- func DefaultEncode(value interface{}) ([]byte, error)
- func NewSimpleTx(ds datastore.Datastore) datastore.Txn
- type Action
- type ActionType
- type Collection
- func (c *Collection) AddIndex(path string, unique bool) error
- func (c *Collection) BaseKey() ds.Key
- func (c *Collection) Create(vs ...interface{}) error
- func (c *Collection) Delete(ids ...core.InstanceID) error
- func (c *Collection) Find(result interface{}, q *Query) error
- func (c *Collection) FindByID(id core.InstanceID, v interface{}) error
- func (c *Collection) FindJSON(q *JSONQuery) (ret []string, err error)
- func (c *Collection) Has(ids ...core.InstanceID) (exists bool, err error)
- func (c *Collection) Indexes() map[string]Index
- func (c *Collection) ReadTxn(f func(txn *Txn) error) error
- func (c *Collection) Save(vs ...interface{}) error
- func (c *Collection) WriteTxn(f func(txn *Txn) error) error
- type CollectionConfig
- type Comparer
- type Config
- type Criterion
- func (c *Criterion) Eq(value interface{}) *Query
- func (c *Criterion) Fn(mf MatchFunc) *Query
- func (c *Criterion) Ge(value interface{}) *Query
- func (c *Criterion) Gt(value interface{}) *Query
- func (c *Criterion) Le(value interface{}) *Query
- func (c *Criterion) Lt(value interface{}) *Query
- func (c *Criterion) Ne(value interface{}) *Query
- type DB
- func (d *DB) Close() error
- func (d *DB) GetCollection(name string) *Collection
- func (d *DB) Listen(los ...ListenOption) (Listener, error)
- func (d *DB) NewCollection(config CollectionConfig) (*Collection, error)
- func (d *DB) NewCollectionFromInstance(name string, defaultInstance interface{}) (*Collection, error)
- func (d *DB) Reduce(events []core.Event) error
- type Datastore
- type DecodeFunc
- type EncodeFunc
- type Index
- type IndexConfig
- type Indexer
- type JSONCriterion
- func (c *JSONCriterion) Eq(value interface{}) *JSONQuery
- func (c *JSONCriterion) Ge(value interface{}) *JSONQuery
- func (c *JSONCriterion) Gt(value interface{}) *JSONQuery
- func (c *JSONCriterion) Le(value interface{}) *JSONQuery
- func (c *JSONCriterion) Lt(value interface{}) *JSONQuery
- func (c *JSONCriterion) Ne(value interface{}) *JSONQuery
- type JSONOperation
- type JSONQuery
- type JSONSort
- type JSONValue
- type ListenActionType
- type ListenOption
- type Listener
- type LocalEventListener
- type Manager
- type MarshaledResult
- type MatchFunc
- type NetBoostrapper
- type NetConfig
- type NetOption
- type Option
- type Query
- type Reducer
- type SimpleTx
- func (bt *SimpleTx) Commit() error
- func (bt *SimpleTx) Delete(key datastore.Key) error
- func (bt *SimpleTx) Discard()
- func (bt *SimpleTx) Get(k datastore.Key) ([]byte, error)
- func (bt *SimpleTx) GetSize(k datastore.Key) (int, error)
- func (bt *SimpleTx) Has(k datastore.Key) (bool, error)
- func (bt *SimpleTx) Put(key datastore.Key, val []byte) error
- func (bt *SimpleTx) Query(q query.Query) (query.Results, error)
- type TxMapDatastore
- type Txn
- func (t *Txn) Commit() error
- func (t *Txn) Create(new ...interface{}) error
- func (t *Txn) Delete(ids ...core.InstanceID) error
- func (t *Txn) Discard()
- func (t *Txn) Find(res interface{}, q *Query) error
- func (t *Txn) FindByID(id core.InstanceID, v interface{}) error
- func (t *Txn) FindJSON(q *JSONQuery) ([]string, error)
- func (t *Txn) Has(ids ...core.InstanceID) (bool, error)
- func (t *Txn) Save(updated ...interface{}) error
Constants ¶
const ( // Eq is "equals" Eq = JSONOperation(eq) // Ne is "not equal to" Ne = JSONOperation(ne) // Gt is "greater than" Gt = JSONOperation(gt) // Lt is "less than" Lt = JSONOperation(lt) // Ge is "greater than or equal to" Ge = JSONOperation(ge) // Le is "less than or equal to" Le = JSONOperation(le) )
Variables ¶
var ( // ErrNotFound indicates that the specified instance doesn't // exist in the collection. ErrNotFound = errors.New("instance not found") // ErrReadonlyTx indicates that no write operations can be done since // the current transaction is readonly. ErrReadonlyTx = errors.New("read only transaction") // ErrInvalidSchemaInstance indicates the current operation is from an // instance that doesn't satisfy the collection schema. ErrInvalidSchemaInstance = errors.New("instance doesn't correspond to schema") )
var ( ErrUniqueExists = errors.New("unique constraint violation") ErrNotIndexable = errors.New("value not indexable") ErrNoIndexFound = errors.New("no index found") )
ErrUniqueExists is the error thrown when data is being inserted for a unique constraint value that already exists
var ( // ErrInvalidSortingField is returned when a query sorts a result by a // non-existent field in the collection schema. ErrInvalidSortingField = errors.New("sorting field doesn't correspond to instance type") // ErrInvalidSliceType is returned when a query receives a result by a // slice type which doesn't correspond to the collection being queried. ErrInvalidSliceType = errors.New("slice type doesn't correspond to collection type") )
var ( // ErrInvalidCollectionType indicates the provided default type isn't compatible // with a Collection type. ErrInvalidCollectionType = errors.New("the collection type should be a non-nil pointer to a struct that has an ID property") )
Functions ¶
func DefaultDecode ¶
DefaultDecode is the default decoding func from badgerhold (Gob)
func DefaultEncode ¶
DefaultEncode is the default encoding func from badgerhold (Gob)
Types ¶
type Action ¶
type Action struct {
Collection string
Type ActionType
ID core.InstanceID
}
type ActionType ¶
type ActionType int
const ( ActionCreate ActionType = iota + 1 ActionSave ActionDelete )
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection contains instances of a schema, and provides operations for creating, updating, deleting, and quering them.
func (*Collection) AddIndex ¶
func (c *Collection) AddIndex(path string, unique bool) error
AddIndex creates a new index based on the given path string. Set unique to true if you want a unique constraint on the given path. See https://github.com/tidwall/gjson for documentation on the supported path structure. Adding an index will override any overlapping index values if they already exist. @note: This does NOT currently build the index. If items have been added prior to adding a new index, they will NOT be indexed a posteriori.
func (*Collection) BaseKey ¶
func (c *Collection) BaseKey() ds.Key
func (*Collection) Create ¶
func (c *Collection) Create(vs ...interface{}) error
Create creates instances in the collection.
func (*Collection) Delete ¶
func (c *Collection) Delete(ids ...core.InstanceID) error
Delete deletes instances by its IDs. It doesn't fail if the ID doesn't exist.
func (*Collection) Find ¶
func (c *Collection) Find(result interface{}, q *Query) error
Find executes a Query into result.
func (*Collection) FindByID ¶
func (c *Collection) FindByID(id core.InstanceID, v interface{}) error
FindByID finds an instance by its ID and saves it in v. If doesn't exists returns ErrNotFound.
func (*Collection) FindJSON ¶
func (c *Collection) FindJSON(q *JSONQuery) (ret []string, err error)
FindJSON executes a Query in in JSONMode and returns the result.
func (*Collection) Has ¶
func (c *Collection) Has(ids ...core.InstanceID) (exists bool, err error)
Has returns true if all IDs exist in the collection, false otherwise.
func (*Collection) Indexes ¶
func (c *Collection) Indexes() map[string]Index
Indexes is a map of collection properties to Indexes
func (*Collection) ReadTxn ¶
func (c *Collection) ReadTxn(f func(txn *Txn) error) error
ReadTxn creates an explicit readonly transaction. Any operation that tries to mutate an instance of the collection will ErrReadonlyTx. Provides serializable isolation gurantees.
func (*Collection) Save ¶
func (c *Collection) Save(vs ...interface{}) error
Save saves changes of instances in the collection.
type CollectionConfig ¶
type CollectionConfig struct {
Name string
Schema string
Indexes []IndexConfig
}
CollectionConfig describes a new Collection.
type Comparer ¶
Comparer compares a type against the encoded value in the db. The result should be 0 if current==other, -1 if current < other, and +1 if current > other. If a field in a struct doesn't specify a comparer, then the default comparison is used (convert to string and compare) this interface is already handled for standard Go Types as well as more complex ones such as those in time and big an error is returned if the type cannot be compared The concrete type will always be passedin, not a pointer
type Config ¶
type Config struct {
RepoPath string
Datastore ds.TxnDatastore
EventCodec core.EventCodec
JsonMode bool
Debug bool
LowMem bool
}
Config has configuration parameters for a db
type Criterion ¶
type Criterion struct {
// contains filtered or unexported fields
}
Criterion is a partial condition that can specify comparison operator for a field.
type DB ¶
DB is the aggregate-root of events and state. External/remote events are dispatched to the DB, and are internally processed to impact collection states. Likewise, local changes in collections registered produce events dispatched externally.
func NewDB ¶
NewDB creates a new DB, which will *own* ds and dispatcher for internal use. Saying it differently, ds and dispatcher shouldn't be used externally.
func NewDBFromAddr ¶
func NewDBFromAddr(ctx context.Context, network net.Net, addr ma.Multiaddr, key thread.Key, opts ...Option) (*DB, error)
NewDBFromAddr creates a new DB from a thread hosted by another peer at address, which will *own* ds and dispatcher for internal use. Saying it differently, ds and dispatcher shouldn't be used externally.
func (*DB) GetCollection ¶
func (d *DB) GetCollection(name string) *Collection
GetCollection returns a collection by name.
func (*DB) Listen ¶
func (d *DB) Listen(los ...ListenOption) (Listener, error)
Listen returns a Listener which notifies about actions applying the defined filters. The DB *won't* wait for slow receivers, so if the channel is full, the action will be dropped.
func (*DB) NewCollection ¶
func (d *DB) NewCollection(config CollectionConfig) (*Collection, error)
NewCollection creates a new collection in the db with a JSON schema.
func (*DB) NewCollectionFromInstance ¶
func (d *DB) NewCollectionFromInstance(name string, defaultInstance interface{}) (*Collection, error)
NewCollectionFromInstance creates a new collection in the db by infering type from a defaultInstance.
type Datastore ¶
type Datastore struct {
kt.KeyTransform
ds.Datastore
// contains filtered or unexported fields
}
Datastore keeps a KeyTransform function
type DecodeFunc ¶
DecodeFunc is a function for decoding a value from bytes
type EncodeFunc ¶
EncodeFunc is a function for encoding a value into bytes
type IndexConfig ¶
IndexConfig stores the configuration for a given Index.
type JSONCriterion ¶
type JSONCriterion struct {
FieldPath string
Operation JSONOperation
Value JSONValue
// contains filtered or unexported fields
}
JSONCriterion represents a restriction on a field
func JSONWhere ¶
func JSONWhere(field string) *JSONCriterion
JSONWhere starts to create a query condition for a field
func (*JSONCriterion) Eq ¶
func (c *JSONCriterion) Eq(value interface{}) *JSONQuery
Eq is an equality operator against a field
func (*JSONCriterion) Ge ¶
func (c *JSONCriterion) Ge(value interface{}) *JSONQuery
Ge is a greater or equal operator against a field
func (*JSONCriterion) Gt ¶
func (c *JSONCriterion) Gt(value interface{}) *JSONQuery
Gt is a greater operator against a field
func (*JSONCriterion) Le ¶
func (c *JSONCriterion) Le(value interface{}) *JSONQuery
Le is a less or equal operator against a field
func (*JSONCriterion) Lt ¶
func (c *JSONCriterion) Lt(value interface{}) *JSONQuery
Lt is a less operation against a field
func (*JSONCriterion) Ne ¶
func (c *JSONCriterion) Ne(value interface{}) *JSONQuery
Ne is a not equal operator against a field
type JSONQuery ¶
type JSONQuery struct {
Ands []*JSONCriterion
Ors []*JSONQuery
Sort JSONSort
Index string
}
JSONQuery is a json-seriable query representation
func JSONOrderBy ¶
JSONOrderBy specify ascending order for the query results.
func JSONOrderByDesc ¶
JSONOrderByDesc specify descending order for the query results.
func (*JSONQuery) JSONAnd ¶
func (q *JSONQuery) JSONAnd(field string) *JSONCriterion
JSONAnd concatenates a new condition in an existing field.
func (*JSONQuery) JSONOr ¶
JSONOr concatenates a new condition that is sufficient for an instance to satisfy, independant of the current Query. Has left-associativity as: (a And b) Or c
func (*JSONQuery) JSONOrderBy ¶
JSONOrderBy specify ascending order for the query results. On multiple calls, only the last one is considered.
func (*JSONQuery) JSONOrderByDesc ¶
JSONOrderByDesc specify descending order for the query results. On multiple calls, only the last one is considered.
type ListenActionType ¶
type ListenActionType int
const ( ListenAll ListenActionType = iota ListenCreate ListenSave ListenDelete )
type ListenOption ¶
type ListenOption struct {
Type ListenActionType
Collection string
ID core.InstanceID
}
type LocalEventListener ¶
type LocalEventListener struct {
// contains filtered or unexported fields
}
LocalEventListener notifies about new locally generated ipld.Nodes results of transactions
func (*LocalEventListener) Channel ¶
func (l *LocalEventListener) Channel() <-chan format.Node
Channel returns an unbuffered channel to receive local events
func (*LocalEventListener) Discard ¶
func (l *LocalEventListener) Discard()
Discard indicates that no further events will be received and ready for being garbage collected
type Manager ¶
func NewManager ¶
NewManager hydrates dbs from prefixes and starts them.
func (*Manager) NewDBFromAddr ¶
func (m *Manager) NewDBFromAddr(ctx context.Context, addr ma.Multiaddr, key thread.Key, collections ...CollectionConfig) (*DB, error)
NewDBFromAddr creates a new db from address and prefixes its datastore with base key. Unlike NewDB, this method takes a list of collections added to the original db that should also be added to this host.
type MarshaledResult ¶
type NetBoostrapper ¶
type NetBoostrapper interface {
corenet.Net
GetIpfsLite() *ipfslite.Peer
Bootstrap(addrs []peer.AddrInfo)
}
DefaultNetwork is a boostrapable default Net with sane defaults.
func DefaultNetwork ¶
func DefaultNetwork(repoPath string, opts ...NetOption) (NetBoostrapper, error)
type NetConfig ¶
type NetConfig struct {
HostAddr ma.Multiaddr
Debug bool
GRPCOptions []grpc.ServerOption
}
type NetOption ¶
func WithNetDebug ¶
func WithNetGRPCOptions ¶
func WithNetGRPCOptions(opts ...grpc.ServerOption) NetOption
func WithNetHostAddr ¶
type Option ¶
Option takes a Config and modifies it
func WithEventCodec ¶
func WithEventCodec(ec core.EventCodec) Option
WithEventCodec configure to use ec as the EventCodec manager for transforming actions in events, and viceversa
func WithJsonMode ¶
func WithLowMem ¶
func WithRepoPath ¶
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query allows to build queries to fetch data from a collection.
func OrderByDesc ¶
OrderByDesc specify descending order for the query results.
func (*Query) Or ¶
Or concatenates a new condition that is sufficient for an instance to satisfy, independent of the current Query. Has left-associativity as: (a And b) Or c
func (*Query) OrderBy ¶
OrderBy specify ascending order for the query results. On multiple calls, only the last one is considered.
func (*Query) OrderByDesc ¶
OrderByDesc specify descending order for the query results. On multiple calls, only the last one is considered.
type SimpleTx ¶
type SimpleTx struct {
// contains filtered or unexported fields
}
SimpleTx implements the transaction interface for datastores who do not have any sort of underlying transactional support
type TxMapDatastore ¶
type TxMapDatastore struct {
*datastore.MapDatastore
// contains filtered or unexported fields
}
func NewTxMapDatastore ¶
func NewTxMapDatastore() *TxMapDatastore
func (*TxMapDatastore) NewTransaction ¶
func (d *TxMapDatastore) NewTransaction(_ bool) (datastore.Txn, error)
type Txn ¶
type Txn struct {
// contains filtered or unexported fields
}
Txn represents a read/write transaction in the db. It allows for serializable isolation level within the db.
func (*Txn) Commit ¶
Commit applies all changes done in the current transaction to the collection. This is a syncrhonous call so changes can be assumed to be applied on function return.
func (*Txn) Create ¶
Create creates new instances in the collection If the ID value on the instance is nil or otherwise a null value (e.g., "" in jsonMode), the ID is updated in-place to reflect the automatically-genereted UUID.
func (*Txn) Delete ¶
func (t *Txn) Delete(ids ...core.InstanceID) error
Delete deletes instances by ID when the current transaction commits.
func (*Txn) Discard ¶
func (t *Txn) Discard()
Discard discards all changes done in the current transaction.
func (*Txn) Find ¶
Find executes a query and stores the result in res which should be a slice of pointers with the correct collection type. If the slice isn't empty, will be emptied.
func (*Txn) FindByID ¶
func (t *Txn) FindByID(id core.InstanceID, v interface{}) error
FindByID gets an instance by ID in the current txn scope.
