2014-06-03 05:51:29 +04:00
|
|
|
package replication
|
2014-05-27 12:05:24 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-06-03 05:51:29 +04:00
|
|
|
const (
|
|
|
|
MaxBinLogFileSize int = 1024 * 1024 * 1024
|
|
|
|
MaxBinLogFileNum int = 10000
|
|
|
|
|
|
|
|
DefaultBinLogFileSize int = MaxBinLogFileSize
|
|
|
|
DefaultBinLogFileNum int = 10
|
|
|
|
)
|
|
|
|
|
2014-05-27 12:05:24 +04:00
|
|
|
/*
|
|
|
|
index file format:
|
|
|
|
ledis-bin.00001
|
|
|
|
ledis-bin.00002
|
|
|
|
ledis-bin.00003
|
|
|
|
|
|
|
|
log file format
|
|
|
|
|
|
|
|
timestamp(bigendian uint32, seconds)|PayloadLen(bigendian uint32)|PayloadData|LogId
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
type BinLogConfig struct {
|
2014-06-03 10:08:57 +04:00
|
|
|
LogConfig
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *BinLogConfig) adjust() {
|
|
|
|
if cfg.MaxFileSize <= 0 {
|
|
|
|
cfg.MaxFileSize = DefaultBinLogFileSize
|
|
|
|
} else if cfg.MaxFileSize > MaxBinLogFileSize {
|
|
|
|
cfg.MaxFileSize = MaxBinLogFileSize
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.MaxFileNum <= 0 {
|
|
|
|
cfg.MaxFileNum = DefaultBinLogFileNum
|
|
|
|
} else if cfg.MaxFileNum > MaxBinLogFileNum {
|
|
|
|
cfg.MaxFileNum = MaxBinLogFileNum
|
|
|
|
}
|
2014-05-29 11:07:14 +04:00
|
|
|
|
|
|
|
if len(cfg.BaseName) == 0 {
|
|
|
|
cfg.BaseName = "ledis"
|
|
|
|
}
|
|
|
|
if len(cfg.IndexName) == 0 {
|
|
|
|
cfg.IndexName = "ledis"
|
|
|
|
}
|
2014-05-27 12:05:24 +04:00
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
cfg.SpaceLimit = -1
|
2014-05-27 12:05:24 +04:00
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
cfg.LogType = "bin"
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
type binlogHandler struct {
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
func (h *binlogHandler) Write(wb *bufio.Writer, data []byte) (int, error) {
|
|
|
|
createTime := uint32(time.Now().Unix())
|
|
|
|
payLoadLen := uint32(len(data))
|
2014-05-27 12:05:24 +04:00
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
if err := binary.Write(wb, binary.BigEndian, createTime); err != nil {
|
|
|
|
return 0, err
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
if err := binary.Write(wb, binary.BigEndian, payLoadLen); err != nil {
|
|
|
|
return 0, err
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
if _, err := wb.Write(data); err != nil {
|
|
|
|
return 0, err
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
return 8 + len(data), nil
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
func NewBinLog(data json.RawMessage) (*Log, error) {
|
|
|
|
var cfg BinLogConfig
|
2014-05-27 12:05:24 +04:00
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
|
|
return nil, err
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
return NewBinLogWithConfig(&cfg)
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
func NewBinLogWithConfig(cfg *BinLogConfig) (*Log, error) {
|
|
|
|
cfg.adjust()
|
2014-05-28 10:20:34 +04:00
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
return newLog(new(binlogHandler), &cfg.LogConfig)
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|