From bd38e693f25a79cf501efcab49a6602bb48c11a0 Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 26 May 2014 09:21:17 +0800 Subject: [PATCH] bug fix for kv scan, add scan test --- ledis/t_hash_test.go | 30 ++++++++++++++++++++++++++++++ ledis/t_kv.go | 2 +- ledis/t_kv_test.go | 34 ++++++++++++++++++++++++++++++++++ ledis/t_zset_test.go | 27 +++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/ledis/t_hash_test.go b/ledis/t_hash_test.go index 5521ad2..e753361 100644 --- a/ledis/t_hash_test.go +++ b/ledis/t_hash_test.go @@ -38,3 +38,33 @@ func TestDBHash(t *testing.T) { t.Fatal(n) } } + +func TestDBHScan(t *testing.T) { + db := getTestDB() + + db.HFlush() + + key := []byte("a") + db.HSet(key, []byte("1"), []byte{}) + db.HSet(key, []byte("2"), []byte{}) + db.HSet(key, []byte("3"), []byte{}) + + if v, err := db.HScan(key, nil, 1, true); err != nil { + t.Fatal(err) + } else if len(v) != 1 { + t.Fatal(len(v)) + } + + if v, err := db.HScan(key, []byte("1"), 2, false); err != nil { + t.Fatal(err) + } else if len(v) != 2 { + t.Fatal(len(v)) + } + + if v, err := db.HScan(key, nil, 10, true); err != nil { + t.Fatal(err) + } else if len(v) != 3 { + t.Fatal(len(v)) + } + +} diff --git a/ledis/t_kv.go b/ledis/t_kv.go index a4cd1f9..c7e228e 100644 --- a/ledis/t_kv.go +++ b/ledis/t_kv.go @@ -296,7 +296,7 @@ func (db *DB) Scan(key []byte, count int, inclusive bool) ([]KVPair, error) { if err := checkKeySize(key); err != nil { return nil, err } - minKey = key + minKey = db.encodeKVKey(key) } else { minKey = db.encodeKVMinKey() } diff --git a/ledis/t_kv_test.go b/ledis/t_kv_test.go index 9bd21e6..967eab6 100644 --- a/ledis/t_kv_test.go +++ b/ledis/t_kv_test.go @@ -25,3 +25,37 @@ func TestDBKV(t *testing.T) { t.Fatal(err) } } + +func TestDBScan(t *testing.T) { + db := getTestDB() + + db.Flush() + + if v, err := db.Scan(nil, 10, true); err != nil { + t.Fatal(err) + } else if len(v) != 0 { + t.Fatal(len(v)) + } + + db.Set([]byte("a"), []byte{}) + db.Set([]byte("b"), []byte{}) + db.Set([]byte("c"), []byte{}) + + if v, err := db.Scan(nil, 1, true); err != nil { + t.Fatal(err) + } else if len(v) != 1 { + t.Fatal(len(v)) + } + + if v, err := db.Scan([]byte("a"), 2, false); err != nil { + t.Fatal(err) + } else if len(v) != 2 { + t.Fatal(len(v)) + } + + if v, err := db.Scan(nil, 3, true); err != nil { + t.Fatal(err) + } else if len(v) != 3 { + t.Fatal(len(v)) + } +} diff --git a/ledis/t_zset_test.go b/ledis/t_zset_test.go index aa562c0..fa2bb45 100644 --- a/ledis/t_zset_test.go +++ b/ledis/t_zset_test.go @@ -191,3 +191,30 @@ func TestZSetOrder(t *testing.T) { t.Fatal(pos) } } + +func TestDBZScan(t *testing.T) { + db := getTestDB() + + db.ZFlush() + + key := []byte("key") + db.ZAdd(key, pair("a", 0), pair("b", 1), pair("c", 2)) + + if v, err := db.ZScan(key, nil, 1, true); err != nil { + t.Fatal(err) + } else if len(v) != 1 { + t.Fatal(len(v)) + } + + if v, err := db.ZScan(key, []byte("a"), 2, false); err != nil { + t.Fatal(err) + } else if len(v) != 2 { + t.Fatal(len(v)) + } + + if v, err := db.ZScan(key, nil, 10, true); err != nil { + t.Fatal(err) + } else if len(v) != 3 { + t.Fatal(len(v)) + } +}