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

@@ -6,13 +6,13 @@ import (
"os"
"testing"
"github.com/coreos/bbolt"
"github.com/lightningnetwork/lnd/channeldb/kvdb"
)
// MakeDB creates a new instance of the ChannelDB for testing purposes. A
// callback which cleans up the created temporary directories is also returned
// and intended to be executed after the test completes.
func MakeDB() (*bbolt.DB, func(), error) {
func MakeDB() (kvdb.Backend, func(), error) {
// Create temporary database for mission control.
file, err := ioutil.TempFile("", "*.db")
if err != nil {
@@ -20,7 +20,7 @@ func MakeDB() (*bbolt.DB, func(), error) {
}
dbPath := file.Name()
db, err := bbolt.Open(dbPath, 0600, nil)
db, err := kvdb.Open(kvdb.BoltBackendName, dbPath, true)
if err != nil {
return nil, nil, err
}
@@ -36,7 +36,7 @@ func MakeDB() (*bbolt.DB, func(), error) {
// ApplyMigration is a helper test function that encapsulates the general steps
// which are needed to properly check the result of applying migration function.
func ApplyMigration(t *testing.T,
beforeMigration, afterMigration, migrationFunc func(tx *bbolt.Tx) error,
beforeMigration, afterMigration, migrationFunc func(tx kvdb.RwTx) error,
shouldFail bool) {
cdb, cleanUp, err := MakeDB()
@@ -47,7 +47,7 @@ func ApplyMigration(t *testing.T,
// beforeMigration usually used for populating the database
// with test data.
err = cdb.Update(beforeMigration)
err = kvdb.Update(cdb, beforeMigration)
if err != nil {
t.Fatal(err)
}
@@ -65,14 +65,14 @@ func ApplyMigration(t *testing.T,
// afterMigration usually used for checking the database state and
// throwing the error if something went wrong.
err = cdb.Update(afterMigration)
err = kvdb.Update(cdb, afterMigration)
if err != nil {
t.Fatal(err)
}
}()
// Apply migration.
err = cdb.Update(migrationFunc)
err = kvdb.Update(cdb, migrationFunc)
if err != nil {
t.Fatal(err)
}

View File

@@ -7,7 +7,7 @@ import (
"fmt"
"strings"
"github.com/coreos/bbolt"
"github.com/lightningnetwork/lnd/channeldb/kvdb"
)
// DumpDB dumps go code describing the contents of the database to stdout. This
@@ -21,8 +21,8 @@ import (
// hex("1111"): hex("5783492373"),
// },
// }
func DumpDB(tx *bbolt.Tx, rootKey []byte) error {
bucket := tx.Bucket(rootKey)
func DumpDB(tx kvdb.ReadTx, rootKey []byte) error {
bucket := tx.ReadBucket(rootKey)
if bucket == nil {
return fmt.Errorf("bucket %v not found", string(rootKey))
}
@@ -30,13 +30,13 @@ func DumpDB(tx *bbolt.Tx, rootKey []byte) error {
return dumpBucket(bucket)
}
func dumpBucket(bucket *bbolt.Bucket) error {
func dumpBucket(bucket kvdb.ReadBucket) error {
fmt.Printf("map[string]interface{} {\n")
err := bucket.ForEach(func(k, v []byte) error {
key := toString(k)
fmt.Printf("%v: ", key)
subBucket := bucket.Bucket(k)
subBucket := bucket.NestedReadBucket(k)
if subBucket != nil {
err := dumpBucket(subBucket)
if err != nil {
@@ -58,8 +58,8 @@ func dumpBucket(bucket *bbolt.Bucket) error {
}
// RestoreDB primes the database with the given data set.
func RestoreDB(tx *bbolt.Tx, rootKey []byte, data map[string]interface{}) error {
bucket, err := tx.CreateBucket(rootKey)
func RestoreDB(tx kvdb.RwTx, rootKey []byte, data map[string]interface{}) error {
bucket, err := tx.CreateTopLevelBucket(rootKey)
if err != nil {
return err
}
@@ -67,7 +67,7 @@ func RestoreDB(tx *bbolt.Tx, rootKey []byte, data map[string]interface{}) error
return restoreDB(bucket, data)
}
func restoreDB(bucket *bbolt.Bucket, data map[string]interface{}) error {
func restoreDB(bucket kvdb.RwBucket, data map[string]interface{}) error {
for k, v := range data {
key := []byte(k)
@@ -100,8 +100,8 @@ func restoreDB(bucket *bbolt.Bucket, data map[string]interface{}) error {
}
// VerifyDB verifies the database against the given data set.
func VerifyDB(tx *bbolt.Tx, rootKey []byte, data map[string]interface{}) error {
bucket := tx.Bucket(rootKey)
func VerifyDB(tx kvdb.ReadTx, rootKey []byte, data map[string]interface{}) error {
bucket := tx.ReadBucket(rootKey)
if bucket == nil {
return fmt.Errorf("bucket %v not found", string(rootKey))
}
@@ -109,7 +109,7 @@ func VerifyDB(tx *bbolt.Tx, rootKey []byte, data map[string]interface{}) error {
return verifyDB(bucket, data)
}
func verifyDB(bucket *bbolt.Bucket, data map[string]interface{}) error {
func verifyDB(bucket kvdb.ReadBucket, data map[string]interface{}) error {
for k, v := range data {
key := []byte(k)
@@ -126,7 +126,7 @@ func verifyDB(bucket *bbolt.Bucket, data map[string]interface{}) error {
// Key contains a sub-bucket.
case map[string]interface{}:
subBucket := bucket.Bucket(key)
subBucket := bucket.NestedReadBucket(key)
if subBucket == nil {
return fmt.Errorf("bucket %v not found", k)
}