BoltDB
An embedded key/value database for Go (用Go实现的嵌入式键值存储) without a full database server such as Postgres or MySQL
etcd kv storage is boltdb
Feature
- consistency and thread safety
- support transaction
thread safety:
Bolt obtains a file lock on the data file so multiple processes cannot open the same database at the same time. Opening an already open Bolt database will cause it to hang until the other process closes it. To prevent an indefinite wait you can pass a timeout option to the Open() function:
db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
Usage
Init
db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Fatal(err)
}
defer db.Close()
Update
Create Bucket
func createBucket(db *bolt.DB, name string) error {
// write transaction
return db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
if b == nil {
log.Printf("no bucket :%s\n",name)
_, err := tx.CreateBucket([]byte(name))
if err != nil {
return err
}
} else {
log.Printf("already exists bucket %s",name)
}
return nil
})
}
Insert Key/Value into Bucket
func insertBucket(db *bolt.DB, name string, key, value []byte) error {
return db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
if b != nil {
return b.Put(key, value)
} else {
return fmt.Errorf("No have this bucket :%s",name)
}
})
}
Read
func queryBucket(db *bolt.DB, name string, key []byte) ([]byte, error) {
// read-only transaction
var data []byte
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
if b != nil {
data = b.Get(key)
return nil
} else {
return fmt.Errorf("No have this bucket :%s",name)
}
})
return data, err
}
Principle
Key words:
- B+ Tree
- Transaction
- Lock-Free MVCC (Reader is not Locked)
mmap
if Wana know more about mmap u can click this article
Ref
- https://byte_dance.fei_shu.cn/wiki/wikcnOJLqLIObcXsx9zMIkt8PMc#
- www.cnblogs.com/X-knight/p/…
- github.com/etcd-io/bbo…
- https://tech.byte_dance.net/articles/6931533584406675463?from=lark_all_search
- www.jianshu.com/p/b86a69892…