Merge pull request #745 from bhandras/walletdb_foreach

walletdb: add ForEachBucket to the ReadTx with bbolt implementation
This commit is contained in:
Olaoluwa Osuntokun 2021-05-12 21:38:50 -07:00 committed by GitHub
commit 3a2f12e3a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View file

@ -60,6 +60,15 @@ func (tx *transaction) ReadBucket(key []byte) walletdb.ReadBucket {
return tx.ReadWriteBucket(key) return tx.ReadWriteBucket(key)
} }
// ForEachBucket will iterate through all top level buckets.
func (tx *transaction) ForEachBucket(fn func(key []byte) error) error {
return convertErr(tx.boltTx.ForEach(
func(name []byte, _ *bbolt.Bucket) error {
return fn(name)
},
))
}
func (tx *transaction) ReadWriteBucket(key []byte) walletdb.ReadWriteBucket { func (tx *transaction) ReadWriteBucket(key []byte) walletdb.ReadWriteBucket {
boltBucket := tx.boltTx.Bucket(key) boltBucket := tx.boltTx.Bucket(key)
if boltBucket == nil { if boltBucket == nil {

View file

@ -19,6 +19,9 @@ type ReadTx interface {
// described by the key does not exist, nil is returned. // described by the key does not exist, nil is returned.
ReadBucket(key []byte) ReadBucket ReadBucket(key []byte) ReadBucket
// ForEachBucket will iterate through all top level buckets.
ForEachBucket(func(key []byte) error) error
// Rollback closes the transaction, discarding changes (if any) if the // Rollback closes the transaction, discarding changes (if any) if the
// database was modified by a write transaction. // database was modified by a write transaction.
Rollback() error Rollback() error

View file

@ -540,6 +540,31 @@ func testNamespaceAndTxInterfaces(tc *testContext, namespaceKey string) bool {
return false return false
} }
// Test that we can read the top level buckets.
var topLevelBuckets []string
walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
return tx.ForEachBucket(func(key []byte) error {
topLevelBuckets = append(topLevelBuckets, string(key))
return nil
})
})
if err != nil {
if err != errSubTestFail {
tc.t.Errorf("%v", err)
}
return false
}
if len(topLevelBuckets) != 1 {
tc.t.Errorf("ForEachBucket: expected only one top level bucket")
return false
}
if topLevelBuckets[0] != namespaceKey {
tc.t.Errorf("ForEachBucket: expected %v, got %v", namespaceKey,
topLevelBuckets[0])
return false
}
// Test the bucket interface via a managed read-write transaction. // Test the bucket interface via a managed read-write transaction.
// Also, put a series of values and force a rollback so the following // Also, put a series of values and force a rollback so the following
// code can ensure the values were not stored. // code can ensure the values were not stored.