khighdb

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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 DataType

type DataType = int8

DataType defines the data structure type.

const (
	String DataType = iota
	List
	Hash
	Set
	ZSet
)

type IOType

type IOType int8

IOType defines the I/O type.

const (
	// FileIO represents using standard file I/O.
	FileIO IOType = iota

	// MMap represents using memory-mapped buffer.
	MMap
)

func (IOType) String

func (t IOType) String() string

type KhighDB

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

KhighDB defines the structure of KhighDB.

func Open

func Open(options Options) (*KhighDB, error)

Open opens a KhighDB instance.

func (*KhighDB) Append

func (db *KhighDB) Append(key, value []byte) error

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) BackUp

func (db *KhighDB) BackUp(path string) error

Backup copies the db directory to the given path for backup.

func (*KhighDB) Close

func (db *KhighDB) Close() error

Close closes the KhighDB instance and saves relative configs.

func (*KhighDB) Count

func (db *KhighDB) Count() int

Count returns the total number of keys of String.

func (*KhighDB) Decr

func (db *KhighDB) Decr(key []byte) (int64, error)

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

func (db *KhighDB) DecrBy(key []byte, delta int64) (int64, error)

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) Delete

func (db *KhighDB) Delete(key []byte) error

Delete deletes key-value pair corresponding to the given key.

func (*KhighDB) Expire

func (db *KhighDB) Expire(key []byte, duration time.Duration) error

Expire sets the expiration time for the given key. Note that the smallest granularity supported is time.Millisecond.

func (*KhighDB) Get

func (db *KhighDB) Get(key []byte) ([]byte, error)

Get gets the value of the key. If the key does not exist, ErrKeyNotFound is returned. Note the parameter can be nil.

func (*KhighDB) GetDel

func (db *KhighDB) GetDel(key []byte) ([]byte, error)

GetDel gets the value of the key and deletes the key.

func (*KhighDB) GetRange

func (db *KhighDB) GetRange(key []byte, start, end int) ([]byte, error)

GetRange returns the substring of the string value stored at key, determined by the offset start and end.

func (*KhighDB) GetStrKeys

func (db *KhighDB) GetStrKeys() ([][]byte, error)

GetStrKeys returns all the stored keys of type String.

func (*KhighDB) HDel

func (db *KhighDB) HDel(key []byte, fields ...[]byte) (int, error)

HDel removes the specified fields from the hash stored at key and returns the number of the fields removed successfully. .

func (*KhighDB) HExists

func (db *KhighDB) HExists(key, field []byte) (bool, error)

HExists returns whether the field exists in the hash stored at key.

func (*KhighDB) HGet

func (db *KhighDB) HGet(key, field []byte) ([]byte, error)

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

func (*KhighDB) HGetAll

func (db *KhighDB) HGetAll(key []byte) ([][]byte, error)

HGetAll returns all fields and values in the hash stored at key. The returned data likes ['field', 'value', 'field', 'value'...].

func (*KhighDB) HIncrBy

func (db *KhighDB) HIncrBy(key, field []byte, delta int64) (int64, error)

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) HKeys

func (db *KhighDB) HKeys(key []byte) ([][]byte, error)

HKeys return all field names in the hash stored at key,

func (*KhighDB) HLen

func (db *KhighDB) HLen(key []byte) int

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

func (*KhighDB) HMGet

func (db *KhighDB) HMGet(key []byte, fields ...[]byte) (vals [][]byte, err error)

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

func (db *KhighDB) HRandField(key []byte, count int, withValues bool) ([][]byte, error)

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

func (db *KhighDB) HScan(key []byte, prefix []byte, pattern string, count int) ([][]byte, error)

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

func (db *KhighDB) HSet(key []byte, args ...[]byte) error

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

func (db *KhighDB) HSetNX(key, field, value []byte) (bool, error)

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

func (db *KhighDB) HStrLen(key, field []byte) int

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) HVals

func (db *KhighDB) HVals(key []byte) ([][]byte, error)

HKeys return all field values in the hash stored at key,

func (*KhighDB) Incr

func (db *KhighDB) Incr(key []byte) (int64, error)

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

func (db *KhighDB) IncrBy(key []byte, delta int64) (int64, error)

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

func (db *KhighDB) LIndex(key []byte, index int) ([]byte, error)

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

func (db *KhighDB) LLen(key []byte) int

LLen returns the length of the list stored at key, If the key does not exist, it returns 0.

func (*KhighDB) LMove

func (db *KhighDB) LMove(srcKey, dstKey []byte, srcIfLeft, dstIsLeft bool) ([]byte, error)

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) LPop

func (db *KhighDB) LPop(key []byte) ([]byte, error)

LPop removes and returns the first element of the list stored at key,

func (*KhighDB) LPush

func (db *KhighDB) LPush(key []byte, values ...[]byte) error

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

func (db *KhighDB) LPushX(key []byte, values ...[]byte) error

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

func (db *KhighDB) LRange(key []byte, start, end int) (values [][]byte, err error)

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

func (db *KhighDB) LRem(key []byte, count int, value []byte) (int, error)

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

func (db *KhighDB) LSet(key []byte, index int, value []byte) error

LSets set the list element at index to the specified value. If key does not exists, ErrKeyNotFound is returned.

func (*KhighDB) MGet

func (db *KhighDB) MGet(keys [][]byte) ([][]byte, error)

MGet gets the values of all specified keys. If just a single key does not exist, ErrKeyNotFound is returned.

func (*KhighDB) MSet

func (db *KhighDB) MSet(args ...[]byte) error

MSet sets key-value pairs in batches. Parameter should be like [key, value, key, value...]

func (*KhighDB) MSetNX

func (db *KhighDB) MSetNX(args ...[]byte) error

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) Persist

func (db *KhighDB) Persist(key []byte) error

Persist removes the expiration time for the given key.

func (*KhighDB) RPop

func (db *KhighDB) RPop(key []byte) ([]byte, error)

LPop removes and returns the last element of the list stored at key,

func (*KhighDB) RPush

func (db *KhighDB) RPush(key []byte, values ...[]byte) error

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

func (db *KhighDB) RPushX(key []byte, values ...[]byte) error

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

func (db *KhighDB) RunLogFileGC(dataType DataType, fid int, gcRatio float64) error

RunLogFileGC executes log file garbage collection manually.

func (*KhighDB) SAdd

func (db *KhighDB) SAdd(key []byte, members ...[]byte) error

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) SCard

func (db *KhighDB) SCard(key []byte) int

SCard returns the set cardinality (number of elements) stored at key.

func (*KhighDB) SDiff

func (db *KhighDB) SDiff(keys ...[]byte) ([][]byte, error)

SDiff returns the members of the set difference between the first set and all the successive sets.

func (*KhighDB) SDiffStore

func (db *KhighDB) SDiffStore(keys ...[]byte) (int, error)

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

func (db *KhighDB) SInter(keys ...[]byte) ([][]byte, error)

SInter returns the members of the set resulting from the inter if all the given sets.

func (*KhighDB) SInterStore

func (db *KhighDB) SInterStore(keys ...[]byte) (int, error)

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

func (db *KhighDB) SIsMember(key, member []byte) bool

SIsMember checks if the given member is a member of the set stored at key.

func (*KhighDB) SMembers

func (db *KhighDB) SMembers(key []byte) ([][]byte, error)

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

func (*KhighDB) SPop

func (db *KhighDB) SPop(key []byte, count uint) ([][]byte, error)

SPop removes and returns specified number of members from the set stored at key.

func (*KhighDB) SRem

func (db *KhighDB) SRem(key []byte, member ...[]byte) error

SRem removes the specified members from the set stored at key.

func (*KhighDB) SUnion

func (db *KhighDB) SUnion(keys ...[]byte) ([][]byte, error)

SUnion returns the members of the set resulting from the union of all the given sets.

func (*KhighDB) SUnionStore

func (db *KhighDB) SUnionStore(keys ...[]byte) (int, error)

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

func (db *KhighDB) Scan(prefix []byte, pattern string, count int) ([][]byte, error)

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

func (db *KhighDB) Set(key, value []byte) error

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

func (db *KhighDB) SetEX(key, value []byte, duration time.Duration) error

SetEX sets key to hold the string value with expiration time. Note that the smallest granularity supported is time.Millisecond.

func (*KhighDB) SetNX

func (db *KhighDB) SetNX(key, value []byte) error

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

func (db *KhighDB) StrLen(key []byte) int

StrLen returns the length of string value stored at key. If the keys does not exist, o is return.

func (*KhighDB) Sync

func (db *KhighDB) Sync() error

Sync synchronizes the db files to disk.

func (*KhighDB) TTL

func (db *KhighDB) TTL(key []byte) (int64, error)

TTL gets time to live in milliseconds for the given key. If the key does not exist, ErrKeyNotFound is returned.

func (*KhighDB) ZAdd

func (db *KhighDB) ZAdd(key []byte, score float64, member []byte) error

ZAdd adds the given member with the given score with the specified score to the sorted set stored at key.

func (*KhighDB) ZCard

func (db *KhighDB) ZCard(key []byte) int

ZCard returns the sorted set cardinality (number of elements) of the sorted set stored at key.

func (*KhighDB) ZRange

func (db *KhighDB) ZRange(key []byte, start, stop int) ([][]byte, error)

ZRange returns the specified range of elements in the sorted set stored at key.

func (*KhighDB) ZRank

func (db *KhighDB) ZRank(key []byte, member []byte) (ok bool, rank int)

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

func (db *KhighDB) ZRem(key, member []byte) error

ZRem removes the specified members from the sorted set stored at key. Non existing members are ignored.

func (*KhighDB) ZRevRange

func (db *KhighDB) ZRevRange(key []byte, start, stop int) ([][]byte, error)

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.

func (*KhighDB) ZRevRank

func (db *KhighDB) ZRevRank(key []byte, member []byte) (ok bool, rank int)

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 (*KhighDB) ZScore

func (db *KhighDB) ZScore(key, member []byte) (ok bool, score float64)

ZScore returns the score of member in the stored set at key.

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

func DefaultOptions(path string) Options

DefaultOptions returns the default options for opening a KhighDB.

func (Options) String

func (o Options) String() string

Jump to

Keyboard shortcuts

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