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:
parent
b688a6d891
commit
3378be750b
2 changed files with 24 additions and 0 deletions
|
@ -99,6 +99,14 @@ func (tx *transaction) Rollback() error {
|
||||||
return convertErr(tx.boltTx.Rollback())
|
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
|
// bucket is an internal type used to represent a collection of key/value pairs
|
||||||
// and implements the walletdb Bucket interfaces.
|
// and implements the walletdb Bucket interfaces.
|
||||||
type bucket bbolt.Bucket
|
type bucket bbolt.Bucket
|
||||||
|
@ -214,6 +222,15 @@ func (b *bucket) ReadWriteCursor() walletdb.ReadWriteCursor {
|
||||||
return (*cursor)((*bbolt.Bucket)(b).Cursor())
|
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
|
// cursor represents a cursor over key/value pairs and nested buckets of a
|
||||||
// bucket.
|
// bucket.
|
||||||
//
|
//
|
||||||
|
|
|
@ -42,6 +42,10 @@ type ReadWriteTx interface {
|
||||||
// Commit commits all changes that have been on the transaction's root
|
// Commit commits all changes that have been on the transaction's root
|
||||||
// buckets and all of their sub-buckets to persistent storage.
|
// buckets and all of their sub-buckets to persistent storage.
|
||||||
Commit() error
|
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)
|
// 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
|
// Cursor returns a new cursor, allowing for iteration over the bucket's
|
||||||
// key/value pairs and nested buckets in forward or backward order.
|
// key/value pairs and nested buckets in forward or backward order.
|
||||||
ReadWriteCursor() ReadWriteCursor
|
ReadWriteCursor() ReadWriteCursor
|
||||||
|
|
||||||
|
// Tx returns the bucket's transaction.
|
||||||
|
Tx() ReadWriteTx
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadCursor represents a bucket cursor that can be positioned at the start or
|
// ReadCursor represents a bucket cursor that can be positioned at the start or
|
||||||
|
|
Loading…
Reference in a new issue