forked from TrueCloudLab/frostfs-node
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
// Package badger provides a kvio.Repository backed by badger.
|
|
package badger
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/kvio"
|
|
badger "github.com/dgraph-io/badger/v4"
|
|
)
|
|
|
|
type impl struct {
|
|
db *badger.DB
|
|
}
|
|
|
|
func Open(opts badger.Options) (kvio.Repository, error) {
|
|
db, err := badger.Open(opts)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not open database: %w", err)
|
|
}
|
|
|
|
return impl{db}, nil
|
|
}
|
|
|
|
func (r impl) Read(f func(kvio.ReadOnlyTx) error) error {
|
|
return r.db.View(func(tx *badger.Txn) error {
|
|
return f(txImpl{tx})
|
|
})
|
|
}
|
|
|
|
func (r impl) Write(f func(kvio.WriteOnlyTx) error) error {
|
|
wb := r.db.NewWriteBatch()
|
|
defer wb.Cancel()
|
|
if err := f(writeBatchTx{wb}); err != nil {
|
|
return err
|
|
}
|
|
return wb.Flush()
|
|
}
|
|
|
|
func (r impl) ReadWrite(f func(kvio.ReadWriteTx) error) error {
|
|
return r.db.Update(func(tx *badger.Txn) error {
|
|
return f(txImpl{tx})
|
|
})
|
|
}
|
|
|
|
func (r impl) Stats() (kvio.Stats, error) {
|
|
var stats kvio.Stats
|
|
err := r.Read(func(tx kvio.ReadOnlyTx) error {
|
|
cur := tx.IterateKeys()
|
|
defer cur.Close()
|
|
for ; cur.Key() != nil; cur.Next() {
|
|
stats.KeyCount++
|
|
}
|
|
return nil
|
|
})
|
|
return stats, err
|
|
}
|
|
|
|
func (r impl) Close() error { return r.db.Close() }
|