bugfix list pop

This commit is contained in:
siddontang 2015-05-14 09:20:56 +08:00
parent 1df83b61b4
commit 5d7df5c773
2 changed files with 59 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"github.com/siddontang/go/log"
"github.com/siddontang/ledisdb/store" "github.com/siddontang/ledisdb/store"
) )
@ -174,12 +175,15 @@ func (db *DB) lpop(key []byte, whereSeq int32) ([]byte, error) {
var headSeq int32 var headSeq int32
var tailSeq int32 var tailSeq int32
var size int32
var err error var err error
metaKey := db.lEncodeMetaKey(key) metaKey := db.lEncodeMetaKey(key)
headSeq, tailSeq, _, err = db.lGetMeta(nil, metaKey) headSeq, tailSeq, size, err = db.lGetMeta(nil, metaKey)
if err != nil { if err != nil {
return nil, err return nil, err
} else if size == 0 {
return nil, nil
} }
var value []byte var value []byte
@ -202,9 +206,9 @@ func (db *DB) lpop(key []byte, whereSeq int32) ([]byte, error) {
} }
t.Delete(itemKey) t.Delete(itemKey)
size := db.lSetMeta(metaKey, headSeq, tailSeq) size = db.lSetMeta(metaKey, headSeq, tailSeq)
if size == 0 { if size == 0 {
db.rmExpire(t, HashType, key) db.rmExpire(t, ListType, key)
} }
err = t.Commit() err = t.Commit()
@ -271,6 +275,7 @@ func (db *DB) lSetMeta(ek []byte, headSeq int32, tailSeq int32) int32 {
var size int32 = tailSeq - headSeq + 1 var size int32 = tailSeq - headSeq + 1
if size < 0 { if size < 0 {
// todo : log error + panic // todo : log error + panic
log.Fatalf("invalid meta sequence range [%d, %d]", headSeq, tailSeq)
} else if size == 0 { } else if size == 0 {
t.Delete(ek) t.Delete(ek)
} else { } else {

View File

@ -179,3 +179,54 @@ func TestLKeyExists(t *testing.T) {
t.Fatal("invalid value ", n) t.Fatal("invalid value ", n)
} }
} }
func TestListPop(t *testing.T) {
db := getTestDB()
key := []byte("lpop_test")
if v, err := db.LPop(key); err != nil {
t.Fatal(err)
} else if v != nil {
t.Fatal(v)
}
if s, err := db.LLen(key); err != nil {
t.Fatal(err)
} else if s != 0 {
t.Fatal(s)
}
for i := 0; i < 10; i++ {
if n, err := db.LPush(key, []byte("a")); err != nil {
t.Fatal(err)
} else if n != 1+int64(i) {
t.Fatal(n)
}
}
if s, err := db.LLen(key); err != nil {
t.Fatal(err)
} else if s != 10 {
t.Fatal(s)
}
for i := 0; i < 10; i++ {
if _, err := db.LPop(key); err != nil {
t.Fatal(err)
}
}
if s, err := db.LLen(key); err != nil {
t.Fatal(err)
} else if s != 0 {
t.Fatal(s)
}
if v, err := db.LPop(key); err != nil {
t.Fatal(err)
} else if v != nil {
t.Fatal(v)
}
}