2022-08-16 09:35:15 +05:30
|
|
|
// 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"
|
2022-10-31 21:21:14 +05:30
|
|
|
|
|
|
|
"xorm.io/builder"
|
2022-08-16 09:35:15 +05:30
|
|
|
)
|
|
|
|
|
2022-10-31 21:21:14 +05:30
|
|
|
// 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 {
|
2022-08-16 09:35:15 +05:30
|
|
|
var start int
|
|
|
|
batchSize := setting.Database.IterateBufferSize
|
|
|
|
sess := GetEngine(ctx)
|
|
|
|
for {
|
2022-10-31 21:21:14 +05:30
|
|
|
beans := make([]*Bean, 0, batchSize)
|
|
|
|
if cond != nil {
|
|
|
|
sess = sess.Where(cond)
|
|
|
|
}
|
|
|
|
if err := sess.Limit(batchSize, start).Find(&beans); err != nil {
|
2022-08-16 09:35:15 +05:30
|
|
|
return err
|
|
|
|
}
|
2022-10-31 21:21:14 +05:30
|
|
|
if len(beans) == 0 {
|
2022-08-16 09:35:15 +05:30
|
|
|
return nil
|
|
|
|
}
|
2022-10-31 21:21:14 +05:30
|
|
|
start += len(beans)
|
2022-08-16 09:35:15 +05:30
|
|
|
|
2022-10-31 21:21:14 +05:30
|
|
|
for _, bean := range beans {
|
|
|
|
if err := f(ctx, bean); err != nil {
|
2022-08-16 09:35:15 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|