bolt
bolt copied to clipboard
Batch example creating a bucket and putting a value in one call?
could you add an example of a call to Batch that shows a bucket being created and a value being added in one call?
@tvmaly You can create a tx, and insert multiple kv pairs to batch updates.
See https://github.com/boltdb/bolt/blob/master/cmd/bolt/main.go#L1042-L1065 for example.
@xiang90 But in the code never touch the db.Batch
API, I also want an example to demonstrate how to use the db.Batch
API properly, can you help with that? Thanks.
Oh, it seems db.Batch
is just a helper method, if you create a tx, and insert multiple kv pairs then it is already a so called batch
.
And read from the document:
Concurrent Batch calls are opportunistically combined into larger transactions. Batch is only useful when there are multiple goroutines calling it.
- the function passed to Batch may be called multiple times, regardless of whether it returns error or not.
that means If I do multiple insertions in the db.Batch
, then call it multiple times from different goroutines, and use NextSequence
style keys, then there is a chance my docs get inserted multiple times?
@benbjohnson can u help to verify am I correct?
+1
Would really appreciate some examples and best practices on the use of the db.Batch() method.
Need confirmation from BoltDB developers. The way I understand it, calling Batch()
concurrently combines the transactions into one larger one, sort-of the same effect if it were to put all the code together under one large Update()
. Basically it allows non-blocking writes, useful in cases where you know you are inserting large amount of data at once.
Here's a question: What if one of them fails? Does this mean the rest of the calls also fail with no way to determine which ones are failing?
Edit: Okay, I am seeing that the failed ones are ignored, and Batch()
under the hood is a multiple Update()
calls
https://github.com/boltdb/bolt/blob/fa5367d20c994db73282594be0146ab221657943/db.go#L704