2014-05-09 10:49:22 +04:00
|
|
|
package ledis
|
2014-05-06 13:37:58 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2014-05-14 12:35:49 +04:00
|
|
|
"github.com/siddontang/go-leveldb/leveldb"
|
2014-05-06 13:37:58 +04:00
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
if len(key) > MaxKeySize {
|
|
|
|
return ErrKeySize
|
|
|
|
} else if len(field) > MaxHashFieldSize {
|
|
|
|
return ErrHashFieldSize
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) hEncodeSizeKey(key []byte) []byte {
|
|
|
|
buf := make([]byte, len(key)+2)
|
2014-05-06 13:37:58 +04:00
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
buf[0] = db.index
|
|
|
|
buf[1] = hSizeType
|
|
|
|
|
|
|
|
copy(buf[2:], key)
|
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) {
|
|
|
|
if len(ek) < 2 || ek[0] != db.index || ek[1] != hSizeType {
|
2014-05-06 13:37:58 +04:00
|
|
|
return nil, errHSizeKey
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
return ek[2:], 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 {
|
|
|
|
buf := make([]byte, len(key)+len(field)+1+1+2+1)
|
2014-05-06 13:37:58 +04:00
|
|
|
|
|
|
|
pos := 0
|
2014-05-20 04:41:24 +04:00
|
|
|
buf[pos] = db.index
|
|
|
|
pos++
|
2014-05-16 11:03:23 +04:00
|
|
|
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) {
|
|
|
|
if len(ek) < 5 || ek[0] != db.index || ek[1] != hashType {
|
2014-05-06 13:37:58 +04:00
|
|
|
return nil, nil, errHashKey
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
pos := 2
|
|
|
|
keyLen := int(binary.BigEndian.Uint16(ek[pos:]))
|
|
|
|
pos += 2
|
2014-05-06 13:37:58 +04:00
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
if keyLen+5 > 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
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) HLen(key []byte) (int64, error) {
|
2014-05-20 04:41:24 +04:00
|
|
|
return Int64(db.db.Get(db.hEncodeSizeKey(key)))
|
2014-05-06 13:37:58 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) hSetItem(key []byte, field []byte, value []byte) (int64, error) {
|
|
|
|
t := db.hashTx
|
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-05-15 10:19:48 +04:00
|
|
|
if v, _ := db.db.Get(ek); v != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
n = 0
|
|
|
|
} else {
|
2014-05-15 10:19:48 +04:00
|
|
|
if _, err := db.hIncrSize(key, 1); err != nil {
|
2014-05-14 12:35:49 +04:00
|
|
|
return 0, err
|
|
|
|
}
|
2014-05-06 13:37:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Put(ek, value)
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.hashTx
|
2014-05-06 13:37:58 +04:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
n, err := db.hSetItem(key, field, value)
|
2014-05-06 13:37:58 +04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//todo add binlog
|
|
|
|
|
|
|
|
err = t.Commit()
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.db.Get(db.hEncodeHashKey(key, field))
|
2014-05-06 13:37:58 +04:00
|
|
|
}
|
|
|
|
|
2014-05-16 04:56:32 +04:00
|
|
|
func (db *DB) HMset(key []byte, args ...FVPair) error {
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.hashTx
|
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
|
2014-05-14 12:35:49 +04:00
|
|
|
var num int64 = 0
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
ek = db.hEncodeHashKey(key, args[i].Field)
|
|
|
|
|
|
|
|
if v, err := db.db.Get(ek); err != nil {
|
|
|
|
return err
|
|
|
|
} else if v == nil {
|
2014-05-14 12:35:49 +04:00
|
|
|
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 {
|
2014-05-14 12:35:49 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) HMget(key []byte, args [][]byte) ([]interface{}, error) {
|
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
|
|
|
r := make([]interface{}, len(args))
|
|
|
|
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])
|
|
|
|
|
|
|
|
if v, err = db.db.Get(ek); err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r[i] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) HDel(key []byte, args [][]byte) (int64, error) {
|
|
|
|
t := db.hashTx
|
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()
|
|
|
|
|
|
|
|
var num int64 = 0
|
|
|
|
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])
|
|
|
|
|
|
|
|
if v, err = db.db.Get(ek); err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return 0, err
|
|
|
|
} else if v == nil {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
num++
|
|
|
|
t.Delete(ek)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
if _, err = db.hIncrSize(key, -num); err != nil {
|
2014-05-14 12:35:49 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) hIncrSize(key []byte, delta int64) (int64, error) {
|
|
|
|
t := db.hashTx
|
2014-05-20 04:41:24 +04:00
|
|
|
sk := db.hEncodeSizeKey(key)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var size int64 = 0
|
|
|
|
if size, err = Int64(db.db.Get(sk)); err != nil {
|
2014-05-14 12:35:49 +04:00
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
size += delta
|
|
|
|
if size <= 0 {
|
|
|
|
size = 0
|
|
|
|
t.Delete(sk)
|
|
|
|
} else {
|
|
|
|
t.Put(sk, PutInt64(size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
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-05-15 10:19:48 +04:00
|
|
|
t := db.hashTx
|
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
|
|
|
|
|
|
|
var n int64 = 0
|
2014-05-20 04:41:24 +04:00
|
|
|
if n, err = StrInt64(db.db.Get(ek)); err != nil {
|
2014-05-06 13:37:58 +04:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n += delta
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
_, err = db.hSetItem(key, field, StrPutInt64(n))
|
2014-05-06 13:37:58 +04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Commit()
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) HGetAll(key []byte) ([]interface{}, 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([]interface{}, 0, 16)
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
it := db.db.Iterator(start, stop, leveldb.RangeROpen, 0, -1)
|
2014-05-06 13:37:58 +04:00
|
|
|
for ; it.Valid(); it.Next() {
|
2014-05-20 04:41:24 +04:00
|
|
|
_, k, err := db.hDecodeHashKey(it.Key())
|
2014-05-06 13:37:58 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
v = append(v, k)
|
|
|
|
v = append(v, it.Value())
|
|
|
|
}
|
|
|
|
|
2014-05-08 06:54:33 +04:00
|
|
|
it.Close()
|
|
|
|
|
2014-05-06 13:37:58 +04:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) HKeys(key []byte) ([]interface{}, 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([]interface{}, 0, 16)
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
it := db.db.Iterator(start, stop, leveldb.RangeROpen, 0, -1)
|
2014-05-06 13:37:58 +04:00
|
|
|
for ; it.Valid(); it.Next() {
|
2014-05-20 04:41:24 +04:00
|
|
|
_, k, err := db.hDecodeHashKey(it.Key())
|
2014-05-06 13:37:58 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
v = append(v, k)
|
|
|
|
}
|
|
|
|
|
2014-05-08 06:54:33 +04:00
|
|
|
it.Close()
|
|
|
|
|
2014-05-06 13:37:58 +04:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) HValues(key []byte) ([]interface{}, 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([]interface{}, 0, 16)
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
it := db.db.Iterator(start, stop, leveldb.RangeROpen, 0, -1)
|
2014-05-06 13:37:58 +04:00
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
v = append(v, it.Value())
|
|
|
|
}
|
|
|
|
|
2014-05-08 06:54:33 +04:00
|
|
|
it.Close()
|
|
|
|
|
2014-05-06 13:37:58 +04:00
|
|
|
return v, nil
|
|
|
|
}
|
2014-05-12 11:08:59 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
sk := db.hEncodeSizeKey(key)
|
|
|
|
start := db.hEncodeStartKey(key)
|
|
|
|
stop := db.hEncodeStopKey(key)
|
2014-05-12 11:08:59 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.hashTx
|
2014-05-12 11:08:59 +04:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
var num int64 = 0
|
2014-05-15 10:19:48 +04:00
|
|
|
it := db.db.Iterator(start, stop, leveldb.RangeROpen, 0, -1)
|
2014-05-12 11:08:59 +04:00
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
t.Delete(it.Key())
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
|
|
|
|
it.Close()
|
|
|
|
|
|
|
|
t.Delete(sk)
|
|
|
|
|
|
|
|
err := t.Commit()
|
|
|
|
return num, err
|
|
|
|
}
|
2014-05-22 15:59:40 +04:00
|
|
|
|
|
|
|
func (db *DB) HFlush() (drop int64, err error) {
|
|
|
|
t := db.kvTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
minKey := make([]byte, 2)
|
|
|
|
minKey[0] = db.index
|
|
|
|
minKey[1] = hashType
|
|
|
|
|
|
|
|
maxKey := make([]byte, 2)
|
|
|
|
maxKey[0] = db.index
|
|
|
|
maxKey[1] = hSizeType + 1
|
|
|
|
|
|
|
|
it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, 0, -1)
|
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
t.Delete(it.Key())
|
|
|
|
drop++
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Commit()
|
|
|
|
return
|
|
|
|
}
|