Documentation
¶
Index ¶
- Variables
- type DataIndexMode
- type DataType
- type IOType
- type KhighDB
- func (db *KhighDB) Append(key, value []byte) error
- func (db *KhighDB) BackUp(path string) error
- func (db *KhighDB) Close() error
- func (db *KhighDB) Count() int
- func (db *KhighDB) Decr(key []byte) (int64, error)
- func (db *KhighDB) DecrBy(key []byte, delta int64) (int64, error)
- func (db *KhighDB) Delete(key []byte) error
- func (db *KhighDB) Expire(key []byte, duration time.Duration) error
- func (db *KhighDB) Get(key []byte) ([]byte, error)
- func (db *KhighDB) GetDel(key []byte) ([]byte, error)
- func (db *KhighDB) GetRange(key []byte, start, end int) ([]byte, error)
- func (db *KhighDB) GetStrKeys() ([][]byte, error)
- func (db *KhighDB) HDel(key []byte, fields ...[]byte) (int, error)
- func (db *KhighDB) HExists(key, field []byte) (bool, error)
- func (db *KhighDB) HGet(key, field []byte) ([]byte, error)
- func (db *KhighDB) HGetAll(key []byte) ([][]byte, error)
- func (db *KhighDB) HIncrBy(key, field []byte, delta int64) (int64, error)
- func (db *KhighDB) HKeys(key []byte) ([][]byte, error)
- func (db *KhighDB) HLen(key []byte) int
- func (db *KhighDB) HMGet(key []byte, fields ...[]byte) (vals [][]byte, err error)
- func (db *KhighDB) HRandField(key []byte, count int, withValues bool) ([][]byte, error)
- func (db *KhighDB) HScan(key []byte, prefix []byte, pattern string, count int) ([][]byte, error)
- func (db *KhighDB) HSet(key []byte, args ...[]byte) error
- func (db *KhighDB) HSetNX(key, field, value []byte) (bool, error)
- func (db *KhighDB) HStrLen(key, field []byte) int
- func (db *KhighDB) HVals(key []byte) ([][]byte, error)
- func (db *KhighDB) Incr(key []byte) (int64, error)
- func (db *KhighDB) IncrBy(key []byte, delta int64) (int64, error)
- func (db *KhighDB) LIndex(key []byte, index int) ([]byte, error)
- func (db *KhighDB) LLen(key []byte) int
- func (db *KhighDB) LMove(srcKey, dstKey []byte, srcIfLeft, dstIsLeft bool) ([]byte, error)
- func (db *KhighDB) LPop(key []byte) ([]byte, error)
- func (db *KhighDB) LPush(key []byte, values ...[]byte) error
- func (db *KhighDB) LPushX(key []byte, values ...[]byte) error
- func (db *KhighDB) LRange(key []byte, start, end int) (values [][]byte, err error)
- func (db *KhighDB) LRem(key []byte, count int, value []byte) (int, error)
- func (db *KhighDB) LSet(key []byte, index int, value []byte) error
- func (db *KhighDB) MGet(keys [][]byte) ([][]byte, error)
- func (db *KhighDB) MSet(args ...[]byte) error
- func (db *KhighDB) MSetNX(args ...[]byte) error
- func (db *KhighDB) Persist(key []byte) error
- func (db *KhighDB) RPop(key []byte) ([]byte, error)
- func (db *KhighDB) RPush(key []byte, values ...[]byte) error
- func (db *KhighDB) RPushX(key []byte, values ...[]byte) error
- func (db *KhighDB) RunLogFileGC(dataType DataType, fid int, gcRatio float64) error
- func (db *KhighDB) SAdd(key []byte, members ...[]byte) error
- func (db *KhighDB) SCard(key []byte) int
- func (db *KhighDB) SDiff(keys ...[]byte) ([][]byte, error)
- func (db *KhighDB) SDiffStore(keys ...[]byte) (int, error)
- func (db *KhighDB) SInter(keys ...[]byte) ([][]byte, error)
- func (db *KhighDB) SInterStore(keys ...[]byte) (int, error)
- func (db *KhighDB) SIsMember(key, member []byte) bool
- func (db *KhighDB) SMembers(key []byte) ([][]byte, error)
- func (db *KhighDB) SPop(key []byte, count uint) ([][]byte, error)
- func (db *KhighDB) SRem(key []byte, member ...[]byte) error
- func (db *KhighDB) SUnion(keys ...[]byte) ([][]byte, error)
- func (db *KhighDB) SUnionStore(keys ...[]byte) (int, error)
- func (db *KhighDB) Scan(prefix []byte, pattern string, count int) ([][]byte, error)
- func (db *KhighDB) Set(key, value []byte) error
- func (db *KhighDB) SetEX(key, value []byte, duration time.Duration) error
- func (db *KhighDB) SetNX(key, value []byte) error
- func (db *KhighDB) StrLen(key []byte) int
- func (db *KhighDB) Sync() error
- func (db *KhighDB) TTL(key []byte) (int64, error)
- func (db *KhighDB) ZAdd(key []byte, score float64, member []byte) error
- func (db *KhighDB) ZCard(key []byte) int
- func (db *KhighDB) ZRange(key []byte, start, stop int) ([][]byte, error)
- func (db *KhighDB) ZRank(key []byte, member []byte) (ok bool, rank int)
- func (db *KhighDB) ZRem(key, member []byte) error
- func (db *KhighDB) ZRevRange(key []byte, start, stop int) ([][]byte, error)
- func (db *KhighDB) ZRevRank(key []byte, member []byte) (ok bool, rank int)
- func (db *KhighDB) ZScore(key, member []byte) (ok bool, score float64)
- type Options
Constants ¶
This section is empty.
Variables ¶
var ( // ErrKeyNotFound represents KhighDB is already closed. ErrDBClosed = errors.New("KhighDB is already closed") // ErrKeyNotFound represents key is not found. ErrKeyNotFound = errors.New("key not found") // ErrLogFileNotFound represents log file is not found. ErrLogFileNotFound = errors.New("log file not found") // ErrWrongNumberOfArgs represents the number of arguments is invalid. ErrInvalidNumberOfArgs = errors.New("invalid number of arguments") // ErrInvalidValueType represents the type of value is invalid. ErrInvalidValueType = errors.New("value is not an integer") // ErrIntegerOverflow represents the result after increment or decrement overflows int64 limitations. ErrIntegerOverflow = errors.New("increment or decrement overflow") // ErrIndexOutOfRange represents the index is out of range, ErrIndexOutOfRange = errors.New("index is out of range") // ErrLogFileGCRunning represents log file gc is running. ErrLogFileGCRunning = errors.New("log file gc is running, retry later") )
var ErrDiscardNoSpace = errors.New("not enough space can be allocated for the discard file")
ErrDiscardNoSpace represents there is no enough space for discard file.
Functions ¶
This section is empty.
Types ¶
type DataIndexMode ¶
type DataIndexMode int8
DataIndexMode defines the data index mode.
const ( // KeyValueMemMode represents key and value are both in memory, read operation // will be very fast in this mode. // This mode is suitable for scenarios where the values are relatively small. KeyValueMemMode DataIndexMode = iota // KeyOnlyMemMode represents only keep keys in memory, there is a disk seek // while getting a value because values are in log file on disk. KeyOnlyMemMode )
func (DataIndexMode) String ¶
func (mode DataIndexMode) String() string
type KhighDB ¶
type KhighDB struct {
// contains filtered or unexported fields
}
KhighDB defines the structure of KhighDB.
func (*KhighDB) Append ¶
Append appends the value at the end of the old value if the key already exists. This function executes the same operation as Set if ytje key does not exist.
func (*KhighDB) Decr ¶
Decr decrements the value for the specified delta stored at key. If the key does not exist, the value will be set to 0 before performing this operation. It returns ErrInvalidValueType if the value type is not integer type. Also, it returns ErrIntegerOverflow if the value exceeds after decrementing the value.
func (*KhighDB) DecrBy ¶
IncrBy decreases the value stored at key. If the key does not exist, the value will be set to 0 before performing this operation. It returns ErrInvalidValueType if the value type is not integer type. Also, it returns ErrIntegerOverflow if the value exceeds after decreasing the value.
func (*KhighDB) Expire ¶
Expire sets the expiration time for the given key. Note that the smallest granularity supported is time.Millisecond.
func (*KhighDB) Get ¶
Get gets the value of the key. If the key does not exist, ErrKeyNotFound is returned. Note the parameter can be nil.
func (*KhighDB) GetRange ¶
GetRange returns the substring of the string value stored at key, determined by the offset start and end.
func (*KhighDB) GetStrKeys ¶
GetStrKeys returns all the stored keys of type String.
func (*KhighDB) HDel ¶
HDel removes the specified fields from the hash stored at key and returns the number of the fields removed successfully. .
func (*KhighDB) HGetAll ¶
HGetAll returns all fields and values in the hash stored at key. The returned data likes ['field', 'value', 'field', 'value'...].
func (*KhighDB) HIncrBy ¶
HIncrBy increases the number stored at field in the hash stored at key by delta. If the key does not exist, a new key holding a hash is created, If the filed does not exist, the value is set to 0 before performing this operation.
func (*KhighDB) HMGet ¶
HMGet returns the values associated with the specified fields in the hash stored at key. For every field that does not exist in the hash, nil is returned. If the key does not exist, a list of nil values is returned.
func (*KhighDB) HRandField ¶
HRandField returns the fields from the hash value stored at key. The count argument determines the returned data in following ways:
- count = 0: Return nil.
- count > 0: Return an array of distinct fields.
- count < 0: The returned fields is allowed to return the same field multiple times.
func (*KhighDB) HScan ¶
HScan iterates over a specified key of type Hash and finds its fields and values. Parameter prefix will match field's prefix, and pattern is a regular expression that also matches the field. Parameter count limits the number of keys, a nil slice will be returned if count is not a positive number, The returned data likes ['field', 'value', 'field', 'value'...].
func (*KhighDB) HSet ¶
HSet sets filed in the hash stored at key to value. If the key does not exist, a new key holding a hash is created. If the filed already exists in the hash, it is overwritten. If you want to set multiple filed-value pair, parameter args be like ['filed', 'value', 'field', 'value'...].
func (*KhighDB) HSetNX ¶
HSetNX sets the given value obly if the field does not exist. If the key does not exist, a new hash is created. If the field already exists, HSetNX does not have side effect.
func (*KhighDB) HStrLen ¶
HStrLen returns the string length associated with field in the hash stored at key, If the key or the field do not exist, 0 is returned.
func (*KhighDB) Incr ¶
Incr increments the value stored at key. If the key does not exist, the value will be set to 0 before performing this operation. It returns ErrInvalidValueType if the value type is not integer type. Also, it returns ErrIntegerOverflow if the value exceeds after incrementing the value.
func (*KhighDB) IncrBy ¶
IncrBy increases the value for the specified delta stored at key. If the key does not exist, the value will be set to 0 before performing this operation. It returns ErrInvalidValueType if the value type is not integer type. Also, it returns ErrIntegerOverflow if the value exceeds after incrementing the value.
func (*KhighDB) LIndex ¶
LIndex returns the element at index in the list stored at key. If the index is 0, it returns the first element. If the index a positive number, it returns the (index+1)-th element. If the index is a negative number, it returns the index-th element from the tail. Also, if the index is out of range, it returns nil.
func (*KhighDB) LLen ¶
LLen returns the length of the list stored at key, If the key does not exist, it returns 0.
func (*KhighDB) LMove ¶
LMove atomically removes the first/last element of the list sored at source, pushes the element `at the head/tail element of the list stored at destination and return the element's value.
func (*KhighDB) LPush ¶
LPush inserts 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 operation.
func (*KhighDB) LPushX ¶
LPushX inserts a specified values at the head of the list stored at key, only if key already exists and holds a list. In contrary to LPush, no operation will be performed and ErrKeyNotFound will be returned if the key does not exist.
func (*KhighDB) LRange ¶
LRange returns the specified elements of the list stored at key. The offsets start and stop are zero-based offsets, 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, If start is larger than the end of the list, an empty list is returned.
func (*KhighDB) LRem ¶
LRem removed the first count occurrences of elements equal to element from the list stored at key and returns the count of removed elements.
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.
Note that this method will rewrite the values, so it maybe very slow.
func (*KhighDB) LSet ¶
LSets set the list element at index to the specified value. If key does not exists, ErrKeyNotFound is returned.
func (*KhighDB) MGet ¶
MGet gets the values of all specified keys. If just a single key does not exist, ErrKeyNotFound is returned.
func (*KhighDB) MSet ¶
MSet sets key-value pairs in batches. Parameter should be like [key, value, key, value...]
func (*KhighDB) MSetNX ¶
MSetNX executes SetNX in batches. If just a single key already exist, all the SetNX operations will not be performed and nil will returned.
func (*KhighDB) RPush ¶
RPush inserts 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 operation.
func (*KhighDB) RPushX ¶
RPushX inserts a specified values at the head of the list stored at key, only if key already exists and holds a list. In contrary to LPush, no operation will be performed and ErrKeyNotFound will be returned if the key does not exist.
func (*KhighDB) RunLogFileGC ¶
RunLogFileGC executes log file garbage collection manually.
func (*KhighDB) SAdd ¶
SAdd adds the specified members to the members to the set stored at key. Specified members which are already a member of this set are ignored. If the key does not exist, a new set is created before adding the specified members.
func (*KhighDB) SDiff ¶
SDiff returns the members of the set difference between the first set and all the successive sets.
func (*KhighDB) SDiffStore ¶
SDiffStore is equal to SDiff, the result is stored in first param instead of being returned. It returns the cardinality of the result normally. Also, it returns -1 if any error occurs.
func (*KhighDB) SInter ¶
SInter returns the members of the set resulting from the inter if all the given sets.
func (*KhighDB) SInterStore ¶
SInterStore is equal to SInter, the result is stored in first param instead of being returned. It returns the cardinality of the result normally. Also, it returns -1 if any error occurs.
func (*KhighDB) SIsMember ¶
SIsMember checks if the given member is a member of the set stored at key.
func (*KhighDB) SPop ¶
SPop removes and returns specified number of members from the set stored at key.
func (*KhighDB) SUnion ¶
SUnion returns the members of the set resulting from the union of all the given sets.
func (*KhighDB) SUnionStore ¶
SDiffStore is equal to SUnion, the result is stored in first param instead of being returned. It returns the cardinality of the result normally. Also, it returns -1 if any error occurs.
func (*KhighDB) Scan ¶
Scan iterates over all keys of type String and finds its value. Parameter prefix will matches key's prefix, and pattern is a regular expression that also matches the key. Parameter count limits the number of keys, a empty slice will will be returned is count is not a positive number. The returned values will be a mixed data of keys and values, like [key, value, key, value ...].
func (*KhighDB) Set ¶
Set sets key to hold the string value. If key already holds a value, it will be overwritten. Any previous time to live associated with the key is discarded o successful set operation. Note the parameter can be nil and value can not be nil.
func (*KhighDB) SetEX ¶
SetEX sets key to hold the string value with expiration time. Note that the smallest granularity supported is time.Millisecond.
func (*KhighDB) SetNX ¶
SetNX sets key to hold the string value if the key is not exist. If the key already exists, nil is return,
func (*KhighDB) StrLen ¶
StrLen returns the length of string value stored at key. If the keys does not exist, o is return.
func (*KhighDB) TTL ¶
TTL gets time to live in milliseconds for the given key. If the key does not exist, ErrKeyNotFound is returned.
func (*KhighDB) ZAdd ¶
ZAdd adds the given member with the given score with the specified score to the sorted set stored at key.
func (*KhighDB) ZCard ¶
ZCard returns the sorted set cardinality (number of elements) of the sorted set stored at key.
func (*KhighDB) ZRange ¶
ZRange returns the specified range of elements in the sorted set stored at key.
func (*KhighDB) 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 (*KhighDB) ZRem ¶
ZRem removes the specified members from the sorted set stored at key. Non existing members are ignored.
func (*KhighDB) ZRevRange ¶
ZRange 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.
type Options ¶
type Options struct { // DBPath is the path of db, which will be created automatically if not exist. DBPath string // LogLevel is the KhighDB log output level. // Support: debug, info, warn, error. panic. LogLevel string // IndexMode is the mode of index, support KeyValueMemMode and KeyOnlyMemMode now. // Note that this mode is noly for KV pairs, not for List, Hash, Set and ZSet. // Default value is KeyOnlyMemMode. IndexMode DataIndexMode // IoType is the type of I/O, support FileIO abd MMap now. // Default value is FileIO. IoType IOType // Sync is whether to synchronize writes from the OS buffer cache through to disk. // If this value is false, some recent writes may be lost when the machine crashes. // Note that if it is just the process crashes but the machine does not then no writes // will be lost. // Default value is false. Sync bool // LogFileGCInternal is the internal for a background goroutine to execute log file // garbage collection periodically. It will pick the log file that meets the condition // for GC, then rewrite the valid data one by one. // Default value is 8 hours. LogFileGCInternal time.Duration // LogFileGCRatio means if discarded data in log file exceeds this ratio, it can be // picked up for compaction. And if there are many files reached the ratio, we will // pick the highest one by one. // The recommended ratio is 0.5, half of the file can be compacted. // Default value is 0.5. LogFileGCRatio float64 // LogFileSizeThreshold is the threshold size of each log file, active log file will // be closed if file reaches the threshold. // Note that this value must be set to the same as the first startup. // Default value is 512MB. LogFileSizeThreshold int64 // DiscardBufferSize is the max size for the channel to send discard log entries. // A channel will be created to send the older entry size when a key is updated or deleted. // Entry size will be saved in the discard file, recoding the invalid size in a log file, // and be used for log file gc. // Default value is 8MB. DiscardBufferSize int }
Options defines the options for opening a KhighDB.
func DefaultOptions ¶
DefaultOptions returns the default options for opening a KhighDB.