diff --git a/ledis/binlog.go b/ledis/binlog.go index 6e6aa5b..077398a 100644 --- a/ledis/binlog.go +++ b/ledis/binlog.go @@ -256,6 +256,10 @@ func (l *BinLog) checkLogFileSize() bool { } func (l *BinLog) closeLog() { + if l.logFile == nil { + return + } + l.lastLogIndex++ l.logFile.Close() @@ -263,6 +267,9 @@ func (l *BinLog) closeLog() { } func (l *BinLog) purge(n int) { + if len(l.logNames) < n { + n = len(l.logNames) + } for i := 0; i < n; i++ { logPath := path.Join(l.path, l.logNames[i]) os.Remove(logPath) @@ -338,6 +345,9 @@ func (l *BinLog) PurgeAll() error { defer l.Unlock() l.closeLog() + + l.purge(len(l.logNames)) + return l.openNewLogFile() } diff --git a/ledis/binlog_test.go b/ledis/binlog_test.go index f7a8320..ea62bd9 100644 --- a/ledis/binlog_test.go +++ b/ledis/binlog_test.go @@ -34,4 +34,16 @@ func TestBinLog(t *testing.T) { } else if len(fs) != 2 { t.Fatal(len(fs)) } + + if err := b.PurgeAll(); err != nil { + t.Fatal(err) + } + + if fs, err := ioutil.ReadDir(b.LogPath()); err != nil { + t.Fatal(err) + } else if len(fs) != 2 { + t.Fatal(len(fs)) + } else if b.LogFilePos() != 0 { + t.Fatal(b.LogFilePos()) + } }