kv

package
v0.0.0-...-959c02d Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const DefTxnCommitBatchSize uint64 = 16 * 1024

DefTxnCommitBatchSize is the default value of TxnCommitBatchSize.

View Source
const (
	// FlagBytes is the byte size of type KeyFlags
	FlagBytes = 2
)

Variables

View Source
var (
	// ErrTxnConflicts indicates the current transaction contains some vertex/edge/index
	// conflicts with others.
	ErrTxnConflicts = errors.New("transaction conflicts")

	// ErrNotExist means the related data not exist.
	ErrNotExist = errors.New("not exist")

	// ErrCannotSetNilValue is the error when sets an empty value.
	ErrCannotSetNilValue = errors.New("can not set nil value")

	// ErrInvalidTxn is the error when commits or rollbacks in an invalid transaction.
	ErrInvalidTxn = errors.New("invalid transaction")

	ErrInvalidStartVer = errors.New("invalid start timestamp for transaction")
)
View Source
var (
	// MaxVersion is the maximum version, notice that it's not a valid version.
	MaxVersion = Version(math.MaxUint64)
	// MinVersion is the minimum version, it's not a valid version, too.
	MinVersion = Version(0)
)
View Source
var TxnCommitBatchSize atomic.Uint64

TxnCommitBatchSize controls the batch size of transaction commit related requests sent by client to TiKV, TiKV recommends each RPC packet should be less than ~1MB.

Functions

func CmpKey

func CmpKey(k, another []byte) int

CmpKey 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 IsErrNotFound

func IsErrNotFound(err error) bool

func IsRetryable

func IsRetryable(err error) bool

IsRetryable reports whether an error retryable.

func NextKey

func NextKey(k []byte) []byte

NextKey returns the next key in byte-order.

func PrefixNextKey

func PrefixNextKey(k []byte) []byte

PrefixNextKey returns the next prefix key.

Assume there are keys like:

rowkey1
rowkey1_column1
rowkey1_column2
rowKey2

If we seek 'rowkey1' NextKey, we will get 'rowkey1_column1'. If we seek 'rowkey1' PrefixNextKey, we will get 'rowkey2'.

func StrKey

func StrKey(k []byte) string

StrKey returns string for key.

func Txn

func Txn(store Storage, fn func(txn Transaction) error) error

Txn creates a new transaction and call the user-define transaction callback.

func TxnContext

func TxnContext(ctx context.Context, store Storage, fn func(ctx context.Context, txn Transaction) error) error

TxnContext creates a new transaction and call the user-define transaction callback. The transaction will be committed automatically.

Types

type BatchGetter

type BatchGetter interface {
	// BatchGet gets a batch of values.
	BatchGet(ctx context.Context, keys []Key) (map[string][]byte, error)
}

BatchGetter is the interface for BatchGet.

type Entry

type Entry struct {
	Key   Key
	Value []byte
}

Entry is the entry for key and value

type ErrConflict

type ErrConflict struct {
	StartVer          Version
	ConflictStartVer  Version
	ConflictCommitVer Version
	Key               Key
}

ErrConflict is returned when the commitTS of key in the DB is greater than startTS.

func (*ErrConflict) Error

func (e *ErrConflict) Error() string

type ErrEntryTooLarge

type ErrEntryTooLarge struct {
	Limit uint64
	Size  uint64
}

ErrEntryTooLarge is the error when a key value entry is too large.

func (*ErrEntryTooLarge) Error

func (e *ErrEntryTooLarge) Error() string

type ErrGroup

type ErrGroup struct {
	Errors []error
}

ErrGroup is used to collect multiple errors.

func (*ErrGroup) Error

func (e *ErrGroup) Error() string

type ErrKeyAlreadyExist

type ErrKeyAlreadyExist struct {
	Key []byte
}

ErrKeyAlreadyExist is returned when key exists but this key has a constraint that it should not exist. Client should return duplicated entry error.

func (*ErrKeyAlreadyExist) Error

func (e *ErrKeyAlreadyExist) Error() string

type ErrTxnTooLarge

type ErrTxnTooLarge struct {
	Size int
}

ErrTxnTooLarge is the error when transaction is too large, lock time reached the maximum value.

func (*ErrTxnTooLarge) Error

func (e *ErrTxnTooLarge) Error() string

type FlagsOp

type FlagsOp uint32

FlagsOp describes KeyFlags modify operation.

const (
	// SetPresumeKeyNotExists marks the existence of the associated key is checked lazily.
	// Implies KeyFlags.HasNeedCheckExists() == true.
	SetPresumeKeyNotExists FlagsOp = 1 << iota
	// DelPresumeKeyNotExists reverts SetPresumeKeyNotExists.
	DelPresumeKeyNotExists
	// SetKeyLocked marks the associated key has acquired lock.
	SetKeyLocked
	// DelKeyLocked reverts SetKeyLocked.
	DelKeyLocked
	// SetNeedLocked marks the associated key need to be acquired lock.
	SetNeedLocked
	// DelNeedLocked reverts SetKeyNeedLocked.
	DelNeedLocked
	// SetKeyLockedValueExists marks the value exists when key has been locked in Transaction.LockKeys.
	SetKeyLockedValueExists
	// SetKeyLockedValueNotExists marks the value doesn't exists when key has been locked in Transaction.LockKeys.
	SetKeyLockedValueNotExists
	// DelNeedCheckExists marks the key no need to be checked in Transaction.LockKeys.
	DelNeedCheckExists
	// SetPrewriteOnly marks the key shouldn't be used in 2pc commit phase.
	SetPrewriteOnly
	// SetIgnoredIn2PC marks the key will be ignored in 2pc.
	SetIgnoredIn2PC
	// SetReadable marks the key is readable by in-transaction read.
	SetReadable
	// SetNewlyInserted marks the key is newly inserted with value length greater than zero.
	SetNewlyInserted
	// SetAssertExist marks the key must exist.
	SetAssertExist
	// SetAssertNotExist marks the key must not exist.
	SetAssertNotExist
	// SetAssertUnknown mark the key maybe exists or not exists.
	SetAssertUnknown
	// SetAssertNone cleans up the key's assert.
	SetAssertNone
	// SetNeedConstraintCheckInPrewrite marks the key needs to check conflict in prewrite.
	SetNeedConstraintCheckInPrewrite
	// DelNeedConstraintCheckInPrewrite reverts SetNeedConstraintCheckInPrewrite. This is required when we decide to
	// make up the pessimistic lock.
	DelNeedConstraintCheckInPrewrite
	// SetPreviousPresumeKNE sets flagPreviousPresumeKNE.
	SetPreviousPresumeKNE
)

type Getter

type Getter interface {
	// Get gets the value for key k from kv db.
	// If corresponding kv pair does not exist, it returns nil and ErrNotExist.
	Get(ctx context.Context, k Key) ([]byte, error)
}

Getter is the interface for the Get method.

type Iterator

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

Iterator is the interface for a SnapshotIter on KV db.

type Key

type Key []byte

Key represents high-level Key type.

func (Key) Clone

func (k Key) Clone() Key

Clone returns a deep 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_column1'. If we seek 'rowkey1' PrefixNext, we will get 'rowkey2'.

func (Key) String

func (k Key) String() string

String implements fmt.Stringer interface.

type KeyFlags

type KeyFlags uint16

KeyFlags are metadata associated with key. Notice that the highest bit is used by red black tree, do not set flags on it.

func ApplyFlagsOps

func ApplyFlagsOps(origin KeyFlags, ops ...FlagsOp) KeyFlags

ApplyFlagsOps applys flagspos to origin.

func (KeyFlags) AndPersistent

func (f KeyFlags) AndPersistent() KeyFlags

AndPersistent returns the value of current flags&persistentFlags

func (KeyFlags) HasAssertExist

func (f KeyFlags) HasAssertExist() bool

HasAssertExist returns whether the key need ensure exists in 2pc.

func (KeyFlags) HasAssertNotExist

func (f KeyFlags) HasAssertNotExist() bool

HasAssertNotExist returns whether the key need ensure non-exists in 2pc.

func (KeyFlags) HasAssertUnknown

func (f KeyFlags) HasAssertUnknown() bool

HasAssertUnknown returns whether the key is marked unable to do any assertion.

func (KeyFlags) HasAssertionFlags

func (f KeyFlags) HasAssertionFlags() bool

HasAssertionFlags returns whether the key's assertion is set.

func (KeyFlags) HasIgnoredIn2PC

func (f KeyFlags) HasIgnoredIn2PC() bool

HasIgnoredIn2PC returns whether the key will be ignored in 2pc.

func (KeyFlags) HasLocked

func (f KeyFlags) HasLocked() bool

HasLocked returns whether the associated key has acquired pessimistic lock.

func (KeyFlags) HasLockedValueExists

func (f KeyFlags) HasLockedValueExists() bool

HasLockedValueExists returns whether the value exists when key locked.

func (KeyFlags) HasNeedCheckExists

func (f KeyFlags) HasNeedCheckExists() bool

HasNeedCheckExists returns whether the key need to check existence when it has been locked.

func (KeyFlags) HasNeedConstraintCheckInPrewrite

func (f KeyFlags) HasNeedConstraintCheckInPrewrite() bool

HasNeedConstraintCheckInPrewrite returns whether the key needs to check conflict in prewrite.

func (KeyFlags) HasNeedLocked

func (f KeyFlags) HasNeedLocked() bool

HasNeedLocked return whether the key needed to be locked

func (KeyFlags) HasNewlyInserted

func (f KeyFlags) HasNewlyInserted() bool

HasNewlyInserted returns whether the in-transaction key is generated by an "insert" operation.

func (KeyFlags) HasPresumeKeyNotExists

func (f KeyFlags) HasPresumeKeyNotExists() bool

HasPresumeKeyNotExists returns whether the associated key use lazy check.

func (KeyFlags) HasPrewriteOnly

func (f KeyFlags) HasPrewriteOnly() bool

HasPrewriteOnly returns whether the key should be used in 2pc commit phase.

func (KeyFlags) HasReadable

func (f KeyFlags) HasReadable() bool

HasReadable returns whether the in-transaction operations is able to read the key.

type KeyRange

type KeyRange struct {
	StartKey Key
	EndKey   Key

	XXXNoUnkeyedLiteral struct{}
	XXXunrecognized     []byte
	XXXsizecache        int32
}

KeyRange represents a range where StartKey <= key < EndKey. Hack: make the layout exactly the same with github.com/pingcap/kvproto/pkg/coprocessor.KeyRange So we can avoid allocation of converting kv.KeyRange to coprocessor.KeyRange Not defined as "type KeyRange = coprocessor.KeyRange" because their field name are different. kv.KeyRange use StartKey,EndKey while coprocessor.KeyRange use Start,End

func (*KeyRange) IsPoint

func (r *KeyRange) IsPoint() bool

IsPoint checks if the key range represents a point.

type Mutator

type Mutator interface {
	// Set sets the value for key k as v into kv db.
	// 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 db.
	Delete(k Key) error
}

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

type Pair

type Pair struct {
	Key Key
	Val []byte
}

Pair represents a key/value pair.

type Retriever

type Retriever interface {
	Getter
	// Iter 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.
	// It yields only keys that < upperBound. If upperBound is nil, it means the upperBound is unbounded.
	// The Iterator must be Closed after use.
	Iter(lowerBound Key, upperBound Key) (Iterator, error)

	// IterReverse creates a reversed Iterator positioned on the first entry which key is less than k.
	// The returned SnapshotIter will iterate from greater key to smaller key.
	// If k is nil, the returned SnapshotIter will be positioned at the last key.
	IterReverse(lowerBound Key, upperBound 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
	BatchGetter
	// StartVer returns the start verstion of the current snapshot.
	StartVer() Version
}

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

type Storage

type Storage interface {
	VersionProvider

	Begin() (Transaction, error)
	Snapshot(ver Version) (Snapshot, error)
	Close() error
}

type Transaction

type Transaction interface {
	RetrieverMutator

	StartVer() Version
	// Snapshot returns the Snapshot binding to this transaction.
	Snapshot() Snapshot
	// BatchGet gets kv from the memory buffer of statement and transaction, and the kv storage.
	// Do not use len(value) == 0 or value == nil to represent non-exist.
	// If a key doesn't exist, there shouldn't be any corresponding entry in the result map.
	BatchGet(ctx context.Context, keys []Key) (map[string][]byte, error)
	// Size returns sum of keys and values length.
	Size() int
	// Len returns the number of entries in the DB.
	Len() int
	// Reset reset the Transaction to initial states.
	Reset()
	// Commit commits the transaction operations to KV db.
	Commit(context.Context) error
	// Rollback undoes the transaction operations to KV db.
	Rollback() error
	// String implements fmt.Stringer interface.
	String() string
}

type Version

type Version uint64

Version is the wrapper of KV's version.

func Max

func Max(a, b Version) Version

Max returns the larger version between a and b

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 VersionPair

type VersionPair struct {
	StartVer  Version
	CommitVer Version
}

type VersionProvider

type VersionProvider interface {
	CurrentVersion() Version
}

VersionProvider provides increasing IDs.

Jump to

Keyboard shortcuts

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