重构:简化查询接口

- RecordQuery.QueryOldest 和 QueryNewest 不再接收 startIdx/endIdx 参数
- 查询方法返回纯 Record 列表,状态判断移到调用方
- TopicProcessor 的查询方法负责添加状态信息
- 更新所有测试文件以适配新接口
This commit is contained in:
2025-10-04 00:10:14 +08:00
parent a421ca1d85
commit 5c028a55b3
8 changed files with 168 additions and 208 deletions

View File

@@ -344,6 +344,12 @@ func (tp *TopicProcessor) Query() *RecordQuery {
// count: 查询数量
// 返回的记录包含状态信息(基于 tailer 的窗口索引),按时间顺序(索引递增方向)
func (tp *TopicProcessor) QueryOldest(startIndex, count int) ([]*RecordWithStatus, error) {
// 查询记录
records, err := tp.query.QueryOldest(startIndex, count)
if err != nil {
return nil, err
}
// 获取窗口索引范围(用于状态判断)
var startIdx, endIdx int
tp.mu.RLock()
@@ -353,7 +359,16 @@ func (tp *TopicProcessor) QueryOldest(startIndex, count int) ([]*RecordWithStatu
}
tp.mu.RUnlock()
return tp.query.QueryOldest(startIndex, count, startIdx, endIdx)
// 为每条记录添加状态
results := make([]*RecordWithStatus, len(records))
for i, rec := range records {
results[i] = &RecordWithStatus{
Record: rec,
Status: GetRecordStatus(startIndex+i, startIdx, endIdx),
}
}
return results, nil
}
// QueryNewest 从指定索引开始向后查询记录(索引递减方向)
@@ -361,6 +376,12 @@ func (tp *TopicProcessor) QueryOldest(startIndex, count int) ([]*RecordWithStatu
// count: 查询数量
// 返回的记录包含状态信息(基于 tailer 的窗口索引),按时间倒序(最新在前)
func (tp *TopicProcessor) QueryNewest(endIndex, count int) ([]*RecordWithStatus, error) {
// 查询记录
records, err := tp.query.QueryNewest(endIndex, count)
if err != nil {
return nil, err
}
// 获取窗口索引范围(用于状态判断)
var startIdx, endIdx int
tp.mu.RLock()
@@ -370,7 +391,16 @@ func (tp *TopicProcessor) QueryNewest(endIndex, count int) ([]*RecordWithStatus,
}
tp.mu.RUnlock()
return tp.query.QueryNewest(endIndex, count, startIdx, endIdx)
// 为每条记录添加状态倒序endIndex, endIndex-1, ...
results := make([]*RecordWithStatus, len(records))
for i, rec := range records {
results[i] = &RecordWithStatus{
Record: rec,
Status: GetRecordStatus(endIndex-i, startIdx, endIdx),
}
}
return results, nil
}
// GetRecordCount 获取记录总数(统一接口)