2014-05-09 10:49:22 +04:00
|
|
|
package ledis
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2014-07-25 13:58:00 +04:00
|
|
|
"github.com/siddontang/ledisdb/store"
|
2014-06-05 12:13:35 +04:00
|
|
|
"time"
|
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
|
2014-07-11 09:27:40 +04:00
|
|
|
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) {
|
2014-07-11 09:27:40 +04:00
|
|
|
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++
|
2014-07-11 09:27:40 +04:00
|
|
|
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) {
|
2014-07-11 09:27:40 +04:00
|
|
|
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
|
|
|
|
|
2014-07-10 07:32:37 +04:00
|
|
|
t := db.listTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
metaKey := db.lEncodeMetaKey(key)
|
2014-07-10 07:32:37 +04:00
|
|
|
headSeq, tailSeq, size, err = db.lGetMeta(nil, metaKey)
|
2014-06-03 11:40:10 +04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-05-16 04:56:32 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
var pushCnt int = len(args)
|
|
|
|
if pushCnt == 0 {
|
|
|
|
return int64(size), nil
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
var seq int32 = headSeq
|
|
|
|
var delta int32 = -1
|
|
|
|
if whereSeq == listTailSeq {
|
2014-05-09 05:17:28 +04:00
|
|
|
seq = tailSeq
|
2014-06-03 11:40:10 +04:00
|
|
|
delta = 1
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
// append elements
|
|
|
|
if size > 0 {
|
2014-05-06 09:32:38 +04:00
|
|
|
seq += delta
|
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
seq += int32(pushCnt-1) * delta
|
2014-05-06 09:32:38 +04:00
|
|
|
if seq <= listMinSeq || seq >= listMaxSeq {
|
|
|
|
return 0, errListSeq
|
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
// set meta info
|
2014-05-09 05:17:28 +04:00
|
|
|
if whereSeq == listHeadSeq {
|
|
|
|
headSeq = seq
|
|
|
|
} else {
|
|
|
|
tailSeq = seq
|
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
db.lSetMeta(metaKey, headSeq, tailSeq)
|
2014-05-06 09:32:38 +04:00
|
|
|
|
|
|
|
err = t.Commit()
|
2014-06-03 11:40:10 +04:00
|
|
|
return int64(size) + int64(pushCnt), err
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +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
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
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)
|
2014-07-10 07:32:37 +04:00
|
|
|
headSeq, tailSeq, _, err = db.lGetMeta(nil, metaKey)
|
2014-05-06 09:32:38 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
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)
|
2014-05-15 10:19:48 +04:00
|
|
|
value, err = db.db.Get(itemKey)
|
2014-05-06 09:32:38 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
if whereSeq == listHeadSeq {
|
|
|
|
headSeq += 1
|
2014-05-06 09:32:38 +04:00
|
|
|
} else {
|
2014-06-03 11:40:10 +04:00
|
|
|
tailSeq -= 1
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
t.Delete(itemKey)
|
2014-06-05 12:13:35 +04:00
|
|
|
size := db.lSetMeta(metaKey, headSeq, tailSeq)
|
|
|
|
if size == 0 {
|
2014-07-11 09:27:40 +04:00
|
|
|
db.rmExpire(t, HashType, key)
|
2014-06-05 12:13:35 +04:00
|
|
|
}
|
2014-06-03 11:40:10 +04:00
|
|
|
|
2014-05-06 09:32:38 +04:00
|
|
|
err = t.Commit()
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
2014-06-05 12:13:35 +04:00
|
|
|
// ps : here just focus on deleting the list data,
|
|
|
|
// any other likes expire is ignore.
|
2014-06-03 11:40:10 +04:00
|
|
|
func (db *DB) lDelete(t *tx, key []byte) int64 {
|
|
|
|
mk := db.lEncodeMetaKey(key)
|
|
|
|
|
|
|
|
var headSeq int32
|
|
|
|
var tailSeq int32
|
|
|
|
var err error
|
|
|
|
|
2014-07-10 07:32:37 +04:00
|
|
|
it := db.db.NewIterator()
|
|
|
|
defer it.Close()
|
|
|
|
|
|
|
|
headSeq, tailSeq, _, err = db.lGetMeta(it, mk)
|
2014-06-03 11:40:10 +04:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var num int64 = 0
|
|
|
|
startKey := db.lEncodeListKey(key, headSeq)
|
|
|
|
stopKey := db.lEncodeListKey(key, tailSeq)
|
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
rit := store.NewRangeIterator(it, &store.Range{startKey, stopKey, store.RangeClose})
|
2014-07-10 07:32:37 +04:00
|
|
|
for ; rit.Valid(); rit.Next() {
|
|
|
|
t.Delete(rit.RawKey())
|
2014-06-03 11:40:10 +04:00
|
|
|
num++
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Delete(mk)
|
|
|
|
|
|
|
|
return num
|
|
|
|
}
|
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
func (db *DB) lGetMeta(it *store.Iterator, ek []byte) (headSeq int32, tailSeq int32, size int32, err error) {
|
2014-05-15 10:19:48 +04:00
|
|
|
var v []byte
|
2014-07-10 07:32:37 +04:00
|
|
|
if it != nil {
|
|
|
|
v = it.Find(ek)
|
|
|
|
} else {
|
|
|
|
v, err = db.db.Get(ek)
|
|
|
|
}
|
2014-05-15 10:19:48 +04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
} else if v == nil {
|
2014-06-03 11:40:10 +04:00
|
|
|
headSeq = listInitialSeq
|
|
|
|
tailSeq = listInitialSeq
|
2014-05-15 10:19:48 +04:00
|
|
|
size = 0
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
headSeq = int32(binary.LittleEndian.Uint32(v[0:4]))
|
|
|
|
tailSeq = int32(binary.LittleEndian.Uint32(v[4:8]))
|
2014-06-03 11:40:10 +04:00
|
|
|
size = tailSeq - headSeq + 1
|
2014-05-15 10:19:48 +04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-05 12:13:35 +04:00
|
|
|
func (db *DB) lSetMeta(ek []byte, headSeq int32, tailSeq int32) int32 {
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.listTx
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
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)
|
2014-05-15 10:19:48 +04:00
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
binary.LittleEndian.PutUint32(buf[0:4], uint32(headSeq))
|
|
|
|
binary.LittleEndian.PutUint32(buf[4:8], uint32(tailSeq))
|
2014-05-15 10:19:48 +04:00
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
t.Put(ek, buf)
|
|
|
|
}
|
2014-06-05 12:13:35 +04:00
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) lExpireAt(key []byte, when int64) (int64, error) {
|
|
|
|
t := db.listTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
if llen, err := db.LLen(key); err != nil || llen == 0 {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
2014-07-11 09:27:40 +04:00
|
|
|
db.expireAt(t, ListType, key, when)
|
2014-06-05 12:13:35 +04:00
|
|
|
if err := t.Commit(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1, nil
|
2014-05-15 10:19:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
var seq int32
|
2014-05-20 04:41:24 +04:00
|
|
|
var headSeq int32
|
|
|
|
var tailSeq int32
|
|
|
|
var err error
|
|
|
|
|
|
|
|
metaKey := db.lEncodeMetaKey(key)
|
|
|
|
|
2014-07-10 07:32:37 +04:00
|
|
|
it := db.db.NewIterator()
|
|
|
|
defer it.Close()
|
|
|
|
|
|
|
|
headSeq, tailSeq, _, err = db.lGetMeta(it, metaKey)
|
2014-05-15 10:19:48 +04:00
|
|
|
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)
|
2014-07-10 07:32:37 +04:00
|
|
|
v := it.Find(sk)
|
|
|
|
|
|
|
|
return v, nil
|
2014-05-15 10:19:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2014-07-10 07:32:37 +04:00
|
|
|
_, _, size, err := db.lGetMeta(nil, ek)
|
2014-05-15 10:19:48 +04:00
|
|
|
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...)
|
2014-05-15 10:19:48 +04:00
|
|
|
}
|
2015-12-01 03:50:42 +03:00
|
|
|
func (db *DB) LSet(key []byte, index int32, value []byte) error {
|
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var seq int32
|
|
|
|
var headSeq int32
|
|
|
|
var tailSeq int32
|
|
|
|
//var size int32
|
|
|
|
var err error
|
|
|
|
t := db.listBatch
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
metaKey := db.lEncodeMetaKey(key)
|
|
|
|
|
|
|
|
headSeq, tailSeq, _, err = db.lGetMeta(nil, metaKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if index >= 0 {
|
|
|
|
seq = headSeq + index
|
|
|
|
} else {
|
|
|
|
seq = tailSeq + index + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
sk := db.lEncodeListKey(key, seq)
|
|
|
|
t.Put(sk, value)
|
|
|
|
err = t.Commit()
|
|
|
|
return err
|
|
|
|
}
|
2014-05-15 10:19:48 +04:00
|
|
|
|
2014-06-22 11:25:51 +04:00
|
|
|
func (db *DB) LRange(key []byte, start int32, stop int32) ([][]byte, error) {
|
2014-05-20 04:41:24 +04:00
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var headSeq int32
|
2014-07-01 05:21:14 +04:00
|
|
|
var llen int32
|
2014-05-20 04:41:24 +04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
metaKey := db.lEncodeMetaKey(key)
|
|
|
|
|
2014-07-10 07:32:37 +04:00
|
|
|
it := db.db.NewIterator()
|
|
|
|
defer it.Close()
|
|
|
|
|
|
|
|
if headSeq, _, llen, err = db.lGetMeta(it, metaKey); err != nil {
|
2014-05-09 05:17:28 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-06 09:32:38 +04:00
|
|
|
|
2014-07-01 05:21:14 +04:00
|
|
|
if start < 0 {
|
|
|
|
start = llen + start
|
|
|
|
}
|
|
|
|
if stop < 0 {
|
|
|
|
stop = llen + stop
|
|
|
|
}
|
|
|
|
if start < 0 {
|
|
|
|
start = 0
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-07-01 05:21:14 +04:00
|
|
|
if start > stop || start >= llen {
|
|
|
|
return [][]byte{}, nil
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
2014-07-01 05:21:14 +04:00
|
|
|
if stop >= llen {
|
|
|
|
stop = llen - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
limit := (stop - start) + 1
|
|
|
|
headSeq += start
|
|
|
|
|
|
|
|
v := make([][]byte, 0, limit)
|
|
|
|
|
|
|
|
startKey := db.lEncodeListKey(key, headSeq)
|
2014-07-25 13:58:00 +04:00
|
|
|
rit := store.NewRangeLimitIterator(it,
|
|
|
|
&store.Range{
|
2014-07-10 07:32:37 +04:00
|
|
|
Min: startKey,
|
|
|
|
Max: nil,
|
2014-07-25 13:58:00 +04:00
|
|
|
Type: store.RangeClose},
|
|
|
|
&store.Limit{
|
2014-07-10 07:32:37 +04:00
|
|
|
Offset: 0,
|
|
|
|
Count: int(limit)})
|
|
|
|
|
|
|
|
for ; rit.Valid(); rit.Next() {
|
|
|
|
v = append(v, rit.Value())
|
2014-05-06 09:32:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
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
|
|
|
|
2014-05-15 10:19:48 +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
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:19:48 +04:00
|
|
|
t := db.listTx
|
2014-05-12 11:08:59 +04:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
num := db.lDelete(t, key)
|
2014-07-11 09:27:40 +04:00
|
|
|
db.rmExpire(t, ListType, key)
|
2014-05-12 11:08:59 +04:00
|
|
|
|
2014-06-03 11:40:10 +04:00
|
|
|
err := t.Commit()
|
2014-05-12 11:08:59 +04:00
|
|
|
return num, err
|
|
|
|
}
|
2014-05-22 15:59:40 +04:00
|
|
|
|
2014-06-27 12:28:26 +04:00
|
|
|
func (db *DB) LMclear(keys ...[]byte) (int64, error) {
|
|
|
|
t := db.listTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.lDelete(t, key)
|
2014-07-11 09:27:40 +04:00
|
|
|
db.rmExpire(t, ListType, key)
|
2014-06-27 12:28:26 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err := t.Commit()
|
|
|
|
return int64(len(keys)), err
|
|
|
|
}
|
|
|
|
|
2014-06-12 06:51:36 +04:00
|
|
|
func (db *DB) lFlush() (drop int64, err error) {
|
2014-05-22 15:59:40 +04:00
|
|
|
minKey := make([]byte, 2)
|
|
|
|
minKey[0] = db.index
|
2014-07-11 09:27:40 +04:00
|
|
|
minKey[1] = ListType
|
2014-05-22 15:59:40 +04:00
|
|
|
|
|
|
|
maxKey := make([]byte, 2)
|
|
|
|
maxKey[0] = db.index
|
2014-07-11 09:27:40 +04:00
|
|
|
maxKey[1] = LMetaType + 1
|
2014-05-22 15:59:40 +04:00
|
|
|
|
2014-07-03 07:38:52 +04:00
|
|
|
t := db.listTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2014-05-22 15:59:40 +04:00
|
|
|
|
2014-07-03 07:38:52 +04:00
|
|
|
drop, err = db.flushRegion(t, minKey, maxKey)
|
2014-07-11 09:27:40 +04:00
|
|
|
err = db.expFlush(t, ListType)
|
2014-06-05 12:13:35 +04:00
|
|
|
|
2014-05-22 15:59:40 +04:00
|
|
|
err = t.Commit()
|
|
|
|
return
|
|
|
|
}
|
2014-06-05 12:13:35 +04:00
|
|
|
|
|
|
|
func (db *DB) LExpire(key []byte, duration int64) (int64, error) {
|
|
|
|
if duration <= 0 {
|
|
|
|
return 0, errExpireValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.lExpireAt(key, time.Now().Unix()+duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) LExpireAt(key []byte, when int64) (int64, error) {
|
|
|
|
if when <= time.Now().Unix() {
|
|
|
|
return 0, errExpireValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.lExpireAt(key, when)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) LTTL(key []byte) (int64, error) {
|
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2014-07-11 09:27:40 +04:00
|
|
|
return db.ttl(ListType, key)
|
2014-06-05 12:13:35 +04:00
|
|
|
}
|
2014-06-23 07:12:20 +04:00
|
|
|
|
|
|
|
func (db *DB) LPersist(key []byte) (int64, error) {
|
|
|
|
if err := checkKeySize(key); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := db.listTx
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2014-07-11 09:27:40 +04:00
|
|
|
n, err := db.rmExpire(t, ListType, key)
|
2014-06-23 07:12:20 +04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Commit()
|
|
|
|
return n, err
|
|
|
|
}
|