From 4b43f0bf7e4b627280337a8c0912d6b46aac8fbd Mon Sep 17 00:00:00 2001 From: wenyekui Date: Sat, 16 Aug 2014 15:35:05 +0800 Subject: [PATCH] add unit test --- ledis/ledis_db.go | 9 +++++-- ledis/t_bit.go | 16 +------------ ledis/t_bit_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++ ledis/t_hash.go | 7 ++---- ledis/t_hash_test.go | 53 +++++++++++++++++++++++++++++++++++++++++ ledis/t_kv.go | 10 +------- ledis/t_kv_test.go | 51 ++++++++++++++++++++++++++++++++++++++++ ledis/t_list.go | 17 ++------------ ledis/t_list_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ ledis/t_zset.go | 27 +-------------------- 10 files changed, 228 insertions(+), 72 deletions(-) diff --git a/ledis/ledis_db.go b/ledis/ledis_db.go index 55f6dd9..af58927 100644 --- a/ledis/ledis_db.go +++ b/ledis/ledis_db.go @@ -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) } diff --git a/ledis/t_bit.go b/ledis/t_bit.go index 3fcc4c2..b32e77b 100644 --- a/ledis/t_bit.go +++ b/ledis/t_bit.go @@ -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) } diff --git a/ledis/t_bit_test.go b/ledis/t_bit_test.go index 9103523..59355da 100644 --- a/ledis/t_bit_test.go +++ b/ledis/t_bit_test.go @@ -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) + } + } + +} diff --git a/ledis/t_hash.go b/ledis/t_hash.go index 91cf7a3..0782e63 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -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) { diff --git a/ledis/t_hash_test.go b/ledis/t_hash_test.go index bf7cc7c..bc79b06 100644 --- a/ledis/t_hash_test.go +++ b/ledis/t_hash_test.go @@ -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) + } + } + +} diff --git a/ledis/t_kv.go b/ledis/t_kv.go index e94a930..b7bf4d6 100644 --- a/ledis/t_kv.go +++ b/ledis/t_kv.go @@ -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) diff --git a/ledis/t_kv_test.go b/ledis/t_kv_test.go index 714e104..4a2c761 100644 --- a/ledis/t_kv_test.go +++ b/ledis/t_kv_test.go @@ -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) + } + } +} diff --git a/ledis/t_list.go b/ledis/t_list.go index 3233548..a97ce97 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -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) { diff --git a/ledis/t_list_test.go b/ledis/t_list_test.go index 2a2bd2f..eb417ec 100644 --- a/ledis/t_list_test.go +++ b/ledis/t_list_test.go @@ -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)) + } +} diff --git a/ledis/t_zset.go b/ledis/t_zset.go index bed0c6f..0431046 100644 --- a/ledis/t_zset.go +++ b/ledis/t_zset.go @@ -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) {