db: switch to bbolt implementation

This commit is contained in:
Mohamed Sohail 2024-04-04 16:44:55 +08:00
parent 239d706042
commit 2f8aaf96ad
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
2 changed files with 63 additions and 67 deletions

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"github.com/bits-and-blooms/bitset" "github.com/bits-and-blooms/bitset"
"github.com/dgraph-io/badger/v4" bolt "go.etcd.io/bbolt"
) )
func (d *DB) SetLowerBound(v uint64) error { func (d *DB) SetLowerBound(v uint64) error {
@ -40,7 +40,7 @@ func (d *DB) GetMissingValuesBitSet(lowerBound uint64, upperBound uint64) (*bits
b bitset.BitSet b bitset.BitSet
) )
err := d.db.View(func(txn *badger.Txn) error { err := d.db.View(func(tx *bolt.Tx) error {
var ( var (
lowerRaw = marshalUint64(lowerBound) lowerRaw = marshalUint64(lowerBound)
upperRaw = marshalUint64(upperBound) upperRaw = marshalUint64(upperBound)
@ -50,19 +50,9 @@ func (d *DB) GetMissingValuesBitSet(lowerBound uint64, upperBound uint64) (*bits
b.Set(uint(i)) b.Set(uint(i))
} }
opts := badger.DefaultIteratorOptions c := tx.Bucket([]byte("blocks")).Cursor()
opts.PrefetchValues = false
iter := txn.NewIterator(opts)
defer iter.Close()
for iter.Seek(lowerRaw); iter.Valid(); iter.Next() {
k := iter.Item().Key()
if bytes.Compare(k, upperRaw) > 0 {
return nil
}
for k, _ := c.Seek(lowerRaw); k != nil && bytes.Compare(k, upperRaw) <= 0; k, _ = c.Next() {
b.Clear(uint(unmarshalUint64(k))) b.Clear(uint(unmarshalUint64(k)))
} }
@ -75,51 +65,51 @@ func (d *DB) GetMissingValuesBitSet(lowerBound uint64, upperBound uint64) (*bits
return &b, nil return &b, nil
} }
func (d *DB) Cleanup() error { // func (d *DB) Cleanup() error {
var ( // var (
safeToDeleteKeys [][]byte // safeToDeleteKeys [][]byte
) // )
err := d.db.View(func(txn *badger.Txn) error { // err := d.db.View(func(txn *badger.Txn) error {
lowerBound, err := d.get(lowerBoundKey) // lowerBound, err := d.get(lowerBoundKey)
if err != nil { // if err != nil {
return err // return err
} // }
lowerBound = marshalUint64(unmarshalUint64(lowerBound) - 1) // lowerBound = marshalUint64(unmarshalUint64(lowerBound) - 1)
opts := badger.DefaultIteratorOptions // opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false // opts.PrefetchValues = false
it := txn.NewIterator(opts) // it := txn.NewIterator(opts)
defer it.Close() // defer it.Close()
for it.Rewind(); it.Valid(); it.Next() { // for it.Rewind(); it.Valid(); it.Next() {
k := it.Item().Key() // k := it.Item().Key()
if bytes.Compare(k, lowerBound) > 0 { // if bytes.Compare(k, lowerBound) > 0 {
return nil // return nil
} // }
safeToDeleteKeys = append(safeToDeleteKeys, it.Item().KeyCopy(nil)) // safeToDeleteKeys = append(safeToDeleteKeys, it.Item().KeyCopy(nil))
} // }
return nil // return nil
}) // })
if err != nil { // if err != nil {
return err // return err
} // }
wb := d.db.NewWriteBatch() // wb := d.db.NewWriteBatch()
for _, k := range safeToDeleteKeys { // for _, k := range safeToDeleteKeys {
if err := wb.Delete(k); err != nil { // if err := wb.Delete(k); err != nil {
return nil // return nil
} // }
} // }
if err := wb.Flush(); err != nil { // if err := wb.Flush(); err != nil {
return err // return err
} // }
return nil // return nil
} // }

View File

@ -2,9 +2,10 @@ package db
import ( import (
"encoding/binary" "encoding/binary"
"fmt"
"log/slog" "log/slog"
"github.com/dgraph-io/badger/v4" bolt "go.etcd.io/bbolt"
) )
type ( type (
@ -13,7 +14,7 @@ type (
} }
DB struct { DB struct {
db *badger.DB db *bolt.DB
logg *slog.Logger logg *slog.Logger
} }
) )
@ -30,13 +31,19 @@ var (
) )
func New(o DBOpts) (*DB, error) { func New(o DBOpts) (*DB, error) {
opts := badger.DefaultOptions(dbFolderName) db, err := bolt.Open(dbFolderName, 0600, nil)
opts.Logger = nil
db, err := badger.Open(opts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("blocks"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
return &DB{ return &DB{
db: db, db: db,
logg: o.Logg, logg: o.Logg,
@ -49,13 +56,10 @@ func (d *DB) Close() error {
func (d *DB) get(k string) ([]byte, error) { func (d *DB) get(k string) ([]byte, error) {
var v []byte var v []byte
err := d.db.View(func(txn *badger.Txn) error { err := d.db.View(func(tx *bolt.Tx) error {
item, err := txn.Get([]byte(k)) b := tx.Bucket([]byte("blocks"))
if err != nil { v = b.Get([]byte(k))
return err return nil
}
v, err = item.ValueCopy(nil)
return err
}) })
if err != nil { if err != nil {
@ -66,8 +70,9 @@ func (d *DB) get(k string) ([]byte, error) {
} }
func (d *DB) setUint64(k string, v uint64) error { func (d *DB) setUint64(k string, v uint64) error {
err := d.db.Update(func(txn *badger.Txn) error { err := d.db.Update(func(tx *bolt.Tx) error {
return txn.Set([]byte(k), marshalUint64(v)) b := tx.Bucket([]byte("blocks"))
return b.Put([]byte(k), marshalUint64(v))
}) })
if err != nil { if err != nil {
return err return err
@ -76,8 +81,9 @@ func (d *DB) setUint64(k string, v uint64) error {
} }
func (d *DB) setUint64AsKey(v uint64) error { func (d *DB) setUint64AsKey(v uint64) error {
err := d.db.Update(func(txn *badger.Txn) error { err := d.db.Update(func(tx *bolt.Tx) error {
return txn.Set(marshalUint64(v), nil) b := tx.Bucket([]byte("blocks"))
return b.Put(marshalUint64(v), nil)
}) })
if err != nil { if err != nil {
return err return err