channeldb: convert to uniformly use new kvdb abstractions

In this commit, we migrate all the code in `channeldb` to only reference
the new `kvdb` package rather than `bbolt` directly.

In many instances, we need to add two version to fetch a bucket as both
read and write when needed. As an example, we add a new
`fetchChanBucketRw` function. This function is identical to
`fetchChanBucket`, but it will be used to fetch the main channel bucket
for all _write_ transactions. We need a new method as you can pass a
write transaction where a read is accepted, but not the other way around
due to the stronger typing of the new `kvdb` package.
This commit is contained in:
Olaoluwa Osuntokun
2019-12-12 18:22:19 -08:00
parent fc808ac538
commit f0911765af
36 changed files with 804 additions and 752 deletions

View File

@@ -1,6 +1,8 @@
package channeldb
import "github.com/coreos/bbolt"
import (
"github.com/lightningnetwork/lnd/channeldb/kvdb"
)
var (
// metaBucket stores all the meta information concerning the state of
@@ -20,10 +22,10 @@ type Meta struct {
// FetchMeta fetches the meta data from boltdb and returns filled meta
// structure.
func (d *DB) FetchMeta(tx *bbolt.Tx) (*Meta, error) {
func (d *DB) FetchMeta(tx kvdb.ReadTx) (*Meta, error) {
meta := &Meta{}
err := d.View(func(tx *bbolt.Tx) error {
err := kvdb.View(d, func(tx kvdb.ReadTx) error {
return fetchMeta(meta, tx)
})
if err != nil {
@@ -36,8 +38,8 @@ func (d *DB) FetchMeta(tx *bbolt.Tx) (*Meta, error) {
// fetchMeta is an internal helper function used in order to allow callers to
// re-use a database transaction. See the publicly exported FetchMeta method
// for more information.
func fetchMeta(meta *Meta, tx *bbolt.Tx) error {
metaBucket := tx.Bucket(metaBucket)
func fetchMeta(meta *Meta, tx kvdb.ReadTx) error {
metaBucket := tx.ReadBucket(metaBucket)
if metaBucket == nil {
return ErrMetaNotFound
}
@@ -54,7 +56,7 @@ func fetchMeta(meta *Meta, tx *bbolt.Tx) error {
// PutMeta writes the passed instance of the database met-data struct to disk.
func (d *DB) PutMeta(meta *Meta) error {
return d.Update(func(tx *bbolt.Tx) error {
return kvdb.Update(d, func(tx kvdb.RwTx) error {
return putMeta(meta, tx)
})
}
@@ -62,8 +64,8 @@ func (d *DB) PutMeta(meta *Meta) error {
// putMeta is an internal helper function used in order to allow callers to
// re-use a database transaction. See the publicly exported PutMeta method for
// more information.
func putMeta(meta *Meta, tx *bbolt.Tx) error {
metaBucket, err := tx.CreateBucketIfNotExists(metaBucket)
func putMeta(meta *Meta, tx kvdb.RwTx) error {
metaBucket, err := tx.CreateTopLevelBucket(metaBucket)
if err != nil {
return err
}
@@ -71,7 +73,7 @@ func putMeta(meta *Meta, tx *bbolt.Tx) error {
return putDbVersion(metaBucket, meta)
}
func putDbVersion(metaBucket *bbolt.Bucket, meta *Meta) error {
func putDbVersion(metaBucket kvdb.RwBucket, meta *Meta) error {
scratch := make([]byte, 4)
byteOrder.PutUint32(scratch, meta.DbVersionNumber)
return metaBucket.Put(dbVersionKey, scratch)