2023-12-22 09:58:20 +00:00
|
|
|
package writecache
|
2021-09-14 15:09:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-09-14 15:09:36 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNoDefaultBucket is returned by IterateDB when default bucket for objects is missing.
|
|
|
|
var ErrNoDefaultBucket = errors.New("no default bucket")
|
|
|
|
|
|
|
|
// IterateDB iterates over all objects stored in bbolt.DB instance and passes them to f until error return.
|
|
|
|
// It is assumed that db is an underlying database of some WriteCache instance.
|
|
|
|
//
|
|
|
|
// Returns ErrNoDefaultBucket if there is no default bucket in db.
|
|
|
|
//
|
|
|
|
// DB must not be nil and should be opened.
|
2022-05-31 17:00:41 +00:00
|
|
|
func IterateDB(db *bbolt.DB, f func(oid.Address) error) error {
|
2023-06-15 10:19:36 +00:00
|
|
|
return metaerr.Wrap(db.View(func(tx *bbolt.Tx) error {
|
2021-09-14 15:09:36 +00:00
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
if b == nil {
|
|
|
|
return ErrNoDefaultBucket
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
2021-09-14 15:09:36 +00:00
|
|
|
|
2024-03-11 14:11:49 +00:00
|
|
|
return b.ForEach(func(k, _ []byte) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
err := addr.DecodeString(string(k))
|
2021-09-14 15:09:36 +00:00
|
|
|
if err != nil {
|
2024-12-18 06:38:14 +00:00
|
|
|
return fmt.Errorf("parse object address: %w", err)
|
2021-09-14 15:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return f(addr)
|
|
|
|
})
|
2023-06-15 10:19:36 +00:00
|
|
|
}))
|
2021-09-14 15:09:36 +00:00
|
|
|
}
|