tuple

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
const (
	SortAsc  = Sorting(+1)
	SortAny  = Sorting(0)
	SortDesc = Sorting(-1)
)

Variables

View Source
var (
	ErrNotFound      = errors.New("tuple: not found")
	ErrTableNotFound = errors.New("tuple: table not found")
	ErrTableExists   = errors.New("tuple: table already exists")
	ErrExists        = errors.New("tuple: this key already exists")
	ErrReadOnly      = errors.New("tuple: read-only database")
)
View Source
var ErrWildGuess = errors.New("can only guess the size")

ErrWildGuess returned if the the size can only be randomly guessed by the backend without scanning the data.

Functions

func DeleteEach

func DeleteEach(ctx context.Context, d Deleter, f *Filter) error

func FilterIterator

func FilterIterator(it LazyTuple, f *Filter, next func() bool) bool

func TableSize

func TableSize(ctx context.Context, t Table, f *Filter, exact bool) (int64, error)

TableSize returns a number of records in a table matching the filter. If exact is set to false, an estimate will be returned. If estimate cannot be obtained without scanning the whole table, ErrWildGuess will be returned with some random number.

func Update

func Update(ctx context.Context, s Store, update func(tx Tx) error) error

Update is a helper to open a read-write transaction and update the database.

func View

func View(ctx context.Context, s Store, view func(tx Tx) error) error

View is a helper to open a read-only transaction to read the database.

Types

type Data

type Data []Value

Data is a tuple payload.

func SData

func SData(data ...string) Data

SData creates a string payload.

type DataFilter

type DataFilter interface {
	FilterData(d Data) bool
}

DataFilter controls if a payload should be considered or not.

type DataFilters

type DataFilters []filter.ValueFilter

DataFilters applies value filters to individual payload components. Filter will reject the value if its length is different. Nil filters are allowed in the slice to indicate no filtering.

func (DataFilters) FilterData

func (arr DataFilters) FilterData(d Data) bool

type Deleter

type Deleter interface {
	// DeleteTuplesByKey removes tuples by key.
	DeleteTuplesByKey(ctx context.Context, keys []Key) error
	Scanner
}

type Field

type Field struct {
	Name string // field name
	Type Type   // field type
}

Field is a single field used in tuple payload.

type Filter

type Filter struct {
	KeyFilter
	DataFilter
}

Filter is a tuple filter.

func (*Filter) FilterData

func (f *Filter) FilterData(d Data) bool

func (*Filter) FilterKey

func (f *Filter) FilterKey(k Key) bool

func (*Filter) FilterTuple

func (f *Filter) FilterTuple(t Tuple) bool

func (*Filter) IsAny

func (f *Filter) IsAny() bool

func (*Filter) IsAnyData

func (f *Filter) IsAnyData() bool

func (*Filter) IsAnyKey

func (f *Filter) IsAnyKey() bool
type Header struct {
	Name string     // name of the table
	Key  []KeyField // primary key fields
	Data []Field    // payload fields
}

Header describes a schema of tuples table.

func (Header) Clone

func (t Header) Clone() Header

Clone makes a copy of the header.

func (Header) DataByName

func (t Header) DataByName(name string) (*Field, int)

DataByName finds a payload field by a name, or returns nil if it not exists. It also returns an index in the tuple payload.

func (Header) KeyByName

func (t Header) KeyByName(name string) (*KeyField, int)

KeyByName finds a key field by a name, or returns nil if it not exists. It also returns an index in the tuple key.

func (Header) Validate

func (t Header) Validate() error

Validate verifies that all fields of header are valid.

func (Header) ValidateData

func (t Header) ValidateData(d Data) error

ValidateData verifies that specific payload is valid for this table.

func (Header) ValidateKey

func (t Header) ValidateKey(k Key, insert bool) error

ValidateKey verifies that specific key is valid for this table.

func (Header) ValidatePref

func (t Header) ValidatePref(k Key) error

ValidatePref verifies that specific key prefix is valid for this table.

type Iterator

type Iterator interface {
	base.Iterator
	// Reset the iterator to the starting state. Closed iterator can not reset.
	Reset()
	// Key returns a primary key of the tuple.
	Key() Key
	// Data returns a payload the tuple.
	Data() Data
}

Iterator is an iterator over a tuple store.

type Key

type Key []Sortable

Key is a tuple primary key.

func AutoKey

func AutoKey() Key

AutoKey returns an auto-increment key value for insert.

func SKey

func SKey(key ...string) Key

SKey creates a string key.

func (Key) Compare

func (k Key) Compare(k2 Key) int

Compare return 0 when keys are equal, -1 when k < k2 and +1 when k > k2.

type KeyField

type KeyField struct {
	Name string  // field name
	Type KeyType // field type
	Auto bool    // autoincrement
}

KeyField is a single primary key field used in tuple.

type KeyFilter

type KeyFilter interface {
	FilterKey(k Key) bool
}

KeyFilter controls if a key should be considered or not.

type KeyFilters

type KeyFilters []filter.ValueFilter

KeyFilters applies value filters to individual key components. If key is shorter, nil value is passed to the filter.

func (KeyFilters) FilterKey

func (arr KeyFilters) FilterKey(k Key) bool

type KeyType

type KeyType = values.SortableType

KeyType is a value type that can be sorted after serialization.

type Keys

type Keys []Key

Keys is a filter that accepts only specific keys.

func (Keys) FilterKey

func (arr Keys) FilterKey(k Key) bool

type LazyTuple

type LazyTuple interface {
	Key() Key
	Data() Data
}

type ScanOptions

type ScanOptions struct {
	// KeysOnly is a hint for backend to omit fetching keys for an iterator.
	KeysOnly bool
	// Sort is an optional sorting order for a tuple key. Defaults to a native order of the backend.
	Sort Sorting
	// Filter is an optional filter for tuples.
	Filter *Filter
	// Limit limits the maximal number of tuples to return. Limit <= 0 indicates an unlimited number of results.
	Limit int
}

type Scanner

type Scanner interface {
	// Scan iterates over all tuples matching specific parameters.
	Scan(ctx context.Context, opt *ScanOptions) Iterator
}

type Sortable

type Sortable = values.Sortable

Sortable is a value that can be sorted after serialization.

type Sorting

type Sorting int

type Store

type Store interface {
	base.DB
	// Tx opens a read-only or read-write transaction in the tuple store.
	Tx(ctx context.Context, rw bool) (Tx, error)
	// View provides functional-style read-only transactional access the tuple store.
	View(ctx context.Context, view func(tx Tx) error) error
	// Update provides functional-style read-write transactional access to the tuple store.
	Update(ctx context.Context, update func(tx Tx) error) error
	// Table returns a table info. It returns ErrTableNotFound if table does not exists.
	// TableInfo can be used to open a Table from transactions more efficiently.
	Table(ctx context.Context, name string) (TableInfo, error)
	// ListTables lists all available tables.
	ListTables(ctx context.Context) ([]TableInfo, error)
}

Store is an interface for tuple stores with a strict schema.

type Table

type Table interface {
	TableInfo

	// Drop clears the data and removes the table.
	Drop(ctx context.Context) error
	// Clear removes all tuples from the table.
	Clear(ctx context.Context) error

	// GetTuple fetches one tuple with a specific key.
	// It returns ErrNotFound if tuple does not exists.
	GetTuple(ctx context.Context, key Key) (Data, error)
	// GetTupleBatch fetches multiple tuples with provided keys.
	// Nil values in the returned slice indicates that specific key does not exists.
	GetTupleBatch(ctx context.Context, keys []Key) ([]Data, error)
	// InsertTuple creates a new tuple. If the tuple with specified key already exists it returns ErrExists.
	InsertTuple(ctx context.Context, t Tuple) (Key, error)
	// UpdateTuple rewrites specified tuple. Options can be provided to create a tuple if ti does not exists.
	// If this flag is not provided and the tuple is missing, it returns ErrNotFound.
	UpdateTuple(ctx context.Context, t Tuple, opt *UpdateOpt) error
	// DeleteTuples removes all tuples that matches a filter.
	DeleteTuples(ctx context.Context, f *Filter) error

	// Scanner adds the Scan function.
	Scanner
}

Table represents an opened tuples table with a specific type (schema).

type TableInfo

type TableInfo interface {
	// Header returns a tuple header used in this table.
	Header() Header
	// Open binds a table to the transaction and opens it for further operations.
	Open(ctx context.Context, tx Tx) (Table, error)
}

TableInfo represent a metadata of a tuple table.

type Tuple

type Tuple struct {
	Key  Key  // primary key
	Data Data // payload
}

Tuple is a data tuple.

type Tx

type Tx interface {
	base.Tx
	// Table opens a tuple table. It returns ErrTableNotFound if table does not exists.
	Table(ctx context.Context, name string) (Table, error)
	// ListTables lists all available tables.
	ListTables(ctx context.Context) ([]Table, error)
	// CreateTable creates and opens a table with a specific schema.
	CreateTable(ctx context.Context, table Header) (Table, error)
}

Tx is a transaction over a tuple store.

type Type

type Type = values.Type

Type is any value type that can be stored in tuple.

type UpdateOpt

type UpdateOpt struct {
	Upsert bool // create a tuple if it does not exists
}

UpdateOpt specifies options used in UpdateTuple.

type Value

type Value = values.Value

Value is any value that can be stored in tuple.

Directories

Path Synopsis
sql
all
Package tuplepb is a generated protocol buffer package.
Package tuplepb is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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