mirror of https://github.com/etcd-io/bbolt.git
moar tweaks
parent
9c1b768185
commit
62592ec840
33
bucket.go
33
bucket.go
|
@ -331,7 +331,7 @@ func (b *Bucket) ForEach(fn func(k, v []byte) error) error {
|
|||
// Stat returns stats on a bucket.
|
||||
func (b *Bucket) Stat() *BucketStat {
|
||||
s := &BucketStat{}
|
||||
pageSize := b.Tx().DB().Info().PageSize
|
||||
pageSize := b.tx.db.pageSize
|
||||
b.tx.forEachPage(b.root, 0, func(p *page, depth int) {
|
||||
if (p.flags & leafPageFlag) != 0 {
|
||||
s.LeafPageCount++
|
||||
|
@ -339,23 +339,23 @@ func (b *Bucket) Stat() *BucketStat {
|
|||
lastElement := p.leafPageElement(p.count - 1)
|
||||
used := pageHeaderSize + (leafPageElementSize * int(p.count-1))
|
||||
used += int(lastElement.pos + lastElement.ksize + lastElement.vsize)
|
||||
s.UsedLeafSpace += used
|
||||
s.FreeLeafSpace += int(p.overflow+1)*pageSize - used
|
||||
s.LeafInuse += used
|
||||
s.LeafOverflowPageCount += int(p.overflow)
|
||||
} else if (p.flags & branchPageFlag) != 0 {
|
||||
s.BranchPageCount++
|
||||
lastElement := p.branchPageElement(p.count - 1)
|
||||
used := pageHeaderSize + (branchPageElementSize * int(p.count-1))
|
||||
used += int(lastElement.pos + lastElement.ksize)
|
||||
s.UsedBranchSpace += used
|
||||
s.FreeBranchSpace += int(p.overflow+1)*pageSize - used
|
||||
s.BranchInuse += used
|
||||
s.BranchOverflowPageCount += int(p.overflow)
|
||||
}
|
||||
|
||||
s.OverflowPageCount += int(p.overflow)
|
||||
|
||||
if depth+1 > s.MaxDepth {
|
||||
s.MaxDepth = (depth + 1)
|
||||
}
|
||||
})
|
||||
s.BranchAlloc = (s.BranchPageCount + s.BranchOverflowPageCount) * pageSize
|
||||
s.LeafAlloc = (s.LeafPageCount + s.LeafOverflowPageCount) * pageSize
|
||||
return s
|
||||
}
|
||||
|
||||
|
@ -517,13 +517,14 @@ func (b *Bucket) pageNode(id pgid) (*page, *node) {
|
|||
|
||||
// BucketStat represents stats on a bucket such as branch pages and leaf pages.
|
||||
type BucketStat struct {
|
||||
BranchPageCount int
|
||||
LeafPageCount int
|
||||
OverflowPageCount int
|
||||
KeyCount int
|
||||
MaxDepth int
|
||||
UsedBranchSpace int
|
||||
FreeBranchSpace int
|
||||
UsedLeafSpace int
|
||||
FreeLeafSpace int
|
||||
BranchPageCount int
|
||||
BranchOverflowPageCount int
|
||||
LeafPageCount int
|
||||
LeafOverflowPageCount int
|
||||
KeyCount int
|
||||
MaxDepth int
|
||||
BranchAlloc int
|
||||
BranchInuse int
|
||||
LeafAlloc int
|
||||
LeafInuse int
|
||||
}
|
||||
|
|
|
@ -485,11 +485,6 @@ func TestBucket_Stat(t *testing.T) {
|
|||
}
|
||||
b.Put([]byte("really-big-value"), []byte(strings.Repeat("*", 10000)))
|
||||
|
||||
// Add a bucket that fits on a single root leaf.
|
||||
b, err = tx.CreateBucket([]byte("whozawhats"))
|
||||
assert.NoError(t, err)
|
||||
b.Put([]byte("foo"), []byte("bar"))
|
||||
|
||||
return nil
|
||||
})
|
||||
mustCheck(db)
|
||||
|
@ -497,26 +492,46 @@ func TestBucket_Stat(t *testing.T) {
|
|||
b := tx.Bucket([]byte("woojits"))
|
||||
stat := b.Stat()
|
||||
assert.Equal(t, stat.BranchPageCount, 1)
|
||||
assert.Equal(t, stat.BranchOverflowPageCount, 0)
|
||||
assert.Equal(t, stat.LeafPageCount, 6)
|
||||
assert.Equal(t, stat.OverflowPageCount, 2)
|
||||
assert.Equal(t, stat.LeafOverflowPageCount, 2)
|
||||
assert.Equal(t, stat.KeyCount, 501)
|
||||
assert.Equal(t, stat.MaxDepth, 2)
|
||||
assert.Equal(t, stat.UsedBranchSpace, 125)
|
||||
assert.Equal(t, stat.FreeBranchSpace, 3971)
|
||||
assert.Equal(t, stat.UsedLeafSpace, 20908)
|
||||
assert.Equal(t, stat.FreeLeafSpace, 11860)
|
||||
assert.Equal(t, stat.BranchInuse, 125)
|
||||
assert.Equal(t, stat.BranchAlloc, 4096)
|
||||
assert.Equal(t, stat.LeafInuse, 20908)
|
||||
assert.Equal(t, stat.LeafAlloc, 32768)
|
||||
|
||||
b = tx.Bucket([]byte("whozawhats"))
|
||||
stat = b.Stat()
|
||||
return nil
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Ensure a bucket can calculate stats.
|
||||
func TestBucket_Stat_Small(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.
|
||||
b, err := tx.CreateBucket([]byte("whozawhats"))
|
||||
assert.NoError(t, err)
|
||||
b.Put([]byte("foo"), []byte("bar"))
|
||||
|
||||
return nil
|
||||
})
|
||||
mustCheck(db)
|
||||
db.View(func(tx *Tx) error {
|
||||
b := tx.Bucket([]byte("whozawhats"))
|
||||
stat := b.Stat()
|
||||
assert.Equal(t, stat.BranchPageCount, 0)
|
||||
assert.Equal(t, stat.BranchOverflowPageCount, 0)
|
||||
assert.Equal(t, stat.LeafPageCount, 1)
|
||||
assert.Equal(t, stat.OverflowPageCount, 0)
|
||||
assert.Equal(t, stat.LeafOverflowPageCount, 0)
|
||||
assert.Equal(t, stat.KeyCount, 1)
|
||||
assert.Equal(t, stat.MaxDepth, 1)
|
||||
assert.Equal(t, stat.UsedBranchSpace, 0)
|
||||
assert.Equal(t, stat.FreeBranchSpace, 0)
|
||||
assert.Equal(t, stat.UsedLeafSpace, 38)
|
||||
assert.Equal(t, stat.FreeLeafSpace, 4058)
|
||||
assert.Equal(t, stat.BranchInuse, 0)
|
||||
assert.Equal(t, stat.BranchAlloc, 0)
|
||||
assert.Equal(t, stat.LeafInuse, 38)
|
||||
assert.Equal(t, stat.LeafAlloc, 4096)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
@ -544,10 +559,15 @@ func TestBucket_Stat_Large(t *testing.T) {
|
|||
b := tx.Bucket([]byte("widgets"))
|
||||
stat := b.Stat()
|
||||
assert.Equal(t, stat.BranchPageCount, 15)
|
||||
assert.Equal(t, stat.BranchOverflowPageCount, 0)
|
||||
assert.Equal(t, stat.LeafPageCount, 1281)
|
||||
assert.Equal(t, stat.OverflowPageCount, 0)
|
||||
assert.Equal(t, stat.LeafOverflowPageCount, 0)
|
||||
assert.Equal(t, stat.KeyCount, 100000)
|
||||
assert.Equal(t, stat.MaxDepth, 3)
|
||||
assert.Equal(t, stat.BranchInuse, 27289)
|
||||
assert.Equal(t, stat.BranchAlloc, 61440)
|
||||
assert.Equal(t, stat.LeafInuse, 2598276)
|
||||
assert.Equal(t, stat.LeafAlloc, 5246976)
|
||||
return nil
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue