diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2015-05-06 09:24:37 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2015-05-06 09:24:37 -0600 |
commit | fd65d6c95495fc58156e9407ad15e3e43dc961f8 (patch) | |
tree | 1925b67efdf686f9bdda49f47a278d8544a67aa9 | |
parent | Merge pull request #354 from xiang90/cmd (diff) | |
parent | Add DB.NoGrowSync flag. (diff) | |
download | dedo-fd65d6c95495fc58156e9407ad15e3e43dc961f8.tar.gz dedo-fd65d6c95495fc58156e9407ad15e3e43dc961f8.tar.xz |
Merge pull request #363 from benbjohnson/no-truncate
Add DB.NoTruncate flag.
-rw-r--r-- | bolt_unix.go | 12 | ||||
-rw-r--r-- | db.go | 15 |
2 files changed, 21 insertions, 6 deletions
diff --git a/bolt_unix.go b/bolt_unix.go index e222cfd..35dce08 100644 --- a/bolt_unix.go +++ b/bolt_unix.go @@ -44,11 +44,13 @@ 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 err := db.file.Truncate(int64(sz)); err != nil { - return fmt.Errorf("file resize error: %s", err) - } - if err := db.file.Sync(); err != nil { - return fmt.Errorf("file sync error: %s", err) + if !db.NoGrowSync { + if err := db.file.Truncate(int64(sz)); err != nil { + return fmt.Errorf("file resize error: %s", err) + } + if err := db.file.Sync(); err != nil { + return fmt.Errorf("file sync error: %s", err) + } } // Map the data file to memory. @@ -55,6 +55,14 @@ type DB struct { // THIS IS UNSAFE. PLEASE USE WITH CAUTION. NoSync bool + // When true, skips the truncate call when growing the database. + // Setting this to true is only safe on non-ext3/ext4 systems. + // Skipping truncation avoids preallocation of hard drive space and + // bypasses a truncate() and fsync() syscall on remapping. + // + // https://github.com/boltdb/bolt/issues/284 + NoGrowSync bool + // MaxBatchSize is the maximum size of a batch. Default value is // copied from DefaultMaxBatchSize in Open. // @@ -123,6 +131,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) { if options == nil { options = DefaultOptions } + db.NoGrowSync = options.NoGrowSync // Set default values for later DB operations. db.MaxBatchSize = DefaultMaxBatchSize @@ -613,12 +622,16 @@ type Options struct { // When set to zero it will wait indefinitely. This option is only // available on Darwin and Linux. Timeout time.Duration + + // Sets the DB.NoGrowSync flag before memory mapping the file. + NoGrowSync bool } // DefaultOptions represent the options used if nil options are passed into Open(). // No timeout is used which will cause Bolt to wait indefinitely for a lock. var DefaultOptions = &Options{ - Timeout: 0, + Timeout: 0, + NoGrowSync: false, } // Stats represents statistics about the database. |