Skip to content

Commit 48ea1b3

Browse files
authored
Merge pull request #65 from zrss/fix-page-cnt
tx: fix the number of pages is incorrectly counted
2 parents b436469 + 22635d7 commit 48ea1b3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

allocate_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package bolt
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestTx_allocatePageStats(t *testing.T) {
8+
f := newFreelist()
9+
f.ids = []pgid{2, 3}
10+
11+
tx := &Tx{
12+
db: &DB{
13+
freelist: f,
14+
pageSize: defaultPageSize,
15+
},
16+
meta: &meta{},
17+
pages: make(map[pgid]*page),
18+
}
19+
20+
prePageCnt := tx.Stats().PageCount
21+
allocateCnt := f.free_count()
22+
23+
if _, err := tx.allocate(allocateCnt); err != nil {
24+
t.Fatal(err)
25+
}
26+
27+
if tx.Stats().PageCount != prePageCnt+allocateCnt {
28+
t.Errorf("Allocated %d but got %d page in stats", allocateCnt, tx.Stats().PageCount)
29+
}
30+
}

tx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func (tx *Tx) allocate(count int) (*page, error) {
483483
tx.pages[p.id] = p
484484

485485
// Update statistics.
486-
tx.stats.PageCount++
486+
tx.stats.PageCount += count
487487
tx.stats.PageAlloc += count * tx.db.pageSize
488488

489489
return p, nil

0 commit comments

Comments
 (0)