From 0dbfa3f08a0d2657cc7fc9b7b46af1f0aa271d45 Mon Sep 17 00:00:00 2001 From: Martin Kobetic Date: Mon, 12 May 2014 18:22:45 +0000 Subject: [PATCH] merge inline branch into leaf branch --- bucket.go | 37 +++++++++++++++++-------------------- bucket_test.go | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/bucket.go b/bucket.go index a2463a3..fdc9852 100644 --- a/bucket.go +++ b/bucket.go @@ -369,29 +369,26 @@ func (b *Bucket) Stats() BucketStats { s.InlineBucketN += 1 } b.forEachPage(func(p *page, depth int) { - if b.root == 0 { // inline bucket + if (p.flags & leafPageFlag) != 0 { s.KeyN += int(p.count) - lastElement := p.leafPageElement(p.count - 1) - used := bucketHeaderSize + pageHeaderSize + (leafPageElementSize * int(p.count-1)) - used += int(lastElement.pos + lastElement.ksize + lastElement.vsize) - s.InlineBucketInuse += used - } else if (p.flags & leafPageFlag) != 0 { - s.LeafPageN++ - if p.count == 0 { - return + used := pageHeaderSize + (leafPageElementSize * int(p.count)) + if p.count != 0 { + lastElement := p.leafPageElement(p.count - 1) + used += int(lastElement.pos + lastElement.ksize + lastElement.vsize) } - s.KeyN += int(p.count) - lastElement := p.leafPageElement(p.count - 1) - used := pageHeaderSize + (leafPageElementSize * int(p.count-1)) - used += int(lastElement.pos + lastElement.ksize + lastElement.vsize) - s.LeafInuse += used - s.LeafOverflowN += int(p.overflow) + if b.root == 0 { + s.InlineBucketInuse += used + } else { + s.LeafPageN++ + s.LeafInuse += used + s.LeafOverflowN += int(p.overflow) - // Collect stats from sub-buckets - for i := uint16(0); i < p.count; i++ { - e := p.leafPageElement(i) - if (e.flags & bucketLeafFlag) != 0 { - subStats.Add(b.openBucket(e.value()).Stats()) + // Collect stats from sub-buckets + for i := uint16(0); i < p.count; i++ { + e := p.leafPageElement(i) + if (e.flags & bucketLeafFlag) != 0 { + subStats.Add(b.openBucket(e.value()).Stats()) + } } } } else if (p.flags & branchPageFlag) != 0 { diff --git a/bucket_test.go b/bucket_test.go index b53a74b..11d1d77 100644 --- a/bucket_test.go +++ b/bucket_test.go @@ -583,7 +583,7 @@ func TestBucket_Stats(t *testing.T) { assert.Equal(t, 501, stats.KeyN, "KeyN") assert.Equal(t, 2, stats.Depth, "Depth") assert.Equal(t, 125, stats.BranchInuse, "BranchInuse") - assert.Equal(t, 20908, stats.LeafInuse, "LeafInuse") + assert.Equal(t, 21004, stats.LeafInuse, "LeafInuse") if os.Getpagesize() == 4096 { // Incompatible page size assert.Equal(t, 4096, stats.BranchAlloc, "BranchAlloc") @@ -634,6 +634,40 @@ func TestBucket_Stats_Small(t *testing.T) { }) } +func TestBucket_Stats_EmptyBucket(t *testing.T) { + + withOpenDB(func(db *DB, path string) { + db.Update(func(tx *Tx) error { + // Add a bucket that fits on a single root leaf. + _, err := tx.CreateBucket([]byte("whozawhats")) + assert.NoError(t, err) + return nil + }) + mustCheck(db) + db.View(func(tx *Tx) error { + b := tx.Bucket([]byte("whozawhats")) + stats := b.Stats() + assert.Equal(t, 0, stats.BranchPageN, "BranchPageN") + assert.Equal(t, 0, stats.BranchOverflowN, "BranchOverflowN") + assert.Equal(t, 0, stats.LeafPageN, "LeafPageN") + assert.Equal(t, 0, stats.LeafOverflowN, "LeafOverflowN") + assert.Equal(t, 0, stats.KeyN, "KeyN") + assert.Equal(t, 1, stats.Depth, "Depth") + assert.Equal(t, 0, stats.BranchInuse, "BranchInuse") + assert.Equal(t, 0, stats.LeafInuse, "LeafInuse") + if os.Getpagesize() == 4096 { + // Incompatible page size + assert.Equal(t, 0, stats.BranchAlloc, "BranchAlloc") + assert.Equal(t, 0, stats.LeafAlloc, "LeafAlloc") + } + assert.Equal(t, 1, stats.BucketN, "BucketN") + assert.Equal(t, 1, stats.InlineBucketN, "InlineBucketN") + assert.Equal(t, pageHeaderSize, stats.InlineBucketInuse, "InlineBucketInuse") + return nil + }) + }) +} + // Ensure a bucket can calculate stats. func TestBucket_Stats_Nested(t *testing.T) { @@ -667,7 +701,7 @@ func TestBucket_Stats_Nested(t *testing.T) { assert.Equal(t, 122, stats.KeyN, "KeyN") assert.Equal(t, 3, stats.Depth, "Depth") assert.Equal(t, 0, stats.BranchInuse, "BranchInuse") - assert.Equal(t, 2474, stats.LeafInuse, "LeafInuse") + assert.Equal(t, 2506, stats.LeafInuse, "LeafInuse") if os.Getpagesize() == 4096 { // Incompatible page size assert.Equal(t, 0, stats.BranchAlloc, "BranchAlloc") @@ -712,7 +746,7 @@ func TestBucket_Stats_Large(t *testing.T) { assert.Equal(t, 100000, stats.KeyN, "KeyN") assert.Equal(t, 3, stats.Depth, "Depth") assert.Equal(t, 27007, stats.BranchInuse, "BranchInuse") - assert.Equal(t, 2598436, stats.LeafInuse, "LeafInuse") + assert.Equal(t, 2619092, stats.LeafInuse, "LeafInuse") if os.Getpagesize() == 4096 { // Incompatible page size assert.Equal(t, 77824, stats.BranchAlloc, "BranchAlloc")