sasha-s 2015-05-18 12:00:40 -07:00
parent fda75748b5
commit aa13f7f94f
3 changed files with 7 additions and 12 deletions

View File

@ -48,8 +48,8 @@ func funlock(f *os.File) error {
func mmap(db *DB, sz int) error {
// Truncate and fsync to ensure file size metadata is flushed.
// https://github.com/boltdb/bolt/issues/284
if !db.NoGrowSync {
if err := db.ops.Truncate(int64(sz)); err != nil {
if !db.NoGrowSync && !db.readOnly {
if err := db.file.Truncate(int64(sz)); err != nil {
return fmt.Errorf("file resize error: %s", err)
}
if err := db.file.Sync(); err != nil {

View File

@ -28,9 +28,11 @@ func funlock(f *os.File) error {
// mmap memory maps a DB's data file.
// Based on: https://github.com/edsrzf/mmap-go
func mmap(db *DB, sz int) error {
// Truncate the database to the size of the mmap.
if err := db.ops.Truncate(int64(sz)); err != nil {
return fmt.Errorf("truncate: %s", err)
if !db.readOnly {
// Truncate the database to the size of the mmap.
if err := db.file.Truncate(int64(sz)); err != nil {
return fmt.Errorf("truncate: %s", err)
}
}
// Open a file mapping handle.

7
db.go
View File

@ -103,7 +103,6 @@ type DB struct {
ops struct {
writeAt func(b []byte, off int64) (n int, err error)
Truncate func(size int64) error
}
readOnly bool // Read only mode. Update()/Begin(true) would return ErrDatabaseReadOnly immediately.
@ -144,8 +143,6 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
if options.ReadOnly {
flag = os.O_RDONLY
db.readOnly = true
// Ignore truncations.
db.ops.Truncate = func(int64) error { return nil }
}
// Open data file and separate sync handler for metadata writes.
@ -156,10 +153,6 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
return nil, err
}
if !db.readOnly {
db.ops.Truncate = db.file.Truncate
}
// Lock file so that other processes using Bolt in read-write mode cannot
// use the database at the same time. This would cause corruption since
// the two processes would write meta pages and free pages separately.