mirror of
https://github.com/go-gitea/gitea
synced 2024-12-27 20:24:38 +01:00
9a70a12a34
These two functions are similiar, merge them.
39 lines
849 B
Go
39 lines
849 B
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
// Iterate iterate all the Bean object
|
|
func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx context.Context, bean *Bean) error) error {
|
|
var start int
|
|
batchSize := setting.Database.IterateBufferSize
|
|
sess := GetEngine(ctx)
|
|
for {
|
|
beans := make([]*Bean, 0, batchSize)
|
|
if cond != nil {
|
|
sess = sess.Where(cond)
|
|
}
|
|
if err := sess.Limit(batchSize, start).Find(&beans); err != nil {
|
|
return err
|
|
}
|
|
if len(beans) == 0 {
|
|
return nil
|
|
}
|
|
start += len(beans)
|
|
|
|
for _, bean := range beans {
|
|
if err := f(ctx, bean); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|