Documentation
¶
Overview ¶
Package database provides database primitives such as tables, transactions and indexes.
Index ¶
- Constants
- Variables
- func IsConstraintViolationError(err error) bool
- type BasicRow
- 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) AddColumnConstraint(tx *Transaction, tableName string, cc *ColumnConstraint, 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 ColumnConstraint
- type ColumnConstraints
- type Connection
- type ConstraintViolationError
- type Database
- type EncodedRow
- type Index
- type IndexInfo
- type IndexInfoRelation
- type IndexIterator
- type Iterator
- type LazyRow
- type OnConflictAction
- type Options
- type Owner
- type Pivot
- type PrimaryKey
- type Range
- type Relation
- type Result
- type Row
- 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) Exists(key *tree.Key) (bool, error)
- func (t *Table) GenerateKey(r row.Row) (*tree.Key, bool, error)
- func (t *Table) GetRow(key *tree.Key) (Row, error)
- func (t *Table) Insert(r row.Row) (*tree.Key, Row, error)
- func (t *Table) Iterator(rng *Range) (*TableIterator, error)
- func (t *Table) Put(key *tree.Key, r row.Row) (Row, error)
- func (t *Table) Replace(key *tree.Key, r row.Row) (Row, error)
- func (t *Table) Truncate() error
- type TableConstraint
- type TableConstraints
- type TableExpression
- type TableInfo
- func (ti *TableInfo) AddColumnConstraint(newCc *ColumnConstraint) 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) EncodeRow(tx *Transaction, dst []byte, r row.Row) ([]byte, error)
- func (ti *TableInfo) GetColumnConstraint(column string) *ColumnConstraint
- func (ti *TableInfo) PrimaryKeySortOrder() tree.SortOrder
- func (ti *TableInfo) String() string
- func (ti *TableInfo) Validate() error
- type TableInfoRelation
- type TableIterator
- 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 ¶
Types ¶
type BasicRow ¶
func NewBasicRow ¶
func (*BasicRow) OriginalRow ¶ added in v0.17.0
func (*BasicRow) SetOriginalRow ¶ added in v0.17.0
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) AddColumnConstraint ¶ added in v0.17.0
func (c *CatalogWriter) AddColumnConstraint(tx *Transaction, tableName string, cc *ColumnConstraint, tcs TableConstraints) error
AddColumnConstraint 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 ColumnConstraint ¶ added in v0.17.0
type ColumnConstraint struct {
Position int
Column string
Type types.Type
IsNotNull bool
DefaultValue TableExpression
}
ColumnConstraint describes constraints on a particular column.
func (*ColumnConstraint) IsEmpty ¶ added in v0.17.0
func (f *ColumnConstraint) IsEmpty() bool
func (*ColumnConstraint) String ¶ added in v0.17.0
func (f *ColumnConstraint) String() string
type ColumnConstraints ¶ added in v0.17.0
type ColumnConstraints struct {
Ordered []*ColumnConstraint
ByColumn map[string]*ColumnConstraint
}
ColumnConstraints is a list of column constraints.
func MustNewColumnConstraints ¶ added in v0.17.0
func MustNewColumnConstraints(constraints ...*ColumnConstraint) ColumnConstraints
func NewColumnConstraints ¶ added in v0.17.0
func NewColumnConstraints(constraints ...*ColumnConstraint) (ColumnConstraints, error)
func (*ColumnConstraints) Add ¶ added in v0.17.0
func (f *ColumnConstraints) Add(newCc *ColumnConstraint) error
Add a column constraint to the list. If another constraint exists for the same path and they are equal, an error is returned.
func (ColumnConstraints) GetColumnConstraint ¶ added in v0.17.0
func (f ColumnConstraints) GetColumnConstraint(column string) *ColumnConstraint
type Connection ¶ added in v0.17.0
type Connection struct {
// contains filtered or unexported fields
}
func (*Connection) BeginTx ¶ added in v0.17.0
func (c *Connection) 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.
func (*Connection) Close ¶ added in v0.17.0
func (c *Connection) Close() error
func (*Connection) GetTx ¶ added in v0.17.0
func (c *Connection) GetTx() *Transaction
GetAttachedTx returns the transaction attached to the connection, if any. The returned transaction is not thread safe.
func (*Connection) Reset ¶ added in v0.17.0
func (c *Connection) Reset() error
type ConstraintViolationError ¶
func (ConstraintViolationError) Error ¶
func (c ConstraintViolationError) Error() string
type Database ¶
type Database struct {
// Underlying kv store.
Engine engine.Engine
// 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) Connect ¶ added in v0.17.0
func (db *Database) Connect() (*Connection, error)
Connect returns a new connection to the database. The returned connection is not thread safe. It is the caller's responsibility to close the connection.
func (*Database) SetCatalog ¶
type EncodedRow ¶ added in v0.17.0
type EncodedRow struct {
// contains filtered or unexported fields
}
func NewEncodedRow ¶ added in v0.17.0
func NewEncodedRow(ccs *ColumnConstraints, data []byte) *EncodedRow
func RowIsEncoded ¶ added in v0.17.0
func RowIsEncoded(r row.Row, ccs *ColumnConstraints) (*EncodedRow, bool)
func (*EncodedRow) Get ¶ added in v0.17.0
func (e *EncodedRow) Get(column string) (v types.Value, err error)
Get decodes the selected column from the buffer.
func (*EncodedRow) Iterate ¶ added in v0.17.0
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 (*EncodedRow) ResetWith ¶ added in v0.17.0
func (e *EncodedRow) ResetWith(ccs *ColumnConstraints, data []byte)
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) Iterator ¶ added in v0.17.0
func (idx *Index) Iterator(rng *tree.Range) (*IndexIterator, error)
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
Columns []string
// 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 IndexIterator ¶ added in v0.17.0
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 Get method is called. It implements the Row and the row.Keyer interfaces.
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 Range ¶
func (*Range) ToTreeRange ¶
type Row ¶
type Row interface {
// Iterate goes through all the columns 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
// Key returns the row key.
Key() *tree.Key
}
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) GenerateKey ¶ added in v0.17.0
GenerateKey generates a key for o based on the table configuration. if the table has a primary key, it extracts the field from the object, converts it to the targeted type and returns its encoded version. if there are no primary key in the table, a default key is generated, called the rowid. It returns a boolean indicating whether the key is a rowid or not.
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 row. 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) Iterator ¶ added in v0.17.0
func (t *Table) Iterator(rng *Range) (*TableIterator, error)
type TableConstraint ¶
type TableConstraint struct {
Name string
Columns []string
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.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
ColumnConstraints ColumnConstraints
TableConstraints TableConstraints
PrimaryKey *PrimaryKey
}
TableInfo contains information about a table.
func (*TableInfo) AddColumnConstraint ¶ added in v0.17.0
func (ti *TableInfo) AddColumnConstraint(newCc *ColumnConstraint) error
func (*TableInfo) AddTableConstraint ¶
func (ti *TableInfo) AddTableConstraint(newTc *TableConstraint) error
func (*TableInfo) BuildPrimaryKey ¶
func (ti *TableInfo) BuildPrimaryKey()
func (*TableInfo) EncodeRow ¶ added in v0.17.0
EncodeRow validates a row against all the constraints of the table and encodes it.
func (*TableInfo) GetColumnConstraint ¶ added in v0.17.0
func (ti *TableInfo) GetColumnConstraint(column string) *ColumnConstraint
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 TableIterator ¶ added in v0.17.0
func (*TableIterator) Value ¶ added in v0.17.0
func (it *TableIterator) Value() (Row, error)
type Transaction ¶
type Transaction struct {
// Timestamp at which the transaction was created.
// The timestamp must use the local timezone.
TxStart time.Time
Session engine.Session
Engine engine.Engine
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) Connection ¶ added in v0.17.0
func (tx *Transaction) Connection() *Connection
func (*Transaction) Rollback ¶
func (tx *Transaction) Rollback() error
Rollback the transaction. Can be used safely after commit.