重构:简化 RecordIndex 结构体

删除独立的 IndexHeader 结构体,将 magic 和 version 字段直接放入 RecordIndex,减少不必要的嵌套层级。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-03 23:55:18 +08:00
parent de39339620
commit a421ca1d85
2 changed files with 14 additions and 21 deletions

View File

@@ -21,19 +21,14 @@ const (
IndexEntrySize = 8 // Offset(8)
)
// IndexHeader 索引文件头部
type IndexHeader struct {
Magic uint32 // 魔数,用于识别索引文件
Version uint32 // 版本号
}
// RecordIndex 记录索引管理器
type RecordIndex struct {
logPath string // 日志文件路径
indexPath string // 索引文件路径
offsets []int64 // 内存中的偏移索引
header IndexHeader // 索引文件头部
indexFile *os.File // 索引文件句柄(用于追加写入)
logPath string // 日志文件路径
indexPath string // 索引文件路径
offsets []int64 // 内存中的偏移索引
magic uint32 // 魔数,用于识别索引文件
version uint32 // 版本号
indexFile *os.File // 索引文件句柄(用于追加写入)
}
// NewRecordIndex 创建或加载记录索引
@@ -45,10 +40,8 @@ func NewRecordIndex(logPath string) (*RecordIndex, error) {
logPath: logPath,
indexPath: indexPath,
offsets: make([]int64, 0, 1024),
header: IndexHeader{
Magic: IndexMagic,
Version: IndexVersion,
},
magic: IndexMagic,
version: IndexVersion,
}
// 启动时总是从日志文件重建索引
@@ -122,8 +115,8 @@ func (ri *RecordIndex) save() error {
// 写入头部
headerBuf := make([]byte, IndexHeaderSize)
binary.LittleEndian.PutUint32(headerBuf[0:4], ri.header.Magic)
binary.LittleEndian.PutUint32(headerBuf[4:8], ri.header.Version)
binary.LittleEndian.PutUint32(headerBuf[0:4], ri.magic)
binary.LittleEndian.PutUint32(headerBuf[4:8], ri.version)
if _, err := f.Write(headerBuf); err != nil {
return fmt.Errorf("write header: %w", err)

View File

@@ -272,12 +272,12 @@ func TestIndexHeader(t *testing.T) {
writer.Close()
// 验证魔数和版本
if index.header.Magic != IndexMagic {
t.Errorf("Magic 不正确: got 0x%X, want 0x%X", index.header.Magic, IndexMagic)
if index.magic != IndexMagic {
t.Errorf("Magic 不正确: got 0x%X, want 0x%X", index.magic, IndexMagic)
}
if index.header.Version != IndexVersion {
t.Errorf("Version 不正确: got %d, want %d", index.header.Version, IndexVersion)
if index.version != IndexVersion {
t.Errorf("Version 不正确: got %d, want %d", index.version, IndexVersion)
}
// 验证记录总数(从内存索引计算)