mod+kvdb+channeldb: use btcwallet new DB interface

Depends on btcsuite/btcwallet#757.
Pulls in the updated version of btcwallet and walletdb that have the DB
interface enhanced by their own View() and Update() methods with the
reset callback/closure supported out of the box. That way the global
package-level View() and Update() functions now become pure redirects.
This commit is contained in:
Oliver Gugger
2021-08-03 09:57:37 +02:00
parent 1e84b52fee
commit 482f76a0f4
13 changed files with 34 additions and 114 deletions

View File

@@ -13,12 +13,7 @@ import (
// database backend used), the reset function will be called before each retry
// respectively.
func Update(db Backend, f func(tx RwTx) error, reset func()) error {
if extendedDB, ok := db.(ExtendedBackend); ok {
return extendedDB.Update(f, reset)
}
reset()
return walletdb.Update(db, f)
return db.Update(f, reset)
}
// View opens a database read transaction and executes the function f with the
@@ -29,15 +24,7 @@ func Update(db Backend, f func(tx RwTx) error, reset func()) error {
// expect retries of the f closure (depending on the database backend used), the
// reset function will be called before each retry respectively.
func View(db Backend, f func(tx RTx) error, reset func()) error {
if extendedDB, ok := db.(ExtendedBackend); ok {
return extendedDB.View(f, reset)
}
// Since we know that walletdb simply calls into bbolt which never
// retries transactions, we'll call the reset function here before View.
reset()
return walletdb.View(db, f)
return db.View(f, reset)
}
// Batch is identical to the Update call, but it attempts to combine several
@@ -46,10 +33,12 @@ func View(db Backend, f func(tx RTx) error, reset func()) error {
// Batch. For etcd Batch simply does an Update since combination is more complex
// in that case due to STM retries.
func Batch(db Backend, f func(tx RwTx) error) error {
if extendedDB, ok := db.(ExtendedBackend); ok {
// Fall back to the normal Update method if the backend doesn't support
// batching.
if _, ok := db.(walletdb.BatchDB); !ok {
// Since Batch calls handle external state reset, we can safely
// pass in an empty reset closure.
return extendedDB.Update(f, func() {})
return db.Update(f, func() {})
}
return walletdb.Batch(db, f)
@@ -66,35 +55,6 @@ var Create = walletdb.Create
// through read or read+write transactions.
type Backend = walletdb.DB
// ExtendedBackend is and interface that supports View and Update and also able
// to collect database access patterns.
type ExtendedBackend interface {
Backend
// PrintStats returns all collected stats pretty printed into a string.
PrintStats() string
// View opens a database read transaction and executes the function f
// with the transaction passed as a parameter. After f exits, the
// transaction is rolled back. If f errors, its error is returned, not a
// rollback error (if any occur). The passed reset function is called
// before the start of the transaction and can be used to reset
// intermediate state. As callers may expect retries of the f closure
// (depending on the database backend used), the reset function will be
//called before each retry respectively.
View(f func(tx walletdb.ReadTx) error, reset func()) error
// Update opens a database read/write transaction and executes the
// function f with the transaction passed as a parameter. After f exits,
// if f did not error, the transaction is committed. Otherwise, if f did
// error, the transaction is rolled back. If the rollback fails, the
// original error returned by f is still returned. If the commit fails,
// the commit error is returned. As callers may expect retries of the f
// closure (depending on the database backend used), the reset function
// will be called before each retry respectively.
Update(f func(tx walletdb.ReadWriteTx) error, reset func()) error
}
// Open opens an existing database for the specified type. The arguments are
// specific to the database type driver. See the documentation for the database
// driver for further details.
@@ -129,9 +89,8 @@ type RwBucket = walletdb.ReadWriteBucket
// operations.
type RwCursor = walletdb.ReadWriteCursor
// ReadWriteTx represents a database transaction that can be used for both
// reads and writes. When only reads are necessary, consider using a RTx
// instead.
// RwTx represents a database transaction that can be used for both reads and
// writes. When only reads are necessary, consider using a RTx instead.
type RwTx = walletdb.ReadWriteTx
var (