database

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: Apache-2.0 Imports: 23 Imported by: 1

Documentation

Overview

Copyright 2023 Codenotary Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	DefaultDbRootPath          = "./data"
	DefaultReadTxPoolSize      = 128
	DefaultTruncationFrequency = 24 * time.Hour
)
View Source
const (
	SetKeyPrefix byte = iota
	SortedSetKeyPrefix
	SQLPrefix
	DocumentPrefix
)
View Source
const (
	PlainValuePrefix = iota
	ReferenceValuePrefix
)
View Source
const MaxKeyResolutionLimit = 1
View Source
const MaxKeyScanLimit = 1000

Variables

View Source
var (
	ErrIndexKeyMismatch      = status.New(codes.InvalidArgument, "mismatch between provided index and key").Err()
	ErrNoReferenceProvided   = status.New(codes.InvalidArgument, "provided argument is not a reference").Err()
	ErrReferenceKeyMissing   = status.New(codes.InvalidArgument, "reference key not provided").Err()
	ErrZAddIndexMissing      = status.New(codes.InvalidArgument, "zAdd index not provided").Err()
	ErrReferenceIndexMissing = status.New(codes.InvalidArgument, "reference index not provided").Err()

	ErrDatabaseAlreadyExists      = errors.New("database already exists")
	ErrDatabaseNotExists          = errors.New("database does not exist")
	ErrCannotDeleteAnOpenDatabase = errors.New("cannot delete an open database")
	ErrTxReadPoolExhausted        = errors.New("read tx pool exhausted")
)
View Source
var (
	ErrTruncatorAlreadyRunning   = errors.New("truncator already running")
	ErrRetentionPeriodNotReached = errors.New("retention period has not been reached")
)
View Source
var ErrFinalKeyCannotBeConvertedIntoReference = errors.New("final key cannot be converted into a reference")
View Source
var ErrIllegalArguments = store.ErrIllegalArguments
View Source
var ErrIllegalState = store.ErrIllegalState
View Source
var ErrInvalidRevision = errors.New("invalid key revision number")
View Source
var ErrIsReplica = errors.New("database is read-only because it's a replica")
View Source
var ErrKeyResolutionLimitReached = errors.New("key resolution limit reached. It may be due to cyclic references")
View Source
var ErrNoWaitOperationMustBeSelfContained = fmt.Errorf("no wait operation must be self-contained: %w", store.ErrIllegalArguments)
View Source
var ErrNotReplica = errors.New("database is NOT a replica")
View Source
var ErrReferencedKeyCannotBeAReference = errors.New("referenced key cannot be a reference")
View Source
var ErrReplicaDivergedFromPrimary = errors.New("replica diverged from primary")
View Source
var ErrResultSizeLimitExceeded = errors.New("result size limit exceeded")
View Source
var ErrResultSizeLimitReached = errors.New("result size limit reached")

Functions

func EncodeEntrySpec added in v1.2.0

func EncodeEntrySpec(
	key []byte,
	md *store.KVMetadata,
	value []byte,
) *store.EntrySpec

func EncodeKey

func EncodeKey(key []byte) []byte

func EncodeReference

func EncodeReference(
	key []byte,
	md *store.KVMetadata,
	referencedKey []byte,
	atTx uint64,
) *store.EntrySpec

func EncodeZAdd

func EncodeZAdd(set []byte, score float64, key []byte, atTx uint64) *store.EntrySpec

func PreconditionFromProto added in v1.2.3

func PreconditionFromProto(c *schema.Precondition) (store.Precondition, error)

func TrimPrefix added in v0.9.1

func TrimPrefix(prefixed []byte) []byte

func WrapReferenceValueAt

func WrapReferenceValueAt(key []byte, atTx uint64) []byte

func WrapWithPrefix

func WrapWithPrefix(b []byte, prefix byte) []byte

WrapWithPrefix ...

func WrapZAddReferenceAt

func WrapZAddReferenceAt(set []byte, score float64, key []byte, atTx uint64) []byte

Types

type DB

type DB interface {
	GetName() string

	// Setttings
	GetOptions() *Options

	Path() string

	AsReplica(asReplica, syncReplication bool, syncAcks int)
	IsReplica() bool

	IsSyncReplicationEnabled() bool
	SetSyncReplication(enabled bool)

	MaxResultSize() int
	UseTimeFunc(timeFunc store.TimeFunc) error

	// State
	Health() (waitingCount int, lastReleaseAt time.Time)
	CurrentState() (*schema.ImmutableState, error)

	Size() (uint64, error)

	// Key-Value
	Set(ctx context.Context, req *schema.SetRequest) (*schema.TxHeader, error)
	VerifiableSet(ctx context.Context, req *schema.VerifiableSetRequest) (*schema.VerifiableTx, error)

	Get(ctx context.Context, req *schema.KeyRequest) (*schema.Entry, error)
	VerifiableGet(ctx context.Context, req *schema.VerifiableGetRequest) (*schema.VerifiableEntry, error)
	GetAll(ctx context.Context, req *schema.KeyListRequest) (*schema.Entries, error)

	Delete(ctx context.Context, req *schema.DeleteKeysRequest) (*schema.TxHeader, error)

	SetReference(ctx context.Context, req *schema.ReferenceRequest) (*schema.TxHeader, error)
	VerifiableSetReference(ctx context.Context, req *schema.VerifiableReferenceRequest) (*schema.VerifiableTx, error)

	Scan(ctx context.Context, req *schema.ScanRequest) (*schema.Entries, error)

	History(ctx context.Context, req *schema.HistoryRequest) (*schema.Entries, error)

	ExecAll(ctx context.Context, operations *schema.ExecAllRequest) (*schema.TxHeader, error)

	Count(ctx context.Context, prefix *schema.KeyPrefix) (*schema.EntryCount, error)
	CountAll(ctx context.Context) (*schema.EntryCount, error)

	ZAdd(ctx context.Context, req *schema.ZAddRequest) (*schema.TxHeader, error)
	VerifiableZAdd(ctx context.Context, req *schema.VerifiableZAddRequest) (*schema.VerifiableTx, error)
	ZScan(ctx context.Context, req *schema.ZScanRequest) (*schema.ZEntries, error)

	// SQL-related
	NewSQLTx(ctx context.Context, opts *sql.TxOptions) (*sql.SQLTx, error)

	SQLExec(ctx context.Context, tx *sql.SQLTx, req *schema.SQLExecRequest) (ntx *sql.SQLTx, ctxs []*sql.SQLTx, err error)
	SQLExecPrepared(ctx context.Context, tx *sql.SQLTx, stmts []sql.SQLStmt, params map[string]interface{}) (ntx *sql.SQLTx, ctxs []*sql.SQLTx, err error)

	InferParameters(ctx context.Context, tx *sql.SQLTx, sql string) (map[string]sql.SQLValueType, error)
	InferParametersPrepared(ctx context.Context, tx *sql.SQLTx, stmt sql.SQLStmt) (map[string]sql.SQLValueType, error)

	SQLQuery(ctx context.Context, tx *sql.SQLTx, req *schema.SQLQueryRequest) (*schema.SQLQueryResult, error)
	SQLQueryPrepared(ctx context.Context, tx *sql.SQLTx, stmt sql.DataSource, namedParams []*schema.NamedParam) (*schema.SQLQueryResult, error)
	SQLQueryRowReader(ctx context.Context, tx *sql.SQLTx, stmt sql.DataSource, params map[string]interface{}) (sql.RowReader, error)

	VerifiableSQLGet(ctx context.Context, req *schema.VerifiableSQLGetRequest) (*schema.VerifiableSQLEntry, error)

	ListTables(ctx context.Context, tx *sql.SQLTx) (*schema.SQLQueryResult, error)
	DescribeTable(ctx context.Context, tx *sql.SQLTx, table string) (*schema.SQLQueryResult, error)

	// Transactional layer
	WaitForTx(ctx context.Context, txID uint64, allowPrecommitted bool) error
	WaitForIndexingUpto(ctx context.Context, txID uint64) error

	TxByID(ctx context.Context, req *schema.TxRequest) (*schema.Tx, error)
	ExportTxByID(ctx context.Context, req *schema.ExportTxRequest) (txbs []byte, mayCommitUpToTxID uint64, mayCommitUpToAlh [sha256.Size]byte, err error)
	ReplicateTx(ctx context.Context, exportedTx []byte, skipIntegrityCheck bool, waitForIndexing bool) (*schema.TxHeader, error)
	AllowCommitUpto(txID uint64, alh [sha256.Size]byte) error
	DiscardPrecommittedTxsSince(txID uint64) error

	VerifiableTxByID(ctx context.Context, req *schema.VerifiableTxRequest) (*schema.VerifiableTx, error)
	TxScan(ctx context.Context, req *schema.TxScanRequest) (*schema.TxList, error)

	// Maintenance
	FlushIndex(req *schema.FlushIndexRequest) error
	CompactIndex() error

	IsClosed() bool
	Close() error

	DocumentDatabase
}

func NewDB added in v1.1.0

func NewDB(dbName string, multidbHandler sql.MultiDBHandler, op *Options, log logger.Logger) (DB, error)

NewDB Creates a new Database along with it's directories and files

func OpenDB added in v1.1.0

func OpenDB(dbName string, multidbHandler sql.MultiDBHandler, op *Options, log logger.Logger) (DB, error)

OpenDB Opens an existing Database from disk

type DatabaseList added in v1.0.0

type DatabaseList interface {
	Put(database DB)
	Delete(string) (DB, error)
	GetByIndex(index int) (DB, error)
	GetByName(string) (DB, error)
	GetId(dbname string) int
	Length() int
}

DatabaseList interface

func NewDatabaseList added in v1.0.0

func NewDatabaseList() DatabaseList

NewDatabaseList constructs a new database list

type DocumentDatabase added in v1.5.0

type DocumentDatabase interface {
	// GetCollection returns the collection schema
	GetCollection(ctx context.Context, req *protomodel.GetCollectionRequest) (*protomodel.GetCollectionResponse, error)
	// GetCollections returns the list of collection schemas
	GetCollections(ctx context.Context, req *protomodel.GetCollectionsRequest) (*protomodel.GetCollectionsResponse, error)
	// CreateCollection creates a new collection
	CreateCollection(ctx context.Context, req *protomodel.CreateCollectionRequest) (*protomodel.CreateCollectionResponse, error)
	// UpdateCollection updates an existing collection
	UpdateCollection(ctx context.Context, req *protomodel.UpdateCollectionRequest) (*protomodel.UpdateCollectionResponse, error)
	// DeleteCollection deletes a collection
	DeleteCollection(ctx context.Context, req *protomodel.DeleteCollectionRequest) (*protomodel.DeleteCollectionResponse, error)
	// CreateIndex creates an index for a collection
	CreateIndex(ctx context.Context, req *protomodel.CreateIndexRequest) (*protomodel.CreateIndexResponse, error)
	// DeleteIndex deletes an index from a collection
	DeleteIndex(ctx context.Context, req *protomodel.DeleteIndexRequest) (*protomodel.DeleteIndexResponse, error)
	// InsertDocuments creates new documents
	InsertDocuments(ctx context.Context, req *protomodel.InsertDocumentsRequest) (*protomodel.InsertDocumentsResponse, error)
	// ReplaceDocuments replaces documents matching the query
	ReplaceDocuments(ctx context.Context, req *protomodel.ReplaceDocumentsRequest) (*protomodel.ReplaceDocumentsResponse, error)
	// AuditDocument returns the document audit history
	AuditDocument(ctx context.Context, req *protomodel.AuditDocumentRequest) (*protomodel.AuditDocumentResponse, error)
	// SearchDocuments returns the documents matching the query
	SearchDocuments(ctx context.Context, query *protomodel.Query, offset int64) (document.DocumentReader, error)
	// CountDocuments returns the number of documents matching the query
	CountDocuments(ctx context.Context, req *protomodel.CountDocumentsRequest) (*protomodel.CountDocumentsResponse, error)
	// DeleteDocuments deletes documents maching the query
	DeleteDocuments(ctx context.Context, req *protomodel.DeleteDocumentsRequest) (*protomodel.DeleteDocumentsResponse, error)
	// ProofDocument returns the proofs for a document
	ProofDocument(ctx context.Context, req *protomodel.ProofDocumentRequest) (*protomodel.ProofDocumentResponse, error)
}

DocumentDatabase is the interface for document database

type Options added in v1.1.0

type Options struct {

	// TruncationFrequency determines how frequently to truncate data from the database.
	TruncationFrequency time.Duration

	// RetentionPeriod determines how long to store data in the database.
	RetentionPeriod time.Duration
	// contains filtered or unexported fields
}

Options database instance options

func DefaultOption

func DefaultOption() *Options

DefaultOption Initialise Db Optionts to default values

func (*Options) AsReplica added in v1.1.0

func (o *Options) AsReplica(replica bool) *Options

AsReplica sets if the database is a replica

func (*Options) GetCorruptionChecker added in v1.1.0

func (o *Options) GetCorruptionChecker() bool

GetCorruptionChecker returns if corruption checker should start for this database instance

func (*Options) GetDBRootPath added in v1.1.0

func (o *Options) GetDBRootPath() string

GetDbRootPath returns the directory in which this database resides

func (*Options) GetStoreOptions added in v1.1.0

func (o *Options) GetStoreOptions() *store.Options

GetStoreOptions returns backing store options

func (*Options) GetTxPoolSize added in v1.3.2

func (o *Options) GetTxPoolSize() int

func (*Options) WithCorruptionChecker added in v1.1.0

func (o *Options) WithCorruptionChecker(cc bool) *Options

WithCorruptionChecker sets if corruption checker should start for this database instance

func (*Options) WithDBRootPath added in v1.1.0

func (o *Options) WithDBRootPath(Path string) *Options

WithDbRootPath sets the directory in which this database will reside

func (*Options) WithReadTxPoolSize added in v1.3.2

func (o *Options) WithReadTxPoolSize(txPoolSize int) *Options

func (*Options) WithRetentionPeriod added in v1.5.0

func (o *Options) WithRetentionPeriod(c time.Duration) *Options

func (*Options) WithStoreOptions added in v1.1.0

func (o *Options) WithStoreOptions(storeOpts *store.Options) *Options

WithStoreOptions sets backing store options

func (*Options) WithSyncAcks added in v1.4.0

func (o *Options) WithSyncAcks(syncAcks int) *Options

func (*Options) WithSyncReplication added in v1.4.0

func (o *Options) WithSyncReplication(syncReplication bool) *Options

func (*Options) WithTruncationFrequency added in v1.5.0

func (o *Options) WithTruncationFrequency(c time.Duration) *Options

type Truncator added in v1.5.0

type Truncator interface {
	// Plan returns the latest transaction upto which the log can be truncated.
	// When resulting transaction before specified time does not exists
	//  * No transaction header is returned.
	//  * Returns nil TxHeader, and an error.
	Plan(ctx context.Context, truncationUntil time.Time) (*store.TxHeader, error)

	// TruncateUptoTx runs truncation against the relevant appendable logs. Must
	// be called after result of Plan().
	TruncateUptoTx(ctx context.Context, txID uint64) error
}

Truncator provides truncation against an underlying storage of appendable data.

func NewVlogTruncator added in v1.5.0

func NewVlogTruncator(d DB) Truncator

Jump to

Keyboard shortcuts

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