refactor: use the built-in max/min to simplify the code (#17059)

### Summary

In Go 1.21, the standard library includes built-in
[max/min](https://pkg.go.dev/builtin@go1.21.0#max) function, which can
greatly simplify the code.

Signed-off-by: futurehua <futurehua@outlook.com>
This commit is contained in:
futurehua
2026-07-24 15:33:02 +08:00
committed by GitHub
parent 890a414a47
commit bd355deaa9
27 changed files with 27 additions and 158 deletions

View File

@@ -959,10 +959,7 @@ func (e *elasticsearchEngine) Search(ctx context.Context, req *types.SearchReque
return nil, fmt.Errorf("index names cannot be empty")
}
offset := req.Offset
if offset < 0 {
offset = 0
}
offset := max(req.Offset, 0)
limit := req.Limit
if limit <= 0 {
limit = 30
@@ -1353,10 +1350,7 @@ func searchAfterPaginate(
// Skip phase: walk past `offset` hits without retaining them.
remainingSkip := offset
for remainingSkip > 0 {
batch := remainingSkip
if batch > common.SearchAfterBatchSize {
batch = common.SearchAfterBatchSize
}
batch := min(remainingSkip, common.SearchAfterBatchSize)
resp, err := fetch(ctx, baseQuery, batch, cursor, firstCall)
firstCall = false
@@ -1390,10 +1384,7 @@ func searchAfterPaginate(
// target) regardless of how many we asked for in this iteration.
for collectedTake < limit {
want := limit - collectedTake
batch := want
if batch > common.SearchAfterBatchSize {
batch = common.SearchAfterBatchSize
}
batch := min(want, common.SearchAfterBatchSize)
resp, err := fetch(ctx, baseQuery, batch, cursor, firstCall)
firstCall = false
@@ -2857,10 +2848,7 @@ func calculatePagination(page, size, topK int) (int, int) {
window := rerankWindow(size, topK)
offset := (page - 1) * window
if offset < 0 {
offset = 0
}
offset := max((page-1)*window, 0)
return offset, window
}

View File

@@ -459,10 +459,7 @@ func paginate(total, size, topK int) (window, capN int, surfaced []int) {
block = append(block, i)
}
begin := globalOffset % window
end := begin + size
if end > len(block) {
end = len(block)
}
end := min(begin+size, len(block))
surfaced = append(surfaced, block[begin:end]...)
}
return window, capN, surfaced

View File

@@ -564,10 +564,7 @@ func (e *elasticsearchEngine) SearchMetadata(ctx context.Context, req *types.Sea
}, nil
}
offset := req.Offset
if offset < 0 {
offset = 0
}
offset := max(req.Offset, 0)
limit := req.Limit
if limit <= 0 {
limit = 30

View File

@@ -703,10 +703,7 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
pageSize = 30
}
offset := req.Offset
if offset < 0 {
offset = 0
}
offset := max(req.Offset, 0)
db, release, err := e.client.checkoutDatabase(ctx, "chunk.go")
if err != nil {

View File

@@ -583,10 +583,7 @@ func (e *infinityEngine) SearchMetadata(ctx context.Context, req *types.SearchMe
if pageSize <= 0 {
pageSize = 30
}
offset := req.Offset
if offset < 0 {
offset = 0
}
offset := max(req.Offset, 0)
// Build filter from req.Filter
var filterStr string