database

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package database provides database primitives such as tables, transactions and indexes.

Index

Constants

This section is empty.

Variables

View Source
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 {
	// contains filtered or unexported fields
}

A Database manages a list of tables in an engine.

func New

func New(ng engine.Engine) (*Database, error)

New initializes the DB using the given engine.

func (*Database) Begin

func (db *Database) Begin(writable bool) (*Transaction, error)

Begin starts a new transaction. The returned transaction must be closed either by calling Rollback or Commit.

func (*Database) Close

func (db *Database) Close() error

Close the underlying engine.

type FieldConstraint

type FieldConstraint struct {
	Path         document.ValuePath
	Type         document.ValueType
	IsPrimaryKey bool
	IsNotNull    bool
}

FieldConstraint describes constraints on a particular field.

type Index

type Index struct {
	index.Index

	IndexName string
	TableName string
	Path      document.ValuePath
	Unique    bool
}

Index of a table field. Contains information about the index configuration and provides methods to manipulate the index.

type IndexConfig

type IndexConfig struct {
	// If set to true, values will be associated with at most one key. False by default.
	Unique bool

	IndexName string
	TableName string
	Path      document.ValuePath
}

IndexConfig holds the configuration of an index.

type Table

type Table struct {
	Store engine.Store
	// contains filtered or unexported fields
}

A Table represents a collection of documents.

func (*Table) Config

func (t *Table) Config() (*TableConfig, error)

Config of the table.

func (*Table) Delete

func (t *Table) Delete(key []byte) error

Delete a document by key. Indexes are automatically updated.

func (*Table) GetDocument

func (t *Table) GetDocument(key []byte) (document.Document, error)

GetDocument returns one document by key.

func (*Table) Indexes

func (t *Table) Indexes() (map[string]Index, error)

Indexes returns a map of all the indexes of a table.

func (*Table) Insert

func (t *Table) Insert(d document.Document) ([]byte, error)

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.

func (*Table) Iterate

func (t *Table) Iterate(fn func(d document.Document) error) error

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

func (t *Table) Replace(key []byte, d document.Document) error

Replace a document by key. An error is returned if the key doesn't exist. Indexes are automatically updated.

func (*Table) TableName

func (t *Table) TableName() string

TableName returns the name of the table.

func (*Table) Truncate

func (t *Table) Truncate() error

Truncate deletes all the documents from the table.

type TableConfig

type TableConfig struct {
	FieldConstraints []FieldConstraint

	LastKey int64
}

TableConfig holds the configuration of a table

func (TableConfig) GetPrimaryKey added in v0.5.0

func (t TableConfig) GetPrimaryKey() *FieldConstraint

GetPrimaryKey returns the field constraint of the primary key. Returns nil if there is no primary key.

type Transaction

type Transaction struct {
	Tx engine.Transaction
	// 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) Commit

func (tx *Transaction) Commit() error

Commit the transaction.

func (Transaction) CreateIndex

func (tx Transaction) CreateIndex(opts IndexConfig) error

CreateIndex creates an index with the given name. If it already exists, returns ErrTableAlreadyExists.

func (Transaction) CreateTable

func (tx Transaction) CreateTable(name string, cfg *TableConfig) error

CreateTable creates a table with the given name. If it already exists, returns ErrTableAlreadyExists.

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

func (tx Transaction) ListTables() ([]string, error)

ListTables lists all the tables.

func (*Transaction) Promote

func (tx *Transaction) Promote() error

Promote rollsback a read-only transaction and begins a read-write transaction transparently. It returns an error if the current transaction is already writable.

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

Jump to

Keyboard shortcuts

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