pax_global_header00006660000000000000000000000064146150754070014523gustar00rootroot0000000000000052 comment=014b0285ccf8585fa12d7caf7f1288397c8f2185 bbolt-1.3.10/000077500000000000000000000000001461507540700127075ustar00rootroot00000000000000bbolt-1.3.10/.github/000077500000000000000000000000001461507540700142475ustar00rootroot00000000000000bbolt-1.3.10/.github/dependabot.yml000066400000000000000000000003041461507540700170740ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: github-actions directory: / schedule: interval: weekly - package-ecosystem: gomod directory: / schedule: interval: weekly bbolt-1.3.10/.github/workflows/000077500000000000000000000000001461507540700163045ustar00rootroot00000000000000bbolt-1.3.10/.github/workflows/failpoint_test.yaml000066400000000000000000000007511461507540700222170ustar00rootroot00000000000000name: Failpoint test on: [push, pull_request] permissions: read-all jobs: test: strategy: matrix: os: [ubuntu-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - id: goversion run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT" - uses: actions/setup-go@v3 with: go-version: ${{ steps.goversion.outputs.goversion }} - run: | make gofail-enable make test-failpoint bbolt-1.3.10/.github/workflows/tests.yaml000066400000000000000000000062441461507540700203400ustar00rootroot00000000000000name: Tests on: [push, pull_request] jobs: test-linux: strategy: fail-fast: false matrix: target: - linux-amd64-unit-test-1-cpu - linux-amd64-unit-test-2-cpu - linux-amd64-unit-test-4-cpu - linux-amd64-unit-test-4-cpu-race runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - id: goversion run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT" - uses: actions/setup-go@v3 with: go-version: ${{ steps.goversion.outputs.goversion }} - run: make fmt - env: TARGET: ${{ matrix.target }} run: | case "${TARGET}" in linux-amd64-unit-test-1-cpu) CPU=1 make test ;; linux-amd64-unit-test-2-cpu) CPU=2 make test ;; linux-amd64-unit-test-4-cpu) CPU=4 make test ;; linux-amd64-unit-test-4-cpu-race) # XXX: By default, the Github Action runner will terminate the process # if it has high resource usage. Try to use GOGC to limit memory and # cpu usage here to prevent unexpected terminating. It can be replaced # with GOMEMLIMIT=2048MiB if the go-version is updated to >=1.19.x. # # REF: https://github.com/actions/runner-images/issues/6680#issuecomment-1335778010 GOGC=30 CPU=4 ENABLE_RACE=true make test ;; *) echo "Failed to find target" exit 1 ;; esac - name: golangci-lint uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 # v3.4.0 test-windows: strategy: fail-fast: false matrix: target: - windows-amd64-unit-test-4-cpu # FIXME(fuweid): # # The windows will throws the following error when enable race. # We skip it until we have solution. # # ThreadSanitizer failed to allocate 0x000200000000 (8589934592) bytes at 0x0400c0000000 (error code: 1455) # #- windows-amd64-unit-test-4-cpu-race runs-on: windows-latest steps: - uses: actions/checkout@v3 - id: goversion run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT" - uses: actions/setup-go@v3 with: go-version: ${{ steps.goversion.outputs.goversion }} - run: make fmt - env: TARGET: ${{ matrix.target }} run: | case "${TARGET}" in windows-amd64-unit-test-4-cpu) CPU=4 make test ;; *) echo "Failed to find target" exit 1 ;; esac shell: bash - name: golangci-lint uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 # v3.4.0 coverage: needs: ["test-linux", "test-windows"] strategy: matrix: os: [ubuntu-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - id: goversion run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT" - uses: actions/setup-go@v3 with: go-version: ${{ steps.goversion.outputs.goversion }} - run: make coverage bbolt-1.3.10/.gitignore000066400000000000000000000001171461507540700146760ustar00rootroot00000000000000*.prof *.test *.swp /bin/ cover.out cover-*.out /.idea *.iml /cmd/bbolt/bbolt bbolt-1.3.10/.go-version000066400000000000000000000000071461507540700147750ustar00rootroot000000000000001.21.9 bbolt-1.3.10/LICENSE000066400000000000000000000020661461507540700137200ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2013 Ben Johnson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. bbolt-1.3.10/Makefile000066400000000000000000000031061461507540700143470ustar00rootroot00000000000000BRANCH=`git rev-parse --abbrev-ref HEAD` COMMIT=`git rev-parse --short HEAD` GOLDFLAGS="-X main.branch $(BRANCH) -X main.commit $(COMMIT)" TESTFLAGS_RACE=-race=false ifdef ENABLE_RACE TESTFLAGS_RACE=-race=true endif TESTFLAGS_CPU= ifdef CPU TESTFLAGS_CPU=-cpu=$(CPU) endif TESTFLAGS = $(TESTFLAGS_RACE) $(TESTFLAGS_CPU) $(EXTRA_TESTFLAGS) .PHONY: fmt fmt: !(gofmt -l -s -d $(shell find . -name \*.go) | grep '[a-z]') .PHONY: lint lint: golangci-lint run ./... .PHONY: test test: @echo "hashmap freelist test" TEST_FREELIST_TYPE=hashmap go test -v ${TESTFLAGS} -timeout 30m TEST_FREELIST_TYPE=hashmap go test -v ${TESTFLAGS} ./cmd/bbolt @echo "array freelist test" TEST_FREELIST_TYPE=array go test -v ${TESTFLAGS} -timeout 30m TEST_FREELIST_TYPE=array go test -v ${TESTFLAGS} ./cmd/bbolt .PHONY: coverage coverage: @echo "hashmap freelist test" TEST_FREELIST_TYPE=hashmap go test -v -timeout 30m \ -coverprofile cover-freelist-hashmap.out -covermode atomic @echo "array freelist test" TEST_FREELIST_TYPE=array go test -v -timeout 30m \ -coverprofile cover-freelist-array.out -covermode atomic .PHONY: gofail-enable gofail-enable: install-gofail gofail enable . .PHONY: gofail-disable gofail-disable: gofail disable . .PHONY: install-gofail install-gofail: go install go.etcd.io/gofail .PHONY: test-failpoint test-failpoint: @echo "[failpoint] hashmap freelist test" TEST_FREELIST_TYPE=hashmap go test -v ${TESTFLAGS} -timeout 30m ./tests/failpoint @echo "[failpoint] array freelist test" TEST_FREELIST_TYPE=array go test -v ${TESTFLAGS} -timeout 30m ./tests/failpoint bbolt-1.3.10/README.md000066400000000000000000001141571461507540700141770ustar00rootroot00000000000000bbolt ===== [![Go Report Card](https://goreportcard.com/badge/github.com/etcd-io/bbolt?style=flat-square)](https://goreportcard.com/report/github.com/etcd-io/bbolt) [![Coverage](https://codecov.io/gh/etcd-io/bbolt/branch/master/graph/badge.svg)](https://codecov.io/gh/etcd-io/bbolt) [![Build Status Travis](https://img.shields.io/travis/etcd-io/bboltlabs.svg?style=flat-square&&branch=master)](https://travis-ci.com/etcd-io/bbolt) [![Godoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://godoc.org/github.com/etcd-io/bbolt) [![Releases](https://img.shields.io/github/release/etcd-io/bbolt/all.svg?style=flat-square)](https://github.com/etcd-io/bbolt/releases) [![LICENSE](https://img.shields.io/github/license/etcd-io/bbolt.svg?style=flat-square)](https://github.com/etcd-io/bbolt/blob/master/LICENSE) bbolt is a fork of [Ben Johnson's][gh_ben] [Bolt][bolt] key/value store. The purpose of this fork is to provide the Go community with an active maintenance and development target for Bolt; the goal is improved reliability and stability. bbolt includes bug fixes, performance enhancements, and features not found in Bolt while preserving backwards compatibility with the Bolt API. 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. [gh_ben]: https://github.com/benbjohnson [bolt]: https://github.com/boltdb/bolt [hyc_symas]: https://twitter.com/hyc_symas [lmdb]: https://www.symas.com/symas-embedded-database-lmdb ## 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. ## Project versioning bbolt uses [semantic versioning](http://semver.org). API should not change between patch and minor releases. New minor versions may add additional features to the API. ## 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 go.etcd.io/bbolt@latest ``` This will retrieve the library and update your `go.mod` and `go.sum` files. To run the command line utility, execute: ```sh $ go run go.etcd.io/bbolt/cmd/bbolt@latest ``` Run `go install` to install the `bbolt` command line utility into your `$GOBIN` path, which defaults to `$GOPATH/bin` or `$HOME/go/bin` if the `GOPATH` environment variable is not set. ```sh $ go install go.etcd.io/bbolt/cmd/bbolt@latest ``` ### Importing bbolt To use bbolt as an embedded key-value store, import as: ```go import bolt "go.etcd.io/bbolt" db, err := bolt.Open(path, 0666, nil) if err != nil { return err } defer db.Close() ``` ### 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" bolt "go.etcd.io/bbolt" ) 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. 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 any read-only transaction is open. Even a nested read-only transaction can cause a deadlock, as the child transaction can block the parent transaction from releasing its resources. #### 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 `Tx.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.CreateBucketIfNotExists()` function. It's a common pattern to call this function for all your top-level buckets after you open your database so you can guarantee that they exist for future transactions. To delete a bucket, simply call the `Tx.DeleteBucket()` function. ### Using key/value pairs To save a key/value pair to a bucket, use the `Bucket.Put()` function: ```go db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("MyBucket")) err := b.Put([]byte("answer"), []byte("42")) return err }) ``` This will set the value of the `"answer"` key to `"42"` in the `MyBucket` bucket. To retrieve this value, we can use the `Bucket.Get()` function: ```go db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("MyBucket")) v := b.Get([]byte("answer")) fmt.Printf("The answer is: %s\n", v) return nil }) ``` The `Get()` function does not return an error because its operation is guaranteed to work (unless there is some kind of system failure). If the key exists then it will return its byte slice value. If it doesn't exist then it will return `nil`. It's important to note that you can have a zero-length value set to a key which is different than the key not existing. Use the `Bucket.Delete()` function to delete a key from the bucket. Please note that values returned from `Get()` are only valid while the transaction is open. If you need to use a value outside of the transaction then you must use `copy()` to copy it to another byte slice. ### Autoincrementing integer for the bucket By using the `NextSequence()` function, you can let Bolt determine a sequence which can be used as the unique identifier for your key/value pairs. See the example below. ```go // CreateUser saves u to the store. The new user ID is set on u once the data is persisted. func (s *Store) CreateUser(u *User) error { return s.db.Update(func(tx *bolt.Tx) error { // Retrieve the users bucket. // This should be created when the DB is first opened. b := tx.Bucket([]byte("users")) // Generate ID for the user. // This returns an error only if the Tx is closed or not writeable. // That can't happen in an Update() call so I ignore the error check. id, _ := b.NextSequence() u.ID = int(id) // Marshal user data into bytes. buf, err := json.Marshal(u) if err != nil { return err } // Persist bytes to users bucket. return b.Put(itob(u.ID), buf) }) } // itob returns an 8-byte big endian representation of v. func itob(v int) []byte { b := make([]byte, 8) binary.BigEndian.PutUint64(b, uint64(v)) return b } type User struct { ID int ... } ``` ### Iterating over keys Bolt stores its keys in byte-sorted order within a bucket. This makes sequential iteration over these keys extremely fast. To iterate over keys we'll use a `Cursor`: ```go db.View(func(tx *bolt.Tx) error { // Assume bucket exists and has keys b := tx.Bucket([]byte("MyBucket")) c := b.Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { fmt.Printf("key=%s, value=%s\n", k, v) } return nil }) ``` The cursor allows you to move to a specific point in the list of keys and move forward or backward through the keys one at a time. The following functions are available on the cursor: ``` First() Move to the first key. Last() Move to the last key. Seek() Move to a specific key. Next() Move to the next key. Prev() Move to the previous key. ``` Each of those functions has a return signature of `(key []byte, value []byte)`. You must seek to a position using `First()`, `Last()`, or `Seek()` before calling `Next()` or `Prev()`. If you do not seek to a position then these functions will return a `nil` key. When you have iterated to the end of the cursor, then `Next()` will return a `nil` key and the cursor still points to the last element if present. When you have iterated to the beginning of the cursor, then `Prev()` will return a `nil` key and the cursor still points to the first element if present. If you remove key/value pairs during iteration, the cursor may automatically move to the next position if present in current node each time removing a key. When you call `c.Next()` after removing a key, it may skip one key/value pair. Refer to [pull/611](https://github.com/etcd-io/bbolt/pull/611) to get more detailed info. During iteration, if the key is non-`nil` but the value is `nil`, that means the key refers to a bucket rather than a value. Use `Bucket.Bucket()` to access the sub-bucket. #### Prefix scans To iterate over a key prefix, you can combine `Seek()` and `bytes.HasPrefix()`: ```go db.View(func(tx *bolt.Tx) error { // Assume bucket exists and has keys c := tx.Bucket([]byte("MyBucket")).Cursor() prefix := []byte("1234") for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() { fmt.Printf("key=%s, value=%s\n", k, v) } return nil }) ``` #### Range scans Another common use case is scanning over a range such as a time range. If you use a sortable time encoding such as RFC3339 then you can query a specific date range like this: ```go db.View(func(tx *bolt.Tx) error { // Assume our events bucket exists and has RFC3339 encoded time keys. c := tx.Bucket([]byte("Events")).Cursor() // Our time range spans the 90's decade. min := []byte("1990-01-01T00:00:00Z") max := []byte("2000-01-01T00:00:00Z") // Iterate over the 90's. for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() { fmt.Printf("%s: %s\n", k, v) } return nil }) ``` Note that, while RFC3339 is sortable, the Golang implementation of RFC3339Nano does not use a fixed number of digits after the decimal point and is therefore not sortable. #### ForEach() You can also use the function `ForEach()` if you know you'll be iterating over all the keys in a bucket: ```go db.View(func(tx *bolt.Tx) error { // Assume bucket exists and has keys b := tx.Bucket([]byte("MyBucket")) b.ForEach(func(k, v []byte) error { fmt.Printf("key=%s, value=%s\n", k, v) return nil }) return nil }) ``` Please note that keys and values in `ForEach()` are only valid while the transaction is open. If you need to use a key or value outside of the transaction, you must use `copy()` to copy it to another byte slice. ### Nested buckets You can also store a bucket in a key to create nested buckets. The API is the same as the bucket management API on the `DB` object: ```go func (*Bucket) CreateBucket(key []byte) (*Bucket, error) func (*Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) func (*Bucket) DeleteBucket(key []byte) error ``` Say you had a multi-tenant application where the root level bucket was the account bucket. Inside of this bucket was a sequence of accounts which themselves are buckets. And inside the sequence bucket you could have many buckets pertaining to the Account itself (Users, Notes, etc) isolating the information into logical groupings. ```go // createUser creates a new user in the given account. func createUser(accountID int, u *User) error { // Start the transaction. tx, err := db.Begin(true) if err != nil { return err } defer tx.Rollback() // Retrieve the root bucket for the account. // Assume this has already been created when the account was set up. root := tx.Bucket([]byte(strconv.FormatUint(accountID, 10))) // Setup the users bucket. bkt, err := root.CreateBucketIfNotExists([]byte("USERS")) if err != nil { return err } // Generate an ID for the new user. userID, err := bkt.NextSequence() if err != nil { return err } u.ID = userID // Marshal and save the encoded user. if buf, err := json.Marshal(u); err != nil { return err } else if err := bkt.Put([]byte(strconv.FormatUint(u.ID, 10)), buf); err != nil { return err } // Commit the transaction. if err := tx.Commit(); err != nil { return err } return nil } ``` ### Database backups Bolt is a single file so it's easy to backup. You can use the `Tx.WriteTo()` function to write a consistent view of the database to a writer. If you call this from a read-only transaction, it will perform a hot backup and not block your other database reads and writes. By default, it will use a regular file handle which will utilize the operating system's page cache. See the [`Tx`](https://godoc.org/go.etcd.io/bbolt#Tx) documentation for information about optimizing for larger-than-RAM datasets. One common use case is to backup over HTTP so you can use tools like `cURL` to do database backups: ```go func BackupHandleFunc(w http.ResponseWriter, req *http.Request) { err := db.View(func(tx *bolt.Tx) error { w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Disposition", `attachment; filename="my.db"`) w.Header().Set("Content-Length", strconv.Itoa(int(tx.Size()))) _, err := tx.WriteTo(w) return err }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } ``` Then you can backup using this command: ```sh $ curl http://localhost/backup > my.db ``` Or you can open your browser to `http://localhost/backup` and it will download automatically. If you want to backup to another file you can use the `Tx.CopyFile()` helper function. ### Statistics The database keeps a running count of many of the internal operations it performs so you can better understand what's going on. By grabbing a snapshot of these stats at two points in time we can see what operations were performed in that time range. For example, we could start a goroutine to log stats every 10 seconds: ```go go func() { // Grab the initial stats. prev := db.Stats() for { // Wait for 10s. time.Sleep(10 * time.Second) // Grab the current stats and diff them. stats := db.Stats() diff := stats.Sub(&prev) // Encode stats to JSON and print to STDERR. json.NewEncoder(os.Stderr).Encode(diff) // Save stats for the next loop. prev = stats } }() ``` It's also useful to pipe these stats to a service such as statsd for monitoring or to provide an HTTP endpoint that will perform a fixed-length sample. ### Read-Only Mode Sometimes it is useful to create a shared, read-only Bolt database. To this, set the `Options.ReadOnly` flag when opening your database. Read-only mode uses a shared lock to allow multiple processes to read from the database but it will block any processes from opening the database in read-write mode. ```go db, err := bolt.Open("my.db", 0666, &bolt.Options{ReadOnly: true}) if err != nil { log.Fatal(err) } ``` ### Mobile Use (iOS/Android) Bolt is able to run on mobile devices by leveraging the binding feature of the [gomobile](https://github.com/golang/mobile) tool. Create a struct that will contain your database logic and a reference to a `*bolt.DB` with a initializing constructor that takes in a filepath where the database file will be stored. Neither Android nor iOS require extra permissions or cleanup from using this method. ```go func NewBoltDB(filepath string) *BoltDB { db, err := bolt.Open(filepath+"/demo.db", 0600, nil) if err != nil { log.Fatal(err) } return &BoltDB{db} } type BoltDB struct { db *bolt.DB ... } func (b *BoltDB) Path() string { return b.db.Path() } func (b *BoltDB) Close() { b.db.Close() } ``` Database logic should be defined as methods on this wrapper struct. To initialize this struct from the native language (both platforms now sync their local storage to the cloud. These snippets disable that functionality for the database file): #### Android ```java String path; if (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.LOLLIPOP){ path = getNoBackupFilesDir().getAbsolutePath(); } else{ path = getFilesDir().getAbsolutePath(); } Boltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path) ``` #### iOS ```objc - (void)demo { NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; GoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path); [self addSkipBackupAttributeToItemAtPath:demo.path]; //Some DB Logic would go here [demo close]; } - (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString { NSURL* URL= [NSURL fileURLWithPath: filePathString]; assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]); NSError *error = nil; BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &error]; if(!success){ NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error); } return success; } ``` ## Resources For more information on getting started with Bolt, check out the following articles: * [Intro to BoltDB: Painless Performant Persistence](http://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/) by [Nate Finch](https://github.com/natefinch). * [Bolt -- an embedded key/value database for Go](https://www.progville.com/go/bolt-embedded-db-golang/) by Progville ## Comparison with other databases ### Postgres, MySQL, & other relational databases Relational databases structure data into rows and are only accessible through the use of SQL. This approach provides flexibility in how you store and query your data but also incurs overhead in parsing and planning SQL statements. Bolt accesses all data by a byte slice key. This makes Bolt fast to read and write data by key but provides no built-in support for joining values together. Most relational databases (with the exception of SQLite) are standalone servers that run separately from your application. This gives your systems flexibility to connect multiple application servers to a single database server but also adds overhead in serializing and transporting data over the network. Bolt runs as a library included in your application so all data access has to go through your application's process. This brings data closer to your application but limits multi-process access to the data. ### LevelDB, RocksDB LevelDB and its derivatives (RocksDB, HyperLevelDB) are similar to Bolt in that they are libraries bundled into the application, however, their underlying structure is a log-structured merge-tree (LSM tree). An LSM tree optimizes random writes by using a write ahead log and multi-tiered, sorted files called SSTables. Bolt uses a B+tree internally and only a single file. Both approaches have trade-offs. If you require a high random write throughput (>10,000 w/sec) or you need to use spinning disks then LevelDB could be a good choice. If your application is read-heavy or does a lot of range scans then Bolt could be a good choice. One other important consideration is that LevelDB does not have transactions. It supports batch writing of key/values pairs and it supports read snapshots but it will not give you the ability to do a compare-and-swap operation safely. Bolt supports fully serializable ACID transactions. ### LMDB Bolt was originally a port of LMDB so it is architecturally similar. Both use a B+tree, have ACID semantics with fully serializable transactions, and support lock-free MVCC using a single writer and multiple readers. The two projects have somewhat diverged. LMDB heavily focuses on raw performance while Bolt has focused on simplicity and ease of use. For example, LMDB allows several unsafe actions such as direct writes for the sake of performance. Bolt opts to disallow actions which can leave the database in a corrupted state. The only exception to this in Bolt is `DB.NoSync`. There are also a few differences in API. LMDB requires a maximum mmap size when opening an `mdb_env` whereas Bolt will handle incremental mmap resizing automatically. LMDB overloads the getter and setter functions with multiple flags whereas Bolt splits these specialized cases into their own functions. ## Caveats & Limitations It's important to pick the right tool for the job and Bolt is no exception. Here are a few things to note when evaluating and using Bolt: * Bolt is good for read intensive workloads. Sequential write performance is also fast but random writes can be slow. You can use `DB.Batch()` or add a write-ahead log to help mitigate this issue. * Bolt uses a B+tree internally so there can be a lot of random page access. SSDs provide a significant performance boost over spinning disks. * Try to avoid long running read transactions. Bolt uses copy-on-write so old pages cannot be reclaimed while an old transaction is using them. * Byte slices returned from Bolt are only valid during a transaction. Once the transaction has been committed or rolled back then the memory they point to can be reused by a new page or can be unmapped from virtual memory and you'll see an `unexpected fault address` panic when accessing it. * Bolt uses an exclusive write lock on the database file so it cannot be shared by multiple processes. * Be careful when using `Bucket.FillPercent`. Setting a high fill percent for buckets that have random inserts will cause your database to have very poor page utilization. * Use larger buckets in general. Smaller buckets causes poor page utilization once they become larger than the page size (typically 4KB). * Bulk loading a lot of random writes into a new bucket can be slow as the page will not split until the transaction is committed. Randomly inserting more than 100,000 key/value pairs into a single new bucket in a single transaction is not advised. * Bolt uses a memory-mapped file so the underlying operating system handles the caching of the data. Typically, the OS will cache as much of the file as it can in memory and will release memory as needed to other processes. This means that Bolt can show very high memory usage when working with large databases. However, this is expected and the OS will release memory as needed. Bolt can handle databases much larger than the available physical RAM, provided its memory-map fits in the process virtual address space. It may be problematic on 32-bits systems. * The data structures in the Bolt database are memory mapped so the data file will be endian specific. This means that you cannot copy a Bolt file from a little endian machine to a big endian machine and have it work. For most users this is not a concern since most modern CPUs are little endian. * Because of the way pages are laid out on disk, Bolt cannot truncate data files and return free pages back to the disk. Instead, Bolt maintains a free list of unused pages within its data file. These free pages can be reused by later transactions. This works well for many use cases as databases generally tend to grow. However, it's important to note that deleting large chunks of data will not allow you to reclaim that space on disk. * Removing key/values pairs in a bucket during iteration on the bucket using cursor may not work properly. Each time when removing a key/value pair, the cursor may automatically move to the next position if present. When users call `c.Next()` after removing a key, it may skip one key/value pair. Refer to https://github.com/etcd-io/bbolt/pull/611 for more detailed info. For more information on page allocation, [see this comment][page-allocation]. [page-allocation]: https://github.com/boltdb/bolt/issues/308#issuecomment-74811638 ## Reading the Source Bolt is a relatively small code base (<5KLOC) for an embedded, serializable, transactional key/value database so it can be a good starting point for people interested in how databases work. The best places to start are the main entry points into Bolt: - `Open()` - Initializes the reference to the database. It's responsible for creating the database if it doesn't exist, obtaining an exclusive lock on the file, reading the meta pages, & memory-mapping the file. - `DB.Begin()` - Starts a read-only or read-write transaction depending on the value of the `writable` argument. This requires briefly obtaining the "meta" lock to keep track of open transactions. Only one read-write transaction can exist at a time so the "rwlock" is acquired during the life of a read-write transaction. - `Bucket.Put()` - Writes a key/value pair into a bucket. After validating the arguments, a cursor is used to traverse the B+tree to the page and position where they key & value will be written. Once the position is found, the bucket materializes the underlying page and the page's parent pages into memory as "nodes". These nodes are where mutations occur during read-write transactions. These changes get flushed to disk during commit. - `Bucket.Get()` - Retrieves a key/value pair from a bucket. This uses a cursor to move to the page & position of a key/value pair. During a read-only transaction, the key and value data is returned as a direct reference to the underlying mmap file so there's no allocation overhead. For read-write transactions, this data may reference the mmap file or one of the in-memory node values. - `Cursor` - This object is simply for traversing the B+tree of on-disk pages or in-memory nodes. It can seek to a specific key, move to the first or last value, or it can move forward or backward. The cursor handles the movement up and down the B+tree transparently to the end user. - `Tx.Commit()` - Converts the in-memory dirty nodes and the list of free pages into pages to be written to disk. Writing to disk then occurs in two phases. First, the dirty pages are written to disk and an `fsync()` occurs. Second, a new meta page with an incremented transaction ID is written and another `fsync()` occurs. This two phase write ensures that partially written data pages are ignored in the event of a crash since the meta page pointing to them is never written. Partially written meta pages are invalidated because they are written with a checksum. If you have additional notes that could be helpful for others, please submit them via pull request. ## Other Projects Using Bolt Below is a list of public, open source projects that use Bolt: * [Algernon](https://github.com/xyproto/algernon) - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend. * [Bazil](https://bazil.org/) - A file system that lets your data reside where it is most convenient for it to reside. * [bolter](https://github.com/hasit/bolter) - Command-line app for viewing BoltDB file in your terminal. * [boltcli](https://github.com/spacewander/boltcli) - the redis-cli for boltdb with Lua script support. * [BoltHold](https://github.com/timshannon/bolthold) - An embeddable NoSQL store for Go types built on BoltDB * [BoltStore](https://github.com/yosssi/boltstore) - Session store using Bolt. * [Boltdb Boilerplate](https://github.com/bobintornado/boltdb-boilerplate) - Boilerplate wrapper around bolt aiming to make simple calls one-liners. * [BoltDbWeb](https://github.com/evnix/boltdbweb) - A web based GUI for BoltDB files. * [BoltDB Viewer](https://github.com/zc310/rich_boltdb) - A BoltDB Viewer Can run on Windows、Linux、Android system. * [bleve](http://www.blevesearch.com/) - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend. * [btcwallet](https://github.com/btcsuite/btcwallet) - A bitcoin wallet. * [buckets](https://github.com/joyrexus/buckets) - a bolt wrapper streamlining simple tx and key scans. * [cayley](https://github.com/google/cayley) - Cayley is an open-source graph database using Bolt as optional backend. * [ChainStore](https://github.com/pressly/chainstore) - Simple key-value interface to a variety of storage engines organized as a chain of operations. * [🌰 Chestnut](https://github.com/jrapoport/chestnut) - Chestnut is encrypted storage for Go. * [Consul](https://github.com/hashicorp/consul) - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware. * [DVID](https://github.com/janelia-flyem/dvid) - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb. * [dcrwallet](https://github.com/decred/dcrwallet) - A wallet for the Decred cryptocurrency. * [drive](https://github.com/odeke-em/drive) - drive is an unofficial Google Drive command line client for \*NIX operating systems. * [event-shuttle](https://github.com/sclasen/event-shuttle) - A Unix system service to collect and reliably deliver messages to Kafka. * [Freehold](http://tshannon.bitbucket.org/freehold/) - An open, secure, and lightweight platform for your files and data. * [Go Report Card](https://goreportcard.com/) - Go code quality report cards as a (free and open source) service. * [GoWebApp](https://github.com/josephspurrier/gowebapp) - A basic MVC web application in Go using BoltDB. * [GoShort](https://github.com/pankajkhairnar/goShort) - GoShort is a URL shortener written in Golang and BoltDB for persistent key/value storage and for routing it's using high performent HTTPRouter. * [gopherpit](https://github.com/gopherpit/gopherpit) - A web service to manage Go remote import paths with custom domains * [gokv](https://github.com/philippgille/gokv) - Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more) * [Gitchain](https://github.com/gitchain/gitchain) - Decentralized, peer-to-peer Git repositories aka "Git meets Bitcoin". * [InfluxDB](https://influxdata.com) - Scalable datastore for metrics, events, and real-time analytics. * [ipLocator](https://github.com/AndreasBriese/ipLocator) - A fast ip-geo-location-server using bolt with bloom filters. * [ipxed](https://github.com/kelseyhightower/ipxed) - Web interface and api for ipxed. * [Ironsmith](https://github.com/timshannon/ironsmith) - A simple, script-driven continuous integration (build - > test -> release) tool, with no external dependencies * [Kala](https://github.com/ajvb/kala) - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs. * [Key Value Access Language (KVAL)](https://github.com/kval-access-language) - A proposed grammar for key-value datastores offering a bbolt binding. * [LedisDB](https://github.com/siddontang/ledisdb) - A high performance NoSQL, using Bolt as optional storage. * [lru](https://github.com/crowdriff/lru) - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores. * [mbuckets](https://github.com/abhigupta912/mbuckets) - A Bolt wrapper that allows easy operations on multi level (nested) buckets. * [MetricBase](https://github.com/msiebuhr/MetricBase) - Single-binary version of Graphite. * [MuLiFS](https://github.com/dankomiocevic/mulifs) - Music Library Filesystem creates a filesystem to organise your music files. * [NATS](https://github.com/nats-io/nats-streaming-server) - NATS Streaming uses bbolt for message and metadata storage. * [Prometheus Annotation Server](https://github.com/oliver006/prom_annotation_server) - Annotation server for PromDash & Prometheus service monitoring system. * [Rain](https://github.com/cenkalti/rain) - BitTorrent client and library. * [reef-pi](https://github.com/reef-pi/reef-pi) - reef-pi is an award winning, modular, DIY reef tank controller using easy to learn electronics based on a Raspberry Pi. * [Request Baskets](https://github.com/darklynx/request-baskets) - A web service to collect arbitrary HTTP requests and inspect them via REST API or simple web UI, similar to [RequestBin](http://requestb.in/) service * [Seaweed File System](https://github.com/chrislusf/seaweedfs) - Highly scalable distributed key~file system with O(1) disk read. * [stow](https://github.com/djherbis/stow) - a persistence manager for objects backed by boltdb. * [Storm](https://github.com/asdine/storm) - Simple and powerful ORM for BoltDB. * [SimpleBolt](https://github.com/xyproto/simplebolt) - A simple way to use BoltDB. Deals mainly with strings. * [Skybox Analytics](https://github.com/skybox/skybox) - A standalone funnel analysis tool for web analytics. * [Scuttlebutt](https://github.com/benbjohnson/scuttlebutt) - Uses Bolt to store and process all Twitter mentions of GitHub projects. * [tentacool](https://github.com/optiflows/tentacool) - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server. * [torrent](https://github.com/anacrolix/torrent) - Full-featured BitTorrent client package and utilities in Go. BoltDB is a storage backend in development. * [Wiki](https://github.com/peterhellberg/wiki) - A tiny wiki using Goji, BoltDB and Blackfriday. If you are using Bolt in a project please send a pull request to add it to the list. bbolt-1.3.10/allocate_test.go000066400000000000000000000011501461507540700160560ustar00rootroot00000000000000package bbolt import ( "testing" ) func TestTx_allocatePageStats(t *testing.T) { f := newTestFreelist() ids := []pgid{2, 3} f.readIDs(ids) tx := &Tx{ db: &DB{ freelist: f, pageSize: defaultPageSize, }, meta: &meta{}, pages: make(map[pgid]*page), } txStats := tx.Stats() prePageCnt := txStats.GetPageCount() allocateCnt := f.free_count() if _, err := tx.allocate(allocateCnt); err != nil { t.Fatal(err) } txStats = tx.Stats() if txStats.GetPageCount() != prePageCnt+int64(allocateCnt) { t.Errorf("Allocated %d but got %d page in stats", allocateCnt, txStats.GetPageCount()) } } bbolt-1.3.10/bolt_386.go000066400000000000000000000003251461507540700145760ustar00rootroot00000000000000package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0x7FFFFFFF // 2GB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0xFFFFFFF bbolt-1.3.10/bolt_amd64.go000066400000000000000000000003341461507540700151710ustar00rootroot00000000000000package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0xFFFFFFFFFFFF // 256TB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0x7FFFFFFF bbolt-1.3.10/bolt_arm.go000066400000000000000000000003251461507540700150350ustar00rootroot00000000000000package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0x7FFFFFFF // 2GB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0xFFFFFFF bbolt-1.3.10/bolt_arm64.go000066400000000000000000000003761461507540700152150ustar00rootroot00000000000000//go:build arm64 // +build arm64 package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0xFFFFFFFFFFFF // 256TB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0x7FFFFFFF bbolt-1.3.10/bolt_linux.go000066400000000000000000000002541461507540700154160ustar00rootroot00000000000000package bbolt import ( "syscall" ) // fdatasync flushes written data to a file descriptor. func fdatasync(db *DB) error { return syscall.Fdatasync(int(db.file.Fd())) } bbolt-1.3.10/bolt_loong64.go000066400000000000000000000004021461507540700155420ustar00rootroot00000000000000//go:build loong64 // +build loong64 package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0xFFFFFFFFFFFF // 256TB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0x7FFFFFFF bbolt-1.3.10/bolt_mips64x.go000066400000000000000000000004231461507540700155670ustar00rootroot00000000000000//go:build mips64 || mips64le // +build mips64 mips64le package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0x8000000000 // 512GB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0x7FFFFFFF bbolt-1.3.10/bolt_mipsx.go000066400000000000000000000004061461507540700154160ustar00rootroot00000000000000//go:build mips || mipsle // +build mips mipsle package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0x40000000 // 1GB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0xFFFFFFF bbolt-1.3.10/bolt_openbsd.go000066400000000000000000000003611461507540700157100ustar00rootroot00000000000000package bbolt import ( "golang.org/x/sys/unix" ) func msync(db *DB) error { return unix.Msync(db.data[:db.datasz], unix.MS_INVALIDATE) } func fdatasync(db *DB) error { if db.data != nil { return msync(db) } return db.file.Sync() } bbolt-1.3.10/bolt_ppc.go000066400000000000000000000003631461507540700150420ustar00rootroot00000000000000//go:build ppc // +build ppc package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0x7FFFFFFF // 2GB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0xFFFFFFF bbolt-1.3.10/bolt_ppc64.go000066400000000000000000000003761461507540700152200ustar00rootroot00000000000000//go:build ppc64 // +build ppc64 package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0xFFFFFFFFFFFF // 256TB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0x7FFFFFFF bbolt-1.3.10/bolt_ppc64le.go000066400000000000000000000004021461507540700155270ustar00rootroot00000000000000//go:build ppc64le // +build ppc64le package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0xFFFFFFFFFFFF // 256TB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0x7FFFFFFF bbolt-1.3.10/bolt_riscv64.go000066400000000000000000000004021461507540700155520ustar00rootroot00000000000000//go:build riscv64 // +build riscv64 package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0xFFFFFFFFFFFF // 256TB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0x7FFFFFFF bbolt-1.3.10/bolt_s390x.go000066400000000000000000000003761461507540700151520ustar00rootroot00000000000000//go:build s390x // +build s390x package bbolt // maxMapSize represents the largest mmap size supported by Bolt. const maxMapSize = 0xFFFFFFFFFFFF // 256TB // maxAllocSize is the size used when creating array pointers. const maxAllocSize = 0x7FFFFFFF bbolt-1.3.10/bolt_unix.go000066400000000000000000000037001461507540700152410ustar00rootroot00000000000000//go:build !windows && !plan9 && !solaris && !aix // +build !windows,!plan9,!solaris,!aix package bbolt import ( "fmt" "syscall" "time" "unsafe" "golang.org/x/sys/unix" ) // flock acquires an advisory lock on a file descriptor. func flock(db *DB, exclusive bool, timeout time.Duration) error { var t time.Time if timeout != 0 { t = time.Now() } fd := db.file.Fd() flag := syscall.LOCK_NB if exclusive { flag |= syscall.LOCK_EX } else { flag |= syscall.LOCK_SH } for { // Attempt to obtain an exclusive lock. err := syscall.Flock(int(fd), flag) if err == nil { return nil } else if err != syscall.EWOULDBLOCK { return err } // If we timed out then return an error. if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout { return ErrTimeout } // Wait for a bit and try again. time.Sleep(flockRetryTimeout) } } // funlock releases an advisory lock on a file descriptor. func funlock(db *DB) error { return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN) } // mmap memory maps a DB's data file. func mmap(db *DB, sz int) error { // Map the data file to memory. b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags) if err != nil { return err } // Advise the kernel that the mmap is accessed randomly. err = unix.Madvise(b, syscall.MADV_RANDOM) if err != nil && err != syscall.ENOSYS { // Ignore not implemented error in kernel because it still works. return fmt.Errorf("madvise: %s", err) } // Save the original byte slice and convert to a byte array pointer. db.dataref = b db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0])) db.datasz = sz return nil } // munmap unmaps a DB's data file from memory. func munmap(db *DB) error { // Ignore the unmap if we have no mapped data. if db.dataref == nil { return nil } // Unmap using the original byte slice. err := unix.Munmap(db.dataref) db.dataref = nil db.data = nil db.datasz = 0 return err } bbolt-1.3.10/bolt_unix_aix.go000066400000000000000000000037251461507540700161110ustar00rootroot00000000000000//go:build aix // +build aix package bbolt import ( "fmt" "syscall" "time" "unsafe" "golang.org/x/sys/unix" ) // flock acquires an advisory lock on a file descriptor. func flock(db *DB, exclusive bool, timeout time.Duration) error { var t time.Time if timeout != 0 { t = time.Now() } fd := db.file.Fd() var lockType int16 if exclusive { lockType = syscall.F_WRLCK } else { lockType = syscall.F_RDLCK } for { // Attempt to obtain an exclusive lock. lock := syscall.Flock_t{Type: lockType} err := syscall.FcntlFlock(fd, syscall.F_SETLK, &lock) if err == nil { return nil } else if err != syscall.EAGAIN { return err } // If we timed out then return an error. if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout { return ErrTimeout } // Wait for a bit and try again. time.Sleep(flockRetryTimeout) } } // funlock releases an advisory lock on a file descriptor. func funlock(db *DB) error { var lock syscall.Flock_t lock.Start = 0 lock.Len = 0 lock.Type = syscall.F_UNLCK lock.Whence = 0 return syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock) } // mmap memory maps a DB's data file. func mmap(db *DB, sz int) error { // Map the data file to memory. b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags) if err != nil { return err } // Advise the kernel that the mmap is accessed randomly. if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil { return fmt.Errorf("madvise: %s", err) } // Save the original byte slice and convert to a byte array pointer. db.dataref = b db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0])) db.datasz = sz return nil } // munmap unmaps a DB's data file from memory. func munmap(db *DB) error { // Ignore the unmap if we have no mapped data. if db.dataref == nil { return nil } // Unmap using the original byte slice. err := unix.Munmap(db.dataref) db.dataref = nil db.data = nil db.datasz = 0 return err } bbolt-1.3.10/bolt_unix_solaris.go000066400000000000000000000036671461507540700170110ustar00rootroot00000000000000package bbolt import ( "fmt" "syscall" "time" "unsafe" "golang.org/x/sys/unix" ) // flock acquires an advisory lock on a file descriptor. func flock(db *DB, exclusive bool, timeout time.Duration) error { var t time.Time if timeout != 0 { t = time.Now() } fd := db.file.Fd() var lockType int16 if exclusive { lockType = syscall.F_WRLCK } else { lockType = syscall.F_RDLCK } for { // Attempt to obtain an exclusive lock. lock := syscall.Flock_t{Type: lockType} err := syscall.FcntlFlock(fd, syscall.F_SETLK, &lock) if err == nil { return nil } else if err != syscall.EAGAIN { return err } // If we timed out then return an error. if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout { return ErrTimeout } // Wait for a bit and try again. time.Sleep(flockRetryTimeout) } } // funlock releases an advisory lock on a file descriptor. func funlock(db *DB) error { var lock syscall.Flock_t lock.Start = 0 lock.Len = 0 lock.Type = syscall.F_UNLCK lock.Whence = 0 return syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock) } // mmap memory maps a DB's data file. func mmap(db *DB, sz int) error { // Map the data file to memory. b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags) if err != nil { return err } // Advise the kernel that the mmap is accessed randomly. if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil { return fmt.Errorf("madvise: %s", err) } // Save the original byte slice and convert to a byte array pointer. db.dataref = b db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0])) db.datasz = sz return nil } // munmap unmaps a DB's data file from memory. func munmap(db *DB) error { // Ignore the unmap if we have no mapped data. if db.dataref == nil { return nil } // Unmap using the original byte slice. err := unix.Munmap(db.dataref) db.dataref = nil db.data = nil db.datasz = 0 return err } bbolt-1.3.10/bolt_windows.go000066400000000000000000000055301461507540700157530ustar00rootroot00000000000000package bbolt import ( "fmt" "os" "syscall" "time" "unsafe" "golang.org/x/sys/windows" ) // fdatasync flushes written data to a file descriptor. func fdatasync(db *DB) error { return db.file.Sync() } // flock acquires an advisory lock on a file descriptor. func flock(db *DB, exclusive bool, timeout time.Duration) error { var t time.Time if timeout != 0 { t = time.Now() } var flags uint32 = windows.LOCKFILE_FAIL_IMMEDIATELY if exclusive { flags |= windows.LOCKFILE_EXCLUSIVE_LOCK } for { // Fix for https://github.com/etcd-io/bbolt/issues/121. Use byte-range // -1..0 as the lock on the database file. var m1 uint32 = (1 << 32) - 1 // -1 in a uint32 err := windows.LockFileEx(windows.Handle(db.file.Fd()), flags, 0, 1, 0, &windows.Overlapped{ Offset: m1, OffsetHigh: m1, }) if err == nil { return nil } else if err != windows.ERROR_LOCK_VIOLATION { return err } // If we timed oumercit then return an error. if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout { return ErrTimeout } // Wait for a bit and try again. time.Sleep(flockRetryTimeout) } } // funlock releases an advisory lock on a file descriptor. func funlock(db *DB) error { var m1 uint32 = (1 << 32) - 1 // -1 in a uint32 return windows.UnlockFileEx(windows.Handle(db.file.Fd()), 0, 1, 0, &windows.Overlapped{ Offset: m1, OffsetHigh: m1, }) } // mmap memory maps a DB's data file. // Based on: https://github.com/edsrzf/mmap-go func mmap(db *DB, sz int) error { var sizelo, sizehi uint32 if !db.readOnly { // Truncate the database to the size of the mmap. if err := db.file.Truncate(int64(sz)); err != nil { return fmt.Errorf("truncate: %s", err) } sizehi = uint32(sz >> 32) sizelo = uint32(sz) & 0xffffffff } // Open a file mapping handle. h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizehi, sizelo, nil) if h == 0 { return os.NewSyscallError("CreateFileMapping", errno) } // Create the memory map. addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, 0) if addr == 0 { // Do our best and report error returned from MapViewOfFile. _ = syscall.CloseHandle(h) return os.NewSyscallError("MapViewOfFile", errno) } // Close mapping handle. if err := syscall.CloseHandle(syscall.Handle(h)); err != nil { return os.NewSyscallError("CloseHandle", err) } // Convert to a byte array. db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr))) db.datasz = sz return nil } // munmap unmaps a pointer from a file. // Based on: https://github.com/edsrzf/mmap-go func munmap(db *DB) error { if db.data == nil { return nil } addr := (uintptr)(unsafe.Pointer(&db.data[0])) var err1 error if err := syscall.UnmapViewOfFile(addr); err != nil { err1 = os.NewSyscallError("UnmapViewOfFile", err) } db.data = nil db.datasz = 0 return err1 } bbolt-1.3.10/boltsync_unix.go000066400000000000000000000003361461507540700161400ustar00rootroot00000000000000//go:build !windows && !plan9 && !linux && !openbsd // +build !windows,!plan9,!linux,!openbsd package bbolt // fdatasync flushes written data to a file descriptor. func fdatasync(db *DB) error { return db.file.Sync() } bbolt-1.3.10/bucket.go000066400000000000000000000532561461507540700145260ustar00rootroot00000000000000package bbolt import ( "bytes" "fmt" "unsafe" ) const ( // MaxKeySize is the maximum length of a key, in bytes. MaxKeySize = 32768 // MaxValueSize is the maximum length of a value, in bytes. MaxValueSize = (1 << 31) - 2 ) const bucketHeaderSize = int(unsafe.Sizeof(bucket{})) const ( minFillPercent = 0.1 maxFillPercent = 1.0 ) // DefaultFillPercent is the percentage that split pages are filled. // This value can be changed by setting Bucket.FillPercent. const DefaultFillPercent = 0.5 // Bucket represents a collection of key/value pairs inside the database. type Bucket struct { *bucket tx *Tx // the associated transaction buckets map[string]*Bucket // subbucket cache page *page // inline page reference rootNode *node // materialized node for the root page. nodes map[pgid]*node // node cache // Sets the threshold for filling nodes when they split. By default, // the bucket will fill to 50% but it can be useful to increase this // amount if you know that your write workloads are mostly append-only. // // This is non-persisted across transactions so it must be set in every Tx. FillPercent float64 } // bucket represents the on-file representation of a bucket. // This is stored as the "value" of a bucket key. If the bucket is small enough, // then its root page can be stored inline in the "value", after the bucket // header. In the case of inline buckets, the "root" will be 0. type bucket struct { root pgid // page id of the bucket's root-level page sequence uint64 // monotonically incrementing, used by NextSequence() } // newBucket returns a new bucket associated with a transaction. func newBucket(tx *Tx) Bucket { var b = Bucket{tx: tx, FillPercent: DefaultFillPercent} if tx.writable { b.buckets = make(map[string]*Bucket) b.nodes = make(map[pgid]*node) } return b } // Tx returns the tx of the bucket. func (b *Bucket) Tx() *Tx { return b.tx } // Root returns the root of the bucket. func (b *Bucket) Root() pgid { return b.root } // Writable returns whether the bucket is writable. func (b *Bucket) Writable() bool { return b.tx.writable } // Cursor creates a cursor associated with the bucket. // The cursor is only valid as long as the transaction is open. // Do not use a cursor after the transaction is closed. func (b *Bucket) Cursor() *Cursor { // Update transaction statistics. b.tx.stats.IncCursorCount(1) // Allocate and return a cursor. return &Cursor{ bucket: b, stack: make([]elemRef, 0), } } // Bucket retrieves a nested bucket by name. // Returns nil if the bucket does not exist. // The bucket instance is only valid for the lifetime of the transaction. func (b *Bucket) Bucket(name []byte) *Bucket { if b.buckets != nil { if child := b.buckets[string(name)]; child != nil { return child } } // Move cursor to key. c := b.Cursor() k, v, flags := c.seek(name) // Return nil if the key doesn't exist or it is not a bucket. if !bytes.Equal(name, k) || (flags&bucketLeafFlag) == 0 { return nil } // Otherwise create a bucket and cache it. var child = b.openBucket(v) if b.buckets != nil { b.buckets[string(name)] = child } return child } // Helper method that re-interprets a sub-bucket value // from a parent into a Bucket func (b *Bucket) openBucket(value []byte) *Bucket { var child = newBucket(b.tx) // Unaligned access requires a copy to be made. const unalignedMask = unsafe.Alignof(struct { bucket page }{}) - 1 unaligned := uintptr(unsafe.Pointer(&value[0]))&unalignedMask != 0 if unaligned { value = cloneBytes(value) } // If this is a writable transaction then we need to copy the bucket entry. // Read-only transactions can point directly at the mmap entry. if b.tx.writable && !unaligned { child.bucket = &bucket{} *child.bucket = *(*bucket)(unsafe.Pointer(&value[0])) } else { child.bucket = (*bucket)(unsafe.Pointer(&value[0])) } // Save a reference to the inline page if the bucket is inline. if child.root == 0 { child.page = (*page)(unsafe.Pointer(&value[bucketHeaderSize])) } return &child } // CreateBucket creates a new bucket at the given key and returns the new bucket. // Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long. // The bucket instance is only valid for the lifetime of the transaction. func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) { if b.tx.db == nil { return nil, ErrTxClosed } else if !b.tx.writable { return nil, ErrTxNotWritable } else if len(key) == 0 { return nil, ErrBucketNameRequired } // Insert into node. // Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent // it from being marked as leaking, and accordingly cannot be allocated on stack. newKey := cloneBytes(key) // Move cursor to correct position. c := b.Cursor() k, _, flags := c.seek(newKey) // Return an error if there is an existing key. if bytes.Equal(newKey, k) { if (flags & bucketLeafFlag) != 0 { return nil, ErrBucketExists } return nil, ErrIncompatibleValue } // Create empty, inline bucket. var bucket = Bucket{ bucket: &bucket{}, rootNode: &node{isLeaf: true}, FillPercent: DefaultFillPercent, } var value = bucket.write() c.node().put(newKey, newKey, value, 0, bucketLeafFlag) // Since subbuckets are not allowed on inline buckets, we need to // dereference the inline page, if it exists. This will cause the bucket // to be treated as a regular, non-inline bucket for the rest of the tx. b.page = nil return b.Bucket(newKey), nil } // CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it. // Returns an error if the bucket name is blank, or if the bucket name is too long. // The bucket instance is only valid for the lifetime of the transaction. func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) { child, err := b.CreateBucket(key) if err == ErrBucketExists { return b.Bucket(key), nil } else if err != nil { return nil, err } return child, nil } // DeleteBucket deletes a bucket at the given key. // Returns an error if the bucket does not exist, or if the key represents a non-bucket value. func (b *Bucket) DeleteBucket(key []byte) error { if b.tx.db == nil { return ErrTxClosed } else if !b.Writable() { return ErrTxNotWritable } // Move cursor to correct position. c := b.Cursor() k, _, flags := c.seek(key) // Return an error if bucket doesn't exist or is not a bucket. if !bytes.Equal(key, k) { return ErrBucketNotFound } else if (flags & bucketLeafFlag) == 0 { return ErrIncompatibleValue } // Recursively delete all child buckets. child := b.Bucket(key) err := child.ForEachBucket(func(k []byte) error { if err := child.DeleteBucket(k); err != nil { return fmt.Errorf("delete bucket: %s", err) } return nil }) if err != nil { return err } // Remove cached copy. delete(b.buckets, string(key)) // Release all bucket pages to freelist. child.nodes = nil child.rootNode = nil child.free() // Delete the node if we have a matching key. c.node().del(key) return nil } // Get retrieves the value for a key in the bucket. // Returns a nil value if the key does not exist or if the key is a nested bucket. // The returned value is only valid for the life of the transaction. func (b *Bucket) Get(key []byte) []byte { k, v, flags := b.Cursor().seek(key) // Return nil if this is a bucket. if (flags & bucketLeafFlag) != 0 { return nil } // If our target node isn't the same key as what's passed in then return nil. if !bytes.Equal(key, k) { return nil } return v } // Put sets the value for a key in the bucket. // If the key exist then its previous value will be overwritten. // Supplied value must remain valid for the life of the transaction. // Returns an error if the bucket was created from a read-only transaction, if the key is blank, if the key is too large, or if the value is too large. func (b *Bucket) Put(key []byte, value []byte) error { if b.tx.db == nil { return ErrTxClosed } else if !b.Writable() { return ErrTxNotWritable } else if len(key) == 0 { return ErrKeyRequired } else if len(key) > MaxKeySize { return ErrKeyTooLarge } else if int64(len(value)) > MaxValueSize { return ErrValueTooLarge } // Insert into node. // Tip: Use a new variable `newKey` instead of reusing the existing `key` to prevent // it from being marked as leaking, and accordingly cannot be allocated on stack. newKey := cloneBytes(key) // Move cursor to correct position. c := b.Cursor() k, _, flags := c.seek(newKey) // Return an error if there is an existing key with a bucket value. if bytes.Equal(newKey, k) && (flags&bucketLeafFlag) != 0 { return ErrIncompatibleValue } // gofail: var beforeBucketPut struct{} c.node().put(newKey, newKey, value, 0, 0) return nil } // Delete removes a key from the bucket. // If the key does not exist then nothing is done and a nil error is returned. // Returns an error if the bucket was created from a read-only transaction. func (b *Bucket) Delete(key []byte) error { if b.tx.db == nil { return ErrTxClosed } else if !b.Writable() { return ErrTxNotWritable } // Move cursor to correct position. c := b.Cursor() k, _, flags := c.seek(key) // Return nil if the key doesn't exist. if !bytes.Equal(key, k) { return nil } // Return an error if there is already existing bucket value. if (flags & bucketLeafFlag) != 0 { return ErrIncompatibleValue } // Delete the node if we have a matching key. c.node().del(key) return nil } // Sequence returns the current integer for the bucket without incrementing it. func (b *Bucket) Sequence() uint64 { return b.bucket.sequence } // SetSequence updates the sequence number for the bucket. func (b *Bucket) SetSequence(v uint64) error { if b.tx.db == nil { return ErrTxClosed } else if !b.Writable() { return ErrTxNotWritable } // Materialize the root node if it hasn't been already so that the // bucket will be saved during commit. if b.rootNode == nil { _ = b.node(b.root, nil) } // Set the sequence. b.bucket.sequence = v return nil } // NextSequence returns an autoincrementing integer for the bucket. func (b *Bucket) NextSequence() (uint64, error) { if b.tx.db == nil { return 0, ErrTxClosed } else if !b.Writable() { return 0, ErrTxNotWritable } // Materialize the root node if it hasn't been already so that the // bucket will be saved during commit. if b.rootNode == nil { _ = b.node(b.root, nil) } // Increment and return the sequence. b.bucket.sequence++ return b.bucket.sequence, nil } // ForEach executes a function for each key/value pair in a bucket. // Because ForEach uses a Cursor, the iteration over keys is in lexicographical order. // If the provided function returns an error then the iteration is stopped and // the error is returned to the caller. The provided function must not modify // the bucket; this will result in undefined behavior. func (b *Bucket) ForEach(fn func(k, v []byte) error) error { if b.tx.db == nil { return ErrTxClosed } c := b.Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { if err := fn(k, v); err != nil { return err } } return nil } func (b *Bucket) ForEachBucket(fn func(k []byte) error) error { if b.tx.db == nil { return ErrTxClosed } c := b.Cursor() for k, _, flags := c.first(); k != nil; k, _, flags = c.next() { if flags&bucketLeafFlag != 0 { if err := fn(k); err != nil { return err } } } return nil } // Stats returns stats on a bucket. func (b *Bucket) Stats() BucketStats { var s, subStats BucketStats pageSize := b.tx.db.pageSize s.BucketN += 1 if b.root == 0 { s.InlineBucketN += 1 } b.forEachPage(func(p *page, depth int, pgstack []pgid) { if (p.flags & leafPageFlag) != 0 { s.KeyN += int(p.count) // used totals the used bytes for the page used := pageHeaderSize if p.count != 0 { // If page has any elements, add all element headers. used += leafPageElementSize * uintptr(p.count-1) // Add all element key, value sizes. // The computation takes advantage of the fact that the position // of the last element's key/value equals to the total of the sizes // of all previous elements' keys and values. // It also includes the last element's header. lastElement := p.leafPageElement(p.count - 1) used += uintptr(lastElement.pos + lastElement.ksize + lastElement.vsize) } if b.root == 0 { // For inlined bucket just update the inline stats s.InlineBucketInuse += int(used) } else { // For non-inlined bucket update all the leaf stats s.LeafPageN++ s.LeafInuse += int(used) s.LeafOverflowN += int(p.overflow) // Collect stats from sub-buckets. // Do that by iterating over all element headers // looking for the ones with the bucketLeafFlag. for i := uint16(0); i < p.count; i++ { e := p.leafPageElement(i) if (e.flags & bucketLeafFlag) != 0 { // For any bucket element, open the element value // and recursively call Stats on the contained bucket. subStats.Add(b.openBucket(e.value()).Stats()) } } } } else if (p.flags & branchPageFlag) != 0 { s.BranchPageN++ lastElement := p.branchPageElement(p.count - 1) // used totals the used bytes for the page // Add header and all element headers. used := pageHeaderSize + (branchPageElementSize * uintptr(p.count-1)) // Add size of all keys and values. // Again, use the fact that last element's position equals to // the total of key, value sizes of all previous elements. used += uintptr(lastElement.pos + lastElement.ksize) s.BranchInuse += int(used) s.BranchOverflowN += int(p.overflow) } // Keep track of maximum page depth. if depth+1 > s.Depth { s.Depth = depth + 1 } }) // Alloc stats can be computed from page counts and pageSize. s.BranchAlloc = (s.BranchPageN + s.BranchOverflowN) * pageSize s.LeafAlloc = (s.LeafPageN + s.LeafOverflowN) * pageSize // Add the max depth of sub-buckets to get total nested depth. s.Depth += subStats.Depth // Add the stats for all sub-buckets s.Add(subStats) return s } // forEachPage iterates over every page in a bucket, including inline pages. func (b *Bucket) forEachPage(fn func(*page, int, []pgid)) { // If we have an inline page then just use that. if b.page != nil { fn(b.page, 0, []pgid{b.root}) return } // Otherwise traverse the page hierarchy. b.tx.forEachPage(b.root, fn) } // forEachPageNode iterates over every page (or node) in a bucket. // This also includes inline pages. func (b *Bucket) forEachPageNode(fn func(*page, *node, int)) { // If we have an inline page or root node then just use that. if b.page != nil { fn(b.page, nil, 0) return } b._forEachPageNode(b.root, 0, fn) } func (b *Bucket) _forEachPageNode(pgId pgid, depth int, fn func(*page, *node, int)) { var p, n = b.pageNode(pgId) // Execute function. fn(p, n, depth) // Recursively loop over children. if p != nil { if (p.flags & branchPageFlag) != 0 { for i := 0; i < int(p.count); i++ { elem := p.branchPageElement(uint16(i)) b._forEachPageNode(elem.pgid, depth+1, fn) } } } else { if !n.isLeaf { for _, inode := range n.inodes { b._forEachPageNode(inode.pgid, depth+1, fn) } } } } // spill writes all the nodes for this bucket to dirty pages. func (b *Bucket) spill() error { // Spill all child buckets first. for name, child := range b.buckets { // If the child bucket is small enough and it has no child buckets then // write it inline into the parent bucket's page. Otherwise spill it // like a normal bucket and make the parent value a pointer to the page. var value []byte if child.inlineable() { child.free() value = child.write() } else { if err := child.spill(); err != nil { return err } // Update the child bucket header in this bucket. value = make([]byte, unsafe.Sizeof(bucket{})) var bucket = (*bucket)(unsafe.Pointer(&value[0])) *bucket = *child.bucket } // Skip writing the bucket if there are no materialized nodes. if child.rootNode == nil { continue } // Update parent node. var c = b.Cursor() k, _, flags := c.seek([]byte(name)) if !bytes.Equal([]byte(name), k) { panic(fmt.Sprintf("misplaced bucket header: %x -> %x", []byte(name), k)) } if flags&bucketLeafFlag == 0 { panic(fmt.Sprintf("unexpected bucket header flag: %x", flags)) } c.node().put([]byte(name), []byte(name), value, 0, bucketLeafFlag) } // Ignore if there's not a materialized root node. if b.rootNode == nil { return nil } // Spill nodes. if err := b.rootNode.spill(); err != nil { return err } b.rootNode = b.rootNode.root() // Update the root node for this bucket. if b.rootNode.pgid >= b.tx.meta.pgid { panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", b.rootNode.pgid, b.tx.meta.pgid)) } b.root = b.rootNode.pgid return nil } // inlineable returns true if a bucket is small enough to be written inline // and if it contains no subbuckets. Otherwise returns false. func (b *Bucket) inlineable() bool { var n = b.rootNode // Bucket must only contain a single leaf node. if n == nil || !n.isLeaf { return false } // Bucket is not inlineable if it contains subbuckets or if it goes beyond // our threshold for inline bucket size. var size = pageHeaderSize for _, inode := range n.inodes { size += leafPageElementSize + uintptr(len(inode.key)) + uintptr(len(inode.value)) if inode.flags&bucketLeafFlag != 0 { return false } else if size > b.maxInlineBucketSize() { return false } } return true } // Returns the maximum total size of a bucket to make it a candidate for inlining. func (b *Bucket) maxInlineBucketSize() uintptr { return uintptr(b.tx.db.pageSize / 4) } // write allocates and writes a bucket to a byte slice. func (b *Bucket) write() []byte { // Allocate the appropriate size. var n = b.rootNode var value = make([]byte, bucketHeaderSize+n.size()) // Write a bucket header. var bucket = (*bucket)(unsafe.Pointer(&value[0])) *bucket = *b.bucket // Convert byte slice to a fake page and write the root node. var p = (*page)(unsafe.Pointer(&value[bucketHeaderSize])) n.write(p) return value } // rebalance attempts to balance all nodes. func (b *Bucket) rebalance() { for _, n := range b.nodes { n.rebalance() } for _, child := range b.buckets { child.rebalance() } } // node creates a node from a page and associates it with a given parent. func (b *Bucket) node(pgId pgid, parent *node) *node { _assert(b.nodes != nil, "nodes map expected") // Retrieve node if it's already been created. if n := b.nodes[pgId]; n != nil { return n } // Otherwise create a node and cache it. n := &node{bucket: b, parent: parent} if parent == nil { b.rootNode = n } else { parent.children = append(parent.children, n) } // Use the inline page if this is an inline bucket. var p = b.page if p == nil { p = b.tx.page(pgId) } // Read the page into the node and cache it. n.read(p) b.nodes[pgId] = n // Update statistics. b.tx.stats.IncNodeCount(1) return n } // free recursively frees all pages in the bucket. func (b *Bucket) free() { if b.root == 0 { return } var tx = b.tx b.forEachPageNode(func(p *page, n *node, _ int) { if p != nil { tx.db.freelist.free(tx.meta.txid, p) } else { n.free() } }) b.root = 0 } // dereference removes all references to the old mmap. func (b *Bucket) dereference() { if b.rootNode != nil { b.rootNode.root().dereference() } for _, child := range b.buckets { child.dereference() } } // pageNode returns the in-memory node, if it exists. // Otherwise returns the underlying page. func (b *Bucket) pageNode(id pgid) (*page, *node) { // Inline buckets have a fake page embedded in their value so treat them // differently. We'll return the rootNode (if available) or the fake page. if b.root == 0 { if id != 0 { panic(fmt.Sprintf("inline bucket non-zero page access(2): %d != 0", id)) } if b.rootNode != nil { return nil, b.rootNode } return b.page, nil } // Check the node cache for non-inline buckets. if b.nodes != nil { if n := b.nodes[id]; n != nil { return nil, n } } // Finally lookup the page from the transaction if no node is materialized. return b.tx.page(id), nil } // BucketStats records statistics about resources used by a bucket. type BucketStats struct { // Page count statistics. BranchPageN int // number of logical branch pages BranchOverflowN int // number of physical branch overflow pages LeafPageN int // number of logical leaf pages LeafOverflowN int // number of physical leaf overflow pages // Tree statistics. KeyN int // number of keys/value pairs Depth int // number of levels in B+tree // Page size utilization. BranchAlloc int // bytes allocated for physical branch pages BranchInuse int // bytes actually used for branch data LeafAlloc int // bytes allocated for physical leaf pages LeafInuse int // bytes actually used for leaf data // Bucket statistics BucketN int // total number of buckets including the top bucket InlineBucketN int // total number on inlined buckets InlineBucketInuse int // bytes used for inlined buckets (also accounted for in LeafInuse) } func (s *BucketStats) Add(other BucketStats) { s.BranchPageN += other.BranchPageN s.BranchOverflowN += other.BranchOverflowN s.LeafPageN += other.LeafPageN s.LeafOverflowN += other.LeafOverflowN s.KeyN += other.KeyN if s.Depth < other.Depth { s.Depth = other.Depth } s.BranchAlloc += other.BranchAlloc s.BranchInuse += other.BranchInuse s.LeafAlloc += other.LeafAlloc s.LeafInuse += other.LeafInuse s.BucketN += other.BucketN s.InlineBucketN += other.InlineBucketN s.InlineBucketInuse += other.InlineBucketInuse } // cloneBytes returns a copy of a given slice. func cloneBytes(v []byte) []byte { var clone = make([]byte, len(v)) copy(clone, v) return clone } bbolt-1.3.10/bucket_test.go000066400000000000000000001415331461507540700155610ustar00rootroot00000000000000package bbolt_test import ( "bytes" "encoding/binary" "errors" "fmt" "log" "math/rand" "os" "strconv" "strings" "testing" "testing/quick" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" ) // Ensure that a bucket that gets a non-existent key returns nil. func TestBucket_Get_NonExistent(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if v := b.Get([]byte("foo")); v != nil { t.Fatal("expected nil value") } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can read a value that is not flushed yet. func TestBucket_Get_FromNode(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if v := b.Get([]byte("foo")); !bytes.Equal(v, []byte("bar")) { t.Fatalf("unexpected value: %v", v) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket retrieved via Get() returns a nil. func TestBucket_Get_IncompatibleValue(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if _, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo")); err != nil { t.Fatal(err) } if tx.Bucket([]byte("widgets")).Get([]byte("foo")) != nil { t.Fatal("expected nil value") } return nil }); err != nil { t.Fatal(err) } } // Ensure that a slice returned from a bucket has a capacity equal to its length. // This also allows slices to be appended to since it will require a realloc by Go. // // https://github.com/boltdb/bolt/issues/544 func TestBucket_Get_Capacity(t *testing.T) { db := btesting.MustCreateDB(t) // Write key to a bucket. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("bucket")) if err != nil { return err } return b.Put([]byte("key"), []byte("val")) }); err != nil { t.Fatal(err) } // Retrieve value and attempt to append to it. if err := db.Update(func(tx *bolt.Tx) error { k, v := tx.Bucket([]byte("bucket")).Cursor().First() // Verify capacity. if len(k) != cap(k) { t.Fatalf("unexpected key slice capacity: %d", cap(k)) } else if len(v) != cap(v) { t.Fatalf("unexpected value slice capacity: %d", cap(v)) } // Ensure slice can be appended to without a segfault. k = append(k, []byte("123")...) v = append(v, []byte("123")...) _, _ = k, v // to pass ineffassign return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can write a key/value. func TestBucket_Put(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } v := tx.Bucket([]byte("widgets")).Get([]byte("foo")) if !bytes.Equal([]byte("bar"), v) { t.Fatalf("unexpected value: %v", v) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can rewrite a key in the same transaction. func TestBucket_Put_Repeat(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("baz")); err != nil { t.Fatal(err) } value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) if !bytes.Equal([]byte("baz"), value) { t.Fatalf("unexpected value: %v", value) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can write a bunch of large values. func TestBucket_Put_Large(t *testing.T) { db := btesting.MustCreateDB(t) count, factor := 100, 200 if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } for i := 1; i < count; i++ { if err := b.Put([]byte(strings.Repeat("0", i*factor)), []byte(strings.Repeat("X", (count-i)*factor))); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for i := 1; i < count; i++ { value := b.Get([]byte(strings.Repeat("0", i*factor))) if !bytes.Equal(value, []byte(strings.Repeat("X", (count-i)*factor))) { t.Fatalf("unexpected value: %v", value) } } return nil }); err != nil { t.Fatal(err) } } // Ensure that a database can perform multiple large appends safely. func TestDB_Put_VeryLarge(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } n, batchN := 400000, 200000 ksize, vsize := 8, 500 db := btesting.MustCreateDB(t) for i := 0; i < n; i += batchN { if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("widgets")) if err != nil { t.Fatal(err) } for j := 0; j < batchN; j++ { k, v := make([]byte, ksize), make([]byte, vsize) binary.BigEndian.PutUint32(k, uint32(i+j)) if err := b.Put(k, v); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } } } // Ensure that a setting a value on a key with a bucket value returns an error. func TestBucket_Put_IncompatibleValue(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b0, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if _, err := tx.Bucket([]byte("widgets")).CreateBucket([]byte("foo")); err != nil { t.Fatal(err) } if err := b0.Put([]byte("foo"), []byte("bar")); err != bolt.ErrIncompatibleValue { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a setting a value while the transaction is closed returns an error. func TestBucket_Put_Closed(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) if err != nil { t.Fatal(err) } b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := tx.Rollback(); err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != bolt.ErrTxClosed { t.Fatalf("unexpected error: %s", err) } } // Ensure that setting a value on a read-only bucket returns an error. func TestBucket_Put_ReadOnly(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) if err := b.Put([]byte("foo"), []byte("bar")); err != bolt.ErrTxNotWritable { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can delete an existing key. func TestBucket_Delete(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if err := b.Delete([]byte("foo")); err != nil { t.Fatal(err) } if v := b.Get([]byte("foo")); v != nil { t.Fatalf("unexpected value: %v", v) } return nil }); err != nil { t.Fatal(err) } } // Ensure that deleting a large set of keys will work correctly. func TestBucket_Delete_Large(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } for i := 0; i < 100; i++ { if err := b.Put([]byte(strconv.Itoa(i)), []byte(strings.Repeat("*", 1024))); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for i := 0; i < 100; i++ { if err := b.Delete([]byte(strconv.Itoa(i))); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for i := 0; i < 100; i++ { if v := b.Get([]byte(strconv.Itoa(i))); v != nil { t.Fatalf("unexpected value: %v, i=%d", v, i) } } return nil }); err != nil { t.Fatal(err) } } // Deleting a very large list of keys will cause the freelist to use overflow. func TestBucket_Delete_FreelistOverflow(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } db := btesting.MustCreateDB(t) k := make([]byte, 16) // The bigger the pages - the more values we need to write. for i := uint64(0); i < 2*uint64(db.Info().PageSize); i++ { if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("0")) if err != nil { t.Fatalf("bucket error: %s", err) } for j := uint64(0); j < 1000; j++ { binary.BigEndian.PutUint64(k[:8], i) binary.BigEndian.PutUint64(k[8:], j) if err := b.Put(k, nil); err != nil { t.Fatalf("put error: %s", err) } } return nil }); err != nil { t.Fatal(err) } } // Delete all of them in one large transaction if err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("0")) c := b.Cursor() for k, _ := c.First(); k != nil; k, _ = c.Next() { if err := c.Delete(); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } // Check more than an overflow's worth of pages are freed. stats := db.Stats() freePages := stats.FreePageN + stats.PendingPageN if freePages <= 0xFFFF { t.Fatalf("expected more than 0xFFFF free pages, got %v", freePages) } // Free page count should be preserved on reopen. db.MustClose() db.MustReopen() if reopenFreePages := db.Stats().FreePageN; freePages != reopenFreePages { t.Fatalf("expected %d free pages, got %+v", freePages, db.Stats()) } } // Ensure that deleting of non-existing key is a no-op. func TestBucket_Delete_NonExisting(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if _, err = b.CreateBucket([]byte("nested")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) if err := b.Delete([]byte("foo")); err != nil { t.Fatal(err) } if b.Bucket([]byte("nested")) == nil { t.Fatal("nested bucket has been deleted") } return nil }); err != nil { t.Fatal(err) } } // Ensure that accessing and updating nested buckets is ok across transactions. func TestBucket_Nested(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { // Create a widgets bucket. b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } // Create a widgets/foo bucket. _, err = b.CreateBucket([]byte("foo")) if err != nil { t.Fatal(err) } // Create a widgets/bar key. if err := b.Put([]byte("bar"), []byte("0000")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } db.MustCheck() // Update widgets/bar. if err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) if err := b.Put([]byte("bar"), []byte("xxxx")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } db.MustCheck() // Cause a split. if err := db.Update(func(tx *bolt.Tx) error { var b = tx.Bucket([]byte("widgets")) for i := 0; i < 10000; i++ { if err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } db.MustCheck() // Insert into widgets/foo/baz. if err := db.Update(func(tx *bolt.Tx) error { var b = tx.Bucket([]byte("widgets")) if err := b.Bucket([]byte("foo")).Put([]byte("baz"), []byte("yyyy")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } db.MustCheck() // Verify. if err := db.View(func(tx *bolt.Tx) error { var b = tx.Bucket([]byte("widgets")) if v := b.Bucket([]byte("foo")).Get([]byte("baz")); !bytes.Equal(v, []byte("yyyy")) { t.Fatalf("unexpected value: %v", v) } if v := b.Get([]byte("bar")); !bytes.Equal(v, []byte("xxxx")) { t.Fatalf("unexpected value: %v", v) } for i := 0; i < 10000; i++ { if v := b.Get([]byte(strconv.Itoa(i))); !bytes.Equal(v, []byte(strconv.Itoa(i))) { t.Fatalf("unexpected value: %v", v) } } return nil }); err != nil { t.Fatal(err) } } // Ensure that deleting a bucket using Delete() returns an error. func TestBucket_Delete_Bucket(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if _, err := b.CreateBucket([]byte("foo")); err != nil { t.Fatal(err) } if err := b.Delete([]byte("foo")); err != bolt.ErrIncompatibleValue { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that deleting a key on a read-only bucket returns an error. func TestBucket_Delete_ReadOnly(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { if err := tx.Bucket([]byte("widgets")).Delete([]byte("foo")); err != bolt.ErrTxNotWritable { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a deleting value while the transaction is closed returns an error. func TestBucket_Delete_Closed(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) if err != nil { t.Fatal(err) } b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := tx.Rollback(); err != nil { t.Fatal(err) } if err := b.Delete([]byte("foo")); err != bolt.ErrTxClosed { t.Fatalf("unexpected error: %s", err) } } // Ensure that deleting a bucket causes nested buckets to be deleted. func TestBucket_DeleteBucket_Nested(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { widgets, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } foo, err := widgets.CreateBucket([]byte("foo")) if err != nil { t.Fatal(err) } bar, err := foo.CreateBucket([]byte("bar")) if err != nil { t.Fatal(err) } if err := bar.Put([]byte("baz"), []byte("bat")); err != nil { t.Fatal(err) } if err := tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that deleting a bucket causes nested buckets to be deleted after they have been committed. func TestBucket_DeleteBucket_Nested2(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { widgets, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } foo, err := widgets.CreateBucket([]byte("foo")) if err != nil { t.Fatal(err) } bar, err := foo.CreateBucket([]byte("bar")) if err != nil { t.Fatal(err) } if err := bar.Put([]byte("baz"), []byte("bat")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { widgets := tx.Bucket([]byte("widgets")) if widgets == nil { t.Fatal("expected widgets bucket") } foo := widgets.Bucket([]byte("foo")) if foo == nil { t.Fatal("expected foo bucket") } bar := foo.Bucket([]byte("bar")) if bar == nil { t.Fatal("expected bar bucket") } if v := bar.Get([]byte("baz")); !bytes.Equal(v, []byte("bat")) { t.Fatalf("unexpected value: %v", v) } if err := tx.DeleteBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { if tx.Bucket([]byte("widgets")) != nil { t.Fatal("expected bucket to be deleted") } return nil }); err != nil { t.Fatal(err) } } // Ensure that deleting a child bucket with multiple pages causes all pages to get collected. // NOTE: Consistency check in bolt_test.DB.Close() will panic if pages not freed properly. func TestBucket_DeleteBucket_Large(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { widgets, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } foo, err := widgets.CreateBucket([]byte("foo")) if err != nil { t.Fatal(err) } for i := 0; i < 1000; i++ { if err := foo.Put([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%0100d", i))); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { if err := tx.DeleteBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a simple value retrieved via Bucket() returns a nil. func TestBucket_Bucket_IncompatibleValue(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { widgets, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if b := tx.Bucket([]byte("widgets")).Bucket([]byte("foo")); b != nil { t.Fatal("expected nil bucket") } return nil }); err != nil { t.Fatal(err) } } // Ensure that creating a bucket on an existing non-bucket key returns an error. func TestBucket_CreateBucket_IncompatibleValue(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { widgets, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if _, err := widgets.CreateBucket([]byte("foo")); err != bolt.ErrIncompatibleValue { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that deleting a bucket on an existing non-bucket key returns an error. func TestBucket_DeleteBucket_IncompatibleValue(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { widgets, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := widgets.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if err := tx.Bucket([]byte("widgets")).DeleteBucket([]byte("foo")); err != bolt.ErrIncompatibleValue { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure bucket can set and update its sequence number. func TestBucket_Sequence(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { bkt, err := tx.CreateBucket([]byte("0")) if err != nil { t.Fatal(err) } // Retrieve sequence. if v := bkt.Sequence(); v != 0 { t.Fatalf("unexpected sequence: %d", v) } // Update sequence. if err := bkt.SetSequence(1000); err != nil { t.Fatal(err) } // Read sequence again. if v := bkt.Sequence(); v != 1000 { t.Fatalf("unexpected sequence: %d", v) } return nil }); err != nil { t.Fatal(err) } // Verify sequence in separate transaction. if err := db.View(func(tx *bolt.Tx) error { if v := tx.Bucket([]byte("0")).Sequence(); v != 1000 { t.Fatalf("unexpected sequence: %d", v) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can return an autoincrementing sequence. func TestBucket_NextSequence(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { widgets, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } woojits, err := tx.CreateBucket([]byte("woojits")) if err != nil { t.Fatal(err) } // Make sure sequence increments. if seq, err := widgets.NextSequence(); err != nil { t.Fatal(err) } else if seq != 1 { t.Fatalf("unexpecte sequence: %d", seq) } if seq, err := widgets.NextSequence(); err != nil { t.Fatal(err) } else if seq != 2 { t.Fatalf("unexpected sequence: %d", seq) } // Buckets should be separate. if seq, err := woojits.NextSequence(); err != nil { t.Fatal(err) } else if seq != 1 { t.Fatalf("unexpected sequence: %d", 1) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket will persist an autoincrementing sequence even if its // the only thing updated on the bucket. // https://github.com/boltdb/bolt/issues/296 func TestBucket_NextSequence_Persist(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.Bucket([]byte("widgets")).NextSequence(); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { seq, err := tx.Bucket([]byte("widgets")).NextSequence() if err != nil { t.Fatalf("unexpected error: %s", err) } else if seq != 2 { t.Fatalf("unexpected sequence: %d", seq) } return nil }); err != nil { t.Fatal(err) } } // Ensure that retrieving the next sequence on a read-only bucket returns an error. func TestBucket_NextSequence_ReadOnly(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { _, err := tx.Bucket([]byte("widgets")).NextSequence() if err != bolt.ErrTxNotWritable { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that retrieving the next sequence for a bucket on a closed database return an error. func TestBucket_NextSequence_Closed(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) if err != nil { t.Fatal(err) } b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := tx.Rollback(); err != nil { t.Fatal(err) } if _, err := b.NextSequence(); err != bolt.ErrTxClosed { t.Fatal(err) } } // Ensure a user can loop over all key/value pairs in a bucket. func TestBucket_ForEach(t *testing.T) { db := btesting.MustCreateDB(t) type kv struct { k []byte v []byte } expectedItems := []kv{ {k: []byte("bar"), v: []byte("0002")}, {k: []byte("baz"), v: []byte("0001")}, {k: []byte("csubbucket"), v: nil}, {k: []byte("foo"), v: []byte("0000")}, } verifyReads := func(b *bolt.Bucket) { var items []kv err := b.ForEach(func(k, v []byte) error { items = append(items, kv{k: k, v: v}) return nil }) assert.NoErrorf(t, err, "b.ForEach failed") assert.Equal(t, expectedItems, items, "what we iterated (ForEach) is not what we put") } err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) require.NoError(t, err, "bucket creation failed") require.NoErrorf(t, b.Put([]byte("foo"), []byte("0000")), "put 'foo' failed") require.NoErrorf(t, b.Put([]byte("baz"), []byte("0001")), "put 'baz' failed") require.NoErrorf(t, b.Put([]byte("bar"), []byte("0002")), "put 'bar' failed") _, err = b.CreateBucket([]byte("csubbucket")) require.NoErrorf(t, err, "creation of subbucket failed") verifyReads(b) return nil }) require.NoErrorf(t, err, "db.Update failed") err = db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) require.NotNil(t, b, "bucket opening failed") verifyReads(b) return nil }) assert.NoErrorf(t, err, "db.View failed") } func TestBucket_ForEachBucket(t *testing.T) { db := btesting.MustCreateDB(t) expectedItems := [][]byte{ []byte("csubbucket"), []byte("zsubbucket"), } verifyReads := func(b *bolt.Bucket) { var items [][]byte err := b.ForEachBucket(func(k []byte) error { items = append(items, k) return nil }) assert.NoErrorf(t, err, "b.ForEach failed") assert.Equal(t, expectedItems, items, "what we iterated (ForEach) is not what we put") } err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) require.NoError(t, err, "bucket creation failed") require.NoErrorf(t, b.Put([]byte("foo"), []byte("0000")), "put 'foo' failed") _, err = b.CreateBucket([]byte("zsubbucket")) require.NoErrorf(t, err, "creation of subbucket failed") require.NoErrorf(t, b.Put([]byte("baz"), []byte("0001")), "put 'baz' failed") require.NoErrorf(t, b.Put([]byte("bar"), []byte("0002")), "put 'bar' failed") _, err = b.CreateBucket([]byte("csubbucket")) require.NoErrorf(t, err, "creation of subbucket failed") verifyReads(b) return nil }) assert.NoErrorf(t, err, "db.Update failed") err = db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) require.NotNil(t, b, "bucket opening failed") verifyReads(b) return nil }) assert.NoErrorf(t, err, "db.View failed") } func TestBucket_ForEachBucket_NoBuckets(t *testing.T) { db := btesting.MustCreateDB(t) verifyReads := func(b *bolt.Bucket) { var items [][]byte err := b.ForEachBucket(func(k []byte) error { items = append(items, k) return nil }) assert.NoErrorf(t, err, "b.ForEach failed") assert.Emptyf(t, items, "what we iterated (ForEach) is not what we put") } err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) require.NoError(t, err, "bucket creation failed") require.NoErrorf(t, b.Put([]byte("foo"), []byte("0000")), "put 'foo' failed") require.NoErrorf(t, err, "creation of subbucket failed") require.NoErrorf(t, b.Put([]byte("baz"), []byte("0001")), "put 'baz' failed") require.NoErrorf(t, err, "creation of subbucket failed") verifyReads(b) return nil }) require.NoErrorf(t, err, "db.Update failed") err = db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) require.NotNil(t, b, "bucket opening failed") verifyReads(b) return nil }) assert.NoErrorf(t, err, "db.View failed") } // Ensure a database can stop iteration early. func TestBucket_ForEach_ShortCircuit(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("bar"), []byte("0000")); err != nil { t.Fatal(err) } if err := b.Put([]byte("baz"), []byte("0000")); err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("0000")); err != nil { t.Fatal(err) } var index int if err := tx.Bucket([]byte("widgets")).ForEach(func(k, v []byte) error { index++ if bytes.Equal(k, []byte("baz")) { return errors.New("marker") } return nil }); err == nil || err.Error() != "marker" { t.Fatalf("unexpected error: %s", err) } if index != 2 { t.Fatalf("unexpected index: %d", index) } return nil }); err != nil { t.Fatal(err) } } // Ensure that looping over a bucket on a closed database returns an error. func TestBucket_ForEach_Closed(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) if err != nil { t.Fatal(err) } b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := tx.Rollback(); err != nil { t.Fatal(err) } if err := b.ForEach(func(k, v []byte) error { return nil }); err != bolt.ErrTxClosed { t.Fatalf("unexpected error: %s", err) } } // Ensure that an error is returned when inserting with an empty key. func TestBucket_Put_EmptyKey(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte(""), []byte("bar")); err != bolt.ErrKeyRequired { t.Fatalf("unexpected error: %s", err) } if err := b.Put(nil, []byte("bar")); err != bolt.ErrKeyRequired { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that an error is returned when inserting with a key that's too large. func TestBucket_Put_KeyTooLarge(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put(make([]byte, 32769), []byte("bar")); err != bolt.ErrKeyTooLarge { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that an error is returned when inserting a value that's too large. func TestBucket_Put_ValueTooLarge(t *testing.T) { // Skip this test on DroneCI because the machine is resource constrained. if os.Getenv("DRONE") == "true" { t.Skip("not enough RAM for test") } db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), make([]byte, bolt.MaxValueSize+1)); err != bolt.ErrValueTooLarge { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure a bucket can calculate stats. func TestBucket_Stats(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode") } db := btesting.MustCreateDB(t) // Add bucket with fewer keys but one big value. bigKey := []byte("really-big-value") for i := 0; i < 500; i++ { if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("woojits")) if err != nil { t.Fatal(err) } if err := b.Put([]byte(fmt.Sprintf("%03d", i)), []byte(strconv.Itoa(i))); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } } longKeyLength := 10*db.Info().PageSize + 17 if err := db.Update(func(tx *bolt.Tx) error { if err := tx.Bucket([]byte("woojits")).Put(bigKey, []byte(strings.Repeat("*", longKeyLength))); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } db.MustCheck() pageSize2stats := map[int]bolt.BucketStats{ 4096: { BranchPageN: 1, BranchOverflowN: 0, LeafPageN: 7, LeafOverflowN: 10, KeyN: 501, Depth: 2, BranchAlloc: 4096, BranchInuse: 149, LeafAlloc: 69632, LeafInuse: 0 + 7*16 + // leaf page header (x LeafPageN) 501*16 + // leaf elements 500*3 + len(bigKey) + // leaf keys 1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ... BucketN: 1, InlineBucketN: 0, InlineBucketInuse: 0}, 16384: { BranchPageN: 1, BranchOverflowN: 0, LeafPageN: 3, LeafOverflowN: 10, KeyN: 501, Depth: 2, BranchAlloc: 16384, BranchInuse: 73, LeafAlloc: 212992, LeafInuse: 0 + 3*16 + // leaf page header (x LeafPageN) 501*16 + // leaf elements 500*3 + len(bigKey) + // leaf keys 1*10 + 2*90 + 3*400 + longKeyLength, // leaf values: 10 * 1digit, 90*2digits, ... BucketN: 1, InlineBucketN: 0, InlineBucketInuse: 0}, } if err := db.View(func(tx *bolt.Tx) error { stats := tx.Bucket([]byte("woojits")).Stats() t.Logf("Stats: %#v", stats) if expected, ok := pageSize2stats[db.Info().PageSize]; ok { assert.EqualValues(t, expected, stats, "stats differs from expectations") } else { t.Skipf("No expectations for page size: %d", db.Info().PageSize) } return nil }); err != nil { t.Fatal(err) } } // Ensure a bucket with random insertion utilizes fill percentage correctly. func TestBucket_Stats_RandomFill(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } else if os.Getpagesize() != 4096 { t.Skip("invalid page size for test") } db := btesting.MustCreateDB(t) // Add a set of values in random order. It will be the same random // order so we can maintain consistency between test runs. var count int rand := rand.New(rand.NewSource(42)) for _, i := range rand.Perm(1000) { if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("woojits")) if err != nil { t.Fatal(err) } b.FillPercent = 0.9 for _, j := range rand.Perm(100) { index := (j * 10000) + i if err := b.Put([]byte(fmt.Sprintf("%d000000000000000", index)), []byte("0000000000")); err != nil { t.Fatal(err) } count++ } return nil }); err != nil { t.Fatal(err) } } db.MustCheck() if err := db.View(func(tx *bolt.Tx) error { stats := tx.Bucket([]byte("woojits")).Stats() if stats.KeyN != 100000 { t.Fatalf("unexpected KeyN: %d", stats.KeyN) } if stats.BranchPageN != 98 { t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN) } else if stats.BranchOverflowN != 0 { t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN) } else if stats.BranchInuse != 130984 { t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse) } else if stats.BranchAlloc != 401408 { t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc) } if stats.LeafPageN != 3412 { t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN) } else if stats.LeafOverflowN != 0 { t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN) } else if stats.LeafInuse != 4742482 { t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse) } else if stats.LeafAlloc != 13975552 { t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc) } return nil }); err != nil { t.Fatal(err) } } // Ensure a bucket can calculate stats. func TestBucket_Stats_Small(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { // Add a bucket that fits on a single root leaf. b, err := tx.CreateBucket([]byte("whozawhats")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } db.MustCheck() if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("whozawhats")) stats := b.Stats() if stats.BranchPageN != 0 { t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN) } else if stats.BranchOverflowN != 0 { t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN) } else if stats.LeafPageN != 0 { t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN) } else if stats.LeafOverflowN != 0 { t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN) } else if stats.KeyN != 1 { t.Fatalf("unexpected KeyN: %d", stats.KeyN) } else if stats.Depth != 1 { t.Fatalf("unexpected Depth: %d", stats.Depth) } else if stats.BranchInuse != 0 { t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse) } else if stats.LeafInuse != 0 { t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse) } if db.Info().PageSize == 4096 { if stats.BranchAlloc != 0 { t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc) } else if stats.LeafAlloc != 0 { t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc) } } if stats.BucketN != 1 { t.Fatalf("unexpected BucketN: %d", stats.BucketN) } else if stats.InlineBucketN != 1 { t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN) } else if stats.InlineBucketInuse != 16+16+6 { t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse) } return nil }); err != nil { t.Fatal(err) } } func TestBucket_Stats_EmptyBucket(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { // Add a bucket that fits on a single root leaf. if _, err := tx.CreateBucket([]byte("whozawhats")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } db.MustCheck() if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("whozawhats")) stats := b.Stats() if stats.BranchPageN != 0 { t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN) } else if stats.BranchOverflowN != 0 { t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN) } else if stats.LeafPageN != 0 { t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN) } else if stats.LeafOverflowN != 0 { t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN) } else if stats.KeyN != 0 { t.Fatalf("unexpected KeyN: %d", stats.KeyN) } else if stats.Depth != 1 { t.Fatalf("unexpected Depth: %d", stats.Depth) } else if stats.BranchInuse != 0 { t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse) } else if stats.LeafInuse != 0 { t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse) } if db.Info().PageSize == 4096 { if stats.BranchAlloc != 0 { t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc) } else if stats.LeafAlloc != 0 { t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc) } } if stats.BucketN != 1 { t.Fatalf("unexpected BucketN: %d", stats.BucketN) } else if stats.InlineBucketN != 1 { t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN) } else if stats.InlineBucketInuse != 16 { t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse) } return nil }); err != nil { t.Fatal(err) } } // Ensure a bucket can calculate stats. func TestBucket_Stats_Nested(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("foo")) if err != nil { t.Fatal(err) } for i := 0; i < 100; i++ { if err := b.Put([]byte(fmt.Sprintf("%02d", i)), []byte(fmt.Sprintf("%02d", i))); err != nil { t.Fatal(err) } } bar, err := b.CreateBucket([]byte("bar")) if err != nil { t.Fatal(err) } for i := 0; i < 10; i++ { if err := bar.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil { t.Fatal(err) } } baz, err := bar.CreateBucket([]byte("baz")) if err != nil { t.Fatal(err) } for i := 0; i < 10; i++ { if err := baz.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } db.MustCheck() if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("foo")) stats := b.Stats() if stats.BranchPageN != 0 { t.Fatalf("unexpected BranchPageN: %d", stats.BranchPageN) } else if stats.BranchOverflowN != 0 { t.Fatalf("unexpected BranchOverflowN: %d", stats.BranchOverflowN) } else if stats.LeafPageN != 2 { t.Fatalf("unexpected LeafPageN: %d", stats.LeafPageN) } else if stats.LeafOverflowN != 0 { t.Fatalf("unexpected LeafOverflowN: %d", stats.LeafOverflowN) } else if stats.KeyN != 122 { t.Fatalf("unexpected KeyN: %d", stats.KeyN) } else if stats.Depth != 3 { t.Fatalf("unexpected Depth: %d", stats.Depth) } else if stats.BranchInuse != 0 { t.Fatalf("unexpected BranchInuse: %d", stats.BranchInuse) } foo := 16 // foo (pghdr) foo += 101 * 16 // foo leaf elements foo += 100*2 + 100*2 // foo leaf key/values foo += 3 + 16 // foo -> bar key/value bar := 16 // bar (pghdr) bar += 11 * 16 // bar leaf elements bar += 10 + 10 // bar leaf key/values bar += 3 + 16 // bar -> baz key/value baz := 16 // baz (inline) (pghdr) baz += 10 * 16 // baz leaf elements baz += 10 + 10 // baz leaf key/values if stats.LeafInuse != foo+bar+baz { t.Fatalf("unexpected LeafInuse: %d", stats.LeafInuse) } if db.Info().PageSize == 4096 { if stats.BranchAlloc != 0 { t.Fatalf("unexpected BranchAlloc: %d", stats.BranchAlloc) } else if stats.LeafAlloc != 8192 { t.Fatalf("unexpected LeafAlloc: %d", stats.LeafAlloc) } } if stats.BucketN != 3 { t.Fatalf("unexpected BucketN: %d", stats.BucketN) } else if stats.InlineBucketN != 1 { t.Fatalf("unexpected InlineBucketN: %d", stats.InlineBucketN) } else if stats.InlineBucketInuse != baz { t.Fatalf("unexpected InlineBucketInuse: %d", stats.InlineBucketInuse) } return nil }); err != nil { t.Fatal(err) } } // Ensure a large bucket can calculate stats. func TestBucket_Stats_Large(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } db := btesting.MustCreateDB(t) var index int for i := 0; i < 100; i++ { // Add bucket with lots of keys. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("widgets")) if err != nil { t.Fatal(err) } for i := 0; i < 1000; i++ { if err := b.Put([]byte(strconv.Itoa(index)), []byte(strconv.Itoa(index))); err != nil { t.Fatal(err) } index++ } return nil }); err != nil { t.Fatal(err) } } db.MustCheck() pageSize2stats := map[int]bolt.BucketStats{ 4096: { BranchPageN: 13, BranchOverflowN: 0, LeafPageN: 1196, LeafOverflowN: 0, KeyN: 100000, Depth: 3, BranchAlloc: 53248, BranchInuse: 25257, LeafAlloc: 4898816, LeafInuse: 2596916, BucketN: 1, InlineBucketN: 0, InlineBucketInuse: 0}, 16384: { BranchPageN: 1, BranchOverflowN: 0, LeafPageN: 292, LeafOverflowN: 0, KeyN: 100000, Depth: 2, BranchAlloc: 16384, BranchInuse: 6094, LeafAlloc: 4784128, LeafInuse: 2582452, BucketN: 1, InlineBucketN: 0, InlineBucketInuse: 0}, } if err := db.View(func(tx *bolt.Tx) error { stats := tx.Bucket([]byte("widgets")).Stats() t.Logf("Stats: %#v", stats) if expected, ok := pageSize2stats[db.Info().PageSize]; ok { assert.EqualValues(t, expected, stats, "stats differs from expectations") } else { t.Skipf("No expectations for page size: %d", db.Info().PageSize) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can write random keys and values across multiple transactions. func TestBucket_Put_Single(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } index := 0 if err := quick.Check(func(items testdata) bool { db := btesting.MustCreateDB(t) defer db.MustClose() m := make(map[string][]byte) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } for _, item := range items { if err := db.Update(func(tx *bolt.Tx) error { if err := tx.Bucket([]byte("widgets")).Put(item.Key, item.Value); err != nil { panic("put error: " + err.Error()) } m[string(item.Key)] = item.Value return nil }); err != nil { t.Fatal(err) } // Verify all key/values so far. if err := db.View(func(tx *bolt.Tx) error { i := 0 for k, v := range m { value := tx.Bucket([]byte("widgets")).Get([]byte(k)) if !bytes.Equal(value, v) { t.Logf("value mismatch [run %d] (%d of %d):\nkey: %x\ngot: %x\nexp: %x", index, i, len(m), []byte(k), value, v) db.CopyTempFile() t.FailNow() } i++ } return nil }); err != nil { t.Fatal(err) } } index++ return true }, qconfig()); err != nil { t.Error(err) } } // Ensure that a transaction can insert multiple key/value pairs at once. func TestBucket_Put_Multiple(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } if err := quick.Check(func(items testdata) bool { db := btesting.MustCreateDB(t) defer db.MustClose() // Bulk insert all values. if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for _, item := range items { if err := b.Put(item.Key, item.Value); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } // Verify all items exist. if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for _, item := range items { value := b.Get(item.Key) if !bytes.Equal(item.Value, value) { db.CopyTempFile() t.Fatalf("exp=%x; got=%x", item.Value, value) } } return nil }); err != nil { t.Fatal(err) } return true }, qconfig()); err != nil { t.Error(err) } } // Ensure that a transaction can delete all key/value pairs and return to a single leaf page. func TestBucket_Delete_Quick(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } if err := quick.Check(func(items testdata) bool { db := btesting.MustCreateDB(t) defer db.MustClose() // Bulk insert all values. if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for _, item := range items { if err := b.Put(item.Key, item.Value); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } // Remove items one at a time and check consistency. for _, item := range items { if err := db.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Delete(item.Key) }); err != nil { t.Fatal(err) } } // Anything before our deletion index should be nil. if err := db.View(func(tx *bolt.Tx) error { if err := tx.Bucket([]byte("widgets")).ForEach(func(k, v []byte) error { t.Fatalf("bucket should be empty; found: %06x", trunc(k, 3)) return nil }); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } return true }, qconfig()); err != nil { t.Error(err) } } func ExampleBucket_Put() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Start a write transaction. if err := db.Update(func(tx *bolt.Tx) error { // Create a bucket. b, err := tx.CreateBucket([]byte("widgets")) if err != nil { return err } // Set the value "bar" for the key "foo". if err := b.Put([]byte("foo"), []byte("bar")); err != nil { return err } return nil }); err != nil { log.Fatal(err) } // Read value back in a different read-only transaction. if err := db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value of 'foo' is: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) } // Output: // The value of 'foo' is: bar } func ExampleBucket_Delete() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Start a write transaction. if err := db.Update(func(tx *bolt.Tx) error { // Create a bucket. b, err := tx.CreateBucket([]byte("widgets")) if err != nil { return err } // Set the value "bar" for the key "foo". if err := b.Put([]byte("foo"), []byte("bar")); err != nil { return err } // Retrieve the key back from the database and verify it. value := b.Get([]byte("foo")) fmt.Printf("The value of 'foo' was: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Delete the key in a different write transaction. if err := db.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Delete([]byte("foo")) }); err != nil { log.Fatal(err) } // Retrieve the key again. if err := db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) if value == nil { fmt.Printf("The value of 'foo' is now: nil\n") } return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) } // Output: // The value of 'foo' was: bar // The value of 'foo' is now: nil } func ExampleBucket_ForEach() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Insert data into a bucket. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("animals")) if err != nil { return err } if err := b.Put([]byte("dog"), []byte("fun")); err != nil { return err } if err := b.Put([]byte("cat"), []byte("lame")); err != nil { return err } if err := b.Put([]byte("liger"), []byte("awesome")); err != nil { return err } // Iterate over items in sorted key order. if err := b.ForEach(func(k, v []byte) error { fmt.Printf("A %s is %s.\n", k, v) return nil }); err != nil { return err } return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) } // Output: // A cat is lame. // A dog is fun. // A liger is awesome. } bbolt-1.3.10/cmd/000077500000000000000000000000001461507540700134525ustar00rootroot00000000000000bbolt-1.3.10/cmd/bbolt/000077500000000000000000000000001461507540700145545ustar00rootroot00000000000000bbolt-1.3.10/cmd/bbolt/main.go000066400000000000000000001253071461507540700160370ustar00rootroot00000000000000package main import ( "bytes" "crypto/sha256" "encoding/binary" "encoding/hex" "errors" "flag" "fmt" "io" "math/rand" "os" "runtime" "runtime/pprof" "strconv" "strings" "time" "unicode" "unicode/utf8" "unsafe" "go.etcd.io/bbolt/internal/guts_cli" bolt "go.etcd.io/bbolt" ) var ( // ErrUsage is returned when a usage message was printed and the process // should simply exit with an error. ErrUsage = errors.New("usage") // ErrUnknownCommand is returned when a CLI command is not specified. ErrUnknownCommand = errors.New("unknown command") // ErrPathRequired is returned when the path to a Bolt database is not specified. ErrPathRequired = errors.New("path required") // ErrFileNotFound is returned when a Bolt database does not exist. ErrFileNotFound = errors.New("file not found") // ErrInvalidValue is returned when a benchmark reads an unexpected value. ErrInvalidValue = errors.New("invalid value") // ErrNonDivisibleBatchSize is returned when the batch size can't be evenly // divided by the iteration count. ErrNonDivisibleBatchSize = errors.New("number of iterations must be divisible by the batch size") // ErrPageIDRequired is returned when a required page id is not specified. ErrPageIDRequired = errors.New("page id required") // ErrBucketRequired is returned when a bucket is not specified. ErrBucketRequired = errors.New("bucket required") // ErrBucketNotFound is returned when a bucket is not found. ErrBucketNotFound = errors.New("bucket not found") // ErrKeyRequired is returned when a key is not specified. ErrKeyRequired = errors.New("key required") // ErrKeyNotFound is returned when a key is not found. ErrKeyNotFound = errors.New("key not found") // ErrNotEnoughArgs is returned with a cmd is being executed with fewer arguments. ErrNotEnoughArgs = errors.New("not enough arguments") ) func main() { m := NewMain() if err := m.Run(os.Args[1:]...); err == ErrUsage { os.Exit(2) } else if err != nil { fmt.Println(err.Error()) os.Exit(1) } } type baseCommand struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer } // Main represents the main program execution. type Main struct { baseCommand } // NewMain returns a new instance of Main connect to the standard input/output. func NewMain() *Main { return &Main{ baseCommand: baseCommand{ Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, }, } } // Run executes the program. func (m *Main) Run(args ...string) error { // Require a command at the beginning. if len(args) == 0 || strings.HasPrefix(args[0], "-") { fmt.Fprintln(m.Stderr, m.Usage()) return ErrUsage } // Execute command. switch args[0] { case "help": fmt.Fprintln(m.Stderr, m.Usage()) return ErrUsage case "bench": return newBenchCommand(m).Run(args[1:]...) case "buckets": return newBucketsCommand(m).Run(args[1:]...) case "check": return newCheckCommand(m).Run(args[1:]...) case "compact": return newCompactCommand(m).Run(args[1:]...) case "dump": return newDumpCommand(m).Run(args[1:]...) case "page-item": return newPageItemCommand(m).Run(args[1:]...) case "get": return newGetCommand(m).Run(args[1:]...) case "info": return newInfoCommand(m).Run(args[1:]...) case "keys": return newKeysCommand(m).Run(args[1:]...) case "page": return newPageCommand(m).Run(args[1:]...) case "pages": return newPagesCommand(m).Run(args[1:]...) case "stats": return newStatsCommand(m).Run(args[1:]...) case "surgery": return newSurgeryCommand(m).Run(args[1:]...) default: return ErrUnknownCommand } } // Usage returns the help message. func (m *Main) Usage() string { return strings.TrimLeft(` Bbolt is a tool for inspecting bbolt databases. Usage: bbolt command [arguments] The commands are: bench run synthetic benchmark against bbolt buckets print a list of buckets check verifies integrity of bbolt database compact copies a bbolt database, compacting it in the process dump print a hexadecimal dump of a single page get print the value of a key in a bucket info print basic info keys print a list of keys in a bucket help print this screen page print one or more pages in human readable format pages print list of pages with their types page-item print the key and value of a page item. stats iterate over all pages and generate usage stats surgery perform surgery on bbolt database Use "bbolt [command] -h" for more information about a command. `, "\n") } // checkCommand represents the "check" command execution. type checkCommand struct { baseCommand } // newCheckCommand returns a checkCommand. func newCheckCommand(m *Main) *checkCommand { c := &checkCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *checkCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path. path := fs.Arg(0) if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } // Open database. db, err := bolt.Open(path, 0666, &bolt.Options{ ReadOnly: true, PreLoadFreelist: true, }) if err != nil { return err } defer db.Close() // Perform consistency check. return db.View(func(tx *bolt.Tx) error { var count int for err := range tx.CheckWithOptions(bolt.WithKVStringer(CmdKvStringer())) { fmt.Fprintln(cmd.Stdout, err) count++ } // Print summary of errors. if count > 0 { fmt.Fprintf(cmd.Stdout, "%d errors found\n", count) return guts_cli.ErrCorrupt } // Notify user that database is valid. fmt.Fprintln(cmd.Stdout, "OK") return nil }) } // Usage returns the help message. func (cmd *checkCommand) Usage() string { return strings.TrimLeft(` usage: bolt check PATH Check opens a database at PATH and runs an exhaustive check to verify that all pages are accessible or are marked as freed. It also verifies that no pages are double referenced. Verification errors will stream out as they are found and the process will return after all pages have been checked. `, "\n") } // infoCommand represents the "info" command execution. type infoCommand struct { baseCommand } // newInfoCommand returns a infoCommand. func newInfoCommand(m *Main) *infoCommand { c := &infoCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *infoCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path. path := fs.Arg(0) if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } // Open the database. db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) if err != nil { return err } defer db.Close() // Print basic database info. info := db.Info() fmt.Fprintf(cmd.Stdout, "Page Size: %d\n", info.PageSize) return nil } // Usage returns the help message. func (cmd *infoCommand) Usage() string { return strings.TrimLeft(` usage: bolt info PATH Info prints basic information about the Bolt database at PATH. `, "\n") } // dumpCommand represents the "dump" command execution. type dumpCommand struct { baseCommand } // newDumpCommand returns a dumpCommand. func newDumpCommand(m *Main) *dumpCommand { c := &dumpCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *dumpCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path and page id. path := fs.Arg(0) if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } // Read page ids. pageIDs, err := stringToPages(fs.Args()[1:]) if err != nil { return err } else if len(pageIDs) == 0 { return ErrPageIDRequired } // Open database to retrieve page size. pageSize, _, err := guts_cli.ReadPageAndHWMSize(path) if err != nil { return err } // Open database file handler. f, err := os.Open(path) if err != nil { return err } defer func() { _ = f.Close() }() // Print each page listed. for i, pageID := range pageIDs { // Print a separator. if i > 0 { fmt.Fprintln(cmd.Stdout, "===============================================") } // Print page to stdout. if err := cmd.PrintPage(cmd.Stdout, f, pageID, uint64(pageSize)); err != nil { return err } } return nil } // PrintPage prints a given page as hexadecimal. func (cmd *dumpCommand) PrintPage(w io.Writer, r io.ReaderAt, pageID uint64, pageSize uint64) error { const bytesPerLineN = 16 // Read page into buffer. buf := make([]byte, pageSize) addr := pageID * uint64(pageSize) if n, err := r.ReadAt(buf, int64(addr)); err != nil { return err } else if uint64(n) != pageSize { return io.ErrUnexpectedEOF } // Write out to writer in 16-byte lines. var prev []byte var skipped bool for offset := uint64(0); offset < pageSize; offset += bytesPerLineN { // Retrieve current 16-byte line. line := buf[offset : offset+bytesPerLineN] isLastLine := (offset == (pageSize - bytesPerLineN)) // If it's the same as the previous line then print a skip. if bytes.Equal(line, prev) && !isLastLine { if !skipped { fmt.Fprintf(w, "%07x *\n", addr+offset) skipped = true } } else { // Print line as hexadecimal in 2-byte groups. fmt.Fprintf(w, "%07x %04x %04x %04x %04x %04x %04x %04x %04x\n", addr+offset, line[0:2], line[2:4], line[4:6], line[6:8], line[8:10], line[10:12], line[12:14], line[14:16], ) skipped = false } // Save the previous line. prev = line } fmt.Fprint(w, "\n") return nil } // Usage returns the help message. func (cmd *dumpCommand) Usage() string { return strings.TrimLeft(` usage: bolt dump PATH pageid [pageid...] Dump prints a hexadecimal dump of one or more pages. `, "\n") } // pageItemCommand represents the "page-item" command execution. type pageItemCommand struct { baseCommand } // newPageItemCommand returns a pageItemCommand. func newPageItemCommand(m *Main) *pageItemCommand { c := &pageItemCommand{} c.baseCommand = m.baseCommand return c } type pageItemOptions struct { help bool keyOnly bool valueOnly bool format string } // Run executes the command. func (cmd *pageItemCommand) Run(args ...string) error { // Parse flags. options := &pageItemOptions{} fs := flag.NewFlagSet("", flag.ContinueOnError) fs.BoolVar(&options.keyOnly, "key-only", false, "Print only the key") fs.BoolVar(&options.valueOnly, "value-only", false, "Print only the value") fs.StringVar(&options.format, "format", "ascii-encoded", "Output format. One of: "+FORMAT_MODES) fs.BoolVar(&options.help, "h", false, "") if err := fs.Parse(args); err != nil { return err } else if options.help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } if options.keyOnly && options.valueOnly { return fmt.Errorf("The --key-only or --value-only flag may be set, but not both.") } // Require database path and page id. path := fs.Arg(0) if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } // Read page id. pageID, err := strconv.ParseUint(fs.Arg(1), 10, 64) if err != nil { return err } // Read item id. itemID, err := strconv.ParseUint(fs.Arg(2), 10, 64) if err != nil { return err } // Open database file handler. f, err := os.Open(path) if err != nil { return err } defer func() { _ = f.Close() }() // Retrieve page info and page size. _, buf, err := guts_cli.ReadPage(path, pageID) if err != nil { return err } if !options.valueOnly { err := cmd.PrintLeafItemKey(cmd.Stdout, buf, uint16(itemID), options.format) if err != nil { return err } } if !options.keyOnly { err := cmd.PrintLeafItemValue(cmd.Stdout, buf, uint16(itemID), options.format) if err != nil { return err } } return nil } // leafPageElement retrieves a leaf page element. func (cmd *pageItemCommand) leafPageElement(pageBytes []byte, index uint16) (*guts_cli.LeafPageElement, error) { p := (*guts_cli.Page)(unsafe.Pointer(&pageBytes[0])) if index >= p.Count() { return nil, fmt.Errorf("leafPageElement: expected item index less than %d, but got %d.", p.Count(), index) } if p.Type() != "leaf" { return nil, fmt.Errorf("leafPageElement: expected page type of 'leaf', but got '%s'", p.Type()) } return p.LeafPageElement(index), nil } const FORMAT_MODES = "auto|ascii-encoded|hex|bytes|redacted" // formatBytes converts bytes into string according to format. // Supported formats: ascii-encoded, hex, bytes. func formatBytes(b []byte, format string) (string, error) { switch format { case "ascii-encoded": return fmt.Sprintf("%q", b), nil case "hex": return fmt.Sprintf("%x", b), nil case "bytes": return string(b), nil case "auto": return bytesToAsciiOrHex(b), nil case "redacted": return fmt.Sprintf("", len(b), sha256.New().Sum(b)), nil default: return "", fmt.Errorf("formatBytes: unsupported format: %s", format) } } func parseBytes(str string, format string) ([]byte, error) { switch format { case "ascii-encoded": return []byte(str), nil case "hex": return hex.DecodeString(str) default: return nil, fmt.Errorf("parseBytes: unsupported format: %s", format) } } // writelnBytes writes the byte to the writer. Supported formats: ascii-encoded, hex, bytes, auto, redacted. // Terminates the write with a new line symbol; func writelnBytes(w io.Writer, b []byte, format string) error { str, err := formatBytes(b, format) if err != nil { return err } _, err = fmt.Fprintln(w, str) return err } // PrintLeafItemKey writes the bytes of a leaf element's key. func (cmd *pageItemCommand) PrintLeafItemKey(w io.Writer, pageBytes []byte, index uint16, format string) error { e, err := cmd.leafPageElement(pageBytes, index) if err != nil { return err } return writelnBytes(w, e.Key(), format) } // PrintLeafItemKey writes the bytes of a leaf element's value. func (cmd *pageItemCommand) PrintLeafItemValue(w io.Writer, pageBytes []byte, index uint16, format string) error { e, err := cmd.leafPageElement(pageBytes, index) if err != nil { return err } return writelnBytes(w, e.Value(), format) } // Usage returns the help message. func (cmd *pageItemCommand) Usage() string { return strings.TrimLeft(` usage: bolt page-item [options] PATH pageid itemid Additional options include: --key-only Print only the key --value-only Print only the value --format Output format. One of: `+FORMAT_MODES+` (default=ascii-encoded) page-item prints a page item key and value. `, "\n") } // pagesCommand represents the "pages" command execution. type pagesCommand struct { baseCommand } // newPagesCommand returns a pagesCommand. func newPagesCommand(m *Main) *pagesCommand { c := &pagesCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *pagesCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path. path := fs.Arg(0) if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } // Open database. db, err := bolt.Open(path, 0666, &bolt.Options{ ReadOnly: true, PreLoadFreelist: true, }) if err != nil { return err } defer func() { _ = db.Close() }() // Write header. fmt.Fprintln(cmd.Stdout, "ID TYPE ITEMS OVRFLW") fmt.Fprintln(cmd.Stdout, "======== ========== ====== ======") return db.View(func(tx *bolt.Tx) error { var id int for { p, err := tx.Page(id) if err != nil { return &PageError{ID: id, Err: err} } else if p == nil { break } // Only display count and overflow if this is a non-free page. var count, overflow string if p.Type != "free" { count = strconv.Itoa(p.Count) if p.OverflowCount > 0 { overflow = strconv.Itoa(p.OverflowCount) } } // Print table row. fmt.Fprintf(cmd.Stdout, "%-8d %-10s %-6s %-6s\n", p.ID, p.Type, count, overflow) // Move to the next non-overflow page. id += 1 if p.Type != "free" { id += p.OverflowCount } } return nil }) } // Usage returns the help message. func (cmd *pagesCommand) Usage() string { return strings.TrimLeft(` usage: bolt pages PATH Pages prints a table of pages with their type (meta, leaf, branch, freelist). Leaf and branch pages will show a key count in the "items" column while the freelist will show the number of free pages in the "items" column. The "overflow" column shows the number of blocks that the page spills over into. Normally there is no overflow but large keys and values can cause a single page to take up multiple blocks. `, "\n") } // statsCommand represents the "stats" command execution. type statsCommand struct { baseCommand } // newStatsCommand returns a statsCommand. func newStatsCommand(m *Main) *statsCommand { c := &statsCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *statsCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path. path, prefix := fs.Arg(0), fs.Arg(1) if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } // Open database. db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) if err != nil { return err } defer db.Close() return db.View(func(tx *bolt.Tx) error { var s bolt.BucketStats var count int if err := tx.ForEach(func(name []byte, b *bolt.Bucket) error { if bytes.HasPrefix(name, []byte(prefix)) { s.Add(b.Stats()) count += 1 } return nil }); err != nil { return err } fmt.Fprintf(cmd.Stdout, "Aggregate statistics for %d buckets\n\n", count) fmt.Fprintln(cmd.Stdout, "Page count statistics") fmt.Fprintf(cmd.Stdout, "\tNumber of logical branch pages: %d\n", s.BranchPageN) fmt.Fprintf(cmd.Stdout, "\tNumber of physical branch overflow pages: %d\n", s.BranchOverflowN) fmt.Fprintf(cmd.Stdout, "\tNumber of logical leaf pages: %d\n", s.LeafPageN) fmt.Fprintf(cmd.Stdout, "\tNumber of physical leaf overflow pages: %d\n", s.LeafOverflowN) fmt.Fprintln(cmd.Stdout, "Tree statistics") fmt.Fprintf(cmd.Stdout, "\tNumber of keys/value pairs: %d\n", s.KeyN) fmt.Fprintf(cmd.Stdout, "\tNumber of levels in B+tree: %d\n", s.Depth) fmt.Fprintln(cmd.Stdout, "Page size utilization") fmt.Fprintf(cmd.Stdout, "\tBytes allocated for physical branch pages: %d\n", s.BranchAlloc) var percentage int if s.BranchAlloc != 0 { percentage = int(float32(s.BranchInuse) * 100.0 / float32(s.BranchAlloc)) } fmt.Fprintf(cmd.Stdout, "\tBytes actually used for branch data: %d (%d%%)\n", s.BranchInuse, percentage) fmt.Fprintf(cmd.Stdout, "\tBytes allocated for physical leaf pages: %d\n", s.LeafAlloc) percentage = 0 if s.LeafAlloc != 0 { percentage = int(float32(s.LeafInuse) * 100.0 / float32(s.LeafAlloc)) } fmt.Fprintf(cmd.Stdout, "\tBytes actually used for leaf data: %d (%d%%)\n", s.LeafInuse, percentage) fmt.Fprintln(cmd.Stdout, "Bucket statistics") fmt.Fprintf(cmd.Stdout, "\tTotal number of buckets: %d\n", s.BucketN) percentage = 0 if s.BucketN != 0 { percentage = int(float32(s.InlineBucketN) * 100.0 / float32(s.BucketN)) } fmt.Fprintf(cmd.Stdout, "\tTotal number on inlined buckets: %d (%d%%)\n", s.InlineBucketN, percentage) percentage = 0 if s.LeafInuse != 0 { percentage = int(float32(s.InlineBucketInuse) * 100.0 / float32(s.LeafInuse)) } fmt.Fprintf(cmd.Stdout, "\tBytes used for inlined buckets: %d (%d%%)\n", s.InlineBucketInuse, percentage) return nil }) } // Usage returns the help message. func (cmd *statsCommand) Usage() string { return strings.TrimLeft(` usage: bolt stats PATH Stats performs an extensive search of the database to track every page reference. It starts at the current meta page and recursively iterates through every accessible bucket. The following errors can be reported: already freed The page is referenced more than once in the freelist. unreachable unfreed The page is not referenced by a bucket or in the freelist. reachable freed The page is referenced by a bucket but is also in the freelist. out of bounds A page is referenced that is above the high water mark. multiple references A page is referenced by more than one other page. invalid type The page type is not "meta", "leaf", "branch", or "freelist". No errors should occur in your database. However, if for some reason you experience corruption, please submit a ticket to the Bolt project page: https://github.com/boltdb/bolt/issues `, "\n") } // bucketsCommand represents the "buckets" command execution. type bucketsCommand struct { baseCommand } // newBucketsCommand returns a bucketsCommand. func newBucketsCommand(m *Main) *bucketsCommand { c := &bucketsCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *bucketsCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path. path := fs.Arg(0) if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } // Open database. db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) if err != nil { return err } defer db.Close() // Print buckets. return db.View(func(tx *bolt.Tx) error { return tx.ForEach(func(name []byte, _ *bolt.Bucket) error { fmt.Fprintln(cmd.Stdout, string(name)) return nil }) }) } // Usage returns the help message. func (cmd *bucketsCommand) Usage() string { return strings.TrimLeft(` usage: bolt buckets PATH Print a list of buckets. `, "\n") } // keysCommand represents the "keys" command execution. type keysCommand struct { baseCommand } // newKeysCommand returns a keysCommand. func newKeysCommand(m *Main) *keysCommand { c := &keysCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *keysCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) optionsFormat := fs.String("format", "bytes", "Output format. One of: "+FORMAT_MODES+" (default: bytes)") help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path and bucket. relevantArgs := fs.Args() if len(relevantArgs) < 2 { return ErrNotEnoughArgs } path, buckets := relevantArgs[0], relevantArgs[1:] if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } else if len(buckets) == 0 { return ErrBucketRequired } // Open database. db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) if err != nil { return err } defer db.Close() // Print keys. return db.View(func(tx *bolt.Tx) error { // Find bucket. var lastbucket *bolt.Bucket = tx.Bucket([]byte(buckets[0])) if lastbucket == nil { return ErrBucketNotFound } for _, bucket := range buckets[1:] { lastbucket = lastbucket.Bucket([]byte(bucket)) if lastbucket == nil { return ErrBucketNotFound } } // Iterate over each key. return lastbucket.ForEach(func(key, _ []byte) error { return writelnBytes(cmd.Stdout, key, *optionsFormat) }) }) } // Usage returns the help message. // TODO: Use https://pkg.go.dev/flag#FlagSet.PrintDefaults to print supported flags. func (cmd *keysCommand) Usage() string { return strings.TrimLeft(` usage: bolt keys PATH [BUCKET...] Print a list of keys in the given (sub)bucket. ======= Additional options include: --format Output format. One of: `+FORMAT_MODES+` (default=bytes) Print a list of keys in the given bucket. `, "\n") } // getCommand represents the "get" command execution. type getCommand struct { baseCommand } // newGetCommand returns a getCommand. func newGetCommand(m *Main) *getCommand { c := &getCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *getCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) var parseFormat string var format string fs.StringVar(&parseFormat, "parse-format", "ascii-encoded", "Input format. One of: ascii-encoded|hex (default: ascii-encoded)") fs.StringVar(&format, "format", "bytes", "Output format. One of: "+FORMAT_MODES+" (default: bytes)") help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path, bucket and key. relevantArgs := fs.Args() if len(relevantArgs) < 3 { return ErrNotEnoughArgs } path, buckets := relevantArgs[0], relevantArgs[1:len(relevantArgs)-1] key, err := parseBytes(relevantArgs[len(relevantArgs)-1], parseFormat) if err != nil { return err } if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } else if len(buckets) == 0 { return ErrBucketRequired } else if len(key) == 0 { return ErrKeyRequired } // Open database. db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) if err != nil { return err } defer db.Close() // Print value. return db.View(func(tx *bolt.Tx) error { // Find bucket. var lastbucket *bolt.Bucket = tx.Bucket([]byte(buckets[0])) if lastbucket == nil { return ErrBucketNotFound } for _, bucket := range buckets[1:] { lastbucket = lastbucket.Bucket([]byte(bucket)) if lastbucket == nil { return ErrBucketNotFound } } // Find value for given key. val := lastbucket.Get(key) if val == nil { return fmt.Errorf("Error %w for key: %q hex: \"%x\"", ErrKeyNotFound, key, string(key)) } // TODO: In this particular case, it would be better to not terminate with '\n' return writelnBytes(cmd.Stdout, val, format) }) } // Usage returns the help message. func (cmd *getCommand) Usage() string { return strings.TrimLeft(` usage: bolt get PATH [BUCKET..] KEY Print the value of the given key in the given (sub)bucket. Additional options include: --format Output format. One of: `+FORMAT_MODES+` (default=bytes) --parse-format Input format (of key). One of: ascii-encoded|hex (default=ascii-encoded)" `, "\n") } var benchBucketName = []byte("bench") // benchCommand represents the "bench" command execution. type benchCommand struct { baseCommand } // newBenchCommand returns a BenchCommand using the func newBenchCommand(m *Main) *benchCommand { c := &benchCommand{} c.baseCommand = m.baseCommand return c } // Run executes the "bench" command. func (cmd *benchCommand) Run(args ...string) error { // Parse CLI arguments. options, err := cmd.ParseFlags(args) if err != nil { return err } // Remove path if "-work" is not set. Otherwise keep path. if options.Work { fmt.Fprintf(cmd.Stdout, "work: %s\n", options.Path) } else { defer os.Remove(options.Path) } // Create database. db, err := bolt.Open(options.Path, 0666, nil) if err != nil { return err } db.NoSync = options.NoSync defer db.Close() // Write to the database. var results BenchResults if err := cmd.runWrites(db, options, &results); err != nil { return fmt.Errorf("write: %v", err) } // Read from the database. if err := cmd.runReads(db, options, &results); err != nil { return fmt.Errorf("bench: read: %s", err) } // Print results. fmt.Fprintf(os.Stderr, "# Write\t%v\t(%v/op)\t(%v op/sec)\n", results.WriteDuration, results.WriteOpDuration(), results.WriteOpsPerSecond()) fmt.Fprintf(os.Stderr, "# Read\t%v\t(%v/op)\t(%v op/sec)\n", results.ReadDuration, results.ReadOpDuration(), results.ReadOpsPerSecond()) fmt.Fprintln(os.Stderr, "") return nil } // ParseFlags parses the command line flags. func (cmd *benchCommand) ParseFlags(args []string) (*BenchOptions, error) { var options BenchOptions // Parse flagset. fs := flag.NewFlagSet("", flag.ContinueOnError) fs.StringVar(&options.ProfileMode, "profile-mode", "rw", "") fs.StringVar(&options.WriteMode, "write-mode", "seq", "") fs.StringVar(&options.ReadMode, "read-mode", "seq", "") fs.IntVar(&options.Iterations, "count", 1000, "") fs.IntVar(&options.BatchSize, "batch-size", 0, "") fs.IntVar(&options.KeySize, "key-size", 8, "") fs.IntVar(&options.ValueSize, "value-size", 32, "") fs.StringVar(&options.CPUProfile, "cpuprofile", "", "") fs.StringVar(&options.MemProfile, "memprofile", "", "") fs.StringVar(&options.BlockProfile, "blockprofile", "", "") fs.Float64Var(&options.FillPercent, "fill-percent", bolt.DefaultFillPercent, "") fs.BoolVar(&options.NoSync, "no-sync", false, "") fs.BoolVar(&options.Work, "work", false, "") fs.StringVar(&options.Path, "path", "", "") fs.SetOutput(cmd.Stderr) if err := fs.Parse(args); err != nil { return nil, err } // Set batch size to iteration size if not set. // Require that batch size can be evenly divided by the iteration count. if options.BatchSize == 0 { options.BatchSize = options.Iterations } else if options.Iterations%options.BatchSize != 0 { return nil, ErrNonDivisibleBatchSize } // Generate temp path if one is not passed in. if options.Path == "" { f, err := os.CreateTemp("", "bolt-bench-") if err != nil { return nil, fmt.Errorf("temp file: %s", err) } f.Close() os.Remove(f.Name()) options.Path = f.Name() } return &options, nil } // Writes to the database. func (cmd *benchCommand) runWrites(db *bolt.DB, options *BenchOptions, results *BenchResults) error { // Start profiling for writes. if options.ProfileMode == "rw" || options.ProfileMode == "w" { cmd.startProfiling(options) } t := time.Now() var err error switch options.WriteMode { case "seq": err = cmd.runWritesSequential(db, options, results) case "rnd": err = cmd.runWritesRandom(db, options, results) case "seq-nest": err = cmd.runWritesSequentialNested(db, options, results) case "rnd-nest": err = cmd.runWritesRandomNested(db, options, results) default: return fmt.Errorf("invalid write mode: %s", options.WriteMode) } // Save time to write. results.WriteDuration = time.Since(t) // Stop profiling for writes only. if options.ProfileMode == "w" { cmd.stopProfiling() } return err } func (cmd *benchCommand) runWritesSequential(db *bolt.DB, options *BenchOptions, results *BenchResults) error { var i = uint32(0) return cmd.runWritesWithSource(db, options, results, func() uint32 { i++; return i }) } func (cmd *benchCommand) runWritesRandom(db *bolt.DB, options *BenchOptions, results *BenchResults) error { r := rand.New(rand.NewSource(time.Now().UnixNano())) return cmd.runWritesWithSource(db, options, results, func() uint32 { return r.Uint32() }) } func (cmd *benchCommand) runWritesSequentialNested(db *bolt.DB, options *BenchOptions, results *BenchResults) error { var i = uint32(0) return cmd.runWritesNestedWithSource(db, options, results, func() uint32 { i++; return i }) } func (cmd *benchCommand) runWritesRandomNested(db *bolt.DB, options *BenchOptions, results *BenchResults) error { r := rand.New(rand.NewSource(time.Now().UnixNano())) return cmd.runWritesNestedWithSource(db, options, results, func() uint32 { return r.Uint32() }) } func (cmd *benchCommand) runWritesWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error { results.WriteOps = options.Iterations for i := 0; i < options.Iterations; i += options.BatchSize { if err := db.Update(func(tx *bolt.Tx) error { b, _ := tx.CreateBucketIfNotExists(benchBucketName) b.FillPercent = options.FillPercent for j := 0; j < options.BatchSize; j++ { key := make([]byte, options.KeySize) value := make([]byte, options.ValueSize) // Write key as uint32. binary.BigEndian.PutUint32(key, keySource()) // Insert key/value. if err := b.Put(key, value); err != nil { return err } } return nil }); err != nil { return err } } return nil } func (cmd *benchCommand) runWritesNestedWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error { results.WriteOps = options.Iterations for i := 0; i < options.Iterations; i += options.BatchSize { if err := db.Update(func(tx *bolt.Tx) error { top, err := tx.CreateBucketIfNotExists(benchBucketName) if err != nil { return err } top.FillPercent = options.FillPercent // Create bucket key. name := make([]byte, options.KeySize) binary.BigEndian.PutUint32(name, keySource()) // Create bucket. b, err := top.CreateBucketIfNotExists(name) if err != nil { return err } b.FillPercent = options.FillPercent for j := 0; j < options.BatchSize; j++ { var key = make([]byte, options.KeySize) var value = make([]byte, options.ValueSize) // Generate key as uint32. binary.BigEndian.PutUint32(key, keySource()) // Insert value into subbucket. if err := b.Put(key, value); err != nil { return err } } return nil }); err != nil { return err } } return nil } // Reads from the database. func (cmd *benchCommand) runReads(db *bolt.DB, options *BenchOptions, results *BenchResults) error { // Start profiling for reads. if options.ProfileMode == "r" { cmd.startProfiling(options) } t := time.Now() var err error switch options.ReadMode { case "seq": switch options.WriteMode { case "seq-nest", "rnd-nest": err = cmd.runReadsSequentialNested(db, options, results) default: err = cmd.runReadsSequential(db, options, results) } default: return fmt.Errorf("invalid read mode: %s", options.ReadMode) } // Save read time. results.ReadDuration = time.Since(t) // Stop profiling for reads. if options.ProfileMode == "rw" || options.ProfileMode == "r" { cmd.stopProfiling() } return err } func (cmd *benchCommand) runReadsSequential(db *bolt.DB, options *BenchOptions, results *BenchResults) error { return db.View(func(tx *bolt.Tx) error { t := time.Now() for { var count int c := tx.Bucket(benchBucketName).Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { if v == nil { return errors.New("invalid value") } count++ } if options.WriteMode == "seq" && count != options.Iterations { return fmt.Errorf("read seq: iter mismatch: expected %d, got %d", options.Iterations, count) } results.ReadOps += count // Make sure we do this for at least a second. if time.Since(t) >= time.Second { break } } return nil }) } func (cmd *benchCommand) runReadsSequentialNested(db *bolt.DB, options *BenchOptions, results *BenchResults) error { return db.View(func(tx *bolt.Tx) error { t := time.Now() for { var count int var top = tx.Bucket(benchBucketName) if err := top.ForEach(func(name, _ []byte) error { if b := top.Bucket(name); b != nil { c := b.Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { if v == nil { return ErrInvalidValue } count++ } } return nil }); err != nil { return err } if options.WriteMode == "seq-nest" && count != options.Iterations { return fmt.Errorf("read seq-nest: iter mismatch: expected %d, got %d", options.Iterations, count) } results.ReadOps += count // Make sure we do this for at least a second. if time.Since(t) >= time.Second { break } } return nil }) } // File handlers for the various profiles. var cpuprofile, memprofile, blockprofile *os.File // Starts all profiles set on the options. func (cmd *benchCommand) startProfiling(options *BenchOptions) { var err error // Start CPU profiling. if options.CPUProfile != "" { cpuprofile, err = os.Create(options.CPUProfile) if err != nil { fmt.Fprintf(cmd.Stderr, "bench: could not create cpu profile %q: %v\n", options.CPUProfile, err) os.Exit(1) } err = pprof.StartCPUProfile(cpuprofile) if err != nil { fmt.Fprintf(cmd.Stderr, "bench: could not start cpu profile %q: %v\n", options.CPUProfile, err) os.Exit(1) } } // Start memory profiling. if options.MemProfile != "" { memprofile, err = os.Create(options.MemProfile) if err != nil { fmt.Fprintf(cmd.Stderr, "bench: could not create memory profile %q: %v\n", options.MemProfile, err) os.Exit(1) } runtime.MemProfileRate = 4096 } // Start fatal profiling. if options.BlockProfile != "" { blockprofile, err = os.Create(options.BlockProfile) if err != nil { fmt.Fprintf(cmd.Stderr, "bench: could not create block profile %q: %v\n", options.BlockProfile, err) os.Exit(1) } runtime.SetBlockProfileRate(1) } } // Stops all profiles. func (cmd *benchCommand) stopProfiling() { if cpuprofile != nil { pprof.StopCPUProfile() cpuprofile.Close() cpuprofile = nil } if memprofile != nil { err := pprof.Lookup("heap").WriteTo(memprofile, 0) if err != nil { fmt.Fprintf(cmd.Stderr, "bench: could not write mem profile") } memprofile.Close() memprofile = nil } if blockprofile != nil { err := pprof.Lookup("block").WriteTo(blockprofile, 0) if err != nil { fmt.Fprintf(cmd.Stderr, "bench: could not write block profile") } blockprofile.Close() blockprofile = nil runtime.SetBlockProfileRate(0) } } // BenchOptions represents the set of options that can be passed to "bolt bench". type BenchOptions struct { ProfileMode string WriteMode string ReadMode string Iterations int BatchSize int KeySize int ValueSize int CPUProfile string MemProfile string BlockProfile string StatsInterval time.Duration FillPercent float64 NoSync bool Work bool Path string } // BenchResults represents the performance results of the benchmark. type BenchResults struct { WriteOps int WriteDuration time.Duration ReadOps int ReadDuration time.Duration } // Returns the duration for a single write operation. func (r *BenchResults) WriteOpDuration() time.Duration { if r.WriteOps == 0 { return 0 } return r.WriteDuration / time.Duration(r.WriteOps) } // Returns average number of write operations that can be performed per second. func (r *BenchResults) WriteOpsPerSecond() int { var op = r.WriteOpDuration() if op == 0 { return 0 } return int(time.Second) / int(op) } // Returns the duration for a single read operation. func (r *BenchResults) ReadOpDuration() time.Duration { if r.ReadOps == 0 { return 0 } return r.ReadDuration / time.Duration(r.ReadOps) } // Returns average number of read operations that can be performed per second. func (r *BenchResults) ReadOpsPerSecond() int { var op = r.ReadOpDuration() if op == 0 { return 0 } return int(time.Second) / int(op) } type PageError struct { ID int Err error } func (e *PageError) Error() string { return fmt.Sprintf("page error: id=%d, err=%s", e.ID, e.Err) } // isPrintable returns true if the string is valid unicode and contains only printable runes. func isPrintable(s string) bool { if !utf8.ValidString(s) { return false } for _, ch := range s { if !unicode.IsPrint(ch) { return false } } return true } func bytesToAsciiOrHex(b []byte) string { sb := string(b) if isPrintable(sb) { return sb } else { return hex.EncodeToString(b) } } func stringToPage(str string) (uint64, error) { return strconv.ParseUint(str, 10, 64) } // stringToPages parses a slice of strings into page ids. func stringToPages(strs []string) ([]uint64, error) { var a []uint64 for _, str := range strs { i, err := stringToPage(str) if err != nil { return nil, err } a = append(a, i) } return a, nil } // compactCommand represents the "compact" command execution. type compactCommand struct { baseCommand SrcPath string DstPath string TxMaxSize int64 } // newCompactCommand returns a CompactCommand. func newCompactCommand(m *Main) *compactCommand { c := &compactCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *compactCommand) Run(args ...string) (err error) { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) fs.SetOutput(io.Discard) fs.StringVar(&cmd.DstPath, "o", "", "") fs.Int64Var(&cmd.TxMaxSize, "tx-max-size", 65536, "") if err := fs.Parse(args); err == flag.ErrHelp { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } else if err != nil { return err } else if cmd.DstPath == "" { return fmt.Errorf("output file required") } // Require database paths. cmd.SrcPath = fs.Arg(0) if cmd.SrcPath == "" { return ErrPathRequired } // Ensure source file exists. fi, err := os.Stat(cmd.SrcPath) if os.IsNotExist(err) { return ErrFileNotFound } else if err != nil { return err } initialSize := fi.Size() // Open source database. src, err := bolt.Open(cmd.SrcPath, 0444, &bolt.Options{ReadOnly: true}) if err != nil { return err } defer src.Close() // Open destination database. dst, err := bolt.Open(cmd.DstPath, fi.Mode(), nil) if err != nil { return err } defer dst.Close() // Run compaction. if err := bolt.Compact(dst, src, cmd.TxMaxSize); err != nil { return err } // Report stats on new size. fi, err = os.Stat(cmd.DstPath) if err != nil { return err } else if fi.Size() == 0 { return fmt.Errorf("zero db size") } fmt.Fprintf(cmd.Stdout, "%d -> %d bytes (gain=%.2fx)\n", initialSize, fi.Size(), float64(initialSize)/float64(fi.Size())) return nil } // Usage returns the help message. func (cmd *compactCommand) Usage() string { return strings.TrimLeft(` usage: bolt compact [options] -o DST SRC Compact opens a database at SRC path and walks it recursively, copying keys as they are found from all buckets, to a newly created database at DST path. The original database is left untouched. Additional options include: -tx-max-size NUM Specifies the maximum size of individual transactions. Defaults to 64KB. `, "\n") } type cmdKvStringer struct{} func (_ cmdKvStringer) KeyToString(key []byte) string { return bytesToAsciiOrHex(key) } func (_ cmdKvStringer) ValueToString(value []byte) string { return bytesToAsciiOrHex(value) } func CmdKvStringer() bolt.KVStringer { return cmdKvStringer{} } bbolt-1.3.10/cmd/bbolt/main_test.go000066400000000000000000000270171461507540700170750ustar00rootroot00000000000000package main_test import ( "bytes" crypto "crypto/rand" "fmt" "io" "math/rand" "os" "strconv" "testing" "go.etcd.io/bbolt/internal/btesting" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" main "go.etcd.io/bbolt/cmd/bbolt" ) // Ensure the "info" command can print information about a database. func TestInfoCommand_Run(t *testing.T) { db := btesting.MustCreateDB(t) db.Close() defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) // Run the info command. m := NewMain() if err := m.Run("info", db.Path()); err != nil { t.Fatal(err) } } // Ensure the "stats" command executes correctly with an empty database. func TestStatsCommand_Run_EmptyDatabase(t *testing.T) { // Ignore if os.Getpagesize() != 4096 { t.Skip("system does not use 4KB page size") } db := btesting.MustCreateDB(t) db.Close() defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) // Generate expected result. exp := "Aggregate statistics for 0 buckets\n\n" + "Page count statistics\n" + "\tNumber of logical branch pages: 0\n" + "\tNumber of physical branch overflow pages: 0\n" + "\tNumber of logical leaf pages: 0\n" + "\tNumber of physical leaf overflow pages: 0\n" + "Tree statistics\n" + "\tNumber of keys/value pairs: 0\n" + "\tNumber of levels in B+tree: 0\n" + "Page size utilization\n" + "\tBytes allocated for physical branch pages: 0\n" + "\tBytes actually used for branch data: 0 (0%)\n" + "\tBytes allocated for physical leaf pages: 0\n" + "\tBytes actually used for leaf data: 0 (0%)\n" + "Bucket statistics\n" + "\tTotal number of buckets: 0\n" + "\tTotal number on inlined buckets: 0 (0%)\n" + "\tBytes used for inlined buckets: 0 (0%)\n" // Run the command. m := NewMain() if err := m.Run("stats", db.Path()); err != nil { t.Fatal(err) } else if m.Stdout.String() != exp { t.Fatalf("unexpected stdout:\n\n%s", m.Stdout.String()) } } // Ensure the "stats" command can execute correctly. func TestStatsCommand_Run(t *testing.T) { // Ignore if os.Getpagesize() != 4096 { t.Skip("system does not use 4KB page size") } db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { // Create "foo" bucket. b, err := tx.CreateBucket([]byte("foo")) if err != nil { return err } for i := 0; i < 10; i++ { if err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil { return err } } // Create "bar" bucket. b, err = tx.CreateBucket([]byte("bar")) if err != nil { return err } for i := 0; i < 100; i++ { if err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil { return err } } // Create "baz" bucket. b, err = tx.CreateBucket([]byte("baz")) if err != nil { return err } if err := b.Put([]byte("key"), []byte("value")); err != nil { return err } return nil }); err != nil { t.Fatal(err) } db.Close() defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) // Generate expected result. exp := "Aggregate statistics for 3 buckets\n\n" + "Page count statistics\n" + "\tNumber of logical branch pages: 0\n" + "\tNumber of physical branch overflow pages: 0\n" + "\tNumber of logical leaf pages: 1\n" + "\tNumber of physical leaf overflow pages: 0\n" + "Tree statistics\n" + "\tNumber of keys/value pairs: 111\n" + "\tNumber of levels in B+tree: 1\n" + "Page size utilization\n" + "\tBytes allocated for physical branch pages: 0\n" + "\tBytes actually used for branch data: 0 (0%)\n" + "\tBytes allocated for physical leaf pages: 4096\n" + "\tBytes actually used for leaf data: 1996 (48%)\n" + "Bucket statistics\n" + "\tTotal number of buckets: 3\n" + "\tTotal number on inlined buckets: 2 (66%)\n" + "\tBytes used for inlined buckets: 236 (11%)\n" // Run the command. m := NewMain() if err := m.Run("stats", db.Path()); err != nil { t.Fatal(err) } else if m.Stdout.String() != exp { t.Fatalf("unexpected stdout:\n\n%s", m.Stdout.String()) } } // Ensure the "buckets" command can print a list of buckets. func TestBucketsCommand_Run(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { for _, name := range []string{"foo", "bar", "baz"} { _, err := tx.CreateBucket([]byte(name)) if err != nil { return err } } return nil }); err != nil { t.Fatal(err) } db.Close() defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) expected := "bar\nbaz\nfoo\n" // Run the command. m := NewMain() if err := m.Run("buckets", db.Path()); err != nil { t.Fatal(err) } else if actual := m.Stdout.String(); actual != expected { t.Fatalf("unexpected stdout:\n\n%s", actual) } } // Ensure the "keys" command can print a list of keys for a bucket. func TestKeysCommand_Run(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { for _, name := range []string{"foo", "bar"} { b, err := tx.CreateBucket([]byte(name)) if err != nil { return err } for i := 0; i < 3; i++ { key := fmt.Sprintf("%s-%d", name, i) if err := b.Put([]byte(key), []byte{0}); err != nil { return err } } } return nil }); err != nil { t.Fatal(err) } db.Close() defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) expected := "foo-0\nfoo-1\nfoo-2\n" // Run the command. m := NewMain() if err := m.Run("keys", db.Path(), "foo"); err != nil { t.Fatal(err) } else if actual := m.Stdout.String(); actual != expected { t.Fatalf("unexpected stdout:\n\n%s", actual) } } // Ensure the "get" command can print the value of a key in a bucket. func TestGetCommand_Run(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { for _, name := range []string{"foo", "bar"} { b, err := tx.CreateBucket([]byte(name)) if err != nil { return err } for i := 0; i < 3; i++ { key := fmt.Sprintf("%s-%d", name, i) val := fmt.Sprintf("val-%s-%d", name, i) if err := b.Put([]byte(key), []byte(val)); err != nil { return err } } } return nil }); err != nil { t.Fatal(err) } db.Close() defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) expected := "val-foo-1\n" // Run the command. m := NewMain() if err := m.Run("get", db.Path(), "foo", "foo-1"); err != nil { t.Fatal(err) } else if actual := m.Stdout.String(); actual != expected { t.Fatalf("unexpected stdout:\n\n%s", actual) } } // Ensure the "pages" command neither panic, nor change the db file. func TestPagesCommand_Run(t *testing.T) { db := btesting.MustCreateDB(t) err := db.Update(func(tx *bolt.Tx) error { for _, name := range []string{"foo", "bar"} { b, err := tx.CreateBucket([]byte(name)) if err != nil { return err } for i := 0; i < 3; i++ { key := fmt.Sprintf("%s-%d", name, i) val := fmt.Sprintf("val-%s-%d", name, i) if err := b.Put([]byte(key), []byte(val)); err != nil { return err } } } return nil }) require.NoError(t, err) db.Close() defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) // Run the command. m := NewMain() err = m.Run("pages", db.Path()) require.NoError(t, err) } // Main represents a test wrapper for main.Main that records output. type Main struct { *main.Main Stdin bytes.Buffer Stdout bytes.Buffer Stderr bytes.Buffer } // NewMain returns a new instance of Main. func NewMain() *Main { m := &Main{Main: main.NewMain()} m.Main.Stdin = &m.Stdin m.Main.Stdout = &m.Stdout m.Main.Stderr = &m.Stderr return m } func TestCompactCommand_Run(t *testing.T) { dstdb := btesting.MustCreateDB(t) dstdb.Close() // fill the db db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { n := 2 + rand.Intn(5) for i := 0; i < n; i++ { k := []byte(fmt.Sprintf("b%d", i)) b, err := tx.CreateBucketIfNotExists(k) if err != nil { return err } if err := b.SetSequence(uint64(i)); err != nil { return err } if err := fillBucket(b, append(k, '.')); err != nil { return err } } return nil }); err != nil { t.Fatal(err) } // make the db grow by adding large values, and delete them. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("large_vals")) if err != nil { return err } n := 5 + rand.Intn(5) for i := 0; i < n; i++ { v := make([]byte, 1000*1000*(1+rand.Intn(5))) _, err := crypto.Read(v) if err != nil { return err } if err := b.Put([]byte(fmt.Sprintf("l%d", i)), v); err != nil { return err } } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { c := tx.Bucket([]byte("large_vals")).Cursor() for k, _ := c.First(); k != nil; k, _ = c.Next() { if err := c.Delete(); err != nil { return err } } return tx.DeleteBucket([]byte("large_vals")) }); err != nil { t.Fatal(err) } db.Close() dbChk, err := chkdb(db.Path()) if err != nil { t.Fatal(err) } m := NewMain() if err := m.Run("compact", "-o", dstdb.Path(), db.Path()); err != nil { t.Fatal(err) } dbChkAfterCompact, err := chkdb(db.Path()) if err != nil { t.Fatal(err) } dstdbChk, err := chkdb(dstdb.Path()) if err != nil { t.Fatal(err) } if !bytes.Equal(dbChk, dbChkAfterCompact) { t.Error("the original db has been touched") } if !bytes.Equal(dbChk, dstdbChk) { t.Error("the compacted db data isn't the same than the original db") } } func TestCommands_Run_NoArgs(t *testing.T) { testCases := []struct { name string cmd string expErr error }{ { name: "get", cmd: "get", expErr: main.ErrNotEnoughArgs, }, { name: "keys", cmd: "keys", expErr: main.ErrNotEnoughArgs, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { m := NewMain() err := m.Run(tc.cmd) require.ErrorIs(t, err, main.ErrNotEnoughArgs) }) } } func fillBucket(b *bolt.Bucket, prefix []byte) error { n := 10 + rand.Intn(50) for i := 0; i < n; i++ { v := make([]byte, 10*(1+rand.Intn(4))) _, err := crypto.Read(v) if err != nil { return err } k := append(prefix, []byte(fmt.Sprintf("k%d", i))...) if err := b.Put(k, v); err != nil { return err } } // limit depth of subbuckets s := 2 + rand.Intn(4) if len(prefix) > (2*s + 1) { return nil } n = 1 + rand.Intn(3) for i := 0; i < n; i++ { k := append(prefix, []byte(fmt.Sprintf("b%d", i))...) sb, err := b.CreateBucket(k) if err != nil { return err } if err := fillBucket(sb, append(k, '.')); err != nil { return err } } return nil } func chkdb(path string) ([]byte, error) { db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) if err != nil { return nil, err } defer db.Close() var buf bytes.Buffer err = db.View(func(tx *bolt.Tx) error { return tx.ForEach(func(name []byte, b *bolt.Bucket) error { return walkBucket(b, name, nil, &buf) }) }) if err != nil { return nil, err } return buf.Bytes(), nil } func walkBucket(parent *bolt.Bucket, k []byte, v []byte, w io.Writer) error { if _, err := fmt.Fprintf(w, "%d:%x=%x\n", parent.Sequence(), k, v); err != nil { return err } // not a bucket, exit. if v != nil { return nil } return parent.ForEach(func(k, v []byte) error { if v == nil { return walkBucket(parent.Bucket(k), k, nil, w) } return walkBucket(parent, k, v, w) }) } func dbData(t *testing.T, filePath string) []byte { data, err := os.ReadFile(filePath) require.NoError(t, err) return data } func requireDBNoChange(t *testing.T, oldData []byte, filePath string) { newData, err := os.ReadFile(filePath) require.NoError(t, err) noChange := bytes.Equal(oldData, newData) require.True(t, noChange) } bbolt-1.3.10/cmd/bbolt/page_command.go000066400000000000000000000161021461507540700175150ustar00rootroot00000000000000package main import ( "bytes" "flag" "fmt" "io" "os" "strings" "go.etcd.io/bbolt/internal/guts_cli" ) // pageCommand represents the "page" command execution. type pageCommand struct { baseCommand } // newPageCommand returns a pageCommand. func newPageCommand(m *Main) *pageCommand { c := &pageCommand{} c.baseCommand = m.baseCommand return c } // Run executes the command. func (cmd *pageCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") all := fs.Bool("all", false, "list all pages") formatValue := fs.String("format-value", "auto", "One of: "+FORMAT_MODES+" . Applies to values on the leaf page.") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Require database path and page id. path := fs.Arg(0) if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound } if !*all { // Read page ids. pageIDs, err := stringToPages(fs.Args()[1:]) if err != nil { return err } else if len(pageIDs) == 0 { return ErrPageIDRequired } cmd.printPages(pageIDs, path, formatValue) } else { cmd.printAllPages(path, formatValue) } return nil } func (cmd *pageCommand) printPages(pageIDs []uint64, path string, formatValue *string) { // Print each page listed. for i, pageID := range pageIDs { // Print a separator. if i > 0 { fmt.Fprintln(cmd.Stdout, "===============================================") } _, err2 := cmd.printPage(path, pageID, *formatValue) if err2 != nil { fmt.Fprintf(cmd.Stdout, "Prining page %d failed: %s. Continuuing...\n", pageID, err2) } } } func (cmd *pageCommand) printAllPages(path string, formatValue *string) { _, hwm, err := guts_cli.ReadPageAndHWMSize(path) if err != nil { fmt.Fprintf(cmd.Stdout, "cannot read number of pages: %v", err) } // Print each page listed. for pageID := uint64(0); pageID < uint64(hwm); { // Print a separator. if pageID > 0 { fmt.Fprintln(cmd.Stdout, "===============================================") } overflow, err2 := cmd.printPage(path, pageID, *formatValue) if err2 != nil { fmt.Fprintf(cmd.Stdout, "Prining page %d failed: %s. Continuuing...\n", pageID, err2) pageID++ } else { pageID += uint64(overflow) + 1 } } } // printPage prints given page to cmd.Stdout and returns error or number of interpreted pages. func (cmd *pageCommand) printPage(path string, pageID uint64, formatValue string) (numPages uint32, reterr error) { defer func() { if err := recover(); err != nil { reterr = fmt.Errorf("%s", err) } }() // Retrieve page info and page size. p, buf, err := guts_cli.ReadPage(path, pageID) if err != nil { return 0, err } // Print basic page info. fmt.Fprintf(cmd.Stdout, "Page ID: %d\n", p.Id()) fmt.Fprintf(cmd.Stdout, "Page Type: %s\n", p.Type()) fmt.Fprintf(cmd.Stdout, "Total Size: %d bytes\n", len(buf)) fmt.Fprintf(cmd.Stdout, "Overflow pages: %d\n", p.Overflow()) // Print type-specific data. switch p.Type() { case "meta": err = cmd.PrintMeta(cmd.Stdout, buf) case "leaf": err = cmd.PrintLeaf(cmd.Stdout, buf, formatValue) case "branch": err = cmd.PrintBranch(cmd.Stdout, buf) case "freelist": err = cmd.PrintFreelist(cmd.Stdout, buf) } if err != nil { return 0, err } return p.Overflow(), nil } // PrintMeta prints the data from the meta page. func (cmd *pageCommand) PrintMeta(w io.Writer, buf []byte) error { m := guts_cli.LoadPageMeta(buf) m.Print(w) return nil } // PrintLeaf prints the data for a leaf page. func (cmd *pageCommand) PrintLeaf(w io.Writer, buf []byte, formatValue string) error { p := guts_cli.LoadPage(buf) // Print number of items. fmt.Fprintf(w, "Item Count: %d\n", p.Count()) fmt.Fprintf(w, "\n") // Print each key/value. for i := uint16(0); i < p.Count(); i++ { e := p.LeafPageElement(i) // Format key as string. var k string if isPrintable(string(e.Key())) { k = fmt.Sprintf("%q", string(e.Key())) } else { k = fmt.Sprintf("%x", string(e.Key())) } // Format value as string. var v string if e.IsBucketEntry() { b := e.Bucket() v = b.String() } else { var err error v, err = formatBytes(e.Value(), formatValue) if err != nil { return err } } fmt.Fprintf(w, "%s: %s\n", k, v) } fmt.Fprintf(w, "\n") return nil } // PrintBranch prints the data for a leaf page. func (cmd *pageCommand) PrintBranch(w io.Writer, buf []byte) error { p := guts_cli.LoadPage(buf) // Print number of items. fmt.Fprintf(w, "Item Count: %d\n", p.Count()) fmt.Fprintf(w, "\n") // Print each key/value. for i := uint16(0); i < p.Count(); i++ { e := p.BranchPageElement(i) // Format key as string. var k string if isPrintable(string(e.Key())) { k = fmt.Sprintf("%q", string(e.Key())) } else { k = fmt.Sprintf("%x", string(e.Key())) } fmt.Fprintf(w, "%s: \n", k, e.PgId()) } fmt.Fprintf(w, "\n") return nil } // PrintFreelist prints the data for a freelist page. func (cmd *pageCommand) PrintFreelist(w io.Writer, buf []byte) error { p := guts_cli.LoadPage(buf) // Print number of items. fmt.Fprintf(w, "Item Count: %d\n", p.FreelistPageCount()) fmt.Fprintf(w, "Overflow: %d\n", p.Overflow()) fmt.Fprintf(w, "\n") // Print each page in the freelist. ids := p.FreelistPagePages() for _, ids := range ids { fmt.Fprintf(w, "%d\n", ids) } fmt.Fprintf(w, "\n") return nil } // PrintPage prints a given page as hexadecimal. func (cmd *pageCommand) PrintPage(w io.Writer, r io.ReaderAt, pageID int, pageSize int) error { const bytesPerLineN = 16 // Read page into buffer. buf := make([]byte, pageSize) addr := pageID * pageSize if n, err := r.ReadAt(buf, int64(addr)); err != nil { return err } else if n != pageSize { return io.ErrUnexpectedEOF } // Write out to writer in 16-byte lines. var prev []byte var skipped bool for offset := 0; offset < pageSize; offset += bytesPerLineN { // Retrieve current 16-byte line. line := buf[offset : offset+bytesPerLineN] isLastLine := (offset == (pageSize - bytesPerLineN)) // If it's the same as the previous line then print a skip. if bytes.Equal(line, prev) && !isLastLine { if !skipped { fmt.Fprintf(w, "%07x *\n", addr+offset) skipped = true } } else { // Print line as hexadecimal in 2-byte groups. fmt.Fprintf(w, "%07x %04x %04x %04x %04x %04x %04x %04x %04x\n", addr+offset, line[0:2], line[2:4], line[4:6], line[6:8], line[8:10], line[10:12], line[12:14], line[14:16], ) skipped = false } // Save the previous line. prev = line } fmt.Fprint(w, "\n") return nil } // Usage returns the help message. func (cmd *pageCommand) Usage() string { return strings.TrimLeft(` usage: bolt page PATH pageid [pageid...] or: bolt page --all PATH Additional options include: --all prints all pages (only skips pages that were considered successful overflow pages) --format-value=`+FORMAT_MODES+` (default: auto) prints values (on the leaf page) using the given format. Page prints one or more pages in human readable format. `, "\n") } bbolt-1.3.10/cmd/bbolt/surgery_commands.go000066400000000000000000000171511461507540700204710ustar00rootroot00000000000000package main import ( "errors" "flag" "fmt" "io" "os" "strconv" "strings" "go.etcd.io/bbolt/internal/guts_cli" "go.etcd.io/bbolt/internal/surgeon" ) // surgeryCommand represents the "surgery" command execution. type surgeryCommand struct { baseCommand srcPath string dstPath string } // newSurgeryCommand returns a SurgeryCommand. func newSurgeryCommand(m *Main) *surgeryCommand { c := &surgeryCommand{} c.baseCommand = m.baseCommand return c } // Run executes the `surgery` program. func (cmd *surgeryCommand) Run(args ...string) error { // Require a command at the beginning. if len(args) == 0 || strings.HasPrefix(args[0], "-") { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } // Execute command. switch args[0] { case "help": fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage case "revert-meta-page": return newRevertMetaPageCommand(cmd).Run(args[1:]...) case "copy-page": return newCopyPageCommand(cmd).Run(args[1:]...) case "clear-page": return newClearPageCommand(cmd).Run(args[1:]...) default: return ErrUnknownCommand } } func (cmd *surgeryCommand) parsePathsAndCopyFile(fs *flag.FlagSet) error { // Require database paths. cmd.srcPath = fs.Arg(0) if cmd.srcPath == "" { return ErrPathRequired } cmd.dstPath = fs.Arg(1) if cmd.dstPath == "" { return errors.New("output file required") } // Ensure source file exists. _, err := os.Stat(cmd.srcPath) if os.IsNotExist(err) { return ErrFileNotFound } else if err != nil { return err } // Ensure output file not exist. _, err = os.Stat(cmd.dstPath) if err == nil { return fmt.Errorf("output file %q already exists", cmd.dstPath) } else if !os.IsNotExist(err) { return err } // Copy database from SrcPath to DstPath if err := copyFile(cmd.srcPath, cmd.dstPath); err != nil { return fmt.Errorf("failed to copy file: %w", err) } return nil } func copyFile(srcPath, dstPath string) error { srcDB, err := os.Open(srcPath) if err != nil { return fmt.Errorf("failed to open source file %q: %w", srcPath, err) } defer srcDB.Close() dstDB, err := os.Create(dstPath) if err != nil { return fmt.Errorf("failed to create output file %q: %w", dstPath, err) } defer dstDB.Close() written, err := io.Copy(dstDB, srcDB) if err != nil { return fmt.Errorf("failed to copy database file from %q to %q: %w", srcPath, dstPath, err) } srcFi, err := srcDB.Stat() if err != nil { return fmt.Errorf("failed to get source file info %q: %w", srcPath, err) } initialSize := srcFi.Size() if initialSize != written { return fmt.Errorf("the byte copied (%q: %d) isn't equal to the initial db size (%q: %d)", dstPath, written, srcPath, initialSize) } return nil } // Usage returns the help message. func (cmd *surgeryCommand) Usage() string { return strings.TrimLeft(` Surgery is a command for performing low level update on bbolt databases. Usage: bbolt surgery command [arguments] The commands are: help print this screen clear-page clear all elements at the given pageId copy-page copy page from source pageId to target pageId revert-meta-page revert the meta page change made by the last transaction Use "bbolt surgery [command] -h" for more information about a command. `, "\n") } // revertMetaPageCommand represents the "surgery revert-meta-page" command execution. type revertMetaPageCommand struct { *surgeryCommand } // newRevertMetaPageCommand returns a revertMetaPageCommand. func newRevertMetaPageCommand(m *surgeryCommand) *revertMetaPageCommand { c := &revertMetaPageCommand{} c.surgeryCommand = m return c } // Run executes the command. func (cmd *revertMetaPageCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } if err := cmd.parsePathsAndCopyFile(fs); err != nil { return fmt.Errorf("revertMetaPageCommand failed to parse paths and copy file: %w", err) } // revert the meta page if err := surgeon.RevertMetaPage(cmd.dstPath); err != nil { return fmt.Errorf("revertMetaPageCommand failed: %w", err) } fmt.Fprintln(cmd.Stdout, "The meta page is reverted.") return nil } // Usage returns the help message. func (cmd *revertMetaPageCommand) Usage() string { return strings.TrimLeft(` usage: bolt surgery revert-meta-page SRC DST RevertMetaPage copies the database file at SRC to a newly created database file at DST. Afterwards, it reverts the meta page on the newly created database at DST. The original database is left untouched. `, "\n") } // copyPageCommand represents the "surgery copy-page" command execution. type copyPageCommand struct { *surgeryCommand } // newCopyPageCommand returns a copyPageCommand. func newCopyPageCommand(m *surgeryCommand) *copyPageCommand { c := ©PageCommand{} c.surgeryCommand = m return c } // Run executes the command. func (cmd *copyPageCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } if err := cmd.parsePathsAndCopyFile(fs); err != nil { return fmt.Errorf("copyPageCommand failed to parse paths and copy file: %w", err) } // Read page id. srcPageId, err := strconv.ParseUint(fs.Arg(2), 10, 64) if err != nil { return err } dstPageId, err := strconv.ParseUint(fs.Arg(3), 10, 64) if err != nil { return err } // copy the page if err := surgeon.CopyPage(cmd.dstPath, guts_cli.Pgid(srcPageId), guts_cli.Pgid(dstPageId)); err != nil { return fmt.Errorf("copyPageCommand failed: %w", err) } fmt.Fprintf(cmd.Stdout, "The page %d was copied to page %d\n", srcPageId, dstPageId) return nil } // Usage returns the help message. func (cmd *copyPageCommand) Usage() string { return strings.TrimLeft(` usage: bolt surgery copy-page SRC DST srcPageId dstPageid CopyPage copies the database file at SRC to a newly created database file at DST. Afterwards, it copies the page at srcPageId to the page at dstPageId in DST. The original database is left untouched. `, "\n") } // clearPageCommand represents the "surgery clear-page" command execution. type clearPageCommand struct { *surgeryCommand } // newClearPageCommand returns a clearPageCommand. func newClearPageCommand(m *surgeryCommand) *clearPageCommand { c := &clearPageCommand{} c.surgeryCommand = m return c } // Run executes the command. func (cmd *clearPageCommand) Run(args ...string) error { // Parse flags. fs := flag.NewFlagSet("", flag.ContinueOnError) help := fs.Bool("h", false, "") if err := fs.Parse(args); err != nil { return err } else if *help { fmt.Fprintln(cmd.Stderr, cmd.Usage()) return ErrUsage } if err := cmd.parsePathsAndCopyFile(fs); err != nil { return fmt.Errorf("clearPageCommand failed to parse paths and copy file: %w", err) } // Read page id. pageId, err := strconv.ParseUint(fs.Arg(2), 10, 64) if err != nil { return err } if err := surgeon.ClearPage(cmd.dstPath, guts_cli.Pgid(pageId)); err != nil { return fmt.Errorf("clearPageCommand failed: %w", err) } fmt.Fprintf(cmd.Stdout, "Page (%d) was cleared\n", pageId) return nil } // Usage returns the help message. func (cmd *clearPageCommand) Usage() string { return strings.TrimLeft(` usage: bolt surgery clear-page SRC DST pageId ClearPage copies the database file at SRC to a newly created database file at DST. Afterwards, it clears all elements in the page at pageId in DST. The original database is left untouched. `, "\n") } bbolt-1.3.10/cmd/bbolt/surgery_commands_test.go000066400000000000000000000102331461507540700215220ustar00rootroot00000000000000package main_test import ( "fmt" "os" "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" "go.etcd.io/bbolt/internal/guts_cli" ) func TestSurgery_RevertMetaPage(t *testing.T) { pageSize := 4096 db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize}) srcPath := db.Path() defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) srcFile, err := os.Open(srcPath) require.NoError(t, err) defer srcFile.Close() // Read both meta0 and meta1 from srcFile srcBuf0 := readPage(t, srcPath, 0, pageSize) srcBuf1 := readPage(t, srcPath, 1, pageSize) meta0Page := guts_cli.LoadPageMeta(srcBuf0) meta1Page := guts_cli.LoadPageMeta(srcBuf1) // Get the non-active meta page nonActiveSrcBuf := srcBuf0 nonActiveMetaPageId := 0 if meta0Page.Txid() > meta1Page.Txid() { nonActiveSrcBuf = srcBuf1 nonActiveMetaPageId = 1 } t.Logf("non active meta page id: %d", nonActiveMetaPageId) // revert the meta page dstPath := filepath.Join(t.TempDir(), "dstdb") m := NewMain() err = m.Run("surgery", "revert-meta-page", srcPath, dstPath) require.NoError(t, err) // read both meta0 and meta1 from dst file dstBuf0 := readPage(t, dstPath, 0, pageSize) dstBuf1 := readPage(t, dstPath, 1, pageSize) // check result. Note we should skip the page ID assert.Equal(t, pageDataWithoutPageId(nonActiveSrcBuf), pageDataWithoutPageId(dstBuf0)) assert.Equal(t, pageDataWithoutPageId(nonActiveSrcBuf), pageDataWithoutPageId(dstBuf1)) } func TestSurgery_CopyPage(t *testing.T) { pageSize := 4096 db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize}) srcPath := db.Path() // Insert some sample data t.Log("Insert some sample data") err := db.Fill([]byte("data"), 1, 20, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 10) }, ) require.NoError(t, err) defer requireDBNoChange(t, dbData(t, srcPath), srcPath) // copy page 3 to page 2 t.Log("copy page 3 to page 2") dstPath := filepath.Join(t.TempDir(), "dstdb") m := NewMain() err = m.Run("surgery", "copy-page", srcPath, dstPath, "3", "2") require.NoError(t, err) // The page 2 should have exactly the same data as page 3. t.Log("Verify result") srcPageId3Data := readPage(t, srcPath, 3, pageSize) dstPageId3Data := readPage(t, dstPath, 3, pageSize) dstPageId2Data := readPage(t, dstPath, 2, pageSize) assert.Equal(t, srcPageId3Data, dstPageId3Data) assert.Equal(t, pageDataWithoutPageId(srcPageId3Data), pageDataWithoutPageId(dstPageId2Data)) } // TODO(ahrtr): add test case below for `surgery clear-page` command: // 1. The page is a branch page. All its children should become free pages. func TestSurgery_ClearPage(t *testing.T) { pageSize := 4096 db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize}) srcPath := db.Path() // Insert some sample data t.Log("Insert some sample data") err := db.Fill([]byte("data"), 1, 20, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 10) }, ) require.NoError(t, err) defer requireDBNoChange(t, dbData(t, srcPath), srcPath) // clear page 3 t.Log("clear page 3") dstPath := filepath.Join(t.TempDir(), "dstdb") m := NewMain() err = m.Run("surgery", "clear-page", srcPath, dstPath, "3") require.NoError(t, err) // The page 2 should have exactly the same data as page 3. t.Log("Verify result") dstPageId3Data := readPage(t, dstPath, 3, pageSize) p := guts_cli.LoadPage(dstPageId3Data) assert.Equal(t, uint16(0), p.Count()) assert.Equal(t, uint32(0), p.Overflow()) } func readPage(t *testing.T, path string, pageId int, pageSize int) []byte { dbFile, err := os.Open(path) require.NoError(t, err) defer dbFile.Close() fi, err := dbFile.Stat() require.NoError(t, err) require.GreaterOrEqual(t, fi.Size(), int64((pageId+1)*pageSize)) buf := make([]byte, pageSize) byteRead, err := dbFile.ReadAt(buf, int64(pageId*pageSize)) require.NoError(t, err) require.Equal(t, pageSize, byteRead) return buf } func pageDataWithoutPageId(buf []byte) []byte { return buf[8:] } bbolt-1.3.10/compact.go000066400000000000000000000061141461507540700146660ustar00rootroot00000000000000package bbolt // Compact will create a copy of the source DB and in the destination DB. This may // reclaim space that the source database no longer has use for. txMaxSize can be // used to limit the transactions size of this process and may trigger intermittent // commits. A value of zero will ignore transaction sizes. // TODO: merge with: https://github.com/etcd-io/etcd/blob/b7f0f52a16dbf83f18ca1d803f7892d750366a94/mvcc/backend/backend.go#L349 func Compact(dst, src *DB, txMaxSize int64) error { // commit regularly, or we'll run out of memory for large datasets if using one transaction. var size int64 tx, err := dst.Begin(true) if err != nil { return err } defer func() { if tempErr := tx.Rollback(); tempErr != nil { err = tempErr } }() if err := walk(src, func(keys [][]byte, k, v []byte, seq uint64) error { // On each key/value, check if we have exceeded tx size. sz := int64(len(k) + len(v)) if size+sz > txMaxSize && txMaxSize != 0 { // Commit previous transaction. if err := tx.Commit(); err != nil { return err } // Start new transaction. tx, err = dst.Begin(true) if err != nil { return err } size = 0 } size += sz // Create bucket on the root transaction if this is the first level. nk := len(keys) if nk == 0 { bkt, err := tx.CreateBucket(k) if err != nil { return err } if err := bkt.SetSequence(seq); err != nil { return err } return nil } // Create buckets on subsequent levels, if necessary. b := tx.Bucket(keys[0]) if nk > 1 { for _, k := range keys[1:] { b = b.Bucket(k) } } // Fill the entire page for best compaction. b.FillPercent = 1.0 // If there is no value then this is a bucket call. if v == nil { bkt, err := b.CreateBucket(k) if err != nil { return err } if err := bkt.SetSequence(seq); err != nil { return err } return nil } // Otherwise treat it as a key/value pair. return b.Put(k, v) }); err != nil { return err } err = tx.Commit() return err } // walkFunc is the type of the function called for keys (buckets and "normal" // values) discovered by Walk. keys is the list of keys to descend to the bucket // owning the discovered key/value pair k/v. type walkFunc func(keys [][]byte, k, v []byte, seq uint64) error // walk walks recursively the bolt database db, calling walkFn for each key it finds. func walk(db *DB, walkFn walkFunc) error { return db.View(func(tx *Tx) error { return tx.ForEach(func(name []byte, b *Bucket) error { return walkBucket(b, nil, name, nil, b.Sequence(), walkFn) }) }) } func walkBucket(b *Bucket, keypath [][]byte, k, v []byte, seq uint64, fn walkFunc) error { // Execute callback. if err := fn(keypath, k, v, seq); err != nil { return err } // If this is not a bucket then stop. if v != nil { return nil } // Iterate over each child key/value. keypath = append(keypath, k) return b.ForEach(func(k, v []byte) error { if v == nil { bkt := b.Bucket(k) return walkBucket(bkt, keypath, k, nil, bkt.Sequence(), fn) } return walkBucket(b, keypath, k, v, b.Sequence(), fn) }) } bbolt-1.3.10/concurrent_test.go000066400000000000000000000561701461507540700164700ustar00rootroot00000000000000package bbolt_test import ( "bytes" crand "crypto/rand" "encoding/hex" "encoding/json" "fmt" "io" mrand "math/rand" "os" "path/filepath" "sort" "strings" "sync" "testing" "time" "unicode/utf8" "github.com/stretchr/testify/require" "golang.org/x/sync/errgroup" bolt "go.etcd.io/bbolt" ) const ( bucketPrefix = "bucket" keyPrefix = "key" noopTxKey = "%magic-no-op-key%" // TestConcurrentCaseDuration is used as a env variable to specify the // concurrent test duration. testConcurrentCaseDuration = "TEST_CONCURRENT_CASE_DURATION" defaultConcurrentTestDuration = 30 * time.Second ) type duration struct { min time.Duration max time.Duration } type bytesRange struct { min int max int } type operationChance struct { operation OperationType chance int } type concurrentConfig struct { bucketCount int keyCount int workInterval duration operationRatio []operationChance readInterval duration // only used by readOperation noopWriteRatio int // only used by writeOperation writeBytes bytesRange // only used by writeOperation } /* TestConcurrentGenericReadAndWrite verifies: 1. Repeatable read: a read transaction should always see the same data view during its lifecycle. 2. Any data written by a writing transaction should be visible to any following reading transactions (with txid >= previous writing txid). 3. The txid should never decrease. */ func TestConcurrentGenericReadAndWrite(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } testDuration := concurrentTestDuration(t) conf := concurrentConfig{ bucketCount: 5, keyCount: 10000, workInterval: duration{}, operationRatio: []operationChance{ {operation: Read, chance: 60}, {operation: Write, chance: 20}, {operation: Delete, chance: 20}, }, readInterval: duration{ min: 50 * time.Millisecond, max: 100 * time.Millisecond, }, noopWriteRatio: 20, writeBytes: bytesRange{ min: 200, max: 16000, }, } testCases := []struct { name string workerCount int conf concurrentConfig testDuration time.Duration }{ { name: "1 worker", workerCount: 1, conf: conf, testDuration: testDuration, }, { name: "10 workers", workerCount: 10, conf: conf, testDuration: testDuration, }, { name: "50 workers", workerCount: 50, conf: conf, testDuration: testDuration, }, { name: "100 workers", workerCount: 100, conf: conf, testDuration: testDuration, }, { name: "200 workers", workerCount: 200, conf: conf, testDuration: testDuration, }, } for _, tc := range testCases { tc := tc t.Run(tc.name, func(t *testing.T) { concurrentReadAndWrite(t, tc.workerCount, tc.conf, tc.testDuration) }) } } func concurrentTestDuration(t *testing.T) time.Duration { durationInEnv := strings.ToLower(os.Getenv(testConcurrentCaseDuration)) if durationInEnv == "" { t.Logf("%q not set, defaults to %s", testConcurrentCaseDuration, defaultConcurrentTestDuration) return defaultConcurrentTestDuration } d, err := time.ParseDuration(durationInEnv) if err != nil { t.Logf("Failed to parse %s=%s, error: %v, defaults to %s", testConcurrentCaseDuration, durationInEnv, err, defaultConcurrentTestDuration) return defaultConcurrentTestDuration } t.Logf("Concurrent test duration set by %s=%s", testConcurrentCaseDuration, d) return d } func concurrentReadAndWrite(t *testing.T, workerCount int, conf concurrentConfig, testDuration time.Duration) { t.Log("Preparing db.") db := mustCreateDB(t, nil) defer db.Close() err := db.Update(func(tx *bolt.Tx) error { for i := 0; i < conf.bucketCount; i++ { if _, err := tx.CreateBucketIfNotExists(bucketName(i)); err != nil { return err } } return nil }) require.NoError(t, err) var records historyRecords // t.Failed() returns false during panicking. We need to forcibly // save data on panicking. // Refer to: https://github.com/golang/go/issues/49929 panicked := true defer func() { t.Log("Save data if failed.") saveDataIfFailed(t, db, records, panicked) }() t.Log("Starting workers.") records = runWorkers(t, db, workerCount, conf, testDuration) t.Log("Analyzing the history records.") if err := validateSequential(records); err != nil { t.Errorf("The history records are not sequential:\n %v", err) } t.Log("Checking database consistency.") if err := checkConsistency(t, db); err != nil { t.Errorf("The data isn't consistency: %v", err) } panicked = false // TODO (ahrtr): // 1. intentionally inject a random failpoint. } // mustCreateDB is created in place of `btesting.MustCreateDB`, and it's // only supposed to be used by the concurrent test case. The purpose is // to ensure the test case can be executed on old branches or versions, // e.g. `release-1.3` or `1.3.[5-7]`. func mustCreateDB(t *testing.T, o *bolt.Options) *bolt.DB { f := filepath.Join(t.TempDir(), "db") return mustOpenDB(t, f, o) } func mustReOpenDB(t *testing.T, db *bolt.DB, o *bolt.Options) *bolt.DB { f := db.Path() t.Logf("Closing bbolt DB at: %s", f) err := db.Close() require.NoError(t, err) return mustOpenDB(t, f, o) } func mustOpenDB(t *testing.T, dbPath string, o *bolt.Options) *bolt.DB { t.Logf("Opening bbolt DB at: %s", dbPath) if o == nil { o = bolt.DefaultOptions } freelistType := bolt.FreelistArrayType if env := os.Getenv("TEST_FREELIST_TYPE"); env == string(bolt.FreelistMapType) { freelistType = bolt.FreelistMapType } o.FreelistType = freelistType db, err := bolt.Open(dbPath, 0600, o) require.NoError(t, err) return db } func checkConsistency(t *testing.T, db *bolt.DB) error { return db.View(func(tx *bolt.Tx) error { cnt := 0 for err := range tx.Check() { t.Errorf("Consistency error: %v", err) cnt++ } if cnt > 0 { return fmt.Errorf("%d consistency errors found", cnt) } return nil }) } /* ********************************************************* Data structures and functions/methods for running concurrent workers, which execute different operations, including `Read`, `Write` and `Delete`. ********************************************************* */ func runWorkers(t *testing.T, db *bolt.DB, workerCount int, conf concurrentConfig, testDuration time.Duration) historyRecords { stopCh := make(chan struct{}, 1) errCh := make(chan error, workerCount) var mu sync.Mutex var rs historyRecords g := new(errgroup.Group) for i := 0; i < workerCount; i++ { w := &worker{ id: i, db: db, conf: conf, errCh: errCh, stopCh: stopCh, t: t, } g.Go(func() error { wrs, err := runWorker(t, w, errCh) mu.Lock() rs = append(rs, wrs...) mu.Unlock() return err }) } t.Logf("Keep all workers running for about %s.", testDuration) select { case <-time.After(testDuration): case <-errCh: } close(stopCh) t.Log("Waiting for all workers to finish.") if err := g.Wait(); err != nil { t.Errorf("Received error: %v", err) } return rs } func runWorker(t *testing.T, w *worker, errCh chan error) (historyRecords, error) { rs, err := w.run() if len(rs) > 0 && err == nil { if terr := validateIncrementalTxid(rs); terr != nil { txidErr := fmt.Errorf("[%s]: %w", w.name(), terr) t.Error(txidErr) errCh <- txidErr return rs, txidErr } } return rs, err } type worker struct { id int db *bolt.DB conf concurrentConfig errCh chan error stopCh chan struct{} t *testing.T } func (w *worker) name() string { return fmt.Sprintf("worker-%d", w.id) } func (w *worker) run() (historyRecords, error) { var rs historyRecords for { select { case <-w.stopCh: w.t.Logf("%q finished.", w.name()) return rs, nil default: } op := w.pickOperation() bucket, key := w.pickBucket(), w.pickKey() rec, err := executeOperation(op, w.db, bucket, key, w.conf) if err != nil { readErr := fmt.Errorf("[%s: %s]: %w", w.name(), op, err) w.t.Error(readErr) w.errCh <- readErr return rs, readErr } rs = append(rs, rec) if w.conf.workInterval != (duration{}) { time.Sleep(randomDurationInRange(w.conf.workInterval.min, w.conf.workInterval.max)) } } } func (w *worker) pickBucket() []byte { return bucketName(mrand.Intn(w.conf.bucketCount)) } func bucketName(index int) []byte { bucket := fmt.Sprintf("%s_%d", bucketPrefix, index) return []byte(bucket) } func (w *worker) pickKey() []byte { key := fmt.Sprintf("%s_%d", keyPrefix, mrand.Intn(w.conf.keyCount)) return []byte(key) } func (w *worker) pickOperation() OperationType { sum := 0 for _, op := range w.conf.operationRatio { sum += op.chance } roll := mrand.Int() % sum for _, op := range w.conf.operationRatio { if roll < op.chance { return op.operation } roll -= op.chance } panic("unexpected") } func executeOperation(op OperationType, db *bolt.DB, bucket []byte, key []byte, conf concurrentConfig) (historyRecord, error) { switch op { case Read: return executeRead(db, bucket, key, conf.readInterval) case Write: return executeWrite(db, bucket, key, conf.writeBytes, conf.noopWriteRatio) case Delete: return executeDelete(db, bucket, key) default: panic(fmt.Sprintf("unexpected operation type: %s", op)) } } func executeRead(db *bolt.DB, bucket []byte, key []byte, readInterval duration) (historyRecord, error) { var rec historyRecord err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket(bucket) initialVal := b.Get(key) time.Sleep(randomDurationInRange(readInterval.min, readInterval.max)) val := b.Get(key) if !bytes.Equal(initialVal, val) { return fmt.Errorf("read different values for the same key (%q), value1: %q, value2: %q", string(key), formatBytes(initialVal), formatBytes(val)) } clonedVal := make([]byte, len(val)) copy(clonedVal, val) rec = historyRecord{ OperationType: Read, Bucket: string(bucket), Key: string(key), Value: clonedVal, Txid: tx.ID(), } return nil }) return rec, err } func executeWrite(db *bolt.DB, bucket []byte, key []byte, writeBytes bytesRange, noopWriteRatio int) (historyRecord, error) { var rec historyRecord err := db.Update(func(tx *bolt.Tx) error { if mrand.Intn(100) < noopWriteRatio { // A no-op write transaction has two consequences: // 1. The txid increases by 1; // 2. Two meta pages point to the same root page. rec = historyRecord{ OperationType: Write, Bucket: string(bucket), Key: noopTxKey, Value: nil, Txid: tx.ID(), } return nil } b := tx.Bucket(bucket) valueBytes := randomIntInRange(writeBytes.min, writeBytes.max) v := make([]byte, valueBytes) if _, cErr := crand.Read(v); cErr != nil { return cErr } putErr := b.Put(key, v) if putErr == nil { rec = historyRecord{ OperationType: Write, Bucket: string(bucket), Key: string(key), Value: v, Txid: tx.ID(), } } return putErr }) return rec, err } func executeDelete(db *bolt.DB, bucket []byte, key []byte) (historyRecord, error) { var rec historyRecord err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket(bucket) deleteErr := b.Delete(key) if deleteErr == nil { rec = historyRecord{ OperationType: Delete, Bucket: string(bucket), Key: string(key), Txid: tx.ID(), } } return deleteErr }) return rec, err } func randomDurationInRange(min, max time.Duration) time.Duration { d := int64(max) - int64(min) d = int64(mrand.Intn(int(d))) + int64(min) return time.Duration(d) } func randomIntInRange(min, max int) int { return mrand.Intn(max-min) + min } func formatBytes(val []byte) string { if utf8.ValidString(string(val)) { return string(val) } return hex.EncodeToString(val) } /* ********************************************************* Functions for persisting test data, including db file and operation history ********************************************************* */ func saveDataIfFailed(t *testing.T, db *bolt.DB, rs historyRecords, force bool) { if t.Failed() || force { t.Log("Saving data...") dbPath := db.Path() if err := db.Close(); err != nil { t.Errorf("Failed to close db: %v", err) } backupPath := testResultsDirectory(t) backupDB(t, dbPath, backupPath) persistHistoryRecords(t, rs, backupPath) } } func backupDB(t *testing.T, srcPath string, dstPath string) { targetFile := filepath.Join(dstPath, "db.bak") t.Logf("Saving the DB file to %s", targetFile) err := copyFile(srcPath, targetFile) require.NoError(t, err) t.Logf("DB file saved to %s", targetFile) } func copyFile(srcPath, dstPath string) error { // Ensure source file exists. _, err := os.Stat(srcPath) if os.IsNotExist(err) { return fmt.Errorf("source file %q not found", srcPath) } else if err != nil { return err } // Ensure output file not exist. _, err = os.Stat(dstPath) if err == nil { return fmt.Errorf("output file %q already exists", dstPath) } else if !os.IsNotExist(err) { return err } srcDB, err := os.Open(srcPath) if err != nil { return fmt.Errorf("failed to open source file %q: %w", srcPath, err) } defer srcDB.Close() dstDB, err := os.Create(dstPath) if err != nil { return fmt.Errorf("failed to create output file %q: %w", dstPath, err) } defer dstDB.Close() written, err := io.Copy(dstDB, srcDB) if err != nil { return fmt.Errorf("failed to copy database file from %q to %q: %w", srcPath, dstPath, err) } srcFi, err := srcDB.Stat() if err != nil { return fmt.Errorf("failed to get source file info %q: %w", srcPath, err) } initialSize := srcFi.Size() if initialSize != written { return fmt.Errorf("the byte copied (%q: %d) isn't equal to the initial db size (%q: %d)", dstPath, written, srcPath, initialSize) } return nil } func persistHistoryRecords(t *testing.T, rs historyRecords, path string) { recordFilePath := filepath.Join(path, "history_records.json") t.Logf("Saving history records to %s", recordFilePath) recordFile, err := os.OpenFile(recordFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) require.NoError(t, err) defer recordFile.Close() encoder := json.NewEncoder(recordFile) for _, rec := range rs { err := encoder.Encode(rec) require.NoError(t, err) } } func testResultsDirectory(t *testing.T) string { resultsDirectory, ok := os.LookupEnv("RESULTS_DIR") var err error if !ok { resultsDirectory, err = os.MkdirTemp("", "*.db") require.NoError(t, err) } resultsDirectory, err = filepath.Abs(resultsDirectory) require.NoError(t, err) path, err := filepath.Abs(filepath.Join(resultsDirectory, strings.ReplaceAll(t.Name(), "/", "_"))) require.NoError(t, err) err = os.RemoveAll(path) require.NoError(t, err) err = os.MkdirAll(path, 0700) require.NoError(t, err) return path } /* ********************************************************* Data structures and functions for analyzing history records ********************************************************* */ type OperationType string const ( Read OperationType = "read" Write OperationType = "write" Delete OperationType = "delete" ) type historyRecord struct { OperationType OperationType `json:"operationType,omitempty"` Txid int `json:"txid,omitempty"` Bucket string `json:"bucket,omitempty"` Key string `json:"key,omitempty"` Value []byte `json:"value,omitempty"` } type historyRecords []historyRecord func (rs historyRecords) Len() int { return len(rs) } func (rs historyRecords) Less(i, j int) bool { // Sorted by (bucket, key) firstly: all records in the same // (bucket, key) are grouped together. bucketCmp := strings.Compare(rs[i].Bucket, rs[j].Bucket) if bucketCmp != 0 { return bucketCmp < 0 } keyCmp := strings.Compare(rs[i].Key, rs[j].Key) if keyCmp != 0 { return keyCmp < 0 } // Sorted by txid if rs[i].Txid != rs[j].Txid { return rs[i].Txid < rs[j].Txid } // Sorted by operation type: put `Read` after other operation types // if they operate on the same (bucket, key) and have the same txid. if rs[i].OperationType == Read { return false } return true } func (rs historyRecords) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] } func validateIncrementalTxid(rs historyRecords) error { lastTxid := rs[0].Txid for i := 1; i < len(rs); i++ { if (rs[i].OperationType == Read && rs[i].Txid < lastTxid) || (rs[i].OperationType != Read && rs[i].Txid <= lastTxid) { return fmt.Errorf("detected non-incremental txid(%d, %d) in %s mode", lastTxid, rs[i].Txid, rs[i].OperationType) } lastTxid = rs[i].Txid } return nil } func validateSequential(rs historyRecords) error { sort.Sort(rs) type bucketAndKey struct { bucket string key string } lastWriteKeyValueMap := make(map[bucketAndKey]*historyRecord) for _, rec := range rs { bk := bucketAndKey{ bucket: rec.Bucket, key: rec.Key, } if v, ok := lastWriteKeyValueMap[bk]; ok { if rec.OperationType == Write { v.Txid = rec.Txid if rec.Key != noopTxKey { v.Value = rec.Value } } else if rec.OperationType == Delete { delete(lastWriteKeyValueMap, bk) } else { if !bytes.Equal(v.Value, rec.Value) { return fmt.Errorf("readOperation[txid: %d, bucket: %s, key: %s] read %x, \nbut writer[txid: %d] wrote %x", rec.Txid, rec.Bucket, rec.Key, rec.Value, v.Txid, v.Value) } } } else { if rec.OperationType == Write && rec.Key != noopTxKey { lastWriteKeyValueMap[bk] = &historyRecord{ OperationType: Write, Bucket: rec.Bucket, Key: rec.Key, Value: rec.Value, Txid: rec.Txid, } } else if rec.OperationType == Read { if len(rec.Value) != 0 { return fmt.Errorf("expected the first readOperation[txid: %d, bucket: %s, key: %s] read nil, \nbut got %x", rec.Txid, rec.Bucket, rec.Key, rec.Value) } } } } return nil } /* TestConcurrentRepeatableRead verifies repeatable read. The case intentionally creates a scenario that read and write transactions are interleaved. It performs several writing operations after starting each long-running read transaction to ensure it has a larger txid than previous read transaction. It verifies that bbolt correctly releases free pages, and will not pollute (e.g. prematurely release) any pages which are still being used by any read transaction. */ func TestConcurrentRepeatableRead(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } testCases := []struct { name string noFreelistSync bool freelistType bolt.FreelistType }{ // [array] freelist { name: "sync array freelist", noFreelistSync: false, freelistType: bolt.FreelistArrayType, }, { name: "not sync array freelist", noFreelistSync: true, freelistType: bolt.FreelistArrayType, }, // [map] freelist { name: "sync map freelist", noFreelistSync: false, freelistType: bolt.FreelistMapType, }, { name: "not sync map freelist", noFreelistSync: true, freelistType: bolt.FreelistMapType, }, } for _, tc := range testCases { tc := tc t.Run(tc.name, func(t *testing.T) { t.Log("Preparing db.") var ( bucket = []byte("data") key = []byte("mykey") option = &bolt.Options{ PageSize: 4096, NoFreelistSync: tc.noFreelistSync, FreelistType: tc.freelistType, } ) db := mustCreateDB(t, option) defer func() { // The db will be reopened later, so put `db.Close()` in a function // to avoid premature evaluation of `db`. Note that the execution // of a deferred function is deferred to the moment the surrounding // function returns, but the function value and parameters to the // call are evaluated as usual and saved anew. db.Close() }() // Create lots of K/V to allocate some pages err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists(bucket) if err != nil { return err } for i := 0; i < 1000; i++ { k := fmt.Sprintf("key_%d", i) if err := b.Put([]byte(k), make([]byte, 1024)); err != nil { return err } } return nil }) require.NoError(t, err) // Remove all K/V to create some free pages err = db.Update(func(tx *bolt.Tx) error { b := tx.Bucket(bucket) for i := 0; i < 1000; i++ { k := fmt.Sprintf("key_%d", i) if err := b.Delete([]byte(k)); err != nil { return err } } return b.Put(key, []byte("randomValue")) }) require.NoError(t, err) // bbolt will not release free pages directly after committing // a writing transaction; instead all pages freed are putting // into a pending list. Accordingly, the free pages might not // be able to be reused by following writing transactions. So // we reopen the db to completely release all free pages. db = mustReOpenDB(t, db, option) var ( wg sync.WaitGroup longRunningReaderCount = 10 stopCh = make(chan struct{}) errCh = make(chan error, longRunningReaderCount) readInterval = duration{5 * time.Millisecond, 10 * time.Millisecond} writeOperationCountInBetween = 5 writeBytes = bytesRange{10, 20} testDuration = 10 * time.Second ) for i := 0; i < longRunningReaderCount; i++ { readWorkerName := fmt.Sprintf("reader_%d", i) t.Logf("Starting long running read operation: %s", readWorkerName) wg.Add(1) go func() { defer wg.Done() rErr := executeLongRunningRead(t, readWorkerName, db, bucket, key, readInterval, stopCh) if rErr != nil { errCh <- rErr } }() time.Sleep(500 * time.Millisecond) t.Logf("Perform %d write operations after starting a long running read operation", writeOperationCountInBetween) for j := 0; j < writeOperationCountInBetween; j++ { _, err := executeWrite(db, bucket, key, writeBytes, 0) require.NoError(t, err) } } t.Log("Perform lots of write operations to check whether the long running read operations will read dirty data") wg.Add(1) go func() { defer wg.Done() cnt := longRunningReaderCount * writeOperationCountInBetween for i := 0; i < cnt; i++ { select { case <-stopCh: return default: } _, err := executeWrite(db, bucket, key, writeBytes, 0) require.NoError(t, err) } }() t.Log("Waiting for result") select { case err := <-errCh: close(stopCh) t.Errorf("Detected dirty read: %v", err) case <-time.After(testDuration): close(stopCh) } wg.Wait() }) } } func executeLongRunningRead(t *testing.T, name string, db *bolt.DB, bucket []byte, key []byte, readInterval duration, stopCh chan struct{}) error { err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket(bucket) initialVal := b.Get(key) for { select { case <-stopCh: t.Logf("%q finished.", name) return nil default: } time.Sleep(randomDurationInRange(readInterval.min, readInterval.max)) val := b.Get(key) if !bytes.Equal(initialVal, val) { dirtyReadErr := fmt.Errorf("read different values for the same key (%q), value1: %q, value2: %q", string(key), formatBytes(initialVal), formatBytes(val)) return dirtyReadErr } } }) return err } bbolt-1.3.10/cursor.go000066400000000000000000000302311461507540700145520ustar00rootroot00000000000000package bbolt import ( "bytes" "fmt" "sort" ) // Cursor represents an iterator that can traverse over all key/value pairs in a bucket // in lexicographical order. // Cursors see nested buckets with value == nil. // Cursors can be obtained from a transaction and are valid as long as the transaction is open. // // Keys and values returned from the cursor are only valid for the life of the transaction. // // Changing data while traversing with a cursor may cause it to be invalidated // and return unexpected keys and/or values. You must reposition your cursor // after mutating data. type Cursor struct { bucket *Bucket stack []elemRef } // Bucket returns the bucket that this cursor was created from. func (c *Cursor) Bucket() *Bucket { return c.bucket } // First moves the cursor to the first item in the bucket and returns its key and value. // If the bucket is empty then a nil key and value are returned. // The returned key and value are only valid for the life of the transaction. func (c *Cursor) First() (key []byte, value []byte) { _assert(c.bucket.tx.db != nil, "tx closed") k, v, flags := c.first() if (flags & uint32(bucketLeafFlag)) != 0 { return k, nil } return k, v } func (c *Cursor) first() (key []byte, value []byte, flags uint32) { c.stack = c.stack[:0] p, n := c.bucket.pageNode(c.bucket.root) c.stack = append(c.stack, elemRef{page: p, node: n, index: 0}) c.goToFirstElementOnTheStack() // If we land on an empty page then move to the next value. // https://github.com/boltdb/bolt/issues/450 if c.stack[len(c.stack)-1].count() == 0 { c.next() } k, v, flags := c.keyValue() if (flags & uint32(bucketLeafFlag)) != 0 { return k, nil, flags } return k, v, flags } // Last moves the cursor to the last item in the bucket and returns its key and value. // If the bucket is empty then a nil key and value are returned. // The returned key and value are only valid for the life of the transaction. func (c *Cursor) Last() (key []byte, value []byte) { _assert(c.bucket.tx.db != nil, "tx closed") c.stack = c.stack[:0] p, n := c.bucket.pageNode(c.bucket.root) ref := elemRef{page: p, node: n} ref.index = ref.count() - 1 c.stack = append(c.stack, ref) c.last() // If this is an empty page (calling Delete may result in empty pages) // we call prev to find the last page that is not empty for len(c.stack) > 1 && c.stack[len(c.stack)-1].count() == 0 { c.prev() } if len(c.stack) == 0 { return nil, nil } k, v, flags := c.keyValue() if (flags & uint32(bucketLeafFlag)) != 0 { return k, nil } return k, v } // Next moves the cursor to the next item in the bucket and returns its key and value. // If the cursor is at the end of the bucket then a nil key and value are returned. // The returned key and value are only valid for the life of the transaction. func (c *Cursor) Next() (key []byte, value []byte) { _assert(c.bucket.tx.db != nil, "tx closed") k, v, flags := c.next() if (flags & uint32(bucketLeafFlag)) != 0 { return k, nil } return k, v } // Prev moves the cursor to the previous item in the bucket and returns its key and value. // If the cursor is at the beginning of the bucket then a nil key and value are returned. // The returned key and value are only valid for the life of the transaction. func (c *Cursor) Prev() (key []byte, value []byte) { _assert(c.bucket.tx.db != nil, "tx closed") k, v, flags := c.prev() if (flags & uint32(bucketLeafFlag)) != 0 { return k, nil } return k, v } // Seek moves the cursor to a given key using a b-tree search and returns it. // If the key does not exist then the next key is used. If no keys // follow, a nil key is returned. // The returned key and value are only valid for the life of the transaction. func (c *Cursor) Seek(seek []byte) (key []byte, value []byte) { _assert(c.bucket.tx.db != nil, "tx closed") k, v, flags := c.seek(seek) // If we ended up after the last element of a page then move to the next one. if ref := &c.stack[len(c.stack)-1]; ref.index >= ref.count() { k, v, flags = c.next() } if k == nil { return nil, nil } else if (flags & uint32(bucketLeafFlag)) != 0 { return k, nil } return k, v } // Delete removes the current key/value under the cursor from the bucket. // Delete fails if current key/value is a bucket or if the transaction is not writable. func (c *Cursor) Delete() error { if c.bucket.tx.db == nil { return ErrTxClosed } else if !c.bucket.Writable() { return ErrTxNotWritable } key, _, flags := c.keyValue() // Return an error if current value is a bucket. if (flags & bucketLeafFlag) != 0 { return ErrIncompatibleValue } c.node().del(key) return nil } // seek moves the cursor to a given key and returns it. // If the key does not exist then the next key is used. func (c *Cursor) seek(seek []byte) (key []byte, value []byte, flags uint32) { // Start from root page/node and traverse to correct page. c.stack = c.stack[:0] c.search(seek, c.bucket.root) // If this is a bucket then return a nil value. return c.keyValue() } // first moves the cursor to the first leaf element under the last page in the stack. func (c *Cursor) goToFirstElementOnTheStack() { for { // Exit when we hit a leaf page. var ref = &c.stack[len(c.stack)-1] if ref.isLeaf() { break } // Keep adding pages pointing to the first element to the stack. var pgId pgid if ref.node != nil { pgId = ref.node.inodes[ref.index].pgid } else { pgId = ref.page.branchPageElement(uint16(ref.index)).pgid } p, n := c.bucket.pageNode(pgId) c.stack = append(c.stack, elemRef{page: p, node: n, index: 0}) } } // last moves the cursor to the last leaf element under the last page in the stack. func (c *Cursor) last() { for { // Exit when we hit a leaf page. ref := &c.stack[len(c.stack)-1] if ref.isLeaf() { break } // Keep adding pages pointing to the last element in the stack. var pgId pgid if ref.node != nil { pgId = ref.node.inodes[ref.index].pgid } else { pgId = ref.page.branchPageElement(uint16(ref.index)).pgid } p, n := c.bucket.pageNode(pgId) var nextRef = elemRef{page: p, node: n} nextRef.index = nextRef.count() - 1 c.stack = append(c.stack, nextRef) } } // next moves to the next leaf element and returns the key and value. // If the cursor is at the last leaf element then it stays there and returns nil. func (c *Cursor) next() (key []byte, value []byte, flags uint32) { for { // Attempt to move over one element until we're successful. // Move up the stack as we hit the end of each page in our stack. var i int for i = len(c.stack) - 1; i >= 0; i-- { elem := &c.stack[i] if elem.index < elem.count()-1 { elem.index++ break } } // If we've hit the root page then stop and return. This will leave the // cursor on the last element of the last page. if i == -1 { return nil, nil, 0 } // Otherwise start from where we left off in the stack and find the // first element of the first leaf page. c.stack = c.stack[:i+1] c.goToFirstElementOnTheStack() // If this is an empty page then restart and move back up the stack. // https://github.com/boltdb/bolt/issues/450 if c.stack[len(c.stack)-1].count() == 0 { continue } return c.keyValue() } } // prev moves the cursor to the previous item in the bucket and returns its key and value. // If the cursor is at the beginning of the bucket then a nil key and value are returned. func (c *Cursor) prev() (key []byte, value []byte, flags uint32) { // Attempt to move back one element until we're successful. // Move up the stack as we hit the beginning of each page in our stack. for i := len(c.stack) - 1; i >= 0; i-- { elem := &c.stack[i] if elem.index > 0 { elem.index-- break } // If we've hit the beginning, we should stop moving the cursor, // and stay at the first element, so that users can continue to // iterate over the elements in reverse direction by calling `Next`. // We should return nil in such case. // Refer to https://github.com/etcd-io/bbolt/issues/733 if len(c.stack) == 1 { c.first() return nil, nil, 0 } c.stack = c.stack[:i] } // If we've hit the end then return nil. if len(c.stack) == 0 { return nil, nil, 0 } // Move down the stack to find the last element of the last leaf under this branch. c.last() return c.keyValue() } // search recursively performs a binary search against a given page/node until it finds a given key. func (c *Cursor) search(key []byte, pgId pgid) { p, n := c.bucket.pageNode(pgId) if p != nil && (p.flags&(branchPageFlag|leafPageFlag)) == 0 { panic(fmt.Sprintf("invalid page type: %d: %x", p.id, p.flags)) } e := elemRef{page: p, node: n} c.stack = append(c.stack, e) // If we're on a leaf page/node then find the specific node. if e.isLeaf() { c.nsearch(key) return } if n != nil { c.searchNode(key, n) return } c.searchPage(key, p) } func (c *Cursor) searchNode(key []byte, n *node) { var exact bool index := sort.Search(len(n.inodes), func(i int) bool { // TODO(benbjohnson): Optimize this range search. It's a bit hacky right now. // sort.Search() finds the lowest index where f() != -1 but we need the highest index. ret := bytes.Compare(n.inodes[i].key, key) if ret == 0 { exact = true } return ret != -1 }) if !exact && index > 0 { index-- } c.stack[len(c.stack)-1].index = index // Recursively search to the next page. c.search(key, n.inodes[index].pgid) } func (c *Cursor) searchPage(key []byte, p *page) { // Binary search for the correct range. inodes := p.branchPageElements() var exact bool index := sort.Search(int(p.count), func(i int) bool { // TODO(benbjohnson): Optimize this range search. It's a bit hacky right now. // sort.Search() finds the lowest index where f() != -1 but we need the highest index. ret := bytes.Compare(inodes[i].key(), key) if ret == 0 { exact = true } return ret != -1 }) if !exact && index > 0 { index-- } c.stack[len(c.stack)-1].index = index // Recursively search to the next page. c.search(key, inodes[index].pgid) } // nsearch searches the leaf node on the top of the stack for a key. func (c *Cursor) nsearch(key []byte) { e := &c.stack[len(c.stack)-1] p, n := e.page, e.node // If we have a node then search its inodes. if n != nil { index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, key) != -1 }) e.index = index return } // If we have a page then search its leaf elements. inodes := p.leafPageElements() index := sort.Search(int(p.count), func(i int) bool { return bytes.Compare(inodes[i].key(), key) != -1 }) e.index = index } // keyValue returns the key and value of the current leaf element. func (c *Cursor) keyValue() ([]byte, []byte, uint32) { ref := &c.stack[len(c.stack)-1] // If the cursor is pointing to the end of page/node then return nil. if ref.count() == 0 || ref.index >= ref.count() { return nil, nil, 0 } // Retrieve value from node. if ref.node != nil { inode := &ref.node.inodes[ref.index] return inode.key, inode.value, inode.flags } // Or retrieve value from page. elem := ref.page.leafPageElement(uint16(ref.index)) return elem.key(), elem.value(), elem.flags } // node returns the node that the cursor is currently positioned on. func (c *Cursor) node() *node { _assert(len(c.stack) > 0, "accessing a node with a zero-length cursor stack") // If the top of the stack is a leaf node then just return it. if ref := &c.stack[len(c.stack)-1]; ref.node != nil && ref.isLeaf() { return ref.node } // Start from root and traverse down the hierarchy. var n = c.stack[0].node if n == nil { n = c.bucket.node(c.stack[0].page.id, nil) } for _, ref := range c.stack[:len(c.stack)-1] { _assert(!n.isLeaf, "expected branch node") n = n.childAt(ref.index) } _assert(n.isLeaf, "expected leaf node") return n } // elemRef represents a reference to an element on a given page/node. type elemRef struct { page *page node *node index int } // isLeaf returns whether the ref is pointing at a leaf page/node. func (r *elemRef) isLeaf() bool { if r.node != nil { return r.node.isLeaf } return (r.page.flags & leafPageFlag) != 0 } // count returns the number of inodes or page elements. func (r *elemRef) count() int { if r.node != nil { return len(r.node.inodes) } return int(r.page.count) } bbolt-1.3.10/cursor_test.go000066400000000000000000000554671461507540700156330ustar00rootroot00000000000000package bbolt_test import ( "bytes" "encoding/binary" "fmt" "log" "os" "reflect" "sort" "testing" "testing/quick" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" ) // TestCursor_RepeatOperations verifies that a cursor can continue to // iterate over all elements in reverse direction when it has already // reached to the end or beginning. // Refer to https://github.com/etcd-io/bbolt/issues/733 func TestCursor_RepeatOperations(t *testing.T) { testCases := []struct { name string testFunc func(t2 *testing.T, bucket *bolt.Bucket) }{ { name: "Repeat NextPrevNext", testFunc: testRepeatCursorOperations_NextPrevNext, }, { name: "Repeat PrevNextPrev", testFunc: testRepeatCursorOperations_PrevNextPrev, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096}) bucketName := []byte("data") _ = db.Update(func(tx *bolt.Tx) error { b, _ := tx.CreateBucketIfNotExists(bucketName) testCursorRepeatOperations_PrepareData(t, b) return nil }) _ = db.View(func(tx *bolt.Tx) error { b := tx.Bucket(bucketName) tc.testFunc(t, b) return nil }) }) } } func testCursorRepeatOperations_PrepareData(t *testing.T, b *bolt.Bucket) { // ensure we have at least one branch page. for i := 0; i < 1000; i++ { k := []byte(fmt.Sprintf("%05d", i)) err := b.Put(k, k) require.NoError(t, err) } } func testRepeatCursorOperations_NextPrevNext(t *testing.T, b *bolt.Bucket) { c := b.Cursor() c.First() startKey := []byte(fmt.Sprintf("%05d", 2)) returnedKey, _ := c.Seek(startKey) require.Equal(t, startKey, returnedKey) // Step 1: verify next for i := 3; i < 1000; i++ { expectedKey := []byte(fmt.Sprintf("%05d", i)) actualKey, _ := c.Next() require.Equal(t, expectedKey, actualKey) } // Once we've reached the end, it should always return nil no matter how many times we call `Next`. for i := 0; i < 10; i++ { k, _ := c.Next() require.Equal(t, []byte(nil), k) } // Step 2: verify prev for i := 998; i >= 0; i-- { expectedKey := []byte(fmt.Sprintf("%05d", i)) actualKey, _ := c.Prev() require.Equal(t, expectedKey, actualKey) } // Once we've reached the beginning, it should always return nil no matter how many times we call `Prev`. for i := 0; i < 10; i++ { k, _ := c.Prev() require.Equal(t, []byte(nil), k) } // Step 3: verify next again for i := 1; i < 1000; i++ { expectedKey := []byte(fmt.Sprintf("%05d", i)) actualKey, _ := c.Next() require.Equal(t, expectedKey, actualKey) } } func testRepeatCursorOperations_PrevNextPrev(t *testing.T, b *bolt.Bucket) { c := b.Cursor() startKey := []byte(fmt.Sprintf("%05d", 998)) returnedKey, _ := c.Seek(startKey) require.Equal(t, startKey, returnedKey) // Step 1: verify prev for i := 997; i >= 0; i-- { expectedKey := []byte(fmt.Sprintf("%05d", i)) actualKey, _ := c.Prev() require.Equal(t, expectedKey, actualKey) } // Once we've reached the beginning, it should always return nil no matter how many times we call `Prev`. for i := 0; i < 10; i++ { k, _ := c.Prev() require.Equal(t, []byte(nil), k) } // Step 2: verify next for i := 1; i < 1000; i++ { expectedKey := []byte(fmt.Sprintf("%05d", i)) actualKey, _ := c.Next() require.Equal(t, expectedKey, actualKey) } // Once we've reached the end, it should always return nil no matter how many times we call `Next`. for i := 0; i < 10; i++ { k, _ := c.Next() require.Equal(t, []byte(nil), k) } // Step 3: verify prev again for i := 998; i >= 0; i-- { expectedKey := []byte(fmt.Sprintf("%05d", i)) actualKey, _ := c.Prev() require.Equal(t, expectedKey, actualKey) } } // Ensure that a cursor can return a reference to the bucket that created it. func TestCursor_Bucket(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if cb := b.Cursor().Bucket(); !reflect.DeepEqual(cb, b) { t.Fatal("cursor bucket mismatch") } return nil }); err != nil { t.Fatal(err) } } // Ensure that a Tx cursor can seek to the appropriate keys. func TestCursor_Seek(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("0001")); err != nil { t.Fatal(err) } if err := b.Put([]byte("bar"), []byte("0002")); err != nil { t.Fatal(err) } if err := b.Put([]byte("baz"), []byte("0003")); err != nil { t.Fatal(err) } if _, err := b.CreateBucket([]byte("bkt")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { c := tx.Bucket([]byte("widgets")).Cursor() // Exact match should go to the key. if k, v := c.Seek([]byte("bar")); !bytes.Equal(k, []byte("bar")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte("0002")) { t.Fatalf("unexpected value: %v", v) } // Inexact match should go to the next key. if k, v := c.Seek([]byte("bas")); !bytes.Equal(k, []byte("baz")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte("0003")) { t.Fatalf("unexpected value: %v", v) } // Low key should go to the first key. if k, v := c.Seek([]byte("")); !bytes.Equal(k, []byte("bar")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte("0002")) { t.Fatalf("unexpected value: %v", v) } // High key should return no key. if k, v := c.Seek([]byte("zzz")); k != nil { t.Fatalf("expected nil key: %v", k) } else if v != nil { t.Fatalf("expected nil value: %v", v) } // Buckets should return their key but no value. if k, v := c.Seek([]byte("bkt")); !bytes.Equal(k, []byte("bkt")) { t.Fatalf("unexpected key: %v", k) } else if v != nil { t.Fatalf("expected nil value: %v", v) } return nil }); err != nil { t.Fatal(err) } } func TestCursor_Delete(t *testing.T) { db := btesting.MustCreateDB(t) const count = 1000 // Insert every other key between 0 and $count. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } for i := 0; i < count; i += 1 { k := make([]byte, 8) binary.BigEndian.PutUint64(k, uint64(i)) if err := b.Put(k, make([]byte, 100)); err != nil { t.Fatal(err) } } if _, err := b.CreateBucket([]byte("sub")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { c := tx.Bucket([]byte("widgets")).Cursor() bound := make([]byte, 8) binary.BigEndian.PutUint64(bound, uint64(count/2)) for key, _ := c.First(); bytes.Compare(key, bound) < 0; key, _ = c.Next() { if err := c.Delete(); err != nil { t.Fatal(err) } } c.Seek([]byte("sub")) if err := c.Delete(); err != bolt.ErrIncompatibleValue { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { stats := tx.Bucket([]byte("widgets")).Stats() if stats.KeyN != count/2+1 { t.Fatalf("unexpected KeyN: %d", stats.KeyN) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a Tx cursor can seek to the appropriate keys when there are a // large number of keys. This test also checks that seek will always move // forward to the next key. // // Related: https://github.com/boltdb/bolt/pull/187 func TestCursor_Seek_Large(t *testing.T) { db := btesting.MustCreateDB(t) var count = 10000 // Insert every other key between 0 and $count. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } for i := 0; i < count; i += 100 { for j := i; j < i+100; j += 2 { k := make([]byte, 8) binary.BigEndian.PutUint64(k, uint64(j)) if err := b.Put(k, make([]byte, 100)); err != nil { t.Fatal(err) } } } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { c := tx.Bucket([]byte("widgets")).Cursor() for i := 0; i < count; i++ { seek := make([]byte, 8) binary.BigEndian.PutUint64(seek, uint64(i)) k, _ := c.Seek(seek) // The last seek is beyond the end of the the range so // it should return nil. if i == count-1 { if k != nil { t.Fatal("expected nil key") } continue } // Otherwise we should seek to the exact key or the next key. num := binary.BigEndian.Uint64(k) if i%2 == 0 { if num != uint64(i) { t.Fatalf("unexpected num: %d", num) } } else { if num != uint64(i+1) { t.Fatalf("unexpected num: %d", num) } } } return nil }); err != nil { t.Fatal(err) } } // Ensure that a cursor can iterate over an empty bucket without error. func TestCursor_EmptyBucket(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { c := tx.Bucket([]byte("widgets")).Cursor() k, v := c.First() if k != nil { t.Fatalf("unexpected key: %v", k) } else if v != nil { t.Fatalf("unexpected value: %v", v) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a Tx cursor can reverse iterate over an empty bucket without error. func TestCursor_EmptyBucketReverse(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { c := tx.Bucket([]byte("widgets")).Cursor() k, v := c.Last() if k != nil { t.Fatalf("unexpected key: %v", k) } else if v != nil { t.Fatalf("unexpected value: %v", v) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a Tx cursor can iterate over a single root with a couple elements. func TestCursor_Iterate_Leaf(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("baz"), []byte{}); err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte{0}); err != nil { t.Fatal(err) } if err := b.Put([]byte("bar"), []byte{1}); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } tx, err := db.Begin(false) if err != nil { t.Fatal(err) } defer func() { _ = tx.Rollback() }() c := tx.Bucket([]byte("widgets")).Cursor() k, v := c.First() if !bytes.Equal(k, []byte("bar")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte{1}) { t.Fatalf("unexpected value: %v", v) } k, v = c.Next() if !bytes.Equal(k, []byte("baz")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte{}) { t.Fatalf("unexpected value: %v", v) } k, v = c.Next() if !bytes.Equal(k, []byte("foo")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte{0}) { t.Fatalf("unexpected value: %v", v) } k, v = c.Next() if k != nil { t.Fatalf("expected nil key: %v", k) } else if v != nil { t.Fatalf("expected nil value: %v", v) } k, v = c.Next() if k != nil { t.Fatalf("expected nil key: %v", k) } else if v != nil { t.Fatalf("expected nil value: %v", v) } if err := tx.Rollback(); err != nil { t.Fatal(err) } } // Ensure that a Tx cursor can iterate in reverse over a single root with a couple elements. func TestCursor_LeafRootReverse(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("baz"), []byte{}); err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte{0}); err != nil { t.Fatal(err) } if err := b.Put([]byte("bar"), []byte{1}); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } tx, err := db.Begin(false) if err != nil { t.Fatal(err) } c := tx.Bucket([]byte("widgets")).Cursor() if k, v := c.Last(); !bytes.Equal(k, []byte("foo")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte{0}) { t.Fatalf("unexpected value: %v", v) } if k, v := c.Prev(); !bytes.Equal(k, []byte("baz")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte{}) { t.Fatalf("unexpected value: %v", v) } if k, v := c.Prev(); !bytes.Equal(k, []byte("bar")) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, []byte{1}) { t.Fatalf("unexpected value: %v", v) } if k, v := c.Prev(); k != nil { t.Fatalf("expected nil key: %v", k) } else if v != nil { t.Fatalf("expected nil value: %v", v) } if k, v := c.Prev(); k != nil { t.Fatalf("expected nil key: %v", k) } else if v != nil { t.Fatalf("expected nil value: %v", v) } if err := tx.Rollback(); err != nil { t.Fatal(err) } } // Ensure that a Tx cursor can restart from the beginning. func TestCursor_Restart(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("bar"), []byte{}); err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte{}); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } tx, err := db.Begin(false) if err != nil { t.Fatal(err) } c := tx.Bucket([]byte("widgets")).Cursor() if k, _ := c.First(); !bytes.Equal(k, []byte("bar")) { t.Fatalf("unexpected key: %v", k) } if k, _ := c.Next(); !bytes.Equal(k, []byte("foo")) { t.Fatalf("unexpected key: %v", k) } if k, _ := c.First(); !bytes.Equal(k, []byte("bar")) { t.Fatalf("unexpected key: %v", k) } if k, _ := c.Next(); !bytes.Equal(k, []byte("foo")) { t.Fatalf("unexpected key: %v", k) } if err := tx.Rollback(); err != nil { t.Fatal(err) } } // Ensure that a cursor can skip over empty pages that have been deleted. func TestCursor_First_EmptyPages(t *testing.T) { db := btesting.MustCreateDB(t) // Create 1000 keys in the "widgets" bucket. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } for i := 0; i < 1000; i++ { if err := b.Put(u64tob(uint64(i)), []byte{}); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } // Delete half the keys and then try to iterate. if err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for i := 0; i < 600; i++ { if err := b.Delete(u64tob(uint64(i))); err != nil { t.Fatal(err) } } c := b.Cursor() var n int for k, _ := c.First(); k != nil; k, _ = c.Next() { n++ } if n != 400 { t.Fatalf("unexpected key count: %d", n) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a cursor can skip over empty pages that have been deleted. func TestCursor_Last_EmptyPages(t *testing.T) { db := btesting.MustCreateDB(t) // Create 1000 keys in the "widgets" bucket. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } for i := 0; i < 1000; i++ { if err := b.Put(u64tob(uint64(i)), []byte{}); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } // Delete last 800 elements to ensure last page is empty if err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for i := 200; i < 1000; i++ { if err := b.Delete(u64tob(uint64(i))); err != nil { t.Fatal(err) } } c := b.Cursor() var n int for k, _ := c.Last(); k != nil; k, _ = c.Prev() { n++ } if n != 200 { t.Fatalf("unexpected key count: %d", n) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a Tx can iterate over all elements in a bucket. func TestCursor_QuickCheck(t *testing.T) { f := func(items testdata) bool { db := btesting.MustCreateDB(t) defer db.MustClose() // Bulk insert all values. tx, err := db.Begin(true) if err != nil { t.Fatal(err) } b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } for _, item := range items { if err := b.Put(item.Key, item.Value); err != nil { t.Fatal(err) } } if err := tx.Commit(); err != nil { t.Fatal(err) } // Sort test data. sort.Sort(items) // Iterate over all items and check consistency. var index = 0 tx, err = db.Begin(false) if err != nil { t.Fatal(err) } c := tx.Bucket([]byte("widgets")).Cursor() for k, v := c.First(); k != nil && index < len(items); k, v = c.Next() { if !bytes.Equal(k, items[index].Key) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, items[index].Value) { t.Fatalf("unexpected value: %v", v) } index++ } if len(items) != index { t.Fatalf("unexpected item count: %v, expected %v", len(items), index) } if err := tx.Rollback(); err != nil { t.Fatal(err) } return true } if err := quick.Check(f, qconfig()); err != nil { t.Error(err) } } // Ensure that a transaction can iterate over all elements in a bucket in reverse. func TestCursor_QuickCheck_Reverse(t *testing.T) { f := func(items testdata) bool { db := btesting.MustCreateDB(t) defer db.MustClose() // Bulk insert all values. tx, err := db.Begin(true) if err != nil { t.Fatal(err) } b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } for _, item := range items { if err := b.Put(item.Key, item.Value); err != nil { t.Fatal(err) } } if err := tx.Commit(); err != nil { t.Fatal(err) } // Sort test data. sort.Sort(revtestdata(items)) // Iterate over all items and check consistency. var index = 0 tx, err = db.Begin(false) if err != nil { t.Fatal(err) } c := tx.Bucket([]byte("widgets")).Cursor() for k, v := c.Last(); k != nil && index < len(items); k, v = c.Prev() { if !bytes.Equal(k, items[index].Key) { t.Fatalf("unexpected key: %v", k) } else if !bytes.Equal(v, items[index].Value) { t.Fatalf("unexpected value: %v", v) } index++ } if len(items) != index { t.Fatalf("unexpected item count: %v, expected %v", len(items), index) } if err := tx.Rollback(); err != nil { t.Fatal(err) } return true } if err := quick.Check(f, qconfig()); err != nil { t.Error(err) } } // Ensure that a Tx cursor can iterate over subbuckets. func TestCursor_QuickCheck_BucketsOnly(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if _, err := b.CreateBucket([]byte("foo")); err != nil { t.Fatal(err) } if _, err := b.CreateBucket([]byte("bar")); err != nil { t.Fatal(err) } if _, err := b.CreateBucket([]byte("baz")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { var names []string c := tx.Bucket([]byte("widgets")).Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { names = append(names, string(k)) if v != nil { t.Fatalf("unexpected value: %v", v) } } if !reflect.DeepEqual(names, []string{"bar", "baz", "foo"}) { t.Fatalf("unexpected names: %+v", names) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a Tx cursor can reverse iterate over subbuckets. func TestCursor_QuickCheck_BucketsOnly_Reverse(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if _, err := b.CreateBucket([]byte("foo")); err != nil { t.Fatal(err) } if _, err := b.CreateBucket([]byte("bar")); err != nil { t.Fatal(err) } if _, err := b.CreateBucket([]byte("baz")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { var names []string c := tx.Bucket([]byte("widgets")).Cursor() for k, v := c.Last(); k != nil; k, v = c.Prev() { names = append(names, string(k)) if v != nil { t.Fatalf("unexpected value: %v", v) } } if !reflect.DeepEqual(names, []string{"foo", "baz", "bar"}) { t.Fatalf("unexpected names: %+v", names) } return nil }); err != nil { t.Fatal(err) } } func ExampleCursor() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Start a read-write transaction. if err := db.Update(func(tx *bolt.Tx) error { // Create a new bucket. b, err := tx.CreateBucket([]byte("animals")) if err != nil { return err } // Insert data into a bucket. if err := b.Put([]byte("dog"), []byte("fun")); err != nil { log.Fatal(err) } if err := b.Put([]byte("cat"), []byte("lame")); err != nil { log.Fatal(err) } if err := b.Put([]byte("liger"), []byte("awesome")); err != nil { log.Fatal(err) } // Create a cursor for iteration. c := b.Cursor() // Iterate over items in sorted key order. This starts from the // first key/value pair and updates the k/v variables to the // next key/value on each iteration. // // The loop finishes at the end of the cursor when a nil key is returned. for k, v := c.First(); k != nil; k, v = c.Next() { fmt.Printf("A %s is %s.\n", k, v) } return nil }); err != nil { log.Fatal(err) } if err := db.Close(); err != nil { log.Fatal(err) } // Output: // A cat is lame. // A dog is fun. // A liger is awesome. } func ExampleCursor_reverse() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Start a read-write transaction. if err := db.Update(func(tx *bolt.Tx) error { // Create a new bucket. b, err := tx.CreateBucket([]byte("animals")) if err != nil { return err } // Insert data into a bucket. if err := b.Put([]byte("dog"), []byte("fun")); err != nil { log.Fatal(err) } if err := b.Put([]byte("cat"), []byte("lame")); err != nil { log.Fatal(err) } if err := b.Put([]byte("liger"), []byte("awesome")); err != nil { log.Fatal(err) } // Create a cursor for iteration. c := b.Cursor() // Iterate over items in reverse sorted key order. This starts // from the last key/value pair and updates the k/v variables to // the previous key/value on each iteration. // // The loop finishes at the beginning of the cursor when a nil key // is returned. for k, v := c.Last(); k != nil; k, v = c.Prev() { fmt.Printf("A %s is %s.\n", k, v) } return nil }); err != nil { log.Fatal(err) } // Close the database to release the file lock. if err := db.Close(); err != nil { log.Fatal(err) } // Output: // A liger is awesome. // A dog is fun. // A cat is lame. } bbolt-1.3.10/db.go000066400000000000000000001142421461507540700136270ustar00rootroot00000000000000package bbolt import ( "errors" "fmt" "hash/fnv" "io" "os" "runtime" "sort" "sync" "time" "unsafe" ) // The largest step that can be taken when remapping the mmap. const maxMmapStep = 1 << 30 // 1GB // The data file format version. const version = 2 // Represents a marker value to indicate that a file is a Bolt DB. const magic uint32 = 0xED0CDAED const pgidNoFreelist pgid = 0xffffffffffffffff // IgnoreNoSync specifies whether the NoSync field of a DB is ignored when // syncing changes to a file. This is required as some operating systems, // such as OpenBSD, do not have a unified buffer cache (UBC) and writes // must be synchronized using the msync(2) syscall. const IgnoreNoSync = runtime.GOOS == "openbsd" // Default values if not set in a DB instance. const ( DefaultMaxBatchSize int = 1000 DefaultMaxBatchDelay = 10 * time.Millisecond DefaultAllocSize = 16 * 1024 * 1024 ) // default page size for db is set to the OS page size. var defaultPageSize = os.Getpagesize() // The time elapsed between consecutive file locking attempts. const flockRetryTimeout = 50 * time.Millisecond // FreelistType is the type of the freelist backend type FreelistType string const ( // FreelistArrayType indicates backend freelist type is array FreelistArrayType = FreelistType("array") // FreelistMapType indicates backend freelist type is hashmap FreelistMapType = FreelistType("hashmap") ) // DB represents a collection of buckets persisted to a file on disk. // All data access is performed through transactions which can be obtained through the DB. // All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called. type DB struct { // Put `stats` at the first field to ensure it's 64-bit aligned. Note that // the first word in an allocated struct can be relied upon to be 64-bit // aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG. Also // refer to discussion in https://github.com/etcd-io/bbolt/issues/577. stats Stats // When enabled, the database will perform a Check() after every commit. // A panic is issued if the database is in an inconsistent state. This // flag has a large performance impact so it should only be used for // debugging purposes. StrictMode bool // Setting the NoSync flag will cause the database to skip fsync() // calls after each commit. This can be useful when bulk loading data // into a database and you can restart the bulk load in the event of // a system failure or database corruption. Do not set this flag for // normal use. // // If the package global IgnoreNoSync constant is true, this value is // ignored. See the comment on that constant for more details. // // THIS IS UNSAFE. PLEASE USE WITH CAUTION. NoSync bool // When true, skips syncing freelist to disk. This improves the database // write performance under normal operation, but requires a full database // re-sync during recovery. NoFreelistSync bool // FreelistType sets the backend freelist type. There are two options. Array which is simple but endures // dramatic performance degradation if database is large and fragmentation in freelist is common. // The alternative one is using hashmap, it is faster in almost all circumstances // but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe. // The default type is array FreelistType FreelistType // When true, skips the truncate call when growing the database. // Setting this to true is only safe on non-ext3/ext4 systems. // Skipping truncation avoids preallocation of hard drive space and // bypasses a truncate() and fsync() syscall on remapping. // // https://github.com/boltdb/bolt/issues/284 NoGrowSync bool // When `true`, bbolt will always load the free pages when opening the DB. // When opening db in write mode, this flag will always automatically // set to `true`. PreLoadFreelist bool // If you want to read the entire database fast, you can set MmapFlag to // syscall.MAP_POPULATE on Linux 2.6.23+ for sequential read-ahead. MmapFlags int // MaxBatchSize is the maximum size of a batch. Default value is // copied from DefaultMaxBatchSize in Open. // // If <=0, disables batching. // // Do not change concurrently with calls to Batch. MaxBatchSize int // MaxBatchDelay is the maximum delay before a batch starts. // Default value is copied from DefaultMaxBatchDelay in Open. // // If <=0, effectively disables batching. // // Do not change concurrently with calls to Batch. MaxBatchDelay time.Duration // AllocSize is the amount of space allocated when the database // needs to create new pages. This is done to amortize the cost // of truncate() and fsync() when growing the data file. AllocSize int // Mlock locks database file in memory when set to true. // It prevents major page faults, however used memory can't be reclaimed. // // Supported only on Unix via mlock/munlock syscalls. Mlock bool path string openFile func(string, int, os.FileMode) (*os.File, error) file *os.File // `dataref` isn't used at all on Windows, and the golangci-lint // always fails on Windows platform. //nolint dataref []byte // mmap'ed readonly, write throws SEGV data *[maxMapSize]byte datasz int filesz int // current on disk file size meta0 *meta meta1 *meta pageSize int opened bool rwtx *Tx txs []*Tx freelist *freelist freelistLoad sync.Once pagePool sync.Pool batchMu sync.Mutex batch *batch rwlock sync.Mutex // Allows only one writer at a time. metalock sync.Mutex // Protects meta page access. mmaplock sync.RWMutex // Protects mmap access during remapping. statlock sync.RWMutex // Protects stats access. ops struct { writeAt func(b []byte, off int64) (n int, err error) } // Read only mode. // When true, Update() and Begin(true) return ErrDatabaseReadOnly immediately. readOnly bool } // Path returns the path to currently open database file. func (db *DB) Path() string { return db.path } // GoString returns the Go string representation of the database. func (db *DB) GoString() string { return fmt.Sprintf("bolt.DB{path:%q}", db.path) } // String returns the string representation of the database. func (db *DB) String() string { return fmt.Sprintf("DB<%q>", db.path) } // Open creates and opens a database at the given path. // If the file does not exist then it will be created automatically. // Passing in nil options will cause Bolt to open the database with the default options. func Open(path string, mode os.FileMode, options *Options) (*DB, error) { db := &DB{ opened: true, } // Set default options if no options are provided. if options == nil { options = DefaultOptions } db.NoSync = options.NoSync db.NoGrowSync = options.NoGrowSync db.MmapFlags = options.MmapFlags db.NoFreelistSync = options.NoFreelistSync db.PreLoadFreelist = options.PreLoadFreelist db.FreelistType = options.FreelistType db.Mlock = options.Mlock // Set default values for later DB operations. db.MaxBatchSize = DefaultMaxBatchSize db.MaxBatchDelay = DefaultMaxBatchDelay db.AllocSize = DefaultAllocSize flag := os.O_RDWR if options.ReadOnly { flag = os.O_RDONLY db.readOnly = true } else { // always load free pages in write mode db.PreLoadFreelist = true } db.openFile = options.OpenFile if db.openFile == nil { db.openFile = os.OpenFile } // Open data file and separate sync handler for metadata writes. var err error if db.file, err = db.openFile(path, flag|os.O_CREATE, mode); err != nil { _ = db.close() return nil, err } db.path = db.file.Name() // Lock file so that other processes using Bolt in read-write mode cannot // use the database at the same time. This would cause corruption since // the two processes would write meta pages and free pages separately. // The database file is locked exclusively (only one process can grab the lock) // if !options.ReadOnly. // The database file is locked using the shared lock (more than one process may // hold a lock at the same time) otherwise (options.ReadOnly is set). if err := flock(db, !db.readOnly, options.Timeout); err != nil { _ = db.close() return nil, err } // Default values for test hooks db.ops.writeAt = db.file.WriteAt if db.pageSize = options.PageSize; db.pageSize == 0 { // Set the default page size to the OS page size. db.pageSize = defaultPageSize } // Initialize the database if it doesn't exist. if info, err := db.file.Stat(); err != nil { _ = db.close() return nil, err } else if info.Size() == 0 { // Initialize new files with meta pages. if err := db.init(); err != nil { // clean up file descriptor on initialization fail _ = db.close() return nil, err } } else { // try to get the page size from the metadata pages if pgSize, err := db.getPageSize(); err == nil { db.pageSize = pgSize } else { _ = db.close() return nil, ErrInvalid } } // Initialize page pool. db.pagePool = sync.Pool{ New: func() interface{} { return make([]byte, db.pageSize) }, } // Memory map the data file. if err := db.mmap(options.InitialMmapSize); err != nil { _ = db.close() return nil, err } if db.PreLoadFreelist { db.loadFreelist() } if db.readOnly { return db, nil } // Flush freelist when transitioning from no sync to sync so // NoFreelistSync unaware boltdb can open the db later. if !db.NoFreelistSync && !db.hasSyncedFreelist() { tx, err := db.Begin(true) if tx != nil { err = tx.Commit() } if err != nil { _ = db.close() return nil, err } } // Mark the database as opened and return. return db, nil } // getPageSize reads the pageSize from the meta pages. It tries // to read the first meta page firstly. If the first page is invalid, // then it tries to read the second page using the default page size. func (db *DB) getPageSize() (int, error) { var ( meta0CanRead, meta1CanRead bool ) // Read the first meta page to determine the page size. if pgSize, canRead, err := db.getPageSizeFromFirstMeta(); err != nil { // We cannot read the page size from page 0, but can read page 0. meta0CanRead = canRead } else { return pgSize, nil } // Read the second meta page to determine the page size. if pgSize, canRead, err := db.getPageSizeFromSecondMeta(); err != nil { // We cannot read the page size from page 1, but can read page 1. meta1CanRead = canRead } else { return pgSize, nil } // If we can't read the page size from both pages, but can read // either page, then we assume it's the same as the OS or the one // given, since that's how the page size was chosen in the first place. // // If both pages are invalid, and (this OS uses a different page size // from what the database was created with or the given page size is // different from what the database was created with), then we are out // of luck and cannot access the database. if meta0CanRead || meta1CanRead { return db.pageSize, nil } return 0, ErrInvalid } // getPageSizeFromFirstMeta reads the pageSize from the first meta page func (db *DB) getPageSizeFromFirstMeta() (int, bool, error) { var buf [0x1000]byte var metaCanRead bool if bw, err := db.file.ReadAt(buf[:], 0); err == nil && bw == len(buf) { metaCanRead = true if m := db.pageInBuffer(buf[:], 0).meta(); m.validate() == nil { return int(m.pageSize), metaCanRead, nil } } return 0, metaCanRead, ErrInvalid } // getPageSizeFromSecondMeta reads the pageSize from the second meta page func (db *DB) getPageSizeFromSecondMeta() (int, bool, error) { var ( fileSize int64 metaCanRead bool ) // get the db file size if info, err := db.file.Stat(); err != nil { return 0, metaCanRead, err } else { fileSize = info.Size() } // We need to read the second meta page, so we should skip the first page; // but we don't know the exact page size yet, it's chicken & egg problem. // The solution is to try all the possible page sizes, which starts from 1KB // and until 16MB (1024<<14) or the end of the db file // // TODO: should we support larger page size? for i := 0; i <= 14; i++ { var buf [0x1000]byte var pos int64 = 1024 << uint(i) if pos >= fileSize-1024 { break } bw, err := db.file.ReadAt(buf[:], pos) if (err == nil && bw == len(buf)) || (err == io.EOF && int64(bw) == (fileSize-pos)) { metaCanRead = true if m := db.pageInBuffer(buf[:], 0).meta(); m.validate() == nil { return int(m.pageSize), metaCanRead, nil } } } return 0, metaCanRead, ErrInvalid } // loadFreelist reads the freelist if it is synced, or reconstructs it // by scanning the DB if it is not synced. It assumes there are no // concurrent accesses being made to the freelist. func (db *DB) loadFreelist() { db.freelistLoad.Do(func() { db.freelist = newFreelist(db.FreelistType) if !db.hasSyncedFreelist() { // Reconstruct free list by scanning the DB. db.freelist.readIDs(db.freepages()) } else { // Read free list from freelist page. db.freelist.read(db.page(db.meta().freelist)) } db.stats.FreePageN = db.freelist.free_count() }) } func (db *DB) hasSyncedFreelist() bool { return db.meta().freelist != pgidNoFreelist } // mmap opens the underlying memory-mapped file and initializes the meta references. // minsz is the minimum size that the new mmap can be. func (db *DB) mmap(minsz int) (err error) { db.mmaplock.Lock() defer db.mmaplock.Unlock() info, err := db.file.Stat() if err != nil { return fmt.Errorf("mmap stat error: %s", err) } else if int(info.Size()) < db.pageSize*2 { return fmt.Errorf("file size too small") } // Ensure the size is at least the minimum size. fileSize := int(info.Size()) var size = fileSize if size < minsz { size = minsz } size, err = db.mmapSize(size) if err != nil { return err } if db.Mlock { // Unlock db memory if err := db.munlock(fileSize); err != nil { return err } } // Dereference all mmap references before unmapping. if db.rwtx != nil { db.rwtx.root.dereference() } // Unmap existing data before continuing. if err = db.munmap(); err != nil { return err } // Memory-map the data file as a byte slice. // gofail: var mapError string // return errors.New(mapError) if err = mmap(db, size); err != nil { return err } // Perform unmmap on any error to reset all data fields: // dataref, data, datasz, meta0 and meta1. defer func() { if err != nil { if unmapErr := db.munmap(); unmapErr != nil { err = fmt.Errorf("%w; rollback unmap also failed: %v", err, unmapErr) } } }() if db.Mlock { // Don't allow swapping of data file if err := db.mlock(fileSize); err != nil { return err } } // Save references to the meta pages. db.meta0 = db.page(0).meta() db.meta1 = db.page(1).meta() // Validate the meta pages. We only return an error if both meta pages fail // validation, since meta0 failing validation means that it wasn't saved // properly -- but we can recover using meta1. And vice-versa. err0 := db.meta0.validate() err1 := db.meta1.validate() if err0 != nil && err1 != nil { return err0 } return nil } func (db *DB) invalidate() { db.dataref = nil db.data = nil db.datasz = 0 db.meta0 = nil db.meta1 = nil } // munmap unmaps the data file from memory. func (db *DB) munmap() error { defer db.invalidate() // gofail: var unmapError string // return errors.New(unmapError) if err := munmap(db); err != nil { return fmt.Errorf("unmap error: " + err.Error()) } return nil } // mmapSize determines the appropriate size for the mmap given the current size // of the database. The minimum size is 32KB and doubles until it reaches 1GB. // Returns an error if the new mmap size is greater than the max allowed. func (db *DB) mmapSize(size int) (int, error) { // Double the size from 32KB until 1GB. for i := uint(15); i <= 30; i++ { if size <= 1< maxMapSize { return 0, fmt.Errorf("mmap too large") } // If larger than 1GB then grow by 1GB at a time. sz := int64(size) if remainder := sz % int64(maxMmapStep); remainder > 0 { sz += int64(maxMmapStep) - remainder } // Ensure that the mmap size is a multiple of the page size. // This should always be true since we're incrementing in MBs. pageSize := int64(db.pageSize) if (sz % pageSize) != 0 { sz = ((sz / pageSize) + 1) * pageSize } // If we've exceeded the max size then only grow up to the max size. if sz > maxMapSize { sz = maxMapSize } return int(sz), nil } func (db *DB) munlock(fileSize int) error { // gofail: var munlockError string // return errors.New(munlockError) if err := munlock(db, fileSize); err != nil { return fmt.Errorf("munlock error: " + err.Error()) } return nil } func (db *DB) mlock(fileSize int) error { // gofail: var mlockError string // return errors.New(mlockError) if err := mlock(db, fileSize); err != nil { return fmt.Errorf("mlock error: " + err.Error()) } return nil } func (db *DB) mrelock(fileSizeFrom, fileSizeTo int) error { if err := db.munlock(fileSizeFrom); err != nil { return err } if err := db.mlock(fileSizeTo); err != nil { return err } return nil } // init creates a new database file and initializes its meta pages. func (db *DB) init() error { // Create two meta pages on a buffer. buf := make([]byte, db.pageSize*4) for i := 0; i < 2; i++ { p := db.pageInBuffer(buf, pgid(i)) p.id = pgid(i) p.flags = metaPageFlag // Initialize the meta page. m := p.meta() m.magic = magic m.version = version m.pageSize = uint32(db.pageSize) m.freelist = 2 m.root = bucket{root: 3} m.pgid = 4 m.txid = txid(i) m.checksum = m.sum64() } // Write an empty freelist at page 3. p := db.pageInBuffer(buf, pgid(2)) p.id = pgid(2) p.flags = freelistPageFlag p.count = 0 // Write an empty leaf page at page 4. p = db.pageInBuffer(buf, pgid(3)) p.id = pgid(3) p.flags = leafPageFlag p.count = 0 // Write the buffer to our data file. if _, err := db.ops.writeAt(buf, 0); err != nil { return err } if err := fdatasync(db); err != nil { return err } db.filesz = len(buf) return nil } // Close releases all database resources. // It will block waiting for any open transactions to finish // before closing the database and returning. func (db *DB) Close() error { db.rwlock.Lock() defer db.rwlock.Unlock() db.metalock.Lock() defer db.metalock.Unlock() db.mmaplock.Lock() defer db.mmaplock.Unlock() return db.close() } func (db *DB) close() error { if !db.opened { return nil } db.opened = false db.freelist = nil // Clear ops. db.ops.writeAt = nil var errs []error // Close the mmap. if err := db.munmap(); err != nil { errs = append(errs, err) } // Close file handles. if db.file != nil { // No need to unlock read-only file. if !db.readOnly { // Unlock the file. if err := funlock(db); err != nil { errs = append(errs, fmt.Errorf("bolt.Close(): funlock error: %w", err)) } } // Close the file descriptor. if err := db.file.Close(); err != nil { errs = append(errs, fmt.Errorf("db file close: %w", err)) } db.file = nil } db.path = "" if len(errs) > 0 { return errs[0] } return nil } // Begin starts a new transaction. // Multiple read-only transactions can be used concurrently but only one // write transaction can be used at a time. Starting multiple write transactions // will cause the calls to block and be serialized until the current write // transaction finishes. // // Transactions should not be dependent on one another. Opening a read // transaction and a write transaction in the same goroutine can cause the // writer to deadlock because the database periodically needs to re-mmap itself // as it grows and it cannot do that while a read transaction is open. // // If a long running read transaction (for example, a snapshot transaction) is // needed, you might want to set DB.InitialMmapSize to a large enough value // to avoid potential blocking of write transaction. // // IMPORTANT: You must close read-only transactions after you are finished or // else the database will not reclaim old pages. func (db *DB) Begin(writable bool) (*Tx, error) { if writable { return db.beginRWTx() } return db.beginTx() } func (db *DB) beginTx() (*Tx, error) { // Lock the meta pages while we initialize the transaction. We obtain // the meta lock before the mmap lock because that's the order that the // write transaction will obtain them. db.metalock.Lock() // Obtain a read-only lock on the mmap. When the mmap is remapped it will // obtain a write lock so all transactions must finish before it can be // remapped. db.mmaplock.RLock() // Exit if the database is not open yet. if !db.opened { db.mmaplock.RUnlock() db.metalock.Unlock() return nil, ErrDatabaseNotOpen } // Exit if the database is not correctly mapped. if db.data == nil { db.mmaplock.RUnlock() db.metalock.Unlock() return nil, ErrInvalidMapping } // Create a transaction associated with the database. t := &Tx{} t.init(db) // Keep track of transaction until it closes. db.txs = append(db.txs, t) n := len(db.txs) // Unlock the meta pages. db.metalock.Unlock() // Update the transaction stats. db.statlock.Lock() db.stats.TxN++ db.stats.OpenTxN = n db.statlock.Unlock() return t, nil } func (db *DB) beginRWTx() (*Tx, error) { // If the database was opened with Options.ReadOnly, return an error. if db.readOnly { return nil, ErrDatabaseReadOnly } // Obtain writer lock. This is released by the transaction when it closes. // This enforces only one writer transaction at a time. db.rwlock.Lock() // Once we have the writer lock then we can lock the meta pages so that // we can set up the transaction. db.metalock.Lock() defer db.metalock.Unlock() // Exit if the database is not open yet. if !db.opened { db.rwlock.Unlock() return nil, ErrDatabaseNotOpen } // Exit if the database is not correctly mapped. if db.data == nil { db.rwlock.Unlock() return nil, ErrInvalidMapping } // Create a transaction associated with the database. t := &Tx{writable: true} t.init(db) db.rwtx = t db.freePages() return t, nil } // freePages releases any pages associated with closed read-only transactions. func (db *DB) freePages() { // Free all pending pages prior to earliest open transaction. sort.Sort(txsById(db.txs)) minid := txid(0xFFFFFFFFFFFFFFFF) if len(db.txs) > 0 { minid = db.txs[0].meta.txid } if minid > 0 { db.freelist.release(minid - 1) } // Release unused txid extents. for _, t := range db.txs { db.freelist.releaseRange(minid, t.meta.txid-1) minid = t.meta.txid + 1 } db.freelist.releaseRange(minid, txid(0xFFFFFFFFFFFFFFFF)) // Any page both allocated and freed in an extent is safe to release. } type txsById []*Tx func (t txsById) Len() int { return len(t) } func (t txsById) Swap(i, j int) { t[i], t[j] = t[j], t[i] } func (t txsById) Less(i, j int) bool { return t[i].meta.txid < t[j].meta.txid } // removeTx removes a transaction from the database. func (db *DB) removeTx(tx *Tx) { // Release the read lock on the mmap. db.mmaplock.RUnlock() // Use the meta lock to restrict access to the DB object. db.metalock.Lock() // Remove the transaction. for i, t := range db.txs { if t == tx { last := len(db.txs) - 1 db.txs[i] = db.txs[last] db.txs[last] = nil db.txs = db.txs[:last] break } } n := len(db.txs) // Unlock the meta pages. db.metalock.Unlock() // Merge statistics. db.statlock.Lock() db.stats.OpenTxN = n db.stats.TxStats.add(&tx.stats) db.statlock.Unlock() } // Update executes a function within the context of a read-write managed transaction. // If no error is returned from the function then the transaction is committed. // If an error is returned then the entire transaction is rolled back. // Any error that is returned from the function or returned from the commit is // returned from the Update() method. // // Attempting to manually commit or rollback within the function will cause a panic. func (db *DB) Update(fn func(*Tx) error) error { t, err := db.Begin(true) if err != nil { return err } // Make sure the transaction rolls back in the event of a panic. defer func() { if t.db != nil { t.rollback() } }() // Mark as a managed tx so that the inner function cannot manually commit. t.managed = true // If an error is returned from the function then rollback and return error. err = fn(t) t.managed = false if err != nil { _ = t.Rollback() return err } return t.Commit() } // View executes a function within the context of a managed read-only transaction. // Any error that is returned from the function is returned from the View() method. // // Attempting to manually rollback within the function will cause a panic. func (db *DB) View(fn func(*Tx) error) error { t, err := db.Begin(false) if err != nil { return err } // Make sure the transaction rolls back in the event of a panic. defer func() { if t.db != nil { t.rollback() } }() // Mark as a managed tx so that the inner function cannot manually rollback. t.managed = true // If an error is returned from the function then pass it through. err = fn(t) t.managed = false if err != nil { _ = t.Rollback() return err } return t.Rollback() } // Batch calls fn as part of a batch. It behaves similar to Update, // except: // // 1. concurrent Batch calls can be combined into a single Bolt // transaction. // // 2. the function passed to Batch may be called multiple times, // regardless of whether it returns error or not. // // This means that Batch function side effects must be idempotent and // take permanent effect only after a successful return is seen in // caller. // // The maximum batch size and delay can be adjusted with DB.MaxBatchSize // and DB.MaxBatchDelay, respectively. // // Batch is only useful when there are multiple goroutines calling it. func (db *DB) Batch(fn func(*Tx) error) error { errCh := make(chan error, 1) db.batchMu.Lock() if (db.batch == nil) || (db.batch != nil && len(db.batch.calls) >= db.MaxBatchSize) { // There is no existing batch, or the existing batch is full; start a new one. db.batch = &batch{ db: db, } db.batch.timer = time.AfterFunc(db.MaxBatchDelay, db.batch.trigger) } db.batch.calls = append(db.batch.calls, call{fn: fn, err: errCh}) if len(db.batch.calls) >= db.MaxBatchSize { // wake up batch, it's ready to run go db.batch.trigger() } db.batchMu.Unlock() err := <-errCh if err == trySolo { err = db.Update(fn) } return err } type call struct { fn func(*Tx) error err chan<- error } type batch struct { db *DB timer *time.Timer start sync.Once calls []call } // trigger runs the batch if it hasn't already been run. func (b *batch) trigger() { b.start.Do(b.run) } // run performs the transactions in the batch and communicates results // back to DB.Batch. func (b *batch) run() { b.db.batchMu.Lock() b.timer.Stop() // Make sure no new work is added to this batch, but don't break // other batches. if b.db.batch == b { b.db.batch = nil } b.db.batchMu.Unlock() retry: for len(b.calls) > 0 { var failIdx = -1 err := b.db.Update(func(tx *Tx) error { for i, c := range b.calls { if err := safelyCall(c.fn, tx); err != nil { failIdx = i return err } } return nil }) if failIdx >= 0 { // take the failing transaction out of the batch. it's // safe to shorten b.calls here because db.batch no longer // points to us, and we hold the mutex anyway. c := b.calls[failIdx] b.calls[failIdx], b.calls = b.calls[len(b.calls)-1], b.calls[:len(b.calls)-1] // tell the submitter re-run it solo, continue with the rest of the batch c.err <- trySolo continue retry } // pass success, or bolt internal errors, to all callers for _, c := range b.calls { c.err <- err } break retry } } // trySolo is a special sentinel error value used for signaling that a // transaction function should be re-run. It should never be seen by // callers. var trySolo = errors.New("batch function returned an error and should be re-run solo") type panicked struct { reason interface{} } func (p panicked) Error() string { if err, ok := p.reason.(error); ok { return err.Error() } return fmt.Sprintf("panic: %v", p.reason) } func safelyCall(fn func(*Tx) error, tx *Tx) (err error) { defer func() { if p := recover(); p != nil { err = panicked{p} } }() return fn(tx) } // Sync executes fdatasync() against the database file handle. // // This is not necessary under normal operation, however, if you use NoSync // then it allows you to force the database file to sync against the disk. func (db *DB) Sync() error { return fdatasync(db) } // Stats retrieves ongoing performance stats for the database. // This is only updated when a transaction closes. func (db *DB) Stats() Stats { db.statlock.RLock() defer db.statlock.RUnlock() return db.stats } // This is for internal access to the raw data bytes from the C cursor, use // carefully, or not at all. func (db *DB) Info() *Info { _assert(db.data != nil, "database file isn't correctly mapped") return &Info{uintptr(unsafe.Pointer(&db.data[0])), db.pageSize} } // page retrieves a page reference from the mmap based on the current page size. func (db *DB) page(id pgid) *page { pos := id * pgid(db.pageSize) return (*page)(unsafe.Pointer(&db.data[pos])) } // pageInBuffer retrieves a page reference from a given byte array based on the current page size. func (db *DB) pageInBuffer(b []byte, id pgid) *page { return (*page)(unsafe.Pointer(&b[id*pgid(db.pageSize)])) } // meta retrieves the current meta page reference. func (db *DB) meta() *meta { // We have to return the meta with the highest txid which doesn't fail // validation. Otherwise, we can cause errors when in fact the database is // in a consistent state. metaA is the one with the higher txid. metaA := db.meta0 metaB := db.meta1 if db.meta1.txid > db.meta0.txid { metaA = db.meta1 metaB = db.meta0 } // Use higher meta page if valid. Otherwise, fallback to previous, if valid. if err := metaA.validate(); err == nil { return metaA } else if err := metaB.validate(); err == nil { return metaB } // This should never be reached, because both meta1 and meta0 were validated // on mmap() and we do fsync() on every write. panic("bolt.DB.meta(): invalid meta pages") } // allocate returns a contiguous block of memory starting at a given page. func (db *DB) allocate(txid txid, count int) (*page, error) { // Allocate a temporary buffer for the page. var buf []byte if count == 1 { buf = db.pagePool.Get().([]byte) } else { buf = make([]byte, count*db.pageSize) } p := (*page)(unsafe.Pointer(&buf[0])) p.overflow = uint32(count - 1) // Use pages from the freelist if they are available. if p.id = db.freelist.allocate(txid, count); p.id != 0 { return p, nil } // Resize mmap() if we're at the end. p.id = db.rwtx.meta.pgid var minsz = int((p.id+pgid(count))+1) * db.pageSize if minsz >= db.datasz { if err := db.mmap(minsz); err != nil { return nil, fmt.Errorf("mmap allocate error: %s", err) } } // Move the page id high water mark. db.rwtx.meta.pgid += pgid(count) return p, nil } // grow grows the size of the database to the given sz. func (db *DB) grow(sz int) error { // Ignore if the new size is less than available file size. if sz <= db.filesz { return nil } // If the data is smaller than the alloc size then only allocate what's needed. // Once it goes over the allocation size then allocate in chunks. if db.datasz <= db.AllocSize { sz = db.datasz } else { sz += db.AllocSize } // Truncate and fsync to ensure file size metadata is flushed. // https://github.com/boltdb/bolt/issues/284 if !db.NoGrowSync && !db.readOnly { if runtime.GOOS != "windows" { if err := db.file.Truncate(int64(sz)); err != nil { return fmt.Errorf("file resize error: %s", err) } } if err := db.file.Sync(); err != nil { return fmt.Errorf("file sync error: %s", err) } if db.Mlock { // unlock old file and lock new one if err := db.mrelock(db.filesz, sz); err != nil { return fmt.Errorf("mlock/munlock error: %s", err) } } } db.filesz = sz return nil } func (db *DB) IsReadOnly() bool { return db.readOnly } func (db *DB) freepages() []pgid { tx, err := db.beginTx() defer func() { err = tx.Rollback() if err != nil { panic("freepages: failed to rollback tx") } }() if err != nil { panic("freepages: failed to open read only tx") } reachable := make(map[pgid]*page) nofreed := make(map[pgid]bool) ech := make(chan error) go func() { for e := range ech { panic(fmt.Sprintf("freepages: failed to get all reachable pages (%v)", e)) } }() tx.checkBucket(&tx.root, reachable, nofreed, HexKVStringer(), ech) close(ech) // TODO: If check bucket reported any corruptions (ech) we shouldn't proceed to freeing the pages. var fids []pgid for i := pgid(2); i < db.meta().pgid; i++ { if _, ok := reachable[i]; !ok { fids = append(fids, i) } } return fids } // Options represents the options that can be set when opening a database. type Options struct { // Timeout is the amount of time to wait to obtain a file lock. // When set to zero it will wait indefinitely. This option is only // available on Darwin and Linux. Timeout time.Duration // Sets the DB.NoGrowSync flag before memory mapping the file. NoGrowSync bool // Do not sync freelist to disk. This improves the database write performance // under normal operation, but requires a full database re-sync during recovery. NoFreelistSync bool // PreLoadFreelist sets whether to load the free pages when opening // the db file. Note when opening db in write mode, bbolt will always // load the free pages. PreLoadFreelist bool // FreelistType sets the backend freelist type. There are two options. Array which is simple but endures // dramatic performance degradation if database is large and fragmentation in freelist is common. // The alternative one is using hashmap, it is faster in almost all circumstances // but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe. // The default type is array FreelistType FreelistType // Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to // grab a shared lock (UNIX). ReadOnly bool // Sets the DB.MmapFlags flag before memory mapping the file. MmapFlags int // InitialMmapSize is the initial mmap size of the database // in bytes. Read transactions won't block write transaction // if the InitialMmapSize is large enough to hold database mmap // size. (See DB.Begin for more information) // // If <=0, the initial map size is 0. // If initialMmapSize is smaller than the previous database size, // it takes no effect. InitialMmapSize int // PageSize overrides the default OS page size. PageSize int // NoSync sets the initial value of DB.NoSync. Normally this can just be // set directly on the DB itself when returned from Open(), but this option // is useful in APIs which expose Options but not the underlying DB. NoSync bool // OpenFile is used to open files. It defaults to os.OpenFile. This option // is useful for writing hermetic tests. OpenFile func(string, int, os.FileMode) (*os.File, error) // Mlock locks database file in memory when set to true. // It prevents potential page faults, however // used memory can't be reclaimed. (UNIX only) Mlock bool } // DefaultOptions represent the options used if nil options are passed into Open(). // No timeout is used which will cause Bolt to wait indefinitely for a lock. var DefaultOptions = &Options{ Timeout: 0, NoGrowSync: false, FreelistType: FreelistArrayType, } // Stats represents statistics about the database. type Stats struct { // Put `TxStats` at the first field to ensure it's 64-bit aligned. Note // that the first word in an allocated struct can be relied upon to be // 64-bit aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG. // Also refer to discussion in https://github.com/etcd-io/bbolt/issues/577. TxStats TxStats // global, ongoing stats. // Freelist stats FreePageN int // total number of free pages on the freelist PendingPageN int // total number of pending pages on the freelist FreeAlloc int // total bytes allocated in free pages FreelistInuse int // total bytes used by the freelist // Transaction stats TxN int // total number of started read transactions OpenTxN int // number of currently open read transactions } // Sub calculates and returns the difference between two sets of database stats. // This is useful when obtaining stats at two different points and time and // you need the performance counters that occurred within that time span. func (s *Stats) Sub(other *Stats) Stats { if other == nil { return *s } var diff Stats diff.FreePageN = s.FreePageN diff.PendingPageN = s.PendingPageN diff.FreeAlloc = s.FreeAlloc diff.FreelistInuse = s.FreelistInuse diff.TxN = s.TxN - other.TxN diff.TxStats = s.TxStats.Sub(&other.TxStats) return diff } type Info struct { Data uintptr PageSize int } type meta struct { magic uint32 version uint32 pageSize uint32 flags uint32 root bucket freelist pgid pgid pgid txid txid checksum uint64 } // validate checks the marker bytes and version of the meta page to ensure it matches this binary. func (m *meta) validate() error { if m.magic != magic { return ErrInvalid } else if m.version != version { return ErrVersionMismatch } else if m.checksum != m.sum64() { return ErrChecksum } return nil } // copy copies one meta object to another. func (m *meta) copy(dest *meta) { *dest = *m } // write writes the meta onto a page. func (m *meta) write(p *page) { if m.root.root >= m.pgid { panic(fmt.Sprintf("root bucket pgid (%d) above high water mark (%d)", m.root.root, m.pgid)) } else if m.freelist >= m.pgid && m.freelist != pgidNoFreelist { // TODO: reject pgidNoFreeList if !NoFreelistSync panic(fmt.Sprintf("freelist pgid (%d) above high water mark (%d)", m.freelist, m.pgid)) } // Page id is either going to be 0 or 1 which we can determine by the transaction ID. p.id = pgid(m.txid % 2) p.flags |= metaPageFlag // Calculate the checksum. m.checksum = m.sum64() m.copy(p.meta()) } // generates the checksum for the meta. func (m *meta) sum64() uint64 { var h = fnv.New64a() _, _ = h.Write((*[unsafe.Offsetof(meta{}.checksum)]byte)(unsafe.Pointer(m))[:]) return h.Sum64() } // _assert will panic with a given formatted message if the given condition is false. func _assert(condition bool, msg string, v ...interface{}) { if !condition { panic(fmt.Sprintf("assertion failed: "+msg, v...)) } } bbolt-1.3.10/db_test.go000066400000000000000000001130231461507540700146620ustar00rootroot00000000000000package bbolt_test import ( "bytes" "encoding/binary" "errors" "fmt" "hash/fnv" "log" "math/rand" "os" "path/filepath" "reflect" "sync" "testing" "time" "unsafe" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" ) // pageSize is the size of one page in the data file. const pageSize = 4096 // pageHeaderSize is the size of a page header. const pageHeaderSize = 16 // meta represents a simplified version of a database meta page for testing. type meta struct { _ uint32 version uint32 _ uint32 _ uint32 _ [16]byte _ uint64 pgid uint64 _ uint64 _ uint64 } // Ensure that a database can be opened without error. func TestOpen(t *testing.T) { path := tempfile() defer os.RemoveAll(path) db, err := bolt.Open(path, 0666, nil) if err != nil { t.Fatal(err) } else if db == nil { t.Fatal("expected db") } if s := db.Path(); s != path { t.Fatalf("unexpected path: %s", s) } if err := db.Close(); err != nil { t.Fatal(err) } } // Regression validation for https://github.com/etcd-io/bbolt/pull/122. // Tests multiple goroutines simultaneously opening a database. func TestOpen_MultipleGoroutines(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode") } const ( instances = 30 iterations = 30 ) path := tempfile() defer os.RemoveAll(path) var wg sync.WaitGroup errCh := make(chan error, iterations*instances) for iteration := 0; iteration < iterations; iteration++ { for instance := 0; instance < instances; instance++ { wg.Add(1) go func() { defer wg.Done() db, err := bolt.Open(path, 0600, nil) if err != nil { errCh <- err return } if err := db.Close(); err != nil { errCh <- err return } }() } wg.Wait() } close(errCh) for err := range errCh { if err != nil { t.Fatalf("error from inside goroutine: %v", err) } } } // Ensure that opening a database with a blank path returns an error. func TestOpen_ErrPathRequired(t *testing.T) { _, err := bolt.Open("", 0666, nil) if err == nil { t.Fatalf("expected error") } } // Ensure that opening a database with a bad path returns an error. func TestOpen_ErrNotExists(t *testing.T) { _, err := bolt.Open(filepath.Join(tempfile(), "bad-path"), 0666, nil) if err == nil { t.Fatal("expected error") } } // Ensure that opening a file that is not a Bolt database returns ErrInvalid. func TestOpen_ErrInvalid(t *testing.T) { path := tempfile() defer os.RemoveAll(path) f, err := os.Create(path) if err != nil { t.Fatal(err) } if _, err := fmt.Fprintln(f, "this is not a bolt database"); err != nil { t.Fatal(err) } if err := f.Close(); err != nil { t.Fatal(err) } if _, err := bolt.Open(path, 0666, nil); err != bolt.ErrInvalid { t.Fatalf("unexpected error: %s", err) } } // Ensure that opening a file with two invalid versions returns ErrVersionMismatch. func TestOpen_ErrVersionMismatch(t *testing.T) { if pageSize != os.Getpagesize() { t.Skip("page size mismatch") } // Create empty database. db := btesting.MustCreateDB(t) path := db.Path() // Close database. if err := db.Close(); err != nil { t.Fatal(err) } // Read data file. buf, err := os.ReadFile(path) if err != nil { t.Fatal(err) } // Rewrite meta pages. meta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize])) meta0.version++ meta1 := (*meta)(unsafe.Pointer(&buf[pageSize+pageHeaderSize])) meta1.version++ if err := os.WriteFile(path, buf, 0666); err != nil { t.Fatal(err) } // Reopen data file. if _, err := bolt.Open(path, 0666, nil); err != bolt.ErrVersionMismatch { t.Fatalf("unexpected error: %s", err) } } // Ensure that opening a file with two invalid checksums returns ErrChecksum. func TestOpen_ErrChecksum(t *testing.T) { if pageSize != os.Getpagesize() { t.Skip("page size mismatch") } // Create empty database. db := btesting.MustCreateDB(t) path := db.Path() // Close database. if err := db.Close(); err != nil { t.Fatal(err) } // Read data file. buf, err := os.ReadFile(path) if err != nil { t.Fatal(err) } // Rewrite meta pages. meta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize])) meta0.pgid++ meta1 := (*meta)(unsafe.Pointer(&buf[pageSize+pageHeaderSize])) meta1.pgid++ if err := os.WriteFile(path, buf, 0666); err != nil { t.Fatal(err) } // Reopen data file. if _, err := bolt.Open(path, 0666, nil); err != bolt.ErrChecksum { t.Fatalf("unexpected error: %s", err) } } // Ensure that it can read the page size from the second meta page if the first one is invalid. // The page size is expected to be the OS's page size in this case. func TestOpen_ReadPageSize_FromMeta1_OS(t *testing.T) { // Create empty database. db := btesting.MustCreateDB(t) path := db.Path() // Close the database db.MustClose() // Read data file. buf, err := os.ReadFile(path) if err != nil { t.Fatal(err) } // Rewrite first meta page. meta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize])) meta0.pgid++ if err := os.WriteFile(path, buf, 0666); err != nil { t.Fatal(err) } // Reopen data file. db = btesting.MustOpenDBWithOption(t, path, nil) require.Equalf(t, os.Getpagesize(), db.Info().PageSize, "check page size failed") } // Ensure that it can read the page size from the second meta page if the first one is invalid. // The page size is expected to be the given page size in this case. func TestOpen_ReadPageSize_FromMeta1_Given(t *testing.T) { // test page size from 1KB (1024<<0) to 16MB(1024<<14) for i := 0; i <= 14; i++ { givenPageSize := 1024 << uint(i) t.Logf("Testing page size %d", givenPageSize) // Create empty database. db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: givenPageSize}) path := db.Path() // Close the database db.MustClose() // Read data file. buf, err := os.ReadFile(path) require.NoError(t, err) // Rewrite meta pages. if i%3 == 0 { t.Logf("#%d: Intentionally corrupt the first meta page for pageSize %d", i, givenPageSize) meta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize])) meta0.pgid++ err = os.WriteFile(path, buf, 0666) require.NoError(t, err) } // Reopen data file. db = btesting.MustOpenDBWithOption(t, path, nil) require.Equalf(t, givenPageSize, db.Info().PageSize, "check page size failed") db.MustClose() } } // Ensure that opening a database does not increase its size. // https://github.com/boltdb/bolt/issues/291 func TestOpen_Size(t *testing.T) { // Open a data file. db := btesting.MustCreateDB(t) pagesize := db.Info().PageSize // Insert until we get above the minimum 4MB size. err := db.Fill([]byte("data"), 1, 10000, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 1000) }, ) if err != nil { t.Fatal(err) } path := db.Path() db.MustClose() sz := fileSize(path) if sz == 0 { t.Fatalf("unexpected new file size: %d", sz) } db.MustReopen() if err := db.Update(func(tx *bolt.Tx) error { if err := tx.Bucket([]byte("data")).Put([]byte{0}, []byte{0}); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Close(); err != nil { t.Fatal(err) } newSz := fileSize(path) if newSz == 0 { t.Fatalf("unexpected new file size: %d", newSz) } // Compare the original size with the new size. // db size might increase by a few page sizes due to the new small update. if sz < newSz-5*int64(pagesize) { t.Fatalf("unexpected file growth: %d => %d", sz, newSz) } } // Ensure that opening a database beyond the max step size does not increase its size. // https://github.com/boltdb/bolt/issues/303 func TestOpen_Size_Large(t *testing.T) { if testing.Short() { t.Skip("short mode") } // Open a data file. db := btesting.MustCreateDB(t) path := db.Path() pagesize := db.Info().PageSize // Insert until we get above the minimum 4MB size. var index uint64 for i := 0; i < 10000; i++ { if err := db.Update(func(tx *bolt.Tx) error { b, _ := tx.CreateBucketIfNotExists([]byte("data")) for j := 0; j < 1000; j++ { if err := b.Put(u64tob(index), make([]byte, 50)); err != nil { t.Fatal(err) } index++ } return nil }); err != nil { t.Fatal(err) } } // Close database and grab the size. if err := db.Close(); err != nil { t.Fatal(err) } sz := fileSize(path) if sz == 0 { t.Fatalf("unexpected new file size: %d", sz) } else if sz < (1 << 30) { t.Fatalf("expected larger initial size: %d", sz) } // Reopen database, update, and check size again. db0, err := bolt.Open(path, 0666, nil) if err != nil { t.Fatal(err) } if err := db0.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("data")).Put([]byte{0}, []byte{0}) }); err != nil { t.Fatal(err) } if err := db0.Close(); err != nil { t.Fatal(err) } newSz := fileSize(path) if newSz == 0 { t.Fatalf("unexpected new file size: %d", newSz) } // Compare the original size with the new size. // db size might increase by a few page sizes due to the new small update. if sz < newSz-5*int64(pagesize) { t.Fatalf("unexpected file growth: %d => %d", sz, newSz) } } // Ensure that a re-opened database is consistent. func TestOpen_Check(t *testing.T) { path := tempfile() defer os.RemoveAll(path) db, err := bolt.Open(path, 0666, nil) if err != nil { t.Fatal(err) } if err = db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil { t.Fatal(err) } if err = db.Close(); err != nil { t.Fatal(err) } db, err = bolt.Open(path, 0666, nil) if err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil { t.Fatal(err) } if err := db.Close(); err != nil { t.Fatal(err) } } // Ensure that write errors to the meta file handler during initialization are returned. func TestOpen_MetaInitWriteError(t *testing.T) { t.Skip("pending") } // Ensure that a database that is too small returns an error. func TestOpen_FileTooSmall(t *testing.T) { path := tempfile() defer os.RemoveAll(path) db, err := bolt.Open(path, 0666, nil) if err != nil { t.Fatal(err) } pageSize := int64(db.Info().PageSize) if err = db.Close(); err != nil { t.Fatal(err) } // corrupt the database if err = os.Truncate(path, pageSize); err != nil { t.Fatal(err) } _, err = bolt.Open(path, 0666, nil) if err == nil || err.Error() != "file size too small" { t.Fatalf("unexpected error: %s", err) } } // TestDB_Open_InitialMmapSize tests if having InitialMmapSize large enough // to hold data from concurrent write transaction resolves the issue that // read transaction blocks the write transaction and causes deadlock. // This is a very hacky test since the mmap size is not exposed. func TestDB_Open_InitialMmapSize(t *testing.T) { path := tempfile() defer os.Remove(path) initMmapSize := 1 << 30 // 1GB testWriteSize := 1 << 27 // 134MB db, err := bolt.Open(path, 0666, &bolt.Options{InitialMmapSize: initMmapSize}) if err != nil { t.Fatal(err) } // create a long-running read transaction // that never gets closed while writing rtx, err := db.Begin(false) if err != nil { t.Fatal(err) } // create a write transaction wtx, err := db.Begin(true) if err != nil { t.Fatal(err) } b, err := wtx.CreateBucket([]byte("test")) if err != nil { t.Fatal(err) } // and commit a large write err = b.Put([]byte("foo"), make([]byte, testWriteSize)) if err != nil { t.Fatal(err) } done := make(chan error, 1) go func() { err := wtx.Commit() done <- err }() select { case <-time.After(5 * time.Second): t.Errorf("unexpected that the reader blocks writer") case err := <-done: if err != nil { t.Fatal(err) } } if err := rtx.Rollback(); err != nil { t.Fatal(err) } } // TestDB_Open_ReadOnly checks a database in read only mode can read but not write. func TestDB_Open_ReadOnly(t *testing.T) { // Create a writable db, write k-v and close it. db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Close(); err != nil { t.Fatal(err) } f := db.Path() o := &bolt.Options{ReadOnly: true} readOnlyDB, err := bolt.Open(f, 0666, o) if err != nil { panic(err) } if !readOnlyDB.IsReadOnly() { t.Fatal("expect db in read only mode") } // Read from a read-only transaction. if err := readOnlyDB.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) if !bytes.Equal(value, []byte("bar")) { t.Fatal("expect value 'bar', got", value) } return nil }); err != nil { t.Fatal(err) } // Can't launch read-write transaction. if _, err := readOnlyDB.Begin(true); err != bolt.ErrDatabaseReadOnly { t.Fatalf("unexpected error: %s", err) } if err := readOnlyDB.Close(); err != nil { t.Fatal(err) } } // TestOpen_BigPage checks the database uses bigger pages when // changing PageSize. func TestOpen_BigPage(t *testing.T) { pageSize := os.Getpagesize() db1 := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize * 2}) db2 := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize * 4}) if db1sz, db2sz := fileSize(db1.Path()), fileSize(db2.Path()); db1sz >= db2sz { t.Errorf("expected %d < %d", db1sz, db2sz) } } // TestOpen_RecoverFreeList tests opening the DB with free-list // write-out after no free list sync will recover the free list // and write it out. func TestOpen_RecoverFreeList(t *testing.T) { db := btesting.MustCreateDBWithOption(t, &bolt.Options{NoFreelistSync: true}) // Write some pages. tx, err := db.Begin(true) if err != nil { t.Fatal(err) } wbuf := make([]byte, 8192) for i := 0; i < 100; i++ { s := fmt.Sprintf("%d", i) b, err := tx.CreateBucket([]byte(s)) if err != nil { t.Fatal(err) } if err = b.Put([]byte(s), wbuf); err != nil { t.Fatal(err) } } if err = tx.Commit(); err != nil { t.Fatal(err) } // Generate free pages. if tx, err = db.Begin(true); err != nil { t.Fatal(err) } for i := 0; i < 50; i++ { s := fmt.Sprintf("%d", i) b := tx.Bucket([]byte(s)) if b == nil { t.Fatal(err) } if err := b.Delete([]byte(s)); err != nil { t.Fatal(err) } } if err := tx.Commit(); err != nil { t.Fatal(err) } db.MustClose() // Record freelist count from opening with NoFreelistSync. db.MustReopen() freepages := db.Stats().FreePageN if freepages == 0 { t.Fatalf("no free pages on NoFreelistSync reopen") } db.MustClose() // Check free page count is reconstructed when opened with freelist sync. db.SetOptions(&bolt.Options{}) db.MustReopen() // One less free page for syncing the free list on open. freepages-- if fp := db.Stats().FreePageN; fp < freepages { t.Fatalf("closed with %d free pages, opened with %d", freepages, fp) } } // Ensure that a database cannot open a transaction when it's not open. func TestDB_Begin_ErrDatabaseNotOpen(t *testing.T) { var db bolt.DB if _, err := db.Begin(false); err != bolt.ErrDatabaseNotOpen { t.Fatalf("unexpected error: %s", err) } } // Ensure that a read-write transaction can be retrieved. func TestDB_BeginRW(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) require.NoError(t, err) require.NotNil(t, tx, "expected tx") defer func() { require.NoError(t, tx.Commit()) }() require.True(t, tx.Writable(), "expected writable tx") require.Same(t, db.DB, tx.DB()) } // TestDB_Concurrent_WriteTo checks that issuing WriteTo operations concurrently // with commits does not produce corrupted db files. func TestDB_Concurrent_WriteTo(t *testing.T) { o := &bolt.Options{NoFreelistSync: false} db := btesting.MustCreateDBWithOption(t, o) var wg sync.WaitGroup wtxs, rtxs := 5, 5 wg.Add(wtxs * rtxs) f := func(tx *bolt.Tx) { defer wg.Done() f, err := os.CreateTemp("", "bolt-") if err != nil { panic(err) } time.Sleep(time.Duration(rand.Intn(20)+1) * time.Millisecond) _, err = tx.WriteTo(f) if err != nil { panic(err) } err = tx.Rollback() if err != nil { panic(err) } f.Close() copyOpt := *o snap := btesting.MustOpenDBWithOption(t, f.Name(), ©Opt) defer snap.MustClose() snap.MustCheck() } tx1, err := db.Begin(true) if err != nil { t.Fatal(err) } if _, err := tx1.CreateBucket([]byte("abc")); err != nil { t.Fatal(err) } if err := tx1.Commit(); err != nil { t.Fatal(err) } for i := 0; i < wtxs; i++ { tx, err := db.Begin(true) if err != nil { t.Fatal(err) } if err := tx.Bucket([]byte("abc")).Put([]byte{0}, []byte{0}); err != nil { t.Fatal(err) } for j := 0; j < rtxs; j++ { rtx, rerr := db.Begin(false) if rerr != nil { t.Fatal(rerr) } go f(rtx) } if err := tx.Commit(); err != nil { t.Fatal(err) } } wg.Wait() } // Ensure that opening a transaction while the DB is closed returns an error. func TestDB_BeginRW_Closed(t *testing.T) { var db bolt.DB if _, err := db.Begin(true); err != bolt.ErrDatabaseNotOpen { t.Fatalf("unexpected error: %s", err) } } func TestDB_Close_PendingTx_RW(t *testing.T) { testDB_Close_PendingTx(t, true) } func TestDB_Close_PendingTx_RO(t *testing.T) { testDB_Close_PendingTx(t, false) } // Ensure that a database cannot close while transactions are open. func testDB_Close_PendingTx(t *testing.T, writable bool) { db := btesting.MustCreateDB(t) // Start transaction. tx, err := db.Begin(writable) if err != nil { t.Fatal(err) } // Open update in separate goroutine. done := make(chan error, 1) go func() { err := db.Close() done <- err }() // Ensure database hasn't closed. time.Sleep(100 * time.Millisecond) select { case err := <-done: if err != nil { t.Errorf("error from inside goroutine: %v", err) } t.Fatal("database closed too early") default: } // Commit/close transaction. if writable { err = tx.Commit() } else { err = tx.Rollback() } if err != nil { t.Fatal(err) } // Ensure database closed now. time.Sleep(100 * time.Millisecond) select { case err := <-done: if err != nil { t.Fatalf("error from inside goroutine: %v", err) } default: t.Fatal("database did not close") } } // Ensure a database can provide a transactional block. func TestDB_Update(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if err := b.Put([]byte("baz"), []byte("bat")); err != nil { t.Fatal(err) } if err := b.Delete([]byte("foo")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) if v := b.Get([]byte("foo")); v != nil { t.Fatalf("expected nil value, got: %v", v) } if v := b.Get([]byte("baz")); !bytes.Equal(v, []byte("bat")) { t.Fatalf("unexpected value: %v", v) } return nil }); err != nil { t.Fatal(err) } } // Ensure a closed database returns an error while running a transaction block func TestDB_Update_Closed(t *testing.T) { var db bolt.DB if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != bolt.ErrDatabaseNotOpen { t.Fatalf("unexpected error: %s", err) } } // Ensure a panic occurs while trying to commit a managed transaction. func TestDB_Update_ManualCommit(t *testing.T) { db := btesting.MustCreateDB(t) var panicked bool if err := db.Update(func(tx *bolt.Tx) error { func() { defer func() { if r := recover(); r != nil { panicked = true } }() if err := tx.Commit(); err != nil { t.Fatal(err) } }() return nil }); err != nil { t.Fatal(err) } else if !panicked { t.Fatal("expected panic") } } // Ensure a panic occurs while trying to rollback a managed transaction. func TestDB_Update_ManualRollback(t *testing.T) { db := btesting.MustCreateDB(t) var panicked bool if err := db.Update(func(tx *bolt.Tx) error { func() { defer func() { if r := recover(); r != nil { panicked = true } }() if err := tx.Rollback(); err != nil { t.Fatal(err) } }() return nil }); err != nil { t.Fatal(err) } else if !panicked { t.Fatal("expected panic") } } // Ensure a panic occurs while trying to commit a managed transaction. func TestDB_View_ManualCommit(t *testing.T) { db := btesting.MustCreateDB(t) var panicked bool if err := db.View(func(tx *bolt.Tx) error { func() { defer func() { if r := recover(); r != nil { panicked = true } }() if err := tx.Commit(); err != nil { t.Fatal(err) } }() return nil }); err != nil { t.Fatal(err) } else if !panicked { t.Fatal("expected panic") } } // Ensure a panic occurs while trying to rollback a managed transaction. func TestDB_View_ManualRollback(t *testing.T) { db := btesting.MustCreateDB(t) var panicked bool if err := db.View(func(tx *bolt.Tx) error { func() { defer func() { if r := recover(); r != nil { panicked = true } }() if err := tx.Rollback(); err != nil { t.Fatal(err) } }() return nil }); err != nil { t.Fatal(err) } else if !panicked { t.Fatal("expected panic") } } // Ensure a write transaction that panics does not hold open locks. func TestDB_Update_Panic(t *testing.T) { db := btesting.MustCreateDB(t) // Panic during update but recover. func() { defer func() { if r := recover(); r != nil { t.Log("recover: update", r) } }() if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } panic("omg") }); err != nil { t.Fatal(err) } }() // Verify we can update again. if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } // Verify that our change persisted. if err := db.Update(func(tx *bolt.Tx) error { if tx.Bucket([]byte("widgets")) == nil { t.Fatal("expected bucket") } return nil }); err != nil { t.Fatal(err) } } // Ensure a database can return an error through a read-only transactional block. func TestDB_View_Error(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.View(func(tx *bolt.Tx) error { return errors.New("xxx") }); err == nil || err.Error() != "xxx" { t.Fatalf("unexpected error: %s", err) } } // Ensure a read transaction that panics does not hold open locks. func TestDB_View_Panic(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } // Panic during view transaction but recover. func() { defer func() { if r := recover(); r != nil { t.Log("recover: view", r) } }() if err := db.View(func(tx *bolt.Tx) error { if tx.Bucket([]byte("widgets")) == nil { t.Fatal("expected bucket") } panic("omg") }); err != nil { t.Fatal(err) } }() // Verify that we can still use read transactions. if err := db.View(func(tx *bolt.Tx) error { if tx.Bucket([]byte("widgets")) == nil { t.Fatal("expected bucket") } return nil }); err != nil { t.Fatal(err) } } // Ensure that DB stats can be returned. func TestDB_Stats(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { t.Fatal(err) } stats := db.Stats() if stats.TxStats.GetPageCount() != 2 { t.Fatalf("unexpected TxStats.PageCount: %d", stats.TxStats.GetPageCount()) } else if stats.FreePageN != 0 { t.Fatalf("unexpected FreePageN != 0: %d", stats.FreePageN) } else if stats.PendingPageN != 2 { t.Fatalf("unexpected PendingPageN != 2: %d", stats.PendingPageN) } } // Ensure that database pages are in expected order and type. func TestDB_Consistency(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { t.Fatal(err) } for i := 0; i < 10; i++ { if err := db.Update(func(tx *bolt.Tx) error { if err := tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } } if err := db.Update(func(tx *bolt.Tx) error { if p, _ := tx.Page(0); p == nil { t.Fatal("expected page") } else if p.Type != "meta" { t.Fatalf("unexpected page type: %s", p.Type) } if p, _ := tx.Page(1); p == nil { t.Fatal("expected page") } else if p.Type != "meta" { t.Fatalf("unexpected page type: %s", p.Type) } if p, _ := tx.Page(2); p == nil { t.Fatal("expected page") } else if p.Type != "free" { t.Fatalf("unexpected page type: %s", p.Type) } if p, _ := tx.Page(3); p == nil { t.Fatal("expected page") } else if p.Type != "free" { t.Fatalf("unexpected page type: %s", p.Type) } if p, _ := tx.Page(4); p == nil { t.Fatal("expected page") } else if p.Type != "leaf" { t.Fatalf("unexpected page type: %s", p.Type) } if p, _ := tx.Page(5); p == nil { t.Fatal("expected page") } else if p.Type != "freelist" { t.Fatalf("unexpected page type: %s", p.Type) } if p, _ := tx.Page(6); p != nil { t.Fatal("unexpected page") } return nil }); err != nil { t.Fatal(err) } } // Ensure that DB stats can be subtracted from one another. func TestDBStats_Sub(t *testing.T) { var a, b bolt.Stats a.TxStats.PageCount = 3 a.FreePageN = 4 b.TxStats.PageCount = 10 b.FreePageN = 14 diff := b.Sub(&a) if diff.TxStats.GetPageCount() != 7 { t.Fatalf("unexpected TxStats.PageCount: %d", diff.TxStats.GetPageCount()) } // free page stats are copied from the receiver and not subtracted if diff.FreePageN != 14 { t.Fatalf("unexpected FreePageN: %d", diff.FreePageN) } } // Ensure two functions can perform updates in a single batch. func TestDB_Batch(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } // Iterate over multiple updates in separate goroutines. n := 2 ch := make(chan error, n) for i := 0; i < n; i++ { go func(i int) { ch <- db.Batch(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Put(u64tob(uint64(i)), []byte{}) }) }(i) } // Check all responses to make sure there's no error. for i := 0; i < n; i++ { if err := <-ch; err != nil { t.Fatal(err) } } // Ensure data is correct. if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for i := 0; i < n; i++ { if v := b.Get(u64tob(uint64(i))); v == nil { t.Errorf("key not found: %d", i) } } return nil }); err != nil { t.Fatal(err) } } func TestDB_Batch_Panic(t *testing.T) { db := btesting.MustCreateDB(t) var sentinel int var bork = &sentinel var problem interface{} var err error // Execute a function inside a batch that panics. func() { defer func() { if p := recover(); p != nil { problem = p } }() err = db.Batch(func(tx *bolt.Tx) error { panic(bork) }) }() // Verify there is no error. if g, e := err, error(nil); g != e { t.Fatalf("wrong error: %v != %v", g, e) } // Verify the panic was captured. if g, e := problem, bork; g != e { t.Fatalf("wrong error: %v != %v", g, e) } } func TestDB_BatchFull(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { t.Fatal(err) } const size = 3 // buffered so we never leak goroutines ch := make(chan error, size) put := func(i int) { ch <- db.Batch(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Put(u64tob(uint64(i)), []byte{}) }) } db.MaxBatchSize = size // high enough to never trigger here db.MaxBatchDelay = 1 * time.Hour go put(1) go put(2) // Give the batch a chance to exhibit bugs. time.Sleep(10 * time.Millisecond) // not triggered yet select { case <-ch: t.Fatalf("batch triggered too early") default: } go put(3) // Check all responses to make sure there's no error. for i := 0; i < size; i++ { if err := <-ch; err != nil { t.Fatal(err) } } // Ensure data is correct. if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for i := 1; i <= size; i++ { if v := b.Get(u64tob(uint64(i))); v == nil { t.Errorf("key not found: %d", i) } } return nil }); err != nil { t.Fatal(err) } } func TestDB_BatchTime(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { t.Fatal(err) } const size = 1 // buffered so we never leak goroutines ch := make(chan error, size) put := func(i int) { ch <- db.Batch(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Put(u64tob(uint64(i)), []byte{}) }) } db.MaxBatchSize = 1000 db.MaxBatchDelay = 0 go put(1) // Batch must trigger by time alone. // Check all responses to make sure there's no error. for i := 0; i < size; i++ { if err := <-ch; err != nil { t.Fatal(err) } } // Ensure data is correct. if err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("widgets")) for i := 1; i <= size; i++ { if v := b.Get(u64tob(uint64(i))); v == nil { t.Errorf("key not found: %d", i) } } return nil }); err != nil { t.Fatal(err) } } // TestDBUnmap verifes that `dataref`, `data` and `datasz` must be reset // to zero values respectively after unmapping the db. func TestDBUnmap(t *testing.T) { db := btesting.MustCreateDB(t) require.NoError(t, db.DB.Close()) // Ignore the following error: // Error: copylocks: call of reflect.ValueOf copies lock value: go.etcd.io/bbolt.DB contains sync.Once contains sync.Mutex (govet) //nolint:govet v := reflect.ValueOf(*db.DB) dataref := v.FieldByName("dataref") data := v.FieldByName("data") datasz := v.FieldByName("datasz") assert.True(t, dataref.IsNil()) assert.True(t, data.IsNil()) assert.True(t, datasz.IsZero()) // Set db.DB to nil to prevent MustCheck from panicking. db.DB = nil } func ExampleDB_Update() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Execute several commands within a read-write transaction. if 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 }); err != nil { log.Fatal(err) } // Read the value back from a separate read-only transaction. if err := db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value of 'foo' is: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Close database to release the file lock. if err := db.Close(); err != nil { log.Fatal(err) } // Output: // The value of 'foo' is: bar } func ExampleDB_View() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Insert data into a bucket. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("people")) if err != nil { return err } if err := b.Put([]byte("john"), []byte("doe")); err != nil { return err } if err := b.Put([]byte("susy"), []byte("que")); err != nil { return err } return nil }); err != nil { log.Fatal(err) } // Access data from within a read-only transactional block. if err := db.View(func(tx *bolt.Tx) error { v := tx.Bucket([]byte("people")).Get([]byte("john")) fmt.Printf("John's last name is %s.\n", v) return nil }); err != nil { log.Fatal(err) } // Close database to release the file lock. if err := db.Close(); err != nil { log.Fatal(err) } // Output: // John's last name is doe. } func ExampleDB_Begin() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Create a bucket using a read-write transaction. if err = db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { log.Fatal(err) } // Create several keys in a transaction. tx, err := db.Begin(true) if err != nil { log.Fatal(err) } b := tx.Bucket([]byte("widgets")) if err = b.Put([]byte("john"), []byte("blue")); err != nil { log.Fatal(err) } if err = b.Put([]byte("abby"), []byte("red")); err != nil { log.Fatal(err) } if err = b.Put([]byte("zephyr"), []byte("purple")); err != nil { log.Fatal(err) } if err = tx.Commit(); err != nil { log.Fatal(err) } // Iterate over the values in sorted key order. tx, err = db.Begin(false) if err != nil { log.Fatal(err) } c := tx.Bucket([]byte("widgets")).Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { fmt.Printf("%s likes %s\n", k, v) } if err = tx.Rollback(); err != nil { log.Fatal(err) } if err = db.Close(); err != nil { log.Fatal(err) } // Output: // abby likes red // john likes blue // zephyr likes purple } func BenchmarkDBBatchAutomatic(b *testing.B) { db := btesting.MustCreateDB(b) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("bench")) return err }); err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { start := make(chan struct{}) var wg sync.WaitGroup for round := 0; round < 1000; round++ { wg.Add(1) go func(id uint32) { defer wg.Done() <-start h := fnv.New32a() buf := make([]byte, 4) binary.LittleEndian.PutUint32(buf, id) _, _ = h.Write(buf[:]) k := h.Sum(nil) insert := func(tx *bolt.Tx) error { b := tx.Bucket([]byte("bench")) return b.Put(k, []byte("filler")) } if err := db.Batch(insert); err != nil { b.Error(err) return } }(uint32(round)) } close(start) wg.Wait() } b.StopTimer() validateBatchBench(b, db) } func BenchmarkDBBatchSingle(b *testing.B) { db := btesting.MustCreateDB(b) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("bench")) return err }); err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { start := make(chan struct{}) var wg sync.WaitGroup for round := 0; round < 1000; round++ { wg.Add(1) go func(id uint32) { defer wg.Done() <-start h := fnv.New32a() buf := make([]byte, 4) binary.LittleEndian.PutUint32(buf, id) _, _ = h.Write(buf[:]) k := h.Sum(nil) insert := func(tx *bolt.Tx) error { b := tx.Bucket([]byte("bench")) return b.Put(k, []byte("filler")) } if err := db.Update(insert); err != nil { b.Error(err) return } }(uint32(round)) } close(start) wg.Wait() } b.StopTimer() validateBatchBench(b, db) } func BenchmarkDBBatchManual10x100(b *testing.B) { db := btesting.MustCreateDB(b) if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("bench")) return err }); err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { start := make(chan struct{}) var wg sync.WaitGroup errCh := make(chan error, 10) for major := 0; major < 10; major++ { wg.Add(1) go func(id uint32) { defer wg.Done() <-start insert100 := func(tx *bolt.Tx) error { h := fnv.New32a() buf := make([]byte, 4) for minor := uint32(0); minor < 100; minor++ { binary.LittleEndian.PutUint32(buf, uint32(id*100+minor)) h.Reset() _, _ = h.Write(buf[:]) k := h.Sum(nil) b := tx.Bucket([]byte("bench")) if err := b.Put(k, []byte("filler")); err != nil { return err } } return nil } err := db.Update(insert100) errCh <- err }(uint32(major)) } close(start) wg.Wait() close(errCh) for err := range errCh { if err != nil { b.Fatal(err) } } } b.StopTimer() validateBatchBench(b, db) } func validateBatchBench(b *testing.B, db *btesting.DB) { var rollback = errors.New("sentinel error to cause rollback") validate := func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte("bench")) h := fnv.New32a() buf := make([]byte, 4) for id := uint32(0); id < 1000; id++ { binary.LittleEndian.PutUint32(buf, id) h.Reset() _, _ = h.Write(buf[:]) k := h.Sum(nil) v := bucket.Get(k) if v == nil { b.Errorf("not found id=%d key=%x", id, k) continue } if g, e := v, []byte("filler"); !bytes.Equal(g, e) { b.Errorf("bad value for id=%d key=%x: %s != %q", id, k, g, e) } if err := bucket.Delete(k); err != nil { return err } } // should be empty now c := bucket.Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { b.Errorf("unexpected key: %x = %q", k, v) } return rollback } if err := db.Update(validate); err != nil && err != rollback { b.Error(err) } } // tempfile returns a temporary file path. func tempfile() string { f, err := os.CreateTemp("", "bolt-") if err != nil { panic(err) } if err := f.Close(); err != nil { panic(err) } if err := os.Remove(f.Name()); err != nil { panic(err) } return f.Name() } func trunc(b []byte, length int) []byte { if length < len(b) { return b[:length] } return b } func fileSize(path string) int64 { fi, err := os.Stat(path) if err != nil { return 0 } return fi.Size() } // u64tob converts a uint64 into an 8-byte slice. func u64tob(v uint64) []byte { b := make([]byte, 8) binary.BigEndian.PutUint64(b, v) return b } bbolt-1.3.10/db_whitebox_test.go000066400000000000000000000053121461507540700165740ustar00rootroot00000000000000package bbolt import ( "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestOpenWithPreLoadFreelist(t *testing.T) { testCases := []struct { name string readonly bool preLoadFreePage bool expectedFreePagesLoaded bool }{ { name: "write mode always load free pages", readonly: false, preLoadFreePage: false, expectedFreePagesLoaded: true, }, { name: "readonly mode load free pages when flag set", readonly: true, preLoadFreePage: true, expectedFreePagesLoaded: true, }, { name: "readonly mode doesn't load free pages when flag not set", readonly: true, preLoadFreePage: false, expectedFreePagesLoaded: false, }, } fileName, err := prepareData(t) require.NoError(t, err) for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { db, err := Open(fileName, 0666, &Options{ ReadOnly: tc.readonly, PreLoadFreelist: tc.preLoadFreePage, }) require.NoError(t, err) assert.Equal(t, tc.expectedFreePagesLoaded, db.freelist != nil) assert.NoError(t, db.Close()) }) } } func TestMethodPage(t *testing.T) { testCases := []struct { name string readonly bool preLoadFreePage bool expectedError error }{ { name: "write mode", readonly: false, preLoadFreePage: false, expectedError: nil, }, { name: "readonly mode with preloading free pages", readonly: true, preLoadFreePage: true, expectedError: nil, }, { name: "readonly mode without preloading free pages", readonly: true, preLoadFreePage: false, expectedError: ErrFreePagesNotLoaded, }, } fileName, err := prepareData(t) require.NoError(t, err) for _, tc := range testCases { tc := tc t.Run(tc.name, func(t *testing.T) { db, err := Open(fileName, 0666, &Options{ ReadOnly: tc.readonly, PreLoadFreelist: tc.preLoadFreePage, }) require.NoError(t, err) defer db.Close() tx, err := db.Begin(!tc.readonly) require.NoError(t, err) _, err = tx.Page(0) require.Equal(t, tc.expectedError, err) if tc.readonly { require.NoError(t, tx.Rollback()) } else { require.NoError(t, tx.Commit()) } require.NoError(t, db.Close()) }) } } func prepareData(t *testing.T) (string, error) { fileName := filepath.Join(t.TempDir(), "db") db, err := Open(fileName, 0666, nil) if err != nil { return "", err } if err := db.Close(); err != nil { return "", err } return fileName, nil } bbolt-1.3.10/doc.go000066400000000000000000000033671461507540700140140ustar00rootroot00000000000000/* package bbolt implements a low-level key/value store in pure Go. It supports fully serializable transactions, ACID semantics, and lock-free MVCC with multiple readers and a single writer. Bolt can be used for projects that want a simple data store without the need to add large dependencies such as Postgres or MySQL. Bolt is a single-level, zero-copy, B+tree data store. This means that Bolt is optimized for fast read access and does not require recovery in the event of a system crash. Transactions which have not finished committing will simply be rolled back in the event of a crash. The design of Bolt is based on Howard Chu's LMDB database project. Bolt currently works on Windows, Mac OS X, and Linux. # Basics There are only a few types in Bolt: DB, Bucket, Tx, and Cursor. The DB is a collection of buckets and is represented by a single file on disk. A bucket is a collection of unique keys that are associated with values. Transactions provide either read-only or read-write access to the database. Read-only transactions can retrieve key/value pairs and can use Cursors to iterate over the dataset sequentially. Read-write transactions can create and delete buckets and can insert and remove keys. Only one read-write transaction is allowed at a time. # Caveats The database uses a read-only, memory-mapped data file to ensure that applications cannot corrupt the database, however, this means that keys and values returned from Bolt cannot be changed. Writing to a read-only byte slice will cause Go to panic. Keys and values retrieved from the database are only valid for the life of the transaction. When used outside the transaction, these byte slices can point to different data or can point to invalid memory which will cause a panic. */ package bbolt bbolt-1.3.10/errors.go000066400000000000000000000060341461507540700145550ustar00rootroot00000000000000package bbolt import "errors" // These errors can be returned when opening or calling methods on a DB. var ( // ErrDatabaseNotOpen is returned when a DB instance is accessed before it // is opened or after it is closed. ErrDatabaseNotOpen = errors.New("database not open") // ErrDatabaseOpen is returned when opening a database that is // already open. ErrDatabaseOpen = errors.New("database already open") // ErrInvalid is returned when both meta pages on a database are invalid. // This typically occurs when a file is not a bolt database. ErrInvalid = errors.New("invalid database") // ErrInvalidMapping is returned when the database file fails to get mapped. ErrInvalidMapping = errors.New("database isn't correctly mapped") // ErrVersionMismatch is returned when the data file was created with a // different version of Bolt. ErrVersionMismatch = errors.New("version mismatch") // ErrChecksum is returned when either meta page checksum does not match. ErrChecksum = errors.New("checksum error") // ErrTimeout is returned when a database cannot obtain an exclusive lock // on the data file after the timeout passed to Open(). ErrTimeout = errors.New("timeout") ) // These errors can occur when beginning or committing a Tx. var ( // ErrTxNotWritable is returned when performing a write operation on a // read-only transaction. ErrTxNotWritable = errors.New("tx not writable") // ErrTxClosed is returned when committing or rolling back a transaction // that has already been committed or rolled back. ErrTxClosed = errors.New("tx closed") // ErrDatabaseReadOnly is returned when a mutating transaction is started on a // read-only database. ErrDatabaseReadOnly = errors.New("database is in read-only mode") // ErrFreePagesNotLoaded is returned when a readonly transaction without // preloading the free pages is trying to access the free pages. ErrFreePagesNotLoaded = errors.New("free pages are not pre-loaded") ) // These errors can occur when putting or deleting a value or a bucket. var ( // ErrBucketNotFound is returned when trying to access a bucket that has // not been created yet. ErrBucketNotFound = errors.New("bucket not found") // ErrBucketExists is returned when creating a bucket that already exists. ErrBucketExists = errors.New("bucket already exists") // ErrBucketNameRequired is returned when creating a bucket with a blank name. ErrBucketNameRequired = errors.New("bucket name required") // ErrKeyRequired is returned when inserting a zero-length key. ErrKeyRequired = errors.New("key required") // ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize. ErrKeyTooLarge = errors.New("key too large") // ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize. ErrValueTooLarge = errors.New("value too large") // ErrIncompatibleValue is returned when trying create or delete a bucket // on an existing non-bucket key or when trying to create or delete a // non-bucket key on an existing bucket key. ErrIncompatibleValue = errors.New("incompatible value") ) bbolt-1.3.10/freelist.go000066400000000000000000000262601461507540700150610ustar00rootroot00000000000000package bbolt import ( "fmt" "sort" "unsafe" ) // txPending holds a list of pgids and corresponding allocation txns // that are pending to be freed. type txPending struct { ids []pgid alloctx []txid // txids allocating the ids lastReleaseBegin txid // beginning txid of last matching releaseRange } // pidSet holds the set of starting pgids which have the same span size type pidSet map[pgid]struct{} // freelist represents a list of all pages that are available for allocation. // It also tracks pages that have been freed but are still in use by open transactions. type freelist struct { freelistType FreelistType // freelist type ids []pgid // all free and available free page ids. allocs map[pgid]txid // mapping of txid that allocated a pgid. pending map[txid]*txPending // mapping of soon-to-be free page ids by tx. cache map[pgid]struct{} // fast lookup of all free and pending page ids. freemaps map[uint64]pidSet // key is the size of continuous pages(span), value is a set which contains the starting pgids of same size forwardMap map[pgid]uint64 // key is start pgid, value is its span size backwardMap map[pgid]uint64 // key is end pgid, value is its span size allocate func(txid txid, n int) pgid // the freelist allocate func free_count func() int // the function which gives you free page number mergeSpans func(ids pgids) // the mergeSpan func getFreePageIDs func() []pgid // get free pgids func readIDs func(pgids []pgid) // readIDs func reads list of pages and init the freelist } // newFreelist returns an empty, initialized freelist. func newFreelist(freelistType FreelistType) *freelist { f := &freelist{ freelistType: freelistType, allocs: make(map[pgid]txid), pending: make(map[txid]*txPending), cache: make(map[pgid]struct{}), freemaps: make(map[uint64]pidSet), forwardMap: make(map[pgid]uint64), backwardMap: make(map[pgid]uint64), } if freelistType == FreelistMapType { f.allocate = f.hashmapAllocate f.free_count = f.hashmapFreeCount f.mergeSpans = f.hashmapMergeSpans f.getFreePageIDs = f.hashmapGetFreePageIDs f.readIDs = f.hashmapReadIDs } else { f.allocate = f.arrayAllocate f.free_count = f.arrayFreeCount f.mergeSpans = f.arrayMergeSpans f.getFreePageIDs = f.arrayGetFreePageIDs f.readIDs = f.arrayReadIDs } return f } // size returns the size of the page after serialization. func (f *freelist) size() int { n := f.count() if n >= 0xFFFF { // The first element will be used to store the count. See freelist.write. n++ } return int(pageHeaderSize) + (int(unsafe.Sizeof(pgid(0))) * n) } // count returns count of pages on the freelist func (f *freelist) count() int { return f.free_count() + f.pending_count() } // arrayFreeCount returns count of free pages(array version) func (f *freelist) arrayFreeCount() int { return len(f.ids) } // pending_count returns count of pending pages func (f *freelist) pending_count() int { var count int for _, txp := range f.pending { count += len(txp.ids) } return count } // copyall copies a list of all free ids and all pending ids in one sorted list. // f.count returns the minimum length required for dst. func (f *freelist) copyall(dst []pgid) { m := make(pgids, 0, f.pending_count()) for _, txp := range f.pending { m = append(m, txp.ids...) } sort.Sort(m) mergepgids(dst, f.getFreePageIDs(), m) } // arrayAllocate returns the starting page id of a contiguous list of pages of a given size. // If a contiguous block cannot be found then 0 is returned. func (f *freelist) arrayAllocate(txid txid, n int) pgid { if len(f.ids) == 0 { return 0 } var initial, previd pgid for i, id := range f.ids { if id <= 1 { panic(fmt.Sprintf("invalid page allocation: %d", id)) } // Reset initial page if this is not contiguous. if previd == 0 || id-previd != 1 { initial = id } // If we found a contiguous block then remove it and return it. if (id-initial)+1 == pgid(n) { // If we're allocating off the beginning then take the fast path // and just adjust the existing slice. This will use extra memory // temporarily but the append() in free() will realloc the slice // as is necessary. if (i + 1) == n { f.ids = f.ids[i+1:] } else { copy(f.ids[i-n+1:], f.ids[i+1:]) f.ids = f.ids[:len(f.ids)-n] } // Remove from the free cache. for i := pgid(0); i < pgid(n); i++ { delete(f.cache, initial+i) } f.allocs[initial] = txid return initial } previd = id } return 0 } // free releases a page and its overflow for a given transaction id. // If the page is already free then a panic will occur. func (f *freelist) free(txid txid, p *page) { if p.id <= 1 { panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id)) } // Free page and all its overflow pages. txp := f.pending[txid] if txp == nil { txp = &txPending{} f.pending[txid] = txp } allocTxid, ok := f.allocs[p.id] if ok { delete(f.allocs, p.id) } else if (p.flags & freelistPageFlag) != 0 { // Freelist is always allocated by prior tx. allocTxid = txid - 1 } for id := p.id; id <= p.id+pgid(p.overflow); id++ { // Verify that page is not already free. if _, ok := f.cache[id]; ok { panic(fmt.Sprintf("page %d already freed", id)) } // Add to the freelist and cache. txp.ids = append(txp.ids, id) txp.alloctx = append(txp.alloctx, allocTxid) f.cache[id] = struct{}{} } } // release moves all page ids for a transaction id (or older) to the freelist. func (f *freelist) release(txid txid) { m := make(pgids, 0) for tid, txp := range f.pending { if tid <= txid { // Move transaction's pending pages to the available freelist. // Don't remove from the cache since the page is still free. m = append(m, txp.ids...) delete(f.pending, tid) } } f.mergeSpans(m) } // releaseRange moves pending pages allocated within an extent [begin,end] to the free list. func (f *freelist) releaseRange(begin, end txid) { if begin > end { return } var m pgids for tid, txp := range f.pending { if tid < begin || tid > end { continue } // Don't recompute freed pages if ranges haven't updated. if txp.lastReleaseBegin == begin { continue } for i := 0; i < len(txp.ids); i++ { if atx := txp.alloctx[i]; atx < begin || atx > end { continue } m = append(m, txp.ids[i]) txp.ids[i] = txp.ids[len(txp.ids)-1] txp.ids = txp.ids[:len(txp.ids)-1] txp.alloctx[i] = txp.alloctx[len(txp.alloctx)-1] txp.alloctx = txp.alloctx[:len(txp.alloctx)-1] i-- } txp.lastReleaseBegin = begin if len(txp.ids) == 0 { delete(f.pending, tid) } } f.mergeSpans(m) } // rollback removes the pages from a given pending tx. func (f *freelist) rollback(txid txid) { // Remove page ids from cache. txp := f.pending[txid] if txp == nil { return } var m pgids for i, pgid := range txp.ids { delete(f.cache, pgid) tx := txp.alloctx[i] if tx == 0 { continue } if tx != txid { // Pending free aborted; restore page back to alloc list. f.allocs[pgid] = tx } else { // Freed page was allocated by this txn; OK to throw away. m = append(m, pgid) } } // Remove pages from pending list and mark as free if allocated by txid. delete(f.pending, txid) f.mergeSpans(m) } // freed returns whether a given page is in the free list. func (f *freelist) freed(pgId pgid) bool { _, ok := f.cache[pgId] return ok } // read initializes the freelist from a freelist page. func (f *freelist) read(p *page) { if (p.flags & freelistPageFlag) == 0 { panic(fmt.Sprintf("invalid freelist page: %d, page type is %s", p.id, p.typ())) } // If the page.count is at the max uint16 value (64k) then it's considered // an overflow and the size of the freelist is stored as the first element. var idx, count = 0, int(p.count) if count == 0xFFFF { idx = 1 c := *(*pgid)(unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))) count = int(c) if count < 0 { panic(fmt.Sprintf("leading element count %d overflows int", c)) } } // Copy the list of page ids from the freelist. if count == 0 { f.ids = nil } else { data := unsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p), unsafe.Sizeof(pgid(0)), idx) ids := unsafe.Slice((*pgid)(data), count) // copy the ids, so we don't modify on the freelist page directly idsCopy := make([]pgid, count) copy(idsCopy, ids) // Make sure they're sorted. sort.Sort(pgids(idsCopy)) f.readIDs(idsCopy) } } // arrayReadIDs initializes the freelist from a given list of ids. func (f *freelist) arrayReadIDs(ids []pgid) { f.ids = ids f.reindex() } func (f *freelist) arrayGetFreePageIDs() []pgid { return f.ids } // write writes the page ids onto a freelist page. All free and pending ids are // saved to disk since in the event of a program crash, all pending ids will // become free. func (f *freelist) write(p *page) error { // Combine the old free pgids and pgids waiting on an open transaction. // Update the header flag. p.flags |= freelistPageFlag // The page.count can only hold up to 64k elements so if we overflow that // number then we handle it by putting the size in the first element. l := f.count() if l == 0 { p.count = uint16(l) } else if l < 0xFFFF { p.count = uint16(l) data := unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p)) ids := unsafe.Slice((*pgid)(data), l) f.copyall(ids) } else { p.count = 0xFFFF data := unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p)) ids := unsafe.Slice((*pgid)(data), l+1) ids[0] = pgid(l) f.copyall(ids[1:]) } return nil } // reload reads the freelist from a page and filters out pending items. func (f *freelist) reload(p *page) { f.read(p) // Build a cache of only pending pages. pcache := make(map[pgid]bool) for _, txp := range f.pending { for _, pendingID := range txp.ids { pcache[pendingID] = true } } // Check each page in the freelist and build a new available freelist // with any pages not in the pending lists. var a []pgid for _, id := range f.getFreePageIDs() { if !pcache[id] { a = append(a, id) } } f.readIDs(a) } // noSyncReload reads the freelist from pgids and filters out pending items. func (f *freelist) noSyncReload(pgids []pgid) { // Build a cache of only pending pages. pcache := make(map[pgid]bool) for _, txp := range f.pending { for _, pendingID := range txp.ids { pcache[pendingID] = true } } // Check each page in the freelist and build a new available freelist // with any pages not in the pending lists. var a []pgid for _, id := range pgids { if !pcache[id] { a = append(a, id) } } f.readIDs(a) } // reindex rebuilds the free cache based on available and pending free lists. func (f *freelist) reindex() { ids := f.getFreePageIDs() f.cache = make(map[pgid]struct{}, len(ids)) for _, id := range ids { f.cache[id] = struct{}{} } for _, txp := range f.pending { for _, pendingID := range txp.ids { f.cache[pendingID] = struct{}{} } } } // arrayMergeSpans try to merge list of pages(represented by pgids) with existing spans but using array func (f *freelist) arrayMergeSpans(ids pgids) { sort.Sort(ids) f.ids = pgids(f.ids).merge(ids) } bbolt-1.3.10/freelist_hmap.go000066400000000000000000000072131461507540700160630ustar00rootroot00000000000000package bbolt import "sort" // hashmapFreeCount returns count of free pages(hashmap version) func (f *freelist) hashmapFreeCount() int { // use the forwardMap to get the total count count := 0 for _, size := range f.forwardMap { count += int(size) } return count } // hashmapAllocate serves the same purpose as arrayAllocate, but use hashmap as backend func (f *freelist) hashmapAllocate(txid txid, n int) pgid { if n == 0 { return 0 } // if we have a exact size match just return short path if bm, ok := f.freemaps[uint64(n)]; ok { for pid := range bm { // remove the span f.delSpan(pid, uint64(n)) f.allocs[pid] = txid for i := pgid(0); i < pgid(n); i++ { delete(f.cache, pid+i) } return pid } } // lookup the map to find larger span for size, bm := range f.freemaps { if size < uint64(n) { continue } for pid := range bm { // remove the initial f.delSpan(pid, size) f.allocs[pid] = txid remain := size - uint64(n) // add remain span f.addSpan(pid+pgid(n), remain) for i := pgid(0); i < pgid(n); i++ { delete(f.cache, pid+i) } return pid } } return 0 } // hashmapReadIDs reads pgids as input an initial the freelist(hashmap version) func (f *freelist) hashmapReadIDs(pgids []pgid) { f.init(pgids) // Rebuild the page cache. f.reindex() } // hashmapGetFreePageIDs returns the sorted free page ids func (f *freelist) hashmapGetFreePageIDs() []pgid { count := f.free_count() if count == 0 { return nil } m := make([]pgid, 0, count) for start, size := range f.forwardMap { for i := 0; i < int(size); i++ { m = append(m, start+pgid(i)) } } sort.Sort(pgids(m)) return m } // hashmapMergeSpans try to merge list of pages(represented by pgids) with existing spans func (f *freelist) hashmapMergeSpans(ids pgids) { for _, id := range ids { // try to see if we can merge and update f.mergeWithExistingSpan(id) } } // mergeWithExistingSpan merges pid to the existing free spans, try to merge it backward and forward func (f *freelist) mergeWithExistingSpan(pid pgid) { prev := pid - 1 next := pid + 1 preSize, mergeWithPrev := f.backwardMap[prev] nextSize, mergeWithNext := f.forwardMap[next] newStart := pid newSize := uint64(1) if mergeWithPrev { //merge with previous span start := prev + 1 - pgid(preSize) f.delSpan(start, preSize) newStart -= pgid(preSize) newSize += preSize } if mergeWithNext { // merge with next span f.delSpan(next, nextSize) newSize += nextSize } f.addSpan(newStart, newSize) } func (f *freelist) addSpan(start pgid, size uint64) { f.backwardMap[start-1+pgid(size)] = size f.forwardMap[start] = size if _, ok := f.freemaps[size]; !ok { f.freemaps[size] = make(map[pgid]struct{}) } f.freemaps[size][start] = struct{}{} } func (f *freelist) delSpan(start pgid, size uint64) { delete(f.forwardMap, start) delete(f.backwardMap, start+pgid(size-1)) delete(f.freemaps[size], start) if len(f.freemaps[size]) == 0 { delete(f.freemaps, size) } } // initial from pgids using when use hashmap version // pgids must be sorted func (f *freelist) init(pgids []pgid) { if len(pgids) == 0 { return } size := uint64(1) start := pgids[0] if !sort.SliceIsSorted([]pgid(pgids), func(i, j int) bool { return pgids[i] < pgids[j] }) { panic("pgids not sorted") } f.freemaps = make(map[uint64]pidSet) f.forwardMap = make(map[pgid]uint64) f.backwardMap = make(map[pgid]uint64) for i := 1; i < len(pgids); i++ { // continuous page if pgids[i] == pgids[i-1]+1 { size++ } else { f.addSpan(start, size) size = 1 start = pgids[i] } } // init the tail if size != 0 && start != 0 { f.addSpan(start, size) } } bbolt-1.3.10/freelist_test.go000066400000000000000000000271741461507540700161250ustar00rootroot00000000000000package bbolt import ( "math/rand" "os" "reflect" "sort" "testing" "unsafe" ) // TestFreelistType is used as a env variable for test to indicate the backend type const TestFreelistType = "TEST_FREELIST_TYPE" // Ensure that a page is added to a transaction's freelist. func TestFreelist_free(t *testing.T) { f := newTestFreelist() f.free(100, &page{id: 12}) if !reflect.DeepEqual([]pgid{12}, f.pending[100].ids) { t.Fatalf("exp=%v; got=%v", []pgid{12}, f.pending[100].ids) } } // Ensure that a page and its overflow is added to a transaction's freelist. func TestFreelist_free_overflow(t *testing.T) { f := newTestFreelist() f.free(100, &page{id: 12, overflow: 3}) if exp := []pgid{12, 13, 14, 15}; !reflect.DeepEqual(exp, f.pending[100].ids) { t.Fatalf("exp=%v; got=%v", exp, f.pending[100].ids) } } // Ensure that a transaction's free pages can be released. func TestFreelist_release(t *testing.T) { f := newTestFreelist() f.free(100, &page{id: 12, overflow: 1}) f.free(100, &page{id: 9}) f.free(102, &page{id: 39}) f.release(100) f.release(101) if exp := []pgid{9, 12, 13}; !reflect.DeepEqual(exp, f.getFreePageIDs()) { t.Fatalf("exp=%v; got=%v", exp, f.getFreePageIDs()) } f.release(102) if exp := []pgid{9, 12, 13, 39}; !reflect.DeepEqual(exp, f.getFreePageIDs()) { t.Fatalf("exp=%v; got=%v", exp, f.getFreePageIDs()) } } // Ensure that releaseRange handles boundary conditions correctly func TestFreelist_releaseRange(t *testing.T) { type testRange struct { begin, end txid } type testPage struct { id pgid n int allocTxn txid freeTxn txid } var releaseRangeTests = []struct { title string pagesIn []testPage releaseRanges []testRange wantFree []pgid }{ { title: "Single pending in range", pagesIn: []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}}, releaseRanges: []testRange{{1, 300}}, wantFree: []pgid{3}, }, { title: "Single pending with minimum end range", pagesIn: []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}}, releaseRanges: []testRange{{1, 200}}, wantFree: []pgid{3}, }, { title: "Single pending outsize minimum end range", pagesIn: []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}}, releaseRanges: []testRange{{1, 199}}, wantFree: nil, }, { title: "Single pending with minimum begin range", pagesIn: []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}}, releaseRanges: []testRange{{100, 300}}, wantFree: []pgid{3}, }, { title: "Single pending outside minimum begin range", pagesIn: []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}}, releaseRanges: []testRange{{101, 300}}, wantFree: nil, }, { title: "Single pending in minimum range", pagesIn: []testPage{{id: 3, n: 1, allocTxn: 199, freeTxn: 200}}, releaseRanges: []testRange{{199, 200}}, wantFree: []pgid{3}, }, { title: "Single pending and read transaction at 199", pagesIn: []testPage{{id: 3, n: 1, allocTxn: 199, freeTxn: 200}}, releaseRanges: []testRange{{100, 198}, {200, 300}}, wantFree: nil, }, { title: "Adjacent pending and read transactions at 199, 200", pagesIn: []testPage{ {id: 3, n: 1, allocTxn: 199, freeTxn: 200}, {id: 4, n: 1, allocTxn: 200, freeTxn: 201}, }, releaseRanges: []testRange{ {100, 198}, {200, 199}, // Simulate the ranges db.freePages might produce. {201, 300}, }, wantFree: nil, }, { title: "Out of order ranges", pagesIn: []testPage{ {id: 3, n: 1, allocTxn: 199, freeTxn: 200}, {id: 4, n: 1, allocTxn: 200, freeTxn: 201}, }, releaseRanges: []testRange{ {201, 199}, {201, 200}, {200, 200}, }, wantFree: nil, }, { title: "Multiple pending, read transaction at 150", pagesIn: []testPage{ {id: 3, n: 1, allocTxn: 100, freeTxn: 200}, {id: 4, n: 1, allocTxn: 100, freeTxn: 125}, {id: 5, n: 1, allocTxn: 125, freeTxn: 150}, {id: 6, n: 1, allocTxn: 125, freeTxn: 175}, {id: 7, n: 2, allocTxn: 150, freeTxn: 175}, {id: 9, n: 2, allocTxn: 175, freeTxn: 200}, }, releaseRanges: []testRange{{50, 149}, {151, 300}}, wantFree: []pgid{4, 9, 10}, }, } for _, c := range releaseRangeTests { f := newTestFreelist() var ids []pgid for _, p := range c.pagesIn { for i := uint64(0); i < uint64(p.n); i++ { ids = append(ids, pgid(uint64(p.id)+i)) } } f.readIDs(ids) for _, p := range c.pagesIn { f.allocate(p.allocTxn, p.n) } for _, p := range c.pagesIn { f.free(p.freeTxn, &page{id: p.id, overflow: uint32(p.n - 1)}) } for _, r := range c.releaseRanges { f.releaseRange(r.begin, r.end) } if exp := c.wantFree; !reflect.DeepEqual(exp, f.getFreePageIDs()) { t.Errorf("exp=%v; got=%v for %s", exp, f.getFreePageIDs(), c.title) } } } func TestFreelistHashmap_allocate(t *testing.T) { f := newTestFreelist() if f.freelistType != FreelistMapType { t.Skip() } ids := []pgid{3, 4, 5, 6, 7, 9, 12, 13, 18} f.readIDs(ids) f.allocate(1, 3) if x := f.free_count(); x != 6 { t.Fatalf("exp=6; got=%v", x) } f.allocate(1, 2) if x := f.free_count(); x != 4 { t.Fatalf("exp=4; got=%v", x) } f.allocate(1, 1) if x := f.free_count(); x != 3 { t.Fatalf("exp=3; got=%v", x) } f.allocate(1, 0) if x := f.free_count(); x != 3 { t.Fatalf("exp=3; got=%v", x) } } // Ensure that a freelist can find contiguous blocks of pages. func TestFreelistArray_allocate(t *testing.T) { f := newTestFreelist() if f.freelistType != FreelistArrayType { t.Skip() } ids := []pgid{3, 4, 5, 6, 7, 9, 12, 13, 18} f.readIDs(ids) if id := int(f.allocate(1, 3)); id != 3 { t.Fatalf("exp=3; got=%v", id) } if id := int(f.allocate(1, 1)); id != 6 { t.Fatalf("exp=6; got=%v", id) } if id := int(f.allocate(1, 3)); id != 0 { t.Fatalf("exp=0; got=%v", id) } if id := int(f.allocate(1, 2)); id != 12 { t.Fatalf("exp=12; got=%v", id) } if id := int(f.allocate(1, 1)); id != 7 { t.Fatalf("exp=7; got=%v", id) } if id := int(f.allocate(1, 0)); id != 0 { t.Fatalf("exp=0; got=%v", id) } if id := int(f.allocate(1, 0)); id != 0 { t.Fatalf("exp=0; got=%v", id) } if exp := []pgid{9, 18}; !reflect.DeepEqual(exp, f.getFreePageIDs()) { t.Fatalf("exp=%v; got=%v", exp, f.getFreePageIDs()) } if id := int(f.allocate(1, 1)); id != 9 { t.Fatalf("exp=9; got=%v", id) } if id := int(f.allocate(1, 1)); id != 18 { t.Fatalf("exp=18; got=%v", id) } if id := int(f.allocate(1, 1)); id != 0 { t.Fatalf("exp=0; got=%v", id) } if exp := []pgid{}; !reflect.DeepEqual(exp, f.getFreePageIDs()) { t.Fatalf("exp=%v; got=%v", exp, f.getFreePageIDs()) } } // Ensure that a freelist can deserialize from a freelist page. func TestFreelist_read(t *testing.T) { // Create a page. var buf [4096]byte page := (*page)(unsafe.Pointer(&buf[0])) page.flags = freelistPageFlag page.count = 2 // Insert 2 page ids. ids := (*[3]pgid)(unsafe.Pointer(uintptr(unsafe.Pointer(page)) + unsafe.Sizeof(*page))) ids[0] = 23 ids[1] = 50 // Deserialize page into a freelist. f := newTestFreelist() f.read(page) // Ensure that there are two page ids in the freelist. if exp := []pgid{23, 50}; !reflect.DeepEqual(exp, f.getFreePageIDs()) { t.Fatalf("exp=%v; got=%v", exp, f.getFreePageIDs()) } } // Ensure that a freelist can serialize into a freelist page. func TestFreelist_write(t *testing.T) { // Create a freelist and write it to a page. var buf [4096]byte f := newTestFreelist() f.readIDs([]pgid{12, 39}) f.pending[100] = &txPending{ids: []pgid{28, 11}} f.pending[101] = &txPending{ids: []pgid{3}} p := (*page)(unsafe.Pointer(&buf[0])) if err := f.write(p); err != nil { t.Fatal(err) } // Read the page back out. f2 := newTestFreelist() f2.read(p) // Ensure that the freelist is correct. // All pages should be present and in reverse order. if exp := []pgid{3, 11, 12, 28, 39}; !reflect.DeepEqual(exp, f2.getFreePageIDs()) { t.Fatalf("exp=%v; got=%v", exp, f2.getFreePageIDs()) } } func Benchmark_FreelistRelease10K(b *testing.B) { benchmark_FreelistRelease(b, 10000) } func Benchmark_FreelistRelease100K(b *testing.B) { benchmark_FreelistRelease(b, 100000) } func Benchmark_FreelistRelease1000K(b *testing.B) { benchmark_FreelistRelease(b, 1000000) } func Benchmark_FreelistRelease10000K(b *testing.B) { benchmark_FreelistRelease(b, 10000000) } func benchmark_FreelistRelease(b *testing.B, size int) { ids := randomPgids(size) pending := randomPgids(len(ids) / 400) b.ResetTimer() for i := 0; i < b.N; i++ { txp := &txPending{ids: pending} f := newTestFreelist() f.pending = map[txid]*txPending{1: txp} f.readIDs(ids) f.release(1) } } func randomPgids(n int) []pgid { pgids := make(pgids, n) for i := range pgids { pgids[i] = pgid(rand.Int63()) } sort.Sort(pgids) return pgids } func Test_freelist_ReadIDs_and_getFreePageIDs(t *testing.T) { f := newTestFreelist() exp := []pgid{3, 4, 5, 6, 7, 9, 12, 13, 18} f.readIDs(exp) if got := f.getFreePageIDs(); !reflect.DeepEqual(exp, got) { t.Fatalf("exp=%v; got=%v", exp, got) } f2 := newTestFreelist() var exp2 []pgid f2.readIDs(exp2) if got2 := f2.getFreePageIDs(); !reflect.DeepEqual(got2, exp2) { t.Fatalf("exp2=%#v; got2=%#v", exp2, got2) } } func Test_freelist_mergeWithExist(t *testing.T) { bm1 := pidSet{1: struct{}{}} bm2 := pidSet{5: struct{}{}} tests := []struct { name string ids []pgid pgid pgid want []pgid wantForwardmap map[pgid]uint64 wantBackwardmap map[pgid]uint64 wantfreemap map[uint64]pidSet }{ { name: "test1", ids: []pgid{1, 2, 4, 5, 6}, pgid: 3, want: []pgid{1, 2, 3, 4, 5, 6}, wantForwardmap: map[pgid]uint64{1: 6}, wantBackwardmap: map[pgid]uint64{6: 6}, wantfreemap: map[uint64]pidSet{6: bm1}, }, { name: "test2", ids: []pgid{1, 2, 5, 6}, pgid: 3, want: []pgid{1, 2, 3, 5, 6}, wantForwardmap: map[pgid]uint64{1: 3, 5: 2}, wantBackwardmap: map[pgid]uint64{6: 2, 3: 3}, wantfreemap: map[uint64]pidSet{3: bm1, 2: bm2}, }, { name: "test3", ids: []pgid{1, 2}, pgid: 3, want: []pgid{1, 2, 3}, wantForwardmap: map[pgid]uint64{1: 3}, wantBackwardmap: map[pgid]uint64{3: 3}, wantfreemap: map[uint64]pidSet{3: bm1}, }, { name: "test4", ids: []pgid{2, 3}, pgid: 1, want: []pgid{1, 2, 3}, wantForwardmap: map[pgid]uint64{1: 3}, wantBackwardmap: map[pgid]uint64{3: 3}, wantfreemap: map[uint64]pidSet{3: bm1}, }, } for _, tt := range tests { f := newTestFreelist() if f.freelistType == FreelistArrayType { t.Skip() } f.readIDs(tt.ids) f.mergeWithExistingSpan(tt.pgid) if got := f.getFreePageIDs(); !reflect.DeepEqual(tt.want, got) { t.Fatalf("name %s; exp=%v; got=%v", tt.name, tt.want, got) } if got := f.forwardMap; !reflect.DeepEqual(tt.wantForwardmap, got) { t.Fatalf("name %s; exp=%v; got=%v", tt.name, tt.wantForwardmap, got) } if got := f.backwardMap; !reflect.DeepEqual(tt.wantBackwardmap, got) { t.Fatalf("name %s; exp=%v; got=%v", tt.name, tt.wantBackwardmap, got) } if got := f.freemaps; !reflect.DeepEqual(tt.wantfreemap, got) { t.Fatalf("name %s; exp=%v; got=%v", tt.name, tt.wantfreemap, got) } } } // newTestFreelist get the freelist type from env and initial the freelist func newTestFreelist() *freelist { freelistType := FreelistArrayType if env := os.Getenv(TestFreelistType); env == string(FreelistMapType) { freelistType = FreelistMapType } return newFreelist(freelistType) } bbolt-1.3.10/go.mod000066400000000000000000000004621461507540700140170ustar00rootroot00000000000000module go.etcd.io/bbolt go 1.21 require ( github.com/stretchr/testify v1.8.1 go.etcd.io/gofail v0.1.0 golang.org/x/sync v0.5.0 golang.org/x/sys v0.4.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) bbolt-1.3.10/go.sum000066400000000000000000000036671461507540700140560ustar00rootroot00000000000000github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= go.etcd.io/gofail v0.1.0 h1:XItAMIhOojXFQMgrxjnd2EIIHun/d5qL0Pf7FzVTkFg= go.etcd.io/gofail v0.1.0/go.mod h1:VZBCXYGZhHAinaBiiqYvuDynvahNsAyLFwB3kEHKz1M= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= bbolt-1.3.10/internal/000077500000000000000000000000001461507540700145235ustar00rootroot00000000000000bbolt-1.3.10/internal/btesting/000077500000000000000000000000001461507540700163425ustar00rootroot00000000000000bbolt-1.3.10/internal/btesting/btesting.go000066400000000000000000000116061461507540700205140ustar00rootroot00000000000000package btesting import ( "flag" "fmt" "os" "path/filepath" "regexp" "testing" "time" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" ) var statsFlag = flag.Bool("stats", false, "show performance stats") // TestFreelistType is used as a env variable for test to indicate the backend type const TestFreelistType = "TEST_FREELIST_TYPE" // DB is a test wrapper for bolt.DB. type DB struct { *bolt.DB f string o *bolt.Options t testing.TB } // MustCreateDB returns a new, open DB at a temporary location. func MustCreateDB(t testing.TB) *DB { return MustCreateDBWithOption(t, nil) } // MustCreateDBWithOption returns a new, open DB at a temporary location with given options. func MustCreateDBWithOption(t testing.TB, o *bolt.Options) *DB { f := filepath.Join(t.TempDir(), "db") return MustOpenDBWithOption(t, f, o) } func MustOpenDBWithOption(t testing.TB, f string, o *bolt.Options) *DB { t.Logf("Opening bbolt DB at: %s", f) if o == nil { o = bolt.DefaultOptions } freelistType := bolt.FreelistArrayType if env := os.Getenv(TestFreelistType); env == string(bolt.FreelistMapType) { freelistType = bolt.FreelistMapType } o.FreelistType = freelistType db, err := bolt.Open(f, 0666, o) require.NoError(t, err) resDB := &DB{ DB: db, f: f, o: o, t: t, } t.Cleanup(resDB.PostTestCleanup) return resDB } func (db *DB) PostTestCleanup() { // Check database consistency after every test. if db.DB != nil { db.MustCheck() db.MustClose() } } // Close closes the database but does NOT delete the underlying file. func (db *DB) Close() error { if db.DB != nil { // Log statistics. if *statsFlag { db.PrintStats() } db.t.Logf("Closing bbolt DB at: %s", db.f) err := db.DB.Close() if err != nil { return err } db.DB = nil } return nil } // MustClose closes the database but does NOT delete the underlying file. func (db *DB) MustClose() { err := db.Close() require.NoError(db.t, err) } func (db *DB) MustDeleteFile() { err := os.Remove(db.Path()) require.NoError(db.t, err) } func (db *DB) SetOptions(o *bolt.Options) { db.o = o } // MustReopen reopen the database. Panic on error. func (db *DB) MustReopen() { if db.DB != nil { panic("Please call Close() before MustReopen()") } db.t.Logf("Reopening bbolt DB at: %s", db.f) indb, err := bolt.Open(db.Path(), 0666, db.o) require.NoError(db.t, err) db.DB = indb } // MustCheck runs a consistency check on the database and panics if any errors are found. func (db *DB) MustCheck() { err := db.Update(func(tx *bolt.Tx) error { // Collect all the errors. var errors []error for err := range tx.Check() { errors = append(errors, err) if len(errors) > 10 { break } } // If errors occurred, copy the DB and print the errors. if len(errors) > 0 { var path = filepath.Join(db.t.TempDir(), "db.backup") err := tx.CopyFile(path, 0600) require.NoError(db.t, err) // Print errors. fmt.Print("\n\n") fmt.Printf("consistency check failed (%d errors)\n", len(errors)) for _, err := range errors { fmt.Println(err) } fmt.Println("") fmt.Println("db saved to:") fmt.Println(path) fmt.Print("\n\n") os.Exit(-1) } return nil }) require.NoError(db.t, err) } // Fill - fills the DB using numTx transactions and numKeysPerTx. func (db *DB) Fill(bucket []byte, numTx int, numKeysPerTx int, keyGen func(tx int, key int) []byte, valueGen func(tx int, key int) []byte) error { for tr := 0; tr < numTx; tr++ { err := db.Update(func(tx *bolt.Tx) error { b, _ := tx.CreateBucketIfNotExists(bucket) for i := 0; i < numKeysPerTx; i++ { if err := b.Put(keyGen(tr, i), valueGen(tr, i)); err != nil { return err } } return nil }) if err != nil { return err } } return nil } func (db *DB) Path() string { return db.f } // CopyTempFile copies a database to a temporary file. func (db *DB) CopyTempFile() { path := filepath.Join(db.t.TempDir(), "db.copy") err := db.View(func(tx *bolt.Tx) error { return tx.CopyFile(path, 0600) }) require.NoError(db.t, err) fmt.Println("db copied to: ", path) } // PrintStats prints the database stats func (db *DB) PrintStats() { var stats = db.Stats() fmt.Printf("[db] %-20s %-20s %-20s\n", fmt.Sprintf("pg(%d/%d)", stats.TxStats.GetPageCount(), stats.TxStats.GetPageAlloc()), fmt.Sprintf("cur(%d)", stats.TxStats.GetCursorCount()), fmt.Sprintf("node(%d/%d)", stats.TxStats.GetNodeCount(), stats.TxStats.GetNodeDeref()), ) fmt.Printf(" %-20s %-20s %-20s\n", fmt.Sprintf("rebal(%d/%v)", stats.TxStats.GetRebalance(), truncDuration(stats.TxStats.GetRebalanceTime())), fmt.Sprintf("spill(%d/%v)", stats.TxStats.GetSpill(), truncDuration(stats.TxStats.GetSpillTime())), fmt.Sprintf("w(%d/%v)", stats.TxStats.GetWrite(), truncDuration(stats.TxStats.GetWriteTime())), ) } func truncDuration(d time.Duration) string { return regexp.MustCompile(`^(\d+)(\.\d+)`).ReplaceAllString(d.String(), "$1") } bbolt-1.3.10/internal/guts_cli/000077500000000000000000000000001461507540700163345ustar00rootroot00000000000000bbolt-1.3.10/internal/guts_cli/guts_cli.go000066400000000000000000000207061461507540700205010ustar00rootroot00000000000000package guts_cli // Low level access to pages / data-structures of the bbolt file. // TODO(ptab): Merge with bbolt/page file that should get ported to internal. import ( "errors" "fmt" "io" "os" "unsafe" ) var ( // ErrCorrupt is returned when a checking a data file finds errors. ErrCorrupt = errors.New("invalid value") ) // PageHeaderSize represents the size of the bolt.Page header. const PageHeaderSize = 16 // Represents a marker value to indicate that a file (Meta Page) is a Bolt DB. const magic uint32 = 0xED0CDAED // DO NOT EDIT. Copied from the "bolt" package. const maxAllocSize = 0xFFFFFFF // DO NOT EDIT. Copied from the "bolt" package. const ( branchPageFlag = 0x01 leafPageFlag = 0x02 metaPageFlag = 0x04 freelistPageFlag = 0x10 ) // DO NOT EDIT. Copied from the "bolt" package. const bucketLeafFlag = 0x01 // DO NOT EDIT. Copied from the "bolt" package. type Pgid uint64 // DO NOT EDIT. Copied from the "bolt" package. type txid uint64 // DO NOT EDIT. Copied from the "bolt" package. type Meta struct { magic uint32 version uint32 pageSize uint32 flags uint32 root Bucket freelist Pgid pgid Pgid // High Water Mark (id of next added Page if the file growths) txid txid checksum uint64 } func LoadPageMeta(buf []byte) *Meta { return (*Meta)(unsafe.Pointer(&buf[PageHeaderSize])) } func (m *Meta) RootBucket() *Bucket { return &m.root } func (m *Meta) Txid() uint64 { return uint64(m.txid) } func (m *Meta) Print(w io.Writer) { fmt.Fprintf(w, "Version: %d\n", m.version) fmt.Fprintf(w, "Page Size: %d bytes\n", m.pageSize) fmt.Fprintf(w, "Flags: %08x\n", m.flags) fmt.Fprintf(w, "Root: \n", m.root.root) fmt.Fprintf(w, "Freelist: \n", m.freelist) fmt.Fprintf(w, "HWM: \n", m.pgid) fmt.Fprintf(w, "Txn ID: %d\n", m.txid) fmt.Fprintf(w, "Checksum: %016x\n", m.checksum) fmt.Fprintf(w, "\n") } // DO NOT EDIT. Copied from the "bolt" package. type Bucket struct { root Pgid sequence uint64 } const bucketHeaderSize = int(unsafe.Sizeof(Bucket{})) func LoadBucket(buf []byte) *Bucket { return (*Bucket)(unsafe.Pointer(&buf[0])) } func (b *Bucket) String() string { return fmt.Sprintf("", b.root, b.sequence) } func (b *Bucket) RootPage() Pgid { return b.root } func (b *Bucket) InlinePage(v []byte) *Page { return (*Page)(unsafe.Pointer(&v[bucketHeaderSize])) } // DO NOT EDIT. Copied from the "bolt" package. type Page struct { id Pgid flags uint16 count uint16 overflow uint32 ptr uintptr } func LoadPage(buf []byte) *Page { return (*Page)(unsafe.Pointer(&buf[0])) } func (p *Page) FreelistPageCount() int { // Check for overflow and, if present, adjust actual element count. if p.count == 0xFFFF { return int(((*[maxAllocSize]Pgid)(unsafe.Pointer(&p.ptr)))[0]) } else { return int(p.count) } } func (p *Page) FreelistPagePages() []Pgid { // Check for overflow and, if present, adjust starting index. idx := 0 if p.count == 0xFFFF { idx = 1 } return (*[maxAllocSize]Pgid)(unsafe.Pointer(&p.ptr))[idx:p.FreelistPageCount()] } func (p *Page) Overflow() uint32 { return p.overflow } func (p *Page) String() string { return fmt.Sprintf("ID: %d, Type: %s, count: %d, overflow: %d", p.id, p.Type(), p.count, p.overflow) } // DO NOT EDIT. Copied from the "bolt" package. // TODO(ptabor): Make the page-types an enum. func (p *Page) Type() string { if (p.flags & branchPageFlag) != 0 { return "branch" } else if (p.flags & leafPageFlag) != 0 { return "leaf" } else if (p.flags & metaPageFlag) != 0 { return "meta" } else if (p.flags & freelistPageFlag) != 0 { return "freelist" } return fmt.Sprintf("unknown<%02x>", p.flags) } func (p *Page) Count() uint16 { return p.count } func (p *Page) Id() Pgid { return p.id } // DO NOT EDIT. Copied from the "bolt" package. func (p *Page) LeafPageElement(index uint16) *LeafPageElement { n := &((*[0x7FFFFFF]LeafPageElement)(unsafe.Pointer(&p.ptr)))[index] return n } // DO NOT EDIT. Copied from the "bolt" package. func (p *Page) BranchPageElement(index uint16) *BranchPageElement { return &((*[0x7FFFFFF]BranchPageElement)(unsafe.Pointer(&p.ptr)))[index] } func (p *Page) SetId(target Pgid) { p.id = target } func (p *Page) SetCount(target uint16) { p.count = target } func (p *Page) SetOverflow(target uint32) { p.overflow = target } // DO NOT EDIT. Copied from the "bolt" package. type BranchPageElement struct { pos uint32 ksize uint32 pgid Pgid } // DO NOT EDIT. Copied from the "bolt" package. func (n *BranchPageElement) Key() []byte { buf := (*[maxAllocSize]byte)(unsafe.Pointer(n)) return buf[n.pos : n.pos+n.ksize] } func (n *BranchPageElement) PgId() Pgid { return n.pgid } // DO NOT EDIT. Copied from the "bolt" package. type LeafPageElement struct { flags uint32 pos uint32 ksize uint32 vsize uint32 } // DO NOT EDIT. Copied from the "bolt" package. func (n *LeafPageElement) Key() []byte { buf := (*[maxAllocSize]byte)(unsafe.Pointer(n)) return buf[n.pos : n.pos+n.ksize] } // DO NOT EDIT. Copied from the "bolt" package. func (n *LeafPageElement) Value() []byte { buf := (*[maxAllocSize]byte)(unsafe.Pointer(n)) return buf[n.pos+n.ksize : n.pos+n.ksize+n.vsize] } func (n *LeafPageElement) IsBucketEntry() bool { return n.flags&uint32(bucketLeafFlag) != 0 } func (n *LeafPageElement) Bucket() *Bucket { if n.IsBucketEntry() { return LoadBucket(n.Value()) } else { return nil } } // ReadPage reads Page info & full Page data from a path. // This is not transactionally safe. func ReadPage(path string, pageID uint64) (*Page, []byte, error) { // Find Page size. pageSize, hwm, err := ReadPageAndHWMSize(path) if err != nil { return nil, nil, fmt.Errorf("read Page size: %s", err) } // Open database file. f, err := os.Open(path) if err != nil { return nil, nil, err } defer f.Close() // Read one block into buffer. buf := make([]byte, pageSize) if n, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil { return nil, nil, err } else if n != len(buf) { return nil, nil, io.ErrUnexpectedEOF } // Determine total number of blocks. p := LoadPage(buf) if p.id != Pgid(pageID) { return nil, nil, fmt.Errorf("error: %w due to unexpected Page id: %d != %d", ErrCorrupt, p.id, pageID) } overflowN := p.overflow if overflowN >= uint32(hwm)-3 { // we exclude 2 Meta pages and the current Page. return nil, nil, fmt.Errorf("error: %w, Page claims to have %d overflow pages (>=hwm=%d). Interrupting to avoid risky OOM", ErrCorrupt, overflowN, hwm) } // Re-read entire Page (with overflow) into buffer. buf = make([]byte, (uint64(overflowN)+1)*pageSize) if n, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil { return nil, nil, err } else if n != len(buf) { return nil, nil, io.ErrUnexpectedEOF } p = LoadPage(buf) if p.id != Pgid(pageID) { return nil, nil, fmt.Errorf("error: %w due to unexpected Page id: %d != %d", ErrCorrupt, p.id, pageID) } return p, buf, nil } func WritePage(path string, pageBuf []byte) error { page := LoadPage(pageBuf) pageSize, _, err := ReadPageAndHWMSize(path) if err != nil { return err } expectedLen := pageSize * (uint64(page.Overflow()) + 1) if expectedLen != uint64(len(pageBuf)) { return fmt.Errorf("WritePage: len(buf):%d != pageSize*(overflow+1):%d", len(pageBuf), expectedLen) } f, err := os.OpenFile(path, os.O_WRONLY, 0) if err != nil { return err } defer f.Close() _, err = f.WriteAt(pageBuf, int64(page.Id())*int64(pageSize)) return err } // ReadPageAndHWMSize reads Page size and HWM (id of the last+1 Page). // This is not transactionally safe. func ReadPageAndHWMSize(path string) (uint64, Pgid, error) { // Open database file. f, err := os.Open(path) if err != nil { return 0, 0, err } defer f.Close() // Read 4KB chunk. buf := make([]byte, 4096) if _, err := io.ReadFull(f, buf); err != nil { return 0, 0, err } // Read Page size from metadata. m := LoadPageMeta(buf) if m.magic != magic { return 0, 0, fmt.Errorf("the Meta Page has wrong (unexpected) magic") } return uint64(m.pageSize), Pgid(m.pgid), nil } // GetRootPage returns the root-page (according to the most recent transaction). func GetRootPage(path string) (root Pgid, activeMeta Pgid, err error) { _, buf0, err0 := ReadPage(path, 0) if err0 != nil { return 0, 0, err0 } m0 := LoadPageMeta(buf0) _, buf1, err1 := ReadPage(path, 1) if err1 != nil { return 0, 1, err1 } m1 := LoadPageMeta(buf1) if m0.txid < m1.txid { return m1.root.root, 1, nil } else { return m0.root.root, 0, nil } } bbolt-1.3.10/internal/surgeon/000077500000000000000000000000001461507540700162055ustar00rootroot00000000000000bbolt-1.3.10/internal/surgeon/surgeon.go000066400000000000000000000022031461507540700202130ustar00rootroot00000000000000package surgeon import ( "fmt" "go.etcd.io/bbolt/internal/guts_cli" ) func CopyPage(path string, srcPage guts_cli.Pgid, target guts_cli.Pgid) error { p1, d1, err1 := guts_cli.ReadPage(path, uint64(srcPage)) if err1 != nil { return err1 } p1.SetId(target) return guts_cli.WritePage(path, d1) } func ClearPage(path string, pgId guts_cli.Pgid) error { // Read the page p, buf, err := guts_cli.ReadPage(path, uint64(pgId)) if err != nil { return fmt.Errorf("ReadPage failed: %w", err) } // Update and rewrite the page p.SetCount(0) p.SetOverflow(0) if err := guts_cli.WritePage(path, buf); err != nil { return fmt.Errorf("WritePage failed: %w", err) } return nil } // RevertMetaPage replaces the newer metadata page with the older. // It usually means that one transaction is being lost. But frequently // data corruption happens on the last transaction pages and the // previous state is consistent. func RevertMetaPage(path string) error { _, activeMetaPage, err := guts_cli.GetRootPage(path) if err != nil { return err } if activeMetaPage == 0 { return CopyPage(path, 1, 0) } else { return CopyPage(path, 0, 1) } } bbolt-1.3.10/internal/surgeon/surgeon_test.go000066400000000000000000000027651461507540700212670ustar00rootroot00000000000000package surgeon_test import ( "fmt" "testing" "github.com/stretchr/testify/assert" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" "go.etcd.io/bbolt/internal/surgeon" ) func TestRevertMetaPage(t *testing.T) { db := btesting.MustCreateDB(t) assert.NoError(t, db.Fill([]byte("data"), 1, 500, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 100) }, )) assert.NoError(t, db.Update( func(tx *bolt.Tx) error { b := tx.Bucket([]byte("data")) assert.NoError(t, b.Put([]byte("0123"), []byte("new Value for 123"))) assert.NoError(t, b.Put([]byte("1234b"), []byte("additional object"))) assert.NoError(t, b.Delete([]byte("0246"))) return nil })) assert.NoError(t, db.View( func(tx *bolt.Tx) error { b := tx.Bucket([]byte("data")) assert.Equal(t, []byte("new Value for 123"), b.Get([]byte("0123"))) assert.Equal(t, []byte("additional object"), b.Get([]byte("1234b"))) assert.Nil(t, b.Get([]byte("0246"))) return nil })) db.Close() // This causes the whole tree to be linked to the previous state assert.NoError(t, surgeon.RevertMetaPage(db.Path())) db.MustReopen() db.MustCheck() assert.NoError(t, db.View( func(tx *bolt.Tx) error { b := tx.Bucket([]byte("data")) assert.Equal(t, make([]byte, 100), b.Get([]byte("0123"))) assert.Nil(t, b.Get([]byte("1234b"))) assert.Equal(t, make([]byte, 100), b.Get([]byte("0246"))) return nil })) } bbolt-1.3.10/internal/surgeon/xray.go000066400000000000000000000052741461507540700175270ustar00rootroot00000000000000package surgeon // Library contains raw access to bbolt files for sake of testing or fixing of corrupted files. // // The library must not be used bbolt btree - just by CLI or tests. // It's not optimized for performance. import ( "bytes" "fmt" "go.etcd.io/bbolt/internal/guts_cli" ) type XRay struct { path string } func NewXRay(path string) XRay { return XRay{path} } func (n XRay) traverse(stack []guts_cli.Pgid, callback func(page *guts_cli.Page, stack []guts_cli.Pgid) error) error { p, data, err := guts_cli.ReadPage(n.path, uint64(stack[len(stack)-1])) if err != nil { return fmt.Errorf("failed reading page (stack %v): %w", stack, err) } err = callback(p, stack) if err != nil { return fmt.Errorf("failed callback for page (stack %v): %w", stack, err) } switch p.Type() { case "meta": { m := guts_cli.LoadPageMeta(data) r := m.RootBucket().RootPage() return n.traverse(append(stack, r), callback) } case "branch": { for i := uint16(0); i < p.Count(); i++ { bpe := p.BranchPageElement(i) if err := n.traverse(append(stack, bpe.PgId()), callback); err != nil { return err } } } case "leaf": for i := uint16(0); i < p.Count(); i++ { lpe := p.LeafPageElement(i) if lpe.IsBucketEntry() { pgid := lpe.Bucket().RootPage() if pgid > 0 { if err := n.traverse(append(stack, pgid), callback); err != nil { return err } } else { inlinePage := lpe.Bucket().InlinePage(lpe.Value()) if err := callback(inlinePage, stack); err != nil { return fmt.Errorf("failed callback for inline page (stack %v): %w", stack, err) } } } } case "freelist": return nil // Free does not have children. } return nil } // FindPathsToKey finds all paths from root to the page that contains the given key. // As it traverses multiple buckets, so in theory there might be multiple keys with the given name. // Note: For simplicity it's currently implemented as traversing of the whole reachable tree. // If key is a bucket name, a page-path referencing the key will be returned as well. func (n XRay) FindPathsToKey(key []byte) ([][]guts_cli.Pgid, error) { var found [][]guts_cli.Pgid rootPage, _, err := guts_cli.GetRootPage(n.path) if err != nil { return nil, err } err = n.traverse([]guts_cli.Pgid{rootPage}, func(page *guts_cli.Page, stack []guts_cli.Pgid) error { if page.Type() == "leaf" { for i := uint16(0); i < page.Count(); i++ { if bytes.Equal(page.LeafPageElement(i).Key(), key) { var copyPath []guts_cli.Pgid copyPath = append(copyPath, stack...) found = append(found, copyPath) } } } return nil }) if err != nil { return nil, err } else { return found, nil } } bbolt-1.3.10/internal/surgeon/xray_test.go000066400000000000000000000036401461507540700205610ustar00rootroot00000000000000package surgeon_test import ( "fmt" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" "go.etcd.io/bbolt/internal/guts_cli" "go.etcd.io/bbolt/internal/surgeon" ) func TestFindPathsToKey(t *testing.T) { db := btesting.MustCreateDB(t) assert.NoError(t, db.Fill([]byte("data"), 1, 500, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 100) }, )) assert.NoError(t, db.Close()) navigator := surgeon.NewXRay(db.Path()) path1, err := navigator.FindPathsToKey([]byte("0451")) assert.NoError(t, err) assert.NotEmpty(t, path1) page := path1[0][len(path1[0])-1] p, _, err := guts_cli.ReadPage(db.Path(), uint64(page)) assert.NoError(t, err) assert.GreaterOrEqual(t, []byte("0451"), p.LeafPageElement(0).Key()) assert.LessOrEqual(t, []byte("0451"), p.LeafPageElement(p.Count()-1).Key()) } func TestFindPathsToKey_Bucket(t *testing.T) { rootBucket := []byte("data") subBucket := []byte("0451A") db := btesting.MustCreateDB(t) assert.NoError(t, db.Fill(rootBucket, 1, 500, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 100) }, )) require.NoError(t, db.Update(func(tx *bbolt.Tx) error { sb, err := tx.Bucket(rootBucket).CreateBucket(subBucket) require.NoError(t, err) require.NoError(t, sb.Put([]byte("foo"), []byte("bar"))) return nil })) assert.NoError(t, db.Close()) navigator := surgeon.NewXRay(db.Path()) path1, err := navigator.FindPathsToKey(subBucket) assert.NoError(t, err) assert.NotEmpty(t, path1) page := path1[0][len(path1[0])-1] p, _, err := guts_cli.ReadPage(db.Path(), uint64(page)) assert.NoError(t, err) assert.GreaterOrEqual(t, subBucket, p.LeafPageElement(0).Key()) assert.LessOrEqual(t, subBucket, p.LeafPageElement(p.Count()-1).Key()) } bbolt-1.3.10/internal/tests/000077500000000000000000000000001461507540700156655ustar00rootroot00000000000000bbolt-1.3.10/internal/tests/tx_check_test.go000066400000000000000000000055301461507540700210460ustar00rootroot00000000000000package tests_test import ( "fmt" "testing" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" "go.etcd.io/bbolt/internal/guts_cli" "go.etcd.io/bbolt/internal/surgeon" ) func TestTx_RecursivelyCheckPages_MisplacedPage(t *testing.T) { db := btesting.MustCreateDB(t) require.NoError(t, db.Fill([]byte("data"), 1, 10000, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 100) }, )) require.NoError(t, db.Close()) xRay := surgeon.NewXRay(db.Path()) path1, err := xRay.FindPathsToKey([]byte("0451")) require.NoError(t, err, "cannot find page that contains key:'0451'") require.Len(t, path1, 1, "Expected only one page that contains key:'0451'") path2, err := xRay.FindPathsToKey([]byte("7563")) require.NoError(t, err, "cannot find page that contains key:'7563'") require.Len(t, path2, 1, "Expected only one page that contains key:'7563'") srcPage := path1[0][len(path1[0])-1] targetPage := path2[0][len(path2[0])-1] require.NoError(t, surgeon.CopyPage(db.Path(), srcPage, targetPage)) db.MustReopen() require.NoError(t, db.Update(func(tx *bolt.Tx) error { // Collect all the errors. var errors []error for err := range tx.Check() { errors = append(errors, err) } require.Len(t, errors, 1) require.ErrorContains(t, errors[0], fmt.Sprintf("leaf page(%v) needs to be >= the key in the ancestor", targetPage)) return nil })) require.NoError(t, db.Close()) } func TestTx_RecursivelyCheckPages_CorruptedLeaf(t *testing.T) { db := btesting.MustCreateDB(t) require.NoError(t, db.Fill([]byte("data"), 1, 10000, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 100) }, )) require.NoError(t, db.Close()) xray := surgeon.NewXRay(db.Path()) path1, err := xray.FindPathsToKey([]byte("0451")) require.NoError(t, err, "cannot find page that contains key:'0451'") require.Len(t, path1, 1, "Expected only one page that contains key:'0451'") srcPage := path1[0][len(path1[0])-1] p, pbuf, err := guts_cli.ReadPage(db.Path(), uint64(srcPage)) require.NoError(t, err) require.Positive(t, p.Count(), "page must be not empty") p.LeafPageElement(p.Count() / 2).Key()[0] = 'z' require.NoError(t, guts_cli.WritePage(db.Path(), pbuf)) db.MustReopen() require.NoError(t, db.Update(func(tx *bolt.Tx) error { // Collect all the errors. var errors []error for err := range tx.Check() { errors = append(errors, err) } require.Len(t, errors, 2) require.ErrorContains(t, errors[0], fmt.Sprintf("leaf page(%v) needs to be < than key of the next element in ancestor", srcPage)) require.ErrorContains(t, errors[1], fmt.Sprintf("leaf page(%v) needs to be > (found <) than previous element", srcPage)) return nil })) require.NoError(t, db.Close()) } bbolt-1.3.10/manydbs_test.go000066400000000000000000000023741461507540700157400ustar00rootroot00000000000000package bbolt import ( "crypto/rand" "fmt" "os" "path/filepath" "testing" ) func createDb(t *testing.T) (*DB, func()) { // First, create a temporary directory to be used for the duration of // this test. tempDirName, err := os.MkdirTemp("", "bboltmemtest") if err != nil { t.Fatalf("error creating temp dir: %v", err) } path := filepath.Join(tempDirName, "testdb.db") bdb, err := Open(path, 0600, nil) if err != nil { t.Fatalf("error creating bbolt db: %v", err) } cleanup := func() { bdb.Close() os.RemoveAll(tempDirName) } return bdb, cleanup } func createAndPutKeys(t *testing.T) { t.Parallel() db, cleanup := createDb(t) defer cleanup() bucketName := []byte("bucket") for i := 0; i < 100; i++ { err := db.Update(func(tx *Tx) error { nodes, err := tx.CreateBucketIfNotExists(bucketName) if err != nil { return err } var key [16]byte if _, err := rand.Read(key[:]); err != nil { return err } if err := nodes.Put(key[:], nil); err != nil { return err } return nil }) if err != nil { t.Fatal(err) } } } func TestManyDBs(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode") } for i := 0; i < 100; i++ { t.Run(fmt.Sprintf("%d", i), createAndPutKeys) } } bbolt-1.3.10/mlock_unix.go000066400000000000000000000013161461507540700154070ustar00rootroot00000000000000//go:build !windows // +build !windows package bbolt import "golang.org/x/sys/unix" // mlock locks memory of db file func mlock(db *DB, fileSize int) error { sizeToLock := fileSize if sizeToLock > db.datasz { // Can't lock more than mmaped slice sizeToLock = db.datasz } if err := unix.Mlock(db.dataref[:sizeToLock]); err != nil { return err } return nil } // munlock unlocks memory of db file func munlock(db *DB, fileSize int) error { if db.dataref == nil { return nil } sizeToUnlock := fileSize if sizeToUnlock > db.datasz { // Can't unlock more than mmaped slice sizeToUnlock = db.datasz } if err := unix.Munlock(db.dataref[:sizeToUnlock]); err != nil { return err } return nil } bbolt-1.3.10/mlock_windows.go000066400000000000000000000004041461507540700161130ustar00rootroot00000000000000package bbolt // mlock locks memory of db file func mlock(_ *DB, _ int) error { panic("mlock is supported only on UNIX systems") } // munlock unlocks memory of db file func munlock(_ *DB, _ int) error { panic("munlock is supported only on UNIX systems") } bbolt-1.3.10/node.go000066400000000000000000000401671461507540700141730ustar00rootroot00000000000000package bbolt import ( "bytes" "fmt" "sort" "unsafe" ) // node represents an in-memory, deserialized page. type node struct { bucket *Bucket isLeaf bool unbalanced bool spilled bool key []byte pgid pgid parent *node children nodes inodes inodes } // root returns the top-level node this node is attached to. func (n *node) root() *node { if n.parent == nil { return n } return n.parent.root() } // minKeys returns the minimum number of inodes this node should have. func (n *node) minKeys() int { if n.isLeaf { return 1 } return 2 } // size returns the size of the node after serialization. func (n *node) size() int { sz, elsz := pageHeaderSize, n.pageElementSize() for i := 0; i < len(n.inodes); i++ { item := &n.inodes[i] sz += elsz + uintptr(len(item.key)) + uintptr(len(item.value)) } return int(sz) } // sizeLessThan returns true if the node is less than a given size. // This is an optimization to avoid calculating a large node when we only need // to know if it fits inside a certain page size. func (n *node) sizeLessThan(v uintptr) bool { sz, elsz := pageHeaderSize, n.pageElementSize() for i := 0; i < len(n.inodes); i++ { item := &n.inodes[i] sz += elsz + uintptr(len(item.key)) + uintptr(len(item.value)) if sz >= v { return false } } return true } // pageElementSize returns the size of each page element based on the type of node. func (n *node) pageElementSize() uintptr { if n.isLeaf { return leafPageElementSize } return branchPageElementSize } // childAt returns the child node at a given index. func (n *node) childAt(index int) *node { if n.isLeaf { panic(fmt.Sprintf("invalid childAt(%d) on a leaf node", index)) } return n.bucket.node(n.inodes[index].pgid, n) } // childIndex returns the index of a given child node. func (n *node) childIndex(child *node) int { index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, child.key) != -1 }) return index } // numChildren returns the number of children. func (n *node) numChildren() int { return len(n.inodes) } // nextSibling returns the next node with the same parent. func (n *node) nextSibling() *node { if n.parent == nil { return nil } index := n.parent.childIndex(n) if index >= n.parent.numChildren()-1 { return nil } return n.parent.childAt(index + 1) } // prevSibling returns the previous node with the same parent. func (n *node) prevSibling() *node { if n.parent == nil { return nil } index := n.parent.childIndex(n) if index == 0 { return nil } return n.parent.childAt(index - 1) } // put inserts a key/value. func (n *node) put(oldKey, newKey, value []byte, pgId pgid, flags uint32) { if pgId >= n.bucket.tx.meta.pgid { panic(fmt.Sprintf("pgId (%d) above high water mark (%d)", pgId, n.bucket.tx.meta.pgid)) } else if len(oldKey) <= 0 { panic("put: zero-length old key") } else if len(newKey) <= 0 { panic("put: zero-length new key") } // Find insertion index. index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, oldKey) != -1 }) // Add capacity and shift nodes if we don't have an exact match and need to insert. exact := (len(n.inodes) > 0 && index < len(n.inodes) && bytes.Equal(n.inodes[index].key, oldKey)) if !exact { n.inodes = append(n.inodes, inode{}) copy(n.inodes[index+1:], n.inodes[index:]) } inode := &n.inodes[index] inode.flags = flags inode.key = newKey inode.value = value inode.pgid = pgId _assert(len(inode.key) > 0, "put: zero-length inode key") } // del removes a key from the node. func (n *node) del(key []byte) { // Find index of key. index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, key) != -1 }) // Exit if the key isn't found. if index >= len(n.inodes) || !bytes.Equal(n.inodes[index].key, key) { return } // Delete inode from the node. n.inodes = append(n.inodes[:index], n.inodes[index+1:]...) // Mark the node as needing rebalancing. n.unbalanced = true } // read initializes the node from a page. func (n *node) read(p *page) { n.pgid = p.id n.isLeaf = ((p.flags & leafPageFlag) != 0) n.inodes = make(inodes, int(p.count)) for i := 0; i < int(p.count); i++ { inode := &n.inodes[i] if n.isLeaf { elem := p.leafPageElement(uint16(i)) inode.flags = elem.flags inode.key = elem.key() inode.value = elem.value() } else { elem := p.branchPageElement(uint16(i)) inode.pgid = elem.pgid inode.key = elem.key() } _assert(len(inode.key) > 0, "read: zero-length inode key") } // Save first key so we can find the node in the parent when we spill. if len(n.inodes) > 0 { n.key = n.inodes[0].key _assert(len(n.key) > 0, "read: zero-length node key") } else { n.key = nil } } // write writes the items onto one or more pages. // The page should have p.id (might be 0 for meta or bucket-inline page) and p.overflow set // and the rest should be zeroed. func (n *node) write(p *page) { _assert(p.count == 0 && p.flags == 0, "node cannot be written into a not empty page") // Initialize page. if n.isLeaf { p.flags = leafPageFlag } else { p.flags = branchPageFlag } if len(n.inodes) >= 0xFFFF { panic(fmt.Sprintf("inode overflow: %d (pgid=%d)", len(n.inodes), p.id)) } p.count = uint16(len(n.inodes)) // Stop here if there are no items to write. if p.count == 0 { return } // Loop over each item and write it to the page. // off tracks the offset into p of the start of the next data. off := unsafe.Sizeof(*p) + n.pageElementSize()*uintptr(len(n.inodes)) for i, item := range n.inodes { _assert(len(item.key) > 0, "write: zero-length inode key") // Create a slice to write into of needed size and advance // byte pointer for next iteration. sz := len(item.key) + len(item.value) b := unsafeByteSlice(unsafe.Pointer(p), off, 0, sz) off += uintptr(sz) // Write the page element. if n.isLeaf { elem := p.leafPageElement(uint16(i)) elem.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem))) elem.flags = item.flags elem.ksize = uint32(len(item.key)) elem.vsize = uint32(len(item.value)) } else { elem := p.branchPageElement(uint16(i)) elem.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem))) elem.ksize = uint32(len(item.key)) elem.pgid = item.pgid _assert(elem.pgid != p.id, "write: circular dependency occurred") } // Write data for the element to the end of the page. l := copy(b, item.key) copy(b[l:], item.value) } // DEBUG ONLY: n.dump() } // split breaks up a node into multiple smaller nodes, if appropriate. // This should only be called from the spill() function. func (n *node) split(pageSize uintptr) []*node { var nodes []*node node := n for { // Split node into two. a, b := node.splitTwo(pageSize) nodes = append(nodes, a) // If we can't split then exit the loop. if b == nil { break } // Set node to b so it gets split on the next iteration. node = b } return nodes } // splitTwo breaks up a node into two smaller nodes, if appropriate. // This should only be called from the split() function. func (n *node) splitTwo(pageSize uintptr) (*node, *node) { // Ignore the split if the page doesn't have at least enough nodes for // two pages or if the nodes can fit in a single page. if len(n.inodes) <= (minKeysPerPage*2) || n.sizeLessThan(pageSize) { return n, nil } // Determine the threshold before starting a new node. var fillPercent = n.bucket.FillPercent if fillPercent < minFillPercent { fillPercent = minFillPercent } else if fillPercent > maxFillPercent { fillPercent = maxFillPercent } threshold := int(float64(pageSize) * fillPercent) // Determine split position and sizes of the two pages. splitIndex, _ := n.splitIndex(threshold) // Split node into two separate nodes. // If there's no parent then we'll need to create one. if n.parent == nil { n.parent = &node{bucket: n.bucket, children: []*node{n}} } // Create a new node and add it to the parent. next := &node{bucket: n.bucket, isLeaf: n.isLeaf, parent: n.parent} n.parent.children = append(n.parent.children, next) // Split inodes across two nodes. next.inodes = n.inodes[splitIndex:] n.inodes = n.inodes[:splitIndex] // Update the statistics. n.bucket.tx.stats.IncSplit(1) return n, next } // splitIndex finds the position where a page will fill a given threshold. // It returns the index as well as the size of the first page. // This is only be called from split(). func (n *node) splitIndex(threshold int) (index, sz uintptr) { sz = pageHeaderSize // Loop until we only have the minimum number of keys required for the second page. for i := 0; i < len(n.inodes)-minKeysPerPage; i++ { index = uintptr(i) inode := n.inodes[i] elsize := n.pageElementSize() + uintptr(len(inode.key)) + uintptr(len(inode.value)) // If we have at least the minimum number of keys and adding another // node would put us over the threshold then exit and return. if index >= minKeysPerPage && sz+elsize > uintptr(threshold) { break } // Add the element size to the total size. sz += elsize } return } // spill writes the nodes to dirty pages and splits nodes as it goes. // Returns an error if dirty pages cannot be allocated. func (n *node) spill() error { var tx = n.bucket.tx if n.spilled { return nil } // Spill child nodes first. Child nodes can materialize sibling nodes in // the case of split-merge so we cannot use a range loop. We have to check // the children size on every loop iteration. sort.Sort(n.children) for i := 0; i < len(n.children); i++ { if err := n.children[i].spill(); err != nil { return err } } // We no longer need the child list because it's only used for spill tracking. n.children = nil // Split nodes into appropriate sizes. The first node will always be n. var nodes = n.split(uintptr(tx.db.pageSize)) for _, node := range nodes { // Add node's page to the freelist if it's not new. if node.pgid > 0 { tx.db.freelist.free(tx.meta.txid, tx.page(node.pgid)) node.pgid = 0 } // Allocate contiguous space for the node. p, err := tx.allocate((node.size() + tx.db.pageSize - 1) / tx.db.pageSize) if err != nil { return err } // Write the node. if p.id >= tx.meta.pgid { panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", p.id, tx.meta.pgid)) } node.pgid = p.id node.write(p) node.spilled = true // Insert into parent inodes. if node.parent != nil { var key = node.key if key == nil { key = node.inodes[0].key } node.parent.put(key, node.inodes[0].key, nil, node.pgid, 0) node.key = node.inodes[0].key _assert(len(node.key) > 0, "spill: zero-length node key") } // Update the statistics. tx.stats.IncSpill(1) } // If the root node split and created a new root then we need to spill that // as well. We'll clear out the children to make sure it doesn't try to respill. if n.parent != nil && n.parent.pgid == 0 { n.children = nil return n.parent.spill() } return nil } // rebalance attempts to combine the node with sibling nodes if the node fill // size is below a threshold or if there are not enough keys. func (n *node) rebalance() { if !n.unbalanced { return } n.unbalanced = false // Update statistics. n.bucket.tx.stats.IncRebalance(1) // Ignore if node is above threshold (25%) and has enough keys. var threshold = n.bucket.tx.db.pageSize / 4 if n.size() > threshold && len(n.inodes) > n.minKeys() { return } // Root node has special handling. if n.parent == nil { // If root node is a branch and only has one node then collapse it. if !n.isLeaf && len(n.inodes) == 1 { // Move root's child up. child := n.bucket.node(n.inodes[0].pgid, n) n.isLeaf = child.isLeaf n.inodes = child.inodes[:] n.children = child.children // Reparent all child nodes being moved. for _, inode := range n.inodes { if child, ok := n.bucket.nodes[inode.pgid]; ok { child.parent = n } } // Remove old child. child.parent = nil delete(n.bucket.nodes, child.pgid) child.free() } return } // If node has no keys then just remove it. if n.numChildren() == 0 { n.parent.del(n.key) n.parent.removeChild(n) delete(n.bucket.nodes, n.pgid) n.free() n.parent.rebalance() return } _assert(n.parent.numChildren() > 1, "parent must have at least 2 children") // Destination node is right sibling if idx == 0, otherwise left sibling. var target *node var useNextSibling = (n.parent.childIndex(n) == 0) if useNextSibling { target = n.nextSibling() } else { target = n.prevSibling() } // If both this node and the target node are too small then merge them. if useNextSibling { // Reparent all child nodes being moved. for _, inode := range target.inodes { if child, ok := n.bucket.nodes[inode.pgid]; ok { child.parent.removeChild(child) child.parent = n child.parent.children = append(child.parent.children, child) } } // Copy over inodes from target and remove target. n.inodes = append(n.inodes, target.inodes...) n.parent.del(target.key) n.parent.removeChild(target) delete(n.bucket.nodes, target.pgid) target.free() } else { // Reparent all child nodes being moved. for _, inode := range n.inodes { if child, ok := n.bucket.nodes[inode.pgid]; ok { child.parent.removeChild(child) child.parent = target child.parent.children = append(child.parent.children, child) } } // Copy over inodes to target and remove node. target.inodes = append(target.inodes, n.inodes...) n.parent.del(n.key) n.parent.removeChild(n) delete(n.bucket.nodes, n.pgid) n.free() } // Either this node or the target node was deleted from the parent so rebalance it. n.parent.rebalance() } // removes a node from the list of in-memory children. // This does not affect the inodes. func (n *node) removeChild(target *node) { for i, child := range n.children { if child == target { n.children = append(n.children[:i], n.children[i+1:]...) return } } } // dereference causes the node to copy all its inode key/value references to heap memory. // This is required when the mmap is reallocated so inodes are not pointing to stale data. func (n *node) dereference() { if n.key != nil { key := make([]byte, len(n.key)) copy(key, n.key) n.key = key _assert(n.pgid == 0 || len(n.key) > 0, "dereference: zero-length node key on existing node") } for i := range n.inodes { inode := &n.inodes[i] key := make([]byte, len(inode.key)) copy(key, inode.key) inode.key = key _assert(len(inode.key) > 0, "dereference: zero-length inode key") value := make([]byte, len(inode.value)) copy(value, inode.value) inode.value = value } // Recursively dereference children. for _, child := range n.children { child.dereference() } // Update statistics. n.bucket.tx.stats.IncNodeDeref(1) } // free adds the node's underlying page to the freelist. func (n *node) free() { if n.pgid != 0 { n.bucket.tx.db.freelist.free(n.bucket.tx.meta.txid, n.bucket.tx.page(n.pgid)) n.pgid = 0 } } // dump writes the contents of the node to STDERR for debugging purposes. /* func (n *node) dump() { // Write node header. var typ = "branch" if n.isLeaf { typ = "leaf" } warnf("[NODE %d {type=%s count=%d}]", n.pgid, typ, len(n.inodes)) // Write out abbreviated version of each item. for _, item := range n.inodes { if n.isLeaf { if item.flags&bucketLeafFlag != 0 { bucket := (*bucket)(unsafe.Pointer(&item.value[0])) warnf("+L %08x -> (bucket root=%d)", trunc(item.key, 4), bucket.root) } else { warnf("+L %08x -> %08x", trunc(item.key, 4), trunc(item.value, 4)) } } else { warnf("+B %08x -> pgid=%d", trunc(item.key, 4), item.pgid) } } warn("") } */ func compareKeys(left, right []byte) int { return bytes.Compare(left, right) } type nodes []*node func (s nodes) Len() int { return len(s) } func (s nodes) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s nodes) Less(i, j int) bool { return bytes.Compare(s[i].inodes[0].key, s[j].inodes[0].key) == -1 } // inode represents an internal node inside of a node. // It can be used to point to elements in a page or point // to an element which hasn't been added to a page yet. type inode struct { flags uint32 pgid pgid key []byte value []byte } type inodes []inode bbolt-1.3.10/node_test.go000066400000000000000000000130561461507540700152270ustar00rootroot00000000000000package bbolt import ( "testing" "unsafe" ) // Ensure that a node can insert a key/value. func TestNode_put(t *testing.T) { n := &node{inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{meta: &meta{pgid: 1}}}} n.put([]byte("baz"), []byte("baz"), []byte("2"), 0, 0) n.put([]byte("foo"), []byte("foo"), []byte("0"), 0, 0) n.put([]byte("bar"), []byte("bar"), []byte("1"), 0, 0) n.put([]byte("foo"), []byte("foo"), []byte("3"), 0, leafPageFlag) if len(n.inodes) != 3 { t.Fatalf("exp=3; got=%d", len(n.inodes)) } if k, v := n.inodes[0].key, n.inodes[0].value; string(k) != "bar" || string(v) != "1" { t.Fatalf("exp=; got=<%s,%s>", k, v) } if k, v := n.inodes[1].key, n.inodes[1].value; string(k) != "baz" || string(v) != "2" { t.Fatalf("exp=; got=<%s,%s>", k, v) } if k, v := n.inodes[2].key, n.inodes[2].value; string(k) != "foo" || string(v) != "3" { t.Fatalf("exp=; got=<%s,%s>", k, v) } if n.inodes[2].flags != uint32(leafPageFlag) { t.Fatalf("not a leaf: %d", n.inodes[2].flags) } } // Ensure that a node can deserialize from a leaf page. func TestNode_read_LeafPage(t *testing.T) { // Create a page. var buf [4096]byte page := (*page)(unsafe.Pointer(&buf[0])) page.flags = leafPageFlag page.count = 2 // Insert 2 elements at the beginning. sizeof(leafPageElement) == 16 nodes := (*[3]leafPageElement)(unsafe.Pointer(uintptr(unsafe.Pointer(page)) + unsafe.Sizeof(*page))) nodes[0] = leafPageElement{flags: 0, pos: 32, ksize: 3, vsize: 4} // pos = sizeof(leafPageElement) * 2 nodes[1] = leafPageElement{flags: 0, pos: 23, ksize: 10, vsize: 3} // pos = sizeof(leafPageElement) + 3 + 4 // Write data for the nodes at the end. const s = "barfoozhelloworldbye" data := unsafeByteSlice(unsafe.Pointer(&nodes[2]), 0, 0, len(s)) copy(data, s) // Deserialize page into a leaf. n := &node{} n.read(page) // Check that there are two inodes with correct data. if !n.isLeaf { t.Fatal("expected leaf") } if len(n.inodes) != 2 { t.Fatalf("exp=2; got=%d", len(n.inodes)) } if k, v := n.inodes[0].key, n.inodes[0].value; string(k) != "bar" || string(v) != "fooz" { t.Fatalf("exp=; got=<%s,%s>", k, v) } if k, v := n.inodes[1].key, n.inodes[1].value; string(k) != "helloworld" || string(v) != "bye" { t.Fatalf("exp=; got=<%s,%s>", k, v) } } // Ensure that a node can serialize into a leaf page. func TestNode_write_LeafPage(t *testing.T) { // Create a node. n := &node{isLeaf: true, inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: &meta{pgid: 1}}}} n.put([]byte("susy"), []byte("susy"), []byte("que"), 0, 0) n.put([]byte("ricki"), []byte("ricki"), []byte("lake"), 0, 0) n.put([]byte("john"), []byte("john"), []byte("johnson"), 0, 0) // Write it to a page. var buf [4096]byte p := (*page)(unsafe.Pointer(&buf[0])) n.write(p) // Read the page back in. n2 := &node{} n2.read(p) // Check that the two pages are the same. if len(n2.inodes) != 3 { t.Fatalf("exp=3; got=%d", len(n2.inodes)) } if k, v := n2.inodes[0].key, n2.inodes[0].value; string(k) != "john" || string(v) != "johnson" { t.Fatalf("exp=; got=<%s,%s>", k, v) } if k, v := n2.inodes[1].key, n2.inodes[1].value; string(k) != "ricki" || string(v) != "lake" { t.Fatalf("exp=; got=<%s,%s>", k, v) } if k, v := n2.inodes[2].key, n2.inodes[2].value; string(k) != "susy" || string(v) != "que" { t.Fatalf("exp=; got=<%s,%s>", k, v) } } // Ensure that a node can split into appropriate subgroups. func TestNode_split(t *testing.T) { // Create a node. n := &node{inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: &meta{pgid: 1}}}} n.put([]byte("00000001"), []byte("00000001"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000002"), []byte("00000002"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000003"), []byte("00000003"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000004"), []byte("00000004"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000005"), []byte("00000005"), []byte("0123456701234567"), 0, 0) // Split between 2 & 3. n.split(100) var parent = n.parent if len(parent.children) != 2 { t.Fatalf("exp=2; got=%d", len(parent.children)) } if len(parent.children[0].inodes) != 2 { t.Fatalf("exp=2; got=%d", len(parent.children[0].inodes)) } if len(parent.children[1].inodes) != 3 { t.Fatalf("exp=3; got=%d", len(parent.children[1].inodes)) } } // Ensure that a page with the minimum number of inodes just returns a single node. func TestNode_split_MinKeys(t *testing.T) { // Create a node. n := &node{inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: &meta{pgid: 1}}}} n.put([]byte("00000001"), []byte("00000001"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000002"), []byte("00000002"), []byte("0123456701234567"), 0, 0) // Split. n.split(20) if n.parent != nil { t.Fatalf("expected nil parent") } } // Ensure that a node that has keys that all fit on a page just returns one leaf. func TestNode_split_SinglePage(t *testing.T) { // Create a node. n := &node{inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: &meta{pgid: 1}}}} n.put([]byte("00000001"), []byte("00000001"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000002"), []byte("00000002"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000003"), []byte("00000003"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000004"), []byte("00000004"), []byte("0123456701234567"), 0, 0) n.put([]byte("00000005"), []byte("00000005"), []byte("0123456701234567"), 0, 0) // Split. n.split(4096) if n.parent != nil { t.Fatalf("expected nil parent") } } bbolt-1.3.10/page.go000066400000000000000000000123431461507540700141550ustar00rootroot00000000000000package bbolt import ( "fmt" "os" "sort" "unsafe" ) const pageHeaderSize = unsafe.Sizeof(page{}) const minKeysPerPage = 2 const branchPageElementSize = unsafe.Sizeof(branchPageElement{}) const leafPageElementSize = unsafe.Sizeof(leafPageElement{}) const ( branchPageFlag = 0x01 leafPageFlag = 0x02 metaPageFlag = 0x04 freelistPageFlag = 0x10 ) const ( bucketLeafFlag = 0x01 ) type pgid uint64 type page struct { id pgid flags uint16 count uint16 overflow uint32 } // typ returns a human readable page type string used for debugging. func (p *page) typ() string { if (p.flags & branchPageFlag) != 0 { return "branch" } else if (p.flags & leafPageFlag) != 0 { return "leaf" } else if (p.flags & metaPageFlag) != 0 { return "meta" } else if (p.flags & freelistPageFlag) != 0 { return "freelist" } return fmt.Sprintf("unknown<%02x>", p.flags) } // meta returns a pointer to the metadata section of the page. func (p *page) meta() *meta { return (*meta)(unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p))) } func (p *page) fastCheck(id pgid) { _assert(p.id == id, "Page expected to be: %v, but self identifies as %v", id, p.id) // Only one flag of page-type can be set. _assert(p.flags == branchPageFlag || p.flags == leafPageFlag || p.flags == metaPageFlag || p.flags == freelistPageFlag, "page %v: has unexpected type/flags: %x", p.id, p.flags) } // leafPageElement retrieves the leaf node by index func (p *page) leafPageElement(index uint16) *leafPageElement { return (*leafPageElement)(unsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p), leafPageElementSize, int(index))) } // leafPageElements retrieves a list of leaf nodes. func (p *page) leafPageElements() []leafPageElement { if p.count == 0 { return nil } data := unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p)) elems := unsafe.Slice((*leafPageElement)(data), int(p.count)) return elems } // branchPageElement retrieves the branch node by index func (p *page) branchPageElement(index uint16) *branchPageElement { return (*branchPageElement)(unsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p), unsafe.Sizeof(branchPageElement{}), int(index))) } // branchPageElements retrieves a list of branch nodes. func (p *page) branchPageElements() []branchPageElement { if p.count == 0 { return nil } data := unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p)) elems := unsafe.Slice((*branchPageElement)(data), int(p.count)) return elems } // dump writes n bytes of the page to STDERR as hex output. func (p *page) hexdump(n int) { buf := unsafeByteSlice(unsafe.Pointer(p), 0, 0, n) fmt.Fprintf(os.Stderr, "%x\n", buf) } type pages []*page func (s pages) Len() int { return len(s) } func (s pages) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s pages) Less(i, j int) bool { return s[i].id < s[j].id } // branchPageElement represents a node on a branch page. type branchPageElement struct { pos uint32 ksize uint32 pgid pgid } // key returns a byte slice of the node key. func (n *branchPageElement) key() []byte { return unsafeByteSlice(unsafe.Pointer(n), 0, int(n.pos), int(n.pos)+int(n.ksize)) } // leafPageElement represents a node on a leaf page. type leafPageElement struct { flags uint32 pos uint32 ksize uint32 vsize uint32 } // key returns a byte slice of the node key. func (n *leafPageElement) key() []byte { i := int(n.pos) j := i + int(n.ksize) return unsafeByteSlice(unsafe.Pointer(n), 0, i, j) } // value returns a byte slice of the node value. func (n *leafPageElement) value() []byte { i := int(n.pos) + int(n.ksize) j := i + int(n.vsize) return unsafeByteSlice(unsafe.Pointer(n), 0, i, j) } // PageInfo represents human readable information about a page. type PageInfo struct { ID int Type string Count int OverflowCount int } type pgids []pgid func (s pgids) Len() int { return len(s) } func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s pgids) Less(i, j int) bool { return s[i] < s[j] } // merge returns the sorted union of a and b. func (a pgids) merge(b pgids) pgids { // Return the opposite slice if one is nil. if len(a) == 0 { return b } if len(b) == 0 { return a } merged := make(pgids, len(a)+len(b)) mergepgids(merged, a, b) return merged } // mergepgids copies the sorted union of a and b into dst. // If dst is too small, it panics. func mergepgids(dst, a, b pgids) { if len(dst) < len(a)+len(b) { panic(fmt.Errorf("mergepgids bad len %d < %d + %d", len(dst), len(a), len(b))) } // Copy in the opposite slice if one is nil. if len(a) == 0 { copy(dst, b) return } if len(b) == 0 { copy(dst, a) return } // Merged will hold all elements from both lists. merged := dst[:0] // Assign lead to the slice with a lower starting value, follow to the higher value. lead, follow := a, b if b[0] < a[0] { lead, follow = b, a } // Continue while there are elements in the lead. for len(lead) > 0 { // Merge largest prefix of lead that is ahead of follow[0]. n := sort.Search(len(lead), func(i int) bool { return lead[i] > follow[0] }) merged = append(merged, lead[:n]...) if n >= len(lead) { break } // Swap lead and follow. lead, follow = follow, lead[n:] } // Append what's left in follow. _ = append(merged, follow...) } bbolt-1.3.10/page_test.go000066400000000000000000000033671461507540700152220ustar00rootroot00000000000000package bbolt import ( "reflect" "sort" "testing" "testing/quick" ) // Ensure that the page type can be returned in human readable format. func TestPage_typ(t *testing.T) { if typ := (&page{flags: branchPageFlag}).typ(); typ != "branch" { t.Fatalf("exp=branch; got=%v", typ) } if typ := (&page{flags: leafPageFlag}).typ(); typ != "leaf" { t.Fatalf("exp=leaf; got=%v", typ) } if typ := (&page{flags: metaPageFlag}).typ(); typ != "meta" { t.Fatalf("exp=meta; got=%v", typ) } if typ := (&page{flags: freelistPageFlag}).typ(); typ != "freelist" { t.Fatalf("exp=freelist; got=%v", typ) } if typ := (&page{flags: 20000}).typ(); typ != "unknown<4e20>" { t.Fatalf("exp=unknown<4e20>; got=%v", typ) } } // Ensure that the hexdump debugging function doesn't blow up. func TestPage_dump(t *testing.T) { (&page{id: 256}).hexdump(16) } func TestPgids_merge(t *testing.T) { a := pgids{4, 5, 6, 10, 11, 12, 13, 27} b := pgids{1, 3, 8, 9, 25, 30} c := a.merge(b) if !reflect.DeepEqual(c, pgids{1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30}) { t.Errorf("mismatch: %v", c) } a = pgids{4, 5, 6, 10, 11, 12, 13, 27, 35, 36} b = pgids{8, 9, 25, 30} c = a.merge(b) if !reflect.DeepEqual(c, pgids{4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30, 35, 36}) { t.Errorf("mismatch: %v", c) } } func TestPgids_merge_quick(t *testing.T) { if err := quick.Check(func(a, b pgids) bool { // Sort incoming lists. sort.Sort(a) sort.Sort(b) // Merge the two lists together. got := a.merge(b) // The expected value should be the two lists combined and sorted. exp := append(a, b...) sort.Sort(exp) if !reflect.DeepEqual(exp, got) { t.Errorf("\nexp=%+v\ngot=%+v\n", exp, got) return false } return true }, nil); err != nil { t.Fatal(err) } } bbolt-1.3.10/quick_test.go000066400000000000000000000047221461507540700154160ustar00rootroot00000000000000package bbolt_test import ( "bytes" "flag" "fmt" "math/rand" "os" "reflect" "testing" "testing/quick" "time" ) // testing/quick defaults to 5 iterations and a random seed. // You can override these settings from the command line: // // -quick.count The number of iterations to perform. // -quick.seed The seed to use for randomizing. // -quick.maxitems The maximum number of items to insert into a DB. // -quick.maxksize The maximum size of a key. // -quick.maxvsize The maximum size of a value. // var qcount, qseed, qmaxitems, qmaxksize, qmaxvsize int func TestMain(m *testing.M) { flag.IntVar(&qcount, "quick.count", 5, "") flag.IntVar(&qseed, "quick.seed", int(time.Now().UnixNano())%100000, "") flag.IntVar(&qmaxitems, "quick.maxitems", 1000, "") flag.IntVar(&qmaxksize, "quick.maxksize", 1024, "") flag.IntVar(&qmaxvsize, "quick.maxvsize", 1024, "") flag.Parse() fmt.Fprintln(os.Stderr, "seed:", qseed) fmt.Fprintf(os.Stderr, "quick settings: count=%v, items=%v, ksize=%v, vsize=%v\n", qcount, qmaxitems, qmaxksize, qmaxvsize) os.Exit(m.Run()) } func qconfig() *quick.Config { return &quick.Config{ MaxCount: qcount, Rand: rand.New(rand.NewSource(int64(qseed))), } } type testdata []testdataitem func (t testdata) Len() int { return len(t) } func (t testdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] } func (t testdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == -1 } func (t testdata) Generate(rand *rand.Rand, size int) reflect.Value { n := rand.Intn(qmaxitems-1) + 1 items := make(testdata, n) used := make(map[string]bool) for i := 0; i < n; i++ { item := &items[i] // Ensure that keys are unique by looping until we find one that we have not already used. for { item.Key = randByteSlice(rand, 1, qmaxksize) if !used[string(item.Key)] { used[string(item.Key)] = true break } } item.Value = randByteSlice(rand, 0, qmaxvsize) } return reflect.ValueOf(items) } type revtestdata []testdataitem func (t revtestdata) Len() int { return len(t) } func (t revtestdata) Swap(i, j int) { t[i], t[j] = t[j], t[i] } func (t revtestdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == 1 } type testdataitem struct { Key []byte Value []byte } func randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte { n := rand.Intn(maxSize-minSize) + minSize b := make([]byte, n) for i := 0; i < n; i++ { b[i] = byte(rand.Intn(255)) } return b } bbolt-1.3.10/scripts/000077500000000000000000000000001461507540700143765ustar00rootroot00000000000000bbolt-1.3.10/scripts/fix.sh000077500000000000000000000011671461507540700155300ustar00rootroot00000000000000GO_CMD="go" # TODO(ptabor): Expand to cover different architectures (GOOS GOARCH), or just list go files. GOFILES=$(${GO_CMD} list --f "{{with \$d:=.}}{{range .GoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) TESTGOFILES=$(${GO_CMD} list --f "{{with \$d:=.}}{{range .TestGoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) XTESTGOFILES=$(${GO_CMD} list --f "{{with \$d:=.}}{{range .XTestGoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) echo "${GOFILES}" "${TESTGOFILES}" "${XTESTGOFILES}"| xargs -n 100 go run golang.org/x/tools/cmd/goimports@latest -w -local go.etcd.io go fmt ./... go mod tidy bbolt-1.3.10/simulation_no_freelist_sync_test.go000066400000000000000000000032711461507540700221110ustar00rootroot00000000000000package bbolt_test import ( "testing" bolt "go.etcd.io/bbolt" ) func TestSimulateNoFreeListSync_1op_1p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 1, 1) } func TestSimulateNoFreeListSync_10op_1p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10, 1) } func TestSimulateNoFreeListSync_100op_1p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 100, 1) } func TestSimulateNoFreeListSync_1000op_1p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 1000, 1) } func TestSimulateNoFreeListSync_10000op_1p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10000, 1) } func TestSimulateNoFreeListSync_10op_10p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10, 10) } func TestSimulateNoFreeListSync_100op_10p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 100, 10) } func TestSimulateNoFreeListSync_1000op_10p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 1000, 10) } func TestSimulateNoFreeListSync_10000op_10p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10000, 10) } func TestSimulateNoFreeListSync_100op_100p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 100, 100) } func TestSimulateNoFreeListSync_1000op_100p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 1000, 100) } func TestSimulateNoFreeListSync_10000op_100p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10000, 100) } func TestSimulateNoFreeListSync_10000op_1000p(t *testing.T) { testSimulate(t, &bolt.Options{NoFreelistSync: true}, 8, 10000, 1000) } bbolt-1.3.10/simulation_test.go000066400000000000000000000212451461507540700164650ustar00rootroot00000000000000package bbolt_test import ( "bytes" "fmt" "math/rand" "sync" "sync/atomic" "testing" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" ) func TestSimulate_1op_1p(t *testing.T) { testSimulate(t, nil, 1, 1, 1) } func TestSimulate_10op_1p(t *testing.T) { testSimulate(t, nil, 1, 10, 1) } func TestSimulate_100op_1p(t *testing.T) { testSimulate(t, nil, 1, 100, 1) } func TestSimulate_1000op_1p(t *testing.T) { testSimulate(t, nil, 1, 1000, 1) } func TestSimulate_10000op_1p(t *testing.T) { testSimulate(t, nil, 1, 10000, 1) } func TestSimulate_10op_10p(t *testing.T) { testSimulate(t, nil, 1, 10, 10) } func TestSimulate_100op_10p(t *testing.T) { testSimulate(t, nil, 1, 100, 10) } func TestSimulate_1000op_10p(t *testing.T) { testSimulate(t, nil, 1, 1000, 10) } func TestSimulate_10000op_10p(t *testing.T) { testSimulate(t, nil, 1, 10000, 10) } func TestSimulate_100op_100p(t *testing.T) { testSimulate(t, nil, 1, 100, 100) } func TestSimulate_1000op_100p(t *testing.T) { testSimulate(t, nil, 1, 1000, 100) } func TestSimulate_10000op_100p(t *testing.T) { testSimulate(t, nil, 1, 10000, 100) } func TestSimulate_10000op_1000p(t *testing.T) { testSimulate(t, nil, 1, 10000, 1000) } // Randomly generate operations on a given database with multiple clients to ensure consistency and thread safety. func testSimulate(t *testing.T, openOption *bolt.Options, round, threadCount, parallelism int) { if testing.Short() { t.Skip("skipping test in short mode.") } // A list of operations that readers and writers can perform. var readerHandlers = []simulateHandler{simulateGetHandler} var writerHandlers = []simulateHandler{simulateGetHandler, simulatePutHandler} var versions = make(map[int]*QuickDB) versions[1] = NewQuickDB() db := btesting.MustCreateDBWithOption(t, openOption) var mutex sync.Mutex for n := 0; n < round; n++ { // Run n threads in parallel, each with their own operation. var threads = make(chan bool, parallelism) var wg sync.WaitGroup // counter for how many goroutines were fired var opCount int64 // counter for ignored operations var igCount int64 var errCh = make(chan error, threadCount) var i int for { // this buffered channel will keep accepting booleans // until it hits the limit defined by the parallelism // argument to testSimulate() threads <- true // this wait group can only be marked "done" from inside // the subsequent goroutine wg.Add(1) writable := ((rand.Int() % 100) < 20) // 20% writers // Choose an operation to execute. var handler simulateHandler if writable { handler = writerHandlers[rand.Intn(len(writerHandlers))] } else { handler = readerHandlers[rand.Intn(len(readerHandlers))] } // Execute a thread for the given operation. go func(writable bool, handler simulateHandler) { defer wg.Done() atomic.AddInt64(&opCount, 1) // Start transaction. tx, err := db.Begin(writable) if err != nil { errCh <- fmt.Errorf("error tx begin: %v", err) return } // Obtain current state of the dataset. mutex.Lock() var qdb = versions[tx.ID()] if writable { qdb = versions[tx.ID()-1].Copy() } mutex.Unlock() // Make sure we commit/rollback the tx at the end and update the state. if writable { defer func() { mutex.Lock() versions[tx.ID()] = qdb mutex.Unlock() if err := tx.Commit(); err != nil { errCh <- err return } }() } else { defer func() { _ = tx.Rollback() }() } // Ignore operation if we don't have data yet. if qdb == nil { atomic.AddInt64(&igCount, 1) return } // Execute handler. handler(tx, qdb) // Release a thread back to the scheduling loop. <-threads }(writable, handler) i++ if i >= threadCount { break } } // Wait until all threads are done. wg.Wait() t.Logf("transactions:%d ignored:%d", opCount, igCount) close(errCh) for err := range errCh { if err != nil { t.Fatalf("error from inside goroutine: %v", err) } } db.MustClose() // I have doubts the DB drop is indented here (as 'versions' is not being reset). // But I'm preserving for now the original behavior. db.MustDeleteFile() db.MustReopen() } } type simulateHandler func(tx *bolt.Tx, qdb *QuickDB) // Retrieves a key from the database and verifies that it is what is expected. func simulateGetHandler(tx *bolt.Tx, qdb *QuickDB) { // Randomly retrieve an existing exist. keys := qdb.Rand() if len(keys) == 0 { return } // Retrieve root bucket. b := tx.Bucket(keys[0]) if b == nil { panic(fmt.Sprintf("bucket[0] expected: %08x\n", trunc(keys[0], 4))) } // Drill into nested buckets. for _, key := range keys[1 : len(keys)-1] { b = b.Bucket(key) if b == nil { panic(fmt.Sprintf("bucket[n] expected: %v -> %v\n", keys, key)) } } // Verify key/value on the final bucket. expected := qdb.Get(keys) actual := b.Get(keys[len(keys)-1]) if !bytes.Equal(actual, expected) { fmt.Println("=== EXPECTED ===") fmt.Println(expected) fmt.Println("=== ACTUAL ===") fmt.Println(actual) fmt.Println("=== END ===") panic("value mismatch") } } // Inserts a key into the database. func simulatePutHandler(tx *bolt.Tx, qdb *QuickDB) { var err error keys, value := randKeys(), randValue() // Retrieve root bucket. b := tx.Bucket(keys[0]) if b == nil { b, err = tx.CreateBucket(keys[0]) if err != nil { panic("create bucket: " + err.Error()) } } // Create nested buckets, if necessary. for _, key := range keys[1 : len(keys)-1] { child := b.Bucket(key) if child != nil { b = child } else { b, err = b.CreateBucket(key) if err != nil { panic("create bucket: " + err.Error()) } } } // Insert into database. if err := b.Put(keys[len(keys)-1], value); err != nil { panic("put: " + err.Error()) } // Insert into in-memory database. qdb.Put(keys, value) } // QuickDB is an in-memory database that replicates the functionality of the // Bolt DB type except that it is entirely in-memory. It is meant for testing // that the Bolt database is consistent. type QuickDB struct { sync.RWMutex m map[string]interface{} } // NewQuickDB returns an instance of QuickDB. func NewQuickDB() *QuickDB { return &QuickDB{m: make(map[string]interface{})} } // Get retrieves the value at a key path. func (db *QuickDB) Get(keys [][]byte) []byte { db.RLock() defer db.RUnlock() m := db.m for _, key := range keys[:len(keys)-1] { value := m[string(key)] if value == nil { return nil } switch value := value.(type) { case map[string]interface{}: m = value case []byte: return nil } } // Only return if it's a simple value. if value, ok := m[string(keys[len(keys)-1])].([]byte); ok { return value } return nil } // Put inserts a value into a key path. func (db *QuickDB) Put(keys [][]byte, value []byte) { db.Lock() defer db.Unlock() // Build buckets all the way down the key path. m := db.m for _, key := range keys[:len(keys)-1] { if _, ok := m[string(key)].([]byte); ok { return // Keypath intersects with a simple value. Do nothing. } if m[string(key)] == nil { m[string(key)] = make(map[string]interface{}) } m = m[string(key)].(map[string]interface{}) } // Insert value into the last key. m[string(keys[len(keys)-1])] = value } // Rand returns a random key path that points to a simple value. func (db *QuickDB) Rand() [][]byte { db.RLock() defer db.RUnlock() if len(db.m) == 0 { return nil } var keys [][]byte db.rand(db.m, &keys) return keys } func (db *QuickDB) rand(m map[string]interface{}, keys *[][]byte) { i, index := 0, rand.Intn(len(m)) for k, v := range m { if i == index { *keys = append(*keys, []byte(k)) if v, ok := v.(map[string]interface{}); ok { db.rand(v, keys) } return } i++ } panic("quickdb rand: out-of-range") } // Copy copies the entire database. func (db *QuickDB) Copy() *QuickDB { db.RLock() defer db.RUnlock() return &QuickDB{m: db.copy(db.m)} } func (db *QuickDB) copy(m map[string]interface{}) map[string]interface{} { clone := make(map[string]interface{}, len(m)) for k, v := range m { switch v := v.(type) { case map[string]interface{}: clone[k] = db.copy(v) default: clone[k] = v } } return clone } func randKey() []byte { var min, max = 1, 1024 n := rand.Intn(max-min) + min b := make([]byte, n) for i := 0; i < n; i++ { b[i] = byte(rand.Intn(255)) } return b } func randKeys() [][]byte { var keys [][]byte var count = rand.Intn(2) + 2 for i := 0; i < count; i++ { keys = append(keys, randKey()) } return keys } func randValue() []byte { n := rand.Intn(8192) b := make([]byte, n) for i := 0; i < n; i++ { b[i] = byte(rand.Intn(255)) } return b } bbolt-1.3.10/tests/000077500000000000000000000000001461507540700140515ustar00rootroot00000000000000bbolt-1.3.10/tests/failpoint/000077500000000000000000000000001461507540700160365ustar00rootroot00000000000000bbolt-1.3.10/tests/failpoint/db_failpoint_test.go000066400000000000000000000134431461507540700220630ustar00rootroot00000000000000package failpoint import ( "fmt" "path/filepath" "testing" "time" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" gofail "go.etcd.io/gofail/runtime" ) func TestFailpoint_MapFail(t *testing.T) { err := gofail.Enable("mapError", `return("map somehow failed")`) require.NoError(t, err) defer func() { err = gofail.Disable("mapError") require.NoError(t, err) }() f := filepath.Join(t.TempDir(), "db") _, err = bolt.Open(f, 0666, nil) require.Error(t, err) require.ErrorContains(t, err, "map somehow failed") } // ensures when munmap fails, the flock is unlocked func TestFailpoint_UnmapFail_DbClose(t *testing.T) { //unmap error on db close //we need to open the db first, and then enable the error. //otherwise the db cannot be opened. f := filepath.Join(t.TempDir(), "db") err := gofail.Enable("unmapError", `return("unmap somehow failed")`) require.NoError(t, err) _, err = bolt.Open(f, 0666, nil) require.Error(t, err) require.ErrorContains(t, err, "unmap somehow failed") //disable the error, and try to reopen the db err = gofail.Disable("unmapError") require.NoError(t, err) db, err := bolt.Open(f, 0666, &bolt.Options{Timeout: 30 * time.Second}) require.NoError(t, err) err = db.Close() require.NoError(t, err) } func TestFailpoint_mLockFail(t *testing.T) { err := gofail.Enable("mlockError", `return("mlock somehow failed")`) require.NoError(t, err) f := filepath.Join(t.TempDir(), "db") _, err = bolt.Open(f, 0666, &bolt.Options{Mlock: true}) require.Error(t, err) require.ErrorContains(t, err, "mlock somehow failed") // It should work after disabling the failpoint. err = gofail.Disable("mlockError") require.NoError(t, err) _, err = bolt.Open(f, 0666, &bolt.Options{Mlock: true}) require.NoError(t, err) } func TestFailpoint_mLockFail_When_remap(t *testing.T) { db := btesting.MustCreateDB(t) db.Mlock = true err := gofail.Enable("mlockError", `return("mlock somehow failed in allocate")`) require.NoError(t, err) err = db.Fill([]byte("data"), 1, 10000, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 100) }, ) require.Error(t, err) require.ErrorContains(t, err, "mlock somehow failed in allocate") // It should work after disabling the failpoint. err = gofail.Disable("mlockError") require.NoError(t, err) db.MustClose() db.MustReopen() err = db.Fill([]byte("data"), 1, 10000, func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) }, func(tx int, k int) []byte { return make([]byte, 100) }, ) require.NoError(t, err) } // TestIssue72 reproduces issue 72. // // When bbolt is processing a `Put` invocation, the key might be concurrently // updated by the application which calls the `Put` API (although it shouldn't). // It might lead to a situation that bbolt use an old key to find a proper // position to insert the key/value pair, but actually inserts a new key. // Eventually it might break the rule that all keys should be sorted. In a // worse case, it might cause page elements to point to already freed pages. // // REF: https://github.com/etcd-io/bbolt/issues/72 func TestIssue72(t *testing.T) { db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096}) bucketName := []byte(t.Name()) err := db.Update(func(tx *bolt.Tx) error { _, txerr := tx.CreateBucket(bucketName) return txerr }) require.NoError(t, err) // The layout is like: // // +--+--+--+ // +------+1 |3 |10+---+ // | +-++--+--+ | // | | | // | | | // +v-+--+ +v-+--+ +-v+--+--+ // |1 |2 | |3 |4 | |10|11|12| // +--+--+ +--+--+ +--+--+--+ // err = db.Update(func(tx *bolt.Tx) error { bk := tx.Bucket(bucketName) for _, id := range []int{1, 2, 3, 4, 10, 11, 12} { if txerr := bk.Put(idToBytes(id), make([]byte, 1000)); txerr != nil { return txerr } } return nil }) require.NoError(t, err) require.NoError(t, gofail.Enable("beforeBucketPut", `sleep(5000)`)) // +--+--+--+ // +------+1 |3 |1 +---+ // | +-++--+--+ | // | | | // | | | // +v-+--+ +v-+--+ +-v+--+--+--+ // |1 |2 | |3 |4 | |1 |10|11|12| // +--+--+ +--+--+ +--+--+--+--+ // key := idToBytes(13) updatedKey := idToBytes(1) err = db.Update(func(tx *bolt.Tx) error { bk := tx.Bucket(bucketName) go func() { time.Sleep(3 * time.Second) copy(key, updatedKey) }() return bk.Put(key, make([]byte, 100)) }) require.NoError(t, err) require.NoError(t, gofail.Disable("beforeBucketPut")) // bbolt inserts 100 into last branch page. Since there are two `1` // keys in branch, spill operation will update first `1` pointer and // then last one won't be updated and continues to point to freed page. // // // +--+--+--+ // +---------------+1 |3 |1 +---------+ // | +--++-+--+ | // | | | // | | | // | +--+--+ +v-+--+ +-----v-----+ // | |1 |2 | |3 |4 | |freed page | // | +--+--+ +--+--+ +-----------+ // | // +v-+--+--+--+---+ // |1 |10|11|12|100| // +--+--+--+--+---+ err = db.Update(func(tx *bolt.Tx) error { return tx.Bucket(bucketName).Put(idToBytes(100), make([]byte, 100)) }) require.NoError(t, err) defer func() { if r := recover(); r != nil { t.Logf("panic info:\n %v", r) } }() // Add more keys to ensure branch node to spill. err = db.Update(func(tx *bolt.Tx) error { bk := tx.Bucket(bucketName) for _, id := range []int{101, 102, 103, 104, 105} { if txerr := bk.Put(idToBytes(id), make([]byte, 1000)); txerr != nil { return txerr } } return nil }) require.NoError(t, err) } func idToBytes(id int) []byte { return []byte(fmt.Sprintf("%010d", id)) } bbolt-1.3.10/tx.go000066400000000000000000000534511461507540700137010ustar00rootroot00000000000000package bbolt import ( "fmt" "io" "os" "sort" "strings" "sync/atomic" "time" "unsafe" ) // txid represents the internal transaction identifier. type txid uint64 // Tx represents a read-only or read/write transaction on the database. // Read-only transactions can be used for retrieving values for keys and creating cursors. // Read/write transactions can create and remove buckets and create and remove keys. // // IMPORTANT: You must commit or rollback transactions when you are done with // them. Pages can not be reclaimed by the writer until no more transactions // are using them. A long running read transaction can cause the database to // quickly grow. type Tx struct { writable bool managed bool db *DB meta *meta root Bucket pages map[pgid]*page stats TxStats commitHandlers []func() // WriteFlag specifies the flag for write-related methods like WriteTo(). // Tx opens the database file with the specified flag to copy the data. // // By default, the flag is unset, which works well for mostly in-memory // workloads. For databases that are much larger than available RAM, // set the flag to syscall.O_DIRECT to avoid trashing the page cache. WriteFlag int } // init initializes the transaction. func (tx *Tx) init(db *DB) { tx.db = db tx.pages = nil // Copy the meta page since it can be changed by the writer. tx.meta = &meta{} db.meta().copy(tx.meta) // Copy over the root bucket. tx.root = newBucket(tx) tx.root.bucket = &bucket{} *tx.root.bucket = tx.meta.root // Increment the transaction id and add a page cache for writable transactions. if tx.writable { tx.pages = make(map[pgid]*page) tx.meta.txid += txid(1) } } // ID returns the transaction id. func (tx *Tx) ID() int { return int(tx.meta.txid) } // DB returns a reference to the database that created the transaction. func (tx *Tx) DB() *DB { return tx.db } // Size returns current database size in bytes as seen by this transaction. func (tx *Tx) Size() int64 { return int64(tx.meta.pgid) * int64(tx.db.pageSize) } // Writable returns whether the transaction can perform write operations. func (tx *Tx) Writable() bool { return tx.writable } // Cursor creates a cursor associated with the root bucket. // All items in the cursor will return a nil value because all root bucket keys point to buckets. // The cursor is only valid as long as the transaction is open. // Do not use a cursor after the transaction is closed. func (tx *Tx) Cursor() *Cursor { return tx.root.Cursor() } // Stats retrieves a copy of the current transaction statistics. func (tx *Tx) Stats() TxStats { return tx.stats } // Bucket retrieves a bucket by name. // Returns nil if the bucket does not exist. // The bucket instance is only valid for the lifetime of the transaction. func (tx *Tx) Bucket(name []byte) *Bucket { return tx.root.Bucket(name) } // CreateBucket creates a new bucket. // Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long. // The bucket instance is only valid for the lifetime of the transaction. func (tx *Tx) CreateBucket(name []byte) (*Bucket, error) { return tx.root.CreateBucket(name) } // CreateBucketIfNotExists creates a new bucket if it doesn't already exist. // Returns an error if the bucket name is blank, or if the bucket name is too long. // The bucket instance is only valid for the lifetime of the transaction. func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) { return tx.root.CreateBucketIfNotExists(name) } // DeleteBucket deletes a bucket. // Returns an error if the bucket cannot be found or if the key represents a non-bucket value. func (tx *Tx) DeleteBucket(name []byte) error { return tx.root.DeleteBucket(name) } // ForEach executes a function for each bucket in the root. // If the provided function returns an error then the iteration is stopped and // the error is returned to the caller. func (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error { return tx.root.ForEach(func(k, v []byte) error { return fn(k, tx.root.Bucket(k)) }) } // OnCommit adds a handler function to be executed after the transaction successfully commits. func (tx *Tx) OnCommit(fn func()) { tx.commitHandlers = append(tx.commitHandlers, fn) } // Commit writes all changes to disk and updates the meta page. // Returns an error if a disk write error occurs, or if Commit is // called on a read-only transaction. func (tx *Tx) Commit() error { _assert(!tx.managed, "managed tx commit not allowed") if tx.db == nil { return ErrTxClosed } else if !tx.writable { return ErrTxNotWritable } // TODO(benbjohnson): Use vectorized I/O to write out dirty pages. // Rebalance nodes which have had deletions. var startTime = time.Now() tx.root.rebalance() if tx.stats.GetRebalance() > 0 { tx.stats.IncRebalanceTime(time.Since(startTime)) } opgid := tx.meta.pgid // spill data onto dirty pages. startTime = time.Now() if err := tx.root.spill(); err != nil { tx.rollback() return err } tx.stats.IncSpillTime(time.Since(startTime)) // Free the old root bucket. tx.meta.root.root = tx.root.root // Free the old freelist because commit writes out a fresh freelist. if tx.meta.freelist != pgidNoFreelist { tx.db.freelist.free(tx.meta.txid, tx.db.page(tx.meta.freelist)) } if !tx.db.NoFreelistSync { err := tx.commitFreelist() if err != nil { return err } } else { tx.meta.freelist = pgidNoFreelist } // If the high water mark has moved up then attempt to grow the database. if tx.meta.pgid > opgid { if err := tx.db.grow(int(tx.meta.pgid+1) * tx.db.pageSize); err != nil { tx.rollback() return err } } // Write dirty pages to disk. startTime = time.Now() if err := tx.write(); err != nil { tx.rollback() return err } // If strict mode is enabled then perform a consistency check. if tx.db.StrictMode { ch := tx.Check() var errs []string for { err, ok := <-ch if !ok { break } errs = append(errs, err.Error()) } if len(errs) > 0 { panic("check fail: " + strings.Join(errs, "\n")) } } // Write meta to disk. if err := tx.writeMeta(); err != nil { tx.rollback() return err } tx.stats.IncWriteTime(time.Since(startTime)) // Finalize the transaction. tx.close() // Execute commit handlers now that the locks have been removed. for _, fn := range tx.commitHandlers { fn() } return nil } func (tx *Tx) commitFreelist() error { // Allocate new pages for the new free list. This will overestimate // the size of the freelist but not underestimate the size (which would be bad). p, err := tx.allocate((tx.db.freelist.size() / tx.db.pageSize) + 1) if err != nil { tx.rollback() return err } if err := tx.db.freelist.write(p); err != nil { tx.rollback() return err } tx.meta.freelist = p.id return nil } // Rollback closes the transaction and ignores all previous updates. Read-only // transactions must be rolled back and not committed. func (tx *Tx) Rollback() error { _assert(!tx.managed, "managed tx rollback not allowed") if tx.db == nil { return ErrTxClosed } tx.nonPhysicalRollback() return nil } // nonPhysicalRollback is called when user calls Rollback directly, in this case we do not need to reload the free pages from disk. func (tx *Tx) nonPhysicalRollback() { if tx.db == nil { return } if tx.writable { tx.db.freelist.rollback(tx.meta.txid) } tx.close() } // rollback needs to reload the free pages from disk in case some system error happens like fsync error. func (tx *Tx) rollback() { if tx.db == nil { return } if tx.writable { tx.db.freelist.rollback(tx.meta.txid) // When mmap fails, the `data`, `dataref` and `datasz` may be reset to // zero values, and there is no way to reload free page IDs in this case. if tx.db.data != nil { if !tx.db.hasSyncedFreelist() { // Reconstruct free page list by scanning the DB to get the whole free page list. // Note: scaning the whole db is heavy if your db size is large in NoSyncFreeList mode. tx.db.freelist.noSyncReload(tx.db.freepages()) } else { // Read free page list from freelist page. tx.db.freelist.reload(tx.db.page(tx.db.meta().freelist)) } } } tx.close() } func (tx *Tx) close() { if tx.db == nil { return } if tx.writable { // Grab freelist stats. var freelistFreeN = tx.db.freelist.free_count() var freelistPendingN = tx.db.freelist.pending_count() var freelistAlloc = tx.db.freelist.size() // Remove transaction ref & writer lock. tx.db.rwtx = nil tx.db.rwlock.Unlock() // Merge statistics. tx.db.statlock.Lock() tx.db.stats.FreePageN = freelistFreeN tx.db.stats.PendingPageN = freelistPendingN tx.db.stats.FreeAlloc = (freelistFreeN + freelistPendingN) * tx.db.pageSize tx.db.stats.FreelistInuse = freelistAlloc tx.db.stats.TxStats.add(&tx.stats) tx.db.statlock.Unlock() } else { tx.db.removeTx(tx) } // Clear all references. tx.db = nil tx.meta = nil tx.root = Bucket{tx: tx} tx.pages = nil } // Copy writes the entire database to a writer. // This function exists for backwards compatibility. // // Deprecated; Use WriteTo() instead. func (tx *Tx) Copy(w io.Writer) error { _, err := tx.WriteTo(w) return err } // WriteTo writes the entire database to a writer. // If err == nil then exactly tx.Size() bytes will be written into the writer. func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) { // Attempt to open reader with WriteFlag f, err := tx.db.openFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0) if err != nil { return 0, err } defer func() { if cerr := f.Close(); err == nil { err = cerr } }() // Generate a meta page. We use the same page data for both meta pages. buf := make([]byte, tx.db.pageSize) page := (*page)(unsafe.Pointer(&buf[0])) page.flags = metaPageFlag *page.meta() = *tx.meta // Write meta 0. page.id = 0 page.meta().checksum = page.meta().sum64() nn, err := w.Write(buf) n += int64(nn) if err != nil { return n, fmt.Errorf("meta 0 copy: %s", err) } // Write meta 1 with a lower transaction id. page.id = 1 page.meta().txid -= 1 page.meta().checksum = page.meta().sum64() nn, err = w.Write(buf) n += int64(nn) if err != nil { return n, fmt.Errorf("meta 1 copy: %s", err) } // Move past the meta pages in the file. if _, err := f.Seek(int64(tx.db.pageSize*2), io.SeekStart); err != nil { return n, fmt.Errorf("seek: %s", err) } // Copy data pages. wn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2)) n += wn if err != nil { return n, err } return n, nil } // CopyFile copies the entire database to file at the given path. // A reader transaction is maintained during the copy so it is safe to continue // using the database while a copy is in progress. func (tx *Tx) CopyFile(path string, mode os.FileMode) error { f, err := tx.db.openFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) if err != nil { return err } _, err = tx.WriteTo(f) if err != nil { _ = f.Close() return err } return f.Close() } // allocate returns a contiguous block of memory starting at a given page. func (tx *Tx) allocate(count int) (*page, error) { p, err := tx.db.allocate(tx.meta.txid, count) if err != nil { return nil, err } // Save to our page cache. tx.pages[p.id] = p // Update statistics. tx.stats.IncPageCount(int64(count)) tx.stats.IncPageAlloc(int64(count * tx.db.pageSize)) return p, nil } // write writes any dirty pages to disk. func (tx *Tx) write() error { // Sort pages by id. pages := make(pages, 0, len(tx.pages)) for _, p := range tx.pages { pages = append(pages, p) } // Clear out page cache early. tx.pages = make(map[pgid]*page) sort.Sort(pages) // Write pages to disk in order. for _, p := range pages { rem := (uint64(p.overflow) + 1) * uint64(tx.db.pageSize) offset := int64(p.id) * int64(tx.db.pageSize) var written uintptr // Write out page in "max allocation" sized chunks. for { sz := rem if sz > maxAllocSize-1 { sz = maxAllocSize - 1 } buf := unsafeByteSlice(unsafe.Pointer(p), written, 0, int(sz)) if _, err := tx.db.ops.writeAt(buf, offset); err != nil { return err } // Update statistics. tx.stats.IncWrite(1) // Exit inner for loop if we've written all the chunks. rem -= sz if rem == 0 { break } // Otherwise move offset forward and move pointer to next chunk. offset += int64(sz) written += uintptr(sz) } } // Ignore file sync if flag is set on DB. if !tx.db.NoSync || IgnoreNoSync { if err := fdatasync(tx.db); err != nil { return err } } // Put small pages back to page pool. for _, p := range pages { // Ignore page sizes over 1 page. // These are allocated using make() instead of the page pool. if int(p.overflow) != 0 { continue } buf := unsafeByteSlice(unsafe.Pointer(p), 0, 0, tx.db.pageSize) // See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1 for i := range buf { buf[i] = 0 } tx.db.pagePool.Put(buf) //nolint:staticcheck } return nil } // writeMeta writes the meta to the disk. func (tx *Tx) writeMeta() error { // Create a temporary buffer for the meta page. buf := make([]byte, tx.db.pageSize) p := tx.db.pageInBuffer(buf, 0) tx.meta.write(p) // Write the meta page to file. if _, err := tx.db.ops.writeAt(buf, int64(p.id)*int64(tx.db.pageSize)); err != nil { return err } if !tx.db.NoSync || IgnoreNoSync { if err := fdatasync(tx.db); err != nil { return err } } // Update statistics. tx.stats.IncWrite(1) return nil } // page returns a reference to the page with a given id. // If page has been written to then a temporary buffered page is returned. func (tx *Tx) page(id pgid) *page { // Check the dirty pages first. if tx.pages != nil { if p, ok := tx.pages[id]; ok { p.fastCheck(id) return p } } // Otherwise return directly from the mmap. p := tx.db.page(id) p.fastCheck(id) return p } // forEachPage iterates over every page within a given page and executes a function. func (tx *Tx) forEachPage(pgidnum pgid, fn func(*page, int, []pgid)) { stack := make([]pgid, 10) stack[0] = pgidnum tx.forEachPageInternal(stack[:1], fn) } func (tx *Tx) forEachPageInternal(pgidstack []pgid, fn func(*page, int, []pgid)) { p := tx.page(pgidstack[len(pgidstack)-1]) // Execute function. fn(p, len(pgidstack)-1, pgidstack) // Recursively loop over children. if (p.flags & branchPageFlag) != 0 { for i := 0; i < int(p.count); i++ { elem := p.branchPageElement(uint16(i)) tx.forEachPageInternal(append(pgidstack, elem.pgid), fn) } } } // Page returns page information for a given page number. // This is only safe for concurrent use when used by a writable transaction. func (tx *Tx) Page(id int) (*PageInfo, error) { if tx.db == nil { return nil, ErrTxClosed } else if pgid(id) >= tx.meta.pgid { return nil, nil } if tx.db.freelist == nil { return nil, ErrFreePagesNotLoaded } // Build the page info. p := tx.db.page(pgid(id)) info := &PageInfo{ ID: id, Count: int(p.count), OverflowCount: int(p.overflow), } // Determine the type (or if it's free). if tx.db.freelist.freed(pgid(id)) { info.Type = "free" } else { info.Type = p.typ() } return info, nil } // TxStats represents statistics about the actions performed by the transaction. type TxStats struct { // Page statistics. // // DEPRECATED: Use GetPageCount() or IncPageCount() PageCount int64 // number of page allocations // DEPRECATED: Use GetPageAlloc() or IncPageAlloc() PageAlloc int64 // total bytes allocated // Cursor statistics. // // DEPRECATED: Use GetCursorCount() or IncCursorCount() CursorCount int64 // number of cursors created // Node statistics // // DEPRECATED: Use GetNodeCount() or IncNodeCount() NodeCount int64 // number of node allocations // DEPRECATED: Use GetNodeDeref() or IncNodeDeref() NodeDeref int64 // number of node dereferences // Rebalance statistics. // // DEPRECATED: Use GetRebalance() or IncRebalance() Rebalance int64 // number of node rebalances // DEPRECATED: Use GetRebalanceTime() or IncRebalanceTime() RebalanceTime time.Duration // total time spent rebalancing // Split/Spill statistics. // // DEPRECATED: Use GetSplit() or IncSplit() Split int64 // number of nodes split // DEPRECATED: Use GetSpill() or IncSpill() Spill int64 // number of nodes spilled // DEPRECATED: Use GetSpillTime() or IncSpillTime() SpillTime time.Duration // total time spent spilling // Write statistics. // // DEPRECATED: Use GetWrite() or IncWrite() Write int64 // number of writes performed // DEPRECATED: Use GetWriteTime() or IncWriteTime() WriteTime time.Duration // total time spent writing to disk } func (s *TxStats) add(other *TxStats) { s.IncPageCount(other.GetPageCount()) s.IncPageAlloc(other.GetPageAlloc()) s.IncCursorCount(other.GetCursorCount()) s.IncNodeCount(other.GetNodeCount()) s.IncNodeDeref(other.GetNodeDeref()) s.IncRebalance(other.GetRebalance()) s.IncRebalanceTime(other.GetRebalanceTime()) s.IncSplit(other.GetSplit()) s.IncSpill(other.GetSpill()) s.IncSpillTime(other.GetSpillTime()) s.IncWrite(other.GetWrite()) s.IncWriteTime(other.GetWriteTime()) } // Sub calculates and returns the difference between two sets of transaction stats. // This is useful when obtaining stats at two different points and time and // you need the performance counters that occurred within that time span. func (s *TxStats) Sub(other *TxStats) TxStats { var diff TxStats diff.PageCount = s.GetPageCount() - other.GetPageCount() diff.PageAlloc = s.GetPageAlloc() - other.GetPageAlloc() diff.CursorCount = s.GetCursorCount() - other.GetCursorCount() diff.NodeCount = s.GetNodeCount() - other.GetNodeCount() diff.NodeDeref = s.GetNodeDeref() - other.GetNodeDeref() diff.Rebalance = s.GetRebalance() - other.GetRebalance() diff.RebalanceTime = s.GetRebalanceTime() - other.GetRebalanceTime() diff.Split = s.GetSplit() - other.GetSplit() diff.Spill = s.GetSpill() - other.GetSpill() diff.SpillTime = s.GetSpillTime() - other.GetSpillTime() diff.Write = s.GetWrite() - other.GetWrite() diff.WriteTime = s.GetWriteTime() - other.GetWriteTime() return diff } // GetPageCount returns PageCount atomically. func (s *TxStats) GetPageCount() int64 { return atomic.LoadInt64(&s.PageCount) } // IncPageCount increases PageCount atomically and returns the new value. func (s *TxStats) IncPageCount(delta int64) int64 { return atomic.AddInt64(&s.PageCount, delta) } // GetPageAlloc returns PageAlloc atomically. func (s *TxStats) GetPageAlloc() int64 { return atomic.LoadInt64(&s.PageAlloc) } // IncPageAlloc increases PageAlloc atomically and returns the new value. func (s *TxStats) IncPageAlloc(delta int64) int64 { return atomic.AddInt64(&s.PageAlloc, delta) } // GetCursorCount returns CursorCount atomically. func (s *TxStats) GetCursorCount() int64 { return atomic.LoadInt64(&s.CursorCount) } // IncCursorCount increases CursorCount atomically and return the new value. func (s *TxStats) IncCursorCount(delta int64) int64 { return atomic.AddInt64(&s.CursorCount, delta) } // GetNodeCount returns NodeCount atomically. func (s *TxStats) GetNodeCount() int64 { return atomic.LoadInt64(&s.NodeCount) } // IncNodeCount increases NodeCount atomically and returns the new value. func (s *TxStats) IncNodeCount(delta int64) int64 { return atomic.AddInt64(&s.NodeCount, delta) } // GetNodeDeref returns NodeDeref atomically. func (s *TxStats) GetNodeDeref() int64 { return atomic.LoadInt64(&s.NodeDeref) } // IncNodeDeref increases NodeDeref atomically and returns the new value. func (s *TxStats) IncNodeDeref(delta int64) int64 { return atomic.AddInt64(&s.NodeDeref, delta) } // GetRebalance returns Rebalance atomically. func (s *TxStats) GetRebalance() int64 { return atomic.LoadInt64(&s.Rebalance) } // IncRebalance increases Rebalance atomically and returns the new value. func (s *TxStats) IncRebalance(delta int64) int64 { return atomic.AddInt64(&s.Rebalance, delta) } // GetRebalanceTime returns RebalanceTime atomically. func (s *TxStats) GetRebalanceTime() time.Duration { return atomicLoadDuration(&s.RebalanceTime) } // IncRebalanceTime increases RebalanceTime atomically and returns the new value. func (s *TxStats) IncRebalanceTime(delta time.Duration) time.Duration { return atomicAddDuration(&s.RebalanceTime, delta) } // GetSplit returns Split atomically. func (s *TxStats) GetSplit() int64 { return atomic.LoadInt64(&s.Split) } // IncSplit increases Split atomically and returns the new value. func (s *TxStats) IncSplit(delta int64) int64 { return atomic.AddInt64(&s.Split, delta) } // GetSpill returns Spill atomically. func (s *TxStats) GetSpill() int64 { return atomic.LoadInt64(&s.Spill) } // IncSpill increases Spill atomically and returns the new value. func (s *TxStats) IncSpill(delta int64) int64 { return atomic.AddInt64(&s.Spill, delta) } // GetSpillTime returns SpillTime atomically. func (s *TxStats) GetSpillTime() time.Duration { return atomicLoadDuration(&s.SpillTime) } // IncSpillTime increases SpillTime atomically and returns the new value. func (s *TxStats) IncSpillTime(delta time.Duration) time.Duration { return atomicAddDuration(&s.SpillTime, delta) } // GetWrite returns Write atomically. func (s *TxStats) GetWrite() int64 { return atomic.LoadInt64(&s.Write) } // IncWrite increases Write atomically and returns the new value. func (s *TxStats) IncWrite(delta int64) int64 { return atomic.AddInt64(&s.Write, delta) } // GetWriteTime returns WriteTime atomically. func (s *TxStats) GetWriteTime() time.Duration { return atomicLoadDuration(&s.WriteTime) } // IncWriteTime increases WriteTime atomically and returns the new value. func (s *TxStats) IncWriteTime(delta time.Duration) time.Duration { return atomicAddDuration(&s.WriteTime, delta) } func atomicAddDuration(ptr *time.Duration, du time.Duration) time.Duration { return time.Duration(atomic.AddInt64((*int64)(unsafe.Pointer(ptr)), int64(du))) } func atomicLoadDuration(ptr *time.Duration) time.Duration { return time.Duration(atomic.LoadInt64((*int64)(unsafe.Pointer(ptr)))) } bbolt-1.3.10/tx_check.go000066400000000000000000000171241461507540700150330ustar00rootroot00000000000000package bbolt import ( "encoding/hex" "fmt" ) // Check performs several consistency checks on the database for this transaction. // An error is returned if any inconsistency is found. // // It can be safely run concurrently on a writable transaction. However, this // incurs a high cost for large databases and databases with a lot of subbuckets // because of caching. This overhead can be removed if running on a read-only // transaction, however, it is not safe to execute other writer transactions at // the same time. func (tx *Tx) Check() <-chan error { return tx.CheckWithOptions() } // CheckWithOptions allows users to provide a customized `KVStringer` implementation, // so that bolt can generate human-readable diagnostic messages. func (tx *Tx) CheckWithOptions(options ...CheckOption) <-chan error { chkConfig := checkConfig{ kvStringer: HexKVStringer(), } for _, op := range options { op(&chkConfig) } ch := make(chan error) go tx.check(chkConfig.kvStringer, ch) return ch } func (tx *Tx) check(kvStringer KVStringer, ch chan error) { // Force loading free list if opened in ReadOnly mode. tx.db.loadFreelist() // Check if any pages are double freed. freed := make(map[pgid]bool) all := make([]pgid, tx.db.freelist.count()) tx.db.freelist.copyall(all) for _, id := range all { if freed[id] { ch <- fmt.Errorf("page %d: already freed", id) } freed[id] = true } // Track every reachable page. reachable := make(map[pgid]*page) reachable[0] = tx.page(0) // meta0 reachable[1] = tx.page(1) // meta1 if tx.meta.freelist != pgidNoFreelist { for i := uint32(0); i <= tx.page(tx.meta.freelist).overflow; i++ { reachable[tx.meta.freelist+pgid(i)] = tx.page(tx.meta.freelist) } } // Recursively check buckets. tx.checkBucket(&tx.root, reachable, freed, kvStringer, ch) // Ensure all pages below high water mark are either reachable or freed. for i := pgid(0); i < tx.meta.pgid; i++ { _, isReachable := reachable[i] if !isReachable && !freed[i] { ch <- fmt.Errorf("page %d: unreachable unfreed", int(i)) } } // Close the channel to signal completion. close(ch) } func (tx *Tx) checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, kvStringer KVStringer, ch chan error) { // Ignore inline buckets. if b.root == 0 { return } // Check every page used by this bucket. b.tx.forEachPage(b.root, func(p *page, _ int, stack []pgid) { if p.id > tx.meta.pgid { ch <- fmt.Errorf("page %d: out of bounds: %d (stack: %v)", int(p.id), int(b.tx.meta.pgid), stack) } // Ensure each page is only referenced once. for i := pgid(0); i <= pgid(p.overflow); i++ { var id = p.id + i if _, ok := reachable[id]; ok { ch <- fmt.Errorf("page %d: multiple references (stack: %v)", int(id), stack) } reachable[id] = p } // We should only encounter un-freed leaf and branch pages. if freed[p.id] { ch <- fmt.Errorf("page %d: reachable freed", int(p.id)) } else if (p.flags&branchPageFlag) == 0 && (p.flags&leafPageFlag) == 0 { ch <- fmt.Errorf("page %d: invalid type: %s (stack: %v)", int(p.id), p.typ(), stack) } }) tx.recursivelyCheckPages(b.root, kvStringer.KeyToString, ch) // Check each bucket within this bucket. _ = b.ForEachBucket(func(k []byte) error { if child := b.Bucket(k); child != nil { tx.checkBucket(child, reachable, freed, kvStringer, ch) } return nil }) } // recursivelyCheckPages confirms database consistency with respect to b-tree // key order constraints: // - keys on pages must be sorted // - keys on children pages are between 2 consecutive keys on the parent's branch page). func (tx *Tx) recursivelyCheckPages(pgId pgid, keyToString func([]byte) string, ch chan error) { tx.recursivelyCheckPagesInternal(pgId, nil, nil, nil, keyToString, ch) } // recursivelyCheckPagesInternal verifies that all keys in the subtree rooted at `pgid` are: // - >=`minKeyClosed` (can be nil) // - <`maxKeyOpen` (can be nil) // - Are in right ordering relationship to their parents. // `pagesStack` is expected to contain IDs of pages from the tree root to `pgid` for the clean debugging message. func (tx *Tx) recursivelyCheckPagesInternal( pgId pgid, minKeyClosed, maxKeyOpen []byte, pagesStack []pgid, keyToString func([]byte) string, ch chan error) (maxKeyInSubtree []byte) { p := tx.page(pgId) pagesStack = append(pagesStack, pgId) switch { case p.flags&branchPageFlag != 0: // For branch page we navigate ranges of all subpages. runningMin := minKeyClosed for i := range p.branchPageElements() { elem := p.branchPageElement(uint16(i)) verifyKeyOrder(elem.pgid, "branch", i, elem.key(), runningMin, maxKeyOpen, ch, keyToString, pagesStack) maxKey := maxKeyOpen if i < len(p.branchPageElements())-1 { maxKey = p.branchPageElement(uint16(i + 1)).key() } maxKeyInSubtree = tx.recursivelyCheckPagesInternal(elem.pgid, elem.key(), maxKey, pagesStack, keyToString, ch) runningMin = maxKeyInSubtree } return maxKeyInSubtree case p.flags&leafPageFlag != 0: runningMin := minKeyClosed for i := range p.leafPageElements() { elem := p.leafPageElement(uint16(i)) verifyKeyOrder(pgId, "leaf", i, elem.key(), runningMin, maxKeyOpen, ch, keyToString, pagesStack) runningMin = elem.key() } if p.count > 0 { return p.leafPageElement(p.count - 1).key() } default: ch <- fmt.Errorf("unexpected page type for pgId:%d", pgId) } return maxKeyInSubtree } /*** * verifyKeyOrder checks whether an entry with given #index on pgId (pageType: "branch|leaf") that has given "key", * is within range determined by (previousKey..maxKeyOpen) and reports found violations to the channel (ch). */ func verifyKeyOrder(pgId pgid, pageType string, index int, key []byte, previousKey []byte, maxKeyOpen []byte, ch chan error, keyToString func([]byte) string, pagesStack []pgid) { if index == 0 && previousKey != nil && compareKeys(previousKey, key) > 0 { ch <- fmt.Errorf("the first key[%d]=(hex)%s on %s page(%d) needs to be >= the key in the ancestor (%s). Stack: %v", index, keyToString(key), pageType, pgId, keyToString(previousKey), pagesStack) } if index > 0 { cmpRet := compareKeys(previousKey, key) if cmpRet > 0 { ch <- fmt.Errorf("key[%d]=(hex)%s on %s page(%d) needs to be > (found <) than previous element (hex)%s. Stack: %v", index, keyToString(key), pageType, pgId, keyToString(previousKey), pagesStack) } if cmpRet == 0 { ch <- fmt.Errorf("key[%d]=(hex)%s on %s page(%d) needs to be > (found =) than previous element (hex)%s. Stack: %v", index, keyToString(key), pageType, pgId, keyToString(previousKey), pagesStack) } } if maxKeyOpen != nil && compareKeys(key, maxKeyOpen) >= 0 { ch <- fmt.Errorf("key[%d]=(hex)%s on %s page(%d) needs to be < than key of the next element in ancestor (hex)%s. Pages stack: %v", index, keyToString(key), pageType, pgId, keyToString(previousKey), pagesStack) } } // =========================================================================================== type checkConfig struct { kvStringer KVStringer } type CheckOption func(options *checkConfig) func WithKVStringer(kvStringer KVStringer) CheckOption { return func(c *checkConfig) { c.kvStringer = kvStringer } } // KVStringer allows to prepare human-readable diagnostic messages. type KVStringer interface { KeyToString([]byte) string ValueToString([]byte) string } // HexKVStringer serializes both key & value to hex representation. func HexKVStringer() KVStringer { return hexKvStringer{} } type hexKvStringer struct{} func (_ hexKvStringer) KeyToString(key []byte) string { return hex.EncodeToString(key) } func (_ hexKvStringer) ValueToString(value []byte) string { return hex.EncodeToString(value) } bbolt-1.3.10/tx_stats_test.go000066400000000000000000000026661461507540700161600ustar00rootroot00000000000000package bbolt import ( "testing" "time" "github.com/stretchr/testify/assert" ) func TestTxStats_add(t *testing.T) { statsA := TxStats{ PageCount: 1, PageAlloc: 2, CursorCount: 3, NodeCount: 100, NodeDeref: 101, Rebalance: 1000, RebalanceTime: 1001 * time.Second, Split: 10000, Spill: 10001, SpillTime: 10001 * time.Second, Write: 100000, WriteTime: 100001 * time.Second, } statsB := TxStats{ PageCount: 2, PageAlloc: 3, CursorCount: 4, NodeCount: 101, NodeDeref: 102, Rebalance: 1001, RebalanceTime: 1002 * time.Second, Split: 11001, Spill: 11002, SpillTime: 11002 * time.Second, Write: 110001, WriteTime: 110010 * time.Second, } statsB.add(&statsA) assert.Equal(t, int64(3), statsB.GetPageCount()) assert.Equal(t, int64(5), statsB.GetPageAlloc()) assert.Equal(t, int64(7), statsB.GetCursorCount()) assert.Equal(t, int64(201), statsB.GetNodeCount()) assert.Equal(t, int64(203), statsB.GetNodeDeref()) assert.Equal(t, int64(2001), statsB.GetRebalance()) assert.Equal(t, 2003*time.Second, statsB.GetRebalanceTime()) assert.Equal(t, int64(21001), statsB.GetSplit()) assert.Equal(t, int64(21003), statsB.GetSpill()) assert.Equal(t, 21003*time.Second, statsB.GetSpillTime()) assert.Equal(t, int64(210001), statsB.GetWrite()) assert.Equal(t, 210011*time.Second, statsB.GetWriteTime()) } bbolt-1.3.10/tx_test.go000066400000000000000000000626121461507540700147370ustar00rootroot00000000000000package bbolt_test import ( "bytes" "errors" "fmt" "log" "os" "runtime" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" ) // TestTx_Check_ReadOnly tests consistency checking on a ReadOnly database. func TestTx_Check_ReadOnly(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.Close(); err != nil { t.Fatal(err) } readOnlyDB, err := bolt.Open(db.Path(), 0666, &bolt.Options{ReadOnly: true}) if err != nil { t.Fatal(err) } defer readOnlyDB.Close() tx, err := readOnlyDB.Begin(false) if err != nil { t.Fatal(err) } // ReadOnly DB will load freelist on Check call. numChecks := 2 errc := make(chan error, numChecks) check := func() { errc <- <-tx.Check() } // Ensure the freelist is not reloaded and does not race. for i := 0; i < numChecks; i++ { go check() } for i := 0; i < numChecks; i++ { if err := <-errc; err != nil { t.Fatal(err) } } // Close the view transaction err = tx.Rollback() if err != nil { t.Fatal(err) } } // Ensure that committing a closed transaction returns an error. func TestTx_Commit_ErrTxClosed(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) if err != nil { t.Fatal(err) } if _, err := tx.CreateBucket([]byte("foo")); err != nil { t.Fatal(err) } if err := tx.Commit(); err != nil { t.Fatal(err) } if err := tx.Commit(); err != bolt.ErrTxClosed { t.Fatalf("unexpected error: %s", err) } } // Ensure that rolling back a closed transaction returns an error. func TestTx_Rollback_ErrTxClosed(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) if err != nil { t.Fatal(err) } if err := tx.Rollback(); err != nil { t.Fatal(err) } if err := tx.Rollback(); err != bolt.ErrTxClosed { t.Fatalf("unexpected error: %s", err) } } // Ensure that committing a read-only transaction returns an error. func TestTx_Commit_ErrTxNotWritable(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(false) if err != nil { t.Fatal(err) } if err := tx.Commit(); err != bolt.ErrTxNotWritable { t.Fatal(err) } // Close the view transaction err = tx.Rollback() if err != nil { t.Fatal(err) } } // Ensure that a transaction can retrieve a cursor on the root bucket. func TestTx_Cursor(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } if _, err := tx.CreateBucket([]byte("woojits")); err != nil { t.Fatal(err) } c := tx.Cursor() if k, v := c.First(); !bytes.Equal(k, []byte("widgets")) { t.Fatalf("unexpected key: %v", k) } else if v != nil { t.Fatalf("unexpected value: %v", v) } if k, v := c.Next(); !bytes.Equal(k, []byte("woojits")) { t.Fatalf("unexpected key: %v", k) } else if v != nil { t.Fatalf("unexpected value: %v", v) } if k, v := c.Next(); k != nil { t.Fatalf("unexpected key: %v", k) } else if v != nil { t.Fatalf("unexpected value: %v", k) } return nil }); err != nil { t.Fatal(err) } } // Ensure that creating a bucket with a read-only transaction returns an error. func TestTx_CreateBucket_ErrTxNotWritable(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.View(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("foo")) if err != bolt.ErrTxNotWritable { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that creating a bucket on a closed transaction returns an error. func TestTx_CreateBucket_ErrTxClosed(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) if err != nil { t.Fatal(err) } if err := tx.Commit(); err != nil { t.Fatal(err) } if _, err := tx.CreateBucket([]byte("foo")); err != bolt.ErrTxClosed { t.Fatalf("unexpected error: %s", err) } } // Ensure that a Tx can retrieve a bucket. func TestTx_Bucket(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } if tx.Bucket([]byte("widgets")) == nil { t.Fatal("expected bucket") } return nil }); err != nil { t.Fatal(err) } } // Ensure that a Tx retrieving a non-existent key returns nil. func TestTx_Get_NotFound(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if b.Get([]byte("no_such_key")) != nil { t.Fatal("expected nil value") } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can be created and retrieved. func TestTx_CreateBucket(t *testing.T) { db := btesting.MustCreateDB(t) // Create a bucket. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } else if b == nil { t.Fatal("expected bucket") } return nil }); err != nil { t.Fatal(err) } // Read the bucket through a separate transaction. if err := db.View(func(tx *bolt.Tx) error { if tx.Bucket([]byte("widgets")) == nil { t.Fatal("expected bucket") } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can be created if it doesn't already exist. func TestTx_CreateBucketIfNotExists(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { // Create bucket. if b, err := tx.CreateBucketIfNotExists([]byte("widgets")); err != nil { t.Fatal(err) } else if b == nil { t.Fatal("expected bucket") } // Create bucket again. if b, err := tx.CreateBucketIfNotExists([]byte("widgets")); err != nil { t.Fatal(err) } else if b == nil { t.Fatal("expected bucket") } return nil }); err != nil { t.Fatal(err) } // Read the bucket through a separate transaction. if err := db.View(func(tx *bolt.Tx) error { if tx.Bucket([]byte("widgets")) == nil { t.Fatal("expected bucket") } return nil }); err != nil { t.Fatal(err) } } // Ensure transaction returns an error if creating an unnamed bucket. func TestTx_CreateBucketIfNotExists_ErrBucketNameRequired(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucketIfNotExists([]byte{}); err != bolt.ErrBucketNameRequired { t.Fatalf("unexpected error: %s", err) } if _, err := tx.CreateBucketIfNotExists(nil); err != bolt.ErrBucketNameRequired { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket cannot be created twice. func TestTx_CreateBucket_ErrBucketExists(t *testing.T) { db := btesting.MustCreateDB(t) // Create a bucket. if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } // Create the same bucket again. if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket([]byte("widgets")); err != bolt.ErrBucketExists { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket is created with a non-blank name. func TestTx_CreateBucket_ErrBucketNameRequired(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if _, err := tx.CreateBucket(nil); err != bolt.ErrBucketNameRequired { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that a bucket can be deleted. func TestTx_DeleteBucket(t *testing.T) { db := btesting.MustCreateDB(t) // Create a bucket and add a value. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } // Delete the bucket and make sure we can't get the value. if err := db.Update(func(tx *bolt.Tx) error { if err := tx.DeleteBucket([]byte("widgets")); err != nil { t.Fatal(err) } if tx.Bucket([]byte("widgets")) != nil { t.Fatal("unexpected bucket") } return nil }); err != nil { t.Fatal(err) } if err := db.Update(func(tx *bolt.Tx) error { // Create the bucket again and make sure there's not a phantom value. b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if v := b.Get([]byte("foo")); v != nil { t.Fatalf("unexpected phantom value: %v", v) } return nil }); err != nil { t.Fatal(err) } } // Ensure that deleting a bucket on a closed transaction returns an error. func TestTx_DeleteBucket_ErrTxClosed(t *testing.T) { db := btesting.MustCreateDB(t) tx, err := db.Begin(true) if err != nil { t.Fatal(err) } if err := tx.Commit(); err != nil { t.Fatal(err) } if err := tx.DeleteBucket([]byte("foo")); err != bolt.ErrTxClosed { t.Fatalf("unexpected error: %s", err) } } // Ensure that deleting a bucket with a read-only transaction returns an error. func TestTx_DeleteBucket_ReadOnly(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.View(func(tx *bolt.Tx) error { if err := tx.DeleteBucket([]byte("foo")); err != bolt.ErrTxNotWritable { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that nothing happens when deleting a bucket that doesn't exist. func TestTx_DeleteBucket_NotFound(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { if err := tx.DeleteBucket([]byte("widgets")); err != bolt.ErrBucketNotFound { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that no error is returned when a tx.ForEach function does not return // an error. func TestTx_ForEach_NoError(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if err := tx.ForEach(func(name []byte, b *bolt.Bucket) error { return nil }); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that an error is returned when a tx.ForEach function returns an error. func TestTx_ForEach_WithError(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } marker := errors.New("marker") if err := tx.ForEach(func(name []byte, b *bolt.Bucket) error { return marker }); err != marker { t.Fatalf("unexpected error: %s", err) } return nil }); err != nil { t.Fatal(err) } } // Ensure that Tx commit handlers are called after a transaction successfully commits. func TestTx_OnCommit(t *testing.T) { db := btesting.MustCreateDB(t) var x int if err := db.Update(func(tx *bolt.Tx) error { tx.OnCommit(func() { x += 1 }) tx.OnCommit(func() { x += 2 }) if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } else if x != 3 { t.Fatalf("unexpected x: %d", x) } } // Ensure that Tx commit handlers are NOT called after a transaction rolls back. func TestTx_OnCommit_Rollback(t *testing.T) { db := btesting.MustCreateDB(t) var x int if err := db.Update(func(tx *bolt.Tx) error { tx.OnCommit(func() { x += 1 }) tx.OnCommit(func() { x += 2 }) if _, err := tx.CreateBucket([]byte("widgets")); err != nil { t.Fatal(err) } return errors.New("rollback this commit") }); err == nil || err.Error() != "rollback this commit" { t.Fatalf("unexpected error: %s", err) } else if x != 0 { t.Fatalf("unexpected x: %d", x) } } // Ensure that the database can be copied to a file path. func TestTx_CopyFile(t *testing.T) { db := btesting.MustCreateDB(t) path := tempfile() if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if err := b.Put([]byte("baz"), []byte("bat")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { return tx.CopyFile(path, 0600) }); err != nil { t.Fatal(err) } db2, err := bolt.Open(path, 0600, nil) if err != nil { t.Fatal(err) } if err := db2.View(func(tx *bolt.Tx) error { if v := tx.Bucket([]byte("widgets")).Get([]byte("foo")); !bytes.Equal(v, []byte("bar")) { t.Fatalf("unexpected value: %v", v) } if v := tx.Bucket([]byte("widgets")).Get([]byte("baz")); !bytes.Equal(v, []byte("bat")) { t.Fatalf("unexpected value: %v", v) } return nil }); err != nil { t.Fatal(err) } if err := db2.Close(); err != nil { t.Fatal(err) } } type failWriterError struct{} func (failWriterError) Error() string { return "error injected for tests" } type failWriter struct { // fail after this many bytes After int } func (f *failWriter) Write(p []byte) (n int, err error) { n = len(p) if n > f.After { n = f.After err = failWriterError{} } f.After -= n return n, err } // Ensure that Copy handles write errors right. func TestTx_CopyFile_Error_Meta(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if err := b.Put([]byte("baz"), []byte("bat")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { return tx.Copy(&failWriter{}) }); err == nil || err.Error() != "meta 0 copy: error injected for tests" { t.Fatalf("unexpected error: %v", err) } } // Ensure that Copy handles write errors right. func TestTx_CopyFile_Error_Normal(t *testing.T) { db := btesting.MustCreateDB(t) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { t.Fatal(err) } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { t.Fatal(err) } if err := b.Put([]byte("baz"), []byte("bat")); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } if err := db.View(func(tx *bolt.Tx) error { return tx.Copy(&failWriter{3 * db.Info().PageSize}) }); err == nil || err.Error() != "error injected for tests" { t.Fatalf("unexpected error: %v", err) } } // TestTx_Rollback ensures there is no error when tx rollback whether we sync freelist or not. func TestTx_Rollback(t *testing.T) { for _, isSyncFreelist := range []bool{false, true} { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) db.NoFreelistSync = isSyncFreelist tx, err := db.Begin(true) if err != nil { t.Fatalf("Error starting tx: %v", err) } bucket := []byte("mybucket") if _, err := tx.CreateBucket(bucket); err != nil { t.Fatalf("Error creating bucket: %v", err) } if err := tx.Commit(); err != nil { t.Fatalf("Error on commit: %v", err) } tx, err = db.Begin(true) if err != nil { t.Fatalf("Error starting tx: %v", err) } b := tx.Bucket(bucket) if err := b.Put([]byte("k"), []byte("v")); err != nil { t.Fatalf("Error on put: %v", err) } // Imagine there is an error and tx needs to be rolled-back if err := tx.Rollback(); err != nil { t.Fatalf("Error on rollback: %v", err) } tx, err = db.Begin(false) if err != nil { t.Fatalf("Error starting tx: %v", err) } b = tx.Bucket(bucket) if v := b.Get([]byte("k")); v != nil { t.Fatalf("Value for k should not have been stored") } if err := tx.Rollback(); err != nil { t.Fatalf("Error on rollback: %v", err) } } } // TestTx_releaseRange ensures db.freePages handles page releases // correctly when there are transaction that are no longer reachable // via any read/write transactions and are "between" ongoing read // transactions, which requires they must be freed by // freelist.releaseRange. func TestTx_releaseRange(t *testing.T) { // Set initial mmap size well beyond the limit we will hit in this // test, since we are testing with long running read transactions // and will deadlock if db.grow is triggered. db := btesting.MustCreateDBWithOption(t, &bolt.Options{InitialMmapSize: os.Getpagesize() * 100}) bucket := "bucket" put := func(key, value string) { if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte(bucket)) if err != nil { t.Fatal(err) } return b.Put([]byte(key), []byte(value)) }); err != nil { t.Fatal(err) } } del := func(key string) { if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte(bucket)) if err != nil { t.Fatal(err) } return b.Delete([]byte(key)) }); err != nil { t.Fatal(err) } } getWithTxn := func(txn *bolt.Tx, key string) []byte { return txn.Bucket([]byte(bucket)).Get([]byte(key)) } openReadTxn := func() *bolt.Tx { readTx, err := db.Begin(false) if err != nil { t.Fatal(err) } return readTx } checkWithReadTxn := func(txn *bolt.Tx, key string, wantValue []byte) { value := getWithTxn(txn, key) if !bytes.Equal(value, wantValue) { t.Errorf("Wanted value to be %s for key %s, but got %s", wantValue, key, string(value)) } } rollback := func(txn *bolt.Tx) { if err := txn.Rollback(); err != nil { t.Fatal(err) } } put("k1", "v1") rtx1 := openReadTxn() put("k2", "v2") hold1 := openReadTxn() put("k3", "v3") hold2 := openReadTxn() del("k3") rtx2 := openReadTxn() del("k1") hold3 := openReadTxn() del("k2") hold4 := openReadTxn() put("k4", "v4") hold5 := openReadTxn() // Close the read transactions we established to hold a portion of the pages in pending state. rollback(hold1) rollback(hold2) rollback(hold3) rollback(hold4) rollback(hold5) // Execute a write transaction to trigger a releaseRange operation in the db // that will free multiple ranges between the remaining open read transactions, now that the // holds have been rolled back. put("k4", "v4") // Check that all long running reads still read correct values. checkWithReadTxn(rtx1, "k1", []byte("v1")) checkWithReadTxn(rtx2, "k2", []byte("v2")) rollback(rtx1) rollback(rtx2) // Check that the final state is correct. rtx7 := openReadTxn() checkWithReadTxn(rtx7, "k1", nil) checkWithReadTxn(rtx7, "k2", nil) checkWithReadTxn(rtx7, "k3", nil) checkWithReadTxn(rtx7, "k4", []byte("v4")) rollback(rtx7) } func ExampleTx_Rollback() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Create a bucket. if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { log.Fatal(err) } // Set a value for a key. if err := db.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")) }); err != nil { log.Fatal(err) } // Update the key but rollback the transaction so it never saves. tx, err := db.Begin(true) if err != nil { log.Fatal(err) } b := tx.Bucket([]byte("widgets")) if err := b.Put([]byte("foo"), []byte("baz")); err != nil { log.Fatal(err) } if err := tx.Rollback(); err != nil { log.Fatal(err) } // Ensure that our original value is still set. if err := db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value for 'foo' is still: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) } // Output: // The value for 'foo' is still: bar } func ExampleTx_CopyFile() { // Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Create a bucket and a key. if 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 }); err != nil { log.Fatal(err) } // Copy the database to another file. toFile := tempfile() if err := db.View(func(tx *bolt.Tx) error { return tx.CopyFile(toFile, 0666) }); err != nil { log.Fatal(err) } defer os.Remove(toFile) // Open the cloned database. db2, err := bolt.Open(toFile, 0666, nil) if err != nil { log.Fatal(err) } // Ensure that the key exists in the copy. if err := db2.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value for 'foo' in the clone is: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) } if err := db2.Close(); err != nil { log.Fatal(err) } // Output: // The value for 'foo' in the clone is: bar } func TestTxStats_GetAndIncAtomically(t *testing.T) { var stats bolt.TxStats stats.IncPageCount(1) assert.Equal(t, int64(1), stats.GetPageCount()) stats.IncPageAlloc(2) assert.Equal(t, int64(2), stats.GetPageAlloc()) stats.IncCursorCount(3) assert.Equal(t, int64(3), stats.GetCursorCount()) stats.IncNodeCount(100) assert.Equal(t, int64(100), stats.GetNodeCount()) stats.IncNodeDeref(101) assert.Equal(t, int64(101), stats.GetNodeDeref()) stats.IncRebalance(1000) assert.Equal(t, int64(1000), stats.GetRebalance()) stats.IncRebalanceTime(1001 * time.Second) assert.Equal(t, 1001*time.Second, stats.GetRebalanceTime()) stats.IncSplit(10000) assert.Equal(t, int64(10000), stats.GetSplit()) stats.IncSpill(10001) assert.Equal(t, int64(10001), stats.GetSpill()) stats.IncSpillTime(10001 * time.Second) assert.Equal(t, 10001*time.Second, stats.GetSpillTime()) stats.IncWrite(100000) assert.Equal(t, int64(100000), stats.GetWrite()) stats.IncWriteTime(100001 * time.Second) assert.Equal(t, 100001*time.Second, stats.GetWriteTime()) assert.Equal(t, bolt.TxStats{ PageCount: 1, PageAlloc: 2, CursorCount: 3, NodeCount: 100, NodeDeref: 101, Rebalance: 1000, RebalanceTime: 1001 * time.Second, Split: 10000, Spill: 10001, SpillTime: 10001 * time.Second, Write: 100000, WriteTime: 100001 * time.Second, }, stats, ) } func TestTxStats_Sub(t *testing.T) { statsA := bolt.TxStats{ PageCount: 1, PageAlloc: 2, CursorCount: 3, NodeCount: 100, NodeDeref: 101, Rebalance: 1000, RebalanceTime: 1001 * time.Second, Split: 10000, Spill: 10001, SpillTime: 10001 * time.Second, Write: 100000, WriteTime: 100001 * time.Second, } statsB := bolt.TxStats{ PageCount: 2, PageAlloc: 3, CursorCount: 4, NodeCount: 101, NodeDeref: 102, Rebalance: 1001, RebalanceTime: 1002 * time.Second, Split: 11001, Spill: 11002, SpillTime: 11002 * time.Second, Write: 110001, WriteTime: 110010 * time.Second, } diff := statsB.Sub(&statsA) assert.Equal(t, int64(1), diff.GetPageCount()) assert.Equal(t, int64(1), diff.GetPageAlloc()) assert.Equal(t, int64(1), diff.GetCursorCount()) assert.Equal(t, int64(1), diff.GetNodeCount()) assert.Equal(t, int64(1), diff.GetNodeDeref()) assert.Equal(t, int64(1), diff.GetRebalance()) assert.Equal(t, time.Second, diff.GetRebalanceTime()) assert.Equal(t, int64(1001), diff.GetSplit()) assert.Equal(t, int64(1001), diff.GetSpill()) assert.Equal(t, 1001*time.Second, diff.GetSpillTime()) assert.Equal(t, int64(10001), diff.GetWrite()) assert.Equal(t, 10009*time.Second, diff.GetWriteTime()) } // TestTx_TruncateBeforeWrite ensures the file is truncated ahead whether we sync freelist or not. func TestTx_TruncateBeforeWrite(t *testing.T) { if runtime.GOOS == "windows" { return } for _, isSyncFreelist := range []bool{false, true} { t.Run(fmt.Sprintf("isSyncFreelist:%v", isSyncFreelist), func(t *testing.T) { // Open the database. db := btesting.MustCreateDBWithOption(t, &bolt.Options{ NoFreelistSync: isSyncFreelist, }) bigvalue := make([]byte, db.AllocSize/100) count := 0 for { count++ tx, err := db.Begin(true) require.NoError(t, err) b, err := tx.CreateBucketIfNotExists([]byte("bucket")) require.NoError(t, err) err = b.Put([]byte{byte(count)}, bigvalue) require.NoError(t, err) err = tx.Commit() require.NoError(t, err) size := fileSize(db.Path()) if size > int64(db.AllocSize) && size < int64(db.AllocSize)*2 { // db.grow expands the file aggresively, that double the size while smaller than db.AllocSize, // or increase with a step of db.AllocSize if larger, by which we can test if db.grow has run. t.Fatalf("db.grow doesn't run when file size changes. file size: %d", size) } if size > int64(db.AllocSize) { break } } db.MustClose() db.MustDeleteFile() }) } } bbolt-1.3.10/unix_test.go000066400000000000000000000046161461507540700152670ustar00rootroot00000000000000//go:build !windows // +build !windows package bbolt_test import ( "fmt" "testing" "golang.org/x/sys/unix" bolt "go.etcd.io/bbolt" "go.etcd.io/bbolt/internal/btesting" ) func TestMlock_DbOpen(t *testing.T) { // 32KB skipOnMemlockLimitBelow(t, 32*1024) btesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true}) } // Test change between "empty" (16KB) and "non-empty" db func TestMlock_DbCanGrow_Small(t *testing.T) { // 32KB skipOnMemlockLimitBelow(t, 32*1024) db := btesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true}) if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("bucket")) if err != nil { t.Fatal(err) } key := []byte("key") value := []byte("value") if err := b.Put(key, value); err != nil { t.Fatal(err) } return nil }); err != nil { t.Fatal(err) } } // Test crossing of 16MB (AllocSize) of db size func TestMlock_DbCanGrow_Big(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode") } // 32MB skipOnMemlockLimitBelow(t, 32*1024*1024) chunksBefore := 64 chunksAfter := 64 db := btesting.MustCreateDBWithOption(t, &bolt.Options{Mlock: true}) for chunk := 0; chunk < chunksBefore; chunk++ { insertChunk(t, db, chunk) } dbSize := fileSize(db.Path()) for chunk := 0; chunk < chunksAfter; chunk++ { insertChunk(t, db, chunksBefore+chunk) } newDbSize := fileSize(db.Path()) if newDbSize <= dbSize { t.Errorf("db didn't grow: %v <= %v", newDbSize, dbSize) } } func insertChunk(t *testing.T, db *btesting.DB, chunkId int) { chunkSize := 1024 if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte("bucket")) if err != nil { t.Fatal(err) } for i := 0; i < chunkSize; i++ { key := []byte(fmt.Sprintf("key-%d-%d", chunkId, i)) value := []byte("value") if err := b.Put(key, value); err != nil { t.Fatal(err) } } return nil }); err != nil { t.Fatal(err) } } // Main reason for this check is travis limiting mlockable memory to 64KB // https://github.com/travis-ci/travis-ci/issues/2462 func skipOnMemlockLimitBelow(t *testing.T, memlockLimitRequest uint64) { var info unix.Rlimit if err := unix.Getrlimit(unix.RLIMIT_MEMLOCK, &info); err != nil { t.Fatal(err) } if info.Cur < memlockLimitRequest { t.Skipf( "skipping as RLIMIT_MEMLOCK is insufficient: %v < %v", info.Cur, memlockLimitRequest, ) } } bbolt-1.3.10/unsafe.go000066400000000000000000000020641461507540700145210ustar00rootroot00000000000000package bbolt import ( "unsafe" ) func unsafeAdd(base unsafe.Pointer, offset uintptr) unsafe.Pointer { return unsafe.Pointer(uintptr(base) + offset) } func unsafeIndex(base unsafe.Pointer, offset uintptr, elemsz uintptr, n int) unsafe.Pointer { return unsafe.Pointer(uintptr(base) + offset + uintptr(n)*elemsz) } func unsafeByteSlice(base unsafe.Pointer, offset uintptr, i, j int) []byte { // See: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices // // This memory is not allocated from C, but it is unmanaged by Go's // garbage collector and should behave similarly, and the compiler // should produce similar code. Note that this conversion allows a // subslice to begin after the base address, with an optional offset, // while the URL above does not cover this case and only slices from // index 0. However, the wiki never says that the address must be to // the beginning of a C allocation (or even that malloc was used at // all), so this is believed to be correct. return (*[maxAllocSize]byte)(unsafeAdd(base, offset))[i:j:j] }