grow grows the size of the database to the given sz.
(sz int)
| 1221 | |
| 1222 | // grow grows the size of the database to the given sz. |
| 1223 | func (db *DB) grow(sz int) error { |
| 1224 | // Ignore if the new size is less than available file size. |
| 1225 | lg := db.Logger() |
| 1226 | fileSize, err := db.fileSize() |
| 1227 | if err != nil { |
| 1228 | lg.Errorf("getting file size failed: %w", err) |
| 1229 | return err |
| 1230 | } |
| 1231 | if sz <= fileSize { |
| 1232 | return nil |
| 1233 | } |
| 1234 | |
| 1235 | sz = db.growSize(db.datasz, sz) |
| 1236 | |
| 1237 | // Truncate and fsync to ensure file size metadata is flushed. |
| 1238 | // https://github.com/boltdb/bolt/issues/284 |
| 1239 | if !db.NoGrowSync && !db.readOnly { |
| 1240 | if runtime.GOOS != "windows" { |
| 1241 | // gofail: var resizeFileError string |
| 1242 | // return errors.New(resizeFileError) |
| 1243 | if err := db.file.Truncate(int64(sz)); err != nil { |
| 1244 | lg.Errorf("[GOOS: %s, GOARCH: %s] truncating file failed, size: %d, db.datasz: %d, error: %v", runtime.GOOS, runtime.GOARCH, sz, db.datasz, err) |
| 1245 | return fmt.Errorf("file resize error: %s", err) |
| 1246 | } |
| 1247 | } |
| 1248 | if err := db.file.Sync(); err != nil { |
| 1249 | lg.Errorf("[GOOS: %s, GOARCH: %s] syncing file failed, db.datasz: %d, error: %v", runtime.GOOS, runtime.GOARCH, db.datasz, err) |
| 1250 | return fmt.Errorf("file sync error: %s", err) |
| 1251 | } |
| 1252 | if db.Mlock { |
| 1253 | // unlock old file and lock new one |
| 1254 | if err := db.mrelock(fileSize, sz); err != nil { |
| 1255 | return fmt.Errorf("mlock/munlock error: %s", err) |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | return nil |
| 1261 | } |
| 1262 | |
| 1263 | func (db *DB) growSize(mmapSize, growSize int) int { |
| 1264 | // If the data is smaller than the alloc size then only allocate what's needed. |