Documentation
¶
Index ¶
- Constants
- Variables
- type ArchivedFiles
- type Config
- type DataIndexMode
- type DataType
- type Expires
- type HashIdx
- type ListIdx
- type LockMgr
- type RoseDB
- func (db *RoseDB) Append(key interface{}, value string) (err error)
- func (db *RoseDB) Backup(dir string) (err error)
- func (db *RoseDB) Close() (err error)
- func (db *RoseDB) Expire(key interface{}, duration int64) (err error)
- func (db *RoseDB) Get(key, dest interface{}) error
- func (db *RoseDB) GetSet(key, value, dest interface{}) (err error)
- func (db *RoseDB) HClear(key []byte) (err error)
- func (db *RoseDB) HDel(key []byte, field ...[]byte) (res int, err error)
- func (db *RoseDB) HExists(key, field []byte) (ok bool)
- func (db *RoseDB) HExpire(key []byte, duration int64) (err error)
- func (db *RoseDB) HGet(key, field []byte) []byte
- func (db *RoseDB) HGetAll(key []byte) [][]byte
- func (db *RoseDB) HKeyExists(key []byte) (ok bool)
- func (db *RoseDB) HKeys(key []byte) (val []string)
- func (db *RoseDB) HLen(key []byte) int
- func (db *RoseDB) HMGet(key []byte, fields ...[]byte) [][]byte
- func (db *RoseDB) HMSet(key []byte, values ...[]byte) error
- func (db *RoseDB) HSet(key []byte, field []byte, value []byte) (res int, err error)
- func (db *RoseDB) HSetNx(key, field, value []byte) (res int, err error)
- func (db *RoseDB) HTTL(key []byte) (ttl int64)
- func (db *RoseDB) HVals(key []byte) (val [][]byte)
- func (db *RoseDB) LClear(key interface{}) (err error)
- func (db *RoseDB) LExpire(key interface{}, duration int64) (err error)
- func (db *RoseDB) LIndex(key interface{}, idx int) []byte
- func (db *RoseDB) LInsert(key string, option list.InsertOption, pivot, val interface{}) (count int, err error)
- func (db *RoseDB) LKeyExists(key interface{}) (ok bool)
- func (db *RoseDB) LLen(key interface{}) int
- func (db *RoseDB) LPop(key interface{}) ([]byte, error)
- func (db *RoseDB) LPush(key interface{}, values ...interface{}) (res int, err error)
- func (db *RoseDB) LRange(key interface{}, start, end int) ([][]byte, error)
- func (db *RoseDB) LRem(key, value interface{}, count int) (int, error)
- func (db *RoseDB) LSet(key interface{}, idx int, val interface{}) (ok bool, err error)
- func (db *RoseDB) LTTL(key interface{}) (ttl int64)
- func (db *RoseDB) LTrim(key interface{}, start, end int) error
- func (db *RoseDB) LValExists(key interface{}, val interface{}) (ok bool)
- func (db *RoseDB) MGet(keys ...interface{}) ([][]byte, error)
- func (db *RoseDB) MSet(values ...interface{}) error
- func (db *RoseDB) MarkCommit(txId uint64) (err error)
- func (db *RoseDB) NewTransaction() *Txn
- func (db *RoseDB) Persist(key interface{}) (err error)
- func (db *RoseDB) PrefixScan(prefix string, limit, offset int) (val []interface{}, err error)
- func (db *RoseDB) RPop(key interface{}) ([]byte, error)
- func (db *RoseDB) RPush(key interface{}, values ...interface{}) (res int, err error)
- func (db *RoseDB) RangeScan(start, end interface{}) (val []interface{}, err error)
- func (db *RoseDB) Remove(key interface{}) error
- func (db *RoseDB) SAdd(key []byte, members ...[]byte) (res int, err error)
- func (db *RoseDB) SCard(key []byte) int
- func (db *RoseDB) SClear(key []byte) (err error)
- func (db *RoseDB) SDiff(keys ...[]byte) (val [][]byte)
- func (db *RoseDB) SExpire(key []byte, duration int64) (err error)
- func (db *RoseDB) SIsMember(key, member []byte) bool
- func (db *RoseDB) SKeyExists(key []byte) (ok bool)
- func (db *RoseDB) SMembers(key []byte) (val [][]byte)
- func (db *RoseDB) SMove(src, dst, member []byte) error
- func (db *RoseDB) SPop(key []byte, count int) (values [][]byte, err error)
- func (db *RoseDB) SRandMember(key []byte, count int) [][]byte
- func (db *RoseDB) SRem(key []byte, members ...[]byte) (res int, err error)
- func (db *RoseDB) STTL(key []byte) (ttl int64)
- func (db *RoseDB) SUnion(keys ...[]byte) (val [][]byte)
- func (db *RoseDB) Set(key, value interface{}) error
- func (db *RoseDB) SetEx(key, value interface{}, duration int64) (err error)
- func (db *RoseDB) SetNx(key, value interface{}) (ok bool, err error)
- func (db *RoseDB) SingleMerge(fileId uint32) (err error)
- func (db *RoseDB) StartMerge() (err error)
- func (db *RoseDB) StopMerge()
- func (db *RoseDB) StrExists(key interface{}) bool
- func (db *RoseDB) Sync() (err error)
- func (db *RoseDB) TTL(key interface{}) (ttl int64)
- func (db *RoseDB) Txn(fn func(tx *Txn) error) (err error)
- func (db *RoseDB) TxnView(fn func(tx *Txn) error) (err error)
- func (db *RoseDB) ZAdd(key interface{}, score float64, member interface{}) error
- func (db *RoseDB) ZCard(key interface{}) int
- func (db *RoseDB) ZClear(key interface{}) (err error)
- func (db *RoseDB) ZExpire(key interface{}, duration int64) (err error)
- func (db *RoseDB) ZGetByRank(key interface{}, rank int) []interface{}
- func (db *RoseDB) ZIncrBy(key interface{}, increment float64, member interface{}) (float64, error)
- func (db *RoseDB) ZKeyExists(key interface{}) (ok bool)
- func (db *RoseDB) ZRange(key interface{}, start, stop int) []interface{}
- func (db *RoseDB) ZRangeWithScores(key interface{}, start, stop int) []interface{}
- func (db *RoseDB) ZRank(key, member interface{}) int64
- func (db *RoseDB) ZRem(key, member interface{}) (ok bool, err error)
- func (db *RoseDB) ZRevGetByRank(key interface{}, rank int) []interface{}
- func (db *RoseDB) ZRevRange(key interface{}, start, stop int) []interface{}
- func (db *RoseDB) ZRevRangeWithScores(key interface{}, start, stop int) []interface{}
- func (db *RoseDB) ZRevRank(key, member interface{}) int64
- func (db *RoseDB) ZRevScoreRange(key interface{}, max, min float64) []interface{}
- func (db *RoseDB) ZScore(key, member interface{}) (ok bool, score float64)
- func (db *RoseDB) ZScoreRange(key interface{}, min, max float64) []interface{}
- func (db *RoseDB) ZTTL(key interface{}) (ttl int64)
- type SetIdx
- type StrIdx
- type Txn
- func (tx *Txn) Append(key interface{}, value string) (err error)
- func (tx *Txn) Commit() (err error)
- func (tx *Txn) Get(key, dest interface{}) (err error)
- func (tx *Txn) GetSet(key, value, dest interface{}) (err error)
- func (tx *Txn) HDel(key interface{}, fields ...interface{}) (err error)
- func (tx *Txn) HExists(key, field interface{}) (ok bool)
- func (tx *Txn) HGet(key, field, dest interface{}) (err error)
- func (tx *Txn) HSet(key, field, value interface{}) (err error)
- func (tx *Txn) HSetNx(key, field, value interface{}) (err error)
- func (tx *Txn) LPush(key interface{}, values ...interface{}) (err error)
- func (tx *Txn) RPush(key interface{}, values ...interface{}) (err error)
- func (tx *Txn) Remove(key interface{}) (err error)
- func (tx *Txn) Rollback()
- func (tx *Txn) SAdd(key interface{}, members ...interface{}) (err error)
- func (tx *Txn) SIsMember(key, member interface{}) (ok bool)
- func (tx *Txn) SRem(key interface{}, members ...interface{}) (err error)
- func (tx *Txn) Set(key, value interface{}) (err error)
- func (tx *Txn) SetEx(key, value interface{}, duration int64) (err error)
- func (tx *Txn) SetNx(key, value interface{}) (ok bool, err error)
- func (tx *Txn) StrExists(key interface{}) (ok bool)
- func (tx *Txn) ZAdd(key interface{}, score float64, member interface{}) (err error)
- func (tx *Txn) ZRem(key, member interface{}) (err error)
- func (tx *Txn) ZScore(key, member interface{}) (exist bool, score float64, err error)
- type TxnFile
- type TxnMeta
- type ZsetIdx
Constants ¶
const ( // DefaultAddr default kvdb server address and port. DefaultAddr = "127.0.0.1:5200" // defaultGrpcAddr server address and port. DefaultGrpcAddr = "127.0.0.1:5300" // DefaultDirPath default kvdb data dir. // Don`t forget to change the path when using. DefaultDirPath = "/tmp/kvdb" // DefaultBlockSize default db file size: 16mb. // If reach the size, db file will never be opened for writing. DefaultBlockSize = 16 * 1024 * 1024 // DefaultMaxKeySize default max key size: 1mb. DefaultMaxKeySize = uint32(1 * 1024 * 1024) // DefaultMaxValueSize default max value size: 8mb. DefaultMaxValueSize = uint32(8 * 1024 * 1024) // DefaultMergeThreshold default disk files reclaim threshold: 64. // This means that it will be reclaimed when there are at least 64 archived files on disk. DefaultMergeThreshold = 64 // DefaultMergeCheckInterval a timer will be set according to the check interval. // Then merge operation will execute periodically. DefaultMergeCheckInterval = time.Hour * 24 // DefaultCacheCapacity default cache capacity: 0, which means we don`t use it by default. // You can only open the cache in KeyOnlyMemMode, because values are in disk in this mode. DefaultCacheCapacity = 0 )
const ( StringSet uint16 = iota StringRem StringExpire StringPersist )
The operations of a String Type, will be a part of Entry, the same for the other four types.
const ( ListLPush uint16 = iota ListRPush ListLPop ListRPop ListLRem ListLInsert ListLSet ListLTrim ListLClear ListLExpire )
The operations of List.
const ( HashHSet uint16 = iota HashHDel HashHClear HashHExpire )
The operations of Hash.
const ( SetSAdd uint16 = iota SetSRem SetSMove SetSClear SetSExpire )
The operations of Set.
const ( ZSetZAdd uint16 = iota ZSetZRem ZSetZClear ZSetZExpire )
The operations of Sorted Set.
const ( // Separator of the extra info, some commands can`t contains it. ExtraSeparator = "\\0" // DataStructureNum the num of different data structures, there are five now(string, list, hash, set, zset). DataStructureNum = 5 )
Variables ¶
var ( // ErrEmptyKey the key is empty ErrEmptyKey = errors.New("kvdb: the key is empty") // ErrKeyNotExist key not exist ErrKeyNotExist = errors.New("kvdb: key not exist") // ErrKeyTooLarge the key too large ErrKeyTooLarge = errors.New("kvdb: key exceeded the max length") // ErrValueTooLarge the value too large ErrValueTooLarge = errors.New("kvdb: value exceeded the max length") // ErrNilIndexer the indexer is nil ErrNilIndexer = errors.New("kvdb: indexer is nil") // ErrMergeUnreached not ready to reclaim ErrMergeUnreached = errors.New("kvdb: unused space not reach the threshold") // ErrExtraContainsSeparator extra contains separator ErrExtraContainsSeparator = errors.New("kvdb: extra contains separator \\0") // ErrInvalidTTL ttl is invalid ErrInvalidTTL = errors.New("kvdb: invalid ttl") // ErrKeyExpired the key is expired ErrKeyExpired = errors.New("kvdb: key is expired") // ErrDBisMerging merge and single merge can`t execute at the same time. ErrDBisMerging = errors.New("kvdb: can`t do reclaim and single reclaim at the same time") // ErrDBIsClosed db can`t be used after closed. ErrDBIsClosed = errors.New("kvdb: db is closed, reopen it") // ErrTxIsFinished tx is finished. ErrTxIsFinished = errors.New("kvdb: transaction is finished, create a new one") // ErrActiveFileIsNil active file is nil. ErrActiveFileIsNil = errors.New("kvdb: active file is nil") // ErrWrongNumberOfArgs wrong number of arguments ErrWrongNumberOfArgs = errors.New("kvdb: wrong number of arguments") )
Functions ¶
This section is empty.
Types ¶
type ArchivedFiles ¶
ArchivedFiles define the archived files, which mean these files can only be read. and will never be opened for writing.
type Config ¶
type Config struct { Addr string `json:"addr" toml:"addr"` // server address GrpcAddr string `json:"grpc_addr" toml:"grpc_addr"` // grpc server address DirPath string `json:"dir_path" toml:"dir_path"` // kvdb dir path of db file // Deprecated: don`t edit the option, it will be removed in future release. BlockSize int64 `json:"block_size" toml:"block_size"` // each db file size RwMethod storage.FileRWMethod `json:"rw_method" toml:"rw_method"` // db file read and write method IdxMode DataIndexMode `json:"idx_mode" toml:"idx_mode"` // data index mode MaxKeySize uint32 `json:"max_key_size" toml:"max_key_size"` MaxValueSize uint32 `json:"max_value_size" toml:"max_value_size"` // Sync is whether to sync writes from the OS buffer cache through to actual disk. // If false, and the machine crashes, then some recent writes may be lost. // // Note that if it is just the process that crashes (and the machine does not) then no writes will be lost. // // The default value is false. Sync bool `json:"sync" toml:"sync"` MergeThreshold int `json:"merge_threshold" toml:"merge_threshold"` // threshold to reclaim disk MergeCheckInterval time.Duration `json:"merge_check_interval"` CacheCapacity int `json:"cache_capacity" toml:"cache_capacity"` }
Config the opening options of kvdb.
type DataIndexMode ¶
type DataIndexMode int
DataIndexMode the data index mode.
const ( // KeyValueMemMode key and value are both in memory, read operation will be very fast in this mode. // Because there is no disk seek, just get value from the corresponding data structures in memory. // This mode is suitable for scenarios where the value are relatively small. KeyValueMemMode DataIndexMode = iota // KeyOnlyMemMode only key in memory, there is a disk seek while getting a value. // Because values are in db file on disk. KeyOnlyMemMode )
type HashIdx ¶
type HashIdx struct {
// contains filtered or unexported fields
}
HashIdx hash index.
type ListIdx ¶
type ListIdx struct {
// contains filtered or unexported fields
}
ListIdx the list index.
type LockMgr ¶
type LockMgr struct {
// contains filtered or unexported fields
}
LockMgr is a lock manager that manages read and write operations of different data structures. It is also be used to manage transaction.
type RoseDB ¶
type RoseDB struct {
// contains filtered or unexported fields
}
RoseDB the kvdb struct, represents a db instance.
func (*RoseDB) Append ¶
Append if key already exists and is a string, this command appends the value at the end of the string. If key does not exist it is created and set as an empty string, so Append will be similar to Set in this special case.
func (*RoseDB) GetSet ¶
GetSet set key to value and returns the old value stored at key. If the key not exist, return an err.
func (*RoseDB) HDel ¶
HDel removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns false.
func (*RoseDB) HGetAll ¶
HGetAll returns all fields and values of the hash stored at key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.
func (*RoseDB) HKeyExists ¶
HKeyExists returns if the key is existed in hash.
func (*RoseDB) HSet ¶
HSet sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten. Return num of elements in hash of the specified key.
func (*RoseDB) HSetNx ¶
HSetNx Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect. Return if the operation is successful.
func (*RoseDB) LIndex ¶
LIndex returns the element at index index in the list stored at key. The index is zero-based, so 0 means the first element, 1 the second element and so on. Negative indices can be used to designate elements starting at the tail of the list. Here, -1 means the last element, -2 means the penultimate and so forth.
func (*RoseDB) LInsert ¶
func (db *RoseDB) LInsert(key string, option list.InsertOption, pivot, val interface{}) (count int, err error)
LInsert inserts element in the list stored at key either before or after the reference value pivot.
func (*RoseDB) LKeyExists ¶
LKeyExists check if the key of a List exists.
func (*RoseDB) LLen ¶
LLen returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned.
func (*RoseDB) LPush ¶
LPush insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations.
func (*RoseDB) LRange ¶
LRange returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on. These offsets can also be negative numbers indicating offsets starting at the end of the list. For example, -1 is the last element of the list, -2 the penultimate, and so on.
func (*RoseDB) LRem ¶
LRem removes the first count occurrences of elements equal to element from the list stored at key. The count argument influences the operation in the following ways: count > 0: Remove elements equal to element moving from head to tail. count < 0: Remove elements equal to element moving from tail to head. count = 0: Remove all elements equal to element.
func (*RoseDB) LSet ¶
LSet sets the list element at index to element. returns whether is successful.
func (*RoseDB) LTrim ¶
LTrim trim an existing list so that it will contain only the specified range of elements specified. Both start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the next element and so on.
func (*RoseDB) LValExists ¶
LValExists check if the val exists in a specified List stored at key.
func (*RoseDB) MarkCommit ¶
MarkCommit write the tx id into txn file.
func (*RoseDB) NewTransaction ¶
NewTransaction create a new transaction, don`t support concurrent execution of transactions now. So you can only open a read-write transaction at the same time. For read-only transactions, you can execute multiple, and any write operations will be omitted.
func (*RoseDB) PrefixScan ¶
PrefixScan find the value corresponding to all matching keys based on the prefix. limit and offset control the range of value. if limit is negative, all matched values will return.
func (*RoseDB) RPush ¶
RPush insert all the specified values at the tail of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.
func (*RoseDB) SAdd ¶
SAdd add the specified members to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.
func (*RoseDB) SCard ¶
SCard returns the set cardinality (number of elements) of the set stored at key.
func (*RoseDB) SDiff ¶
SDiff returns the members of the set resulting from the difference between the first set and all the successive sets.
func (*RoseDB) SKeyExists ¶
SKeyExists returns if the key exists.
func (*RoseDB) SPop ¶
SPop removes and returns one or more random members from the set value store at key.
func (*RoseDB) SRandMember ¶
SRandMember returns a random element from the set value stored at key. count > 0: if count less than set`s card, returns an array containing count different elements. if count greater than set`s card, the entire set will be returned. count < 0: the command is allowed to return the same element multiple times, and in this case, the number of returned elements is the absolute value of the specified count.
func (*RoseDB) SRem ¶
SRem remove the specified members from the set stored at key. Specified members that are not a member of this set are ignored. If key does not exist, it is treated as an empty set and this command returns 0.
func (*RoseDB) SUnion ¶
SUnion returns the members of the set resulting from the union of all the given sets.
func (*RoseDB) Set ¶
Set set key to hold the string value. If key already holds a value, it is overwritten. Any previous time to live associated with the key is discarded on successful Set operation.
func (*RoseDB) SetEx ¶
SetEx set key to hold the string value and set key to timeout after a given number of seconds.
func (*RoseDB) SetNx ¶
SetNx is short for "Set if not exists", set key to hold string value if key does not exist. In that case, it is equal to Set. When key already holds a value, no operation is performed.
func (*RoseDB) SingleMerge ¶
SingleMerge reclaim a single db file`s space according to the param fileId. File id is the non-zero part of a db file`s name prefix, such as 000000000.data.str (fileId is 0), 000000101.data.str (fileId is 101), etc. Only support String type now.
func (*RoseDB) StartMerge ¶
StartMerge reclaim db files`s redundant space in disk. StartMerge operation will read all archived files, iterate all entries and find the valid. Then rewrite the valid entries to new db files. So the time required for reclaim operation depend on the number of entries, you`d better execute it in low peak period.
func (*RoseDB) StopMerge ¶
func (db *RoseDB) StopMerge()
StopMerge send a stop signal to merge process. Then the merge operation will quit.
func (*RoseDB) Txn ¶
Txn execute a transaction including read and write. If no error is returned from the function then the transaction is committed. If an error is returned then the entire transaction is rollback.
func (*RoseDB) ZAdd ¶
ZAdd adds the specified member with the specified score to the sorted set stored at key.
func (*RoseDB) ZCard ¶
ZCard returns the sorted set cardinality (number of elements) of the sorted set stored at key.
func (*RoseDB) ZGetByRank ¶
ZGetByRank get the member at key by rank, the rank is ordered from lowest to highest. The rank of lowest is 0 and so on.
func (*RoseDB) ZIncrBy ¶
ZIncrBy increments the score of member in the sorted set stored at key by increment. If member does not exist in the sorted set, it is added with increment as its score (as if its previous score was 0.0). If key does not exist, a new sorted set with the specified member as its sole member is created.
func (*RoseDB) ZKeyExists ¶
ZKeyExists check if the key exists in zset.
func (*RoseDB) ZRange ¶
ZRange returns the specified range of elements in the sorted set stored at key.
func (*RoseDB) ZRangeWithScores ¶
ZRangeWithScores returns the specified range of elements in the sorted set stored at key.
func (*RoseDB) ZRank ¶
ZRank returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.
func (*RoseDB) ZRem ¶
ZRem removes the specified members from the sorted set stored at key. Non existing members are ignored. An error is returned when key exists and does not hold a sorted set.
func (*RoseDB) ZRevGetByRank ¶
ZRevGetByRank get the member at key by rank, the rank is ordered from highest to lowest. The rank of highest is 0 and so on.
func (*RoseDB) ZRevRange ¶
ZRevRange returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the highest to the lowest score. Descending lexicographical order is used for elements with equal score.
func (*RoseDB) ZRevRangeWithScores ¶
ZRevRangeWithScores returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the highest to the lowest score. Descending lexicographical order is used for elements with equal score.
func (*RoseDB) ZRevRank ¶
ZRevRank returns the rank of member in the sorted set stored at key, with the scores ordered from high to low. The rank (or index) is 0-based, which means that the member with the highest score has rank 0.
func (*RoseDB) ZRevScoreRange ¶
ZRevScoreRange returns all the elements in the sorted set at key with a score between max and min (including elements with score equal to max or min). In contrary to the default ordering of sorted sets, for this command the elements are considered to be ordered from high to low scores.
func (*RoseDB) ZScoreRange ¶
ZScoreRange returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.
type Txn ¶
type Txn struct {
// contains filtered or unexported fields
}
Txn is a kvdb Transaction. You can begin a read-write transaction by calling Txn, and read-only transaction by calling TxnView. Transaction will be committed or rollback automatically in method Txn and TxnView.
type TxnMeta ¶
type TxnMeta struct { // MaxTxId the max tx id now. MaxTxId uint64 // ActiveTxIds committed tx ids in active files. ActiveTxIds *sync.Map // CommittedTxIds save the transaction ids that has been successfully committed. CommittedTxIds map[uint64]struct{} // contains filtered or unexported fields }
TxnMeta represents some transaction info while tx is running.
func LoadTxnMeta ¶
LoadTxnMeta load txn meta info, committed tx id.