ledisdb/ledis/t_hash.go

557 lines
10 KiB
Go
Raw Permalink Normal View History

2014-05-09 10:49:22 +04:00
package ledis
2014-05-06 13:37:58 +04:00
import (
"encoding/binary"
"errors"
2015-05-04 17:42:28 +03:00
"time"
2023-02-15 10:41:45 +03:00
"git.internal/re/ledisdb/store"
2014-09-24 09:29:27 +04:00
"github.com/siddontang/go/num"
2014-05-06 13:37:58 +04:00
)
2018-03-29 15:33:36 +03:00
// FVPair is the pair of field and value.
2014-05-16 04:56:32 +04:00
type FVPair struct {
Field []byte
Value []byte
}
2014-05-06 13:37:58 +04:00
var errHashKey = errors.New("invalid hash key")
var errHSizeKey = errors.New("invalid hsize key")
const (
hashStartSep byte = ':'
2014-05-08 06:54:33 +04:00
hashStopSep byte = hashStartSep + 1
2014-05-06 13:37:58 +04:00
)
2014-05-20 04:41:24 +04:00
func checkHashKFSize(key []byte, field []byte) error {
2014-05-23 08:23:20 +04:00
if len(key) > MaxKeySize || len(key) == 0 {
2014-06-05 11:46:38 +04:00
return errKeySize
2014-05-23 08:23:20 +04:00
} else if len(field) > MaxHashFieldSize || len(field) == 0 {
2014-06-05 11:46:38 +04:00
return errHashFieldSize
2014-05-20 04:41:24 +04:00
}
return nil
}
func (db *DB) hEncodeSizeKey(key []byte) []byte {
2015-03-15 15:36:14 +03:00
buf := make([]byte, len(key)+1+len(db.indexVarBuf))
2014-05-06 13:37:58 +04:00
2015-03-15 15:36:14 +03:00
pos := 0
n := copy(buf, db.indexVarBuf)
pos += n
buf[pos] = HSizeType
pos++
copy(buf[pos:], key)
2014-05-20 04:41:24 +04:00
2014-05-06 13:37:58 +04:00
return buf
}
2014-05-20 04:41:24 +04:00
func (db *DB) hDecodeSizeKey(ek []byte) ([]byte, error) {
2015-03-15 15:36:14 +03:00
pos, err := db.checkKeyIndex(ek)
if err != nil {
return nil, err
}
if pos+1 > len(ek) || ek[pos] != HSizeType {
2014-05-06 13:37:58 +04:00
return nil, errHSizeKey
}
2015-03-15 15:36:14 +03:00
pos++
2014-05-06 13:37:58 +04:00
2015-03-15 15:36:14 +03:00
return ek[pos:], nil
2014-05-06 13:37:58 +04:00
}
2014-05-20 04:41:24 +04:00
func (db *DB) hEncodeHashKey(key []byte, field []byte) []byte {
2015-03-15 15:36:14 +03:00
buf := make([]byte, len(key)+len(field)+1+1+2+len(db.indexVarBuf))
2014-05-06 13:37:58 +04:00
pos := 0
2015-03-15 15:36:14 +03:00
n := copy(buf, db.indexVarBuf)
pos += n
buf[pos] = HashType
2014-05-06 13:37:58 +04:00
pos++
2014-05-20 04:41:24 +04:00
binary.BigEndian.PutUint16(buf[pos:], uint16(len(key)))
pos += 2
2014-05-06 13:37:58 +04:00
copy(buf[pos:], key)
pos += len(key)
buf[pos] = hashStartSep
pos++
copy(buf[pos:], field)
return buf
}
2014-05-20 04:41:24 +04:00
func (db *DB) hDecodeHashKey(ek []byte) ([]byte, []byte, error) {
2015-03-15 15:36:14 +03:00
pos, err := db.checkKeyIndex(ek)
if err != nil {
return nil, nil, err
}
if pos+1 > len(ek) || ek[pos] != HashType {
return nil, nil, errHashKey
}
pos++
if pos+2 > len(ek) {
2014-05-06 13:37:58 +04:00
return nil, nil, errHashKey
}
2014-05-20 04:41:24 +04:00
keyLen := int(binary.BigEndian.Uint16(ek[pos:]))
pos += 2
2014-05-06 13:37:58 +04:00
2015-03-15 15:36:14 +03:00
if keyLen+pos > len(ek) {
2014-05-06 13:37:58 +04:00
return nil, nil, errHashKey
}
key := ek[pos : pos+keyLen]
pos += keyLen
if ek[pos] != hashStartSep {
return nil, nil, errHashKey
}
pos++
field := ek[pos:]
return key, field, nil
}
2014-05-20 04:41:24 +04:00
func (db *DB) hEncodeStartKey(key []byte) []byte {
return db.hEncodeHashKey(key, nil)
}
func (db *DB) hEncodeStopKey(key []byte) []byte {
k := db.hEncodeHashKey(key, nil)
k[len(k)-1] = hashStopSep
return k
}
func (db *DB) hSetItem(key []byte, field []byte, value []byte) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-05-06 13:37:58 +04:00
2014-05-20 04:41:24 +04:00
ek := db.hEncodeHashKey(key, field)
2014-05-06 13:37:58 +04:00
var n int64 = 1
2014-08-25 10:18:23 +04:00
if v, _ := db.bucket.Get(ek); v != nil {
2014-05-06 13:37:58 +04:00
n = 0
} else {
if _, err := db.hIncrSize(key, 1); err != nil {
return 0, err
}
2014-05-06 13:37:58 +04:00
}
t.Put(ek, value)
return n, nil
}
// ps : here just focus on deleting the hash data,
// any other likes expire is ignore.
2014-08-25 10:18:23 +04:00
func (db *DB) hDelete(t *batch, key []byte) int64 {
sk := db.hEncodeSizeKey(key)
start := db.hEncodeStartKey(key)
stop := db.hEncodeStopKey(key)
2018-03-29 15:33:36 +03:00
var num int64
2014-08-25 10:18:23 +04:00
it := db.bucket.RangeLimitIterator(start, stop, store.RangeROpen, 0, -1)
for ; it.Valid(); it.Next() {
t.Delete(it.Key())
num++
}
it.Close()
t.Delete(sk)
return num
}
func (db *DB) hExpireAt(key []byte, when int64) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.hashBatch
t.Lock()
defer t.Unlock()
if hlen, err := db.HLen(key); err != nil || hlen == 0 {
return 0, err
}
2018-03-29 15:33:36 +03:00
db.expireAt(t, HashType, key, when)
if err := t.Commit(); err != nil {
return 0, err
}
return 1, nil
}
2018-03-29 15:33:36 +03:00
// HLen returns the lengh of hash.
func (db *DB) HLen(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
2014-08-25 10:18:23 +04:00
return Int64(db.bucket.Get(db.hEncodeSizeKey(key)))
}
2018-03-29 15:33:36 +03:00
// HSet sets the field with value of key.
func (db *DB) HSet(key []byte, field []byte, value []byte) (int64, error) {
2014-05-20 04:41:24 +04:00
if err := checkHashKFSize(key, field); err != nil {
return 0, err
} else if err := checkValueSize(value); err != nil {
return 0, err
2014-05-20 04:41:24 +04:00
}
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-05-06 13:37:58 +04:00
t.Lock()
defer t.Unlock()
n, err := db.hSetItem(key, field, value)
2014-05-06 13:37:58 +04:00
if err != nil {
return 0, err
}
err = t.Commit()
return n, err
}
2018-03-29 15:33:36 +03:00
// HGet gets the value of the field.
func (db *DB) HGet(key []byte, field []byte) ([]byte, error) {
2014-05-20 04:41:24 +04:00
if err := checkHashKFSize(key, field); err != nil {
return nil, err
}
2014-08-25 10:18:23 +04:00
return db.bucket.Get(db.hEncodeHashKey(key, field))
2014-05-06 13:37:58 +04:00
}
2018-03-29 15:33:36 +03:00
// HMset sets multi field-values.
2014-05-16 04:56:32 +04:00
func (db *DB) HMset(key []byte, args ...FVPair) error {
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-05-06 13:37:58 +04:00
t.Lock()
defer t.Unlock()
2014-05-20 04:41:24 +04:00
var err error
var ek []byte
2018-03-29 15:33:36 +03:00
var num int64
2014-05-16 04:56:32 +04:00
for i := 0; i < len(args); i++ {
2014-05-20 04:41:24 +04:00
if err := checkHashKFSize(key, args[i].Field); err != nil {
return err
} else if err := checkValueSize(args[i].Value); err != nil {
return err
2014-05-20 04:41:24 +04:00
}
ek = db.hEncodeHashKey(key, args[i].Field)
2014-08-25 10:18:23 +04:00
if v, err := db.bucket.Get(ek); err != nil {
2014-05-20 04:41:24 +04:00
return err
} else if v == nil {
num++
2014-05-06 13:37:58 +04:00
}
2014-05-16 04:56:32 +04:00
t.Put(ek, args[i].Value)
2014-05-06 13:37:58 +04:00
}
2014-05-20 04:41:24 +04:00
if _, err = db.hIncrSize(key, num); err != nil {
return err
}
2014-05-06 13:37:58 +04:00
//todo add binglog
2014-05-20 04:41:24 +04:00
err = t.Commit()
2014-05-06 13:37:58 +04:00
return err
}
2018-03-29 15:33:36 +03:00
// HMget gets multi values of fields
func (db *DB) HMget(key []byte, args ...[]byte) ([][]byte, error) {
2014-05-20 04:41:24 +04:00
var ek []byte
2014-06-19 13:19:40 +04:00
2014-08-25 10:18:23 +04:00
it := db.bucket.NewIterator()
2014-06-19 13:19:40 +04:00
defer it.Close()
2014-05-20 04:41:24 +04:00
r := make([][]byte, len(args))
2014-05-06 13:37:58 +04:00
for i := 0; i < len(args); i++ {
2014-05-20 04:41:24 +04:00
if err := checkHashKFSize(key, args[i]); err != nil {
return nil, err
}
ek = db.hEncodeHashKey(key, args[i])
2014-06-19 13:19:40 +04:00
r[i] = it.Find(ek)
2014-05-06 13:37:58 +04:00
}
return r, nil
}
2018-03-29 15:33:36 +03:00
// HDel deletes the fields.
func (db *DB) HDel(key []byte, args ...[]byte) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-05-20 04:41:24 +04:00
var ek []byte
var v []byte
var err error
2014-05-06 13:37:58 +04:00
t.Lock()
defer t.Unlock()
2014-08-25 10:18:23 +04:00
it := db.bucket.NewIterator()
defer it.Close()
2018-03-29 15:33:36 +03:00
var num int64
2014-05-06 13:37:58 +04:00
for i := 0; i < len(args); i++ {
2014-05-20 04:41:24 +04:00
if err := checkHashKFSize(key, args[i]); err != nil {
return 0, err
}
ek = db.hEncodeHashKey(key, args[i])
2014-07-07 11:36:45 +04:00
v = it.RawFind(ek)
if v == nil {
2014-05-06 13:37:58 +04:00
continue
} else {
num++
t.Delete(ek)
}
}
2014-05-20 04:41:24 +04:00
if _, err = db.hIncrSize(key, -num); err != nil {
return 0, err
2014-05-06 13:37:58 +04:00
}
2014-05-20 04:41:24 +04:00
err = t.Commit()
2014-05-06 13:37:58 +04:00
return num, err
}
func (db *DB) hIncrSize(key []byte, delta int64) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-05-20 04:41:24 +04:00
sk := db.hEncodeSizeKey(key)
var err error
2018-03-29 15:33:36 +03:00
var size int64
2014-08-25 10:18:23 +04:00
if size, err = Int64(db.bucket.Get(sk)); err != nil {
return 0, err
2018-03-29 15:33:36 +03:00
}
size += delta
if size <= 0 {
size = 0
t.Delete(sk)
db.rmExpire(t, HashType, key)
} else {
2018-03-29 15:33:36 +03:00
t.Put(sk, PutInt64(size))
}
return size, nil
}
2018-03-29 15:33:36 +03:00
// HIncrBy increases the value of field by delta.
func (db *DB) HIncrBy(key []byte, field []byte, delta int64) (int64, error) {
2014-05-20 04:41:24 +04:00
if err := checkHashKFSize(key, field); err != nil {
return 0, err
}
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-05-20 04:41:24 +04:00
var ek []byte
var err error
2014-05-06 13:37:58 +04:00
t.Lock()
defer t.Unlock()
2014-05-20 04:41:24 +04:00
ek = db.hEncodeHashKey(key, field)
2014-05-06 13:37:58 +04:00
2018-03-29 15:33:36 +03:00
var n int64
2014-08-25 10:18:23 +04:00
if n, err = StrInt64(db.bucket.Get(ek)); err != nil {
2014-05-06 13:37:58 +04:00
return 0, err
}
n += delta
2014-09-24 09:29:27 +04:00
_, err = db.hSetItem(key, field, num.FormatInt64ToSlice(n))
2014-05-06 13:37:58 +04:00
if err != nil {
return 0, err
}
err = t.Commit()
return n, err
}
2018-03-29 15:33:36 +03:00
// HGetAll returns all field-values.
func (db *DB) HGetAll(key []byte) ([]FVPair, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return nil, err
}
start := db.hEncodeStartKey(key)
stop := db.hEncodeStopKey(key)
2014-05-06 13:37:58 +04:00
v := make([]FVPair, 0, 16)
2014-05-06 13:37:58 +04:00
2014-08-25 10:18:23 +04:00
it := db.bucket.RangeLimitIterator(start, stop, store.RangeROpen, 0, -1)
2015-03-02 09:30:04 +03:00
defer it.Close()
2014-05-06 13:37:58 +04:00
for ; it.Valid(); it.Next() {
_, f, err := db.hDecodeHashKey(it.Key())
2014-05-06 13:37:58 +04:00
if err != nil {
return nil, err
}
v = append(v, FVPair{Field: f, Value: it.Value()})
2014-05-06 13:37:58 +04:00
}
return v, nil
}
2018-03-29 15:33:36 +03:00
// HKeys returns the all fields.
func (db *DB) HKeys(key []byte) ([][]byte, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return nil, err
}
start := db.hEncodeStartKey(key)
stop := db.hEncodeStopKey(key)
2014-05-06 13:37:58 +04:00
v := make([][]byte, 0, 16)
2014-05-06 13:37:58 +04:00
2014-08-25 10:18:23 +04:00
it := db.bucket.RangeLimitIterator(start, stop, store.RangeROpen, 0, -1)
2015-03-02 09:30:04 +03:00
defer it.Close()
2014-05-06 13:37:58 +04:00
for ; it.Valid(); it.Next() {
_, f, err := db.hDecodeHashKey(it.Key())
2014-05-06 13:37:58 +04:00
if err != nil {
return nil, err
}
v = append(v, f)
2014-05-06 13:37:58 +04:00
}
return v, nil
}
2018-03-29 15:33:36 +03:00
// HValues returns all values
func (db *DB) HValues(key []byte) ([][]byte, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return nil, err
}
start := db.hEncodeStartKey(key)
stop := db.hEncodeStopKey(key)
2014-05-06 13:37:58 +04:00
v := make([][]byte, 0, 16)
2014-05-06 13:37:58 +04:00
2014-08-25 10:18:23 +04:00
it := db.bucket.RangeLimitIterator(start, stop, store.RangeROpen, 0, -1)
2015-03-02 09:30:04 +03:00
defer it.Close()
2014-05-06 13:37:58 +04:00
for ; it.Valid(); it.Next() {
_, _, err := db.hDecodeHashKey(it.Key())
if err != nil {
return nil, err
}
2014-05-06 13:37:58 +04:00
v = append(v, it.Value())
}
return v, nil
}
2014-05-12 11:08:59 +04:00
2018-03-29 15:33:36 +03:00
// HClear clears the data.
func (db *DB) HClear(key []byte) (int64, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return 0, err
}
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-05-12 11:08:59 +04:00
t.Lock()
defer t.Unlock()
num := db.hDelete(t, key)
db.rmExpire(t, HashType, key)
2014-05-12 11:08:59 +04:00
err := t.Commit()
return num, err
}
2018-03-29 15:33:36 +03:00
// HMclear cleans multi data.
func (db *DB) HMclear(keys ...[]byte) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.hashBatch
t.Lock()
defer t.Unlock()
for _, key := range keys {
if err := checkKeySize(key); err != nil {
return 0, err
}
db.hDelete(t, key)
db.rmExpire(t, HashType, key)
}
err := t.Commit()
return int64(len(keys)), err
}
func (db *DB) hFlush() (drop int64, err error) {
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-07-03 07:38:52 +04:00
t.Lock()
defer t.Unlock()
2014-08-25 10:18:23 +04:00
2014-08-16 11:35:05 +04:00
return db.flushType(t, HashType)
}
2014-05-25 10:01:35 +04:00
2018-03-29 15:33:36 +03:00
// HExpire expires the data with duration.
func (db *DB) HExpire(key []byte, duration int64) (int64, error) {
if duration <= 0 {
return 0, errExpireValue
}
return db.hExpireAt(key, time.Now().Unix()+duration)
}
2018-03-29 15:33:36 +03:00
// HExpireAt expires the data at time when.
func (db *DB) HExpireAt(key []byte, when int64) (int64, error) {
if when <= time.Now().Unix() {
return 0, errExpireValue
}
return db.hExpireAt(key, when)
}
2018-03-29 15:33:36 +03:00
// HTTL gets the TTL of data.
func (db *DB) HTTL(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return -1, err
}
return db.ttl(HashType, key)
}
2014-06-23 07:12:20 +04:00
2018-03-29 15:33:36 +03:00
// HPersist removes the TTL of data.
2014-06-23 07:12:20 +04:00
func (db *DB) HPersist(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
2014-08-25 10:18:23 +04:00
t := db.hashBatch
2014-06-23 07:12:20 +04:00
t.Lock()
defer t.Unlock()
n, err := db.rmExpire(t, HashType, key)
2014-06-23 07:12:20 +04:00
if err != nil {
return 0, err
}
err = t.Commit()
return n, err
}
2015-02-01 12:19:46 +03:00
2018-03-29 15:33:36 +03:00
// HKeyExists checks whether data exists or not.
func (db *DB) HKeyExists(key []byte) (int64, error) {
2015-02-01 12:19:46 +03:00
if err := checkKeySize(key); err != nil {
return 0, err
}
sk := db.hEncodeSizeKey(key)
v, err := db.bucket.Get(sk)
if v != nil && err == nil {
return 1, nil
}
return 0, err
}