Load the index from the data file Iterate over all the records in the file and update them to the memory index
()
| 424 | // Load the index from the data file |
| 425 | // Iterate over all the records in the file and update them to the memory index |
| 426 | func (db *DB) loadIndexFromDataFiles() error { |
| 427 | // If there is no file, the database is empty |
| 428 | if len(db.fileIds) == 0 { |
| 429 | return nil |
| 430 | } |
| 431 | |
| 432 | // Check whether the merge occurred |
| 433 | var hasMerge bool = false |
| 434 | var nonMergeFileId uint32 = 0 |
| 435 | mergeFileName := filepath.Join(db.options.DirPath, data2.MergeFinaFileSuffix) |
| 436 | // If a file exists, retrieve the id of the file that did not participate in the merge |
| 437 | if _, err := os.Stat(mergeFileName); err == nil { |
| 438 | // Check if the merge file exists |
| 439 | // If it exists, determine the ID of the most recently non-merged file |
| 440 | fileId, err := db.getRecentlyNonMergeFileId(db.options.DirPath) |
| 441 | if err != nil { |
| 442 | return err |
| 443 | } |
| 444 | nonMergeFileId = fileId |
| 445 | hasMerge = true |
| 446 | } |
| 447 | |
| 448 | // Define a function to update the in-memory index |
| 449 | updataIndex := func(key []byte, typ data2.LogRecrdType, pst *data2.LogRecordPst) { |
| 450 | var ok bool |
| 451 | if typ == data2.LogRecordDeleted { |
| 452 | // If the log record type is 'deleted', delete the key from the index |
| 453 | ok = db.index.Delete(key) |
| 454 | } else { |
| 455 | // Otherwise, update the key with the new position in the index |
| 456 | ok = db.index.Put(key, pst) |
| 457 | } |
| 458 | if !ok { |
| 459 | // Panic if the index update fails |
| 460 | panic(_const.ErrIndexUpdateFailed) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Temporary transaction data |
| 465 | transactionRecords := make(map[uint64][]*data2.TransactionRecord) |
| 466 | var currentSeqNo = nonTransactionSeqNo |
| 467 | |
| 468 | // Iterate through all file ids, processing records in the file |
| 469 | for i, fid := range db.fileIds { |
| 470 | var fileID = uint32(fid) |
| 471 | // If the id is smaller than that of the file that did not participate in the merge recently, |
| 472 | // the hint file has been loaded |
| 473 | if hasMerge && fileID < nonMergeFileId { |
| 474 | continue |
| 475 | } |
| 476 | |
| 477 | var dataFile *data2.DataFile |
| 478 | if fileID == db.activeFile.FileID { |
| 479 | dataFile = db.activeFile |
| 480 | } else { |
| 481 | dataFile = db.olderFiles[fileID] |
| 482 | } |
| 483 |
no test coverage detected