ledisdb/ledis/t_list.go

402 lines
7.4 KiB
Go
Raw Normal View History

2014-05-09 10:49:22 +04:00
package ledis
2014-05-06 09:32:38 +04:00
import (
"encoding/binary"
"errors"
"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-20 04:41:24 +04:00
func (db *DB) lEncodeMetaKey(key []byte) []byte {
buf := make([]byte, len(key)+2)
buf[0] = db.index
buf[1] = lMetaType
2014-05-06 09:32:38 +04:00
2014-05-20 04:41:24 +04:00
copy(buf[2:], key)
2014-05-06 09:32:38 +04:00
return buf
}
2014-05-20 04:41:24 +04:00
func (db *DB) lDecodeMetaKey(ek []byte) ([]byte, error) {
if len(ek) < 2 || ek[0] != db.index || ek[1] != lMetaType {
2014-05-09 05:17:28 +04:00
return nil, errLMetaKey
2014-05-06 09:32:38 +04:00
}
2014-05-20 04:41:24 +04:00
return ek[2:], nil
2014-05-06 09:32:38 +04:00
}
2014-05-20 04:41:24 +04:00
func (db *DB) lEncodeListKey(key []byte, seq int32) []byte {
buf := make([]byte, len(key)+8)
2014-05-06 09:32:38 +04:00
pos := 0
2014-05-20 04:41:24 +04:00
buf[pos] = db.index
pos++
buf[pos] = listType
2014-05-06 09:32:38 +04:00
pos++
2014-05-20 04:41:24 +04:00
binary.BigEndian.PutUint16(buf[pos:], uint16(len(key)))
pos += 2
2014-05-06 09:32:38 +04:00
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-20 04:41:24 +04:00
func (db *DB) lDecodeListKey(ek []byte) (key []byte, seq int32, err error) {
if len(ek) < 8 || ek[0] != db.index || ek[1] != listType {
2014-05-06 09:32:38 +04:00
err = errListKey
return
}
2014-05-20 04:41:24 +04:00
keyLen := int(binary.BigEndian.Uint16(ek[2:]))
if keyLen+8 != len(ek) {
2014-05-06 09:32:38 +04:00
err = errListKey
return
}
2014-05-20 04:41:24 +04:00
key = ek[4 : 4+keyLen]
seq = int32(binary.BigEndian.Uint32(ek[4+keyLen:]))
2014-05-06 09:32:38 +04:00
return
}
2014-05-16 04:56:32 +04:00
func (db *DB) lpush(key []byte, whereSeq int32, args ...[]byte) (int64, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return 0, err
}
var headSeq int32
var tailSeq int32
var size int32
var err error
metaKey := db.lEncodeMetaKey(key)
headSeq, tailSeq, size, err = db.lGetMeta(metaKey)
if err != nil {
return 0, err
2014-05-16 04:56:32 +04:00
}
var pushCnt int = len(args)
if pushCnt == 0 {
return int64(size), nil
2014-05-06 09:32:38 +04:00
}
var seq int32 = headSeq
var delta int32 = -1
if whereSeq == listTailSeq {
2014-05-09 05:17:28 +04:00
seq = tailSeq
delta = 1
2014-05-06 09:32:38 +04:00
}
t := db.listTx
t.Lock()
defer t.Unlock()
// append elements
if size > 0 {
2014-05-06 09:32:38 +04:00
seq += delta
}
for i := 0; i < pushCnt; i++ {
2014-05-20 04:41:24 +04:00
ek := db.lEncodeListKey(key, seq+int32(i)*delta)
t.Put(ek, args[i])
2014-05-06 09:32:38 +04:00
}
seq += int32(pushCnt-1) * delta
2014-05-06 09:32:38 +04:00
if seq <= listMinSeq || seq >= listMaxSeq {
return 0, errListSeq
}
// set meta info
2014-05-09 05:17:28 +04:00
if whereSeq == listHeadSeq {
headSeq = seq
} else {
tailSeq = seq
}
db.lSetMeta(metaKey, headSeq, tailSeq)
2014-05-06 09:32:38 +04:00
err = t.Commit()
return int64(size) + int64(pushCnt), err
2014-05-06 09:32:38 +04:00
}
func (db *DB) lpop(key []byte, whereSeq int32) ([]byte, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return nil, err
}
t := db.listTx
2014-05-06 09:32:38 +04:00
t.Lock()
defer t.Unlock()
2014-05-20 04:41:24 +04:00
var headSeq int32
var tailSeq int32
var err error
metaKey := db.lEncodeMetaKey(key)
headSeq, tailSeq, _, err = db.lGetMeta(metaKey)
2014-05-06 09:32:38 +04:00
if err != nil {
return nil, err
}
var value []byte
var seq int32 = headSeq
if whereSeq == listTailSeq {
2014-05-09 05:17:28 +04:00
seq = tailSeq
}
2014-05-20 04:41:24 +04:00
itemKey := db.lEncodeListKey(key, seq)
value, err = db.db.Get(itemKey)
2014-05-06 09:32:38 +04:00
if err != nil {
return nil, err
}
if whereSeq == listHeadSeq {
headSeq += 1
2014-05-06 09:32:38 +04:00
} else {
tailSeq -= 1
2014-05-06 09:32:38 +04:00
}
t.Delete(itemKey)
db.lSetMeta(metaKey, headSeq, tailSeq)
2014-05-06 09:32:38 +04:00
err = t.Commit()
return value, err
}
func (db *DB) lDelete(t *tx, key []byte) int64 {
mk := db.lEncodeMetaKey(key)
var headSeq int32
var tailSeq int32
var err error
headSeq, tailSeq, _, err = db.lGetMeta(mk)
if err != nil {
return 0
}
var num int64 = 0
startKey := db.lEncodeListKey(key, headSeq)
stopKey := db.lEncodeListKey(key, tailSeq)
it := db.db.Iterator(startKey, stopKey, leveldb.RangeClose, 0, -1)
for ; it.Valid(); it.Next() {
t.Delete(it.Key())
num++
}
it.Close()
t.Delete(mk)
return num
}
func (db *DB) lGetSeq(key []byte, whereSeq int32) (int64, error) {
2014-05-20 04:41:24 +04:00
ek := db.lEncodeListKey(key, whereSeq)
return Int64(db.db.Get(ek))
}
func (db *DB) lGetMeta(ek []byte) (headSeq int32, tailSeq int32, size int32, err error) {
var v []byte
v, err = db.db.Get(ek)
if err != nil {
return
} else if v == nil {
headSeq = listInitialSeq
tailSeq = listInitialSeq
size = 0
return
} else {
headSeq = int32(binary.LittleEndian.Uint32(v[0:4]))
tailSeq = int32(binary.LittleEndian.Uint32(v[4:8]))
size = tailSeq - headSeq + 1
}
return
}
func (db *DB) lSetMeta(ek []byte, headSeq int32, tailSeq int32) {
t := db.listTx
var size int32 = tailSeq - headSeq + 1
if size < 0 {
// todo : log error + panic
} else if size == 0 {
t.Delete(ek)
} else {
buf := make([]byte, 8)
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 (db *DB) LIndex(key []byte, index int32) ([]byte, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return nil, err
}
var seq int32
2014-05-20 04:41:24 +04:00
var headSeq int32
var tailSeq int32
var err error
metaKey := db.lEncodeMetaKey(key)
headSeq, tailSeq, _, err = db.lGetMeta(metaKey)
if err != nil {
return nil, err
}
if index >= 0 {
seq = headSeq + index
} else {
seq = tailSeq + index + 1
}
2014-05-20 04:41:24 +04:00
sk := db.lEncodeListKey(key, seq)
return db.db.Get(sk)
}
func (db *DB) LLen(key []byte) (int64, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return 0, err
}
ek := db.lEncodeMetaKey(key)
_, _, size, err := db.lGetMeta(ek)
return int64(size), err
}
func (db *DB) LPop(key []byte) ([]byte, error) {
return db.lpop(key, listHeadSeq)
}
2014-05-16 04:56:32 +04:00
func (db *DB) LPush(key []byte, args ...[]byte) (int64, error) {
return db.lpush(key, listHeadSeq, args...)
}
func (db *DB) LRange(key []byte, start int32, stop int32) ([]interface{}, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return nil, err
}
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-20 04:41:24 +04:00
var headSeq int32
var tailSeq int32
var err error
metaKey := db.lEncodeMetaKey(key)
if headSeq, tailSeq, _, err = db.lGetMeta(metaKey); err != nil {
2014-05-09 05:17:28 +04:00
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
}
2014-05-20 04:41:24 +04:00
startKey := db.lEncodeListKey(key, startSeq)
stopKey := db.lEncodeListKey(key, stopSeq)
it := db.db.Iterator(startKey, stopKey, 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
}
func (db *DB) RPop(key []byte) ([]byte, error) {
return db.lpop(key, listTailSeq)
}
2014-05-06 09:32:38 +04:00
2014-05-16 04:56:32 +04:00
func (db *DB) RPush(key []byte, args ...[]byte) (int64, error) {
return db.lpush(key, listTailSeq, args...)
2014-05-06 09:32:38 +04:00
}
2014-05-12 11:08:59 +04:00
func (db *DB) LClear(key []byte) (int64, error) {
2014-05-20 04:41:24 +04:00
if err := checkKeySize(key); err != nil {
return 0, err
}
t := db.listTx
2014-05-12 11:08:59 +04:00
t.Lock()
defer t.Unlock()
num := db.lDelete(t, key)
2014-05-12 11:08:59 +04:00
err := t.Commit()
2014-05-12 11:08:59 +04:00
return num, err
}
func (db *DB) LFlush() (drop int64, err error) {
t := db.listTx
t.Lock()
defer t.Unlock()
minKey := make([]byte, 2)
minKey[0] = db.index
minKey[1] = listType
maxKey := make([]byte, 2)
maxKey[0] = db.index
maxKey[1] = lMetaType + 1
it := db.db.Iterator(minKey, maxKey, leveldb.RangeROpen, 0, -1)
for ; it.Valid(); it.Next() {
t.Delete(it.Key())
drop++
if drop&1023 == 0 {
if err = t.Commit(); err != nil {
return
}
}
}
it.Close()
err = t.Commit()
return
}