Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

DB size doubling on OS X #291

Closed
GregIngelmo opened this issue Jan 24, 2015 · 4 comments · Fixed by #292
Closed

DB size doubling on OS X #291

GregIngelmo opened this issue Jan 24, 2015 · 4 comments · Fixed by #292

Comments

@GregIngelmo
Copy link

I'm on OS X Yosemite 10.10.1, using tip. go version go1.4.1 darwin/amd64

When creating a DB larger than 4096 KiB, future calls to bolt.Open/Close will double the file size of the DB. Probably related to #216. Strangely the tests for 216 were dropped after a refactor, ba6badc#diff-65348100fd3ccd8cb8fb4f6bad6bc2d9L362

Many thanks for all your work on Bolt!

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/boltdb/bolt"
)

const path = "testdb.db"

func main() {
    // When Bolt is under 4096 (pagesize), everything is cool
    fmt.Println("Add one key, DB < 4096 bytes")
    seedTestDB(1)
    fmt.Printf("%d KiB\n", dbSize()) // 4096 KiB
    openAndClose()
    fmt.Printf("%d KiB\n", dbSize()) // 4096 KiB

    fmt.Println("")

    // If Bolt is > 4096 bytes, any subsequent open/close will double its size
    seedTestDB(1000)
    fmt.Println("Add 1000 keys, DB > 4096 bytes:")
    fmt.Printf("%d KiB\n", dbSize()) // 8208 KiB
    openAndClose()
    fmt.Printf("%d KiB\n", dbSize()) // 16416 KiB

}

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 {
        pings, err := tx.CreateBucketIfNotExists([]byte("testbucket"))
        if err != nil {
            return err
        }
        for x := 0; x < numKeys; x++ {
            key := []byte(fmt.Sprintf("%d", x))
            val := make([]byte, 4096)

            err = pings.Put(key, val)
            if err != nil {
                return fmt.Errorf("Error writing key: %s", err)
            }
        }

        return nil
    })

    if err != nil {
        log.Fatal(err)
    }
}

func openAndClose() {
    db, err := bolt.Open(path, 0600, nil)
    defer db.Close()
    if err != nil {
        log.Fatal(err)
    }
}

func dbSize() 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
    return kb
}
@benbjohnson
Copy link
Member

Sorry for the delay on fixing this. Thanks for submitting the issue with an example. I have it fixed on this PR: #292. Let me know if that works for you.

@benbjohnson
Copy link
Member

Also, note that you may need to specify the -a flag when rerunning your example. That'll recompile Bolt too. I was getting some stale compiles and I couldn't figure out why initially.

@benbjohnson
Copy link
Member

Fixed by #292.

@GregIngelmo
Copy link
Author

Thank you Ben! I just re-tested, all good here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants