修复:统一查询方法的返回顺序为索引递增
重要变更: - 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 <noreply@anthropic.com>
This commit is contained in:
@@ -69,22 +69,24 @@ func main() {
|
|||||||
startIndex := 5
|
startIndex := 5
|
||||||
fmt.Printf("从第 %d 条记录开始查询\n", startIndex)
|
fmt.Printf("从第 %d 条记录开始查询\n", startIndex)
|
||||||
|
|
||||||
// 向后查询(查询更早的记录)
|
// 向索引递减方向查询(查询更早的记录)
|
||||||
|
// QueryNewest(4, 3) 查询索引 2, 3, 4,返回按索引递增排序
|
||||||
backward, err := query.QueryNewest(startIndex-1, 3)
|
backward, err := query.QueryNewest(startIndex-1, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("向后查询 3 条记录:\n")
|
fmt.Printf("向索引递减方向查询 3 条记录(索引 2-4):\n")
|
||||||
for i, rec := range backward {
|
for i, rec := range backward {
|
||||||
fmt.Printf(" [%d] 数据=%s\n", i, string(rec.Data))
|
fmt.Printf(" [%d] 数据=%s\n", i, string(rec.Data))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 向前查询(查询更新的记录)
|
// 向索引递增方向查询(查询更新的记录)
|
||||||
|
// QueryOldest(5, 3) 查询索引 5, 6, 7,返回按索引递增排序
|
||||||
forward, err := query.QueryOldest(startIndex, 3)
|
forward, err := query.QueryOldest(startIndex, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("向前查询 3 条记录:\n")
|
fmt.Printf("向索引递增方向查询 3 条记录(索引 5-7):\n")
|
||||||
for i, rec := range forward {
|
for i, rec := range forward {
|
||||||
fmt.Printf(" [%d] 数据=%s\n", i, string(rec.Data))
|
fmt.Printf(" [%d] 数据=%s\n", i, string(rec.Data))
|
||||||
}
|
}
|
||||||
|
|||||||
26
query.go
26
query.go
@@ -115,10 +115,10 @@ func (rq *RecordQuery) readRecordsForward(startIndex, count int) ([]*Record, err
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryOldest 从指定索引开始查询记录(向前读取)
|
// QueryOldest 从指定索引向索引递增方向查询记录
|
||||||
// startIndex: 查询起始索引
|
// startIndex: 查询起始索引
|
||||||
// count: 查询数量
|
// count: 查询数量
|
||||||
// 返回的记录按时间顺序(索引递增方向)
|
// 返回的记录按索引递增方向排序
|
||||||
func (rq *RecordQuery) QueryOldest(startIndex, count int) ([]*Record, error) {
|
func (rq *RecordQuery) QueryOldest(startIndex, count int) ([]*Record, error) {
|
||||||
if count <= 0 {
|
if count <= 0 {
|
||||||
return nil, NewValidationError("count", "count must be greater than 0", ErrInvalidCount)
|
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)
|
return rq.readRecordsForward(startIndex, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryNewest 从指定索引开始向后查询记录(索引递减方向)
|
// QueryNewest 从指定索引向索引递减方向查询记录
|
||||||
// endIndex: 查询结束索引(包含,最新的记录)
|
// endIndex: 查询的最大索引(向前查询更早的记录)
|
||||||
// count: 查询数量
|
// count: 查询数量
|
||||||
// 返回结果按时间倒序(最新在前,即 endIndex 对应的记录在最前)
|
// 返回的记录按索引递增方向排序(与 QueryOldest 一致)
|
||||||
func (rq *RecordQuery) QueryNewest(endIndex, count int) ([]*Record, error) {
|
func (rq *RecordQuery) QueryNewest(endIndex, count int) ([]*Record, error) {
|
||||||
if count <= 0 {
|
if count <= 0 {
|
||||||
return nil, NewValidationError("count", "count must be greater than 0", ErrInvalidCount)
|
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
|
endIndex = totalCount - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算实际起始索引(向前推 count-1 条)
|
// 计算实际起始索引(向索引递减方向查询 count 条)
|
||||||
queryStartIdx := endIndex - count + 1
|
queryStartIdx := endIndex - count + 1
|
||||||
if queryStartIdx < 0 {
|
if queryStartIdx < 0 {
|
||||||
queryStartIdx = 0
|
queryStartIdx = 0
|
||||||
count = endIndex + 1 // 调整实际数量
|
count = endIndex + 1 // 调整实际数量
|
||||||
}
|
}
|
||||||
|
|
||||||
// 向前读取
|
// 向前读取,返回按索引递增排序的结果
|
||||||
results, err := rq.readRecordsForward(queryStartIdx, count)
|
return 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRecordCount 获取记录总数
|
// GetRecordCount 获取记录总数
|
||||||
|
|||||||
@@ -1246,7 +1246,7 @@ func TestRecordQuery(t *testing.T) {
|
|||||||
t.Errorf("expected status Processing, got %s", status)
|
t.Errorf("expected status Processing, got %s", status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试向后查询(查询更早的记录,返回倒序)
|
// 测试向后查询(查询更早的记录,返回按索引递增排序)
|
||||||
backResults, err := query.QueryNewest(startIdx-1, 3)
|
backResults, err := query.QueryNewest(startIdx-1, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to query backward: %v", err)
|
t.Fatalf("failed to query backward: %v", err)
|
||||||
@@ -1254,14 +1254,14 @@ func TestRecordQuery(t *testing.T) {
|
|||||||
if len(backResults) != 3 {
|
if len(backResults) != 3 {
|
||||||
t.Errorf("expected 3 backward results, got %d", len(backResults))
|
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 {
|
for i, rec := range backResults {
|
||||||
if string(rec.Data) != expectedBack[i] {
|
if string(rec.Data) != expectedBack[i] {
|
||||||
t.Errorf("backward[%d]: expected '%s', got '%s'", i, expectedBack[i], string(rec.Data))
|
t.Errorf("backward[%d]: expected '%s', got '%s'", i, expectedBack[i], string(rec.Data))
|
||||||
}
|
}
|
||||||
// 手动判断状态:索引 4, 3, 2
|
// 手动判断状态:索引 2, 3, 4
|
||||||
recStatus := GetRecordStatus(startIdx-1-i, startIdx, endIdx)
|
recStatus := GetRecordStatus(startIdx-3+i, startIdx, endIdx)
|
||||||
if recStatus != StatusProcessed {
|
if recStatus != StatusProcessed {
|
||||||
t.Errorf("backward[%d]: expected status Processed, got %s", i, recStatus)
|
t.Errorf("backward[%d]: expected status Processed, got %s", i, recStatus)
|
||||||
}
|
}
|
||||||
@@ -1799,9 +1799,9 @@ func TestQueryOldestNewest(t *testing.T) {
|
|||||||
if len(newest) != 3 {
|
if len(newest) != 3 {
|
||||||
t.Errorf("expected 3 records, got %d", len(newest))
|
t.Errorf("expected 3 records, got %d", len(newest))
|
||||||
}
|
}
|
||||||
// 验证顺序:应该是 9, 8, 7(倒序)
|
// 验证顺序:应该是 7, 8, 9(按索引递增)
|
||||||
for i := 0; i < 3; i++ {
|
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 {
|
if string(newest[i].Record.Data) != expected {
|
||||||
t.Errorf("newest[%d]: expected %s, got %s", i, expected, string(newest[i].Record.Data))
|
t.Errorf("newest[%d]: expected %s, got %s", i, expected, string(newest[i].Record.Data))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -339,10 +339,10 @@ func (tp *TopicProcessor) Query() *RecordQuery {
|
|||||||
return tp.query
|
return tp.query
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryOldest 从指定索引开始查询记录(向前读取)
|
// QueryOldest 从指定索引向索引递增方向查询记录
|
||||||
// startIndex: 查询起始索引
|
// startIndex: 查询起始索引
|
||||||
// count: 查询数量
|
// count: 查询数量
|
||||||
// 返回的记录包含状态信息(基于 tailer 的窗口索引),按时间顺序(索引递增方向)
|
// 返回的记录包含状态信息(基于 tailer 的窗口索引),按索引递增方向排序
|
||||||
func (tp *TopicProcessor) QueryOldest(startIndex, count int) ([]*RecordWithStatus, error) {
|
func (tp *TopicProcessor) QueryOldest(startIndex, count int) ([]*RecordWithStatus, error) {
|
||||||
// 查询记录
|
// 查询记录
|
||||||
records, err := tp.query.QueryOldest(startIndex, count)
|
records, err := tp.query.QueryOldest(startIndex, count)
|
||||||
@@ -371,10 +371,10 @@ func (tp *TopicProcessor) QueryOldest(startIndex, count int) ([]*RecordWithStatu
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryNewest 从指定索引开始向后查询记录(索引递减方向)
|
// QueryNewest 从指定索引向索引递减方向查询记录
|
||||||
// endIndex: 查询结束索引(最新的记录)
|
// endIndex: 查询的最大索引(向前查询更早的记录)
|
||||||
// count: 查询数量
|
// count: 查询数量
|
||||||
// 返回的记录包含状态信息(基于 tailer 的窗口索引),按时间倒序(最新在前)
|
// 返回的记录包含状态信息(基于 tailer 的窗口索引),按索引递增方向排序
|
||||||
func (tp *TopicProcessor) QueryNewest(endIndex, count int) ([]*RecordWithStatus, error) {
|
func (tp *TopicProcessor) QueryNewest(endIndex, count int) ([]*RecordWithStatus, error) {
|
||||||
// 查询记录
|
// 查询记录
|
||||||
records, err := tp.query.QueryNewest(endIndex, count)
|
records, err := tp.query.QueryNewest(endIndex, count)
|
||||||
|
|||||||
Reference in New Issue
Block a user