func openSegments(dir string, needRecover bool) []*Segment {
	files, err := utils.ReadDir(dir)
	if err != nil {
		log.Log.Fatalf("ReadDir error,err:%s,dir:%s", err, dir)
	}
	logFiles := make([]os.FileInfo, 0, 10)
	for _, file := range files {
		if strings.HasSuffix(file.Name(), utils.LogSuffix) {
			logFiles = append(logFiles, file)
		}
	}
	fileCount := len(logFiles)
	segments := make([]*Segment, 0, 10)
	if fileCount == 0 {
		return segments
	}
	for i := 0; i < fileCount-1; i++ {
		s := openSegment(dir, logFiles[i].Name(), SegmentReadOnly, false)
		segments = append(segments, s)
	}
	s := openSegment(dir, logFiles[fileCount-1].Name(), SegmentRDWR, needRecover)
	segments = append(segments, s)
	return segments
}
todo test the order is right?
package storage
import (
	"encoding/binary"
	"fmt"
	"hash/crc32"
	"os"
	"path"
	"strings"
	"sync"
	"github.com/flike/kingbus/storage/storagepb"
	"github.com/flike/kingbus/log"
	"github.com/flike/kingbus/utils"
	"github.com/coreos/etcd/raft/raftpb"
)
type SegmentStatus int8
const (
	SegmentReadOnly SegmentStatus = iota
	SegmentRDWR
	SegmentClosed
)
const (
	ReadonlySegmentPattern = "%020d-%020d.log"
	ReadonlyIndexPattern = "%020d-%020d.index"
	ReadWriteSegmentPattern = "%020d-inprogress.log"
	ReadWriteIndexPattern = "%020d-inprogress.index"
	RecordLengthSize int = 4
)
type Segment struct {
	Mu         sync.RWMutex
	FirstIndex uint64
	LastIndex  uint64
	LogFile  *MmapFile
	LogIndex *Index
	Status   SegmentStatus
}
func newSegment(dir string, name string) *Segment {
	s := new(Segment)
	s.FirstIndex = 0
	s.LastIndex = 0
	s.LogFile = newMmapFile(dir, name, SegmentSize)
	indexFileName := utils.GetIndexName(name)
	s.LogIndex = newIndex(dir, indexFileName)
	s.Status = SegmentRDWR
	return s
}