diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2015-01-28 11:29:27 -0500 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2015-01-28 11:29:27 -0500 |
commit | 834b38e3e742f99b682311f61afc223407eca757 (patch) | |
tree | 6988a13aca467dc0d8190553a803c4a578fffe5f /db_test.go | |
parent | Merge pull request #290 from pmcgrath/deadcode (diff) | |
download | dedo-834b38e3e742f99b682311f61afc223407eca757.tar.gz dedo-834b38e3e742f99b682311f61afc223407eca757.tar.xz |
Fix mmap resize calculation.
This commit fixes an issue where the database would grow whenever it was opened. This was caused by
a recent change that performed a truncation when the database grew. Now there are fixed growth sizes
for the database (1MB, 2MB, 4MB, 8MB, etc) up to 1GB and then the database will grow by 1GB when it
resizes.
See also: 6bb25854a183f3d3bfa50096f910d3a3984e9834
Diffstat (limited to 'db_test.go')
-rw-r--r-- | db_test.go | 41 |
1 files changed, 41 insertions, 0 deletions
@@ -85,6 +85,39 @@ func TestOpen_Wait(t *testing.T) { assert(t, time.Since(start) > 100*time.Millisecond, "") } +// Ensure that opening a database does not increase its size. +// https://github.com/boltdb/bolt/issues/291 +func TestOpen_Size(t *testing.T) { + // Open a data file. + db := NewTestDB() + path := db.Path() + defer db.Close() + + // Insert until we get above the minimum 4MB size. + db.Update(func(tx *bolt.Tx) error { + b, _ := tx.CreateBucketIfNotExists([]byte("data")) + for i := 0; i < 10000; i++ { + _ = b.Put([]byte(fmt.Sprintf("%04d", i)), make([]byte, 1000)) + } + return nil + }) + + // Close database and grab the size. + db.DB.Close() + sz := fileSize(path) + + // Reopen database, update, and check size again. + db0, _ := bolt.Open(path, 0666, nil) + db0.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("data")).Put([]byte{0}, []byte{0}) }) + db0.Close() + newSz := fileSize(path) + + // Compare the original size with the new size. + if sz != newSz { + t.Fatalf("unexpected file growth: %d => %d", sz, newSz) + } +} + // Ensure that a re-opened database is consistent. func TestOpen_Check(t *testing.T) { path := tempfile() @@ -648,3 +681,11 @@ func trunc(b []byte, length int) []byte { func truncDuration(d time.Duration) string { return regexp.MustCompile(`^(\d+)(\.\d+)`).ReplaceAllString(d.String(), "$1") } + +func fileSize(path string) int64 { + fi, err := os.Stat(path) + if err != nil { + return 0 + } + return fi.Size() +} |