Documentation
¶
Overview ¶
Package database provides database primitives such as tables, transactions and indexes.
Index ¶
- Variables
- type Database
- type FieldConstraint
- type FieldConstraints
- type Index
- type IndexConfig
- type Options
- type Table
- func (t *Table) AscendGreaterOrEqual(pivot document.Value, fn func(d document.Document) error) error
- func (t *Table) Delete(key []byte) error
- func (t *Table) DescendLessOrEqual(pivot document.Value, fn func(d document.Document) error) error
- func (t *Table) EncodeValueToKey(v document.Value) ([]byte, error)
- func (t *Table) GetDocument(key []byte) (document.Document, error)
- func (t *Table) Indexes() (map[string]Index, error)
- func (t *Table) Info() (*TableInfo, error)
- func (t *Table) Insert(d document.Document) (document.Document, error)
- func (t *Table) Iterate(fn func(d document.Document) error) error
- func (t *Table) Name() string
- func (t *Table) ReIndex() error
- func (t *Table) Replace(key []byte, d document.Document) error
- func (t *Table) Truncate() error
- func (t *Table) Tx() *Transaction
- type TableInfo
- type Transaction
- func (tx *Transaction) AddField(tableName string, fc FieldConstraint) error
- func (tx *Transaction) Commit() error
- func (tx *Transaction) CreateIndex(opts IndexConfig) error
- func (tx *Transaction) CreateTable(name string, info *TableInfo) error
- func (tx *Transaction) DB() *Database
- func (tx *Transaction) DropIndex(name string) error
- func (tx *Transaction) DropTable(name string) error
- func (tx *Transaction) GetIndex(name string) (*Index, error)
- func (tx *Transaction) GetTable(name string) (*Table, error)
- func (tx *Transaction) ListIndexes() ([]*IndexConfig, error)
- func (tx *Transaction) ReIndex(indexName string) error
- func (tx *Transaction) ReIndexAll() error
- func (tx *Transaction) RenameTable(oldName, newName string) error
- func (tx *Transaction) Rollback() error
- func (tx *Transaction) Writable() bool
- type TxOptions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTableNotFound is returned when the targeted table doesn't exist. ErrTableNotFound = errors.New("table not found") // ErrTableAlreadyExists is returned when attempting to create a table with the // same name as an existing one. ErrTableAlreadyExists = errors.New("table already exists") // ErrIndexNotFound is returned when the targeted index doesn't exist. ErrIndexNotFound = errors.New("index not found") // ErrIndexAlreadyExists is returned when attempting to create an index with the // same name as an existing one. ErrIndexAlreadyExists = errors.New("index already exists") // ErrDocumentNotFound is returned when no document is associated with the provided key. ErrDocumentNotFound = errors.New("document not found") // ErrDuplicateDocument is returned when another document is already associated with a given key, primary key, // or if there is a unique index violation. ErrDuplicateDocument = errors.New("duplicate document") )
Functions ¶
This section is empty.
Types ¶
type Database ¶
type Database struct { // Codec used to encode documents. Defaults to MessagePack. Codec encoding.Codec // contains filtered or unexported fields }
A Database manages a list of tables in an engine.
func (*Database) Begin ¶
func (db *Database) Begin(writable bool) (*Transaction, error)
Begin starts a new transaction with default options. The returned transaction must be closed either by calling Rollback or Commit.
func (*Database) BeginTx ¶ added in v0.8.0
BeginTx starts a new transaction with the given options. If opts is empty, it will use the default options. The returned transaction must be closed either by calling Rollback or Commit. If the Attached option is passed, it opens a database level transaction, which gets attached to the database and prevents any other transaction to be opened afterwards until it gets rolled back or commited.
func (*Database) GetAttachedTx ¶ added in v0.8.0
func (db *Database) GetAttachedTx() *Transaction
GetAttachedTx returns the transaction attached to the database. It returns nil if there is no such transaction. The returned transaction is not thread safe.
type FieldConstraint ¶
type FieldConstraint struct { Path document.Path Type document.ValueType IsPrimaryKey bool IsNotNull bool DefaultValue document.Value }
FieldConstraint describes constraints on a particular field.
func (*FieldConstraint) HasDefaultValue ¶ added in v0.9.0
func (f *FieldConstraint) HasDefaultValue() bool
func (*FieldConstraint) ScanDocument ¶
func (f *FieldConstraint) ScanDocument(d document.Document) error
ScanDocument implements the document.Scanner interface.
func (*FieldConstraint) ToDocument ¶
func (f *FieldConstraint) ToDocument() document.Document
ToDocument returns a document from f.
type FieldConstraints ¶ added in v0.9.0
type FieldConstraints []FieldConstraint
FieldConstraints is a list of field constraints.
func (FieldConstraints) Convert ¶ added in v0.9.0
func (f FieldConstraints) Convert(d document.Document) (*document.FieldBuffer, error)
Convert the document using the field constraints. It converts any path that has a field constraint on it into the specified type. If there is no constraint on an integer field or value, it converts it into a double. Default values on missing fields are not applied.
func (FieldConstraints) ValidateDocument ¶ added in v0.9.0
func (f FieldConstraints) ValidateDocument(d document.Document) (*document.FieldBuffer, error)
ValidateDocument calls Convert then ensures the document validates against the field constraints.
type Index ¶
type Index struct { *index.Index Opts IndexConfig }
Index of a table field. Contains information about the index configuration and provides methods to manipulate the index.
type IndexConfig ¶
type IndexConfig struct { TableName string IndexName string Path document.Path // If set to true, values will be associated with at most one key. False by default. Unique bool // If set, the index is typed and only accepts that type Type document.ValueType }
IndexConfig holds the configuration of an index.
func (*IndexConfig) ScanDocument ¶
func (i *IndexConfig) ScanDocument(d document.Document) error
ScanDocument implements the document.Scanner interface.
func (*IndexConfig) ToDocument ¶
func (i *IndexConfig) ToDocument() document.Document
ToDocument creates a document from an IndexConfig.
type Table ¶
A Table represents a collection of documents.
func (*Table) AscendGreaterOrEqual ¶ added in v0.10.0
func (t *Table) AscendGreaterOrEqual(pivot document.Value, fn func(d document.Document) error) error
AscendGreaterOrEqual iterates over the documents of the table whose key is greater than or equal to the pivot. The pivot is converted to the type of the primary key, if any, prior to iteration. If the pivot is empty, it iterates from the beginning of the table.
func (*Table) DescendLessOrEqual ¶ added in v0.10.0
DescendLessOrEqual iterates over the documents of the table whose key is less than or equal to the pivot, in reverse order. The pivot is converted to the type of the primary key, if any, prior to iteration. If the pivot is empty, it iterates from the end of the table in reverse order.
func (*Table) EncodeValueToKey ¶ added in v0.10.0
EncodeValueToKey encodes a value following primary key constraints. It returns a binary representation of the key as used in the store. It can be used to manually add a new entry to the store or to compare with other keys during table iteration.
func (*Table) GetDocument ¶
GetDocument returns one document by key.
func (*Table) Insert ¶
Insert the document into the table. If a primary key has been specified during the table creation, the field is expected to be present in the given document. If no primary key has been selected, a monotonic autoincremented integer key will be generated. It returns the inserted document alongside its key. They key can be accessed using the document.Keyer interface.
func (*Table) Iterate ¶
Iterate goes through all the documents of the table and calls the given function by passing each one of them. If the given function returns an error, the iteration stops.
func (*Table) Replace ¶
Replace a document by key. An error is returned if the key doesn't exist. Indexes are automatically updated.
func (*Table) Tx ¶ added in v0.8.0
func (t *Table) Tx() *Transaction
Tx returns the current transaction.
type TableInfo ¶ added in v0.7.0
type TableInfo struct { FieldConstraints FieldConstraints // contains filtered or unexported fields }
TableInfo contains information about a table.
func (*TableInfo) GetPrimaryKey ¶ added in v0.7.0
func (ti *TableInfo) GetPrimaryKey() *FieldConstraint
GetPrimaryKey returns the field constraint of the primary key. Returns nil if there is no primary key.
func (*TableInfo) ScanDocument ¶ added in v0.7.0
ScanDocument decodes d into ti.
func (*TableInfo) ToDocument ¶ added in v0.7.0
ToDocument turns ti into a document.
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction represents a database transaction. It provides methods for managing the collection of tables and the transaction itself. Transaction is either read-only or read/write. Read-only can be used to read tables and read/write can be used to read, create, delete and modify tables.
func (*Transaction) AddField ¶ added in v0.9.0
func (tx *Transaction) AddField(tableName string, fc FieldConstraint) error
AddField adds a field constraint to a table.
func (*Transaction) Commit ¶
func (tx *Transaction) Commit() error
Commit the transaction. Calling this method on read-only transactions will return an error.
func (*Transaction) CreateIndex ¶
func (tx *Transaction) CreateIndex(opts IndexConfig) error
CreateIndex creates an index with the given name. If it already exists, returns ErrIndexAlreadyExists.
func (*Transaction) CreateTable ¶
func (tx *Transaction) CreateTable(name string, info *TableInfo) error
CreateTable creates a table with the given name. If it already exists, returns ErrTableAlreadyExists.
func (*Transaction) DB ¶ added in v0.8.0
func (tx *Transaction) DB() *Database
DB returns the underlying database that created the transaction.
func (*Transaction) DropIndex ¶
func (tx *Transaction) DropIndex(name string) error
DropIndex deletes an index from the database.
func (*Transaction) DropTable ¶
func (tx *Transaction) DropTable(name string) error
DropTable deletes a table from the database.
func (*Transaction) GetIndex ¶
func (tx *Transaction) GetIndex(name string) (*Index, error)
GetIndex returns an index by name.
func (*Transaction) GetTable ¶
func (tx *Transaction) GetTable(name string) (*Table, error)
GetTable returns a table by name. The table instance is only valid for the lifetime of the transaction.
func (*Transaction) ListIndexes ¶
func (tx *Transaction) ListIndexes() ([]*IndexConfig, error)
ListIndexes lists all indexes.
func (*Transaction) ReIndex ¶
func (tx *Transaction) ReIndex(indexName string) error
ReIndex truncates and recreates selected index from scratch.
func (*Transaction) ReIndexAll ¶
func (tx *Transaction) ReIndexAll() error
ReIndexAll truncates and recreates all indexes of the database from scratch.
func (*Transaction) RenameTable ¶ added in v0.7.0
func (tx *Transaction) RenameTable(oldName, newName string) error
RenameTable renames a table. If it doesn't exist, it returns ErrTableNotFound.
func (*Transaction) Rollback ¶
func (tx *Transaction) Rollback() error
Rollback the transaction. Can be used safely after commit.
func (*Transaction) Writable ¶
func (tx *Transaction) Writable() bool
Writable indicates if the transaction is writable or not.
type TxOptions ¶ added in v0.8.0
type TxOptions struct { // Open a read-only transaction. ReadOnly bool // Set the transaction as global at the database level. // Any queries run by the database will use that transaction until it is // rolled back or commited. Attached bool }
TxOptions are passed to Begin to configure transactions.