You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.2 KiB
66 lines
1.2 KiB
package rotate
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/alibaba/RedisShake/internal/log"
|
|
"os"
|
|
)
|
|
|
|
const MaxFileSize = 1024 * 1024 * 1024 // 1G
|
|
|
|
type AOFWriter struct {
|
|
file *os.File
|
|
offset int64
|
|
filename string
|
|
filesize int64
|
|
}
|
|
|
|
func NewAOFWriter(offset int64) *AOFWriter {
|
|
w := &AOFWriter{}
|
|
w.openFile(offset)
|
|
return w
|
|
}
|
|
|
|
func (w *AOFWriter) openFile(offset int64) {
|
|
w.filename = fmt.Sprintf("%d.aof", offset)
|
|
var err error
|
|
w.file, err = os.OpenFile(w.filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
if err != nil {
|
|
log.PanicError(err)
|
|
}
|
|
w.offset = offset
|
|
w.filesize = 0
|
|
log.Infof("AOFWriter open file. filename=[%s]", w.filename)
|
|
}
|
|
|
|
func (w *AOFWriter) Write(buf []byte) {
|
|
_, err := w.file.Write(buf)
|
|
if err != nil {
|
|
log.PanicError(err)
|
|
}
|
|
w.offset += int64(len(buf))
|
|
w.filesize += int64(len(buf))
|
|
if w.filesize > MaxFileSize {
|
|
w.Close()
|
|
w.openFile(w.offset)
|
|
}
|
|
err = w.file.Sync()
|
|
if err != nil {
|
|
log.PanicError(err)
|
|
}
|
|
}
|
|
|
|
func (w *AOFWriter) Close() {
|
|
if w.file == nil {
|
|
return
|
|
}
|
|
err := w.file.Sync()
|
|
if err != nil {
|
|
log.PanicError(err)
|
|
}
|
|
err = w.file.Close()
|
|
if err != nil {
|
|
log.PanicError(err)
|
|
}
|
|
log.Infof("AOFWriter close file. filename=[%s], filesize=[%d]", w.filename, w.filesize)
|
|
}
|
|
|