Load the data file from disk
()
| 380 | |
| 381 | // Load the data file from disk |
| 382 | func (db *DB) loadDataFiles() error { |
| 383 | dirEntry, err := os.ReadDir(db.options.DirPath) |
| 384 | if err != nil { |
| 385 | return nil |
| 386 | } |
| 387 | |
| 388 | var fileIds []int |
| 389 | // Walk through all the files in the directory, finding all files ending in '.data' |
| 390 | for _, entry := range dirEntry { |
| 391 | if strings.HasSuffix(entry.Name(), data2.DataFileSuffix) { |
| 392 | splitNames := strings.Split(entry.Name(), ".") |
| 393 | fileID, err := strconv.Atoi(splitNames[0]) |
| 394 | // The data directory may be corrupted |
| 395 | if err != nil { |
| 396 | return _const.ErrDataDirectoryCorrupted |
| 397 | } |
| 398 | |
| 399 | fileIds = append(fileIds, fileID) |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Sort file ids and load them from smallest to largest |
| 404 | sort.Ints(fileIds) |
| 405 | db.fileIds = fileIds |
| 406 | |
| 407 | // Walk through each file id and open the corresponding data file |
| 408 | for i, fid := range fileIds { |
| 409 | dataFile, err := data2.OpenDataFile(db.options.DirPath, uint32(fid), db.options.DataFileSize, db.options.FIOType) |
| 410 | if err != nil { |
| 411 | return err |
| 412 | } |
| 413 | if i == len(fileIds)-1 { |
| 414 | // The last id is the largest, indicating that the current file is active |
| 415 | db.activeFile = dataFile |
| 416 | } else { |
| 417 | // Note It is an old data file |
| 418 | db.olderFiles[uint32(fid)] = dataFile |
| 419 | } |
| 420 | } |
| 421 | return nil |
| 422 | } |
| 423 | |
| 424 | // Load the index from the data file |
| 425 | // Iterate over all the records in the file and update them to the memory index |