Bolt [![Coverage Status](https://coveralls.io/repos/boltdb/bolt/badge.svg?branch=master)](https://coveralls.io/r/boltdb/bolt?branch=master) [![GoDoc](https://godoc.org/github.com/boltdb/bolt?status.svg)](https://godoc.org/github.com/boltdb/bolt) ![Version](https://img.shields.io/badge/version-1.2.1-green.svg)
====
Bolt is a pure Go key/value store inspired by [Howard Chu's][hyc_symas]
[LMDB project][lmdb]. The goal of the project is to provide a simple,
fast, and reliable database for projects that don't require a full database
server such as Postgres or MySQL.
Since Bolt is meant to be used as such a low-level piece of functionality,
simplicity is key. The API will be small and only focus on getting values
and setting values. That's it.
[hyc_symas]: https://twitter.com/hyc_symas
[lmdb]: http://symas.com/mdb/
## Project Status
Bolt is stable, the API is fixed, and the file format is fixed. Full unit
test coverage and randomized black box testing are used to ensure database
consistency and thread safety. Bolt is currently used in high-load production
environments serving databases as large as 1TB. Many companies such as
Shopify and Heroku use Bolt-backed services every day.
## Table of Contents
- [Getting Started](#getting-started)
- [Installing](#installing)
- [Opening a database](#opening-a-database)
- [Transactions](#transactions)
- [Read-write transactions](#read-write-transactions)
- [Read-only transactions](#read-only-transactions)
- [Batch read-write transactions](#batch-read-write-transactions)
- [Managing transactions manually](#managing-transactions-manually)
- [Using buckets](#using-buckets)
- [Using key/value pairs](#using-keyvalue-pairs)
- [Autoincrementing integer for the bucket](#autoincrementing-integer-for-the-bucket)
- [Iterating over keys](#iterating-over-keys)
- [Prefix scans](#prefix-scans)
- [Range scans](#range-scans)
- [ForEach()](#foreach)
- [Nested buckets](#nested-buckets)
- [Database backups](#database-backups)
- [Statistics](#statistics)
- [Read-Only Mode](#read-only-mode)
- [Mobile Use (iOS/Android)](#mobile-use-iosandroid)
- [Resources](#resources)
- [Comparison with other databases](#comparison-with-other-databases)
- [Postgres, MySQL, & other relational databases](#postgres-mysql--other-relational-databases)
- [LevelDB, RocksDB](#leveldb-rocksdb)
- [LMDB](#lmdb)
- [Caveats & Limitations](#caveats--limitations)
- [Reading the Source](#reading-the-source)
- [Other Projects Using Bolt](#other-projects-using-bolt)
## Getting Started
### Installing
To start using Bolt, install Go and run `go get`:
```sh
$ go get github.com/boltdb/bolt/...
```
This will retrieve the library and install the `bolt` command line utility into
your `$GOBIN` path.
### Opening a database
The top-level object in Bolt is a `DB`. It is represented as a single file on
your disk and represents a consistent snapshot of your data.
To open your database, simply use the `bolt.Open()` function:
```go
package main
import (
"log"
"github.com/boltdb/bolt"
)
func main() {
// Open the my.db data file in your current directory.
// It will be created if it doesn't exist.
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
...
}
```
Please note that 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:
```go
db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
```
### Transactions
Bolt allows only one read-write transaction at a time but allows as many
read-only transactions as you want at a time. Each transaction has a consistent
view of the data as it existed when the transaction started.
Individual transactions and all objects created from them (e.g. buckets, keys)
are not thread safe. To work with data in multiple goroutines you must start
a transaction for each one or use locking to ensure only one goroutine accesses
a transaction at a time. Creating transaction from the `DB` is thread safe.
Read-only transactions and read-write transactions should not depend on one
another and generally shouldn't be opened simultaneously in the same goroutine.
This can cause a deadlock as the read-write transaction needs to periodically
re-map the data file but it cannot do so while a read-only transaction is open.
#### Read-write transactions
To start a read-write transaction, you can use the `DB.Update()` function:
```go
err := db.Update(func(tx *bolt.Tx) error {
...
return nil
})
```
Inside the closure, you have a consistent view of the database. You commit the
transaction by returning `nil` at the end. You can also rollback the transaction
at any point by returning an error. All database operations are allowed inside
a read-write transaction.
Always check the return error as it will report any disk failures that can cause
your transaction to not complete. If you return an error within your closure
it will be passed through.
#### Read-only transactions
To start a read-only transaction, you can use the `DB.View()` function:
```go
err := db.View(func(tx *bolt.Tx) error {
...
return nil
})
```
You also get a consistent view of the database within this closure, however,
no mutating operations are allowed within a read-only transaction. You can only
retrieve buckets, retrieve values, and copy the database within a read-only
transaction.
#### Batch read-write transactions
Each `DB.Update()` waits for disk to commit the writes. This overhead
can be minimized by combining multiple updates with the `DB.Batch()`
function:
```go
err := db.Batch(func(tx *bolt.Tx) error {
...
return nil
})
```
Concurrent Batch calls are opportunistically combined into larger
transactions. Batch is only useful when there are multiple goroutines
calling it.
The trade-off is that `Batch` can call the given
function multiple times, if parts of the transaction fail. The
function must be idempotent and side effects must take effect only
after a successful return from `DB.Batch()`.
For example: don't display messages from inside the function, instead
set variables in the enclosing scope:
```go
var id uint64
err := db.Batch(func(tx *bolt.Tx) error {
// Find last key in bucket, decode as bigendian uint64, increment
// by one, encode back to []byte, and add new key.
...
id = newValue
return nil
})
if err != nil {
return ...
}
fmt.Println("Allocated ID %d", id)
```
#### Managing transactions manually
The `DB.View()` and `DB.Update()` functions are wrappers around the `DB.Begin()`
function. These helper functions will start the transaction, execute a function,
and then safely close your transaction if an error is returned. This is the
recommended way to use Bolt transactions.
However, sometimes you may want to manually start and end your transactions.
You can use the `DB.Begin()` function directly but **please** be sure to close
the transaction.
```go
// 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
}
```
The first argument to `DB.Begin()` is a boolean stating if the transaction
should be writable.
### Using buckets
Buckets are collections of key/value pairs within the database. All keys in a
bucket must be unique. You can create a bucket using the `DB.CreateBucket()`
function:
```go
db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("MyBucket"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
```
You can also create a bucket only if it doesn't exist by using the
`Tx.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
搜索引擎-使用go开发的高度可定制化的全文搜索引擎-优质项目分享.zip (540个子文件)
AUTHORS 501B
AUTHORS 412B
AUTHORS 368B
AUTHORS 346B
AUTHORS 346B
AUTHORS 346B
AUTHORS 346B
AUTHORS 173B
mkerrors.bash 2KB
mkknownfolderids.bash 1KB
gccgo_c.c 1KB
CONTRIBUTORS 525B
CONTRIBUTORS 460B
CONTRIBUTORS 446B
CONTRIBUTORS 323B
CONTRIBUTORS 313B
CONTRIBUTORS 311B
CONTRIBUTORS 277B
CONTRIBUTORS 170B
Dockerfile 165B
.gitignore 44B
.gitignore 26B
.gitignore 16B
zerrors_windows.go 923KB
tables.go 363KB
zsyscall_windows.go 156KB
zerrors_linux.go 149KB
ztypes_linux.go 140KB
zsysnum_zos_s390x.go 117KB
types_windows.go 84KB
zerrors_openbsd_mips64.go 77KB
zerrors_darwin_amd64.go 76KB
zerrors_darwin_arm64.go 76KB
zerrors_freebsd_arm64.go 76KB
zerrors_freebsd_386.go 76KB
zerrors_freebsd_amd64.go 76KB
zerrors_openbsd_arm64.go 74KB
zerrors_netbsd_386.go 73KB
zerrors_openbsd_amd64.go 73KB
zerrors_netbsd_arm64.go 72KB
zerrors_netbsd_amd64.go 72KB
zerrors_freebsd_arm.go 72KB
zerrors_netbsd_arm.go 72KB
syscall_windows.go 71KB
zerrors_dragonfly_amd64.go 71KB
syscall_linux.go 69KB
zerrors_openbsd_arm.go 68KB
zerrors_openbsd_386.go 68KB
zsyscall_darwin_amd64.go 64KB
zsyscall_darwin_arm64.go 64KB
zerrors_solaris_amd64.go 59KB
zsyscall_solaris_amd64.go 57KB
zerrors_aix_ppc64.go 53KB
zerrors_aix_ppc.go 52KB
security_windows.go 52KB
btree.go 51KB
syscall_zos_s390x.go 49KB
zsyscall_freebsd_arm.go 48KB
zsyscall_freebsd_386.go 48KB
zsyscall_freebsd_amd64.go 48KB
zsyscall_freebsd_arm64.go 48KB
zsyscall_linux.go 47KB
falloc.go 46KB
zsyscall_netbsd_arm.go 44KB
zsyscall_netbsd_386.go 44KB
zsyscall_netbsd_amd64.go 44KB
zsyscall_netbsd_arm64.go 44KB
zsyscall_aix_ppc64_gc.go 42KB
zsyscall_openbsd_arm.go 40KB
zsyscall_openbsd_386.go 40KB
zsyscall_openbsd_mips64.go 39KB
zsyscall_openbsd_amd64.go 39KB
zsyscall_openbsd_arm64.go 39KB
zsyscall_dragonfly_amd64.go 39KB
zsysnum_freebsd_amd64.go 36KB
zsysnum_freebsd_arm64.go 36KB
zsysnum_freebsd_arm.go 36KB
zsysnum_freebsd_386.go 36KB
zsyscall_aix_ppc.go 36KB
zerrors_linux_ppc64le.go 36KB
zerrors_linux_ppc64.go 36KB
zerrors_linux_sparc64.go 36KB
zerrors_linux_ppc.go 36KB
zerrors_linux_s390x.go 36KB
zerrors_zos_s390x.go 34KB
zerrors_linux_mips64le.go 34KB
zerrors_linux_mipsle.go 34KB
zerrors_linux_mips64.go 34KB
zerrors_linux_mips.go 34KB
zerrors_linux_arm.go 33KB
zerrors_linux_amd64.go 33KB
zerrors_linux_386.go 33KB
zerrors_linux_arm64.go 33KB
zerrors_linux_riscv64.go 33KB
zsyscall_aix_ppc64_gccgo.go 31KB
zsyscall_aix_ppc64.go 30KB
zsyscall_zos_s390x.go 30KB
syscall_solaris.go 28KB
zsysnum_dragonfly_amd64.go 28KB
db.go 28KB
共 540 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
极智视界
- 粉丝: 2w+
- 资源: 1504
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功