From 2972f57436b03888ea091ae3d78fdcf2c533311d Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 18 Sep 2014 22:20:18 +0800 Subject: [PATCH] remove DataDB --- ledis/ledis.go | 5 ---- server/cmd_replication_test.go | 43 ++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/ledis/ledis.go b/ledis/ledis.go index 30e6ac7..2d52c11 100644 --- a/ledis/ledis.go +++ b/ledis/ledis.go @@ -90,11 +90,6 @@ func (l *Ledis) FlushAll() error { return nil } -// very dangerous to use -func (l *Ledis) DataDB() *store.DB { - return l.ldb -} - func (l *Ledis) activeExpireCycle() { var executors []*elimination = make([]*elimination, len(l.dbs)) for i, db := range l.dbs { diff --git a/server/cmd_replication_test.go b/server/cmd_replication_test.go index 3e7e285..5d20ff5 100644 --- a/server/cmd_replication_test.go +++ b/server/cmd_replication_test.go @@ -1,25 +1,32 @@ package server import ( - "bytes" "fmt" "github.com/siddontang/ledisdb/config" - "github.com/siddontang/ledisdb/store" "os" + "reflect" "testing" "time" ) func checkDataEqual(master *App, slave *App) error { - it := master.ldb.DataDB().RangeLimitIterator(nil, nil, store.RangeClose, 0, -1) - for ; it.Valid(); it.Next() { - key := it.Key() - value := it.Value() + mdb, _ := master.ldb.Select(0) + sdb, _ := slave.ldb.Select(0) - if v, err := slave.ldb.DataDB().Get(key); err != nil { - return err - } else if !bytes.Equal(v, value) { - return fmt.Errorf("replication error %d != %d", len(v), len(value)) + mkeys, _ := mdb.Scan(nil, 100, true, "") + skeys, _ := sdb.Scan(nil, 100, true, "") + + if len(mkeys) != len(skeys) { + return fmt.Errorf("keys number not equal") + } else if !reflect.DeepEqual(mkeys, skeys) { + return fmt.Errorf("keys not equal") + } else { + for _, k := range mkeys { + v1, _ := mdb.Get(k) + v2, _ := sdb.Get(k) + if !reflect.DeepEqual(v1, v2) { + return fmt.Errorf("value not equal") + } } } @@ -64,8 +71,8 @@ func TestReplication(t *testing.T) { db.Set([]byte("a"), value) db.Set([]byte("b"), value) - db.HSet([]byte("a"), []byte("1"), value) - db.HSet([]byte("b"), []byte("2"), value) + db.Set([]byte("c"), value) + db.Set([]byte("d"), value) go slave.Run() @@ -77,8 +84,8 @@ func TestReplication(t *testing.T) { db.Set([]byte("a1"), value) db.Set([]byte("b1"), value) - db.HSet([]byte("a1"), []byte("1"), value) - db.HSet([]byte("b1"), []byte("2"), value) + db.Set([]byte("c1"), value) + db.Set([]byte("d1"), value) time.Sleep(1 * time.Second) if err = checkDataEqual(master, slave); err != nil { @@ -89,13 +96,13 @@ func TestReplication(t *testing.T) { db.Set([]byte("a2"), value) db.Set([]byte("b2"), value) - db.HSet([]byte("a2"), []byte("1"), value) - db.HSet([]byte("b2"), []byte("2"), value) + db.Set([]byte("c2"), value) + db.Set([]byte("d2"), value) db.Set([]byte("a3"), value) db.Set([]byte("b3"), value) - db.HSet([]byte("a3"), []byte("1"), value) - db.HSet([]byte("b3"), []byte("2"), value) + db.Set([]byte("c3"), value) + db.Set([]byte("d3"), value) if err = checkDataEqual(master, slave); err == nil { t.Fatal("must error")