重构:简化查询接口
- RecordQuery.QueryOldest 和 QueryNewest 不再接收 startIdx/endIdx 参数 - 查询方法返回纯 Record 列表,状态判断移到调用方 - TopicProcessor 的查询方法负责添加状态信息 - 更新所有测试文件以适配新接口
This commit is contained in:
@@ -1211,7 +1211,6 @@ func TestRecordQuery(t *testing.T) {
|
||||
writer.Close()
|
||||
|
||||
// 模拟处理到第 5 条记录
|
||||
currentPos := offsets[5]
|
||||
// 窗口范围:[索引 5, 索引 6)
|
||||
startIdx := 5
|
||||
endIdx := 6
|
||||
@@ -1231,41 +1230,45 @@ func TestRecordQuery(t *testing.T) {
|
||||
defer query.Close()
|
||||
|
||||
// 测试查询当前位置
|
||||
current, err := query.QueryAt(currentPos, 0, 1, startIdx, endIdx)
|
||||
current, err := query.QueryOldest(startIdx, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query current: %v", err)
|
||||
}
|
||||
if len(current) != 1 {
|
||||
t.Fatalf("expected 1 current result, got %d", len(current))
|
||||
}
|
||||
if string(current[0].Record.Data) != "message 5" {
|
||||
t.Errorf("expected current 'message 5', got '%s'", string(current[0].Record.Data))
|
||||
if string(current[0].Data) != "message 5" {
|
||||
t.Errorf("expected current 'message 5', got '%s'", string(current[0].Data))
|
||||
}
|
||||
if current[0].Status != StatusProcessing {
|
||||
t.Errorf("expected status Processing, got %s", current[0].Status)
|
||||
// 手动判断状态
|
||||
status := GetRecordStatus(startIdx, startIdx, endIdx)
|
||||
if status != StatusProcessing {
|
||||
t.Errorf("expected status Processing, got %s", status)
|
||||
}
|
||||
|
||||
// 测试向后查询(查询更早的记录)
|
||||
backResults, err := query.QueryAt(currentPos, -1, 3, startIdx, endIdx)
|
||||
// 测试向后查询(查询更早的记录,返回倒序)
|
||||
backResults, err := query.QueryNewest(startIdx-1, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query backward: %v", err)
|
||||
}
|
||||
if len(backResults) != 3 {
|
||||
t.Errorf("expected 3 backward results, got %d", len(backResults))
|
||||
}
|
||||
// 向后查询返回顺序结果
|
||||
expectedBack := []string{"message 2", "message 3", "message 4"}
|
||||
// 向后查询返回倒序结果(newest first)
|
||||
expectedBack := []string{"message 4", "message 3", "message 2"}
|
||||
for i, rec := range backResults {
|
||||
if string(rec.Record.Data) != expectedBack[i] {
|
||||
t.Errorf("backward[%d]: expected '%s', got '%s'", i, expectedBack[i], string(rec.Record.Data))
|
||||
if string(rec.Data) != expectedBack[i] {
|
||||
t.Errorf("backward[%d]: expected '%s', got '%s'", i, expectedBack[i], string(rec.Data))
|
||||
}
|
||||
if rec.Status != StatusProcessed {
|
||||
t.Errorf("backward[%d]: expected status Processed, got %s", i, rec.Status)
|
||||
// 手动判断状态:索引 4, 3, 2
|
||||
recStatus := GetRecordStatus(startIdx-1-i, startIdx, endIdx)
|
||||
if recStatus != StatusProcessed {
|
||||
t.Errorf("backward[%d]: expected status Processed, got %s", i, recStatus)
|
||||
}
|
||||
}
|
||||
|
||||
// 测试向前查询(查询更新的记录)
|
||||
forwardResults, err := query.QueryAt(currentPos, 1, 3, startIdx, endIdx)
|
||||
forwardResults, err := query.QueryOldest(endIdx, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query forward: %v", err)
|
||||
}
|
||||
@@ -1275,11 +1278,13 @@ func TestRecordQuery(t *testing.T) {
|
||||
// 向前查询返回顺序结果
|
||||
expectedForward := []string{"message 6", "message 7", "message 8"}
|
||||
for i, rec := range forwardResults {
|
||||
if string(rec.Record.Data) != expectedForward[i] {
|
||||
t.Errorf("forward[%d]: expected '%s', got '%s'", i, expectedForward[i], string(rec.Record.Data))
|
||||
if string(rec.Data) != expectedForward[i] {
|
||||
t.Errorf("forward[%d]: expected '%s', got '%s'", i, expectedForward[i], string(rec.Data))
|
||||
}
|
||||
if rec.Status != StatusPending {
|
||||
t.Errorf("forward[%d]: expected status Pending, got %s", i, rec.Status)
|
||||
// 手动判断状态:索引 6, 7, 8
|
||||
recStatus := GetRecordStatus(endIdx+i, startIdx, endIdx)
|
||||
if recStatus != StatusPending {
|
||||
t.Errorf("forward[%d]: expected status Pending, got %s", i, recStatus)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1329,14 +1334,9 @@ func TestTopicQuery(t *testing.T) {
|
||||
endIdx := processor.GetReadIndex()
|
||||
t.Logf("Processing index: [%d, %d)", startIdx, endIdx)
|
||||
|
||||
// 获取共享查询器
|
||||
query := processor.Query()
|
||||
index := processor.Index()
|
||||
|
||||
// 测试查询当前位置
|
||||
// 测试查询当前位置(使用 processor 方法,带状态)
|
||||
if startIdx < endIdx {
|
||||
startPos, _ := index.GetOffset(startIdx)
|
||||
current, err := query.QueryAt(startPos, 0, 1, startIdx, endIdx)
|
||||
current, err := processor.QueryOldest(startIdx, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query current: %v", err)
|
||||
}
|
||||
@@ -1347,8 +1347,7 @@ func TestTopicQuery(t *testing.T) {
|
||||
|
||||
// 测试向后查询
|
||||
if startIdx > 0 {
|
||||
startPos, _ := index.GetOffset(startIdx)
|
||||
back, err := query.QueryAt(startPos, -1, 2, startIdx, endIdx)
|
||||
back, err := processor.QueryNewest(startIdx-1, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query backward: %v", err)
|
||||
}
|
||||
@@ -1358,9 +1357,8 @@ func TestTopicQuery(t *testing.T) {
|
||||
}
|
||||
|
||||
// 测试向前查询
|
||||
if startIdx < index.Count() {
|
||||
startPos, _ := index.GetOffset(startIdx)
|
||||
forward, err := query.QueryAt(startPos, 1, 3, startIdx, endIdx)
|
||||
if startIdx < processor.GetRecordCount() {
|
||||
forward, err := processor.QueryOldest(endIdx, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query forward: %v", err)
|
||||
}
|
||||
@@ -1414,14 +1412,13 @@ func TestSeqlogQuery(t *testing.T) {
|
||||
endIdx := seq.GetReadIndex("app")
|
||||
t.Logf("Processing index: [%d, %d)", startIdx, endIdx)
|
||||
|
||||
// 获取 index 用于转换索引到 offset
|
||||
// 获取 processor 用于查询(带状态)
|
||||
processor, _ := seq.GetProcessor("app")
|
||||
index := processor.Index()
|
||||
|
||||
// 测试查询当前
|
||||
if startIdx < endIdx {
|
||||
startPos, _ := index.GetOffset(startIdx)
|
||||
current, err := query.QueryAt(startPos, 0, 1, startIdx, endIdx)
|
||||
current, err := processor.QueryOldest(startIdx, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query current: %v", err)
|
||||
}
|
||||
@@ -1432,8 +1429,7 @@ func TestSeqlogQuery(t *testing.T) {
|
||||
|
||||
// 测试向后查询
|
||||
if startIdx > 0 {
|
||||
startPos, _ := index.GetOffset(startIdx)
|
||||
back, err := query.QueryAt(startPos, -1, 2, startIdx, endIdx)
|
||||
back, err := processor.QueryNewest(startIdx-1, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query backward: %v", err)
|
||||
}
|
||||
@@ -1444,8 +1440,7 @@ func TestSeqlogQuery(t *testing.T) {
|
||||
|
||||
// 测试向前查询
|
||||
if startIdx < index.Count() {
|
||||
startPos, _ := index.GetOffset(startIdx)
|
||||
forward, err := query.QueryAt(startPos, 1, 3, startIdx, endIdx)
|
||||
forward, err := processor.QueryOldest(endIdx, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query forward: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user