2014-05-09 10:49:22 +04:00
|
|
|
package ledis
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2014-05-14 12:35:49 +04:00
|
|
|
"github.com/siddontang/go-leveldb/leveldb"
|
2014-05-06 09:32:38 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-05-09 05:17:28 +04:00
|
|
|
listHeadSeq int32 = 1
|
|
|
|
listTailSeq int32 = 2
|
2014-05-06 09:32:38 +04:00
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
listMinSeq int32 = 1000
|
|
|
|
listMaxSeq int32 = 1<<31 - 1000
|
|
|
|
listInitialSeq int32 = listMinSeq + (listMaxSeq-listMinSeq)/2
|
2014-05-06 09:32:38 +04:00
|
|
|
)
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
var errLMetaKey = errors.New("invalid lmeta key")
|
2014-05-06 09:32:38 +04:00
|
|
|
var errListKey = errors.New("invalid list key")
|
|
|
|
var errListSeq = errors.New("invalid list sequence, overflow")
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func encode_lmeta_key(key []byte) []byte {
|
2014-05-06 09:32:38 +04:00
|
|
|
buf := make([]byte, len(key)+1)
|
2014-05-09 05:17:28 +04:00
|
|
|
buf[0] = LMETA_TYPE
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
copy(buf[1:], key)
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func decode_lmeta_key(ek []byte) ([]byte, error) {
|
|
|
|
if len(ek) == 0 || ek[0] != LMETA_TYPE {
|
|
|
|
return nil, errLMetaKey
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ek[1:], nil
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func encode_list_key(key []byte, seq int32) []byte {
|
|
|
|
buf := make([]byte, len(key)+9)
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
pos := 0
|
|
|
|
buf[pos] = LIST_TYPE
|
|
|
|
pos++
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint32(buf[pos:], uint32(len(key)))
|
|
|
|
pos += 4
|
|
|
|
|
|
|
|
copy(buf[pos:], key)
|
|
|
|
pos += len(key)
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
binary.BigEndian.PutUint32(buf[pos:], uint32(seq))
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func decode_list_key(ek []byte) (key []byte, seq int32, err error) {
|
|
|
|
if len(ek) < 9 || ek[0] != LIST_TYPE {
|
2014-05-06 09:32:38 +04:00
|
|
|
err = errListKey
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keyLen := int(binary.BigEndian.Uint32(ek[1:]))
|
2014-05-09 05:17:28 +04:00
|
|
|
if keyLen+9 != len(ek) {
|
2014-05-06 09:32:38 +04:00
|
|
|
err = errListKey
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
key = ek[5 : 5+keyLen]
|
2014-05-09 05:17:28 +04:00
|
|
|
seq = int32(binary.BigEndian.Uint32(ek[5+keyLen:]))
|
2014-05-06 09:32:38 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) list_lpush(key []byte, args [][]byte) (int64, error) {
|
|
|
|
return a.list_push(key, args, listHeadSeq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) list_rpush(key []byte, args [][]byte) (int64, error) {
|
|
|
|
return a.list_push(key, args, listTailSeq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) list_lpop(key []byte) ([]byte, error) {
|
|
|
|
return a.list_pop(key, listHeadSeq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) list_rpop(key []byte) ([]byte, error) {
|
|
|
|
return a.list_pop(key, listTailSeq)
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func (a *App) list_getSeq(key []byte, whereSeq int32) (int64, error) {
|
2014-05-06 09:32:38 +04:00
|
|
|
ek := encode_list_key(key, whereSeq)
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
return Int64(a.db.Get(ek))
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func (a *App) list_getMeta(ek []byte) (headSeq int32, tailSeq int32, size int32, err error) {
|
|
|
|
var v []byte
|
|
|
|
v, err = a.db.Get(ek)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
} else if v == nil {
|
|
|
|
size = 0
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
headSeq = int32(binary.LittleEndian.Uint32(v[0:4]))
|
|
|
|
tailSeq = int32(binary.LittleEndian.Uint32(v[4:8]))
|
|
|
|
size = int32(binary.LittleEndian.Uint32(v[8:]))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-05-06 09:32:38 +04:00
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func (a *App) list_setMeta(ek []byte, headSeq int32, tailSeq int32, size int32) {
|
|
|
|
t := a.listTx
|
|
|
|
|
|
|
|
buf := make([]byte, 12)
|
|
|
|
|
|
|
|
binary.LittleEndian.PutUint32(buf[0:4], uint32(headSeq))
|
|
|
|
binary.LittleEndian.PutUint32(buf[4:8], uint32(tailSeq))
|
|
|
|
binary.LittleEndian.PutUint32(buf[8:], uint32(size))
|
|
|
|
|
|
|
|
t.Put(ek, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) list_len(key []byte) (int64, error) {
|
|
|
|
ek := encode_lmeta_key(key)
|
|
|
|
_, _, size, err := a.list_getMeta(ek)
|
|
|
|
return int64(size), err
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func (a *App) list_push(key []byte, args [][]byte, whereSeq int32) (int64, error) {
|
2014-05-06 09:32:38 +04:00
|
|
|
t := a.listTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
metaKey := encode_lmeta_key(key)
|
|
|
|
headSeq, tailSeq, size, err := a.list_getMeta(metaKey)
|
|
|
|
|
2014-05-06 09:32:38 +04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
var delta int32 = 1
|
|
|
|
var seq int32 = 0
|
2014-05-06 09:32:38 +04:00
|
|
|
if whereSeq == listHeadSeq {
|
|
|
|
delta = -1
|
2014-05-09 05:17:28 +04:00
|
|
|
seq = headSeq
|
|
|
|
} else {
|
|
|
|
seq = tailSeq
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
if size == 0 {
|
|
|
|
headSeq = listInitialSeq
|
|
|
|
tailSeq = listInitialSeq
|
|
|
|
seq = headSeq
|
2014-05-06 09:32:38 +04:00
|
|
|
} else {
|
|
|
|
seq += delta
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
2014-05-09 05:17:28 +04:00
|
|
|
t.Put(encode_list_key(key, seq+int32(i)*delta), args[i])
|
2014-05-06 09:32:38 +04:00
|
|
|
//to do add binlog
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
seq += int32(len(args)-1) * delta
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
if seq <= listMinSeq || seq >= listMaxSeq {
|
|
|
|
return 0, errListSeq
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
size += int32(len(args))
|
2014-05-06 09:32:38 +04:00
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
if whereSeq == listHeadSeq {
|
|
|
|
headSeq = seq
|
|
|
|
} else {
|
|
|
|
tailSeq = seq
|
|
|
|
}
|
|
|
|
|
|
|
|
a.list_setMeta(metaKey, headSeq, tailSeq, size)
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
err = t.Commit()
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
return int64(size), err
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func (a *App) list_pop(key []byte, whereSeq int32) ([]byte, error) {
|
2014-05-06 09:32:38 +04:00
|
|
|
t := a.listTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
metaKey := encode_lmeta_key(key)
|
|
|
|
headSeq, tailSeq, size, err := a.list_getMeta(metaKey)
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
var seq int32 = 0
|
|
|
|
var delta int32 = 1
|
|
|
|
if whereSeq == listHeadSeq {
|
|
|
|
seq = headSeq
|
|
|
|
} else {
|
|
|
|
delta = -1
|
|
|
|
seq = tailSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
itemKey := encode_list_key(key, seq)
|
2014-05-06 09:32:38 +04:00
|
|
|
var value []byte
|
2014-05-09 05:17:28 +04:00
|
|
|
value, err = a.db.Get(itemKey)
|
2014-05-06 09:32:38 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
t.Delete(itemKey)
|
2014-05-06 09:32:38 +04:00
|
|
|
seq += delta
|
|
|
|
|
|
|
|
size--
|
|
|
|
if size <= 0 {
|
2014-05-09 05:17:28 +04:00
|
|
|
t.Delete(metaKey)
|
2014-05-06 09:32:38 +04:00
|
|
|
} else {
|
2014-05-09 05:17:28 +04:00
|
|
|
if whereSeq == listHeadSeq {
|
|
|
|
headSeq = seq
|
|
|
|
} else {
|
|
|
|
tailSeq = seq
|
|
|
|
}
|
|
|
|
|
|
|
|
a.list_setMeta(metaKey, headSeq, tailSeq, size)
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//todo add binlog
|
|
|
|
err = t.Commit()
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func (a *App) list_range(key []byte, start int32, stop int32) ([]interface{}, error) {
|
2014-05-06 09:32:38 +04:00
|
|
|
v := make([]interface{}, 0, 16)
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
var startSeq int32
|
|
|
|
var stopSeq int32
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
if start > stop {
|
|
|
|
return []interface{}{}, nil
|
2014-05-09 05:17:28 +04:00
|
|
|
}
|
2014-05-06 09:32:38 +04:00
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
headSeq, tailSeq, _, err := a.list_getMeta(encode_lmeta_key(key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-06 09:32:38 +04:00
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
if start >= 0 && stop >= 0 {
|
|
|
|
startSeq = headSeq + start
|
|
|
|
stopSeq = headSeq + stop
|
2014-05-06 09:32:38 +04:00
|
|
|
} else if start < 0 && stop < 0 {
|
2014-05-09 05:17:28 +04:00
|
|
|
startSeq = tailSeq + start + 1
|
|
|
|
stopSeq = tailSeq + stop + 1
|
2014-05-06 09:32:38 +04:00
|
|
|
} else {
|
|
|
|
//start < 0 && stop > 0
|
2014-05-09 05:17:28 +04:00
|
|
|
startSeq = tailSeq + start + 1
|
|
|
|
stopSeq = headSeq + stop
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if startSeq < listMinSeq {
|
|
|
|
startSeq = listMinSeq
|
|
|
|
} else if stopSeq > listMaxSeq {
|
|
|
|
stopSeq = listMaxSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
it := a.db.Iterator(encode_list_key(key, startSeq),
|
2014-05-08 06:54:33 +04:00
|
|
|
encode_list_key(key, stopSeq), leveldb.RangeClose, 0, -1)
|
2014-05-06 09:32:38 +04:00
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
v = append(v, it.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
it.Close()
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func (a *App) list_index(key []byte, index int32) ([]byte, error) {
|
|
|
|
var seq int32
|
|
|
|
headSeq, tailSeq, _, err := a.list_getMeta(encode_lmeta_key(key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-06 09:32:38 +04:00
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
if index >= 0 {
|
|
|
|
seq = headSeq + index
|
2014-05-06 09:32:38 +04:00
|
|
|
} else {
|
2014-05-09 05:17:28 +04:00
|
|
|
seq = tailSeq + index + 1
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.db.Get(encode_list_key(key, seq))
|
|
|
|
}
|
2014-05-12 11:08:59 +04:00
|
|
|
|
|
|
|
func (a *App) list_clear(key []byte) (int64, error) {
|
|
|
|
mk := encode_lmeta_key(key)
|
|
|
|
|
|
|
|
t := a.listTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
metaKey := encode_lmeta_key(key)
|
|
|
|
headSeq, tailSeq, _, err := a.list_getMeta(metaKey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var num int64 = 0
|
|
|
|
it := a.db.Iterator(encode_list_key(key, headSeq),
|
|
|
|
encode_list_key(key, tailSeq), leveldb.RangeClose, 0, -1)
|
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
t.Delete(it.Key())
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
|
|
|
|
it.Close()
|
|
|
|
|
|
|
|
t.Delete(mk)
|
|
|
|
|
|
|
|
err = t.Commit()
|
|
|
|
return num, err
|
|
|
|
}
|