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(config IndexConfig) error
- func (c *Collection) BaseKey() ds.Key
- func (c *Collection) Create(v []byte, opts ...TxnOption) (id core.InstanceID, err error)
- func (c *Collection) CreateMany(vs [][]byte, opts ...TxnOption) (ids []core.InstanceID, err error)
- func (c *Collection) Delete(id core.InstanceID, opts ...TxnOption) error
- func (c *Collection) DeleteMany(ids []core.InstanceID, opts ...TxnOption) error
- func (c *Collection) Find(q *Query, opts ...TxnOption) (instances [][]byte, err error)
- func (c *Collection) FindByID(id core.InstanceID, opts ...TxnOption) (instance []byte, err error)
- func (c *Collection) Has(id core.InstanceID, opts ...TxnOption) (exists bool, err error)
- func (c *Collection) HasMany(ids []core.InstanceID, opts ...TxnOption) (exists bool, err error)
- func (c *Collection) Indexes() map[string]Index
- func (c *Collection) ReadTxn(f func(txn *Txn) error, opts ...TxnOption) error
- func (c *Collection) Save(v []byte, opts ...TxnOption) error
- func (c *Collection) SaveMany(vs [][]byte, opts ...TxnOption) error
- func (c *Collection) WriteTxn(f func(txn *Txn) error, opts ...TxnOption) error
- type CollectionConfig
- type Comparer
- type Criterion
- func (c *Criterion) Eq(value interface{}) *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
- func (c *Criterion) Validate() error
- type DB
- func (d *DB) Close() error
- func (d *DB) GetCollection(name string) *Collection
- func (d *DB) GetDBInfo(opts ...InviteInfoOption) ([]ma.Multiaddr, thread.Key, error)
- func (d *DB) HandleNetRecord(rec net.ThreadRecord, key thread.Key, lid peer.ID, timeout time.Duration) error
- func (d *DB) Listen(los ...ListenOption) (Listener, error)
- func (d *DB) LocalEventListen() *app.LocalEventListener
- func (d *DB) NewCollection(config CollectionConfig) (*Collection, error)
- func (d *DB) Reduce(events []core.Event) error
- type Datastore
- type DecodeFunc
- type EncodeFunc
- type Index
- type IndexConfig
- type Indexer
- type InviteInfoOption
- type InviteInfoOptions
- type ListenActionType
- type ListenOption
- type Listener
- type ManagedDBOption
- type ManagedDBOptions
- type Manager
- func (m *Manager) Close() error
- func (m *Manager) DeleteDB(ctx context.Context, id thread.ID, opts ...ManagedDBOption) error
- func (m *Manager) GetDB(ctx context.Context, id thread.ID, opts ...ManagedDBOption) (*DB, error)
- func (m *Manager) GetToken(ctx context.Context, identity thread.Identity) (thread.Token, error)
- func (m *Manager) Net() net.Net
- func (m *Manager) NewDB(ctx context.Context, id thread.ID, opts ...NewManagedDBOption) (*DB, error)
- func (m *Manager) NewDBFromAddr(ctx context.Context, addr ma.Multiaddr, key thread.Key, ...) (*DB, error)
- type MarshaledResult
- type MatchFunc
- type NewDBOption
- func WithNewDBCollections(cs ...CollectionConfig) NewDBOption
- func WithNewDBDebug(enable bool) NewDBOption
- func WithNewDBEventCodec(ec core.EventCodec) NewDBOption
- func WithNewDBLowMem(low bool) NewDBOption
- func WithNewDBRepoPath(path string) NewDBOption
- func WithNewDBToken(t thread.Token) NewDBOption
- type NewDBOptions
- type NewManagedDBOption
- type NewManagedDBOptions
- type Operation
- 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 Sort
- type TxMapDatastore
- type Txn
- func (t *Txn) Commit() error
- func (t *Txn) Create(new ...[]byte) ([]core.InstanceID, error)
- func (t *Txn) Delete(ids ...core.InstanceID) error
- func (t *Txn) Discard()
- func (t *Txn) Find(q *Query) ([][]byte, error)
- func (t *Txn) FindByID(id core.InstanceID) ([]byte, error)
- func (t *Txn) Has(ids ...core.InstanceID) (bool, error)
- func (t *Txn) Save(updated ...[]byte) error
- type TxnOption
- type TxnOptions
- type Value
Constants ¶
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 ( // ErrInvalidCollectionSchema indicates the provided schema isn't valid for a Collection. ErrInvalidCollectionSchema = errors.New("the collection schema should specify an _id string property") )
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") )
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(config IndexConfig) 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(v []byte, opts ...TxnOption) (id core.InstanceID, err error)
Create creates an instance in the collection.
func (*Collection) CreateMany ¶ added in v0.1.13
func (c *Collection) CreateMany(vs [][]byte, opts ...TxnOption) (ids []core.InstanceID, err error)
CreateMany creates multiple instances in the collection.
func (*Collection) Delete ¶
func (c *Collection) Delete(id core.InstanceID, opts ...TxnOption) error
Delete deletes an instance by its ID. It doesn't fail if the ID doesn't exist.
func (*Collection) DeleteMany ¶ added in v0.1.13
func (c *Collection) DeleteMany(ids []core.InstanceID, opts ...TxnOption) error
Delete deletes multiple instances by ID. It doesn't fail if one of the IDs don't exist.
func (*Collection) Find ¶
func (c *Collection) Find(q *Query, opts ...TxnOption) (instances [][]byte, err error)
Find executes a Query and returns the result.
func (*Collection) FindByID ¶
func (c *Collection) FindByID(id core.InstanceID, opts ...TxnOption) (instance []byte, err error)
FindByID finds an instance by its ID. If doesn't exists returns ErrNotFound.
func (*Collection) Has ¶
func (c *Collection) Has(id core.InstanceID, opts ...TxnOption) (exists bool, err error)
Has returns true if ID exists in the collection, false otherwise.
func (*Collection) HasMany ¶ added in v0.1.13
func (c *Collection) HasMany(ids []core.InstanceID, opts ...TxnOption) (exists bool, err error)
HasMany 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, opts ...TxnOption) 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(v []byte, opts ...TxnOption) error
Save saves changes of an instance in the collection.
type CollectionConfig ¶
type CollectionConfig struct {
Name string
Schema *jsonschema.Schema
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 Criterion ¶
type Criterion struct {
FieldPath string
Operation Operation
Value Value
// contains filtered or unexported fields
}
Criterion represents a restriction on 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 app.Net, addr ma.Multiaddr, key thread.Key, opts ...NewDBOption) (*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) GetDBInfo ¶ added in v0.1.13
GetDBInfo returns the addresses and key that can be used to join the DB thread
func (*DB) HandleNetRecord ¶ added in v0.1.13
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) LocalEventListen ¶ added in v0.1.13
func (d *DB) LocalEventListen() *app.LocalEventListener
func (*DB) NewCollection ¶
func (d *DB) NewCollection(config CollectionConfig) (*Collection, error)
NewCollection creates a new collection in the db with a JSON schema.
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 InviteInfoOption ¶ added in v0.1.13
type InviteInfoOption func(*InviteInfoOptions)
InviteInfoOption specifies a managed db option.
func WithInviteInfoToken ¶ added in v0.1.13
func WithInviteInfoToken(t thread.Token) InviteInfoOption
WithInviteInfoToken provides authorization for accessing DB invite info.
type InviteInfoOptions ¶ added in v0.1.13
InviteInfoOptions defines options getting DB invite info.
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 ManagedDBOption ¶ added in v0.1.13
type ManagedDBOption func(*ManagedDBOptions)
ManagedDBOption specifies a managed db option.
func WithManagedDBToken ¶ added in v0.1.13
func WithManagedDBToken(t thread.Token) ManagedDBOption
WithManagedDBToken provides authorization for interacting with a managed db.
type ManagedDBOptions ¶ added in v0.1.13
ManagedDBOptions defines options for interacting with a managed db.
type Manager ¶
func NewManager ¶
func NewManager(network app.Net, opts ...NewDBOption) (*Manager, error)
NewManager hydrates and starts dbs from prefixes.
func (*Manager) NewDBFromAddr ¶
func (m *Manager) NewDBFromAddr(ctx context.Context, addr ma.Multiaddr, key thread.Key, opts ...NewManagedDBOption) (*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 NewDBOption ¶ added in v0.1.13
type NewDBOption func(*NewDBOptions) error
NewDBOption takes a Config and modifies it.
func WithNewDBCollections ¶ added in v0.1.13
func WithNewDBCollections(cs ...CollectionConfig) NewDBOption
WithNewDBCollections is used to specify collections that will be created.
func WithNewDBDebug ¶ added in v0.1.13
func WithNewDBDebug(enable bool) NewDBOption
WithNewDBDebug indicate to output debug information.
func WithNewDBEventCodec ¶ added in v0.1.13
func WithNewDBEventCodec(ec core.EventCodec) NewDBOption
WithNewDBEventCodec configure to use ec as the EventCodec for transforming actions in events, and viceversa.
func WithNewDBLowMem ¶ added in v0.1.13
func WithNewDBLowMem(low bool) NewDBOption
WithNewDBLowMem specifies whether or not to use low memory settings.
func WithNewDBRepoPath ¶ added in v0.1.13
func WithNewDBRepoPath(path string) NewDBOption
WithNewDBRepoPath sets the repo path.
func WithNewDBToken ¶ added in v0.1.13
func WithNewDBToken(t thread.Token) NewDBOption
WithNewDBToken provides authorization for interacting with a db.
type NewDBOptions ¶ added in v0.1.13
type NewDBOptions struct {
RepoPath string
Datastore ds.TxnDatastore
EventCodec core.EventCodec
Debug bool
LowMem bool
Collections []CollectionConfig
Token thread.Token
}
NewDBOptions has configuration parameters for a db.
type NewManagedDBOption ¶ added in v0.1.13
type NewManagedDBOption func(*NewManagedDBOptions)
NewManagedDBOption specifies a new managed db option.
func WithNewManagedDBCollections ¶ added in v0.1.13
func WithNewManagedDBCollections(cs ...CollectionConfig) NewManagedDBOption
WithNewManagedDBCollections is used to specify collections that will be created in a managed db.
func WithNewManagedDBToken ¶ added in v0.1.13
func WithNewManagedDBToken(t thread.Token) NewManagedDBOption
WithNewManagedDBToken provides authorization for creating a new managed db.
type NewManagedDBOptions ¶ added in v0.1.13
type NewManagedDBOptions struct {
Collections []CollectionConfig
Token thread.Token
}
NewManagedDBOptions defines options for creating a new managed db.
type Query ¶
Query is a json-seriable query representation
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, independant 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 ¶
func (t *Txn) Create(new ...[]byte) ([]core.InstanceID, error)
Create creates new instances in the collection If the ID value on the instance is nil or otherwise a null value (e.g., ""), and ID is generated and used to store the instance.
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) FindByID ¶
func (t *Txn) FindByID(id core.InstanceID) ([]byte, error)
FindByID gets an instance by ID in the current txn scope.
type TxnOption ¶ added in v0.1.13
type TxnOption func(*TxnOptions)
TxnOption specifies a transaction option.
func WithTxnToken ¶ added in v0.1.13
WithTxnToken provides authorization for the transaction.
type TxnOptions ¶ added in v0.1.13
TxnOptions defines options for a transaction.
