重构:简化查询接口
- RecordQuery.QueryOldest 和 QueryNewest 不再接收 startIdx/endIdx 参数 - 查询方法返回纯 Record 列表,状态判断移到调用方 - TopicProcessor 的查询方法负责添加状态信息 - 更新所有测试文件以适配新接口
This commit is contained in:
@@ -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 获取记录总数(统一接口)
|
||||
|
||||
Reference in New Issue
Block a user