From 22635d7451281821e01aa786b7e605e8982d0b99 Mon Sep 17 00:00:00 2001 From: "zhesi.huang" Date: Thu, 9 Nov 2017 01:45:17 +0800 Subject: [PATCH] tx: fix the number of pages is not incorrectly counted --- allocate_test.go | 30 ++++++++++++++++++++++++++++++ tx.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 allocate_test.go diff --git a/allocate_test.go b/allocate_test.go new file mode 100644 index 000000000..8566b4df2 --- /dev/null +++ b/allocate_test.go @@ -0,0 +1,30 @@ +package bolt + +import ( + "testing" +) + +func TestTx_allocatePageStats(t *testing.T) { + f := newFreelist() + f.ids = []pgid{2, 3} + + tx := &Tx{ + db: &DB{ + freelist: f, + pageSize: defaultPageSize, + }, + meta: &meta{}, + pages: make(map[pgid]*page), + } + + prePageCnt := tx.Stats().PageCount + allocateCnt := f.free_count() + + if _, err := tx.allocate(allocateCnt); err != nil { + t.Fatal(err) + } + + if tx.Stats().PageCount != prePageCnt+allocateCnt { + t.Errorf("Allocated %d but got %d page in stats", allocateCnt, tx.Stats().PageCount) + } +} diff --git a/tx.go b/tx.go index aefcec09e..5c0290733 100644 --- a/tx.go +++ b/tx.go @@ -483,7 +483,7 @@ func (tx *Tx) allocate(count int) (*page, error) { tx.pages[p.id] = p // Update statistics. - tx.stats.PageCount++ + tx.stats.PageCount += count tx.stats.PageAlloc += count * tx.db.pageSize return p, nil