binlog purgeall bugfix

This commit is contained in:
siddontang 2014-09-12 15:06:47 +08:00
parent 8284c94453
commit a1096fd1d8
2 changed files with 22 additions and 0 deletions

View File

@ -256,6 +256,10 @@ func (l *BinLog) checkLogFileSize() bool {
} }
func (l *BinLog) closeLog() { func (l *BinLog) closeLog() {
if l.logFile == nil {
return
}
l.lastLogIndex++ l.lastLogIndex++
l.logFile.Close() l.logFile.Close()
@ -263,6 +267,9 @@ func (l *BinLog) closeLog() {
} }
func (l *BinLog) purge(n int) { func (l *BinLog) purge(n int) {
if len(l.logNames) < n {
n = len(l.logNames)
}
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
logPath := path.Join(l.path, l.logNames[i]) logPath := path.Join(l.path, l.logNames[i])
os.Remove(logPath) os.Remove(logPath)
@ -338,6 +345,9 @@ func (l *BinLog) PurgeAll() error {
defer l.Unlock() defer l.Unlock()
l.closeLog() l.closeLog()
l.purge(len(l.logNames))
return l.openNewLogFile() return l.openNewLogFile()
} }

View File

@ -34,4 +34,16 @@ func TestBinLog(t *testing.T) {
} else if len(fs) != 2 { } else if len(fs) != 2 {
t.Fatal(len(fs)) 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())
}
} }