bug fix for kv scan, add scan test

This commit is contained in:
siddontang 2014-05-26 09:21:17 +08:00
parent 39c88b956c
commit bd38e693f2
4 changed files with 92 additions and 1 deletions

View File

@ -38,3 +38,33 @@ func TestDBHash(t *testing.T) {
t.Fatal(n) 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))
}
}

View File

@ -296,7 +296,7 @@ func (db *DB) Scan(key []byte, count int, inclusive bool) ([]KVPair, error) {
if err := checkKeySize(key); err != nil { if err := checkKeySize(key); err != nil {
return nil, err return nil, err
} }
minKey = key minKey = db.encodeKVKey(key)
} else { } else {
minKey = db.encodeKVMinKey() minKey = db.encodeKVMinKey()
} }

View File

@ -25,3 +25,37 @@ func TestDBKV(t *testing.T) {
t.Fatal(err) 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))
}
}

View File

@ -191,3 +191,30 @@ func TestZSetOrder(t *testing.T) {
t.Fatal(pos) 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))
}
}