db

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2018 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Separator of the key segment
	Separator = ":"
	// ObjectEncodingLength indecate current object marshaled length
	ObjectEncodingLength = 42
)
View Source
const (
	ObjectEncodingRaw = ObjectEncoding(iota)
	ObjectEncodingInt
	ObjectEncodingHT
	ObjectEncodingZipmap
	ObjectEncodingLinkedlist
	ObjectEncodingZiplist
	ObjectEncodingIntset
	ObjectEncodingSkiplist
	ObjectEncodingEmbstr
	ObjectEncodingQuicklist
)

Encoding values, see https://github.com/antirez/redis/blob/unstable/src/server.h#L581

View Source
const (
	ObjectString = ObjectType(iota)
	ObjectList
	ObjectSet
	ObjectZset
	ObjectHash
)

Object types, see https://github.com/antirez/redis/blob/unstable/src/server.h#L461

Variables

View Source
var (
	// ErrTypeMismatch indicates object type of key is not as expect
	ErrTypeMismatch = errors.New("type mismatch")

	// ErrKeyNotFound key not exist
	ErrKeyNotFound = errors.New("key not found")

	// ErrInteger valeu is not interge
	ErrInteger = errors.New("value is not an integer or out of range")

	// ErrPrecision list index reach precision limitatin
	ErrPrecision = errors.New("list reaches precision limitation, rebalance now")

	// ErrOutOfRange index/offset out of range
	ErrOutOfRange = errors.New("error index/offset out of range")

	// ErrInvalidLength data length is invalid for unmarshaler"
	ErrInvalidLength = errors.New("error data length is invalid for unmarshaler")

	// ErrEncodingMismatch object encoding type
	ErrEncodingMismatch = errors.New("error object encoding type")

	// IsErrNotFound returns true if the key is not found, otherwise return false
	IsErrNotFound = store.IsErrNotFound

	// IsRetryableError returns true if the error is temporary and can be retried
	IsRetryableError = store.IsRetryableError

	// IsConflictError return true if the error is conflict
	IsConflictError = store.IsConflictError
)
View Source
var SetNilValue = []byte{0}

SetNilValue is the value set to a tikv key for tikv do not support a real empty value

Functions

func BatchGetValues

func BatchGetValues(txn *Transaction, keys [][]byte) ([][]byte, error)

BatchGetValues issues batch requests to get values

func DataKey

func DataKey(db *DB, key []byte) []byte

DataKey builds a datakey from a redis key

func DecodeFloat64

func DecodeFloat64(d []byte) float64

DecodeFloat64 decode the float64 object from binary

func DecodeInt64

func DecodeInt64(b []byte) int64

DecodeInt64 decode the int64 object from binary

func EncodeFloat64

func EncodeFloat64(v float64) []byte

EncodeFloat64 encode the float64 object to binary

func EncodeInt64

func EncodeInt64(v int64) []byte

EncodeInt64 encode the int64 object to binary

func EncodeObject

func EncodeObject(obj *Object) []byte

EncodeObject encode the object to binary

func IsExpired

func IsExpired(obj *Object, now int64) bool

IsExpired judge object expire through now

func MetaKey

func MetaKey(db *DB, key []byte) []byte

MetaKey build to metakey from a redis key

func Now

func Now() int64

Now returns the current unix nano timestamp.

func PutZList

func PutZList(txn *Transaction, metakey []byte) error

PutZList should be called after ZList created

func RemoveZTKey

func RemoveZTKey(txn *Transaction, metakey []byte) error

RemoveZTKey remove an metakey from ZT

func StartExpire

func StartExpire(db *DB) error

StartExpire get leader from db

func StartGC

func StartGC(db *DB)

StartGC start gc 1.获取leader许可 2.leader 执行清理任务

func StartZT

func StartZT(db *DB, conf *conf.ZT)

StartZT start ZT fill in the queue(channel), and start the worker to consume.

func UUID

func UUID() []byte

UUID allocates an unique object ID.

func UUIDString

func UUIDString(id []byte) string

UUIDString returns canonical string representation of UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Types

type DB

type DB struct {
	Namespace string
	ID        DBID
	// contains filtered or unexported fields
}

DB is a redis compatible data structure storage

func (*DB) Begin

func (db *DB) Begin() (*Transaction, error)

Begin a transaction

func (*DB) Prefix

func (db *DB) Prefix() []byte

Prefix returns the prefix of a DB object

type DBID

type DBID byte

DBID is the redis database ID

func (DBID) Bytes

func (id DBID) Bytes() []byte

Bytes DBID returns a byte slice

func (DBID) String

func (id DBID) String() string

String returns the string format of DBID

type Hash

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

Hash implements the hashtable

func GetHash

func GetHash(txn *Transaction, key []byte) (*Hash, error)

GetHash returns a hash object, create new one if nonexists

func (*Hash) Destory

func (hash *Hash) Destory() error

Destory the hash store

func (*Hash) HDel

func (hash *Hash) HDel(fields [][]byte) (int64, error)

HDel removes the specified fields from the hash stored at key

func (*Hash) HExists

func (hash *Hash) HExists(field []byte) (bool, error)

HExists returns if field is an existing field in the hash stored at key

func (*Hash) HGet

func (hash *Hash) HGet(field []byte) ([]byte, error)

HGet returns the value associated with field in the hash stored at key

func (*Hash) HGetAll

func (hash *Hash) HGetAll() ([][]byte, [][]byte, error)

HGetAll returns all fields and values of the hash stored at key

func (*Hash) HIncrBy

func (hash *Hash) HIncrBy(field []byte, v int64) (int64, error)

HIncrBy increments the number stored at field in the hash stored at key by increment

func (*Hash) HIncrByFloat

func (hash *Hash) HIncrByFloat(field []byte, v float64) (float64, error)

HIncrByFloat increment the specified field of a hash stored at key, and representing a floating point number, by the specified increment

func (*Hash) HLen

func (hash *Hash) HLen() int64

HLen returns the number of fields contained in the hash stored at key

func (*Hash) HMGet

func (hash *Hash) HMGet(fields [][]byte) ([][]byte, error)

HMGet returns the values associated with the specified fields in the hash stored at key

func (*Hash) HMSet

func (hash *Hash) HMSet(fields [][]byte, values [][]byte) error

HMSet sets the specified fields to their respective values in the hash stored at key

func (*Hash) HSet

func (hash *Hash) HSet(field []byte, value []byte) (int, error)

HSet sets field in the hash stored at key to value

func (*Hash) HSetNX

func (hash *Hash) HSetNX(field []byte, value []byte) (int, error)

HSetNX sets field in the hash stored at key to value, only if field does not yet exist

type HashMeta

type HashMeta struct {
	Object
	Len int64
}

HashMeta is the meta data of the hashtable

type Iterator

type Iterator store.Iterator

Iterator store.Iterator

type Kv

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

Kv supplies key releated operations

func GetKv

func GetKv(txn *Transaction) *Kv

GetKv returns a Kv object

func (*Kv) Delete

func (kv *Kv) Delete(keys [][]byte) (int64, error)

Delete specific keys, ignore if non exist

func (*Kv) Exists

func (kv *Kv) Exists(keys [][]byte) (int64, error)

Exists check if the given keys exist

func (*Kv) ExpireAt

func (kv *Kv) ExpireAt(key []byte, at int64) error

ExpireAt set a timeout on key

func (*Kv) FlushAll

func (kv *Kv) FlushAll() error

FlushAll clean up all databases. FIXME one txn is limited for number of entries

func (*Kv) FlushDB

func (kv *Kv) FlushDB() error

FlushDB clear current db. FIXME one txn is limited for number of entries

func (*Kv) Keys

func (kv *Kv) Keys(start []byte, f func(key []byte) bool) error

Keys iterator all keys in db

func (*Kv) RandomKey

func (kv *Kv) RandomKey() ([]byte, error)

RandomKey return a key from current db randomly Now we use an static length(64) to generate the key spaces, it means it is random for keys that len(key) <= 64, it is enough for most cases

type LList

type LList struct {
	LListMeta
	// contains filtered or unexported fields
}

LList is a distributed object that works like a double link list {Object schema}:{index} -> value

func (*LList) Destory

func (l *LList) Destory() error

Destory the list

func (*LList) Exist

func (l *LList) Exist() bool

Exist checks if a list exists

func (*LList) Index

func (l *LList) Index(n int64) (data []byte, err error)

Index returns the element at index n in the list stored at key

func (*LList) Insert

func (l *LList) Insert(pivot, v []byte, before bool) error

Insert value in the list stored at key either before or after the reference value pivot 1. pivot berfore/ pivot/ next --> real indexs

func (*LList) LPop

func (l *LList) LPop() (data []byte, err error)

LPop returns and deletes the left most element 0. calculate data key 1. iterate to last value 2. get the key and call kv delete 3. modify the new index in meta

func (*LList) LPush

func (l *LList) LPush(data ...[]byte) (err error)

LPush adds new elements to the left 1. calculate index 2. encode object and call kv 3. modify the new index in meta

func (*LList) LRem

func (l *LList) LRem(v []byte, n int64) (int, error)

LRem removes the first count occurrences of elements equal to value from the list stored at key

func (*LList) LTrim

func (l *LList) LTrim(start int64, stop int64) error

LTrim an existing list so that it will contain only the specified range of elements specified

func (*LList) Length

func (l *LList) Length() int64

Length returns length of the list

func (*LList) RPop

func (l *LList) RPop() ([]byte, error)

RPop returns and deletes the right most element

func (*LList) RPush

func (l *LList) RPush(data ...[]byte) (err error)

RPush pushes elements into right side of list

func (*LList) Range

func (l *LList) Range(left, right int64) (value [][]byte, err error)

Range returns the elements in [left, right]

func (*LList) Set

func (l *LList) Set(n int64, data []byte) error

Set the index object with given value, return ErrIndex on out of range error.

type LListMeta

type LListMeta struct {
	Object
	Len    int64
	Lindex float64
	Rindex float64
}

LListMeta keeps all meta info of a list object after marshaled, listmeta raw data should organized like below |----object 34----| |------len 8------| |----linedx 8-----| |----rindex 8-----|

func (*LListMeta) Marshal

func (l *LListMeta) Marshal() []byte

Marshal encodes meta data into byte slice

func (*LListMeta) Unmarshal

func (l *LListMeta) Unmarshal(obj *Object, b []byte) (err error)

Unmarshal parses meta data into meta field

type Lease

type Lease struct {
	Object
	TouchedAt int64
}

Lease is an object that can be associated with other objects those can share the same ttl of the lease

type List

type List interface {
	Index(n int64) (data []byte, err error)
	Insert(pivot, v []byte, before bool) error
	LPop() (data []byte, err error)
	LPush(data ...[]byte) (err error)
	RPop() (data []byte, err error)
	RPush(data ...[]byte) (err error)
	Range(left, right int64) (value [][]byte, err error)
	LRem(v []byte, n int64) (int, error)
	Set(n int64, data []byte) error
	LTrim(start int64, stop int64) error
	Length() int64
	Exist() bool
	Destory() error
}

List defines the list interface

func GetLList

func GetLList(txn *Transaction, metaKey []byte, obj *Object, val []byte) (List, error)

GetLList returns a list

func GetList

func GetList(txn *Transaction, key []byte, opts ...ListOption) (List, error)

GetList returns a List object, it creates a new one if the key does not exist, when UseZip() is set, it will create a ziplist instead of a linklist

func NewLList

func NewLList(txn *Transaction, key []byte) List

NewLList creates a new list

func NewZList

func NewZList(txn *Transaction, key []byte) List

NewZList create new list object ,the key is not checked for presence

type ListOption

type ListOption func(o *listOption)

ListOption customize how to get a list

func UseZip

func UseZip() ListOption

UseZip will create a ziplist if set when the key is missing

type Object

type Object struct {
	ID        []byte
	Type      ObjectType     //refer to redis
	Encoding  ObjectEncoding //refer to redis
	CreatedAt int64
	UpdatedAt int64
	ExpireAt  int64
}

Object meta schema

Layout {DB}:{TAG}:{Key}
DB     [0-255]
Key    Usersapce key
TAG    M(Meta), D(Data)

Object data schema

Layout: {DB}:{TAG}:{ID}:{Others}
ID     Object ID, ID is not used for meta

String schema (associated value with meta)

Layout: {DB}:M:{key}

func DecodeObject

func DecodeObject(b []byte) (obj *Object, err error)

DecodeObject decode the object from binary

func (*Object) String

func (obj *Object) String() string

String representation of an object

type ObjectEncoding

type ObjectEncoding byte

ObjectEncoding is the encoding type of an object

func (ObjectEncoding) String

func (enc ObjectEncoding) String() string

String representation of ObjectEncoding

type ObjectType

type ObjectType byte

ObjectType is the type of a data structure

func (ObjectType) String

func (t ObjectType) String() string

String representation of object type

type RedisStore

type RedisStore struct {
	store.Storage
}

RedisStore wraps store.Storage

func Open

func Open(conf *conf.Tikv) (*RedisStore, error)

Open a storage instance

func (*RedisStore) Close

func (rds *RedisStore) Close() error

Close the storage instance

func (*RedisStore) DB

func (rds *RedisStore) DB(namesapce string, id int) *DB

DB returns a DB object with sepcific ID

type Set

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

Set implements the set data structure

func GetSet

func GetSet(txn *Transaction, key []byte) (*Set, error)

GetSet returns a set object, create new one if nonexists

func (*Set) SAdd

func (set *Set) SAdd(members [][]byte) (int64, error)

SAdd adds the specified members to the set stored at key

func (*Set) SMembers

func (set *Set) SMembers() ([][]byte, error)

SMembers returns all the members of the set value stored at key

type SetMeta

type SetMeta struct {
	Object
	Len int64
}

SetMeta is the meta data of the set

type String

type String struct {
	Meta StringMeta
	// contains filtered or unexported fields
}

String object operate tikv

func GetString

func GetString(txn *Transaction, key []byte) (*String, error)

GetString return string object , if key is exist , object load meta otherwise object is null if key is not exist and err is not found otherwise return err

func NewString

func NewString(txn *Transaction, key []byte) *String

NewString create new string object

func (*String) Append

func (s *String) Append(value []byte) (int, error)

Append append a value to key

func (*String) Exist

func (s *String) Exist() bool

Exist return ture if key exist

func (*String) Get

func (s *String) Get() ([]byte, error)

Get the value information for the key from db

func (*String) GetRange

func (s *String) GetRange(start, end int) []byte

GetRange return string from the absolute of start to the absolute of end

func (*String) GetSet

func (s *String) GetSet(value []byte) ([]byte, error)

GetSet return old value ,value replace old value

func (*String) Incr

func (s *String) Incr(delta int64) (int64, error)

Incr increment the integer value by the given amount the old value must be integer

func (*String) Incrf

func (s *String) Incrf(delta float64) (float64, error)

Incrf increment the float value by the given amount the old value must be float

func (*String) Len

func (s *String) Len() (int, error)

Len value len

func (*String) Set

func (s *String) Set(val []byte, expire ...int64) error

Set set the string value of a key the num of expire slice is not zero and expire[0] is not zero ,the key add exprie queue otherwise the delete expire queue

func (*String) SetRange

func (s *String) SetRange(offset int64, value []byte) ([]byte, error)

SetRange Overwrites part of the string stored at key, starting at the specified offset, for the entire length of value.

type StringMeta

type StringMeta struct {
	Object
	Value []byte
}

StringMeta string meta msg

type Transaction

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

Transaction supplies transaction for data structures

func (*Transaction) Commit

func (txn *Transaction) Commit(ctx context.Context) error

Commit a transaction

func (*Transaction) Destory

func (txn *Transaction) Destory(obj *Object, key []byte) error

Destory the object

func (*Transaction) Hash

func (txn *Transaction) Hash(key []byte) (*Hash, error)

Hash returns a hash object

func (*Transaction) Kv

func (txn *Transaction) Kv() *Kv

Kv returns a kv object

func (*Transaction) List

func (txn *Transaction) List(key []byte, opts ...ListOption) (List, error)

List return a lists object, a new list is created if the key dose not exist.

func (*Transaction) LockKeys

func (txn *Transaction) LockKeys(keys ...[]byte) error

LockKeys tries to lock the entries with the keys in KV store.

func (*Transaction) Object

func (txn *Transaction) Object(key []byte) (*Object, error)

Object new object thougth key

func (*Transaction) Rollback

func (txn *Transaction) Rollback() error

Rollback a transaction

func (*Transaction) Set

func (txn *Transaction) Set(key []byte) (*Set, error)

Set returns a set object

func (*Transaction) String

func (txn *Transaction) String(key []byte) (*String, error)

String returns a string object, but the object is unsafe, maybe the object is expire,or not exist

func (*Transaction) Strings

func (txn *Transaction) Strings(keys [][]byte) ([]*String, error)

Strings returns a slice of String

type ZList

type ZList struct {
	Object
	// contains filtered or unexported fields
}

ZList ZListMeta defined zip list, with only objectMeta info.

func GetZList

func GetZList(txn *Transaction, metaKey []byte, obj *Object, val []byte) (*ZList, error)

GetZList generate List objectm with auto reation, if zip is true, zipped list will be choose

func (*ZList) Destory

func (l *ZList) Destory() error

Destory the zlist

func (*ZList) Exist

func (l *ZList) Exist() bool

Exist if zlist is effective return true ,otherwise return false

func (*ZList) Index

func (l *ZList) Index(n int64) (data []byte, err error)

Index return the value at index

func (*ZList) Insert

func (l *ZList) Insert(pivot, v []byte, before bool) error

Insert v before/after pivot in zlist

func (*ZList) LPop

func (l *ZList) LPop() (data []byte, err error)

LPop return and delete the left most element

func (*ZList) LPush

func (l *ZList) LPush(data ...[]byte) (err error)

LPush append new elements to the object values

func (*ZList) LRem

func (l *ZList) LRem(v []byte, n int64) (int, error)

LRem begin delete the count of n key from v

func (*ZList) LTrim

func (l *ZList) LTrim(start int64, stop int64) error

LTrim get keys from start index to stop index

func (*ZList) Length

func (l *ZList) Length() int64

Length return z list length

func (*ZList) Marshal

func (l *ZList) Marshal() ([]byte, error)

Marshal encode zlist into byte slice

func (*ZList) RPop

func (l *ZList) RPop() ([]byte, error)

RPop return and delete the right most element

func (*ZList) RPush

func (l *ZList) RPush(data ...[]byte) (err error)

RPush insert data befroe object values

func (*ZList) Range

func (l *ZList) Range(left, right int64) (value [][]byte, err error)

Range return the elements in [left, right]

func (*ZList) Set

func (l *ZList) Set(n int64, data []byte) error

Set the index object with given value, return ErrIndex on out of range error.

func (*ZList) TransferToLList

func (l *ZList) TransferToLList(dbns []byte, dbid DBID, key []byte) (*LList, error)

TransferToLList create an llist and put values into llist from zlist, LList will inheritance information from ZList

func (*ZList) Unmarshal

func (l *ZList) Unmarshal(obj *Object, b []byte) (err error)

Unmarshal parse meta data into meta field

Directories

Path Synopsis
Package zlistproto is a generated protocol buffer package.
Package zlistproto is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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