-
Notifications
You must be signed in to change notification settings - Fork 25
/
merge.go
287 lines (252 loc) · 6.59 KB
/
merge.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package CouloyDB
import (
"io"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"github.com/Kirov7/CouloyDB/data"
"github.com/Kirov7/CouloyDB/public"
)
func (db *DB) Merge() error {
db.mergeChan <- struct{}{}
return <-db.mergeDone
}
// Merge Clear invalid data and generate the hint file
func (db *DB) merge() error {
if db.activityFile == nil {
return nil
}
db.mu.Lock()
if db.isMerging {
db.mu.Unlock()
return public.ErrInMerging
}
db.isMerging = true
defer func() {
db.isMerging = false
}()
if err := db.activityFile.Sync(); err != nil {
db.mu.Unlock()
return err
}
// convert current activityFile to oldFile
db.oldFile[db.activityFile.FileId] = db.activityFile
// open a new activityFile
if err := db.setActivityFile(); err != nil {
db.mu.Unlock()
return err
}
// record recent not merge file id
nowMergeFile := db.activityFile.FileId
var mergeFiles []*data.DataFile
for _, file := range db.oldFile {
mergeFiles = append(mergeFiles, file)
}
db.mu.Unlock()
// merge from small to big
sort.Slice(mergeFiles, func(i, j int) bool {
return mergeFiles[i].FileId < mergeFiles[j].FileId
})
mergePath := db.getMergePath()
// if the path is already exist, then remove the path
if _, err := os.Stat(mergePath); err == nil {
if err := os.RemoveAll(mergePath); err != nil {
return err
}
}
// create a new db instance temporarily
mergeOptions := db.options
mergeOptions.DirPath = mergePath
// in merging sync is not needed because it will not affect user's normal behavior If merge panic occurs
mergeOptions.SyncWrites = false
mergeDb, err := NewCouloyDB(mergeOptions)
if err != nil {
return err
}
// open a hintFile, store the index
hintFile, err := data.OpenHintFile(mergePath)
if err != nil {
return err
}
// iterate every dataFile and process them
for _, oldFile := range mergeFiles {
var offset int64 = 0
for {
logRecord, size, err := oldFile.ReadLogRecord(offset)
if err != nil {
if err == io.EOF {
break
}
return err
}
// parse and get the real key
realKey, _ := parseLogRecordKey(logRecord.Key)
var logRecordPos *data.LogPos
switch logRecord.DataType {
case data.String:
logRecordPos = db.index.getStrIndex().Get(realKey)
case data.Hash:
decodedKey, field := decodeFieldKey(realKey)
if idx, ok := db.index.getHashIndex(string(decodedKey)); ok {
logRecordPos = idx.Get(field)
}
case data.List:
decodedKey, seq, _, _ := decodeListKey(realKey)
if idx, ok := db.index.getListDataIndex(string(decodedKey)); ok {
seqBuf, _ := seq.GobEncode()
logRecordPos = idx.Get(seqBuf)
}
case data.ListMeta:
logRecordPos = db.index.getListMetaIndex().Get(realKey)
case data.Set:
realKey, member := decodeMemberKey(realKey)
hashKey := hashMemberKey(realKey, member)
if idx, ok := db.index.getSetIndex(string(realKey)); ok {
logRecordPos = idx.Get(hashKey)
}
}
// compare with the memTable, if the already exist in memTable then rewrite it
if logRecordPos != nil && logRecordPos.Fid == oldFile.FileId && logRecordPos.Offset == offset {
// clean the txId mark
logRecord.Key = encodeKeyWithTxId(realKey, public.NO_TX_ID)
pos, err := mergeDb.appendLogRecord(logRecord)
if err != nil {
return err
}
// write the pos to the hint file
if err := hintFile.WriteHintRecord(realKey, pos); err != nil {
return err
}
}
// add offset
offset += size
}
}
// sync the file
if err := hintFile.Sync(); err != nil {
return err
}
if err := mergeDb.Sync(); err != nil {
return err
}
// add a file to mark merge is finish
mergeFinishedFile, err := data.OpenMergeFinishedFile(mergePath)
if err != nil {
return err
}
mergeFinRecord := &data.LogRecord{
Key: public.MERGE_FIN_Key,
Value: []byte(strconv.Itoa(int(nowMergeFile))),
}
encRecord, _ := data.EncodeLogRecord(mergeFinRecord)
if err := mergeFinishedFile.Write(encRecord); err != nil {
return err
}
if err := mergeFinishedFile.Sync(); err != nil {
return err
}
return err
}
// create a new directory
func (db *DB) getMergePath() string {
// get the parent dir
dir := path.Dir(path.Clean(db.options.DirPath))
// get the current dir name
base := path.Base(db.options.DirPath)
return filepath.Join(dir, base+public.MergeDirName)
}
func (db *DB) loadMergeFiles() error {
mergePath := db.getMergePath()
if _, err := os.Stat(mergePath); os.IsNotExist(err) {
return nil
}
defer func() {
_ = os.RemoveAll(mergePath)
}()
dirEnts, err := os.ReadDir(mergePath)
if err != nil {
return err
}
// find the mark and check whether the merger is complete
var MergeFin bool
// stores the names of all merge files temporarily
var mergeFileNames []string
for _, ent := range dirEnts {
if ent.Name() == public.MergeFinishedFileName {
MergeFin = true
}
mergeFileNames = append(mergeFileNames, ent.Name())
}
// if the merge does not complete, return directly
if !MergeFin {
return nil
}
nonMergeFileId, err := db.getNonMergeFileId(mergePath)
if err != nil {
return err
}
// remove the old dataFile
var fileId uint32
for ; fileId < nonMergeFileId; fileId++ {
fileName := data.GetDataFileName(db.options.DirPath, fileId)
if _, err := os.Stat(fileName); err != nil {
if err := os.Remove(fileName); err != nil {
return err
}
}
}
// move the new dataFile to data Dir
for _, fileName := range mergeFileNames {
srcPath := filepath.Join(mergePath, fileName)
dstPath := filepath.Join(db.options.DirPath, fileName)
if err := os.Rename(srcPath, dstPath); err != nil {
return err
}
}
return nil
}
func (db *DB) getNonMergeFileId(dirPath string) (uint32, error) {
mergeFinishedFile, err := data.OpenMergeFinishedFile(dirPath)
if err != nil {
return 0, err
}
record, _, err := mergeFinishedFile.ReadLogRecord(0)
if err != nil {
return 0, err
}
nonMergeFileId, err := strconv.Atoi(string(record.Value))
if err != nil {
return 0, err
}
return uint32(nonMergeFileId), nil
}
func (db *DB) loadIndexFromHintFile() error {
// check hint file is exist
hintFileName := filepath.Join(db.options.DirPath, public.HintFileName)
if _, err := os.Stat(hintFileName); os.IsNotExist(err) {
return nil
}
// open the hint file
hintFile, err := data.OpenHintFile(db.options.DirPath)
if err != nil {
return err
}
// read the index from the file
var offset int64
for {
logRecord, size, err := hintFile.ReadLogRecord(offset)
if err != nil {
if err == io.EOF {
break
}
return err
}
// get the real pos index
pos := data.DecodeLogRecordPos(logRecord.Value)
db.index.getStrIndex().Put(logRecord.Key, pos)
offset += size
}
return nil
}