Skip to content

Instantly share code, notes, and snippets.

@GregIngelmo
Created February 16, 2015 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GregIngelmo/f2c4a6ded05fb6aeb98d to your computer and use it in GitHub Desktop.
Save GregIngelmo/f2c4a6ded05fb6aeb98d to your computer and use it in GitHub Desktop.
bolt issue 303
package main
import (
"fmt"
"log"
"os"
"github.com/boltdb/bolt"
)
var path = "file.db"
func main() {
/*
* If the initial size < 1 GiB everything is cool
*/
os.Remove(path)
seedTestDB(1000)
fmt.Printf("Size: %d MiB\n", sizeInMiB()) // 1024 MiB (Initial)
openAndClose()
fmt.Printf("Size: %d MiB\n", sizeInMiB()) // 1024 MiB
/*
* If the initial size > 1 GiB the size steadily grows with each open/close
*/
os.Remove(path)
seedTestDB(1200)
fmt.Printf("Size: %d MiB\n", sizeInMiB()) // 2048 MiB (initial)
openAndClose()
fmt.Printf("Size: %d MiB\n", sizeInMiB()) // 3072 MiB
openAndClose()
fmt.Printf("Size: %d MiB\n", sizeInMiB()) // 4096 MiB
openAndClose()
fmt.Printf("Size: %d MiB\n", sizeInMiB()) // 5120 MiB
}
func openAndClose() {
db, _ := bolt.Open(path, 0644, nil)
db.Close()
}
func seedTestDB(numKeys int) {
db, err := bolt.Open(path, 0600, nil)
defer db.Close()
if err != nil {
log.Fatal(err)
}
err = db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte("testbucket"))
if err != nil {
return err
}
for x := 0; x < numKeys; x++ {
key := []byte(fmt.Sprintf("%d", x))
// ~1 MiB
val := make([]byte, 1048576)
err = bucket.Put(key, val)
if err != nil {
return fmt.Errorf("Error writing key: %s", err)
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
func sizeInMiB() int64 {
file, err := os.Open(path)
if err != nil {
log.Panic(err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
log.Panic(err)
}
kb := stat.Size() / 1024 / 1024
return kb
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment