Documentation ¶
Overview ¶
Package database provides database primitives such as tables, transactions and indexes.
Index ¶
- Constants
- Variables
- func CastConversion(v types.Value, path object.Path, targetType types.ValueType) (types.Value, error)
- func IsConstraintViolationError(err error) bool
- type AnonymousType
- type BasicRow
- func (r *BasicRow) Get(name string) (types.Value, error)
- func (r *BasicRow) Iterate(fn func(name string, value types.Value) error) error
- func (r *BasicRow) Key() *tree.Key
- func (r *BasicRow) MarshalJSON() ([]byte, error)
- func (r *BasicRow) Object() types.Object
- func (r *BasicRow) ResetWith(tableName string, key *tree.Key, obj types.Object)
- func (r *BasicRow) TableName() string
- type Catalog
- func (c *Catalog) Clone() *Catalog
- func (c *Catalog) GetFreeTransientNamespace() tree.Namespace
- func (c *Catalog) GetIndex(tx *Transaction, indexName string) (*Index, error)
- func (c *Catalog) GetIndexInfo(indexName string) (*IndexInfo, error)
- func (c *Catalog) GetSequence(name string) (*Sequence, error)
- func (c *Catalog) GetTable(tx *Transaction, tableName string) (*Table, error)
- func (c *Catalog) GetTableInfo(tableName string) (*TableInfo, error)
- func (c *Catalog) ListIndexes(tableName string) []string
- func (c *Catalog) ListSequences() []string
- type CatalogLoader
- type CatalogStore
- func (s *CatalogStore) Delete(tx *Transaction, name string) error
- func (s *CatalogStore) Info() *TableInfo
- func (s *CatalogStore) Insert(tx *Transaction, r Relation) error
- func (s *CatalogStore) Replace(tx *Transaction, name string, r Relation) error
- func (s *CatalogStore) Table(tx *Transaction) *Table
- type CatalogWriter
- func (c *CatalogWriter) AddFieldConstraint(tx *Transaction, tableName string, fc *FieldConstraint, tcs TableConstraints) error
- func (c *CatalogWriter) CreateIndex(tx *Transaction, info *IndexInfo) (*IndexInfo, error)
- func (c *CatalogWriter) CreateSequence(tx *Transaction, info *SequenceInfo) error
- func (c *CatalogWriter) CreateTable(tx *Transaction, tableName string, info *TableInfo) error
- func (c *CatalogWriter) DropIndex(tx *Transaction, name string) error
- func (c *CatalogWriter) DropSequence(tx *Transaction, name string) error
- func (c *CatalogWriter) DropTable(tx *Transaction, tableName string) error
- func (c *CatalogWriter) Init(tx *Transaction) error
- func (c *CatalogWriter) RenameTable(tx *Transaction, oldName, newName string) error
- type ConstraintViolationError
- type ConversionFunc
- type Database
- type EncodedObject
- type FieldConstraint
- type FieldConstraints
- type Index
- func (idx *Index) Delete(vs []types.Value, key []byte) error
- func (idx *Index) Exists(vs []types.Value) (bool, *tree.Key, error)
- func (idx *Index) IterateOnRange(rng *tree.Range, reverse bool, fn func(key *tree.Key) error) error
- func (idx *Index) Set(vs []types.Value, key []byte) error
- func (idx *Index) Truncate() error
- type IndexInfo
- type IndexInfoRelation
- type LazyRow
- func (r *LazyRow) Get(name string) (types.Value, error)
- func (r *LazyRow) GetByField(field string) (types.Value, error)
- func (r *LazyRow) Iterate(fn func(name string, value types.Value) error) error
- func (r *LazyRow) Key() *tree.Key
- func (r *LazyRow) MarshalJSON() ([]byte, error)
- func (r *LazyRow) Object() types.Object
- func (r *LazyRow) ResetWith(table *Table, key *tree.Key)
- func (r *LazyRow) TableName() string
- type OnConflictAction
- type Options
- type Owner
- type Pivot
- type PrimaryKey
- type Range
- type Relation
- type Row
- type RowIterator
- type Sequence
- func (s *Sequence) Clone() Relation
- func (s *Sequence) Drop(tx *Transaction, catalog *Catalog) error
- func (s *Sequence) GenerateBaseName() string
- func (s *Sequence) GetOrCreateTable(tx *Transaction) (*Table, error)
- func (s *Sequence) Init(tx *Transaction) error
- func (s *Sequence) Name() string
- func (s *Sequence) Next(tx *Transaction) (int64, error)
- func (s *Sequence) Release(tx *Transaction) error
- func (s *Sequence) SetLease(tx *Transaction, name string, v int64) error
- func (s *Sequence) SetName(name string)
- func (s *Sequence) Type() string
- type SequenceInfo
- type Table
- func (t *Table) Delete(key *tree.Key) error
- func (t *Table) GetRow(key *tree.Key) (Row, error)
- func (t *Table) Insert(o types.Object) (*tree.Key, Row, error)
- func (t *Table) IterateOnRange(rng *Range, reverse bool, fn func(key *tree.Key, r Row) error) error
- func (t *Table) Put(key *tree.Key, o types.Object) (Row, error)
- func (t *Table) Replace(key *tree.Key, o types.Object) (Row, error)
- func (t *Table) Truncate() error
- type TableConstraint
- type TableConstraints
- type TableExpression
- type TableInfo
- func (ti *TableInfo) AddFieldConstraint(newFc *FieldConstraint) error
- func (ti *TableInfo) AddTableConstraint(newTc *TableConstraint) error
- func (ti *TableInfo) BuildPrimaryKey()
- func (ti *TableInfo) Clone() *TableInfo
- func (ti *TableInfo) EncodeKey(key *tree.Key) ([]byte, error)
- func (t *TableInfo) EncodeObject(tx *Transaction, dst []byte, o types.Object) ([]byte, error)
- func (ti *TableInfo) GetFieldConstraintForPath(p object.Path) *FieldConstraint
- func (ti *TableInfo) PrimaryKeySortOrder() tree.SortOrder
- func (ti *TableInfo) String() string
- type TableInfoRelation
- type Transaction
- type TxOptions
Constants ¶
const ( CatalogTableName = InternalPrefix + "catalog" SequenceTableName = InternalPrefix + "sequence" )
System tables
const ( RelationTableType = "table" RelationIndexType = "index" RelationSequenceType = "sequence" )
Relation types
const ( CatalogTableNamespace tree.Namespace = 1 SequenceTableNamespace tree.Namespace = 2 RollbackSegmentNamespace tree.Namespace = 3 MinTransientNamespace tree.Namespace = math.MaxInt64 - 1<<24 MaxTransientNamespace tree.Namespace = math.MaxInt64 )
System namespaces
const ( // OnConflictDoNothing ignores the duplicate error and returns nothing. OnConflictDoNothing = iota + 1 // OnConflictDoReplace replaces the conflicting row with a new one. OnConflictDoReplace )
const (
InternalPrefix = "__chai_"
)
const (
StoreSequence = InternalPrefix + "store_seq"
)
System sequences
Variables ¶
var ( // ErrIndexDuplicateValue is returned when a value is already associated with a key ErrIndexDuplicateValue = errors.New("duplicate value") )
Functions ¶
func CastConversion ¶
func CastConversion(v types.Value, path object.Path, targetType types.ValueType) (types.Value, error)
CastConversion is a ConversionFunc that casts the value to the target type.
Types ¶
type AnonymousType ¶
type AnonymousType struct {
FieldConstraints FieldConstraints
}
func (*AnonymousType) AddFieldConstraint ¶
func (an *AnonymousType) AddFieldConstraint(newFc *FieldConstraint) error
func (*AnonymousType) String ¶
func (an *AnonymousType) String() string
type BasicRow ¶
type BasicRow struct {
// contains filtered or unexported fields
}
func NewBasicRow ¶
func (*BasicRow) MarshalJSON ¶
type Catalog ¶
type Catalog struct { Cache *catalogCache CatalogTable *CatalogStore TransientNamespaces *atomic.Counter }
Catalog manages all database objects such as tables, indexes and sequences. It stores all these objects in memory for fast access. Any modification is persisted into the __chai_catalog table.
func NewCatalog ¶
func NewCatalog() *Catalog
func (*Catalog) GetFreeTransientNamespace ¶
GetFreeTransientNamespace returns the next available transient namespace. Transient namespaces start from math.MaxInt64 - (2 << 24) to math.MaxInt64 (around 16 M). The transient namespaces counter is not persisted and resets when the database is restarted. Once the counter reaches its maximum value, it will wrap around to the minimum value. Technically, if a transient namespace is still in use by the time the counter wraps around its data may be overwritten. However, transient trees are supposed to verify that the namespace is not in use before writing to it.
func (*Catalog) GetIndex ¶
func (c *Catalog) GetIndex(tx *Transaction, indexName string) (*Index, error)
GetIndex returns an index by name.
func (*Catalog) GetIndexInfo ¶
GetIndexInfo returns an index info by name.
func (*Catalog) GetTable ¶
func (c *Catalog) GetTable(tx *Transaction, tableName string) (*Table, error)
func (*Catalog) GetTableInfo ¶
GetTableInfo returns the table info for the given table name.
func (*Catalog) ListIndexes ¶
ListIndexes returns all indexes for a given table name. If tableName is empty if returns a list of all indexes. The returned list of indexes is sorted lexicographically.
func (*Catalog) ListSequences ¶
ListSequences returns all sequence names sorted lexicographically.
type CatalogLoader ¶
CatalogLoader loads the catalog from the disk. It may parse a SQL representation of the catalog and return a Catalog that represents all entities stored on disk.
type CatalogStore ¶
type CatalogStore struct {
// contains filtered or unexported fields
}
func (*CatalogStore) Delete ¶
func (s *CatalogStore) Delete(tx *Transaction, name string) error
func (*CatalogStore) Info ¶
func (s *CatalogStore) Info() *TableInfo
func (*CatalogStore) Insert ¶
func (s *CatalogStore) Insert(tx *Transaction, r Relation) error
Insert a catalog object to the table.
func (*CatalogStore) Replace ¶
func (s *CatalogStore) Replace(tx *Transaction, name string, r Relation) error
Replace a catalog object with another.
func (*CatalogStore) Table ¶
func (s *CatalogStore) Table(tx *Transaction) *Table
type CatalogWriter ¶
type CatalogWriter struct {
*Catalog
}
A CatalogWriter is used to apply modifications to the catalog in a thread-safe manner. All the updates are only visible to the current transaction and don't require any lock. Upon commit, the transaction will apply the changes to the catalog.
func NewCatalogWriter ¶
func NewCatalogWriter(c *Catalog) *CatalogWriter
func (*CatalogWriter) AddFieldConstraint ¶
func (c *CatalogWriter) AddFieldConstraint(tx *Transaction, tableName string, fc *FieldConstraint, tcs TableConstraints) error
AddFieldConstraint adds a field constraint to a table.
func (*CatalogWriter) CreateIndex ¶
func (c *CatalogWriter) CreateIndex(tx *Transaction, info *IndexInfo) (*IndexInfo, error)
CreateIndex creates an index with the given name. If it already exists, returns errs.ErrIndexAlreadyExists.
func (*CatalogWriter) CreateSequence ¶
func (c *CatalogWriter) CreateSequence(tx *Transaction, info *SequenceInfo) error
CreateSequence creates a sequence with the given name.
func (*CatalogWriter) CreateTable ¶
func (c *CatalogWriter) CreateTable(tx *Transaction, tableName string, info *TableInfo) error
CreateTable creates a table with the given name. If it already exists, returns ErrTableAlreadyExists.
func (*CatalogWriter) DropIndex ¶
func (c *CatalogWriter) DropIndex(tx *Transaction, name string) error
DropIndex deletes an index from the
func (*CatalogWriter) DropSequence ¶
func (c *CatalogWriter) DropSequence(tx *Transaction, name string) error
DropSequence deletes a sequence from the catalog.
func (*CatalogWriter) DropTable ¶
func (c *CatalogWriter) DropTable(tx *Transaction, tableName string) error
DropTable deletes a table from the catalog
func (*CatalogWriter) Init ¶
func (c *CatalogWriter) Init(tx *Transaction) error
func (*CatalogWriter) RenameTable ¶
func (c *CatalogWriter) RenameTable(tx *Transaction, oldName, newName string) error
RenameTable renames a table. If it doesn't exist, it returns errs.ErrTableNotFound.
type ConstraintViolationError ¶
func (ConstraintViolationError) Error ¶
func (c ConstraintViolationError) Error() string
type ConversionFunc ¶
type ConversionFunc func(v types.Value, path object.Path, targetType types.ValueType) (types.Value, error)
ConversionFunc is called when the type of a value is different than the expected type and the value needs to be converted.
type Database ¶
type Database struct { // TransactionIDs is used to assign transaction an ID at runtime. // Since transaction IDs are not persisted and not used for concurrent // access, we can use 8 bytes ids that will be reset every time // the database restarts. TransactionIDs uint64 // Underlying kv store. Store *kv.Store // contains filtered or unexported fields }
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 ¶
func (db *Database) BeginTx(opts *TxOptions) (*Transaction, error)
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 ¶
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.
func (*Database) SetCatalog ¶
type EncodedObject ¶
type EncodedObject struct {
// contains filtered or unexported fields
}
func NewEncodedObject ¶
func NewEncodedObject(fcs *FieldConstraints, data []byte) *EncodedObject
func (*EncodedObject) GetByField ¶
func (e *EncodedObject) GetByField(field string) (v types.Value, err error)
GetByField decodes the selected field from the buffer.
func (*EncodedObject) Iterate ¶
Iterate decodes each columns one by one and passes them to fn until the end of the row or until fn returns an error.
func (*EncodedObject) MarshalJSON ¶
func (e *EncodedObject) MarshalJSON() ([]byte, error)
func (*EncodedObject) ResetWith ¶
func (e *EncodedObject) ResetWith(fcs *FieldConstraints, data []byte)
type FieldConstraint ¶
type FieldConstraint struct { Position int Field string Type types.ValueType IsNotNull bool DefaultValue TableExpression AnonymousType *AnonymousType }
FieldConstraint describes constraints on a particular field.
func (*FieldConstraint) IsEmpty ¶
func (f *FieldConstraint) IsEmpty() bool
func (*FieldConstraint) String ¶
func (f *FieldConstraint) String() string
type FieldConstraints ¶
type FieldConstraints struct { Ordered []*FieldConstraint ByField map[string]*FieldConstraint AllowExtraFields bool }
FieldConstraints is a list of field constraints.
func MustNewFieldConstraints ¶
func MustNewFieldConstraints(constraints ...*FieldConstraint) FieldConstraints
func NewFieldConstraints ¶
func NewFieldConstraints(constraints ...*FieldConstraint) (FieldConstraints, error)
func (*FieldConstraints) Add ¶
func (f *FieldConstraints) Add(newFc *FieldConstraint) error
Add a field constraint to the list. If another constraint exists for the same path and they are equal, an error is returned.
func (FieldConstraints) ConvertValueAtPath ¶
func (f FieldConstraints) ConvertValueAtPath(path object.Path, v types.Value, conversionFn ConversionFunc) (types.Value, error)
ConvertValueAtPath converts the value using the field constraints that are applicable at the given path.
func (FieldConstraints) GetFieldConstraintForPath ¶
func (f FieldConstraints) GetFieldConstraintForPath(path object.Path) *FieldConstraint
type Index ¶
type Index struct { // How many values the index is operating on. // For example, an index created with `CREATE INDEX idx_a_b ON foo (a, b)` has an arity of 2. Arity int Tree *tree.Tree }
An Index associates encoded values with keys.
The association is performed by encoding the values in a binary format that preserve ordering when compared lexicographically. For the implementation, see the binarysort package and the types.ValueEncoder.
func (*Index) IterateOnRange ¶
func (*Index) Set ¶
Set associates values with a key. If Unique is set to false, it is possible to associate multiple keys for the same value but a key can be associated to only one value.
Values are stored in the index following the "index format". Every record is stored like this:
k: <encoded values><primary key> v: length of the encoded value, as an unsigned varint
type IndexInfo ¶
type IndexInfo struct { // namespace of the store associated with the index. StoreNamespace tree.Namespace IndexName string Paths []object.Path // Sort order of each indexed field. KeySortOrder tree.SortOrder // If set to true, values will be associated with at most one key. False by default. Unique bool // If set, this index has been created from a table constraint // i.e CREATE TABLE tbl(a INT UNIQUE) // The path refers to the path this index is related to. Owner Owner }
IndexInfo holds the configuration of an index.
type IndexInfoRelation ¶
type IndexInfoRelation struct {
Info *IndexInfo
}
func (*IndexInfoRelation) Clone ¶
func (r *IndexInfoRelation) Clone() Relation
func (*IndexInfoRelation) GenerateBaseName ¶
func (r *IndexInfoRelation) GenerateBaseName() string
func (*IndexInfoRelation) Name ¶
func (r *IndexInfoRelation) Name() string
func (*IndexInfoRelation) SetName ¶
func (r *IndexInfoRelation) SetName(name string)
func (*IndexInfoRelation) Type ¶
func (r *IndexInfoRelation) Type() string
type LazyRow ¶
type LazyRow struct {
// contains filtered or unexported fields
}
LazyRow holds an LazyRow key and lazily loads the LazyRow on demand when the Iterate or GetByField method is called. It implements the Row and the object.Keyer interfaces.
func (*LazyRow) MarshalJSON ¶
type OnConflictAction ¶
type OnConflictAction int
OnConflictAction is a function triggered when trying to insert a row that already exists. This function is triggered if the key is duplicated or if there is a unique constraint violation on one of the columns of the row.
func (OnConflictAction) String ¶
func (o OnConflictAction) String() string
type Options ¶
type Options struct {
CatalogLoader func(tx *Transaction) error
}
Options are passed to Open to control how the database is loaded.
type Owner ¶
Owner is used to determine who owns a relation. If the relation has been created by a table (for rowids for example), only the TableName is filled. If it has been created by a field constraint (for identities for example), the path must also be filled.
type PrimaryKey ¶
type Row ¶
type Row interface { // Iterate goes through all the fields of the row and calls the given function // by passing the column name Iterate(fn func(column string, value types.Value) error) error // Get returns the value of the given column. // If the column does not exist, it returns ErrColumnNotFound. Get(name string) (types.Value, error) // TableName returns the name of the table the row belongs to. TableName() string // MarshalJSON encodes the row as JSON. MarshalJSON() ([]byte, error) // Key returns the row key. Key() *tree.Key Object() types.Object }
type RowIterator ¶
type Sequence ¶
type Sequence struct { Info *SequenceInfo CurrentValue *int64 Cached uint64 Key *tree.Key }
A Sequence manages a sequence of numbers. It is not thread safe.
func NewSequence ¶
func NewSequence(info *SequenceInfo, currentValue *int64) Sequence
NewSequence creates a new or existing sequence. If currentValue is not nil next call to Next will increase the lease.
func (*Sequence) GenerateBaseName ¶
func (*Sequence) GetOrCreateTable ¶
func (s *Sequence) GetOrCreateTable(tx *Transaction) (*Table, error)
func (*Sequence) Init ¶
func (s *Sequence) Init(tx *Transaction) error
func (*Sequence) Release ¶
func (s *Sequence) Release(tx *Transaction) error
Release the sequence by storing the actual current value to the sequence table. If the sequence has cache, the cached value is overwritten.
type SequenceInfo ¶
type SequenceInfo struct { Name string IncrementBy int64 Min, Max int64 Start int64 Cache uint64 Cycle bool Owner Owner }
SequenceInfo holds the configuration of a sequence.
func (SequenceInfo) Clone ¶
func (s SequenceInfo) Clone() *SequenceInfo
Clone returns a copy of the sequence information.
func (*SequenceInfo) String ¶
func (s *SequenceInfo) String() string
String returns a SQL representation.
type Table ¶
type Table struct { Tx *Transaction Tree *tree.Tree // Table information. // May not represent the most up to date data. // Always get a fresh Table instance before relying on this field. Info *TableInfo }
A Table represents a collection of objects.
func (*Table) Insert ¶
Insert the object into the table. If a primary key has been specified during the table creation, the field is expected to be present in the given object. If no primary key has been selected, a monotonic autoincremented integer key will be generated. It returns the inserted object alongside its key.
func (*Table) IterateOnRange ¶
type TableConstraint ¶
type TableConstraint struct { Name string Paths object.Paths Check TableExpression Unique bool PrimaryKey bool SortOrder tree.SortOrder }
A TableConstraint represent a constraint specific to a table and not necessarily to a single field path.
func (*TableConstraint) String ¶
func (t *TableConstraint) String() string
type TableConstraints ¶
type TableConstraints []*TableConstraint
TableConstraints holds the list of CHECK constraints.
func (*TableConstraints) ValidateRow ¶
func (t *TableConstraints) ValidateRow(tx *Transaction, r Row) error
ValidateRow checks all the table constraint for the given row.
type TableExpression ¶
type TableInfo ¶
type TableInfo struct { // name of the table. TableName string // namespace of the store associated with the table. StoreNamespace tree.Namespace ReadOnly bool // Name of the rowid sequence if any. RowidSequenceName string FieldConstraints FieldConstraints TableConstraints TableConstraints PrimaryKey *PrimaryKey }
TableInfo contains information about a table.
func (*TableInfo) AddFieldConstraint ¶
func (ti *TableInfo) AddFieldConstraint(newFc *FieldConstraint) error
func (*TableInfo) AddTableConstraint ¶
func (ti *TableInfo) AddTableConstraint(newTc *TableConstraint) error
func (*TableInfo) BuildPrimaryKey ¶
func (ti *TableInfo) BuildPrimaryKey()
func (*TableInfo) EncodeObject ¶
EncodeObject validates a row against all the constraints of the table and encodes it.
func (*TableInfo) GetFieldConstraintForPath ¶
func (ti *TableInfo) GetFieldConstraintForPath(p object.Path) *FieldConstraint
func (*TableInfo) PrimaryKeySortOrder ¶
type TableInfoRelation ¶
type TableInfoRelation struct {
Info *TableInfo
}
func (*TableInfoRelation) Clone ¶
func (r *TableInfoRelation) Clone() Relation
func (*TableInfoRelation) GenerateBaseName ¶
func (r *TableInfoRelation) GenerateBaseName() string
func (*TableInfoRelation) Name ¶
func (r *TableInfoRelation) Name() string
func (*TableInfoRelation) SetName ¶
func (r *TableInfoRelation) SetName(name string)
func (*TableInfoRelation) Type ¶
func (r *TableInfoRelation) Type() string
type Transaction ¶
type Transaction struct { // Timestamp at which the transaction was created. // The timestamp must use the local timezone. TxStart time.Time Session kv.Session Store *kv.Store ID uint64 Writable bool WriteTxMu *sync.Mutex // these functions are run after a successful rollback. OnRollbackHooks []func() // these functions are run after a successful commit. OnCommitHooks []func() Catalog *Catalog // 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) CatalogWriter ¶
func (tx *Transaction) CatalogWriter() *CatalogWriter
func (*Transaction) Commit ¶
func (tx *Transaction) Commit() error
Commit the transaction. Calling this method on read-only transactions will return an error.
func (*Transaction) Rollback ¶
func (tx *Transaction) Rollback() error
Rollback the transaction. Can be used safely after commit.
type TxOptions ¶
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.