This is a creation in Article, where the information may have evolved or changed.
Description
Boltdb is an embedded Key/value database that allows you to efficiently access data using the APIs provided by Boltdb simply by linking it to your application code. and BOLTDB supports fully serializable acid transactions, making it easier for applications to handle complex operations.
The Boltdb design originates from Lmdb and has the following characteristics:
- Accessing data directly using API, no query statement;
- Supports fully serializable acid transactions, which are stronger than leveldb;
- The data is stored in a memory-mapped file. No Wal, thread compression, and garbage collection;
- Through the cow technology, can realize the lock-free read and write concurrency, but can not achieve lock-free write concurrency, which is doomed to read performance is very high, but generally write performance, suitable and read more write less scenes.
Finally, BOLTDB is developed using Golang and is applied to the INFLUXDB project as the underlying storage.
Use
Installing BOLTDB
go get github.com/boltdb/bolt/...
Open Database
package mainimport ( "log" "github.com/boltdb/bolt")func main() { db, err := bolt.Open("my.db", 0600, nil) if err != nil { log.Fatal(err) } defer db.Close() ...}
Update transaction
err := db.Update(func(tx *bolt.Tx) error { ... return nil})
Description
- This interface enables data update operations
- The operation is handled as a transaction, and if the operation within update () returns nil, the transaction is committed or the transaction is rolled back
Read-Only transactions
err := db.View(func(tx *bolt.Tx) error { ... return nil})
Description
- This interface enables and can only perform data query operations
Bulk Update transactions
err := db.Batch(func(tx *bolt.Tx) error { ... return nil})
Description
- This interface enables multiple data update operations
- All updates are handled as a transaction, and if the operation within update () returns nil, the transaction is committed or the transaction is rolled back
Manual transaction Management
// Start a writable transaction.tx, err := db.Begin(true)if err != nil { return err}defer tx.Rollback()// Use the transaction..._, err := tx.CreateBucket([]byte("MyBucket"))if err != nil { return err}// Commit the transaction and check for error.if err := tx.Commit(); err != nil { return err}
Description
- Create the transaction yourself and manage the commit and rollback of the transaction without using the encapsulated notation provided by BOLTDB
More
For more fun usage please refer to Boltdb/bolt
Performance testing
To give you a visual understanding of Boltdb, I did some basic performance testing on it, and the results were as follows:
Environment preparation
A single thread creates 1000 buckets, and each bucket inserts 10w of data, so the total data volume is approximately 100 million.
Concurrent Write Tests
Test principle:
- Adjusting the number of write concurrency during the test
- Keep concurrent numbers unchanged, adjust write times
Concurrent Read Test
Each round of test pre-created 1000 buckets, each bucket to create 100 records, a total of 10w records, read the process of randomly selected buckets.