重构:简化查询接口
- RecordQuery.QueryOldest 和 QueryNewest 不再接收 startIdx/endIdx 参数 - 查询方法返回纯 Record 列表,状态判断移到调用方 - TopicProcessor 的查询方法负责添加状态信息 - 更新所有测试文件以适配新接口
This commit is contained in:
@@ -63,15 +63,12 @@ func main() {
|
||||
}
|
||||
fmt.Printf("记录总数: %d\n", count)
|
||||
|
||||
// 可以直接使用共享的索引获取偏移量
|
||||
offset, err := index.GetOffset(5)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("第 5 条记录的偏移: %d\n", offset)
|
||||
// 从第 5 条记录开始查询
|
||||
startIndex := 5
|
||||
fmt.Printf("从第 %d 条记录开始查询\n", startIndex)
|
||||
|
||||
// 向后查询(使用索引,高效)
|
||||
backward, err := query.QueryAt(offset, -1, 3, 0, offset)
|
||||
// 向后查询(查询更早的记录)
|
||||
backward, err := query.QueryNewest(startIndex-1, 3, 0, startIndex)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -80,8 +77,8 @@ func main() {
|
||||
fmt.Printf(" [%d] 状态=%s, 数据=%s\n", i, rws.Status, string(rws.Record.Data))
|
||||
}
|
||||
|
||||
// 向前查询(顺序读取)
|
||||
forward, err := query.QueryAt(offset, 1, 3, 0, offset)
|
||||
// 向前查询(查询更新的记录)
|
||||
forward, err := query.QueryOldest(startIndex, 3, 0, startIndex)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -541,47 +541,30 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
|
||||
startIdx := seq.GetProcessingIndex(topic)
|
||||
endIdx := seq.GetReadIndex(topic)
|
||||
|
||||
// 获取索引用于转换
|
||||
processor, err := seq.GetProcessor(topic)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
index := processor.Index()
|
||||
|
||||
// 合并查询结果:向后 + 当前 + 向前
|
||||
var results []*seqlog.RecordWithStatus
|
||||
|
||||
// 向后查询
|
||||
if backward > 0 && startIdx > 0 {
|
||||
startPos, err := index.GetOffset(startIdx)
|
||||
backResults, err := query.QueryNewest(startIdx-1, backward, startIdx, endIdx)
|
||||
if err == nil {
|
||||
backResults, err := query.QueryAt(startPos, -1, backward, startIdx, endIdx)
|
||||
if err == nil {
|
||||
results = append(results, backResults...)
|
||||
}
|
||||
results = append(results, backResults...)
|
||||
}
|
||||
}
|
||||
|
||||
// 当前位置
|
||||
if startIdx < endIdx {
|
||||
startPos, err := index.GetOffset(startIdx)
|
||||
currentResults, err := query.QueryOldest(startIdx, 1, startIdx, endIdx)
|
||||
if err == nil {
|
||||
currentResults, err := query.QueryAt(startPos, 0, 1, startIdx, endIdx)
|
||||
if err == nil {
|
||||
results = append(results, currentResults...)
|
||||
}
|
||||
results = append(results, currentResults...)
|
||||
}
|
||||
}
|
||||
|
||||
// 向前查询
|
||||
if forward > 0 {
|
||||
startPos, err := index.GetOffset(startIdx)
|
||||
forwardResults, err := query.QueryOldest(endIdx, forward, startIdx, endIdx)
|
||||
if err == nil {
|
||||
forwardResults, err := query.QueryAt(startPos, 1, forward, startIdx, endIdx)
|
||||
if err == nil {
|
||||
results = append(results, forwardResults...)
|
||||
}
|
||||
results = append(results, forwardResults...)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user