aboutsummaryrefslogtreecommitdiff
path: root/db.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2015-01-28 11:29:27 -0500
committerBen Johnson <benbjohnson@yahoo.com>2015-01-28 11:29:27 -0500
commit834b38e3e742f99b682311f61afc223407eca757 (patch)
tree6988a13aca467dc0d8190553a803c4a578fffe5f /db.go
parentMerge pull request #290 from pmcgrath/deadcode (diff)
downloaddedo-834b38e3e742f99b682311f61afc223407eca757.tar.gz
dedo-834b38e3e742f99b682311f61afc223407eca757.tar.xz
Fix mmap resize calculation.
This commit fixes an issue where the database would grow whenever it was opened. This was caused by a recent change that performed a truncation when the database grew. Now there are fixed growth sizes for the database (1MB, 2MB, 4MB, 8MB, etc) up to 1GB and then the database will grow by 1GB when it resizes. See also: 6bb25854a183f3d3bfa50096f910d3a3984e9834
Diffstat (limited to 'db.go')
-rw-r--r--db.go21
1 files changed, 12 insertions, 9 deletions
diff --git a/db.go b/db.go
index f3fe278..535e566 100644
--- a/db.go
+++ b/db.go
@@ -12,9 +12,6 @@ import (
"unsafe"
)
-// The smallest size that the mmap can be.
-const minMmapSize = 1 << 22 // 4MB
-
// The largest step that can be taken when remapping the mmap.
const maxMmapStep = 1 << 30 // 1GB
@@ -222,15 +219,21 @@ func (db *DB) munmap() error {
// mmapSize determines the appropriate size for the mmap given the current size
// of the database. The minimum size is 4MB and doubles until it reaches 1GB.
func (db *DB) mmapSize(size int) int {
- if size <= minMmapSize {
- return minMmapSize
- } else if size < maxMmapStep {
- size *= 2
- } else {
- size += maxMmapStep
+ // Double the size from 1MB until 1GB.
+ for i := uint(20); i < 30; i++ {
+ if size <= 1<<i {
+ return 1 << i
+ }
+ }
+
+ // If larger than 1GB then grow by 1GB at a time.
+ size += maxMmapStep
+ if remainder := size % maxMmapStep; remainder > 0 {
+ size -= remainder
}
// Ensure that the mmap size is a multiple of the page size.
+ // This should always be true since we're incrementing in MBs.
if (size % db.pageSize) != 0 {
size = ((size / db.pageSize) + 1) * db.pageSize
}