Documentation ¶
Overview ¶
Package ledis is a high performance embedded NoSQL.
Ledis supports various data structure like kv, list, hash and zset like redis.
Other features include replication, data with a limited time-to-live.
Usage ¶
First create a ledis instance before use:
l := ledis.Open(cfg)
cfg is a Config instance which contains configuration for ledis use, like DataDir (root directory for ledis working to store data).
After you create a ledis instance, you can select a DB to store you data:
db, _ := l.Select(0)
DB must be selected by a index, ledis supports only 16 databases, so the index range is [0-15].
KV ¶
KV is the most basic ledis type like any other key-value database.
err := db.Set(key, value) value, err := db.Get(key)
List ¶
List is simply lists of values, sorted by insertion order. You can push or pop value on the list head (left) or tail (right).
err := db.LPush(key, value1) err := db.RPush(key, value2) value1, err := db.LPop(key) value2, err := db.RPop(key)
Hash ¶
Hash is a map between fields and values.
n, err := db.HSet(key, field1, value1) n, err := db.HSet(key, field2, value2) value1, err := db.HGet(key, field1) value2, err := db.HGet(key, field2)
ZSet ¶
ZSet is a sorted collections of values. Every member of zset is associated with score, a int64 value which used to sort, from smallest to greatest score. Members are unique, but score may be same.
n, err := db.ZAdd(key, ScorePair{score1, member1}, ScorePair{score2, member2}) ay, err := db.ZRangeByScore(key, minScore, maxScore, 0, -1)
Index ¶
- Constants
- Variables
- func AsyncNotify(ch chan struct{})
- func Int64(v []byte, err error) (int64, error)
- func PutInt64(v int64) []byte
- func StrInt32(v []byte, err error) (int32, error)
- func StrInt64(v []byte, err error) (int64, error)
- func StrInt8(v []byte, err error) (int8, error)
- func StrUint64(v []byte, err error) (uint64, error)
- func Uint64(v []byte, err error) (uint64, error)
- type DB
- func (db *DB) Append(key []byte, value []byte) (int64, error)
- func (db *DB) BLPop(keys [][]byte, timeout time.Duration) ([]interface{}, error)
- func (db *DB) BRPop(keys [][]byte, timeout time.Duration) ([]interface{}, error)
- func (db *DB) BitCount(key []byte, start int, end int) (int64, error)
- func (db *DB) BitOP(op string, destKey []byte, srcKeys ...[]byte) (int64, error)
- func (db *DB) BitPos(key []byte, on int, start int, end int) (int64, error)
- func (db *DB) Decr(key []byte) (int64, error)
- func (db *DB) DecrBy(key []byte, decrement int64) (int64, error)
- func (db *DB) Del(keys ...[]byte) (int64, error)
- func (db *DB) Dump(key []byte) ([]byte, error)
- func (db *DB) Exists(key []byte) (int64, error)
- func (db *DB) Expire(key []byte, duration int64) (int64, error)
- func (db *DB) ExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) FlushAll() (drop int64, err error)
- func (db *DB) Get(key []byte) ([]byte, error)
- func (db *DB) GetBit(key []byte, offset int) (int64, error)
- func (db *DB) GetRange(key []byte, start int, end int) ([]byte, error)
- func (db *DB) GetSet(key []byte, value []byte) ([]byte, error)
- func (db *DB) GetSlice(key []byte) (store.Slice, error)
- func (db *DB) HClear(key []byte) (int64, error)
- func (db *DB) HDel(key []byte, args ...[]byte) (int64, error)
- func (db *DB) HDump(key []byte) ([]byte, error)
- func (db *DB) HExpire(key []byte, duration int64) (int64, error)
- func (db *DB) HExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) HGet(key []byte, field []byte) ([]byte, error)
- func (db *DB) HGetAll(key []byte) ([]FVPair, error)
- func (db *DB) HIncrBy(key []byte, field []byte, delta int64) (int64, error)
- func (db *DB) HKeyExists(key []byte) (int64, error)
- func (db *DB) HKeys(key []byte) ([][]byte, error)
- func (db *DB) HLen(key []byte) (int64, error)
- func (db *DB) HMclear(keys ...[]byte) (int64, error)
- func (db *DB) HMget(key []byte, args ...[]byte) ([][]byte, error)
- func (db *DB) HMset(key []byte, args ...FVPair) error
- func (db *DB) HPersist(key []byte) (int64, error)
- func (db *DB) HRevScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([]FVPair, error)
- func (db *DB) HScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([]FVPair, error)
- func (db *DB) HSet(key []byte, field []byte, value []byte) (int64, error)
- func (db *DB) HTTL(key []byte) (int64, error)
- func (db *DB) HValues(key []byte) ([][]byte, error)
- func (db *DB) Incr(key []byte) (int64, error)
- func (db *DB) IncrBy(key []byte, increment int64) (int64, error)
- func (db *DB) Index() int
- func (db *DB) LClear(key []byte) (int64, error)
- func (db *DB) LDump(key []byte) ([]byte, error)
- func (db *DB) LExpire(key []byte, duration int64) (int64, error)
- func (db *DB) LExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) LIndex(key []byte, index int32) ([]byte, error)
- func (db *DB) LKeyExists(key []byte) (int64, error)
- func (db *DB) LLen(key []byte) (int64, error)
- func (db *DB) LMclear(keys ...[]byte) (int64, error)
- func (db *DB) LPersist(key []byte) (int64, error)
- func (db *DB) LPop(key []byte) ([]byte, error)
- func (db *DB) LPush(key []byte, args ...[]byte) (int64, error)
- func (db *DB) LRange(key []byte, start int32, stop int32) ([][]byte, error)
- func (db *DB) LSet(key []byte, index int32, value []byte) error
- func (db *DB) LTTL(key []byte) (int64, error)
- func (db *DB) LTrim(key []byte, start, stop int64) error
- func (db *DB) LTrimBack(key []byte, trimSize int32) (int32, error)
- func (db *DB) LTrimFront(key []byte, trimSize int32) (int32, error)
- func (db *DB) MGet(keys ...[]byte) ([][]byte, error)
- func (db *DB) MSet(args ...KVPair) error
- func (db *DB) Persist(key []byte) (int64, error)
- func (db *DB) RPop(key []byte) ([]byte, error)
- func (db *DB) RPush(key []byte, args ...[]byte) (int64, error)
- func (db *DB) Restore(key []byte, ttl int64, data []byte) error
- func (db *DB) RevScan(dataType DataType, cursor []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) SAdd(key []byte, args ...[]byte) (int64, error)
- func (db *DB) SCard(key []byte) (int64, error)
- func (db *DB) SClear(key []byte) (int64, error)
- func (db *DB) SDiff(keys ...[]byte) ([][]byte, error)
- func (db *DB) SDiffStore(dstKey []byte, keys ...[]byte) (int64, error)
- func (db *DB) SDump(key []byte) ([]byte, error)
- func (db *DB) SExpire(key []byte, duration int64) (int64, error)
- func (db *DB) SExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) SInter(keys ...[]byte) ([][]byte, error)
- func (db *DB) SInterStore(dstKey []byte, keys ...[]byte) (int64, error)
- func (db *DB) SIsMember(key []byte, member []byte) (int64, error)
- func (db *DB) SKeyExists(key []byte) (int64, error)
- func (db *DB) SMclear(keys ...[]byte) (int64, error)
- func (db *DB) SMembers(key []byte) ([][]byte, error)
- func (db *DB) SPersist(key []byte) (int64, error)
- func (db *DB) SRem(key []byte, args ...[]byte) (int64, error)
- func (db *DB) SRevScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) SScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) STTL(key []byte) (int64, error)
- func (db *DB) SUnion(keys ...[]byte) ([][]byte, error)
- func (db *DB) SUnionStore(dstKey []byte, keys ...[]byte) (int64, error)
- func (db *DB) Scan(dataType DataType, cursor []byte, count int, inclusive bool, match string) ([][]byte, error)
- func (db *DB) Set(key []byte, value []byte) error
- func (db *DB) SetBit(key []byte, offset int, on int) (int64, error)
- func (db *DB) SetEX(key []byte, duration int64, value []byte) error
- func (db *DB) SetNX(key []byte, value []byte) (int64, error)
- func (db *DB) SetRange(key []byte, offset int, value []byte) (int64, error)
- func (db *DB) StrLen(key []byte) (int64, error)
- func (db *DB) TTL(key []byte) (int64, error)
- func (db *DB) XLSort(key []byte, offset int, size int, alpha bool, desc bool, sortBy []byte, ...) ([][]byte, error)
- func (db *DB) XSSort(key []byte, offset int, size int, alpha bool, desc bool, sortBy []byte, ...) ([][]byte, error)
- func (db *DB) XZSort(key []byte, offset int, size int, alpha bool, desc bool, sortBy []byte, ...) ([][]byte, error)
- func (db *DB) ZAdd(key []byte, args ...ScorePair) (int64, error)
- func (db *DB) ZCard(key []byte) (int64, error)
- func (db *DB) ZClear(key []byte) (int64, error)
- func (db *DB) ZCount(key []byte, min int64, max int64) (int64, error)
- func (db *DB) ZDump(key []byte) ([]byte, error)
- func (db *DB) ZExpire(key []byte, duration int64) (int64, error)
- func (db *DB) ZExpireAt(key []byte, when int64) (int64, error)
- func (db *DB) ZIncrBy(key []byte, delta int64, member []byte) (int64, error)
- func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error)
- func (db *DB) ZKeyExists(key []byte) (int64, error)
- func (db *DB) ZLexCount(key []byte, min []byte, max []byte, rangeType uint8) (int64, error)
- func (db *DB) ZMclear(keys ...[]byte) (int64, error)
- func (db *DB) ZPersist(key []byte) (int64, error)
- func (db *DB) ZRange(key []byte, start int, stop int) ([]ScorePair, error)
- func (db *DB) ZRangeByLex(key []byte, min []byte, max []byte, rangeType uint8, offset int, count int) ([][]byte, error)
- func (db *DB) ZRangeByScore(key []byte, min int64, max int64, offset int, count int) ([]ScorePair, error)
- func (db *DB) ZRangeByScoreGeneric(key []byte, min int64, max int64, offset int, count int, reverse bool) ([]ScorePair, error)
- func (db *DB) ZRangeGeneric(key []byte, start int, stop int, reverse bool) ([]ScorePair, error)
- func (db *DB) ZRank(key []byte, member []byte) (int64, error)
- func (db *DB) ZRem(key []byte, members ...[]byte) (int64, error)
- func (db *DB) ZRemRangeByLex(key []byte, min []byte, max []byte, rangeType uint8) (int64, error)
- func (db *DB) ZRemRangeByRank(key []byte, start int, stop int) (int64, error)
- func (db *DB) ZRemRangeByScore(key []byte, min int64, max int64) (int64, error)
- func (db *DB) ZRevRange(key []byte, start int, stop int) ([]ScorePair, error)
- func (db *DB) ZRevRangeByScore(key []byte, min int64, max int64, offset int, count int) ([]ScorePair, error)
- func (db *DB) ZRevRank(key []byte, member []byte) (int64, error)
- func (db *DB) ZRevScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([]ScorePair, error)
- func (db *DB) ZScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([]ScorePair, error)
- func (db *DB) ZScore(key []byte, member []byte) (int64, error)
- func (db *DB) ZTTL(key []byte) (int64, error)
- func (db *DB) ZUnionStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error)
- type DataType
- type DumpHead
- type FVPair
- type KVPair
- type Ledis
- func (l *Ledis) AddNewLogEventHandler(h NewLogEventHandler) error
- func (l *Ledis) Close()
- func (l *Ledis) CompactStore() error
- func (l *Ledis) Dump(w io.Writer) error
- func (l *Ledis) DumpFile(path string) error
- func (l *Ledis) FlushAll() error
- func (l *Ledis) IsReadOnly() bool
- func (l *Ledis) LoadDump(r io.Reader) (*DumpHead, error)
- func (l *Ledis) LoadDumpFile(path string) (*DumpHead, error)
- func (l *Ledis) ReadLogsTo(startLogID uint64, w io.Writer) (n int, nextLogID uint64, err error)
- func (l *Ledis) ReadLogsToTimeout(startLogID uint64, w io.Writer, timeout int, quitCh chan struct{}) (n int, nextLogID uint64, err error)
- func (l *Ledis) ReplicationStat() (*rpl.Stat, error)
- func (l *Ledis) ReplicationUsed() bool
- func (l *Ledis) Select(index int) (*DB, error)
- func (l *Ledis) StoreLogsFromData(data []byte) error
- func (l *Ledis) StoreLogsFromReader(rb io.Reader) error
- func (l *Ledis) StoreStat() *store.Stat
- func (l *Ledis) WaitReplication() error
- type Limit
- type NewLogEventHandler
- type ScorePair
Constants ¶
const ( KVName = "KV" ListName = "LIST" HashName = "HASH" SetName = "SET" ZSetName = "ZSET" )
For different type name
const ( NoneType byte = 0 KVType byte = 1 HashType byte = 2 HSizeType byte = 3 ListType byte = 4 LMetaType byte = 5 ZSetType byte = 6 ZSizeType byte = 7 ZScoreType byte = 8 // BitType byte = 9 // BitMetaType byte = 10 SetType byte = 11 SSizeType byte = 12 /* I make a big mistake about TTL time key format and have to use a new one (change 101 to 103). You must run the ledis-upgrade-ttl to upgrade db. */ ObsoleteExpTimeType byte = 101 ExpMetaType byte = 102 ExpTimeType byte = 103 MetaType byte = 201 )
for backend store
const ( // max allowed databases MaxDatabases int = 10240 // max key size MaxKeySize int = 1024 // max hash field size MaxHashFieldSize int = 1024 // max zset member size MaxZSetMemberSize int = 5 * 1024 // max set member size MaxSetMemberSize int = 1024 // max value size MaxValueSize int = 1024 * 1024 * 1024 )
For different const size configuration
const ( BitAND = "and" BitOR = "or" BitXOR = "xor" BitNot = "not" )
For bit operation
const ( UnionType byte = 51 DiffType byte = 52 InterType byte = 53 )
For set operation type.
const ( MinScore int64 = -1<<63 + 1 MaxScore int64 = 1<<63 - 1 InvalidScore int64 = -1 << 63 AggregateSum byte = 0 AggregateMin byte = 1 AggregateMax byte = 2 )
For zset const.
const Version = "0.5"
Version is for version
Variables ¶
var ( ErrScoreMiss = errors.New("zset score miss") ErrWriteInROnly = errors.New("write not support in readonly mode") ErrRplInRDWR = errors.New("replication not support in read write mode") ErrRplNotSupport = errors.New("replication not support") )
For different common errors
var (
ErrLogMissed = errors.New("log is pured in server")
)
For replication error.
var TypeName = map[byte]string{ KVType: "kv", HashType: "hash", HSizeType: "hsize", ListType: "list", LMetaType: "lmeta", ZSetType: "zset", ZSizeType: "zsize", ZScoreType: "zscore", SetType: "set", SSizeType: "ssize", ExpTimeType: "exptime", ExpMetaType: "expmeta", }
TypeName is the map of type -> name
Functions ¶
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the database.
func (*DB) HKeyExists ¶
HKeyExists checks whether data exists or not.
func (*DB) HRevScan ¶
func (db *DB) HRevScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([]FVPair, error)
HRevScan reversed scans data for hash.
func (*DB) HScan ¶
func (db *DB) HScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([]FVPair, error)
HScan scans data for hash.
func (*DB) LKeyExists ¶
LKeyExists check list existed or not.
func (*DB) LTrimFront ¶
LTrimFront trims the value from top.
func (*DB) RevScan ¶
func (db *DB) RevScan(dataType DataType, cursor []byte, count int, inclusive bool, match string) ([][]byte, error)
RevScan scans the data reversed. if inclusive is true, revscan range (-inf, cursor] else (inf, cursor)
func (*DB) SDiffStore ¶
SDiffStore gets the different of sets and stores to dest set.
func (*DB) SInterStore ¶
SInterStore intersects the sets and stores to dest set.
func (*DB) SKeyExists ¶
SKeyExists checks whether set existed or not.
func (*DB) SRevScan ¶
func (db *DB) SRevScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([][]byte, error)
SRevScan scans data reversed for set.
func (*DB) SScan ¶
func (db *DB) SScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([][]byte, error)
SScan scans data for set.
func (*DB) SUnionStore ¶
SUnionStore unions the sets and stores to the dest set.
func (*DB) Scan ¶
func (db *DB) Scan(dataType DataType, cursor []byte, count int, inclusive bool, match string) ([][]byte, error)
Scan scans the data. If inclusive is true, scan range [cursor, inf) else (cursor, inf)
func (*DB) XLSort ¶
func (db *DB) XLSort(key []byte, offset int, size int, alpha bool, desc bool, sortBy []byte, sortGet [][]byte) ([][]byte, error)
XLSort sorts list.
func (*DB) XSSort ¶
func (db *DB) XSSort(key []byte, offset int, size int, alpha bool, desc bool, sortBy []byte, sortGet [][]byte) ([][]byte, error)
XSSort sorts set.
func (*DB) XZSort ¶
func (db *DB) XZSort(key []byte, offset int, size int, alpha bool, desc bool, sortBy []byte, sortGet [][]byte) ([][]byte, error)
XZSort sorts zset.
func (*DB) ZInterStore ¶
func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error)
ZInterStore intersects the zsets and stores to dest zset.
func (*DB) ZKeyExists ¶
ZKeyExists checks zset existed or not.
func (*DB) ZRangeByLex ¶
func (db *DB) ZRangeByLex(key []byte, min []byte, max []byte, rangeType uint8, offset int, count int) ([][]byte, error)
ZRangeByLex scans the zset lexicographically
func (*DB) ZRangeByScore ¶
func (db *DB) ZRangeByScore(key []byte, min int64, max int64, offset int, count int) ([]ScorePair, error)
ZRangeByScore gets the data with score in min and max. min and max must be inclusive if no limit, set offset = 0 and count = -1
func (*DB) ZRangeByScoreGeneric ¶
func (db *DB) ZRangeByScoreGeneric(key []byte, min int64, max int64, offset int, count int, reverse bool) ([]ScorePair, error)
ZRangeByScoreGeneric is a generic function to scan zset with score. min and max must be inclusive if no limit, set offset = 0 and count = -1
func (*DB) ZRangeGeneric ¶
ZRangeGeneric is a generic function for scan zset.
func (*DB) ZRemRangeByLex ¶
ZRemRangeByLex remvoes members in [min, max] lexicographically
func (*DB) ZRemRangeByRank ¶
ZRemRangeByRank removes the member at range from start to stop.
func (*DB) ZRemRangeByScore ¶
ZRemRangeByScore removes the data with score at [min, max]
func (*DB) ZRevRangeByScore ¶
func (db *DB) ZRevRangeByScore(key []byte, min int64, max int64, offset int, count int) ([]ScorePair, error)
ZRevRangeByScore gets the data with score at [min, max] min and max must be inclusive if no limit, set offset = 0 and count = -1
func (*DB) ZRevScan ¶
func (db *DB) ZRevScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([]ScorePair, error)
ZRevScan scans data reversed for zset.
func (*DB) ZScan ¶
func (db *DB) ZScan(key []byte, cursor []byte, count int, inclusive bool, match string) ([]ScorePair, error)
ZScan scans data for zset.
type Ledis ¶
type Ledis struct {
// contains filtered or unexported fields
}
Ledis is the core structure to handle the database.
func (*Ledis) AddNewLogEventHandler ¶
func (l *Ledis) AddNewLogEventHandler(h NewLogEventHandler) error
AddNewLogEventHandler adds the handler for the new log event
func (*Ledis) CompactStore ¶
CompactStore compacts the backend storage.
func (*Ledis) IsReadOnly ¶
IsReadOnly returns whether Ledis is read only or not.
func (*Ledis) LoadDumpFile ¶
LoadDumpFile clears all data and loads dump file to db
func (*Ledis) ReadLogsTo ¶
ReadLogsTo reads logs and write to the Writer.
func (*Ledis) ReadLogsToTimeout ¶
func (l *Ledis) ReadLogsToTimeout(startLogID uint64, w io.Writer, timeout int, quitCh chan struct{}) (n int, nextLogID uint64, err error)
ReadLogsToTimeout tries to read events, if no events read, tres to wait the new event singal until timeout seconds
func (*Ledis) ReplicationStat ¶
ReplicationStat returns the statistics of repliaciton.
func (*Ledis) ReplicationUsed ¶
ReplicationUsed returns whether replication is used or not.
func (*Ledis) StoreLogsFromData ¶
StoreLogsFromData stores logs from data.
func (*Ledis) StoreLogsFromReader ¶
StoreLogsFromReader stores logs from the Reader
func (*Ledis) WaitReplication ¶
WaitReplication waits replication done
type NewLogEventHandler ¶
NewLogEventHandler is the handler to handle new log event.