Documentation
¶
Overview ¶
Package databases implements the OrbitDB database types on top of the operation log: Events (an append-only event log), KeyValue, KeyValueIndexed and Documents.
It mirrors src/database.js and src/databases in @orbitdb/core 4. Operations are stored as the same {op, key, value} payloads, so a database written by one implementation reads the same in the other.
Index ¶
- Constants
- Variables
- func UseDatabaseType(typ string, f Factory) error
- type Database
- func (db *Database) AccessController() oplog.AccessController
- func (db *Database) AddOperation(ctx context.Context, op any) (string, error)
- func (db *Database) Address() string
- func (db *Database) Close() error
- func (db *Database) Drop(ctx context.Context) error
- func (db *Database) Events() *Emitter
- func (db *Database) Identity() *identitytypes.Identity
- func (db *Database) Log() *oplog.Log
- func (db *Database) Meta() any
- func (db *Database) Name() string
- func (db *Database) Peers() []peer.ID
- func (db *Database) Sync() *syncutils.Sync
- type Document
- type Documents
- func (d *Documents) All(ctx context.Context) ([]Document, error)
- func (d *Documents) Del(ctx context.Context, key string) (string, error)
- func (d *Documents) Get(ctx context.Context, key string) (*Document, error)
- func (d *Documents) IndexBy() string
- func (d *Documents) Iterator(ctx context.Context, amount int) iter.Seq2[Document, error]
- func (d *Documents) Put(ctx context.Context, doc any) (string, error)
- func (d *Documents) Query(ctx context.Context, match func(doc map[string]any) bool) ([]map[string]any, error)
- func (*Documents) Type() string
- type DocumentsOptions
- type Emitter
- type Event
- type EventEntry
- type EventType
- type Events
- func (e *Events) Add(ctx context.Context, value any) (string, error)
- func (e *Events) All(ctx context.Context) ([]EventEntry, error)
- func (e *Events) Get(ctx context.Context, hash string) (any, error)
- func (e *Events) Iterator(ctx context.Context, opts oplog.IteratorOptions) iter.Seq2[EventEntry, error]
- func (*Events) Type() string
- type Factory
- type KeyValue
- func (kv *KeyValue) All(ctx context.Context) ([]KeyValueEntry, error)
- func (kv *KeyValue) Del(ctx context.Context, key string) (string, error)
- func (kv *KeyValue) Get(ctx context.Context, key string) (any, error)
- func (kv *KeyValue) Iterator(ctx context.Context, amount int) iter.Seq2[KeyValueEntry, error]
- func (kv *KeyValue) Put(ctx context.Context, key string, value any) (string, error)
- func (kv *KeyValue) Set(ctx context.Context, key string, value any) (string, error)
- func (*KeyValue) Type() string
- type KeyValueEntry
- type KeyValueIndexed
- func (kvi *KeyValueIndexed) All(ctx context.Context) ([]KeyValueEntry, error)
- func (kvi *KeyValueIndexed) Close() error
- func (kvi *KeyValueIndexed) Drop(ctx context.Context) error
- func (kvi *KeyValueIndexed) Get(ctx context.Context, key string) (any, error)
- func (kvi *KeyValueIndexed) Iterator(ctx context.Context, amount int) iter.Seq2[KeyValueEntry, error]
- type Operation
- type Params
- type Store
- type Subscription
Constants ¶
const ( // DefaultReferencesCount is how many older entries each new entry // refers to, beyond the heads. DefaultReferencesCount = 16 // DefaultDirectory is where databases keep their files. DefaultDirectory = "./orbitdb" )
const DefaultIndexBy = "_id"
DefaultIndexBy is the document field Documents uses as the key.
const DocumentsType = "documents"
DocumentsType is the type name of Documents databases.
const EventsType = "events"
EventsType is the type name of Events databases.
const KeyValueType = "keyvalue"
KeyValueType is the type name of KeyValue databases.
Variables ¶
var ErrNotFound = errors.New("databases: not found")
ErrNotFound is returned when a key, document or event does not exist.
Functions ¶
func UseDatabaseType ¶
UseDatabaseType registers a database type, replacing any factory registered under the same name.
Types ¶
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database is the base every database type builds on: an operation log, the sync protocol replicating it and an event stream.
func (*Database) AccessController ¶
func (db *Database) AccessController() oplog.AccessController
AccessController returns the access controller.
func (*Database) AddOperation ¶
AddOperation appends op to the log, announces it to peers and emits EventUpdate. It returns the new entry's hash.
func (*Database) Close ¶
Close stops syncing, closes the log and its storages and emits EventClose. Closing twice is a no-op.
func (*Database) Identity ¶
func (db *Database) Identity() *identitytypes.Identity
Identity returns the identity the database writes as.
type Documents ¶
type Documents struct {
*Database
// contains filtered or unexported fields
}
Documents stores documents (maps, or structs that encode to maps) keyed by one of their fields.
func NewDocuments ¶
NewDocuments opens a Documents database.
func (*Documents) Get ¶
Get returns the document with the given key, or an error wrapping ErrNotFound. A document stored under a numeric key by another peer is found by the number's decimal form.
func (*Documents) Iterator ¶
Iterator yields the current version of every document, most recently written first. amount limits the number of documents; zero or negative means all.
func (*Documents) Put ¶
Put stores doc under the value of its IndexBy field and returns the hash of the entry. doc may be a map or a struct (encoded through its JSON form).
type DocumentsOptions ¶
type DocumentsOptions struct {
// IndexBy is the field holding each document's key. Defaults to
// DefaultIndexBy.
IndexBy string
}
DocumentsOptions configures a Documents database.
type Emitter ¶
type Emitter struct {
// contains filtered or unexported fields
}
Emitter fans events out to subscriptions. Emitting never blocks: each subscription buffers events until its reader takes them.
func (*Emitter) Subscribe ¶
func (e *Emitter) Subscribe() *Subscription
Subscribe returns a subscription that receives every event emitted from now on. Call Close when done with it. After the database closes, the channel delivers the remaining events, ending with EventClose, and is closed.
type EventEntry ¶
EventEntry is an event and the hash of the entry that holds it.
type EventType ¶
type EventType int
EventType identifies a database event.
const ( // EventUpdate: an entry was appended locally or joined from a peer. // Event.Entry is set. EventUpdate EventType = iota + 1 // EventJoin: a heads exchange with a peer completed. Event.Peer and // Event.Heads are set. EventJoin // EventLeave: a peer stopped replicating. Event.Peer is set. EventLeave // EventError: a background error, e.g. an invalid entry from a peer. // Event.Err is set. EventError // EventClose: the database was closed. It is the last event. EventClose // EventDrop: the database was dropped. EventDrop )
Database events, as emitted by @orbitdb/core's database.events.
type Events ¶
type Events struct {
*Database
}
Events is an immutable, append-only event log.
func (*Events) All ¶
func (e *Events) All(ctx context.Context) ([]EventEntry, error)
All returns every event, oldest first.
type Factory ¶
Factory opens a database of one type.
func DocumentsFactory ¶
func DocumentsFactory(opts DocumentsOptions) Factory
DocumentsFactory returns a Factory for Documents databases with opts.
func GetDatabaseType ¶
GetDatabaseType returns the factory registered for typ.
type KeyValue ¶
type KeyValue struct {
*Database
}
KeyValue is a key/value store. Reads walk the log from the heads, so the latest write to a key wins; KeyValueIndexed keeps an index instead.
func NewKeyValue ¶
NewKeyValue opens a KeyValue database.
func (*KeyValue) All ¶
func (kv *KeyValue) All(ctx context.Context) ([]KeyValueEntry, error)
All returns every key and its current value, least recently written first.
func (*KeyValue) Get ¶
Get returns the value of key, or an error wrapping ErrNotFound if the key was never set or was deleted.
func (*KeyValue) Iterator ¶
Iterator yields the current value of every key, most recently written first. amount limits the number of keys; zero or negative means all.
type KeyValueEntry ¶
KeyValueEntry is a key, its value and the hash of the entry that set it.
type KeyValueIndexed ¶
type KeyValueIndexed struct {
*KeyValue
// contains filtered or unexported fields
}
KeyValueIndexed is a KeyValue database that keeps the latest value of every key in a LevelDB index, so reads do not walk the log. Its type name is KeyValueType: it is a drop-in replacement, opened with orbitdb.WithDatabase(databases.KeyValueIndexedFactory).
func NewKeyValueIndexed ¶
func NewKeyValueIndexed(ctx context.Context, p Params) (*KeyValueIndexed, error)
NewKeyValueIndexed opens a KeyValueIndexed database. The index lives in Directory/<address>/_index.
func (*KeyValueIndexed) All ¶
func (kvi *KeyValueIndexed) All(ctx context.Context) ([]KeyValueEntry, error)
All returns every key and its value, in ascending key order.
func (*KeyValueIndexed) Close ¶
func (kvi *KeyValueIndexed) Close() error
Close closes the database and its index.
func (*KeyValueIndexed) Drop ¶
func (kvi *KeyValueIndexed) Drop(ctx context.Context) error
Drop drops the database and clears its index.
func (*KeyValueIndexed) Get ¶
Get returns the value of key from the index, or an error wrapping ErrNotFound.
func (*KeyValueIndexed) Iterator ¶
func (kvi *KeyValueIndexed) Iterator(ctx context.Context, amount int) iter.Seq2[KeyValueEntry, error]
Iterator yields every key and its value from the index, in descending key order. amount limits the number of keys; zero or negative means all.
type Operation ¶
Operation is the payload database types append: {op, key, value}.
func ParseOperation ¶
ParseOperation reads an {op, key, value} payload. ok is false for payloads of another shape.
type Params ¶
type Params struct {
// IPFS provides block storage and networking. Required unless every
// storage is given and sync is disabled.
IPFS *ipfs.Node
// Identity writes to the database. Required.
Identity *identitytypes.Identity
// Address is the database address; it is also the log id and the sync
// topic. Required.
Address string
Name string
// AccessController decides who may write. Defaults to anyone.
AccessController oplog.AccessController
// Directory holds the database files, under Directory/<address>.
// Defaults to DefaultDirectory.
Directory string
// Meta is the manifest metadata.
Meta any
// EntryStorage defaults to an LRU cache over IPFS block storage;
// HeadsStorage and IndexStorage to an LRU cache over LevelDB in
// Directory.
HeadsStorage storage.Storage
EntryStorage storage.Storage
IndexStorage storage.Storage
// ReferencesCount is passed to Log.Append. Zero means
// DefaultReferencesCount; negative means no references.
ReferencesCount int
// DisableAutoSync leaves sync stopped until Sync().Start() is called.
DisableAutoSync bool
// OnUpdate is called, before EventUpdate is emitted, for every entry
// appended or joined.
OnUpdate func(ctx context.Context, log *oplog.Log, entry *oplog.Entry) error
// OnClose is called once the database has closed.
OnClose func()
// Encryption turns on entry and/or payload encryption.
Encryption oplog.Encryption
}
Params configures a database. OrbitDB.Open fills it; it is exported for custom database types and for using a database without OrbitDB.
type Store ¶
type Store interface {
Type() string
Address() string
Name() string
Identity() *identitytypes.Identity
Meta() any
Log() *oplog.Log
Sync() *syncutils.Sync
Peers() []peer.ID
Events() *Emitter
AccessController() oplog.AccessController
AddOperation(ctx context.Context, op any) (string, error)
Close() error
Drop(ctx context.Context) error
}
Store is what every database type provides. OrbitDB.Open returns one; type-assert it to *Events, *KeyValue, *KeyValueIndexed or *Documents, or use the typed Open helpers of package orbitdb.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription receives database events.
func (*Subscription) Close ¶
func (s *Subscription) Close()
Close unsubscribes and discards undelivered events. The channel is closed.
func (*Subscription) Events ¶
func (s *Subscription) Events() <-chan Event
Events returns the channel events are delivered on.