Raunak Dugar
Raunak Dugar

Reputation: 247

Batching Operations in Boltdb

Currently using db.Update() to update the key-value in boltdb.

err := db.Update(func(tx *bolt.Tx) error {

    b, err := tx.CreateBucket([]byte("widgets"))
    if err != nil {
        return err
    }
    if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
        return err
    }
    return nil
})

How to use db.Batch() operations using go routines?

Upvotes: 3

Views: 2089

Answers (2)

juanmonroynieto
juanmonroynieto

Reputation: 67

Even though BoltDB was archived, it's derivations have retained much of the functionality. I came across this while working with etcd-io/bbolt.

The solution here is to use db.Batch as a drop-in replacement for update. The rest is handled by the library. In this case an example based off of your example would be:

//creating a differential name for each operation to be added
var diffName string
err := db.Batch(func(tx *bolt.Tx) error {

    b, err := tx.CreateBucket([]byte("widgets"))
    if err != nil {
        return err
    }
// hopefully bar would also be different in each case
    if err := b.Put([]byte("foo_"+diffName), []byte("bar")); err != nil {
        return err
    }
    return nil
})

Excuse the necromancy

Upvotes: 0

Marko Kevac
Marko Kevac

Reputation: 2982

Just call db.Batch() from your goroutines. Batch() was created to be used this way. There is an example in documentation.

Upvotes: 2

Related Questions