ledisdb/wal/file_store.go

233 lines
3.9 KiB
Go
Raw Normal View History

2014-09-15 18:42:25 +04:00
package wal
import (
2014-09-17 13:54:04 +04:00
"fmt"
"github.com/siddontang/go-log/log"
"io/ioutil"
2014-09-15 18:42:25 +04:00
"os"
2014-09-17 13:54:04 +04:00
"path"
"strconv"
"strings"
2014-09-15 18:42:25 +04:00
"sync"
)
const (
defaultMaxLogFileSize = 1024 * 1024 * 1024
)
2014-09-17 13:54:04 +04:00
/*
index file format:
ledis-bin.00001
ledis-bin.00002
ledis-bin.00003
*/
2014-09-15 18:42:25 +04:00
type FileStore struct {
Store
m sync.Mutex
maxFileSize int
first uint64
last uint64
2014-09-17 13:54:04 +04:00
logFile *os.File
logNames []string
nextLogIndex int64
indexName string
path string
2014-09-15 18:42:25 +04:00
}
func NewFileStore(path string) (*FileStore, error) {
s := new(FileStore)
if err := os.MkdirAll(path, 0755); err != nil {
return nil, err
}
2014-09-17 13:54:04 +04:00
s.path = path
2014-09-15 18:42:25 +04:00
s.maxFileSize = defaultMaxLogFileSize
s.first = 0
s.last = 0
2014-09-17 13:54:04 +04:00
s.logNames = make([]string, 0, 16)
if err := s.loadIndex(); err != nil {
return nil, err
}
2014-09-15 18:42:25 +04:00
return s, nil
}
func (s *FileStore) SetMaxFileSize(size int) {
s.maxFileSize = size
}
func (s *FileStore) GetLog(id uint64, log *Log) error {
return nil
}
func (s *FileStore) SeekLog(id uint64, log *Log) error {
return nil
}
func (s *FileStore) FirstID() (uint64, error) {
return 0, nil
}
func (s *FileStore) LastID() (uint64, error) {
return 0, nil
}
func (s *FileStore) StoreLog(log *Log) error {
return nil
}
func (s *FileStore) StoreLogs(logs []*Log) error {
return nil
}
2014-09-17 13:54:04 +04:00
func (s *FileStore) Purge(n uint64) error {
return nil
}
func (s *FileStore) PuregeExpired(n int) error {
2014-09-15 18:42:25 +04:00
return nil
}
func (s *FileStore) Clear() error {
return nil
}
func (s *FileStore) Close() error {
return nil
}
2014-09-17 13:54:04 +04:00
func (s *FileStore) flushIndex() error {
data := strings.Join(s.logNames, "\n")
bakName := fmt.Sprintf("%s.bak", s.indexName)
f, err := os.OpenFile(bakName, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Error("create bak index error %s", err.Error())
return err
}
if _, err := f.WriteString(data); err != nil {
log.Error("write index error %s", err.Error())
f.Close()
return err
}
f.Close()
if err := os.Rename(bakName, s.indexName); err != nil {
log.Error("rename bak index error %s", err.Error())
return err
}
return nil
}
func (s *FileStore) fileExists(name string) bool {
p := path.Join(s.path, name)
_, err := os.Stat(p)
return !os.IsNotExist(err)
}
func (s *FileStore) loadIndex() error {
s.indexName = path.Join(s.path, fmt.Sprintf("ledis-bin.index"))
if _, err := os.Stat(s.indexName); os.IsNotExist(err) {
//no index file, nothing to do
} else {
indexData, err := ioutil.ReadFile(s.indexName)
if err != nil {
return err
}
lines := strings.Split(string(indexData), "\n")
for _, line := range lines {
line = strings.Trim(line, "\r\n ")
if len(line) == 0 {
continue
}
if s.fileExists(line) {
s.logNames = append(s.logNames, line)
} else {
log.Info("log %s has not exists", line)
}
}
}
var err error
if len(s.logNames) == 0 {
s.nextLogIndex = 1
} else {
lastName := s.logNames[len(s.logNames)-1]
if s.nextLogIndex, err = strconv.ParseInt(path.Ext(lastName)[1:], 10, 64); err != nil {
log.Error("invalid logfile name %s", err.Error())
return err
}
//like mysql, if server restart, a new log will create
s.nextLogIndex++
}
return nil
}
func (s *FileStore) openNewLogFile() error {
var err error
lastName := s.formatLogFileName(s.nextLogIndex)
logPath := path.Join(s.path, lastName)
if s.logFile, err = os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0644); err != nil {
log.Error("open new logfile error %s", err.Error())
return err
}
s.logNames = append(s.logNames, lastName)
if err = s.flushIndex(); err != nil {
return err
}
return nil
}
func (s *FileStore) checkLogFileSize() bool {
if s.logFile == nil {
return false
}
st, _ := s.logFile.Stat()
if st.Size() >= int64(s.maxFileSize) {
s.closeLog()
return true
}
return false
}
func (s *FileStore) closeLog() {
if s.logFile == nil {
return
}
s.nextLogIndex++
s.logFile.Close()
s.logFile = nil
}
func (s *FileStore) formatLogFileName(index int64) string {
return fmt.Sprintf("ledis-bin.%07d", index)
}