2014-05-09 10:49:22 +04:00
|
|
|
package ledis
|
2014-05-04 15:02:55 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-05-22 15:59:40 +04:00
|
|
|
"github.com/siddontang/go-leveldb/leveldb"
|
2014-05-04 15:02:55 +04:00
|
|
|
)
|
|
|
|
|
2014-05-16 04:56:32 +04:00
|
|
|
type KVPair struct {
|
|
|
|
Key []byte
|
|
|
|
Value []byte
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:02:55 +04:00
|
|
|
var errKVKey = errors.New("invalid encode kv key")
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
func checkKeySize(key []byte) error {
|
|
|
|
if len(key) > MaxKeySize {
|
|
|
|
return ErrKeySize
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) encodeKVKey(key []byte) []byte {
|
|
|
|
ek := make([]byte, len(key)+2)
|
|
|
|
ek[0] = db.index
|
|
|
|
ek[1] = kvType
|
|
|
|
copy(ek[2:], key)
|
2014-05-04 15:02:55 +04:00
|
|
|
return ek
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
func (db *DB) decodeKVKey(ek []byte) ([]byte, error) {
|
|
|
|
if len(ek) < 2 || ek[0] != db.index || ek[1] != kvType {
|
2014-05-04 15:02:55 +04:00
|
|
|
return nil, errKVKey
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
return ek[2:], nil
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) incr(key []byte, delta int64) (int64, error) {
|
2014-05-20 04:41:24 +04:00
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:02:55 +04:00
|
|
|
var err error
|
2014-05-20 04:41:24 +04:00
|
|
|
key = db.encodeKVKey(key)
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.kvTx
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-06 09:32:38 +04:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
var n int64
|
|
|
|
n, err = StrInt64(db.db.Get(key))
|
2014-05-14 04:50:19 +04:00
|
|
|
if err != nil {
|
2014-05-15 10:19:48 +04:00
|
|
|
return 0, err
|
2014-05-14 04:50:19 +04:00
|
|
|
}
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
n += delta
|
|
|
|
|
|
|
|
t.Put(key, StrPutInt64(n))
|
|
|
|
|
|
|
|
//todo binlog
|
2014-05-04 15:02:55 +04:00
|
|
|
|
|
|
|
err = t.Commit()
|
2014-05-15 10:19:48 +04:00
|
|
|
return n, err
|
|
|
|
}
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) Decr(key []byte) (int64, error) {
|
|
|
|
return db.incr(key, -1)
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) DecrBy(key []byte, decrement int64) (int64, error) {
|
2014-05-15 16:48:38 +04:00
|
|
|
return db.incr(key, -decrement)
|
2014-05-15 10:19:48 +04:00
|
|
|
}
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-16 04:56:32 +04:00
|
|
|
func (db *DB) Del(keys ...[]byte) (int64, error) {
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
var err error
|
2014-05-15 10:19:48 +04:00
|
|
|
for i := range keys {
|
2014-05-20 04:41:24 +04:00
|
|
|
keys[i] = db.encodeKVKey(keys[i])
|
2014-05-15 10:19:48 +04:00
|
|
|
}
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.kvTx
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-06 09:32:38 +04:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
for i := range keys {
|
|
|
|
t.Delete(keys[i])
|
2014-05-04 15:02:55 +04:00
|
|
|
//todo binlog
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
err = t.Commit()
|
2014-05-15 10:19:48 +04:00
|
|
|
return int64(len(keys)), err
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) Exists(key []byte) (int64, error) {
|
2014-05-20 04:41:24 +04:00
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:02:55 +04:00
|
|
|
var err error
|
2014-05-20 04:41:24 +04:00
|
|
|
key = db.encodeKVKey(key)
|
2014-05-04 15:02:55 +04:00
|
|
|
|
|
|
|
var v []byte
|
2014-05-15 10:19:48 +04:00
|
|
|
v, err = db.db.Get(key)
|
2014-05-05 07:37:44 +04:00
|
|
|
if v != nil && err == nil {
|
2014-05-04 15:02:55 +04:00
|
|
|
return 1, nil
|
|
|
|
}
|
2014-05-15 10:19:48 +04:00
|
|
|
|
|
|
|
return 0, err
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) Get(key []byte) ([]byte, error) {
|
2014-05-20 04:41:24 +04:00
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key = db.encodeKVKey(key)
|
2014-05-15 10:19:48 +04:00
|
|
|
|
|
|
|
return db.db.Get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) GetSet(key []byte, value []byte) ([]byte, error) {
|
2014-05-20 04:41:24 +04:00
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key = db.encodeKVKey(key)
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.kvTx
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-06 09:32:38 +04:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
oldValue, err := db.db.Get(key)
|
2014-05-04 15:02:55 +04:00
|
|
|
if err != nil {
|
2014-05-15 10:19:48 +04:00
|
|
|
return nil, err
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t.Put(key, value)
|
|
|
|
//todo, binlog
|
2014-05-04 15:02:55 +04:00
|
|
|
|
|
|
|
err = t.Commit()
|
2014-05-15 10:19:48 +04:00
|
|
|
|
|
|
|
return oldValue, err
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) Incr(key []byte) (int64, error) {
|
|
|
|
return db.incr(key, 1)
|
|
|
|
}
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) IncryBy(key []byte, increment int64) (int64, error) {
|
|
|
|
return db.incr(key, increment)
|
|
|
|
}
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-16 04:56:32 +04:00
|
|
|
func (db *DB) MGet(keys ...[]byte) ([]interface{}, error) {
|
2014-05-15 10:19:48 +04:00
|
|
|
values := make([]interface{}, len(keys))
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
var err error
|
|
|
|
var value []byte
|
2014-05-04 15:02:55 +04:00
|
|
|
for i := range keys {
|
2014-05-20 04:41:24 +04:00
|
|
|
if err := checkKeySize(keys[i]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if value, err = db.db.Get(db.encodeKVKey(keys[i])); err != nil {
|
2014-05-15 10:19:48 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
values[i] = value
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
return values, nil
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-16 04:56:32 +04:00
|
|
|
func (db *DB) MSet(args ...KVPair) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.kvTx
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
var err error
|
|
|
|
var key []byte
|
|
|
|
var value []byte
|
|
|
|
|
2014-05-06 09:32:38 +04:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2014-05-04 15:02:55 +04:00
|
|
|
|
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 := checkKeySize(args[i].Key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key = db.encodeKVKey(args[i].Key)
|
|
|
|
|
|
|
|
value = args[i].Value
|
2014-05-04 15:02:55 +04:00
|
|
|
|
|
|
|
t.Put(key, value)
|
|
|
|
|
|
|
|
//todo binlog
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
err = t.Commit()
|
2014-05-04 15:02:55 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
func (db *DB) Set(key []byte, value []byte) error {
|
2014-05-20 04:41:24 +04:00
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
var err error
|
2014-05-20 04:41:24 +04:00
|
|
|
key = db.encodeKVKey(key)
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.kvTx
|
2014-05-04 15:02:55 +04:00
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
t.Put(key, value)
|
|
|
|
|
|
|
|
//todo, binlog
|
|
|
|
|
|
|
|
err = t.Commit()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) SetNX(key []byte, value []byte) (int64, error) {
|
2014-05-20 04:41:24 +04:00
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
var err error
|
2014-05-20 04:41:24 +04:00
|
|
|
key = db.encodeKVKey(key)
|
2014-05-15 10:19:48 +04:00
|
|
|
|
|
|
|
var n int64 = 1
|
|
|
|
|
|
|
|
t := db.kvTx
|
|
|
|
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
if v, err := db.db.Get(key); err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if v != nil {
|
|
|
|
n = 0
|
|
|
|
} else {
|
|
|
|
t.Put(key, value)
|
|
|
|
|
|
|
|
//todo binlog
|
|
|
|
|
|
|
|
err = t.Commit()
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
return n, err
|
2014-05-04 15:02:55 +04:00
|
|
|
}
|
2014-05-22 15:59:40 +04:00
|
|
|
|
|
|
|
func (db *DB) KvFlush() (drop int64, err error) {
|
|
|
|
t := db.kvTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
minKey := make([]byte, 2)
|
|
|
|
minKey[0] = db.index
|
|
|
|
minKey[1] = kvType
|
|
|
|
|
|
|
|
maxKey := make([]byte, 2)
|
|
|
|
maxKey[0] = db.index
|
|
|
|
maxKey[1] = kvType + 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
|
|
|
|
}
|