Skip to content

Instantly share code, notes, and snippets.

@artyom
Created August 13, 2015 14:35
Show Gist options
  • Select an option

  • Save artyom/805ae04bfa4ae2f8df10 to your computer and use it in GitHub Desktop.

Select an option

Save artyom/805ae04bfa4ae2f8df10 to your computer and use it in GitHub Desktop.
Illustration of inconsistency of Put semantics: https://github.com/boltdb/bolt/issues/324
package main
import (
"encoding/binary"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/boltdb/bolt"
)
func main() {
dir, err := ioutil.TempDir("", "boltdb-")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
db, err := bolt.Open(filepath.Join(dir, "test.db"), 0644, nil)
if err != nil {
log.Print(err)
return
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucketIfNotExists([]byte("one"))
if err != nil {
return err
}
buf := make([]byte, 10)
for i := int64(0); i < 3; i++ {
n := binary.PutVarint(buf, i)
if err := bkt.Put(buf[:n], buf[:n]); err != nil {
return err
}
}
return nil
})
if err != nil {
log.Print(err)
return
}
err = db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte("one"))
if bkt == nil {
return bolt.ErrBucketNotFound
}
c := bkt.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
fmt.Printf("%v\t%v\n", k, v)
}
return nil
})
if err != nil {
log.Print(err)
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment