From 9f22f3e11e6bf0104bc2da01a28f5e2a9ff12afa Mon Sep 17 00:00:00 2001 From: silentsai Date: Thu, 22 May 2014 11:11:50 +0800 Subject: [PATCH] append test cases for t_zset; add 'FlushDB' api into the core --- ledis/ledis.go | 21 ++++++ ledis/t_zset_test.go | 153 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 172 insertions(+), 2 deletions(-) diff --git a/ledis/ledis.go b/ledis/ledis.go index 330f24c..33bd09e 100644 --- a/ledis/ledis.go +++ b/ledis/ledis.go @@ -80,3 +80,24 @@ func (l *Ledis) Select(index int) (*DB, error) { return l.dbs[index], nil } + +func (l *Ledis) FlushDB() (drop int64, err error) { + db := l.ldb + if db == nil { + drop = int64(-1) + return + } + + ftx := &tx{wb: db.NewWriteBatch()} + ftx.Lock() + defer ftx.Unlock() + + it := db.Iterator(nil, nil, leveldb.RangeClose, 0, -1) + for ; it.Valid(); it.Next() { + ftx.Delete(it.Key()) + drop++ + } + + err = ftx.Commit() + return +} diff --git a/ledis/t_zset_test.go b/ledis/t_zset_test.go index 3eed420..57f0698 100644 --- a/ledis/t_zset_test.go +++ b/ledis/t_zset_test.go @@ -1,9 +1,22 @@ package ledis import ( + "fmt" "testing" ) +const ( + EndPos int = -1 +) + +func bin(sz string) []byte { + return []byte(sz) +} + +func pair(memb string, score int) ScorePair { + return ScorePair{int64(score), bin(memb)} +} + func TestZSetCodec(t *testing.T) { db := getTestDB() @@ -42,11 +55,147 @@ func TestZSetCodec(t *testing.T) { func TestDBZSet(t *testing.T) { db := getTestDB() - key := []byte("testdb_zset_a") + key := bin("testdb_zset_a") - if n, err := db.ZAdd(key, ScorePair{1, []byte("a")}, ScorePair{1, []byte("a")}); err != nil { + // {'a':0, 'b':1, 'c':2, 'd':3} + if n, err := db.ZAdd(key, pair("a", 0), pair("b", 1), + pair("c", 2), pair("d", 3)); err != nil { + t.Fatal(err) + } else if n != 4 { + t.Fatal(n) + } + + if n, err := db.ZCount(key, 0, 0XFF); err != nil { + t.Fatal(err) + } else if n != 4 { + t.Fatal(n) + } + + // {c':2, 'd':3} + if n, err := db.ZRem(key, bin("a"), bin("b")); err != nil { t.Fatal(err) } else if n != 2 { t.Fatal(n) } + + if n, err := db.ZRem(key, bin("a"), bin("b")); err != nil { + t.Fatal(err) + } else if n != 0 { + t.Fatal(n) + } + + if n, err := db.ZCard(key); err != nil { + t.Fatal(err) + } else if n != 2 { + t.Fatal(n) + } + + // {} + if n, err := db.ZClear(key); err != nil { + t.Fatal(err) + } else if n != 2 { + t.Fatal(n) + } + + if n, err := db.ZCount(key, 0, 0XFF); err != nil { + t.Fatal(err) + } else if n != 0 { + t.Fatal(n) + } +} + +func TestZSetOrder(t *testing.T) { + db := getTestDB() + + key := bin("testdb_zset_order") + + // {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5} + membs := [...]string{"a", "b", "c", "d", "e", "f"} + membCnt := len(membs) + + for i := 0; i < membCnt; i++ { + db.ZAdd(key, pair(membs[i], i)) + } + + if n, _ := db.ZCount(key, 0, 0XFFFF); int(n) != membCnt { + t.Fatal(n) + } + + for i := 0; i < membCnt; i++ { + if pos, err := db.ZRank(key, bin(membs[i])); err != nil { + t.Fatal(err) + } else if int(pos) != i { + t.Fatal(pos) + } + + if pos, err := db.ZRevRank(key, bin(membs[i])); err != nil { + t.Fatal(err) + } else if int(pos) != membCnt-i-1 { + t.Fatal(pos) + } + } + + if qMembs, err := db.ZRange(key, 0, EndPos, false); err != nil { + t.Fatal(err) + } else if len(qMembs) != membCnt { + t.Fatal(fmt.Sprintf("%d vs %d", len(qMembs), membCnt)) + } else { + for i := 0; i < membCnt; i++ { + if memb, ok := qMembs[i].([]byte); !ok { + t.Fatal(ok) + } else if string(memb) != membs[i] { + t.Fatal(fmt.Sprintf("[%s] vs [%s]", qMembs[i], membs[i])) + } + } + } + + // {'a':0, 'b':1, 'c':2, 'd':999, 'e':4, 'f':5} + if n, err := db.ZAdd(key, pair("d", 999)); err != nil { + t.Fatal(err) + } else if n != 0 { + t.Fatal(n) + } + + if pos, _ := db.ZRank(key, bin("d")); int(pos) != membCnt-1 { + t.Fatal(pos) + } + + if pos, _ := db.ZRevRank(key, bin("d")); int(pos) != 0 { + t.Fatal(pos) + } + + if pos, _ := db.ZRank(key, bin("e")); int(pos) != 3 { + t.Fatal(pos) + } + + if pos, _ := db.ZRank(key, bin("f")); int(pos) != 4 { + t.Fatal(pos) + } + + if qMembs, err := db.ZRangeByScore(key, 999, 0XFFFF, false, 0, membCnt); err != nil { + t.Fatal(err) + } else if len(qMembs) != 1 { + t.Fatal(len(qMembs)) + } + + // {'a':0, 'b':1, 'c':2, 'd':999, 'e':6, 'f':5} + if _, err := db.ZIncrBy(key, 2, bin("e")); err != nil { + t.Fatal(err) + } + + if pos, _ := db.ZRank(key, bin("e")); int(pos) != 4 { + t.Fatal(pos) + } + + if pos, _ := db.ZRevRank(key, bin("e")); int(pos) != 1 { + t.Fatal(pos) + } + + if dropCnt, _ := testLedis.FlushDB(); dropCnt <= 0 { + t.Fatal(dropCnt) + } + + if n, _ := db.ZCount(key, 0, 0XFFFF); n > 0 { + t.Fatal(n) + } }