From 4a0244180ace63ceff77044cd2bd358fc336979d Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 6 Nov 2014 15:05:21 +0800 Subject: [PATCH] update table reader --- rpl/file_table.go | 18 ++++++++++-------- rpl/file_table_test.go | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/rpl/file_table.go b/rpl/file_table.go index f1db90a..e94a471 100644 --- a/rpl/file_table.go +++ b/rpl/file_table.go @@ -41,27 +41,26 @@ type tableReader struct { first uint64 last uint64 + lastTime uint32 + offsetStartPos int64 offsetLen uint32 lastReadTime sync2.AtomicInt64 } -func newTableReader(name string) (*tableReader, error) { +func newTableReader(base string, index int64) (*tableReader, error) { t := new(tableReader) - t.name = name + t.name = path.Join(base, fmtTableName(index)) + t.index = index var err error - if _, err = fmt.Sscanf(path.Base(name), "%d.ldb", &t.index); err != nil { - return nil, err - } - if err = t.check(); err != nil { - log.Error("check %s error: %s, try to repair", name, err.Error()) + log.Error("check %s error: %s, try to repair", t.name, err.Error()) if err = t.repair(); err != nil { - log.Error("repair %s error: %s", name, err.Error()) + log.Error("repair %s error: %s", t.name, err.Error()) return nil, err } } @@ -164,6 +163,7 @@ func (t *tableReader) check() error { } t.last = l.ID + t.lastTime = l.CreateTime if t.first > t.last { return fmt.Errorf("invalid log table first %d > last %d", t.first, t.last) @@ -206,6 +206,8 @@ func (t *tableReader) repair() error { break } + t.lastTime = l.CreateTime + if err := tw.StoreLog(&l); err != nil { return err } diff --git a/rpl/file_table_test.go b/rpl/file_table_test.go index 929bdb0..65b7db7 100644 --- a/rpl/file_table_test.go +++ b/rpl/file_table_test.go @@ -4,6 +4,7 @@ import ( "github.com/siddontang/go/log" "io/ioutil" "os" + "path" "testing" "time" ) @@ -80,6 +81,7 @@ func TestFileTable(t *testing.T) { } var r *tableReader + name := w.name if r, err = w.Flush(); err != nil { @@ -111,7 +113,7 @@ func TestFileTable(t *testing.T) { r.Close() - if r, err = newTableReader(name); err != nil { + if r, err = newTableReader(base, 1); err != nil { t.Fatal(err) } defer r.Close() @@ -135,34 +137,32 @@ func TestFileTable(t *testing.T) { r.Close() - testRepair(t, name, s, 11) - testRepair(t, name, s, 32) - testRepair(t, name, s, 42) - testRepair(t, name, s, 72) + testRepair(t, name, 1, s, 11) + testRepair(t, name, 1, s, 32) + testRepair(t, name, 1, s, 42) + testRepair(t, name, 1, s, 72) if err := os.Truncate(name, s-73); err != nil { t.Fatal(err) } - if r, err = newTableReader(name); err == nil { + if r, err = newTableReader(base, 1); err == nil { t.Fatal("can not repair") } - name = w.name - if r, err := w.Flush(); err != nil { t.Fatal(err) } else { r.Close() } - if r, err = newTableReader(name); err != nil { + if r, err = newTableReader(base, 2); err != nil { t.Fatal(err) } defer r.Close() } -func testRepair(t *testing.T, name string, s int64, cutSize int64) { +func testRepair(t *testing.T, name string, index int64, s int64, cutSize int64) { var r *tableReader var err error @@ -170,7 +170,7 @@ func testRepair(t *testing.T, name string, s int64, cutSize int64) { t.Fatal(err) } - if r, err = newTableReader(name); err != nil { + if r, err = newTableReader(path.Dir(name), index); err != nil { t.Fatal(err) } defer r.Close()