重构:简化 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:
19
index.go
19
index.go
@@ -21,18 +21,13 @@ const (
|
|||||||
IndexEntrySize = 8 // Offset(8)
|
IndexEntrySize = 8 // Offset(8)
|
||||||
)
|
)
|
||||||
|
|
||||||
// IndexHeader 索引文件头部
|
|
||||||
type IndexHeader struct {
|
|
||||||
Magic uint32 // 魔数,用于识别索引文件
|
|
||||||
Version uint32 // 版本号
|
|
||||||
}
|
|
||||||
|
|
||||||
// RecordIndex 记录索引管理器
|
// RecordIndex 记录索引管理器
|
||||||
type RecordIndex struct {
|
type RecordIndex struct {
|
||||||
logPath string // 日志文件路径
|
logPath string // 日志文件路径
|
||||||
indexPath string // 索引文件路径
|
indexPath string // 索引文件路径
|
||||||
offsets []int64 // 内存中的偏移索引
|
offsets []int64 // 内存中的偏移索引
|
||||||
header IndexHeader // 索引文件头部
|
magic uint32 // 魔数,用于识别索引文件
|
||||||
|
version uint32 // 版本号
|
||||||
indexFile *os.File // 索引文件句柄(用于追加写入)
|
indexFile *os.File // 索引文件句柄(用于追加写入)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,10 +40,8 @@ func NewRecordIndex(logPath string) (*RecordIndex, error) {
|
|||||||
logPath: logPath,
|
logPath: logPath,
|
||||||
indexPath: indexPath,
|
indexPath: indexPath,
|
||||||
offsets: make([]int64, 0, 1024),
|
offsets: make([]int64, 0, 1024),
|
||||||
header: IndexHeader{
|
magic: IndexMagic,
|
||||||
Magic: IndexMagic,
|
version: IndexVersion,
|
||||||
Version: IndexVersion,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启动时总是从日志文件重建索引
|
// 启动时总是从日志文件重建索引
|
||||||
@@ -122,8 +115,8 @@ func (ri *RecordIndex) save() error {
|
|||||||
|
|
||||||
// 写入头部
|
// 写入头部
|
||||||
headerBuf := make([]byte, IndexHeaderSize)
|
headerBuf := make([]byte, IndexHeaderSize)
|
||||||
binary.LittleEndian.PutUint32(headerBuf[0:4], ri.header.Magic)
|
binary.LittleEndian.PutUint32(headerBuf[0:4], ri.magic)
|
||||||
binary.LittleEndian.PutUint32(headerBuf[4:8], ri.header.Version)
|
binary.LittleEndian.PutUint32(headerBuf[4:8], ri.version)
|
||||||
|
|
||||||
if _, err := f.Write(headerBuf); err != nil {
|
if _, err := f.Write(headerBuf); err != nil {
|
||||||
return fmt.Errorf("write header: %w", err)
|
return fmt.Errorf("write header: %w", err)
|
||||||
|
|||||||
@@ -272,12 +272,12 @@ func TestIndexHeader(t *testing.T) {
|
|||||||
writer.Close()
|
writer.Close()
|
||||||
|
|
||||||
// 验证魔数和版本
|
// 验证魔数和版本
|
||||||
if index.header.Magic != IndexMagic {
|
if index.magic != IndexMagic {
|
||||||
t.Errorf("Magic 不正确: got 0x%X, want 0x%X", index.header.Magic, IndexMagic)
|
t.Errorf("Magic 不正确: got 0x%X, want 0x%X", index.magic, IndexMagic)
|
||||||
}
|
}
|
||||||
|
|
||||||
if index.header.Version != IndexVersion {
|
if index.version != IndexVersion {
|
||||||
t.Errorf("Version 不正确: got %d, want %d", index.header.Version, IndexVersion)
|
t.Errorf("Version 不正确: got %d, want %d", index.version, IndexVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证记录总数(从内存索引计算)
|
// 验证记录总数(从内存索引计算)
|
||||||
|
|||||||
Reference in New Issue
Block a user