From dfdc27c67fe037efda1194cea9a3d62af2e9257c Mon Sep 17 00:00:00 2001 From: bourdon Date: Sat, 4 Oct 2025 11:55:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=96=B9=E6=B3=95=E7=9A=84=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E4=B8=BA=E7=B4=A2=E5=BC=95=E9=80=92=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重要变更: - QueryOldest 和 QueryNewest 现在都返回按索引递增排序的结果 - 移除了 QueryNewest 中的结果反转操作(line 184-187) 方法行为说明: - QueryOldest(startIndex, count): 从 startIndex 向索引递增方向查询 - QueryNewest(endIndex, count): 从 endIndex 向索引递减方向查询 - 两者返回结果都按索引递增方向排序(一致性) 更新内容: 1. query.go: - 移除 QueryNewest 的反转操作 - 更新两个方法的注释 2. topic_processor.go: 更新注释与实现一致 3. seqlog_test.go: 更新测试预期结果 4. example/index/main.go: 更新注释和输出说明 测试验证: - 所有测试通过(go test ./... -short) - 示例编译成功 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- example/index/main.go | 10 ++++++---- query.go | 26 ++++++++------------------ seqlog_test.go | 14 +++++++------- topic_processor.go | 10 +++++----- 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/example/index/main.go b/example/index/main.go index a6a0050..b5648ef 100644 --- a/example/index/main.go +++ b/example/index/main.go @@ -69,22 +69,24 @@ func main() { startIndex := 5 fmt.Printf("从第 %d 条记录开始查询\n", startIndex) - // 向后查询(查询更早的记录) + // 向索引递减方向查询(查询更早的记录) + // QueryNewest(4, 3) 查询索引 2, 3, 4,返回按索引递增排序 backward, err := query.QueryNewest(startIndex-1, 3) if err != nil { log.Fatal(err) } - fmt.Printf("向后查询 3 条记录:\n") + fmt.Printf("向索引递减方向查询 3 条记录(索引 2-4):\n") for i, rec := range backward { fmt.Printf(" [%d] 数据=%s\n", i, string(rec.Data)) } - // 向前查询(查询更新的记录) + // 向索引递增方向查询(查询更新的记录) + // QueryOldest(5, 3) 查询索引 5, 6, 7,返回按索引递增排序 forward, err := query.QueryOldest(startIndex, 3) if err != nil { log.Fatal(err) } - fmt.Printf("向前查询 3 条记录:\n") + fmt.Printf("向索引递增方向查询 3 条记录(索引 5-7):\n") for i, rec := range forward { fmt.Printf(" [%d] 数据=%s\n", i, string(rec.Data)) } diff --git a/query.go b/query.go index f0a2ed3..1c00bd8 100644 --- a/query.go +++ b/query.go @@ -115,10 +115,10 @@ func (rq *RecordQuery) readRecordsForward(startIndex, count int) ([]*Record, err return results, nil } -// QueryOldest 从指定索引开始查询记录(向前读取) +// QueryOldest 从指定索引向索引递增方向查询记录 // startIndex: 查询起始索引 // count: 查询数量 -// 返回的记录按时间顺序(索引递增方向) +// 返回的记录按索引递增方向排序 func (rq *RecordQuery) QueryOldest(startIndex, count int) ([]*Record, error) { if count <= 0 { return nil, NewValidationError("count", "count must be greater than 0", ErrInvalidCount) @@ -146,10 +146,10 @@ func (rq *RecordQuery) QueryOldest(startIndex, count int) ([]*Record, error) { return rq.readRecordsForward(startIndex, count) } -// QueryNewest 从指定索引开始向后查询记录(索引递减方向) -// endIndex: 查询结束索引(包含,最新的记录) +// QueryNewest 从指定索引向索引递减方向查询记录 +// endIndex: 查询的最大索引(向前查询更早的记录) // count: 查询数量 -// 返回结果按时间倒序(最新在前,即 endIndex 对应的记录在最前) +// 返回的记录按索引递增方向排序(与 QueryOldest 一致) func (rq *RecordQuery) QueryNewest(endIndex, count int) ([]*Record, error) { if count <= 0 { return nil, NewValidationError("count", "count must be greater than 0", ErrInvalidCount) @@ -168,25 +168,15 @@ func (rq *RecordQuery) QueryNewest(endIndex, count int) ([]*Record, error) { endIndex = totalCount - 1 } - // 计算实际起始索引(向前推 count-1 条) + // 计算实际起始索引(向索引递减方向查询 count 条) queryStartIdx := endIndex - count + 1 if queryStartIdx < 0 { queryStartIdx = 0 count = endIndex + 1 // 调整实际数量 } - // 向前读取 - results, err := rq.readRecordsForward(queryStartIdx, count) - if err != nil { - return nil, err - } - - // 反转结果,使最新的在前 - for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 { - results[i], results[j] = results[j], results[i] - } - - return results, nil + // 向前读取,返回按索引递增排序的结果 + return rq.readRecordsForward(queryStartIdx, count) } // GetRecordCount 获取记录总数 diff --git a/seqlog_test.go b/seqlog_test.go index 1eb2779..cb523aa 100644 --- a/seqlog_test.go +++ b/seqlog_test.go @@ -1246,7 +1246,7 @@ func TestRecordQuery(t *testing.T) { t.Errorf("expected status Processing, got %s", status) } - // 测试向后查询(查询更早的记录,返回倒序) + // 测试向后查询(查询更早的记录,返回按索引递增排序) backResults, err := query.QueryNewest(startIdx-1, 3) if err != nil { t.Fatalf("failed to query backward: %v", err) @@ -1254,14 +1254,14 @@ func TestRecordQuery(t *testing.T) { if len(backResults) != 3 { t.Errorf("expected 3 backward results, got %d", len(backResults)) } - // 向后查询返回倒序结果(newest first) - expectedBack := []string{"message 4", "message 3", "message 2"} + // 向后查询返回按索引递增排序的结果 + expectedBack := []string{"message 2", "message 3", "message 4"} for i, rec := range backResults { if string(rec.Data) != expectedBack[i] { t.Errorf("backward[%d]: expected '%s', got '%s'", i, expectedBack[i], string(rec.Data)) } - // 手动判断状态:索引 4, 3, 2 - recStatus := GetRecordStatus(startIdx-1-i, startIdx, endIdx) + // 手动判断状态:索引 2, 3, 4 + recStatus := GetRecordStatus(startIdx-3+i, startIdx, endIdx) if recStatus != StatusProcessed { t.Errorf("backward[%d]: expected status Processed, got %s", i, recStatus) } @@ -1799,9 +1799,9 @@ func TestQueryOldestNewest(t *testing.T) { if len(newest) != 3 { t.Errorf("expected 3 records, got %d", len(newest)) } - // 验证顺序:应该是 9, 8, 7(倒序) + // 验证顺序:应该是 7, 8, 9(按索引递增) for i := 0; i < 3; i++ { - expected := fmt.Sprintf("message %d", 9-i) + expected := fmt.Sprintf("message %d", 7+i) if string(newest[i].Record.Data) != expected { t.Errorf("newest[%d]: expected %s, got %s", i, expected, string(newest[i].Record.Data)) } diff --git a/topic_processor.go b/topic_processor.go index be3f83b..dc0f35b 100644 --- a/topic_processor.go +++ b/topic_processor.go @@ -339,10 +339,10 @@ func (tp *TopicProcessor) Query() *RecordQuery { return tp.query } -// QueryOldest 从指定索引开始查询记录(向前读取) +// QueryOldest 从指定索引向索引递增方向查询记录 // startIndex: 查询起始索引 // count: 查询数量 -// 返回的记录包含状态信息(基于 tailer 的窗口索引),按时间顺序(索引递增方向) +// 返回的记录包含状态信息(基于 tailer 的窗口索引),按索引递增方向排序 func (tp *TopicProcessor) QueryOldest(startIndex, count int) ([]*RecordWithStatus, error) { // 查询记录 records, err := tp.query.QueryOldest(startIndex, count) @@ -371,10 +371,10 @@ func (tp *TopicProcessor) QueryOldest(startIndex, count int) ([]*RecordWithStatu return results, nil } -// QueryNewest 从指定索引开始向后查询记录(索引递减方向) -// endIndex: 查询结束索引(最新的记录) +// QueryNewest 从指定索引向索引递减方向查询记录 +// endIndex: 查询的最大索引(向前查询更早的记录) // count: 查询数量 -// 返回的记录包含状态信息(基于 tailer 的窗口索引),按时间倒序(最新在前) +// 返回的记录包含状态信息(基于 tailer 的窗口索引),按索引递增方向排序 func (tp *TopicProcessor) QueryNewest(endIndex, count int) ([]*RecordWithStatus, error) { // 查询记录 records, err := tp.query.QueryNewest(endIndex, count)