mmapSize determines the appropriate size for the mmap given the current size of the database. The minimum size is 32KB and doubles until it reaches 1GB. Returns an error if the new mmap size is greater than the max allowed.
(size int)
| 579 | // of the database. The minimum size is 32KB and doubles until it reaches 1GB. |
| 580 | // Returns an error if the new mmap size is greater than the max allowed. |
| 581 | func (db *DB) mmapSize(size int) (int, error) { |
| 582 | // Double the size from 32KB until 1GB. |
| 583 | for i := uint(15); i <= 30; i++ { |
| 584 | if size <= 1<<i { |
| 585 | return 1 << i, nil |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | // Verify the requested size is not above the maximum allowed. |
| 590 | if size > common.MaxMapSize { |
| 591 | return 0, errors.New("mmap too large") |
| 592 | } |
| 593 | |
| 594 | // If larger than 1GB then grow by 1GB at a time. |
| 595 | sz := int64(size) |
| 596 | if remainder := sz % int64(common.MaxMmapStep); remainder > 0 { |
| 597 | sz += int64(common.MaxMmapStep) - remainder |
| 598 | } |
| 599 | |
| 600 | // Ensure that the mmap size is a multiple of the page size. |
| 601 | // This should always be true since we're incrementing in MBs. |
| 602 | pageSize := int64(db.pageSize) |
| 603 | if (sz % pageSize) != 0 { |
| 604 | sz = ((sz / pageSize) + 1) * pageSize |
| 605 | } |
| 606 | |
| 607 | // If we've exceeded the max size then only grow up to the max size. |
| 608 | if sz > common.MaxMapSize { |
| 609 | sz = common.MaxMapSize |
| 610 | } |
| 611 | |
| 612 | return int(sz), nil |
| 613 | } |
| 614 | |
| 615 | func (db *DB) munlock(fileSize int) error { |
| 616 | // gofail: var munlockError string |