From 9abced434f459c20fd6629efa2be0b91e56de179 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Thu, 27 Feb 2014 11:55:44 -0700 Subject: [PATCH] Add bucket reclamation. After RWTransaction.DeleteBucket() is called, all pages related to the bucket are moved to the freelist for that transaction. --- rwtransaction.go | 8 ++++++-- rwtransaction_test.go | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/rwtransaction.go b/rwtransaction.go index d9097f3..ddd4b96 100644 --- a/rwtransaction.go +++ b/rwtransaction.go @@ -63,14 +63,18 @@ func (t *RWTransaction) CreateBucketIfNotExists(name string) error { // DeleteBucket deletes a bucket. // Returns an error if the bucket cannot be found. func (t *RWTransaction) DeleteBucket(name string) error { - if b := t.Bucket(name); b == nil { + b := t.Bucket(name) + if b == nil { return ErrBucketNotFound } // Remove from buckets page. t.buckets.del(name) - // TODO(benbjohnson): Free all pages. + // Free all pages. + t.forEachPage(b.root, 0, func(p *page, depth int) { + t.db.freelist.free(t.id(), p) + }) return nil } diff --git a/rwtransaction_test.go b/rwtransaction_test.go index 83b2d1f..1635b45 100644 --- a/rwtransaction_test.go +++ b/rwtransaction_test.go @@ -66,6 +66,7 @@ func TestRWTransactionCreateBucketIfNotExists(t *testing.T) { withOpenDB(func(db *DB, path string) { assert.NoError(t, db.CreateBucketIfNotExists("widgets")) assert.NoError(t, db.CreateBucketIfNotExists("widgets")) + assert.Equal(t, db.CreateBucketIfNotExists(""), ErrBucketNameRequired) // Read the bucket through a separate transaction. b, err := db.Bucket("widgets") @@ -113,12 +114,17 @@ func TestRWTransactionDeleteBucket(t *testing.T) { db.CreateBucket("widgets") db.Put("widgets", []byte("foo"), []byte("bar")) + b, _ := db.Bucket("widgets") + // Delete the bucket and make sure we can't get the value. assert.NoError(t, db.DeleteBucket("widgets")) value, err := db.Get("widgets", []byte("foo")) assert.Equal(t, err, ErrBucketNotFound) assert.Nil(t, value) + // Verify that the bucket's page is free. + assert.Equal(t, db.freelist.all(), []pgid{b.root}) + // Create the bucket again and make sure there's not a phantom value. assert.NoError(t, db.CreateBucket("widgets")) value, err = db.Get("widgets", []byte("foo"))