forked from yonggaogit/bitcask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
377 lines (319 loc) · 7.59 KB
/
db.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package bitcask
import (
"bitcask/data"
"bitcask/index"
"errors"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
)
type DB struct {
options Options
mu *sync.RWMutex
activeFile *data.DataFile
olderFiles map[uint32]*data.DataFile
index index.Indexer
fileIds []int
seqNo uint64
isMerging bool
}
func (db *DB) Put(key []byte, value []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
logRecord := &data.LogRecord{
Key: logRecordKeyWithSeq(key, nonTransactionSeqNo),
Value: value,
Type: data.LogRecordNormal,
}
pos, err := db.appendLogRecordWithLock(logRecord)
if err != nil {
return nil
}
if ok := db.index.Put(key, pos); !ok {
return ErrIndexUpdateFailed
}
return nil
}
func (db *DB) Get(key []byte) ([]byte, error) {
db.mu.Lock()
defer db.mu.Unlock()
if len(key) == 0 {
return nil, ErrKeyIsEmpty
}
logRecordPos := db.index.Get(key)
if logRecordPos == nil {
return nil, ErrKeyNotFound
}
return db.getValueByPosition(logRecordPos)
}
func (db *DB) getValueByPosition(logRecordPos *data.LogRecordPos) ([]byte, error) {
var dataFile *data.DataFile
if db.activeFile.FileId == logRecordPos.Fid {
dataFile = db.activeFile
} else {
dataFile = db.olderFiles[logRecordPos.Fid]
}
if dataFile == nil {
return nil, ErrDataFileNotFound
}
logRecord, _, err := dataFile.ReadLogRecord(logRecordPos.Offset)
if err != nil {
return nil, err
}
if logRecord.Type == data.LogRecordDeleted {
return nil, ErrKeyNotFound
}
return logRecord.Value, nil
}
func (db *DB) appendLogRecordWithLock(record *data.LogRecord) (*data.LogRecordPos, error) {
db.mu.Lock()
defer db.mu.Unlock()
return db.appendLogRecord(record)
}
func (db *DB) appendLogRecord(record *data.LogRecord) (*data.LogRecordPos, error) {
if db.activeFile == nil {
if err := db.setActiveDataFile(); err != nil {
return nil, err
}
}
encRecord, size := data.EncodeLogRecord(record)
if db.activeFile.WriteOff+size > db.options.DataFileSize {
if err := db.activeFile.Sync(); err != nil {
return nil, err
}
db.olderFiles[db.activeFile.FileId] = db.activeFile
if err := db.setActiveDataFile(); err != nil {
return nil, err
}
}
writeOff := db.activeFile.WriteOff
if err := db.activeFile.Write(encRecord); err != nil {
return nil, err
}
if db.options.SynWrite {
if err := db.activeFile.Sync(); err != nil {
return nil, err
}
}
pos := &data.LogRecordPos{Fid: db.activeFile.FileId, Offset: writeOff}
return pos, nil
}
func (db *DB) setActiveDataFile() error {
var initialFileId uint32 = 0
if db.activeFile != nil {
initialFileId = db.activeFile.FileId + 1
}
dataFile, err := data.OpenDataFile(db.options.DirPath, initialFileId)
if err != nil {
return err
}
db.activeFile = dataFile
return nil
}
func checkOptions(options Options) error {
if options.DirPath == "" {
return errors.New("database dir path is empty")
}
if options.DataFileSize <= 0 {
return errors.New("database data file size must be greater than 0")
}
return nil
}
func Open(options Options) (*DB, error) {
if err := checkOptions(options); err != nil {
return nil, err
}
if _, err := os.Stat(options.DirPath); os.IsNotExist(err) {
if err := os.MkdirAll(options.DirPath, os.ModePerm); err != nil {
return nil, err
}
}
db := &DB{
options: options,
mu: new(sync.RWMutex),
olderFiles: make(map[uint32]*data.DataFile),
index: index.NewIndexer(options.IndexType),
}
// 加载 merge 数据目录
if err := db.loadMergeFiles(); err != nil {
return nil, err
}
// 加载数据文件
if err := db.loadDataFiles(); err != nil {
return nil, err
}
// 从 hint 索引文件中加载索引
if err := db.loadIndexFromHintFile(); err != nil {
return nil, err
}
// 从数据文件中加载索引
if err := db.loadIndexFromDataFiles(); err != nil {
return nil, err
}
return db, nil
}
func (db *DB) loadDataFiles() error {
dirEntries, err := os.ReadDir(db.options.DirPath)
if err != nil {
return err
}
var fileIds []int
for _, entry := range dirEntries {
if strings.HasSuffix(entry.Name(), data.DataFileNameSuffix) {
splitNames := strings.Split(entry.Name(), ".")
fileId, err := strconv.Atoi(splitNames[0])
if err != nil {
return err
}
fileIds = append(fileIds, fileId)
}
}
sort.Ints(fileIds)
db.fileIds = fileIds
for i, fid := range fileIds {
dataFile, err := data.OpenDataFile(db.options.DirPath, uint32(fid))
if err != nil {
return err
}
if i == len(fileIds)-1 {
db.activeFile = dataFile
} else {
db.olderFiles[uint32(fid)] = dataFile
}
}
return nil
}
func (db *DB) loadIndexFromDataFiles() error {
if len(db.fileIds) == 0 {
return nil
}
// 查看是否发生过 merge
hasMerge, nonMergeFileId := false, uint32(0)
mergeFinFileName := filepath.Join(db.options.DirPath, data.MergeFinishedFileName)
if _, err := os.Stat(mergeFinFileName); err == nil {
fid, err := db.getNonMergeFileId(db.options.DirPath)
if err != nil {
return err
}
hasMerge = true
nonMergeFileId = fid
}
updateIndex := func(key []byte, typ data.LogRecordType, pos *data.LogRecordPos) {
var ok bool
if typ == data.LogRecordDeleted {
ok = db.index.Delete(key)
} else {
ok = db.index.Put(key, pos)
}
if !ok {
panic("failed to update index at startup")
}
}
// 暂存事务数据
transactionRecords := make(map[uint64][]*data.TransactionRecord)
var currentSeqNo = nonTransactionSeqNo
for i, fid := range db.fileIds {
var fileId = uint32(fid)
// 说明已经从 hint 文件加载过了
if hasMerge && fileId < nonMergeFileId {
continue
}
var dataFile *data.DataFile
if fileId == db.activeFile.FileId {
dataFile = db.activeFile
} else {
dataFile = db.olderFiles[fileId]
}
var offset int64 = 0
for {
logRecord, size, err := dataFile.ReadLogRecord(offset)
if err != nil {
if err == io.EOF {
break
}
return err
}
logRecordPos := &data.LogRecordPos{
Fid: fileId,
Offset: offset,
}
// 解析 key,拿到事务序号
realKey, seqNo := parseLogRecordKey(logRecord.Key)
if seqNo == nonTransactionSeqNo {
updateIndex(realKey, logRecord.Type, logRecordPos)
} else {
if logRecord.Type == data.LogRecordTxnFinished {
for _, txnRecord := range transactionRecords[seqNo] {
updateIndex(txnRecord.Record.Key, txnRecord.Record.Type, txnRecord.Pos)
}
delete(transactionRecords, seqNo)
} else {
logRecord.Key = realKey
transactionRecords[seqNo] = append(transactionRecords[seqNo], &data.TransactionRecord{
Record: logRecord,
Pos: logRecordPos,
})
}
}
if seqNo > currentSeqNo {
currentSeqNo = seqNo
}
offset += size
}
if i == len(db.fileIds)-1 {
db.activeFile.WriteOff = offset
}
}
db.seqNo = currentSeqNo
return nil
}
func (db *DB) Delete(key []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
if pos := db.index.Get(key); pos == nil {
return nil
}
logRecord := &data.LogRecord{
Key: logRecordKeyWithSeq(key, nonTransactionSeqNo),
Type: data.LogRecordDeleted,
}
_, err := db.appendLogRecordWithLock(logRecord)
if err != nil {
return nil
}
ok := db.index.Delete(key)
if !ok {
return ErrIndexUpdateFailed
}
return nil
}
func (db *DB) Close() error {
if db.activeFile == nil {
return nil
}
db.mu.Lock()
defer db.mu.Unlock()
if err := db.activeFile.Close(); err != nil {
return err
}
for _, file := range db.olderFiles {
if err := file.Close(); err != nil {
return err
}
}
return nil
}
func (db *DB) Sync() error {
if db.activeFile == nil {
return nil
}
db.mu.Lock()
defer db.mu.Unlock()
return db.activeFile.Sync()
}