add unit test

This commit is contained in:
wenyekui 2014-08-16 15:35:05 +08:00
parent 928b4a03ae
commit 4b43f0bf7e
10 changed files with 228 additions and 72 deletions

View File

@ -68,7 +68,9 @@ func (db *DB) flushType(t *tx, dataType byte) (drop int64, err error) {
case ZSetType:
deleteFunc = db.zDelete
metaDataType = ZSizeType
case BitType:
deleteFunc = db.bDelete
metaDataType = BitMetaType
default:
return 0, fmt.Errorf("invalid data type: %s", TypeName[dataType])
}
@ -77,12 +79,15 @@ func (db *DB) flushType(t *tx, dataType byte) (drop int64, err error) {
keys, err = db.scan(metaDataType, nil, 1024, false)
for len(keys) != 0 || err != nil {
for _, key := range keys {
drop += deleteFunc(t, key)
deleteFunc(t, key)
db.rmExpire(t, dataType, key)
}
if err = t.Commit(); err != nil {
return
} else {
drop += int64(len(keys))
}
keys, err = db.scan(metaDataType, nil, 1024, false)
}

View File

@ -917,19 +917,5 @@ func (db *DB) bFlush() (drop int64, err error) {
t.Lock()
defer t.Unlock()
var startKey []byte = nil
var keys [][]byte
for keys, err = db.BScan(startKey, 1024, false); len(keys) != 0 || err != nil; {
for _, key := range keys {
drop += db.bDelete(t, key)
db.rmExpire(t, BitType, key)
}
if err = t.Commit(); err != nil {
return
}
startKey = keys[len(keys)-1]
keys, err = db.BScan(startKey, 1024, false)
}
return
return db.flushType(t, BitType)
}

View File

@ -1,6 +1,7 @@
package ledis
import (
"encoding/binary"
"testing"
)
@ -536,3 +537,56 @@ func testBitExpire(t *testing.T) {
t.Fatal(false)
}
}
func testBFlush(t *testing.T) {
db := getTestDB()
db.FlushAll()
for i := 0; i < 2000; i++ {
key := []byte{}
binary.LittleEndian.PutUint32(key, uint32(i))
if _, err := db.BSetBit(key, 1, 1); err != nil {
t.Fatal(err.Error())
}
}
if v, err := db.BScan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 2000 {
t.Fatal("invalid value ", len(v))
}
for i := 0; i < 2000; i++ {
key := []byte{}
binary.LittleEndian.PutUint32(key, uint32(i))
if v, err := db.BGetBit(key, 1); err != nil {
t.Fatal(err.Error())
} else if v != 1 {
t.Fatal("invalid value ", v)
}
}
if n, err := db.bFlush(); err != nil {
t.Fatal(err.Error())
} else if n != 2000 {
t.Fatal("invalid value ", n)
}
if v, err := db.BScan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if v != nil {
t.Fatal("invalid value length ", len(v))
}
for i := 0; i < 2000; i++ {
key := []byte{}
binary.LittleEndian.PutUint32(key, uint32(i))
if v, err := db.BGet(key); err != nil {
t.Fatal(err.Error())
} else if v != nil {
t.Fatal("invalid value ", v)
}
}
}

View File

@ -453,13 +453,10 @@ func (db *DB) HMclear(keys ...[]byte) (int64, error) {
}
func (db *DB) hFlush() (drop int64, err error) {
t := db.kvTx
t := db.hashTx
t.Lock()
defer t.Unlock()
// return db.flushAll(t *tx, dataType byte, expDataType byte)
return
return db.flushType(t, HashType)
}
func (db *DB) HScan(key []byte, count int, inclusive bool) ([][]byte, error) {

View File

@ -1,6 +1,7 @@
package ledis
import (
"fmt"
"testing"
)
@ -78,3 +79,55 @@ func TestHashPersist(t *testing.T) {
t.Fatal(n)
}
}
func TestHFlush(t *testing.T) {
db := getTestDB()
db.FlushAll()
for i := 0; i < 2000; i++ {
key := fmt.Sprintf("%d", i)
if _, err := db.HSet([]byte(key), []byte("f"), []byte("v")); err != nil {
t.Fatal(err.Error())
}
}
if v, err := db.HScan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 2000 {
t.Fatal("invalid value ", len(v))
}
for i := 0; i < 2000; i++ {
key := fmt.Sprintf("%d", i)
if v, err := db.HGet([]byte(key), []byte("f")); err != nil {
t.Fatal(err.Error())
} else if string(v) != "v" {
t.Fatal("invalid value ", v)
}
}
if n, err := db.hFlush(); err != nil {
t.Fatal(err.Error())
} else if n != 2000 {
t.Fatal("invalid value ", n)
}
if v, err := db.HScan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 0 {
t.Fatal("invalid value length ", len(v))
}
for i := 0; i < 2000; i++ {
key := []byte(fmt.Sprintf("%d", i))
if v, err := db.HGet(key, []byte("f")); err != nil {
t.Fatal(err.Error())
} else if v != nil {
t.Fatal("invalid value ", v)
}
}
}

View File

@ -308,18 +308,10 @@ func (db *DB) SetNX(key []byte, value []byte) (int64, error) {
}
func (db *DB) flush() (drop int64, err error) {
minKey := db.encodeKVMinKey()
maxKey := db.encodeKVMaxKey()
t := db.kvTx
t.Lock()
defer t.Unlock()
drop, err = db.flushRegion(t, minKey, maxKey)
err = db.expFlush(t, KVType)
err = t.Commit()
return
return db.flushType(t, KVType)
}
//if inclusive is true, scan range [key, inf) else (key, inf)

View File

@ -1,6 +1,7 @@
package ledis
import (
"fmt"
"testing"
)
@ -65,3 +66,53 @@ func TestKVPersist(t *testing.T) {
t.Fatal(n)
}
}
func TestKVFlush(t *testing.T) {
db := getTestDB()
db.FlushAll()
for i := 0; i < 2000; i++ {
key := fmt.Sprintf("%d", i)
if err := db.Set([]byte(key), []byte("v")); err != nil {
t.Fatal(err.Error())
}
}
if v, err := db.Scan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 2000 {
t.Fatal("invalid value ", len(v))
}
for i := 0; i < 2000; i++ {
key := fmt.Sprintf("%d", i)
if v, err := db.Get([]byte(key)); err != nil {
t.Fatal(err.Error())
} else if string(v) != "v" {
t.Fatal("invalid value ", v)
}
}
if n, err := db.flush(); err != nil {
t.Fatal(err.Error())
} else if n != 2000 {
t.Fatal("invalid value ", n)
}
if v, err := db.Scan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 0 {
t.Fatal("invalid value length ", len(v))
}
for i := 0; i < 2000; i++ {
key := []byte(fmt.Sprintf("%d", i))
if v, err := db.Get(key); err != nil {
t.Fatal(err.Error())
} else if v != nil {
t.Fatal("invalid value ", v)
}
}
}

View File

@ -424,23 +424,10 @@ func (db *DB) LMclear(keys ...[]byte) (int64, error) {
}
func (db *DB) lFlush() (drop int64, err error) {
t := db.binTx
t := db.listTx
t.Lock()
defer t.Unlock()
var startKey []byte = nil
var keys [][]byte
for keys, err = db.LScan(startKey, 1024, false); len(keys) != 0 || err != nil; {
var num int64
if num, err = db.LMclear(keys...); err != nil {
return
} else {
drop += num
}
startKey = keys[len(keys)-1]
keys, err = db.LScan(startKey, 1024, false)
}
return
return db.flushType(t, ListType)
}
func (db *DB) LExpire(key []byte, duration int64) (int64, error) {

View File

@ -1,6 +1,7 @@
package ledis
import (
"fmt"
"testing"
)
@ -100,3 +101,58 @@ func TestListPersist(t *testing.T) {
t.Fatal(n)
}
}
func TestLFlush(t *testing.T) {
db := getTestDB()
db.FlushAll()
for i := 0; i < 2000; i++ {
key := fmt.Sprintf("%d", i)
if _, err := db.LPush([]byte(key), []byte("v")); err != nil {
t.Fatal(err.Error())
}
}
if v, err := db.LScan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 2000 {
t.Fatal("invalid value ", len(v))
}
if n, err := db.lFlush(); err != nil {
t.Fatal(err.Error())
} else if n != 2000 {
t.Fatal("invalid value ", n)
}
if v, err := db.LScan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 0 {
t.Fatal("invalid value length ", len(v))
}
for i := 0; i < 2000; i++ {
key := fmt.Sprintf("%d", i)
if _, err := db.ZAdd([]byte(key), ScorePair{1, []byte("v")}); err != nil {
t.Fatal(err.Error())
}
}
if v, err := db.ZScan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 2000 {
t.Fatal("invalid value ", len(v))
}
if n, err := db.zFlush(); err != nil {
t.Fatal(err.Error())
} else if n != 2000 {
t.Fatal("invalid value ", n)
}
if v, err := db.ZScan(nil, 3000, true); err != nil {
t.Fatal(err.Error())
} else if len(v) != 0 {
t.Fatal("invalid value length ", len(v))
}
}

View File

@ -738,32 +738,7 @@ func (db *DB) zFlush() (drop int64, err error) {
t := db.zsetTx
t.Lock()
defer t.Unlock()
minKey := make([]byte, 2)
minKey[0] = db.index
minKey[1] = ZSetType
maxKey := make([]byte, 2)
maxKey[0] = db.index
maxKey[1] = ZScoreType + 1
it := db.db.RangeLimitIterator(minKey, maxKey, store.RangeROpen, 0, -1)
defer it.Close()
for ; it.Valid(); it.Next() {
t.Delete(it.RawKey())
drop++
if drop&1023 == 0 {
if err = t.Commit(); err != nil {
return
}
}
}
db.expFlush(t, ZSetType)
err = t.Commit()
return
return db.flushType(t, ZSetType)
}
func (db *DB) ZExpire(key []byte, duration int64) (int64, error) {