walletdb/interface: add OnCommit and Tx methods

This commit adds the method OnCommit to the ReadWriteTx interface,
making it possible to add closures to be executed once the transaction
is commitited. The method Tx is added to the ReadWriteBucket interface,
for getting the bucket's underlying tx.

The bdb implementation is updated to satisfy the interface change.
This commit is contained in:
Johan T. Halseth 2018-12-10 11:57:23 +01:00
parent b688a6d891
commit 3378be750b
No known key found for this signature in database
GPG key ID: 15BAADA29DA20D26
2 changed files with 24 additions and 0 deletions

View file

@ -99,6 +99,14 @@ func (tx *transaction) Rollback() error {
return convertErr(tx.boltTx.Rollback())
}
// OnCommit takes a function closure that will be executed when the transaction
// successfully gets committed.
//
// This function is part of the walletdb.ReadWriteTx interface implementation.
func (tx *transaction) OnCommit(f func()) {
tx.boltTx.OnCommit(f)
}
// bucket is an internal type used to represent a collection of key/value pairs
// and implements the walletdb Bucket interfaces.
type bucket bbolt.Bucket
@ -214,6 +222,15 @@ func (b *bucket) ReadWriteCursor() walletdb.ReadWriteCursor {
return (*cursor)((*bbolt.Bucket)(b).Cursor())
}
// Tx returns the bucket's transaction.
//
// This function is part of the walletdb.ReadWriteBucket interface implementation.
func (b *bucket) Tx() walletdb.ReadWriteTx {
return &transaction{
(*bbolt.Bucket)(b).Tx(),
}
}
// cursor represents a cursor over key/value pairs and nested buckets of a
// bucket.
//

View file

@ -42,6 +42,10 @@ type ReadWriteTx interface {
// Commit commits all changes that have been on the transaction's root
// buckets and all of their sub-buckets to persistent storage.
Commit() error
// OnCommit takes a function closure that will be executed when the
// transaction successfully gets committed.
OnCommit(func())
}
// ReadBucket represents a bucket (a hierarchical structure within the database)
@ -119,6 +123,9 @@ type ReadWriteBucket interface {
// Cursor returns a new cursor, allowing for iteration over the bucket's
// key/value pairs and nested buckets in forward or backward order.
ReadWriteCursor() ReadWriteCursor
// Tx returns the bucket's transaction.
Tx() ReadWriteTx
}
// ReadCursor represents a bucket cursor that can be positioned at the start or