db

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: MIT Imports: 40 Imported by: 32

README

DB

DB is a json-document database backed by Threads V2.

This document describes its public API, and its internal design/architecture. Internal understanding isn't necessary to use DB, but will help understand how things are wired. Creating a DB under the default configuration will automatically build everything required to work.

Currently, a DB is backed by a single Thread. In the future, this can change making the DB map different Collections to different Threads, or any other combination.

Usage

ToDo: Describe public API here.

Internal Design

In this section, there is a high-level overview of internal DB design.

Diagram

The following diagram try to express all components and their relationships and communications:

Design

The above diagram depicts the different components inside a DB. Also, it travels through their relationships caused by a transaction commit. The inverse path, caused by a new event detected in other peer log in the thread, is somewhat similar but in the other direction.

Arrows aren't always synchronous calls, but also channel notifications and other mediums in order to inverse dependency between components. Arrows are conceptual communications.

Collections

Collections are part of DB public-api. Main responsibility: store instances of a user-defined schema.

Collections are json-schemas that describe instance types of the DB. They provide the public API for creating, deleting, updating, and querying instances within this collection. They also provide read/write transactions which have serializable isolation within the DB scope.

Indexes

Collections support indexes for faster queries on schema-defined fields. When registering a new schema (and defining a Collection), a caller may supply a list of field paths to index on. This creates an Index, which can be used to speed up queries at the expense of additional storage and compute on instance creation and updates. For dbs with a small number of instances, it may not be worth the added overhead, so as always avoid optimizing your queries until you need it!

Insertion with indexes costs approximately twice as much as without (depending on the complexity and frequency of a given index), whereas updates are only slightly more costly (almost identical in most cases). Depending on the underlying data distribution, queries can be greater than an order of magnitude faster. This depends on many factors, including the size of the db (i.e., number of instances), the uniqueness of the indexed field, and the complexity of the query. For example, in our benchmark tests using a relatively simple Collection and a relatively small db size (i.e., ~5000 instances), the query speedup for a simple OR-based equality test is ~10x. See db/bench_test.go for details or to run the benchmarks yourself.

EventCodec

This is an internal component not available in the public API. Main responsibility: Transform and apply and encode/decode transaction actions.

EventCodec is an abstraction used to:

  • Transform actions made in a txn, to an array of db.Event that will be dispatcher to be reduced.
  • Encode actions made in a txn to a format.Node which will serve as the next building block for the appended Record in the local peer log.
  • The reverse of last point, when receiving external actions to allow to be dispatched.

For example, if within a collection WriteTxn(), a new instance is created and other was updated, these two action will be sent to the EventCodec to transform them in Events. These Event have a byte payload with the encoded transformation. Currently, the only implementation of EventCodec is a jsonpatcher, which transforms these actions in json-merge/patches, and store them as payloads in events.

These events are also aggregated in a returned format.Node, which is the compatible/analogous information to be used by net.Net to add in the peer own log in the thread associated with the DB. Likewise, EventCodec also do the inverse transformation. Given a format.Node, it transforms its byte payload into actions that will be reduced in the db.

The EventCodec abstraction allows an extensibility point. If instead of a json-patcher we want to encode instance changes as full instance snapshots (i.e: instead of generating the json-patch, let generate the full instance data), we could provide another implementation of the EventCodec to use in the DB.

Similarly, more advanced encodings of JSON-Document changes can be implemented as EventCodec such as JSON-Documents-Delta-CRDTs, or a hybrid json-patch with logical clocks.

Dispatcher

This is an internal component not available in the public API. Main responsibility: Source of truth regarding known db.Events for the DB. Will notify registered parties to let them know about new ones..

Every Event generated in the DB is sent to a Dispatcher when write transactions are committed. The dispatcher is responsible for broadcasting these events to all registered Reducers. A reducer is a party which is interested in knowing about DB events. Currently, the only reducer is the DB itself.

For example, if a particular instance is updated in a Collection, these corresponding actions will be encoded as Event by the EventCodec as mentioned in the last section. These Events will be dispatched to the Dispatcher, which will:

  • Store the new event in durable storage. If the txn made multiple changes, this is done transactionally.
  • Broadcast them to all registered Reducers (which currently is only DB). Reducers will apply those changes for their own interests.

The implications of this design imply that real DB state changes can only happen when the Dispatcher broadcast new db.Events. A Reducer can't distinguish between Events generated locally or externally. External events are the results of net.Net sending new events to the Dispatcher, which means that new Events where detected in other peer logs of the same Thread.

Datastore

This is an internal component not available in the public API. Main responsibility: Delivering durable persistence for data.

Datastore is the underlying persistence of Collection instances and Dispatcher raw Event information. In both cases, their interface is a datastore.TxnDatastore to have txn guarantees.

Local Event Bus

This is an internal component not available in the public API. Main responsibility: Deliver format.Node encoded information of changes done in local commited transactions. Currently, only to SingleThreadAdapter is listening to this bus.

DB Listener

This is part of the public-api. Main responsibility: Notify external actors that the DB changed its state, with details about the change: in which collection, what action (Create, Save, Delete), and wich InstanceID.

Listeners are useful for clients that want to be notified about changes in the DB. Recall that DB state can change by external events, such as receiving external changes from other peers sharing the DB.

The client can configure which kind of events wants to be notified. Can add any number of criterias; if more than one criteria is used they will be interpreted as OR conditions. A criteria contains the following information:

  • Which collection to listen changes
  • What action is done (Create, Save, Delete)
  • Which InstanceID

Any of the above three attributes can be set empty. For example, we can listen to all changes of all instances in a collection if only the first attribute is set and the other two are left empty/default.

DBThreadAdapter (SingleThreadAdapter, unique implementation)

This is an internal component not available in the public API. Main responsibility: Responsible to be the two-way communication between DB and Threads.

Every time a new local format.Node is generated in the DB due to a write transaction commit, the DBThreadAdapter will notify net.Net that a new Record should be added to the local peer log.

Similarly, when net.Net detects new Records in other peer logs, it will dispatch them to SingleThreadAdapter. Then, it will transform it into a DB Events that will be dispatched to Dispatcher and ultimately will be reduced to impact DB state.

As said initially, currently, the DB is only mapped to a single Thread. But is possible to decide a different map, where a DB might be backed by more than one thread or any other schema. This is the component that should be taking this decisions.

net.Net

This component is part of the public-api so that it can be accessed. Main responsibility: Is the DB interface with Threads layer.

net.Net is the bidirectional communication interface to the underlying Thread backing the DB. It only interacts with DBThreadAdapter

Documentation

Overview

Package db provides a DB which manage collections

Index

Constants

View Source
const (
	// Eq is "equals"
	Eq = Operation(eq)
	// Ne is "not equal to"
	Ne = Operation(ne)
	// Gt is "greater than"
	Gt = Operation(gt)
	// Lt is "less than"
	Lt = Operation(lt)
	// Ge is "greater than or equal to"
	Ge = Operation(ge)
	// Le is "less than or equal to"
	Le = Operation(le)
)

Variables

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

View Source
var (

	// ErrInvalidCollectionSchema indicates the provided schema isn't valid for a Collection.
	ErrInvalidCollectionSchema = errors.New("the collection schema should specify an _id string property")
)
View Source
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

func DefaultDecode(data []byte, value interface{}) error

DefaultDecode is the default decoding func from badgerhold (Gob)

func DefaultEncode

func DefaultEncode(value interface{}) ([]byte, error)

DefaultEncode is the default encoding func from badgerhold (Gob)

func NewSimpleTx

func NewSimpleTx(ds datastore.Datastore) datastore.Txn

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.

func (*Collection) SaveMany added in v0.1.13

func (c *Collection) SaveMany(vs [][]byte, opts ...TxnOption) error

SaveMany saves changes of multiple instances in the collection.

func (*Collection) WriteTxn

func (c *Collection) WriteTxn(f func(txn *Txn) error, opts ...TxnOption) error

WriteTxn creates an explicit write transaction. Provides serializable isolation gurantees.

type CollectionConfig

type CollectionConfig struct {
	Name    string
	Schema  *jsonschema.Schema
	Indexes []IndexConfig
}

CollectionConfig describes a new Collection.

type Comparer

type Comparer interface {
	Compare(other interface{}) (int, error)
}

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

func Where

func Where(field string) *Criterion

Where starts to create a query condition for a field

func (*Criterion) Eq

func (c *Criterion) Eq(value interface{}) *Query

Eq is an equality operator against a field

func (*Criterion) Ge

func (c *Criterion) Ge(value interface{}) *Query

Ge is a greater or equal operator against a field

func (*Criterion) Gt

func (c *Criterion) Gt(value interface{}) *Query

Gt is a greater operator against a field

func (*Criterion) Le

func (c *Criterion) Le(value interface{}) *Query

Le is a less or equal operator against a field

func (*Criterion) Lt

func (c *Criterion) Lt(value interface{}) *Query

Lt is a less operation against a field

func (*Criterion) Ne

func (c *Criterion) Ne(value interface{}) *Query

Ne is a not equal operator against a field

func (*Criterion) Validate added in v0.1.15

func (c *Criterion) Validate() error

type DB

type DB struct {
	io.Closer
	// contains filtered or unexported fields
}

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

func NewDB(ctx context.Context, network app.Net, id thread.ID, opts ...NewDBOption) (*DB, error)

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) Close

func (d *DB) Close() error

Close closes the db.

func (*DB) GetCollection

func (d *DB) GetCollection(name string) *Collection

GetCollection returns a collection by name.

func (*DB) GetDBInfo added in v0.1.13

func (d *DB) GetDBInfo(opts ...InviteInfoOption) ([]ma.Multiaddr, thread.Key, error)

GetDBInfo returns the addresses and key that can be used to join the DB thread

func (*DB) HandleNetRecord added in v0.1.13

func (d *DB) HandleNetRecord(rec net.ThreadRecord, key thread.Key, lid peer.ID, timeout time.Duration) error

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.

func (*DB) Reduce

func (d *DB) Reduce(events []core.Event) error

Reduce processes txn events into the collections.

type Datastore

type Datastore struct {
	kt.KeyTransform
	ds.Datastore
	// contains filtered or unexported fields
}

Datastore keeps a KeyTransform function

func (*Datastore) NewTransaction

func (d *Datastore) NewTransaction(readOnly bool) (ds.Txn, error)

type DecodeFunc

type DecodeFunc func(data []byte, value interface{}) error

DecodeFunc is a function for decoding a value from bytes

type EncodeFunc

type EncodeFunc func(value interface{}) ([]byte, error)

EncodeFunc is a function for encoding a value into bytes

type Index

type Index struct {
	IndexFunc func(name string, value []byte) (ds.Key, error)
	Unique    bool
}

Index is a function that returns the indexable, encoded bytes of the passed in bytes

type IndexConfig

type IndexConfig struct {
	Path   string `json:"path"`
	Unique bool   `json:"unique,omitempty"`
}

IndexConfig stores the configuration for a given Index.

type Indexer

type Indexer interface {
	BaseKey() ds.Key
	Indexes() map[string]Index //[indexname]indexFunc
}

Indexer is the interface to implement to support Collection indexes

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

type InviteInfoOptions struct {
	Token thread.Token
}

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 Listener

type Listener interface {
	Channel() <-chan Action
	Close()
}

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

type ManagedDBOptions struct {
	Token thread.Token
}

ManagedDBOptions defines options for interacting with a managed db.

type Manager

type Manager struct {
	io.Closer
	// contains filtered or unexported fields
}

func NewManager

func NewManager(network app.Net, opts ...NewDBOption) (*Manager, error)

NewManager hydrates and starts dbs from prefixes.

func (*Manager) Close

func (m *Manager) Close() error

Close all dbs.

func (*Manager) DeleteDB added in v0.1.13

func (m *Manager) DeleteDB(ctx context.Context, id thread.ID, opts ...ManagedDBOption) error

DeleteDB deletes a db by id.

func (*Manager) GetDB

func (m *Manager) GetDB(ctx context.Context, id thread.ID, opts ...ManagedDBOption) (*DB, error)

GetDB returns a db by id.

func (*Manager) GetToken added in v0.1.13

func (m *Manager) GetToken(ctx context.Context, identity thread.Identity) (thread.Token, error)

GetToken provides access to thread network tokens.

func (*Manager) Net

func (m *Manager) Net() net.Net

Net returns the manager's thread network.

func (*Manager) NewDB

func (m *Manager) NewDB(ctx context.Context, id thread.ID, opts ...NewManagedDBOption) (*DB, error)

NewDB creates a new db and prefixes its datastore with base key.

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 MarshaledResult struct {
	query.Result
	MarshaledValue map[string]interface{}
}

type MatchFunc

type MatchFunc func(value interface{}) (bool, error)

MatchFunc is a function used to test an arbitrary matching value in a query

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 Operation added in v0.1.13

type Operation int

Operation models comparison operators

type Query

type Query struct {
	Ands  []*Criterion
	Ors   []*Query
	Sort  Sort
	Index string
}

Query is a json-seriable query representation

func OrderBy

func OrderBy(field string) *Query

OrderBy specify ascending order for the query results.

func OrderByDesc

func OrderByDesc(field string) *Query

OrderByDesc specify descending order for the query results.

func (*Query) And

func (q *Query) And(field string) *Criterion

And concatenates a new condition in an existing field.

func (*Query) Or

func (q *Query) Or(orQuery *Query) *Query

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

func (q *Query) OrderBy(field string) *Query

OrderBy specify ascending order for the query results. On multiple calls, only the last one is considered.

func (*Query) OrderByDesc

func (q *Query) OrderByDesc(field string) *Query

OrderByDesc specify descending order for the query results. On multiple calls, only the last one is considered.

func (*Query) UseIndex added in v0.1.13

func (q *Query) UseIndex(path string) *Query

UseIndex specifies the index to use when running this query

func (*Query) Validate added in v0.1.15

func (q *Query) Validate() error

type Reducer

type Reducer interface {
	Reduce(events []core.Event) error
}

Reducer applies an event to an existing state.

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

func (*SimpleTx) Commit

func (bt *SimpleTx) Commit() error

func (*SimpleTx) Delete

func (bt *SimpleTx) Delete(key datastore.Key) error

func (*SimpleTx) Discard

func (bt *SimpleTx) Discard()

func (*SimpleTx) Get

func (bt *SimpleTx) Get(k datastore.Key) ([]byte, error)

func (*SimpleTx) GetSize

func (bt *SimpleTx) GetSize(k datastore.Key) (int, error)

func (*SimpleTx) Has

func (bt *SimpleTx) Has(k datastore.Key) (bool, error)

func (*SimpleTx) Put

func (bt *SimpleTx) Put(key datastore.Key, val []byte) error

func (*SimpleTx) Query

func (bt *SimpleTx) Query(q query.Query) (query.Results, error)

type Sort added in v0.1.13

type Sort struct {
	FieldPath string
	Desc      bool
}

Sort represents a sort order on a field

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

func (t *Txn) Commit() error

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) Find

func (t *Txn) Find(q *Query) ([][]byte, error)

Find queries for instances by Query

func (*Txn) FindByID

func (t *Txn) FindByID(id core.InstanceID) ([]byte, error)

FindByID gets an instance by ID in the current txn scope.

func (*Txn) Has

func (t *Txn) Has(ids ...core.InstanceID) (bool, error)

Has returns true if all IDs exists in the collection, false otherwise.

func (*Txn) Save

func (t *Txn) Save(updated ...[]byte) error

Save saves an instance changes to be commited when the current transaction commits.

type TxnOption added in v0.1.13

type TxnOption func(*TxnOptions)

TxnOption specifies a transaction option.

func WithTxnToken added in v0.1.13

func WithTxnToken(t thread.Token) TxnOption

WithTxnToken provides authorization for the transaction.

type TxnOptions added in v0.1.13

type TxnOptions struct {
	Token thread.Token
}

TxnOptions defines options for a transaction.

type Value added in v0.1.13

type Value struct {
	String *string
	Bool   *bool
	Float  *float64
}

Value models a single value in JSON

Jump to

Keyboard shortcuts

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