重构:为核心组件实现 Reset 方法优化重置机制

为所有核心组件添加 Reset() 方法:
- LogWriter.Reset(): 删除并重新创建日志文件,保持 index 和 wbuf 引用不变
- RecordIndex.Reset(): 清空索引数据并重新创建索引文件
- RecordQuery.Reset(): 关闭并重新打开日志文件
- ProcessCursor.Reset(): 删除位置文件并重置游标位置
- LogTailer.Reset(): 重置内部 channel 状态

优化 TopicProcessor.Reset() 实现:
- 不再销毁和重建组件对象
- 通过调用各组件的 Reset() 方法重置状态
- 保持组件间引用关系稳定
- 减少代码行数约 20 行
- 避免空指针风险和内存分配开销

代码改进:
- LogWriter 添加 path 字段用于重置
- 移除 topic_processor.go 中未使用的 os import
- 职责分离更清晰,每个组件管理自己的重置逻辑

测试结果:
- TestTopicReset: PASS
- TestTopicResetWithPendingRecords: PASS
- 所有 TopicProcessor 相关测试通过

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-04 21:58:54 +08:00
parent bcc328b129
commit 6fb0731935
6 changed files with 184 additions and 54 deletions

View File

@@ -172,6 +172,36 @@ func (c *ProcessCursor) Close() error {
return c.fd.Close()
}
// Reset 重置游标,删除位置文件并重新打开日志文件
// 保持 index 和 writer 引用不变
func (c *ProcessCursor) Reset() error {
// 关闭文件
if c.fd != nil {
if err := c.fd.Close(); err != nil {
return err
}
c.fd = nil
}
// 删除位置文件
if err := os.Remove(c.posFile); err != nil && !os.IsNotExist(err) {
return err
}
// 重新打开日志文件
fd, err := os.Open(c.path)
if err != nil {
return err
}
// 重置状态
c.fd = fd
c.startIdx = 0
c.endIdx = 0
return nil
}
// savePosition 保存当前读取位置到文件
func (c *ProcessCursor) savePosition() error {
f, err := os.Create(c.posFile)