2022-08-16 06:05:15 +02:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-08-16 06:05:15 +02:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-10-31 16:51:14 +01:00
|
|
|
|
|
|
|
"xorm.io/builder"
|
2022-08-16 06:05:15 +02:00
|
|
|
)
|
|
|
|
|
2024-10-27 23:48:07 +01:00
|
|
|
// Iterate iterates all the Bean object
|
2022-10-31 16:51:14 +01:00
|
|
|
func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx context.Context, bean *Bean) error) error {
|
2022-08-16 06:05:15 +02:00
|
|
|
var start int
|
|
|
|
batchSize := setting.Database.IterateBufferSize
|
|
|
|
sess := GetEngine(ctx)
|
|
|
|
for {
|
2023-10-21 04:05:29 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
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 06:05:15 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-10-21 04:05:29 +02:00
|
|
|
if len(beans) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
start += len(beans)
|
|
|
|
|
|
|
|
for _, bean := range beans {
|
|
|
|
if err := f(ctx, bean); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 06:05:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|