Skip to content

Commit

Permalink
Merge pull request #516 from ahrtr/filesz_20230526
Browse files Browse the repository at this point in the history
Remove field `filesz` from DB
  • Loading branch information
ahrtr authored Jun 15, 2023
2 parents e79ff5f + fddd3ac commit 8ebdaf8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
33 changes: 21 additions & 12 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ type DB struct {
dataref []byte // mmap'ed readonly, write throws SEGV
data *[maxMapSize]byte
datasz int
filesz int // current on disk file size
meta0 *common.Meta
meta1 *common.Meta
pageSize int
Expand Down Expand Up @@ -402,21 +401,29 @@ func (db *DB) hasSyncedFreelist() bool {
return db.meta().Freelist() != common.PgidNoFreelist
}

func (db *DB) fileSize() (int, error) {
info, err := db.file.Stat()
if err != nil {
return 0, fmt.Errorf("file stat error: %w", err)
}
sz := int(info.Size())
if sz < db.pageSize*2 {
return 0, fmt.Errorf("file size too small %d", sz)
}
return sz, nil
}

// mmap opens the underlying memory-mapped file and initializes the meta references.
// minsz is the minimum size that the new mmap can be.
func (db *DB) mmap(minsz int) (err error) {
db.mmaplock.Lock()
defer db.mmaplock.Unlock()

info, err := db.file.Stat()
// Ensure the size is at least the minimum size.
fileSize, err := db.fileSize()
if err != nil {
return fmt.Errorf("mmap stat error: %s", err)
} else if int(info.Size()) < db.pageSize*2 {
return fmt.Errorf("file size too small")
return err
}

// Ensure the size is at least the minimum size.
fileSize := int(info.Size())
var size = fileSize
if size < minsz {
size = minsz
Expand Down Expand Up @@ -610,7 +617,6 @@ func (db *DB) init() error {
if err := fdatasync(db); err != nil {
return err
}
db.filesz = len(buf)

return nil
}
Expand Down Expand Up @@ -1120,7 +1126,11 @@ func (db *DB) allocate(txid common.Txid, count int) (*common.Page, error) {
// grow grows the size of the database to the given sz.
func (db *DB) grow(sz int) error {
// Ignore if the new size is less than available file size.
if sz <= db.filesz {
fileSize, err := db.fileSize()
if err != nil {
return err
}
if sz <= fileSize {
return nil
}

Expand All @@ -1147,13 +1157,12 @@ func (db *DB) grow(sz int) error {
}
if db.Mlock {
// unlock old file and lock new one
if err := db.mrelock(db.filesz, sz); err != nil {
if err := db.mrelock(fileSize, sz); err != nil {
return fmt.Errorf("mlock/munlock error: %s", err)
}
}
}

db.filesz = sz
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -443,7 +444,7 @@ func TestOpen_FileTooSmall(t *testing.T) {
}

_, err = bolt.Open(path, 0666, nil)
if err == nil || err.Error() != "file size too small" {
if err == nil || !strings.Contains(err.Error(), "file size too small") {
t.Fatalf("unexpected error: %s", err)
}
}
Expand Down

0 comments on commit 8ebdaf8

Please sign in to comment.