mirror of https://github.com/etcd-io/bbolt.git
merge inline branch into leaf branch
parent
deffc06a05
commit
0dbfa3f08a
37
bucket.go
37
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 {
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue