/
util.go
186 lines (166 loc) · 4.3 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package bitcask
import (
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/laohanlinux/go-logger/logger"
)
const (
lockFileName = "bitcask.lock"
)
// if writeableFile size large than Opts.MaxFileSize and the fileID not equal to local time stamp;
// if will create a new writeable file
func checkWriteableFile(bc *BitCask) {
if bc.writeFile.writeOffset > bc.Opts.MaxFileSize && bc.writeFile.fileID != uint32(time.Now().Unix()) {
logger.Info("open a new data/hint file:", bc.writeFile.writeOffset, bc.Opts.MaxFileSize)
//close data/hint fp
bc.writeFile.hintFp.Close()
bc.writeFile.fp.Close()
writeFp, fileID := setWriteableFile(0, bc.dirFile)
hintFp := setHintFile(fileID, bc.dirFile)
bf := &BFile{
fp: writeFp,
fileID: fileID,
writeOffset: 0,
hintFp: hintFp,
}
bc.writeFile = bf
// update pid
writePID(bc.lockFile, fileID)
}
}
// return the hint file lists
func listHintFiles(bc *BitCask) ([]string, error) {
filterFiles := []string{lockFileName}
dirFp, err := os.OpenFile(bc.dirFile, os.O_RDONLY, os.ModeDir)
if err != nil {
return nil, err
}
defer dirFp.Close()
//
lists, err := dirFp.Readdirnames(-1)
if err != nil {
return nil, err
}
var hintLists []string
for _, v := range lists {
if strings.Contains(v, "hint") && !existsSuffixs(filterFiles, v) {
hintLists = append(hintLists, v)
}
}
return hintLists, nil
}
// return the data file lists
func listDataFiles(bc *BitCask) ([]string, error) {
filterFiles := []string{lockFileName}
dirFp, err := os.OpenFile(bc.dirFile, os.O_RDONLY, os.ModeDir)
if err != nil {
return nil, err
}
defer dirFp.Close()
//
lists, err := dirFp.Readdirnames(-1)
if err != nil {
return nil, err
}
var dataFileLists []string
for _, v := range lists {
if strings.Contains(v, ".data") && !existsSuffixs(filterFiles, v) {
dataFileLists = append(dataFileLists, v)
}
}
sort.Strings(dataFileLists)
return dataFileLists, nil
}
// lock a file by fp locker; the file must exits
func lockFile(fileName string) (*os.File, error) {
return os.OpenFile(fileName, os.O_EXCL|os.O_CREATE|os.O_RDWR, os.ModePerm)
}
func existsSuffixs(suffixs []string, src string) (b bool) {
for _, suffix := range suffixs {
if b = strings.HasSuffix(src, suffix); b {
return
}
}
return
}
func writePID(pidFp *os.File, fileID uint32) {
pidFp.WriteAt([]byte(strconv.Itoa(os.Getpid())+"\t"+strconv.Itoa(int(fileID))+".data"), 0)
}
// get file last hint file info
func lastFileInfo(files []*os.File) (uint32, *os.File) {
if files == nil {
return uint32(0), nil
}
lastFp := files[0]
fileName := lastFp.Name()
s := strings.LastIndex(fileName, "/") + 1
e := strings.LastIndex(fileName, ".hint")
idx, _ := strconv.Atoi(fileName[s:e])
lastID := idx
for i := 0; i < len(files); i++ {
idxFp := files[i]
fileName = idxFp.Name()
s = strings.LastIndex(fileName, "/") + 1
e = strings.LastIndex(fileName, ".hint")
idx, _ = strconv.Atoi(fileName[s:e])
if lastID < idx {
lastID = idx
lastFp = idxFp
}
}
return uint32(lastID), lastFp
}
func closeReadHintFp(files []*os.File, fileID uint32) {
for _, fp := range files {
if !strings.Contains(fp.Name(), strconv.Itoa(int(fileID))) {
fp.Close()
}
}
}
func setWriteableFile(fileID uint32, dirName string) (*os.File, uint32) {
var fp *os.File
var err error
if fileID == 0 {
fileID = uint32(time.Now().Unix())
}
fileName := dirName + "/" + strconv.Itoa(int(fileID)) + ".data"
fp, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0755)
if err != nil {
panic(err)
}
return fp, fileID
}
func setHintFile(fileID uint32, dirName string) *os.File {
var fp *os.File
var err error
if fileID == 0 {
fileID = uint32(time.Now().Unix())
}
fileName := dirName + "/" + strconv.Itoa(int(fileID)) + ".hint"
fp, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0755)
if err != nil {
panic(err)
}
return fp
}
func appendWriteFile(fp *os.File, buf []byte) (int, error) {
stat, err := fp.Stat()
if err != nil {
return -1, err
}
return fp.WriteAt(buf, stat.Size())
}
// return a unique not exists file name by timeStamp
func uniqueFileName(root, suffix string) string {
for {
tStamp := strconv.Itoa(int(time.Now().Unix()))
_, err := os.Stat(root + "/" + tStamp + "." + suffix)
if err != nil && os.IsNotExist(err) {
return tStamp + "." + suffix
}
time.Sleep(time.Second)
}
}