From 3378be750bd49bcad4649c3610069051855b0e8b Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Mon, 10 Dec 2018 11:57:23 +0100 Subject: [PATCH] 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. --- walletdb/bdb/db.go | 17 +++++++++++++++++ walletdb/interface.go | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/walletdb/bdb/db.go b/walletdb/bdb/db.go index 956a190..bf34e7a 100644 --- a/walletdb/bdb/db.go +++ b/walletdb/bdb/db.go @@ -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. // diff --git a/walletdb/interface.go b/walletdb/interface.go index 754e08b..f89fa91 100644 --- a/walletdb/interface.go +++ b/walletdb/interface.go @@ -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