frostfs-node/pkg/local_object_storage/writecache/writecachebadger/iterate.go
Alejandro Lopez 4d88bdd4e5
All checks were successful
Build / Build Components (1.19) (pull_request) Successful in 2m15s
Tests and linters / Tests (1.19) (pull_request) Successful in 2m22s
Tests and linters / Tests (1.20) (pull_request) Successful in 2m50s
Build / Build Components (1.20) (pull_request) Successful in 12m23s
Tests and linters / Tests with -race (pull_request) Successful in 5m47s
Vulncheck / Vulncheck (pull_request) Successful in 1m6s
Tests and linters / Lint (pull_request) Successful in 14m38s
Tests and linters / Staticcheck (pull_request) Successful in 6m14s
[#421] Try using badger for the write-cache
Signed-off-by: Alejandro Lopez <a.lopez@yadro.com>
2023-08-02 18:25:53 +03:00

32 lines
1,017 B
Go

package writecachebadger
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
"github.com/dgraph-io/badger/v4"
)
// IterateDB iterates over all objects stored in badger.DB instance and passes them to f until error return.
// It is assumed that db is an underlying database of some WriteCache instance.
//
// DB must not be nil and should be opened.
func IterateDB(db *badger.DB, f func(oid.Address) error) error {
return metaerr.Wrap(db.View(func(tx *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := tx.NewIterator(opts)
for it.Rewind(); it.Valid(); it.Next() {
var key internalKey
if got, want := len(it.Item().Key()), len(key); got != want {
return fmt.Errorf("invalid db key len: got %d, want %d", got, want)
}
copy(key[:], it.Item().Key())
if err := f(key.address()); err != nil {
return err
}
}
return nil
}))
}