ledisdb/store/store_test.go

393 lines
7.6 KiB
Go
Raw Permalink Normal View History

2014-07-25 13:58:00 +04:00
package store
2014-06-19 13:19:40 +04:00
import (
"bytes"
"fmt"
2014-08-15 20:08:01 +04:00
"github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/store/driver"
"os"
2014-11-10 06:07:33 +03:00
"reflect"
2014-06-19 13:19:40 +04:00
"testing"
)
2014-07-28 05:25:40 +04:00
func TestStore(t *testing.T) {
2014-10-11 12:00:59 +04:00
cfg := config.NewConfigDefault()
2014-08-15 20:08:01 +04:00
cfg.DataDir = "/tmp/testdb"
cfg.LMDB.MapSize = 10 * 1024 * 1024
2014-06-19 13:19:40 +04:00
2014-08-15 20:08:01 +04:00
ns := driver.ListStores()
for _, s := range ns {
cfg.DBName = s
os.RemoveAll(getStorePath(cfg))
db, err := Open(cfg)
if err != nil {
t.Fatal(err)
}
testStore(db, t)
testClear(db, t)
testTx(db, t)
db.Close()
}
2014-06-19 13:19:40 +04:00
}
2014-07-28 05:25:40 +04:00
func testStore(db *DB, t *testing.T) {
testSimple(db, t)
testBatch(db, t)
testIterator(db, t)
2014-08-25 10:18:23 +04:00
testSnapshot(db, t)
2014-11-10 06:07:33 +03:00
testBatchData(db, t)
2014-07-28 05:25:40 +04:00
}
2014-06-19 13:19:40 +04:00
2014-08-15 20:08:01 +04:00
func testClear(db *DB, t *testing.T) {
it := db.RangeIterator(nil, nil, RangeClose)
for ; it.Valid(); it.Next() {
db.Delete(it.RawKey())
}
2014-08-25 10:18:23 +04:00
it.Close()
2014-08-15 20:08:01 +04:00
}
2014-07-28 05:25:40 +04:00
func testSimple(db *DB, t *testing.T) {
2014-06-19 13:19:40 +04:00
key := []byte("key")
value := []byte("hello world")
if err := db.Put(key, value); err != nil {
t.Fatal(err)
}
if v, err := db.Get(key); err != nil {
t.Fatal(err)
} else if !bytes.Equal(v, value) {
t.Fatal("not equal")
}
2014-10-29 05:29:35 +03:00
if v, err := db.GetSlice(key); err != nil {
t.Fatal(err)
} else if v == nil {
t.Fatal("must not nil")
} else if !bytes.Equal(v.Data(), value) {
t.Fatal("not equal")
} else {
v.Free()
}
2014-06-19 13:19:40 +04:00
if err := db.Delete(key); err != nil {
t.Fatal(err)
}
if v, err := db.Get(key); err != nil {
t.Fatal(err)
} else if v != nil {
t.Fatal("must nil")
}
2014-07-29 04:58:13 +04:00
2014-10-29 05:29:35 +03:00
if v, err := db.GetSlice(key); err != nil {
t.Fatal(err)
} else if v != nil {
t.Fatal("must nil")
}
2014-07-29 04:58:13 +04:00
if err := db.Put(key, nil); err != nil {
t.Fatal(err)
}
if v, err := db.Get(key); err != nil {
t.Fatal(err)
} else if !bytes.Equal(v, []byte{}) {
t.Fatal("must empty")
}
2014-06-19 13:19:40 +04:00
}
2014-07-28 05:25:40 +04:00
func testBatch(db *DB, t *testing.T) {
2014-06-19 13:19:40 +04:00
key1 := []byte("key1")
key2 := []byte("key2")
value := []byte("hello world")
db.Put(key1, value)
db.Put(key2, value)
wb := db.NewWriteBatch()
wb.Delete(key2)
wb.Put(key1, []byte("hello world2"))
if err := wb.Commit(); err != nil {
t.Fatal(err)
}
if v, err := db.Get(key2); err != nil {
t.Fatal(err)
} else if v != nil {
t.Fatal("must nil")
}
if v, err := db.Get(key1); err != nil {
t.Fatal(err)
} else if string(v) != "hello world2" {
t.Fatal(string(v))
}
wb.Delete(key1)
wb.Rollback()
if v, err := db.Get(key1); err != nil {
t.Fatal(err)
} else if string(v) != "hello world2" {
t.Fatal(string(v))
}
2014-07-29 04:58:13 +04:00
wb.Put(key1, nil)
wb.Put(key2, []byte{})
if err := wb.Commit(); err != nil {
t.Fatal(err)
}
if v, err := db.Get(key1); err != nil {
t.Fatal(err)
} else if !bytes.Equal(v, []byte{}) {
t.Fatal("must empty")
}
if v, err := db.Get(key2); err != nil {
t.Fatal(err)
} else if !bytes.Equal(v, []byte{}) {
t.Fatal("must empty")
}
2014-06-19 13:19:40 +04:00
db.Delete(key1)
2014-07-29 04:58:13 +04:00
db.Delete(key2)
2014-06-19 13:19:40 +04:00
}
func checkIterator(it *RangeLimitIterator, cv ...int) error {
v := make([]string, 0, len(cv))
for ; it.Valid(); it.Next() {
k := it.Key()
2014-09-12 11:06:36 +04:00
if string(it.Value()) != "value" {
return fmt.Errorf("invalid value")
}
2014-06-19 13:19:40 +04:00
v = append(v, string(k))
}
it.Close()
if len(v) != len(cv) {
return fmt.Errorf("len error %d != %d", len(v), len(cv))
}
for k, i := range cv {
if fmt.Sprintf("key_%d", i) != v[k] {
return fmt.Errorf("%s, %d", v[k], i)
}
}
return nil
}
2014-07-28 05:25:40 +04:00
func testIterator(db *DB, t *testing.T) {
2014-07-25 13:58:00 +04:00
i := db.NewIterator()
for i.SeekToFirst(); i.Valid(); i.Next() {
db.Delete(i.Key())
}
i.Close()
2014-06-19 13:19:40 +04:00
for i := 0; i < 10; i++ {
key := []byte(fmt.Sprintf("key_%d", i))
2014-09-12 11:06:36 +04:00
value := []byte("value")
2014-06-19 13:19:40 +04:00
db.Put(key, value)
}
2014-07-25 15:41:04 +04:00
i = db.NewIterator()
i.SeekToFirst()
if !i.Valid() {
t.Fatal("must valid")
} else if string(i.Key()) != "key_0" {
t.Fatal(string(i.Key()))
}
i.Close()
2014-06-19 13:19:40 +04:00
var it *RangeLimitIterator
k := func(i int) []byte {
return []byte(fmt.Sprintf("key_%d", i))
}
it = db.RangeLimitIterator(k(1), k(5), RangeClose, 0, -1)
if err := checkIterator(it, 1, 2, 3, 4, 5); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
it = db.RangeLimitIterator(k(1), k(5), RangeClose, 0, -1)
if err := checkIterator(it, 1, 2, 3, 4, 5); err != nil {
t.Fatal(err)
}
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RangeLimitIterator(k(1), k(5), RangeClose, 1, 3)
if err := checkIterator(it, 2, 3, 4); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RangeLimitIterator(k(1), k(5), RangeLOpen, 0, -1)
if err := checkIterator(it, 2, 3, 4, 5); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RangeLimitIterator(k(1), k(5), RangeROpen, 0, -1)
if err := checkIterator(it, 1, 2, 3, 4); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RangeLimitIterator(k(1), k(5), RangeOpen, 0, -1)
if err := checkIterator(it, 2, 3, 4); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RevRangeLimitIterator(k(1), k(5), RangeClose, 0, -1)
if err := checkIterator(it, 5, 4, 3, 2, 1); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RevRangeLimitIterator(k(1), k(5), RangeClose, 1, 3)
if err := checkIterator(it, 4, 3, 2); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RevRangeLimitIterator(k(1), k(5), RangeLOpen, 0, -1)
if err := checkIterator(it, 5, 4, 3, 2); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RevRangeLimitIterator(k(1), k(5), RangeROpen, 0, -1)
if err := checkIterator(it, 4, 3, 2, 1); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
it = db.RevRangeLimitIterator(k(1), k(5), RangeOpen, 0, -1)
if err := checkIterator(it, 4, 3, 2); err != nil {
t.Fatal(err)
}
2014-07-25 15:41:04 +04:00
it.Close()
2014-06-19 13:19:40 +04:00
}
2014-08-25 10:18:23 +04:00
func testSnapshot(db *DB, t *testing.T) {
foo := []byte("foo")
bar := []byte("bar")
v1 := []byte("v1")
v2 := []byte("v2")
db.Put(foo, v1)
db.Put(bar, v1)
snap, err := db.NewSnapshot()
if err != nil {
t.Fatal(err)
}
i := snap.NewIterator()
i.Seek([]byte("foo"))
if !i.Valid() {
t.Fatal("must valid")
} else if string(i.Value()) != "v1" {
t.Fatal(string(i.Value()))
}
i.Close()
db.Put(foo, v2)
db.Put(bar, v2)
if v, err := snap.Get(foo); err != nil {
t.Fatal(err)
} else if string(v) != "v1" {
t.Fatal(string(v))
}
if v, err := snap.Get(bar); err != nil {
t.Fatal(err)
} else if string(v) != "v1" {
t.Fatal(string(v))
}
if v, err := db.Get(foo); err != nil {
t.Fatal(err)
} else if string(v) != "v2" {
t.Fatal(string(v))
}
if v, err := db.Get(bar); err != nil {
t.Fatal(err)
} else if string(v) != "v2" {
t.Fatal(string(v))
}
snap.Close()
if v, err := db.Get(foo); err != nil {
t.Fatal(err)
} else if string(v) != "v2" {
t.Fatal(string(v))
}
}
2014-11-10 06:07:33 +03:00
func testBatchData(db *DB, t *testing.T) {
w := db.NewWriteBatch()
w.Put([]byte("a"), []byte("1"))
w.Put([]byte("b"), nil)
w.Delete([]byte("c"))
d := w.BatchData()
2014-11-10 06:07:33 +03:00
if kvs, err := d.Items(); err != nil {
t.Fatal(err)
} else if len(kvs) != 3 {
t.Fatal(len(kvs))
} else if !reflect.DeepEqual(kvs[0], BatchItem{[]byte("a"), []byte("1")}) {
t.Fatal("must equal")
} else if !reflect.DeepEqual(kvs[1], BatchItem{[]byte("b"), []byte{}}) {
t.Fatal("must equal")
} else if !reflect.DeepEqual(kvs[2], BatchItem{[]byte("c"), nil}) {
t.Fatal("must equal")
}
if err := d.Append(d); err != nil {
t.Fatal(err)
} else if d.Len() != 6 {
t.Fatal(d.Len())
}
if kvs, err := d.Items(); err != nil {
t.Fatal(err)
} else if len(kvs) != 6 {
t.Fatal(len(kvs))
} else if !reflect.DeepEqual(kvs[0], BatchItem{[]byte("a"), []byte("1")}) {
t.Fatal("must equal")
} else if !reflect.DeepEqual(kvs[1], BatchItem{[]byte("b"), []byte{}}) {
t.Fatal("must equal")
} else if !reflect.DeepEqual(kvs[2], BatchItem{[]byte("c"), nil}) {
t.Fatal("must equal")
} else if !reflect.DeepEqual(kvs[3], BatchItem{[]byte("a"), []byte("1")}) {
t.Fatal("must equal")
} else if !reflect.DeepEqual(kvs[4], BatchItem{[]byte("b"), []byte{}}) {
t.Fatal("must equal")
} else if !reflect.DeepEqual(kvs[5], BatchItem{[]byte("c"), nil}) {
t.Fatal("must equal")
}
}