2021-09-14 15:09:36 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
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 {
|
2021-09-14 15:09:36 +00:00
|
|
|
return db.View(func(tx *bbolt.Tx) error {
|
|
|
|
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
|
|
|
|
|
|
|
return b.ForEach(func(k, v []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 {
|
|
|
|
return fmt.Errorf("could not parse object address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f(addr)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|