ledisdb/ledis/t_kv.go

186 lines
2.6 KiB
Go
Raw Normal View History

2014-05-09 10:49:22 +04:00
package ledis
2014-05-04 15:02:55 +04:00
import (
"errors"
)
var errKVKey = errors.New("invalid encode kv key")
func encode_kv_key(key []byte) []byte {
2014-05-05 07:37:44 +04:00
ek := make([]byte, len(key)+1)
2014-05-04 15:02:55 +04:00
ek[0] = KV_TYPE
copy(ek[1:], key)
return ek
}
2014-05-06 09:32:38 +04:00
func decode_kv_key(ek []byte) ([]byte, error) {
if len(ek) == 0 || ek[0] != KV_TYPE {
2014-05-04 15:02:55 +04:00
return nil, errKVKey
}
2014-05-06 09:32:38 +04:00
return ek[1:], nil
2014-05-04 15:02:55 +04:00
}
func (a *App) kv_get(key []byte) ([]byte, error) {
key = encode_kv_key(key)
return a.db.Get(key)
}
func (a *App) kv_set(key []byte, value []byte) error {
key = encode_kv_key(key)
var err error
2014-05-06 09:32:38 +04:00
t := a.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
t.Put(key, value)
//todo, binlog
err = t.Commit()
return err
}
func (a *App) kv_getset(key []byte, value []byte) ([]byte, error) {
key = encode_kv_key(key)
2014-05-06 09:32:38 +04:00
t := a.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-14 04:50:19 +04:00
oldValue, err := a.db.Get(key)
if err != nil {
return nil, err
}
2014-05-04 15:02:55 +04:00
t.Put(key, value)
//todo, binlog
err = t.Commit()
return oldValue, err
}
func (a *App) kv_setnx(key []byte, value []byte) (int64, error) {
key = encode_kv_key(key)
var err error
var n int64 = 1
2014-05-06 09:32:38 +04:00
t := a.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-14 04:50:19 +04:00
if v, err := a.db.Get(key); err != nil {
return 0, err
} else if v != nil {
2014-05-04 15:02:55 +04:00
n = 0
} else {
t.Put(key, value)
//todo binlog
err = t.Commit()
}
return n, err
}
func (a *App) kv_exists(key []byte) (int64, error) {
key = encode_kv_key(key)
var err error
var v []byte
v, err = a.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
} else {
return 0, err
}
}
func (a *App) kv_incr(key []byte, delta int64) (int64, error) {
key = encode_kv_key(key)
var err error
2014-05-06 09:32:38 +04:00
t := a.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-06 09:32:38 +04:00
var n int64
n, err = StrInt64(a.db.Get(key))
2014-05-04 15:02:55 +04:00
if err != nil {
return 0, err
}
n += delta
t.Put(key, StrPutInt64(n))
2014-05-04 15:02:55 +04:00
//todo binlog
err = t.Commit()
return n, err
}
func (a *App) tx_del(keys [][]byte) (int64, error) {
for i := range keys {
keys[i] = encode_kv_key(keys[i])
}
2014-05-06 09:32:38 +04:00
t := a.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
for i := range keys {
t.Delete(keys[i])
//todo binlog
}
err := t.Commit()
return int64(len(keys)), err
}
func (a *App) tx_mset(args [][]byte) error {
2014-05-06 09:32:38 +04:00
t := a.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
for i := 0; i < len(args); i += 2 {
key := encode_kv_key(args[i])
value := args[i+1]
t.Put(key, value)
//todo binlog
}
err := t.Commit()
return err
}
func (a *App) kv_mget(args [][]byte) ([]interface{}, error) {
values := make([]interface{}, len(args))
for i := range args {
key := encode_kv_key(args[i])
value, err := a.db.Get(key)
if err != nil {
return nil, err
}
values[i] = value
}
return values, nil
}