kv

package
v0.0.0-...-887f819 Latest Latest
Warning

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

Go to latest
Published: May 4, 2016 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReqTypeSelect = 101
	ReqTypeIndex  = 102

	ReqSubTypeBasic = 0
	ReqSubTypeDesc  = 10000
)

ReqTypes.

Variables

View Source
var (
	// ErrClosed is used when close an already closed txn.
	ErrClosed = terror.ClassKV.New(codeClosed, "Error: Transaction already closed")
	// ErrNotExist is used when try to get an entry with an unexist key from KV store.
	ErrNotExist = terror.ClassKV.New(codeNotExist, "Error: key not exist")
	// ErrConditionNotMatch is used when condition is not met.
	ErrConditionNotMatch = terror.ClassKV.New(codeCondithinNotMatch, "Error: Condition not match")
	// ErrLockConflict is used when try to lock an already locked key.
	ErrLockConflict = terror.ClassKV.New(codeLockConfilict, "Error: Lock conflict")
	// ErrLazyConditionPairsNotMatch is used when value in store differs from expect pairs.
	ErrLazyConditionPairsNotMatch = terror.ClassKV.New(codeLazyConditionPairsNotMatch, "Error: Lazy condition pairs not match")
	// ErrRetryable is used when KV store occurs RPC error or some other
	// errors which SQL layer can safely retry.
	ErrRetryable = terror.ClassKV.New(codeRetryable, "Error: KV error safe to retry")
	// ErrCannotSetNilValue is the error when sets an empty value.
	ErrCannotSetNilValue = terror.ClassKV.New(codeCantSetNilValue, "can not set nil value")
	// ErrInvalidTxn is the error when commits or rollbacks in an invalid transaction.
	ErrInvalidTxn = terror.ClassKV.New(codeInvalidTxn, "invalid transaction")

	// ErrNotCommitted is the error returned by CommitVersion when this
	// transaction is not committed.
	ErrNotCommitted = terror.ClassKV.New(codeNotCommitted, "this transaction has not committed")

	// ErrKeyExists returns when key is already exist.
	ErrKeyExists = terror.ClassKV.New(codeKeyExists, "key already exist")
	// ErrNotImplemented returns when a function is not implemented yet.
	ErrNotImplemented = terror.ClassKV.New(codeNotImplemented, "not implemented")
)
View Source
var (
	// MaxVersion is the maximum version, notice that it's not a valid version.
	MaxVersion = Version{Ver: math.MaxUint64}
	// MinVersion is the minimum version, it's not a valid version, too.
	MinVersion = Version{Ver: 0}
)

Functions

func BackOff

func BackOff(attempts int) int

BackOff Implements exponential backoff with full jitter. Returns real back off time in microsecond. See: http://www.awsarchitectureblog.com/2015/03/backoff.html.

func GetInt64

func GetInt64(r Retriever, k Key) (int64, error)

GetInt64 get int64 value which created by IncInt64 method.

func IncInt64

func IncInt64(rm RetrieverMutator, k Key, step int64) (int64, error)

IncInt64 increases the value for key k in kv store by step.

func IsErrNotFound

func IsErrNotFound(err error) bool

IsErrNotFound checks if err is a kind of NotFound error.

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError checks if the err is a fatal error and the under going operation is worth to retry.

func NextUntil

func NextUntil(it Iterator, fn FnKeyCmp) error

NextUntil applies FnKeyCmp to each entry of the iterator until meets some condition. It will stop when fn returns true, or iterator is invalid or an error occurs.

func RunInNewTxn

func RunInNewTxn(store Storage, retryable bool, f func(txn Transaction) error) error

RunInNewTxn will run the f in a new transaction environment.

Types

type BufferStore

type BufferStore struct {
	MemBuffer
	// contains filtered or unexported fields
}

BufferStore wraps a Retriever for read and a MemBuffer for buffered write. Common usage pattern:

bs := NewBufferStore(r) // use BufferStore to wrap a Retriever
defer bs.Release()      // make sure it will be released
// ...
// read/write on bs
// ...
bs.SaveTo(m)	        // save above operations to a Mutator

func NewBufferStore

func NewBufferStore(r Retriever) *BufferStore

NewBufferStore creates a BufferStore using r for read.

func (*BufferStore) Get

func (s *BufferStore) Get(k Key) ([]byte, error)

Get implements the Retriever interface.

func (*BufferStore) SaveTo

func (s *BufferStore) SaveTo(m Mutator) error

SaveTo saves all buffered kv pairs into a Mutator.

func (*BufferStore) Seek

func (s *BufferStore) Seek(k Key) (Iterator, error)

Seek implements the Retriever interface.

func (*BufferStore) SeekReverse

func (s *BufferStore) SeekReverse(k Key) (Iterator, error)

SeekReverse implements the Retriever interface.

func (*BufferStore) WalkBuffer

func (s *BufferStore) WalkBuffer(f func(k Key, v []byte) error) error

WalkBuffer iterates all buffered kv pairs.

type Client

type Client interface {
	// Send sends request to KV layer, returns a Response.
	Send(req *Request) Response

	// SupportRequestType checks if reqType and subType is supported.
	SupportRequestType(reqType, subType int64) bool
}

Client is used to send request to KV layer.

type Driver

type Driver interface {
	// Open returns a new Storage.
	// The path is the string for storage specific format.
	Open(path string) (Storage, error)
}

Driver is the interface that must be implemented by a KV storage.

type EncodedKey

type EncodedKey []byte

EncodedKey represents encoded key in low-level storage engine.

func (EncodedKey) Cmp

func (k EncodedKey) Cmp(another EncodedKey) int

Cmp returns the comparison result of two key. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.

func (EncodedKey) Next

func (k EncodedKey) Next() EncodedKey

Next returns the next key in byte-order.

type FnKeyCmp

type FnKeyCmp func(key Key) bool

FnKeyCmp is the function for iterator the keys

type Index

type Index interface {
	// Create supports insert into statement.
	Create(rm RetrieverMutator, indexedValues []types.Datum, h int64) error
	// Delete supports delete from statement.
	Delete(m Mutator, indexedValues []types.Datum, h int64) error
	// Drop supports drop table, drop index statements.
	Drop(rm RetrieverMutator) error
	// Exist supports check index exists or not.
	Exist(rm RetrieverMutator, indexedValues []types.Datum, h int64) (bool, int64, error)
	// GenIndexKey generates an index key.
	GenIndexKey(indexedValues []types.Datum, h int64) (key []byte, distinct bool, err error)
	// Seek supports where clause.
	Seek(r Retriever, indexedValues []types.Datum) (iter IndexIterator, hit bool, err error)
	// SeekFirst supports aggregate min and ascend order by.
	SeekFirst(r Retriever) (iter IndexIterator, err error)
}

Index is the interface for index data on KV store.

func NewKVIndex

func NewKVIndex(indexPrefix Key, indexName string, indexID int64, unique bool) Index

NewKVIndex builds a new kvIndex object.

type IndexIterator

type IndexIterator interface {
	Next() (k []types.Datum, h int64, err error)
	Close()
}

IndexIterator is the interface for iterator of index data on KV store.

type Iterator

type Iterator interface {
	Valid() bool
	Key() Key
	Value() []byte
	Next() error
	Close()
}

Iterator is the interface for a iterator on KV store.

type Key

type Key []byte

Key represents high-level Key type.

func GenIndexPrefix

func GenIndexPrefix(indexPrefix Key, indexID int64) Key

GenIndexPrefix generates the index prefix.

func (Key) Clone

func (k Key) Clone() Key

Clone returns a copy of the Key.

func (Key) Cmp

func (k Key) Cmp(another Key) int

Cmp returns the comparison result of two key. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.

func (Key) HasPrefix

func (k Key) HasPrefix(prefix Key) bool

HasPrefix tests whether the Key begins with prefix.

func (Key) Next

func (k Key) Next() Key

Next returns the next key in byte-order.

func (Key) PrefixNext

func (k Key) PrefixNext() Key

PrefixNext returns the next prefix key.

Assume there are keys like:

rowkey1
rowkey1_column1
rowkey1_column2
rowKey2

If we seek 'rowkey1' Next, we will get 'rowkey1_colum1'. If we seek 'rowkey1' PrefixNext, we will get 'rowkey2'.

type KeyRange

type KeyRange struct {
	StartKey Key
	EndKey   Key
}

KeyRange represents a range where StartKey <= key < EndKey.

func (*KeyRange) IsPoint

func (r *KeyRange) IsPoint() bool

IsPoint checks if the key range represents a point.

type MemBuffer

type MemBuffer interface {
	RetrieverMutator
	// Release releases the buffer.
	Release()
}

MemBuffer is an in-memory kv collection. It should be released after use.

func NewMemDbBuffer

func NewMemDbBuffer() MemBuffer

NewMemDbBuffer creates a new memDbBuffer.

func NewRBTreeBuffer

func NewRBTreeBuffer() MemBuffer

NewRBTreeBuffer creates a new rbTreeBuffer.

type Mutator

type Mutator interface {
	// Set sets the value for key k as v into kv store.
	// v must NOT be nil or empty, otherwise it returns ErrCannotSetNilValue.
	Set(k Key, v []byte) error
	// Delete removes the entry for key k from kv store.
	Delete(k Key) error
}

Mutator is the interface wraps the basic Set and Delete methods.

type Option

type Option int

Option is used for customizing kv store's behaviors during a transaction.

const (
	// PresumeKeyNotExists directives that when dealing with a Get operation but failing to read data from cache,
	// we presume that the key does not exist in Store. The actual existence will be checked before the
	// transaction's commit.
	// This option is an optimization for frequent checks during a transaction, e.g. batch inserts.
	PresumeKeyNotExists Option = iota + 1
	// PresumeKeyNotExistsError is the option key for error.
	// When PresumeKeyNotExists is set and condition is not match, should throw the error.
	PresumeKeyNotExistsError
)

type Options

type Options interface {
	// Get gets an option value.
	Get(opt Option) (v interface{}, ok bool)
}

Options is an interface of a set of options. Each option is associated with a value.

type Request

type Request struct {
	// The request type.
	Tp   int64
	Data []byte
	// Key Ranges
	KeyRanges []KeyRange
	// If desc is true, the request is sent in descending order.
	Desc bool
	// If concurrency is 1, it only sends the request to a single storage unit when
	// ResponseIterator.Next is called. If concurrency is greater than 1, the request will be
	// sent to multiple storage units concurrently.
	Concurrency int
}

Request represents a kv request.

type Response

type Response interface {
	// Next returns a resultSubset from a single storage unit.
	// When full result set is returned, nil is returned.
	Next() (resultSubset io.ReadCloser, err error)
	// Close response.
	Close() error
}

Response represents the response returned from KV layer.

type Retriever

type Retriever interface {
	// Get gets the value for key k from kv store.
	// If corresponding kv pair does not exist, it returns nil and ErrNotExist.
	Get(k Key) ([]byte, error)
	// Seek creates an Iterator positioned on the first entry that k <= entry's key.
	// If such entry is not found, it returns an invalid Iterator with no error.
	// The Iterator must be Closed after use.
	Seek(k Key) (Iterator, error)

	// SeekReverse creates a reversed Iterator positioned on the first entry which key is less than k.
	// The returned iterator will iterate from greater key to smaller key.
	// If k is nil, the returned iterator will be positioned at the last key.
	SeekReverse(k Key) (Iterator, error)
}

Retriever is the interface wraps the basic Get and Seek methods.

type RetrieverMutator

type RetrieverMutator interface {
	Retriever
	Mutator
}

RetrieverMutator is the interface that groups Retriever and Mutator interfaces.

type Snapshot

type Snapshot interface {
	Retriever
	// BatchGet gets a batch of values from snapshot.
	BatchGet(keys []Key) (map[string][]byte, error)
	// Release releases the snapshot to store.
	Release()
}

Snapshot defines the interface for the snapshot fetched from KV store.

type Storage

type Storage interface {
	// Begin transaction
	Begin() (Transaction, error)
	// GetSnapshot gets a snapshot that is able to read any data which data is <= ver.
	// if ver is MaxVersion or > current max committed version, we will use current version for this snapshot.
	GetSnapshot(ver Version) (Snapshot, error)
	// Close store
	Close() error
	// Storage's unique ID
	UUID() string
	// CurrentVersion returns current max committed version.
	CurrentVersion() (Version, error)
}

Storage defines the interface for storage. Isolation should be at least SI(SNAPSHOT ISOLATION)

type Transaction

type Transaction interface {
	RetrieverMutator
	// Commit commits the transaction operations to KV store.
	Commit() error
	// Rollback undoes the transaction operations to KV store.
	Rollback() error
	// String implements fmt.Stringer interface.
	String() string
	// LockKeys tries to lock the entries with the keys in KV store.
	LockKeys(keys ...Key) error
	// SetOption sets an option with a value, when val is nil, uses the default
	// value of this option.
	SetOption(opt Option, val interface{})
	// DelOption deletes an option.
	DelOption(opt Option)
	// IsReadOnly checks if the transaction has only performed read operations.
	IsReadOnly() bool
	// GetClient gets a client instance.
	GetClient() Client
	// StartTS returns the transaction start timestamp.
	StartTS() uint64
}

Transaction defines the interface for operations inside a Transaction. This is not thread safe.

type UnionIter

type UnionIter struct {
	// contains filtered or unexported fields
}

UnionIter is the iterator on an UnionStore.

func (*UnionIter) Close

func (iter *UnionIter) Close()

Close implements the Iterator Close interface.

func (*UnionIter) Key

func (iter *UnionIter) Key() Key

Key implements the Iterator Key interface.

func (*UnionIter) Next

func (iter *UnionIter) Next() error

Next implements the Iterator Next interface.

func (*UnionIter) Valid

func (iter *UnionIter) Valid() bool

Valid implements the Iterator Valid interface.

func (*UnionIter) Value

func (iter *UnionIter) Value() []byte

Value implements the Iterator Value interface. Multi columns

type UnionStore

type UnionStore interface {
	MemBuffer
	// CheckLazyConditionPairs loads all lazy values from store then checks if all values are matched.
	// Lazy condition pairs should be checked before transaction commit.
	CheckLazyConditionPairs() error
	// WalkBuffer iterates all buffered kv pairs.
	WalkBuffer(f func(k Key, v []byte) error) error
	// SetOption sets an option with a value, when val is nil, uses the default
	// value of this option.
	SetOption(opt Option, val interface{})
	// DelOption deletes an option.
	DelOption(opt Option)
}

UnionStore is a store that wraps a snapshot for read and a BufferStore for buffered write. Also, it provides some transaction related utilities.

func NewUnionStore

func NewUnionStore(snapshot Snapshot) UnionStore

NewUnionStore builds a new UnionStore.

type Version

type Version struct {
	Ver uint64
}

Version is the wrapper of KV's version.

func NewVersion

func NewVersion(v uint64) Version

NewVersion creates a new Version struct.

func (Version) Cmp

func (v Version) Cmp(another Version) int

Cmp returns the comparison result of two versions. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.

type VersionProvider

type VersionProvider interface {
	CurrentVersion() (Version, error)
}

VersionProvider provides increasing IDs.

Jump to

Keyboard shortcuts

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