修复:统一查询方法的返回顺序为索引递增
重要变更: - 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:
26
query.go
26
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 获取记录总数
|
||||
|
||||
Reference in New Issue
Block a user