[
  {
    "path": ".gitignore",
    "content": "*.prof\n*.test\n*.swp\n/bin/\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2013 Ben Johnson\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "Makefile",
    "content": "BRANCH=`git rev-parse --abbrev-ref HEAD`\nCOMMIT=`git rev-parse --short HEAD`\nGOLDFLAGS=\"-X main.branch $(BRANCH) -X main.commit $(COMMIT)\"\n\ndefault: build\n\nrace:\n\t@go test -v -race -test.run=\"TestSimulate_(100op|1000op)\"\n\n# go get github.com/kisielk/errcheck\nerrcheck:\n\t@errcheck -ignorepkg=bytes -ignore=os:Remove github.com/boltdb/bolt\n\ntest: \n\t@go test -v -cover .\n\t@go test -v ./cmd/bolt\n\n.PHONY: fmt test\n"
  },
  {
    "path": "README.md",
    "content": "Bolt [![Coverage Status](https://coveralls.io/repos/boltdb/bolt/badge.svg?branch=master)](https://coveralls.io/r/boltdb/bolt?branch=master) [![GoDoc](https://godoc.org/github.com/boltdb/bolt?status.svg)](https://godoc.org/github.com/boltdb/bolt) ![Version](https://img.shields.io/badge/version-1.2.1-green.svg)\n====\n\nBolt is a pure Go key/value store inspired by [Howard Chu's][hyc_symas]\n[LMDB project][lmdb]. The goal of the project is to provide a simple,\nfast, and reliable database for projects that don't require a full database\nserver such as Postgres or MySQL.\n\nSince Bolt is meant to be used as such a low-level piece of functionality,\nsimplicity is key. The API will be small and only focus on getting values\nand setting values. That's it.\n\n[hyc_symas]: https://twitter.com/hyc_symas\n[lmdb]: http://symas.com/mdb/\n\n## Project Status\n\nBolt is stable, the API is fixed, and the file format is fixed. Full unit\ntest coverage and randomized black box testing are used to ensure database\nconsistency and thread safety. Bolt is currently used in high-load production\nenvironments serving databases as large as 1TB. Many companies such as\nShopify and Heroku use Bolt-backed services every day.\n\n## A message from the author\n\n> The original goal of Bolt was to provide a simple pure Go key/value store and to\n> not bloat the code with extraneous features. To that end, the project has been\n> a success. However, this limited scope also means that the project is complete.\n> \n> Maintaining an open source database requires an immense amount of time and energy.\n> Changes to the code can have unintended and sometimes catastrophic effects so\n> even simple changes require hours and hours of careful testing and validation.\n>\n> Unfortunately I no longer have the time or energy to continue this work. Bolt is\n> in a stable state and has years of successful production use. As such, I feel that\n> leaving it in its current state is the most prudent course of action.\n>\n> If you are interested in using a more featureful version of Bolt, I suggest that\n> you look at the CoreOS fork called [bbolt](https://github.com/coreos/bbolt).\n\n- Ben Johnson ([@benbjohnson](https://twitter.com/benbjohnson))\n\n## Table of Contents\n\n- [Getting Started](#getting-started)\n  - [Installing](#installing)\n  - [Opening a database](#opening-a-database)\n  - [Transactions](#transactions)\n    - [Read-write transactions](#read-write-transactions)\n    - [Read-only transactions](#read-only-transactions)\n    - [Batch read-write transactions](#batch-read-write-transactions)\n    - [Managing transactions manually](#managing-transactions-manually)\n  - [Using buckets](#using-buckets)\n  - [Using key/value pairs](#using-keyvalue-pairs)\n  - [Autoincrementing integer for the bucket](#autoincrementing-integer-for-the-bucket)\n  - [Iterating over keys](#iterating-over-keys)\n    - [Prefix scans](#prefix-scans)\n    - [Range scans](#range-scans)\n    - [ForEach()](#foreach)\n  - [Nested buckets](#nested-buckets)\n  - [Database backups](#database-backups)\n  - [Statistics](#statistics)\n  - [Read-Only Mode](#read-only-mode)\n  - [Mobile Use (iOS/Android)](#mobile-use-iosandroid)\n- [Resources](#resources)\n- [Comparison with other databases](#comparison-with-other-databases)\n  - [Postgres, MySQL, & other relational databases](#postgres-mysql--other-relational-databases)\n  - [LevelDB, RocksDB](#leveldb-rocksdb)\n  - [LMDB](#lmdb)\n- [Caveats & Limitations](#caveats--limitations)\n- [Reading the Source](#reading-the-source)\n- [Other Projects Using Bolt](#other-projects-using-bolt)\n\n## Getting Started\n\n### Installing\n\nTo start using Bolt, install Go and run `go get`:\n\n```sh\n$ go get github.com/boltdb/bolt/...\n```\n\nThis will retrieve the library and install the `bolt` command line utility into\nyour `$GOBIN` path.\n\n\n### Opening a database\n\nThe top-level object in Bolt is a `DB`. It is represented as a single file on\nyour disk and represents a consistent snapshot of your data.\n\nTo open your database, simply use the `bolt.Open()` function:\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\n\t\"github.com/boltdb/bolt\"\n)\n\nfunc main() {\n\t// Open the my.db data file in your current directory.\n\t// It will be created if it doesn't exist.\n\tdb, err := bolt.Open(\"my.db\", 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer db.Close()\n\n\t...\n}\n```\n\nPlease note that Bolt obtains a file lock on the data file so multiple processes\ncannot open the same database at the same time. Opening an already open Bolt\ndatabase will cause it to hang until the other process closes it. To prevent\nan indefinite wait you can pass a timeout option to the `Open()` function:\n\n```go\ndb, err := bolt.Open(\"my.db\", 0600, &bolt.Options{Timeout: 1 * time.Second})\n```\n\n\n### Transactions\n\nBolt allows only one read-write transaction at a time but allows as many\nread-only transactions as you want at a time. Each transaction has a consistent\nview of the data as it existed when the transaction started.\n\nIndividual transactions and all objects created from them (e.g. buckets, keys)\nare not thread safe. To work with data in multiple goroutines you must start\na transaction for each one or use locking to ensure only one goroutine accesses\na transaction at a time. Creating transaction from the `DB` is thread safe.\n\nRead-only transactions and read-write transactions should not depend on one\nanother and generally shouldn't be opened simultaneously in the same goroutine.\nThis can cause a deadlock as the read-write transaction needs to periodically\nre-map the data file but it cannot do so while a read-only transaction is open.\n\n\n#### Read-write transactions\n\nTo start a read-write transaction, you can use the `DB.Update()` function:\n\n```go\nerr := db.Update(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\n```\n\nInside the closure, you have a consistent view of the database. You commit the\ntransaction by returning `nil` at the end. You can also rollback the transaction\nat any point by returning an error. All database operations are allowed inside\na read-write transaction.\n\nAlways check the return error as it will report any disk failures that can cause\nyour transaction to not complete. If you return an error within your closure\nit will be passed through.\n\n\n#### Read-only transactions\n\nTo start a read-only transaction, you can use the `DB.View()` function:\n\n```go\nerr := db.View(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\n```\n\nYou also get a consistent view of the database within this closure, however,\nno mutating operations are allowed within a read-only transaction. You can only\nretrieve buckets, retrieve values, and copy the database within a read-only\ntransaction.\n\n\n#### Batch read-write transactions\n\nEach `DB.Update()` waits for disk to commit the writes. This overhead\ncan be minimized by combining multiple updates with the `DB.Batch()`\nfunction:\n\n```go\nerr := db.Batch(func(tx *bolt.Tx) error {\n\t...\n\treturn nil\n})\n```\n\nConcurrent Batch calls are opportunistically combined into larger\ntransactions. Batch is only useful when there are multiple goroutines\ncalling it.\n\nThe trade-off is that `Batch` can call the given\nfunction multiple times, if parts of the transaction fail. The\nfunction must be idempotent and side effects must take effect only\nafter a successful return from `DB.Batch()`.\n\nFor example: don't display messages from inside the function, instead\nset variables in the enclosing scope:\n\n```go\nvar id uint64\nerr := db.Batch(func(tx *bolt.Tx) error {\n\t// Find last key in bucket, decode as bigendian uint64, increment\n\t// by one, encode back to []byte, and add new key.\n\t...\n\tid = newValue\n\treturn nil\n})\nif err != nil {\n\treturn ...\n}\nfmt.Println(\"Allocated ID %d\", id)\n```\n\n\n#### Managing transactions manually\n\nThe `DB.View()` and `DB.Update()` functions are wrappers around the `DB.Begin()`\nfunction. These helper functions will start the transaction, execute a function,\nand then safely close your transaction if an error is returned. This is the\nrecommended way to use Bolt transactions.\n\nHowever, sometimes you may want to manually start and end your transactions.\nYou can use the `DB.Begin()` function directly but **please** be sure to close\nthe transaction.\n\n```go\n// Start a writable transaction.\ntx, err := db.Begin(true)\nif err != nil {\n    return err\n}\ndefer tx.Rollback()\n\n// Use the transaction...\n_, err := tx.CreateBucket([]byte(\"MyBucket\"))\nif err != nil {\n    return err\n}\n\n// Commit the transaction and check for error.\nif err := tx.Commit(); err != nil {\n    return err\n}\n```\n\nThe first argument to `DB.Begin()` is a boolean stating if the transaction\nshould be writable.\n\n\n### Using buckets\n\nBuckets are collections of key/value pairs within the database. All keys in a\nbucket must be unique. You can create a bucket using the `DB.CreateBucket()`\nfunction:\n\n```go\ndb.Update(func(tx *bolt.Tx) error {\n\tb, err := tx.CreateBucket([]byte(\"MyBucket\"))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create bucket: %s\", err)\n\t}\n\treturn nil\n})\n```\n\nYou can also create a bucket only if it doesn't exist by using the\n`Tx.CreateBucketIfNotExists()` function. It's a common pattern to call this\nfunction for all your top-level buckets after you open your database so you can\nguarantee that they exist for future transactions.\n\nTo delete a bucket, simply call the `Tx.DeleteBucket()` function.\n\n\n### Using key/value pairs\n\nTo save a key/value pair to a bucket, use the `Bucket.Put()` function:\n\n```go\ndb.Update(func(tx *bolt.Tx) error {\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\terr := b.Put([]byte(\"answer\"), []byte(\"42\"))\n\treturn err\n})\n```\n\nThis will set the value of the `\"answer\"` key to `\"42\"` in the `MyBucket`\nbucket. To retrieve this value, we can use the `Bucket.Get()` function:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\tv := b.Get([]byte(\"answer\"))\n\tfmt.Printf(\"The answer is: %s\\n\", v)\n\treturn nil\n})\n```\n\nThe `Get()` function does not return an error because its operation is\nguaranteed to work (unless there is some kind of system failure). If the key\nexists then it will return its byte slice value. If it doesn't exist then it\nwill return `nil`. It's important to note that you can have a zero-length value\nset to a key which is different than the key not existing.\n\nUse the `Bucket.Delete()` function to delete a key from the bucket.\n\nPlease note that values returned from `Get()` are only valid while the\ntransaction is open. If you need to use a value outside of the transaction\nthen you must use `copy()` to copy it to another byte slice.\n\n\n### Autoincrementing integer for the bucket\nBy using the `NextSequence()` function, you can let Bolt determine a sequence\nwhich can be used as the unique identifier for your key/value pairs. See the\nexample below.\n\n```go\n// CreateUser saves u to the store. The new user ID is set on u once the data is persisted.\nfunc (s *Store) CreateUser(u *User) error {\n    return s.db.Update(func(tx *bolt.Tx) error {\n        // Retrieve the users bucket.\n        // This should be created when the DB is first opened.\n        b := tx.Bucket([]byte(\"users\"))\n\n        // Generate ID for the user.\n        // This returns an error only if the Tx is closed or not writeable.\n        // That can't happen in an Update() call so I ignore the error check.\n        id, _ := b.NextSequence()\n        u.ID = int(id)\n\n        // Marshal user data into bytes.\n        buf, err := json.Marshal(u)\n        if err != nil {\n            return err\n        }\n\n        // Persist bytes to users bucket.\n        return b.Put(itob(u.ID), buf)\n    })\n}\n\n// itob returns an 8-byte big endian representation of v.\nfunc itob(v int) []byte {\n    b := make([]byte, 8)\n    binary.BigEndian.PutUint64(b, uint64(v))\n    return b\n}\n\ntype User struct {\n    ID int\n    ...\n}\n```\n\n### Iterating over keys\n\nBolt stores its keys in byte-sorted order within a bucket. This makes sequential\niteration over these keys extremely fast. To iterate over keys we'll use a\n`Cursor`:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\n\tc := b.Cursor()\n\n\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\tfmt.Printf(\"key=%s, value=%s\\n\", k, v)\n\t}\n\n\treturn nil\n})\n```\n\nThe cursor allows you to move to a specific point in the list of keys and move\nforward or backward through the keys one at a time.\n\nThe following functions are available on the cursor:\n\n```\nFirst()  Move to the first key.\nLast()   Move to the last key.\nSeek()   Move to a specific key.\nNext()   Move to the next key.\nPrev()   Move to the previous key.\n```\n\nEach of those functions has a return signature of `(key []byte, value []byte)`.\nWhen you have iterated to the end of the cursor then `Next()` will return a\n`nil` key.  You must seek to a position using `First()`, `Last()`, or `Seek()`\nbefore calling `Next()` or `Prev()`. If you do not seek to a position then\nthese functions will return a `nil` key.\n\nDuring iteration, if the key is non-`nil` but the value is `nil`, that means\nthe key refers to a bucket rather than a value.  Use `Bucket.Bucket()` to\naccess the sub-bucket.\n\n\n#### Prefix scans\n\nTo iterate over a key prefix, you can combine `Seek()` and `bytes.HasPrefix()`:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tc := tx.Bucket([]byte(\"MyBucket\")).Cursor()\n\n\tprefix := []byte(\"1234\")\n\tfor k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {\n\t\tfmt.Printf(\"key=%s, value=%s\\n\", k, v)\n\t}\n\n\treturn nil\n})\n```\n\n#### Range scans\n\nAnother common use case is scanning over a range such as a time range. If you\nuse a sortable time encoding such as RFC3339 then you can query a specific\ndate range like this:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\t// Assume our events bucket exists and has RFC3339 encoded time keys.\n\tc := tx.Bucket([]byte(\"Events\")).Cursor()\n\n\t// Our time range spans the 90's decade.\n\tmin := []byte(\"1990-01-01T00:00:00Z\")\n\tmax := []byte(\"2000-01-01T00:00:00Z\")\n\n\t// Iterate over the 90's.\n\tfor k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {\n\t\tfmt.Printf(\"%s: %s\\n\", k, v)\n\t}\n\n\treturn nil\n})\n```\n\nNote 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.\n\n\n#### ForEach()\n\nYou can also use the function `ForEach()` if you know you'll be iterating over\nall the keys in a bucket:\n\n```go\ndb.View(func(tx *bolt.Tx) error {\n\t// Assume bucket exists and has keys\n\tb := tx.Bucket([]byte(\"MyBucket\"))\n\n\tb.ForEach(func(k, v []byte) error {\n\t\tfmt.Printf(\"key=%s, value=%s\\n\", k, v)\n\t\treturn nil\n\t})\n\treturn nil\n})\n```\n\nPlease note that keys and values in `ForEach()` are only valid while\nthe transaction is open. If you need to use a key or value outside of\nthe transaction, you must use `copy()` to copy it to another byte\nslice.\n\n### Nested buckets\n\nYou can also store a bucket in a key to create nested buckets. The API is the\nsame as the bucket management API on the `DB` object:\n\n```go\nfunc (*Bucket) CreateBucket(key []byte) (*Bucket, error)\nfunc (*Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error)\nfunc (*Bucket) DeleteBucket(key []byte) error\n```\n\nSay 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.\n\n```go\n\n// createUser creates a new user in the given account.\nfunc createUser(accountID int, u *User) error {\n    // Start the transaction.\n    tx, err := db.Begin(true)\n    if err != nil {\n        return err\n    }\n    defer tx.Rollback()\n\n    // Retrieve the root bucket for the account.\n    // Assume this has already been created when the account was set up.\n    root := tx.Bucket([]byte(strconv.FormatUint(accountID, 10)))\n\n    // Setup the users bucket.\n    bkt, err := root.CreateBucketIfNotExists([]byte(\"USERS\"))\n    if err != nil {\n        return err\n    }\n\n    // Generate an ID for the new user.\n    userID, err := bkt.NextSequence()\n    if err != nil {\n        return err\n    }\n    u.ID = userID\n\n    // Marshal and save the encoded user.\n    if buf, err := json.Marshal(u); err != nil {\n        return err\n    } else if err := bkt.Put([]byte(strconv.FormatUint(u.ID, 10)), buf); err != nil {\n        return err\n    }\n\n    // Commit the transaction.\n    if err := tx.Commit(); err != nil {\n        return err\n    }\n\n    return nil\n}\n\n```\n\n\n\n\n### Database backups\n\nBolt is a single file so it's easy to backup. You can use the `Tx.WriteTo()`\nfunction to write a consistent view of the database to a writer. If you call\nthis from a read-only transaction, it will perform a hot backup and not block\nyour other database reads and writes.\n\nBy default, it will use a regular file handle which will utilize the operating\nsystem's page cache. See the [`Tx`](https://godoc.org/github.com/boltdb/bolt#Tx)\ndocumentation for information about optimizing for larger-than-RAM datasets.\n\nOne common use case is to backup over HTTP so you can use tools like `cURL` to\ndo database backups:\n\n```go\nfunc BackupHandleFunc(w http.ResponseWriter, req *http.Request) {\n\terr := db.View(func(tx *bolt.Tx) error {\n\t\tw.Header().Set(\"Content-Type\", \"application/octet-stream\")\n\t\tw.Header().Set(\"Content-Disposition\", `attachment; filename=\"my.db\"`)\n\t\tw.Header().Set(\"Content-Length\", strconv.Itoa(int(tx.Size())))\n\t\t_, err := tx.WriteTo(w)\n\t\treturn err\n\t})\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t}\n}\n```\n\nThen you can backup using this command:\n\n```sh\n$ curl http://localhost/backup > my.db\n```\n\nOr you can open your browser to `http://localhost/backup` and it will download\nautomatically.\n\nIf you want to backup to another file you can use the `Tx.CopyFile()` helper\nfunction.\n\n\n### Statistics\n\nThe database keeps a running count of many of the internal operations it\nperforms so you can better understand what's going on. By grabbing a snapshot\nof these stats at two points in time we can see what operations were performed\nin that time range.\n\nFor example, we could start a goroutine to log stats every 10 seconds:\n\n```go\ngo func() {\n\t// Grab the initial stats.\n\tprev := db.Stats()\n\n\tfor {\n\t\t// Wait for 10s.\n\t\ttime.Sleep(10 * time.Second)\n\n\t\t// Grab the current stats and diff them.\n\t\tstats := db.Stats()\n\t\tdiff := stats.Sub(&prev)\n\n\t\t// Encode stats to JSON and print to STDERR.\n\t\tjson.NewEncoder(os.Stderr).Encode(diff)\n\n\t\t// Save stats for the next loop.\n\t\tprev = stats\n\t}\n}()\n```\n\nIt's also useful to pipe these stats to a service such as statsd for monitoring\nor to provide an HTTP endpoint that will perform a fixed-length sample.\n\n\n### Read-Only Mode\n\nSometimes it is useful to create a shared, read-only Bolt database. To this,\nset the `Options.ReadOnly` flag when opening your database. Read-only mode\nuses a shared lock to allow multiple processes to read from the database but\nit will block any processes from opening the database in read-write mode.\n\n```go\ndb, err := bolt.Open(\"my.db\", 0666, &bolt.Options{ReadOnly: true})\nif err != nil {\n\tlog.Fatal(err)\n}\n```\n\n### Mobile Use (iOS/Android)\n\nBolt is able to run on mobile devices by leveraging the binding feature of the\n[gomobile](https://github.com/golang/mobile) tool. Create a struct that will\ncontain your database logic and a reference to a `*bolt.DB` with a initializing\nconstructor that takes in a filepath where the database file will be stored.\nNeither Android nor iOS require extra permissions or cleanup from using this method.\n\n```go\nfunc NewBoltDB(filepath string) *BoltDB {\n\tdb, err := bolt.Open(filepath+\"/demo.db\", 0600, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\treturn &BoltDB{db}\n}\n\ntype BoltDB struct {\n\tdb *bolt.DB\n\t...\n}\n\nfunc (b *BoltDB) Path() string {\n\treturn b.db.Path()\n}\n\nfunc (b *BoltDB) Close() {\n\tb.db.Close()\n}\n```\n\nDatabase logic should be defined as methods on this wrapper struct.\n\nTo initialize this struct from the native language (both platforms now sync\ntheir local storage to the cloud. These snippets disable that functionality for the\ndatabase file):\n\n#### Android\n\n```java\nString path;\nif (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.LOLLIPOP){\n    path = getNoBackupFilesDir().getAbsolutePath();\n} else{\n    path = getFilesDir().getAbsolutePath();\n}\nBoltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path)\n```\n\n#### iOS\n\n```objc\n- (void)demo {\n    NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,\n                                                          NSUserDomainMask,\n                                                          YES) objectAtIndex:0];\n\tGoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path);\n\t[self addSkipBackupAttributeToItemAtPath:demo.path];\n\t//Some DB Logic would go here\n\t[demo close];\n}\n\n- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString\n{\n    NSURL* URL= [NSURL fileURLWithPath: filePathString];\n    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);\n\n    NSError *error = nil;\n    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]\n                                  forKey: NSURLIsExcludedFromBackupKey error: &error];\n    if(!success){\n        NSLog(@\"Error excluding %@ from backup %@\", [URL lastPathComponent], error);\n    }\n    return success;\n}\n\n```\n\n## Resources\n\nFor more information on getting started with Bolt, check out the following articles:\n\n* [Intro to BoltDB: Painless Performant Persistence](http://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/) by [Nate Finch](https://github.com/natefinch).\n* [Bolt -- an embedded key/value database for Go](https://www.progville.com/go/bolt-embedded-db-golang/) by Progville\n\n\n## Comparison with other databases\n\n### Postgres, MySQL, & other relational databases\n\nRelational databases structure data into rows and are only accessible through\nthe use of SQL. This approach provides flexibility in how you store and query\nyour data but also incurs overhead in parsing and planning SQL statements. Bolt\naccesses all data by a byte slice key. This makes Bolt fast to read and write\ndata by key but provides no built-in support for joining values together.\n\nMost relational databases (with the exception of SQLite) are standalone servers\nthat run separately from your application. This gives your systems\nflexibility to connect multiple application servers to a single database\nserver but also adds overhead in serializing and transporting data over the\nnetwork. Bolt runs as a library included in your application so all data access\nhas to go through your application's process. This brings data closer to your\napplication but limits multi-process access to the data.\n\n\n### LevelDB, RocksDB\n\nLevelDB and its derivatives (RocksDB, HyperLevelDB) are similar to Bolt in that\nthey are libraries bundled into the application, however, their underlying\nstructure is a log-structured merge-tree (LSM tree). An LSM tree optimizes\nrandom writes by using a write ahead log and multi-tiered, sorted files called\nSSTables. Bolt uses a B+tree internally and only a single file. Both approaches\nhave trade-offs.\n\nIf you require a high random write throughput (>10,000 w/sec) or you need to use\nspinning disks then LevelDB could be a good choice. If your application is\nread-heavy or does a lot of range scans then Bolt could be a good choice.\n\nOne other important consideration is that LevelDB does not have transactions.\nIt supports batch writing of key/values pairs and it supports read snapshots\nbut it will not give you the ability to do a compare-and-swap operation safely.\nBolt supports fully serializable ACID transactions.\n\n\n### LMDB\n\nBolt was originally a port of LMDB so it is architecturally similar. Both use\na B+tree, have ACID semantics with fully serializable transactions, and support\nlock-free MVCC using a single writer and multiple readers.\n\nThe two projects have somewhat diverged. LMDB heavily focuses on raw performance\nwhile Bolt has focused on simplicity and ease of use. For example, LMDB allows\nseveral unsafe actions such as direct writes for the sake of performance. Bolt\nopts to disallow actions which can leave the database in a corrupted state. The\nonly exception to this in Bolt is `DB.NoSync`.\n\nThere are also a few differences in API. LMDB requires a maximum mmap size when\nopening an `mdb_env` whereas Bolt will handle incremental mmap resizing\nautomatically. LMDB overloads the getter and setter functions with multiple\nflags whereas Bolt splits these specialized cases into their own functions.\n\n\n## Caveats & Limitations\n\nIt's important to pick the right tool for the job and Bolt is no exception.\nHere are a few things to note when evaluating and using Bolt:\n\n* Bolt is good for read intensive workloads. Sequential write performance is\n  also fast but random writes can be slow. You can use `DB.Batch()` or add a\n  write-ahead log to help mitigate this issue.\n\n* Bolt uses a B+tree internally so there can be a lot of random page access.\n  SSDs provide a significant performance boost over spinning disks.\n\n* Try to avoid long running read transactions. Bolt uses copy-on-write so\n  old pages cannot be reclaimed while an old transaction is using them.\n\n* Byte slices returned from Bolt are only valid during a transaction. Once the\n  transaction has been committed or rolled back then the memory they point to\n  can be reused by a new page or can be unmapped from virtual memory and you'll\n  see an `unexpected fault address` panic when accessing it.\n\n* Bolt uses an exclusive write lock on the database file so it cannot be\n  shared by multiple processes.\n\n* Be careful when using `Bucket.FillPercent`. Setting a high fill percent for\n  buckets that have random inserts will cause your database to have very poor\n  page utilization.\n\n* Use larger buckets in general. Smaller buckets causes poor page utilization\n  once they become larger than the page size (typically 4KB).\n\n* Bulk loading a lot of random writes into a new bucket can be slow as the\n  page will not split until the transaction is committed. Randomly inserting\n  more than 100,000 key/value pairs into a single new bucket in a single\n  transaction is not advised.\n\n* Bolt uses a memory-mapped file so the underlying operating system handles the\n  caching of the data. Typically, the OS will cache as much of the file as it\n  can in memory and will release memory as needed to other processes. This means\n  that Bolt can show very high memory usage when working with large databases.\n  However, this is expected and the OS will release memory as needed. Bolt can\n  handle databases much larger than the available physical RAM, provided its\n  memory-map fits in the process virtual address space. It may be problematic\n  on 32-bits systems.\n\n* The data structures in the Bolt database are memory mapped so the data file\n  will be endian specific. This means that you cannot copy a Bolt file from a\n  little endian machine to a big endian machine and have it work. For most\n  users this is not a concern since most modern CPUs are little endian.\n\n* Because of the way pages are laid out on disk, Bolt cannot truncate data files\n  and return free pages back to the disk. Instead, Bolt maintains a free list\n  of unused pages within its data file. These free pages can be reused by later\n  transactions. This works well for many use cases as databases generally tend\n  to grow. However, it's important to note that deleting large chunks of data\n  will not allow you to reclaim that space on disk.\n\n  For more information on page allocation, [see this comment][page-allocation].\n\n[page-allocation]: https://github.com/boltdb/bolt/issues/308#issuecomment-74811638\n\n\n## Reading the Source\n\nBolt is a relatively small code base (<3KLOC) for an embedded, serializable,\ntransactional key/value database so it can be a good starting point for people\ninterested in how databases work.\n\nThe best places to start are the main entry points into Bolt:\n\n- `Open()` - Initializes the reference to the database. It's responsible for\n  creating the database if it doesn't exist, obtaining an exclusive lock on the\n  file, reading the meta pages, & memory-mapping the file.\n\n- `DB.Begin()` - Starts a read-only or read-write transaction depending on the\n  value of the `writable` argument. This requires briefly obtaining the \"meta\"\n  lock to keep track of open transactions. Only one read-write transaction can\n  exist at a time so the \"rwlock\" is acquired during the life of a read-write\n  transaction.\n\n- `Bucket.Put()` - Writes a key/value pair into a bucket. After validating the\n  arguments, a cursor is used to traverse the B+tree to the page and position\n  where they key & value will be written. Once the position is found, the bucket\n  materializes the underlying page and the page's parent pages into memory as\n  \"nodes\". These nodes are where mutations occur during read-write transactions.\n  These changes get flushed to disk during commit.\n\n- `Bucket.Get()` - Retrieves a key/value pair from a bucket. This uses a cursor\n  to move to the page & position of a key/value pair. During a read-only\n  transaction, the key and value data is returned as a direct reference to the\n  underlying mmap file so there's no allocation overhead. For read-write\n  transactions, this data may reference the mmap file or one of the in-memory\n  node values.\n\n- `Cursor` - This object is simply for traversing the B+tree of on-disk pages\n  or in-memory nodes. It can seek to a specific key, move to the first or last\n  value, or it can move forward or backward. The cursor handles the movement up\n  and down the B+tree transparently to the end user.\n\n- `Tx.Commit()` - Converts the in-memory dirty nodes and the list of free pages\n  into pages to be written to disk. Writing to disk then occurs in two phases.\n  First, the dirty pages are written to disk and an `fsync()` occurs. Second, a\n  new meta page with an incremented transaction ID is written and another\n  `fsync()` occurs. This two phase write ensures that partially written data\n  pages are ignored in the event of a crash since the meta page pointing to them\n  is never written. Partially written meta pages are invalidated because they\n  are written with a checksum.\n\nIf you have additional notes that could be helpful for others, please submit\nthem via pull request.\n\n\n## Other Projects Using Bolt\n\nBelow is a list of public, open source projects that use Bolt:\n\n* [BoltDbWeb](https://github.com/evnix/boltdbweb) - A web based GUI for BoltDB files.\n* [Operation Go: A Routine Mission](http://gocode.io) - An online programming game for Golang using Bolt for user accounts and a leaderboard.\n* [Bazil](https://bazil.org/) - A file system that lets your data reside where it is most convenient for it to reside.\n* [DVID](https://github.com/janelia-flyem/dvid) - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.\n* [Skybox Analytics](https://github.com/skybox/skybox) - A standalone funnel analysis tool for web analytics.\n* [Scuttlebutt](https://github.com/benbjohnson/scuttlebutt) - Uses Bolt to store and process all Twitter mentions of GitHub projects.\n* [Wiki](https://github.com/peterhellberg/wiki) - A tiny wiki using Goji, BoltDB and Blackfriday.\n* [ChainStore](https://github.com/pressly/chainstore) - Simple key-value interface to a variety of storage engines organized as a chain of operations.\n* [MetricBase](https://github.com/msiebuhr/MetricBase) - Single-binary version of Graphite.\n* [Gitchain](https://github.com/gitchain/gitchain) - Decentralized, peer-to-peer Git repositories aka \"Git meets Bitcoin\".\n* [event-shuttle](https://github.com/sclasen/event-shuttle) - A Unix system service to collect and reliably deliver messages to Kafka.\n* [ipxed](https://github.com/kelseyhightower/ipxed) - Web interface and api for ipxed.\n* [BoltStore](https://github.com/yosssi/boltstore) - Session store using Bolt.\n* [photosite/session](https://godoc.org/bitbucket.org/kardianos/photosite/session) - Sessions for a photo viewing site.\n* [LedisDB](https://github.com/siddontang/ledisdb) - A high performance NoSQL, using Bolt as optional storage.\n* [ipLocator](https://github.com/AndreasBriese/ipLocator) - A fast ip-geo-location-server using bolt with bloom filters.\n* [cayley](https://github.com/google/cayley) - Cayley is an open-source graph database using Bolt as optional backend.\n* [bleve](http://www.blevesearch.com/) - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.\n* [tentacool](https://github.com/optiflows/tentacool) - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.\n* [Seaweed File System](https://github.com/chrislusf/seaweedfs) - Highly scalable distributed key~file system with O(1) disk read.\n* [InfluxDB](https://influxdata.com) - Scalable datastore for metrics, events, and real-time analytics.\n* [Freehold](http://tshannon.bitbucket.org/freehold/) - An open, secure, and lightweight platform for your files and data.\n* [Prometheus Annotation Server](https://github.com/oliver006/prom_annotation_server) - Annotation server for PromDash & Prometheus service monitoring system.\n* [Consul](https://github.com/hashicorp/consul) - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.\n* [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.\n* [drive](https://github.com/odeke-em/drive) - drive is an unofficial Google Drive command line client for \\*NIX operating systems.\n* [stow](https://github.com/djherbis/stow) -  a persistence manager for objects\n  backed by boltdb.\n* [buckets](https://github.com/joyrexus/buckets) - a bolt wrapper streamlining\n  simple tx and key scans.\n* [mbuckets](https://github.com/abhigupta912/mbuckets) - A Bolt wrapper that allows easy operations on multi level (nested) buckets.\n* [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\n* [Go Report Card](https://goreportcard.com/) - Go code quality report cards as a (free and open source) service.\n* [Boltdb Boilerplate](https://github.com/bobintornado/boltdb-boilerplate) - Boilerplate wrapper around bolt aiming to make simple calls one-liners.\n* [lru](https://github.com/crowdriff/lru) - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores.\n* [Storm](https://github.com/asdine/storm) - Simple and powerful ORM for BoltDB.\n* [GoWebApp](https://github.com/josephspurrier/gowebapp) - A basic MVC web application in Go using BoltDB.\n* [SimpleBolt](https://github.com/xyproto/simplebolt) - A simple way to use BoltDB. Deals mainly with strings.\n* [Algernon](https://github.com/xyproto/algernon) - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend.\n* [MuLiFS](https://github.com/dankomiocevic/mulifs) - Music Library Filesystem creates a filesystem to organise your music files.\n* [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.\n* [torrent](https://github.com/anacrolix/torrent) - Full-featured BitTorrent client package and utilities in Go. BoltDB is a storage backend in development.\n* [gopherpit](https://github.com/gopherpit/gopherpit) - A web service to manage Go remote import paths with custom domains\n* [bolter](https://github.com/hasit/bolter) - Command-line app for viewing BoltDB file in your terminal.\n* [btcwallet](https://github.com/btcsuite/btcwallet) - A bitcoin wallet.\n* [dcrwallet](https://github.com/decred/dcrwallet) - A wallet for the Decred cryptocurrency.\n* [Ironsmith](https://github.com/timshannon/ironsmith) - A simple, script-driven continuous integration (build - > test -> release) tool, with no external dependencies\n* [BoltHold](https://github.com/timshannon/bolthold) - An embeddable NoSQL store for Go types built on BoltDB\n* [Ponzu CMS](https://ponzu-cms.org) - Headless CMS + automatic JSON API with auto-HTTPS, HTTP/2 Server Push, and flexible server framework.\n\nIf you are using Bolt in a project please send a pull request to add it to the list.\n"
  },
  {
    "path": "appveyor.yml",
    "content": "version: \"{build}\"\n\nos: Windows Server 2012 R2\n\nclone_folder: c:\\gopath\\src\\github.com\\boltdb\\bolt\n\nenvironment:\n  GOPATH: c:\\gopath\n\ninstall:\n  - echo %PATH%\n  - echo %GOPATH%\n  - go version\n  - go env\n  - go get -v -t ./...\n\nbuild_script:\n  - go test -v ./...\n"
  },
  {
    "path": "bolt_386.go",
    "content": "package bolt\n\n// maxMapSize represents the largest mmap size supported by Bolt.\nconst maxMapSize = 0x7FFFFFFF // 2GB\n\n// maxAllocSize is the size used when creating array pointers.\nconst maxAllocSize = 0xFFFFFFF\n\n// Are unaligned load/stores broken on this arch?\nvar brokenUnaligned = false\n"
  },
  {
    "path": "bolt_amd64.go",
    "content": "package bolt\n\n// maxMapSize represents the largest mmap size supported by Bolt.\nconst maxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// maxAllocSize is the size used when creating array pointers.\nconst maxAllocSize = 0x7FFFFFFF\n\n// Are unaligned load/stores broken on this arch?\nvar brokenUnaligned = false\n"
  },
  {
    "path": "bolt_arm.go",
    "content": "package bolt\n\nimport \"unsafe\"\n\n// maxMapSize represents the largest mmap size supported by Bolt.\nconst maxMapSize = 0x7FFFFFFF // 2GB\n\n// maxAllocSize is the size used when creating array pointers.\nconst maxAllocSize = 0xFFFFFFF\n\n// Are unaligned load/stores broken on this arch?\nvar brokenUnaligned bool\n\nfunc init() {\n\t// Simple check to see whether this arch handles unaligned load/stores\n\t// correctly.\n\n\t// ARM9 and older devices require load/stores to be from/to aligned\n\t// addresses. If not, the lower 2 bits are cleared and that address is\n\t// read in a jumbled up order.\n\n\t// See http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html\n\n\traw := [6]byte{0xfe, 0xef, 0x11, 0x22, 0x22, 0x11}\n\tval := *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(&raw)) + 2))\n\n\tbrokenUnaligned = val != 0x11222211\n}\n"
  },
  {
    "path": "bolt_arm64.go",
    "content": "// +build arm64\n\npackage bolt\n\n// maxMapSize represents the largest mmap size supported by Bolt.\nconst maxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// maxAllocSize is the size used when creating array pointers.\nconst maxAllocSize = 0x7FFFFFFF\n\n// Are unaligned load/stores broken on this arch?\nvar brokenUnaligned = false\n"
  },
  {
    "path": "bolt_linux.go",
    "content": "package bolt\n\nimport (\n\t\"syscall\"\n)\n\n// fdatasync flushes written data to a file descriptor.\nfunc fdatasync(db *DB) error {\n\treturn syscall.Fdatasync(int(db.file.Fd()))\n}\n"
  },
  {
    "path": "bolt_openbsd.go",
    "content": "package bolt\n\nimport (\n\t\"syscall\"\n\t\"unsafe\"\n)\n\nconst (\n\tmsAsync      = 1 << iota // perform asynchronous writes\n\tmsSync                   // perform synchronous writes\n\tmsInvalidate             // invalidate cached data\n)\n\nfunc msync(db *DB) error {\n\t_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(db.data)), uintptr(db.datasz), msInvalidate)\n\tif errno != 0 {\n\t\treturn errno\n\t}\n\treturn nil\n}\n\nfunc fdatasync(db *DB) error {\n\tif db.data != nil {\n\t\treturn msync(db)\n\t}\n\treturn db.file.Sync()\n}\n"
  },
  {
    "path": "bolt_ppc.go",
    "content": "// +build ppc\n\npackage bolt\n\n// maxMapSize represents the largest mmap size supported by Bolt.\nconst maxMapSize = 0x7FFFFFFF // 2GB\n\n// maxAllocSize is the size used when creating array pointers.\nconst maxAllocSize = 0xFFFFFFF\n"
  },
  {
    "path": "bolt_ppc64.go",
    "content": "// +build ppc64\n\npackage bolt\n\n// maxMapSize represents the largest mmap size supported by Bolt.\nconst maxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// maxAllocSize is the size used when creating array pointers.\nconst maxAllocSize = 0x7FFFFFFF\n\n// Are unaligned load/stores broken on this arch?\nvar brokenUnaligned = false\n"
  },
  {
    "path": "bolt_ppc64le.go",
    "content": "// +build ppc64le\n\npackage bolt\n\n// maxMapSize represents the largest mmap size supported by Bolt.\nconst maxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// maxAllocSize is the size used when creating array pointers.\nconst maxAllocSize = 0x7FFFFFFF\n\n// Are unaligned load/stores broken on this arch?\nvar brokenUnaligned = false\n"
  },
  {
    "path": "bolt_s390x.go",
    "content": "// +build s390x\n\npackage bolt\n\n// maxMapSize represents the largest mmap size supported by Bolt.\nconst maxMapSize = 0xFFFFFFFFFFFF // 256TB\n\n// maxAllocSize is the size used when creating array pointers.\nconst maxAllocSize = 0x7FFFFFFF\n\n// Are unaligned load/stores broken on this arch?\nvar brokenUnaligned = false\n"
  },
  {
    "path": "bolt_unix.go",
    "content": "// +build !windows,!plan9,!solaris\n\npackage bolt\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"syscall\"\n\t\"time\"\n\t\"unsafe\"\n)\n\n// flock acquires an advisory lock on a file descriptor.\nfunc flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {\n\tvar t time.Time\n\tfor {\n\t\t// If we're beyond our timeout then return an error.\n\t\t// This can only occur after we've attempted a flock once.\n\t\tif t.IsZero() {\n\t\t\tt = time.Now()\n\t\t} else if timeout > 0 && time.Since(t) > timeout {\n\t\t\treturn ErrTimeout\n\t\t}\n\t\tflag := syscall.LOCK_SH\n\t\tif exclusive {\n\t\t\tflag = syscall.LOCK_EX\n\t\t}\n\n\t\t// Otherwise attempt to obtain an exclusive lock.\n\t\terr := syscall.Flock(int(db.file.Fd()), flag|syscall.LOCK_NB)\n\t\tif err == nil {\n\t\t\treturn nil\n\t\t} else if err != syscall.EWOULDBLOCK {\n\t\t\treturn err\n\t\t}\n\n\t\t// Wait for a bit and try again.\n\t\ttime.Sleep(50 * time.Millisecond)\n\t}\n}\n\n// funlock releases an advisory lock on a file descriptor.\nfunc funlock(db *DB) error {\n\treturn syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)\n}\n\n// mmap memory maps a DB's data file.\nfunc mmap(db *DB, sz int) error {\n\t// Map the data file to memory.\n\tb, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Advise the kernel that the mmap is accessed randomly.\n\tif err := madvise(b, syscall.MADV_RANDOM); err != nil {\n\t\treturn fmt.Errorf(\"madvise: %s\", err)\n\t}\n\n\t// Save the original byte slice and convert to a byte array pointer.\n\tdb.dataref = b\n\tdb.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))\n\tdb.datasz = sz\n\treturn nil\n}\n\n// munmap unmaps a DB's data file from memory.\nfunc munmap(db *DB) error {\n\t// Ignore the unmap if we have no mapped data.\n\tif db.dataref == nil {\n\t\treturn nil\n\t}\n\n\t// Unmap using the original byte slice.\n\terr := syscall.Munmap(db.dataref)\n\tdb.dataref = nil\n\tdb.data = nil\n\tdb.datasz = 0\n\treturn err\n}\n\n// NOTE: This function is copied from stdlib because it is not available on darwin.\nfunc madvise(b []byte, advice int) (err error) {\n\t_, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))\n\tif e1 != 0 {\n\t\terr = e1\n\t}\n\treturn\n}\n"
  },
  {
    "path": "bolt_unix_solaris.go",
    "content": "package bolt\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"syscall\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"golang.org/x/sys/unix\"\n)\n\n// flock acquires an advisory lock on a file descriptor.\nfunc flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {\n\tvar t time.Time\n\tfor {\n\t\t// If we're beyond our timeout then return an error.\n\t\t// This can only occur after we've attempted a flock once.\n\t\tif t.IsZero() {\n\t\t\tt = time.Now()\n\t\t} else if timeout > 0 && time.Since(t) > timeout {\n\t\t\treturn ErrTimeout\n\t\t}\n\t\tvar lock syscall.Flock_t\n\t\tlock.Start = 0\n\t\tlock.Len = 0\n\t\tlock.Pid = 0\n\t\tlock.Whence = 0\n\t\tlock.Pid = 0\n\t\tif exclusive {\n\t\t\tlock.Type = syscall.F_WRLCK\n\t\t} else {\n\t\t\tlock.Type = syscall.F_RDLCK\n\t\t}\n\t\terr := syscall.FcntlFlock(db.file.Fd(), syscall.F_SETLK, &lock)\n\t\tif err == nil {\n\t\t\treturn nil\n\t\t} else if err != syscall.EAGAIN {\n\t\t\treturn err\n\t\t}\n\n\t\t// Wait for a bit and try again.\n\t\ttime.Sleep(50 * time.Millisecond)\n\t}\n}\n\n// funlock releases an advisory lock on a file descriptor.\nfunc funlock(db *DB) error {\n\tvar lock syscall.Flock_t\n\tlock.Start = 0\n\tlock.Len = 0\n\tlock.Type = syscall.F_UNLCK\n\tlock.Whence = 0\n\treturn syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)\n}\n\n// mmap memory maps a DB's data file.\nfunc mmap(db *DB, sz int) error {\n\t// Map the data file to memory.\n\tb, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Advise the kernel that the mmap is accessed randomly.\n\tif err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {\n\t\treturn fmt.Errorf(\"madvise: %s\", err)\n\t}\n\n\t// Save the original byte slice and convert to a byte array pointer.\n\tdb.dataref = b\n\tdb.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))\n\tdb.datasz = sz\n\treturn nil\n}\n\n// munmap unmaps a DB's data file from memory.\nfunc munmap(db *DB) error {\n\t// Ignore the unmap if we have no mapped data.\n\tif db.dataref == nil {\n\t\treturn nil\n\t}\n\n\t// Unmap using the original byte slice.\n\terr := unix.Munmap(db.dataref)\n\tdb.dataref = nil\n\tdb.data = nil\n\tdb.datasz = 0\n\treturn err\n}\n"
  },
  {
    "path": "bolt_windows.go",
    "content": "package bolt\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"syscall\"\n\t\"time\"\n\t\"unsafe\"\n)\n\n// LockFileEx code derived from golang build filemutex_windows.go @ v1.5.1\nvar (\n\tmodkernel32      = syscall.NewLazyDLL(\"kernel32.dll\")\n\tprocLockFileEx   = modkernel32.NewProc(\"LockFileEx\")\n\tprocUnlockFileEx = modkernel32.NewProc(\"UnlockFileEx\")\n)\n\nconst (\n\tlockExt = \".lock\"\n\n\t// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx\n\tflagLockExclusive       = 2\n\tflagLockFailImmediately = 1\n\n\t// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx\n\terrLockViolation syscall.Errno = 0x21\n)\n\nfunc lockFileEx(h syscall.Handle, flags, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {\n\tr, _, err := procLockFileEx.Call(uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))\n\tif r == 0 {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc unlockFileEx(h syscall.Handle, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {\n\tr, _, err := procUnlockFileEx.Call(uintptr(h), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)), 0)\n\tif r == 0 {\n\t\treturn err\n\t}\n\treturn nil\n}\n\n// fdatasync flushes written data to a file descriptor.\nfunc fdatasync(db *DB) error {\n\treturn db.file.Sync()\n}\n\n// flock acquires an advisory lock on a file descriptor.\nfunc flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {\n\t// Create a separate lock file on windows because a process\n\t// cannot share an exclusive lock on the same file. This is\n\t// needed during Tx.WriteTo().\n\tf, err := os.OpenFile(db.path+lockExt, os.O_CREATE, mode)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdb.lockfile = f\n\n\tvar t time.Time\n\tfor {\n\t\t// If we're beyond our timeout then return an error.\n\t\t// This can only occur after we've attempted a flock once.\n\t\tif t.IsZero() {\n\t\t\tt = time.Now()\n\t\t} else if timeout > 0 && time.Since(t) > timeout {\n\t\t\treturn ErrTimeout\n\t\t}\n\n\t\tvar flag uint32 = flagLockFailImmediately\n\t\tif exclusive {\n\t\t\tflag |= flagLockExclusive\n\t\t}\n\n\t\terr := lockFileEx(syscall.Handle(db.lockfile.Fd()), flag, 0, 1, 0, &syscall.Overlapped{})\n\t\tif err == nil {\n\t\t\treturn nil\n\t\t} else if err != errLockViolation {\n\t\t\treturn err\n\t\t}\n\n\t\t// Wait for a bit and try again.\n\t\ttime.Sleep(50 * time.Millisecond)\n\t}\n}\n\n// funlock releases an advisory lock on a file descriptor.\nfunc funlock(db *DB) error {\n\terr := unlockFileEx(syscall.Handle(db.lockfile.Fd()), 0, 1, 0, &syscall.Overlapped{})\n\tdb.lockfile.Close()\n\tos.Remove(db.path + lockExt)\n\treturn err\n}\n\n// mmap memory maps a DB's data file.\n// Based on: https://github.com/edsrzf/mmap-go\nfunc mmap(db *DB, sz int) error {\n\tif !db.readOnly {\n\t\t// Truncate the database to the size of the mmap.\n\t\tif err := db.file.Truncate(int64(sz)); err != nil {\n\t\t\treturn fmt.Errorf(\"truncate: %s\", err)\n\t\t}\n\t}\n\n\t// Open a file mapping handle.\n\tsizelo := uint32(sz >> 32)\n\tsizehi := uint32(sz) & 0xffffffff\n\th, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)\n\tif h == 0 {\n\t\treturn os.NewSyscallError(\"CreateFileMapping\", errno)\n\t}\n\n\t// Create the memory map.\n\taddr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))\n\tif addr == 0 {\n\t\treturn os.NewSyscallError(\"MapViewOfFile\", errno)\n\t}\n\n\t// Close mapping handle.\n\tif err := syscall.CloseHandle(syscall.Handle(h)); err != nil {\n\t\treturn os.NewSyscallError(\"CloseHandle\", err)\n\t}\n\n\t// Convert to a byte array.\n\tdb.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))\n\tdb.datasz = sz\n\n\treturn nil\n}\n\n// munmap unmaps a pointer from a file.\n// Based on: https://github.com/edsrzf/mmap-go\nfunc munmap(db *DB) error {\n\tif db.data == nil {\n\t\treturn nil\n\t}\n\n\taddr := (uintptr)(unsafe.Pointer(&db.data[0]))\n\tif err := syscall.UnmapViewOfFile(addr); err != nil {\n\t\treturn os.NewSyscallError(\"UnmapViewOfFile\", err)\n\t}\n\treturn nil\n}\n"
  },
  {
    "path": "boltsync_unix.go",
    "content": "// +build !windows,!plan9,!linux,!openbsd\n\npackage bolt\n\n// fdatasync flushes written data to a file descriptor.\nfunc fdatasync(db *DB) error {\n\treturn db.file.Sync()\n}\n"
  },
  {
    "path": "bucket.go",
    "content": "package bolt\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"unsafe\"\n)\n\nconst (\n\t// MaxKeySize is the maximum length of a key, in bytes.\n\tMaxKeySize = 32768\n\n\t// MaxValueSize is the maximum length of a value, in bytes.\n\tMaxValueSize = (1 << 31) - 2\n)\n\nconst (\n\tmaxUint = ^uint(0)\n\tminUint = 0\n\tmaxInt  = int(^uint(0) >> 1)\n\tminInt  = -maxInt - 1\n)\n\nconst bucketHeaderSize = int(unsafe.Sizeof(bucket{}))\n\nconst (\n\tminFillPercent = 0.1\n\tmaxFillPercent = 1.0\n)\n\n// DefaultFillPercent is the percentage that split pages are filled.\n// This value can be changed by setting Bucket.FillPercent.\nconst DefaultFillPercent = 0.5\n\n// Bucket represents a collection of key/value pairs inside the database.\ntype Bucket struct {\n\t*bucket\n\ttx       *Tx                // the associated transaction\n\tbuckets  map[string]*Bucket // subbucket cache\n\tpage     *page              // inline page reference\n\trootNode *node              // materialized node for the root page.\n\tnodes    map[pgid]*node     // node cache\n\n\t// Sets the threshold for filling nodes when they split. By default,\n\t// the bucket will fill to 50% but it can be useful to increase this\n\t// amount if you know that your write workloads are mostly append-only.\n\t//\n\t// This is non-persisted across transactions so it must be set in every Tx.\n\tFillPercent float64\n}\n\n// bucket represents the on-file representation of a bucket.\n// This is stored as the \"value\" of a bucket key. If the bucket is small enough,\n// then its root page can be stored inline in the \"value\", after the bucket\n// header. In the case of inline buckets, the \"root\" will be 0.\ntype bucket struct {\n\troot     pgid   // page id of the bucket's root-level page\n\tsequence uint64 // monotonically incrementing, used by NextSequence()\n}\n\n// newBucket returns a new bucket associated with a transaction.\nfunc newBucket(tx *Tx) Bucket {\n\tvar b = Bucket{tx: tx, FillPercent: DefaultFillPercent}\n\tif tx.writable {\n\t\tb.buckets = make(map[string]*Bucket)\n\t\tb.nodes = make(map[pgid]*node)\n\t}\n\treturn b\n}\n\n// Tx returns the tx of the bucket.\nfunc (b *Bucket) Tx() *Tx {\n\treturn b.tx\n}\n\n// Root returns the root of the bucket.\nfunc (b *Bucket) Root() pgid {\n\treturn b.root\n}\n\n// Writable returns whether the bucket is writable.\nfunc (b *Bucket) Writable() bool {\n\treturn b.tx.writable\n}\n\n// Cursor creates a cursor associated with the bucket.\n// The cursor is only valid as long as the transaction is open.\n// Do not use a cursor after the transaction is closed.\nfunc (b *Bucket) Cursor() *Cursor {\n\t// Update transaction statistics.\n\tb.tx.stats.CursorCount++\n\n\t// Allocate and return a cursor.\n\treturn &Cursor{\n\t\tbucket: b,\n\t\tstack:  make([]elemRef, 0),\n\t}\n}\n\n// Bucket retrieves a nested bucket by name.\n// Returns nil if the bucket does not exist.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (b *Bucket) Bucket(name []byte) *Bucket {\n\tif b.buckets != nil {\n\t\tif child := b.buckets[string(name)]; child != nil {\n\t\t\treturn child\n\t\t}\n\t}\n\n\t// Move cursor to key.\n\tc := b.Cursor()\n\tk, v, flags := c.seek(name)\n\n\t// Return nil if the key doesn't exist or it is not a bucket.\n\tif !bytes.Equal(name, k) || (flags&bucketLeafFlag) == 0 {\n\t\treturn nil\n\t}\n\n\t// Otherwise create a bucket and cache it.\n\tvar child = b.openBucket(v)\n\tif b.buckets != nil {\n\t\tb.buckets[string(name)] = child\n\t}\n\n\treturn child\n}\n\n// Helper method that re-interprets a sub-bucket value\n// from a parent into a Bucket\nfunc (b *Bucket) openBucket(value []byte) *Bucket {\n\tvar child = newBucket(b.tx)\n\n\t// If unaligned load/stores are broken on this arch and value is\n\t// unaligned simply clone to an aligned byte array.\n\tunaligned := brokenUnaligned && uintptr(unsafe.Pointer(&value[0]))&3 != 0\n\n\tif unaligned {\n\t\tvalue = cloneBytes(value)\n\t}\n\n\t// If this is a writable transaction then we need to copy the bucket entry.\n\t// Read-only transactions can point directly at the mmap entry.\n\tif b.tx.writable && !unaligned {\n\t\tchild.bucket = &bucket{}\n\t\t*child.bucket = *(*bucket)(unsafe.Pointer(&value[0]))\n\t} else {\n\t\tchild.bucket = (*bucket)(unsafe.Pointer(&value[0]))\n\t}\n\n\t// Save a reference to the inline page if the bucket is inline.\n\tif child.root == 0 {\n\t\tchild.page = (*page)(unsafe.Pointer(&value[bucketHeaderSize]))\n\t}\n\n\treturn &child\n}\n\n// CreateBucket creates a new bucket at the given key and returns the new bucket.\n// Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {\n\tif b.tx.db == nil {\n\t\treturn nil, ErrTxClosed\n\t} else if !b.tx.writable {\n\t\treturn nil, ErrTxNotWritable\n\t} else if len(key) == 0 {\n\t\treturn nil, ErrBucketNameRequired\n\t}\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, _, flags := c.seek(key)\n\n\t// Return an error if there is an existing key.\n\tif bytes.Equal(key, k) {\n\t\tif (flags & bucketLeafFlag) != 0 {\n\t\t\treturn nil, ErrBucketExists\n\t\t}\n\t\treturn nil, ErrIncompatibleValue\n\t}\n\n\t// Create empty, inline bucket.\n\tvar bucket = Bucket{\n\t\tbucket:      &bucket{},\n\t\trootNode:    &node{isLeaf: true},\n\t\tFillPercent: DefaultFillPercent,\n\t}\n\tvar value = bucket.write()\n\n\t// Insert into node.\n\tkey = cloneBytes(key)\n\tc.node().put(key, key, value, 0, bucketLeafFlag)\n\n\t// Since subbuckets are not allowed on inline buckets, we need to\n\t// dereference the inline page, if it exists. This will cause the bucket\n\t// to be treated as a regular, non-inline bucket for the rest of the tx.\n\tb.page = nil\n\n\treturn b.Bucket(key), nil\n}\n\n// CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.\n// Returns an error if the bucket name is blank, or if the bucket name is too long.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {\n\tchild, err := b.CreateBucket(key)\n\tif err == ErrBucketExists {\n\t\treturn b.Bucket(key), nil\n\t} else if err != nil {\n\t\treturn nil, err\n\t}\n\treturn child, nil\n}\n\n// DeleteBucket deletes a bucket at the given key.\n// Returns an error if the bucket does not exists, or if the key represents a non-bucket value.\nfunc (b *Bucket) DeleteBucket(key []byte) error {\n\tif b.tx.db == nil {\n\t\treturn ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn ErrTxNotWritable\n\t}\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, _, flags := c.seek(key)\n\n\t// Return an error if bucket doesn't exist or is not a bucket.\n\tif !bytes.Equal(key, k) {\n\t\treturn ErrBucketNotFound\n\t} else if (flags & bucketLeafFlag) == 0 {\n\t\treturn ErrIncompatibleValue\n\t}\n\n\t// Recursively delete all child buckets.\n\tchild := b.Bucket(key)\n\terr := child.ForEach(func(k, v []byte) error {\n\t\tif v == nil {\n\t\t\tif err := child.DeleteBucket(k); err != nil {\n\t\t\t\treturn fmt.Errorf(\"delete bucket: %s\", err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Remove cached copy.\n\tdelete(b.buckets, string(key))\n\n\t// Release all bucket pages to freelist.\n\tchild.nodes = nil\n\tchild.rootNode = nil\n\tchild.free()\n\n\t// Delete the node if we have a matching key.\n\tc.node().del(key)\n\n\treturn nil\n}\n\n// Get retrieves the value for a key in the bucket.\n// Returns a nil value if the key does not exist or if the key is a nested bucket.\n// The returned value is only valid for the life of the transaction.\nfunc (b *Bucket) Get(key []byte) []byte {\n\tk, v, flags := b.Cursor().seek(key)\n\n\t// Return nil if this is a bucket.\n\tif (flags & bucketLeafFlag) != 0 {\n\t\treturn nil\n\t}\n\n\t// If our target node isn't the same key as what's passed in then return nil.\n\tif !bytes.Equal(key, k) {\n\t\treturn nil\n\t}\n\treturn v\n}\n\n// Put sets the value for a key in the bucket.\n// If the key exist then its previous value will be overwritten.\n// Supplied value must remain valid for the life of the transaction.\n// 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.\nfunc (b *Bucket) Put(key []byte, value []byte) error {\n\tif b.tx.db == nil {\n\t\treturn ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn ErrTxNotWritable\n\t} else if len(key) == 0 {\n\t\treturn ErrKeyRequired\n\t} else if len(key) > MaxKeySize {\n\t\treturn ErrKeyTooLarge\n\t} else if int64(len(value)) > MaxValueSize {\n\t\treturn ErrValueTooLarge\n\t}\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\tk, _, flags := c.seek(key)\n\n\t// Return an error if there is an existing key with a bucket value.\n\tif bytes.Equal(key, k) && (flags&bucketLeafFlag) != 0 {\n\t\treturn ErrIncompatibleValue\n\t}\n\n\t// Insert into node.\n\tkey = cloneBytes(key)\n\tc.node().put(key, key, value, 0, 0)\n\n\treturn nil\n}\n\n// Delete removes a key from the bucket.\n// If the key does not exist then nothing is done and a nil error is returned.\n// Returns an error if the bucket was created from a read-only transaction.\nfunc (b *Bucket) Delete(key []byte) error {\n\tif b.tx.db == nil {\n\t\treturn ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn ErrTxNotWritable\n\t}\n\n\t// Move cursor to correct position.\n\tc := b.Cursor()\n\t_, _, flags := c.seek(key)\n\n\t// Return an error if there is already existing bucket value.\n\tif (flags & bucketLeafFlag) != 0 {\n\t\treturn ErrIncompatibleValue\n\t}\n\n\t// Delete the node if we have a matching key.\n\tc.node().del(key)\n\n\treturn nil\n}\n\n// Sequence returns the current integer for the bucket without incrementing it.\nfunc (b *Bucket) Sequence() uint64 { return b.bucket.sequence }\n\n// SetSequence updates the sequence number for the bucket.\nfunc (b *Bucket) SetSequence(v uint64) error {\n\tif b.tx.db == nil {\n\t\treturn ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn ErrTxNotWritable\n\t}\n\n\t// Materialize the root node if it hasn't been already so that the\n\t// bucket will be saved during commit.\n\tif b.rootNode == nil {\n\t\t_ = b.node(b.root, nil)\n\t}\n\n\t// Increment and return the sequence.\n\tb.bucket.sequence = v\n\treturn nil\n}\n\n// NextSequence returns an autoincrementing integer for the bucket.\nfunc (b *Bucket) NextSequence() (uint64, error) {\n\tif b.tx.db == nil {\n\t\treturn 0, ErrTxClosed\n\t} else if !b.Writable() {\n\t\treturn 0, ErrTxNotWritable\n\t}\n\n\t// Materialize the root node if it hasn't been already so that the\n\t// bucket will be saved during commit.\n\tif b.rootNode == nil {\n\t\t_ = b.node(b.root, nil)\n\t}\n\n\t// Increment and return the sequence.\n\tb.bucket.sequence++\n\treturn b.bucket.sequence, nil\n}\n\n// ForEach executes a function for each key/value pair in a bucket.\n// If the provided function returns an error then the iteration is stopped and\n// the error is returned to the caller. The provided function must not modify\n// the bucket; this will result in undefined behavior.\nfunc (b *Bucket) ForEach(fn func(k, v []byte) error) error {\n\tif b.tx.db == nil {\n\t\treturn ErrTxClosed\n\t}\n\tc := b.Cursor()\n\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\tif err := fn(k, v); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\n// Stat returns stats on a bucket.\nfunc (b *Bucket) Stats() BucketStats {\n\tvar s, subStats BucketStats\n\tpageSize := b.tx.db.pageSize\n\ts.BucketN += 1\n\tif b.root == 0 {\n\t\ts.InlineBucketN += 1\n\t}\n\tb.forEachPage(func(p *page, depth int) {\n\t\tif (p.flags & leafPageFlag) != 0 {\n\t\t\ts.KeyN += int(p.count)\n\n\t\t\t// used totals the used bytes for the page\n\t\t\tused := pageHeaderSize\n\n\t\t\tif p.count != 0 {\n\t\t\t\t// If page has any elements, add all element headers.\n\t\t\t\tused += leafPageElementSize * int(p.count-1)\n\n\t\t\t\t// Add all element key, value sizes.\n\t\t\t\t// The computation takes advantage of the fact that the position\n\t\t\t\t// of the last element's key/value equals to the total of the sizes\n\t\t\t\t// of all previous elements' keys and values.\n\t\t\t\t// It also includes the last element's header.\n\t\t\t\tlastElement := p.leafPageElement(p.count - 1)\n\t\t\t\tused += int(lastElement.pos + lastElement.ksize + lastElement.vsize)\n\t\t\t}\n\n\t\t\tif b.root == 0 {\n\t\t\t\t// For inlined bucket just update the inline stats\n\t\t\t\ts.InlineBucketInuse += used\n\t\t\t} else {\n\t\t\t\t// For non-inlined bucket update all the leaf stats\n\t\t\t\ts.LeafPageN++\n\t\t\t\ts.LeafInuse += used\n\t\t\t\ts.LeafOverflowN += int(p.overflow)\n\n\t\t\t\t// Collect stats from sub-buckets.\n\t\t\t\t// Do that by iterating over all element headers\n\t\t\t\t// looking for the ones with the bucketLeafFlag.\n\t\t\t\tfor i := uint16(0); i < p.count; i++ {\n\t\t\t\t\te := p.leafPageElement(i)\n\t\t\t\t\tif (e.flags & bucketLeafFlag) != 0 {\n\t\t\t\t\t\t// For any bucket element, open the element value\n\t\t\t\t\t\t// and recursively call Stats on the contained bucket.\n\t\t\t\t\t\tsubStats.Add(b.openBucket(e.value()).Stats())\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (p.flags & branchPageFlag) != 0 {\n\t\t\ts.BranchPageN++\n\t\t\tlastElement := p.branchPageElement(p.count - 1)\n\n\t\t\t// used totals the used bytes for the page\n\t\t\t// Add header and all element headers.\n\t\t\tused := pageHeaderSize + (branchPageElementSize * int(p.count-1))\n\n\t\t\t// Add size of all keys and values.\n\t\t\t// Again, use the fact that last element's position equals to\n\t\t\t// the total of key, value sizes of all previous elements.\n\t\t\tused += int(lastElement.pos + lastElement.ksize)\n\t\t\ts.BranchInuse += used\n\t\t\ts.BranchOverflowN += int(p.overflow)\n\t\t}\n\n\t\t// Keep track of maximum page depth.\n\t\tif depth+1 > s.Depth {\n\t\t\ts.Depth = (depth + 1)\n\t\t}\n\t})\n\n\t// Alloc stats can be computed from page counts and pageSize.\n\ts.BranchAlloc = (s.BranchPageN + s.BranchOverflowN) * pageSize\n\ts.LeafAlloc = (s.LeafPageN + s.LeafOverflowN) * pageSize\n\n\t// Add the max depth of sub-buckets to get total nested depth.\n\ts.Depth += subStats.Depth\n\t// Add the stats for all sub-buckets\n\ts.Add(subStats)\n\treturn s\n}\n\n// forEachPage iterates over every page in a bucket, including inline pages.\nfunc (b *Bucket) forEachPage(fn func(*page, int)) {\n\t// If we have an inline page then just use that.\n\tif b.page != nil {\n\t\tfn(b.page, 0)\n\t\treturn\n\t}\n\n\t// Otherwise traverse the page hierarchy.\n\tb.tx.forEachPage(b.root, 0, fn)\n}\n\n// forEachPageNode iterates over every page (or node) in a bucket.\n// This also includes inline pages.\nfunc (b *Bucket) forEachPageNode(fn func(*page, *node, int)) {\n\t// If we have an inline page or root node then just use that.\n\tif b.page != nil {\n\t\tfn(b.page, nil, 0)\n\t\treturn\n\t}\n\tb._forEachPageNode(b.root, 0, fn)\n}\n\nfunc (b *Bucket) _forEachPageNode(pgid pgid, depth int, fn func(*page, *node, int)) {\n\tvar p, n = b.pageNode(pgid)\n\n\t// Execute function.\n\tfn(p, n, depth)\n\n\t// Recursively loop over children.\n\tif p != nil {\n\t\tif (p.flags & branchPageFlag) != 0 {\n\t\t\tfor i := 0; i < int(p.count); i++ {\n\t\t\t\telem := p.branchPageElement(uint16(i))\n\t\t\t\tb._forEachPageNode(elem.pgid, depth+1, fn)\n\t\t\t}\n\t\t}\n\t} else {\n\t\tif !n.isLeaf {\n\t\t\tfor _, inode := range n.inodes {\n\t\t\t\tb._forEachPageNode(inode.pgid, depth+1, fn)\n\t\t\t}\n\t\t}\n\t}\n}\n\n// spill writes all the nodes for this bucket to dirty pages.\nfunc (b *Bucket) spill() error {\n\t// Spill all child buckets first.\n\tfor name, child := range b.buckets {\n\t\t// If the child bucket is small enough and it has no child buckets then\n\t\t// write it inline into the parent bucket's page. Otherwise spill it\n\t\t// like a normal bucket and make the parent value a pointer to the page.\n\t\tvar value []byte\n\t\tif child.inlineable() {\n\t\t\tchild.free()\n\t\t\tvalue = child.write()\n\t\t} else {\n\t\t\tif err := child.spill(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t// Update the child bucket header in this bucket.\n\t\t\tvalue = make([]byte, unsafe.Sizeof(bucket{}))\n\t\t\tvar bucket = (*bucket)(unsafe.Pointer(&value[0]))\n\t\t\t*bucket = *child.bucket\n\t\t}\n\n\t\t// Skip writing the bucket if there are no materialized nodes.\n\t\tif child.rootNode == nil {\n\t\t\tcontinue\n\t\t}\n\n\t\t// Update parent node.\n\t\tvar c = b.Cursor()\n\t\tk, _, flags := c.seek([]byte(name))\n\t\tif !bytes.Equal([]byte(name), k) {\n\t\t\tpanic(fmt.Sprintf(\"misplaced bucket header: %x -> %x\", []byte(name), k))\n\t\t}\n\t\tif flags&bucketLeafFlag == 0 {\n\t\t\tpanic(fmt.Sprintf(\"unexpected bucket header flag: %x\", flags))\n\t\t}\n\t\tc.node().put([]byte(name), []byte(name), value, 0, bucketLeafFlag)\n\t}\n\n\t// Ignore if there's not a materialized root node.\n\tif b.rootNode == nil {\n\t\treturn nil\n\t}\n\n\t// Spill nodes.\n\tif err := b.rootNode.spill(); err != nil {\n\t\treturn err\n\t}\n\tb.rootNode = b.rootNode.root()\n\n\t// Update the root node for this bucket.\n\tif b.rootNode.pgid >= b.tx.meta.pgid {\n\t\tpanic(fmt.Sprintf(\"pgid (%d) above high water mark (%d)\", b.rootNode.pgid, b.tx.meta.pgid))\n\t}\n\tb.root = b.rootNode.pgid\n\n\treturn nil\n}\n\n// inlineable returns true if a bucket is small enough to be written inline\n// and if it contains no subbuckets. Otherwise returns false.\nfunc (b *Bucket) inlineable() bool {\n\tvar n = b.rootNode\n\n\t// Bucket must only contain a single leaf node.\n\tif n == nil || !n.isLeaf {\n\t\treturn false\n\t}\n\n\t// Bucket is not inlineable if it contains subbuckets or if it goes beyond\n\t// our threshold for inline bucket size.\n\tvar size = pageHeaderSize\n\tfor _, inode := range n.inodes {\n\t\tsize += leafPageElementSize + len(inode.key) + len(inode.value)\n\n\t\tif inode.flags&bucketLeafFlag != 0 {\n\t\t\treturn false\n\t\t} else if size > b.maxInlineBucketSize() {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n\n// Returns the maximum total size of a bucket to make it a candidate for inlining.\nfunc (b *Bucket) maxInlineBucketSize() int {\n\treturn b.tx.db.pageSize / 4\n}\n\n// write allocates and writes a bucket to a byte slice.\nfunc (b *Bucket) write() []byte {\n\t// Allocate the appropriate size.\n\tvar n = b.rootNode\n\tvar value = make([]byte, bucketHeaderSize+n.size())\n\n\t// Write a bucket header.\n\tvar bucket = (*bucket)(unsafe.Pointer(&value[0]))\n\t*bucket = *b.bucket\n\n\t// Convert byte slice to a fake page and write the root node.\n\tvar p = (*page)(unsafe.Pointer(&value[bucketHeaderSize]))\n\tn.write(p)\n\n\treturn value\n}\n\n// rebalance attempts to balance all nodes.\nfunc (b *Bucket) rebalance() {\n\tfor _, n := range b.nodes {\n\t\tn.rebalance()\n\t}\n\tfor _, child := range b.buckets {\n\t\tchild.rebalance()\n\t}\n}\n\n// node creates a node from a page and associates it with a given parent.\nfunc (b *Bucket) node(pgid pgid, parent *node) *node {\n\t_assert(b.nodes != nil, \"nodes map expected\")\n\n\t// Retrieve node if it's already been created.\n\tif n := b.nodes[pgid]; n != nil {\n\t\treturn n\n\t}\n\n\t// Otherwise create a node and cache it.\n\tn := &node{bucket: b, parent: parent}\n\tif parent == nil {\n\t\tb.rootNode = n\n\t} else {\n\t\tparent.children = append(parent.children, n)\n\t}\n\n\t// Use the inline page if this is an inline bucket.\n\tvar p = b.page\n\tif p == nil {\n\t\tp = b.tx.page(pgid)\n\t}\n\n\t// Read the page into the node and cache it.\n\tn.read(p)\n\tb.nodes[pgid] = n\n\n\t// Update statistics.\n\tb.tx.stats.NodeCount++\n\n\treturn n\n}\n\n// free recursively frees all pages in the bucket.\nfunc (b *Bucket) free() {\n\tif b.root == 0 {\n\t\treturn\n\t}\n\n\tvar tx = b.tx\n\tb.forEachPageNode(func(p *page, n *node, _ int) {\n\t\tif p != nil {\n\t\t\ttx.db.freelist.free(tx.meta.txid, p)\n\t\t} else {\n\t\t\tn.free()\n\t\t}\n\t})\n\tb.root = 0\n}\n\n// dereference removes all references to the old mmap.\nfunc (b *Bucket) dereference() {\n\tif b.rootNode != nil {\n\t\tb.rootNode.root().dereference()\n\t}\n\n\tfor _, child := range b.buckets {\n\t\tchild.dereference()\n\t}\n}\n\n// pageNode returns the in-memory node, if it exists.\n// Otherwise returns the underlying page.\nfunc (b *Bucket) pageNode(id pgid) (*page, *node) {\n\t// Inline buckets have a fake page embedded in their value so treat them\n\t// differently. We'll return the rootNode (if available) or the fake page.\n\tif b.root == 0 {\n\t\tif id != 0 {\n\t\t\tpanic(fmt.Sprintf(\"inline bucket non-zero page access(2): %d != 0\", id))\n\t\t}\n\t\tif b.rootNode != nil {\n\t\t\treturn nil, b.rootNode\n\t\t}\n\t\treturn b.page, nil\n\t}\n\n\t// Check the node cache for non-inline buckets.\n\tif b.nodes != nil {\n\t\tif n := b.nodes[id]; n != nil {\n\t\t\treturn nil, n\n\t\t}\n\t}\n\n\t// Finally lookup the page from the transaction if no node is materialized.\n\treturn b.tx.page(id), nil\n}\n\n// BucketStats records statistics about resources used by a bucket.\ntype BucketStats struct {\n\t// Page count statistics.\n\tBranchPageN     int // number of logical branch pages\n\tBranchOverflowN int // number of physical branch overflow pages\n\tLeafPageN       int // number of logical leaf pages\n\tLeafOverflowN   int // number of physical leaf overflow pages\n\n\t// Tree statistics.\n\tKeyN  int // number of keys/value pairs\n\tDepth int // number of levels in B+tree\n\n\t// Page size utilization.\n\tBranchAlloc int // bytes allocated for physical branch pages\n\tBranchInuse int // bytes actually used for branch data\n\tLeafAlloc   int // bytes allocated for physical leaf pages\n\tLeafInuse   int // bytes actually used for leaf data\n\n\t// Bucket statistics\n\tBucketN           int // total number of buckets including the top bucket\n\tInlineBucketN     int // total number on inlined buckets\n\tInlineBucketInuse int // bytes used for inlined buckets (also accounted for in LeafInuse)\n}\n\nfunc (s *BucketStats) Add(other BucketStats) {\n\ts.BranchPageN += other.BranchPageN\n\ts.BranchOverflowN += other.BranchOverflowN\n\ts.LeafPageN += other.LeafPageN\n\ts.LeafOverflowN += other.LeafOverflowN\n\ts.KeyN += other.KeyN\n\tif s.Depth < other.Depth {\n\t\ts.Depth = other.Depth\n\t}\n\ts.BranchAlloc += other.BranchAlloc\n\ts.BranchInuse += other.BranchInuse\n\ts.LeafAlloc += other.LeafAlloc\n\ts.LeafInuse += other.LeafInuse\n\n\ts.BucketN += other.BucketN\n\ts.InlineBucketN += other.InlineBucketN\n\ts.InlineBucketInuse += other.InlineBucketInuse\n}\n\n// cloneBytes returns a copy of a given slice.\nfunc cloneBytes(v []byte) []byte {\n\tvar clone = make([]byte, len(v))\n\tcopy(clone, v)\n\treturn clone\n}\n"
  },
  {
    "path": "bucket_test.go",
    "content": "package bolt_test\n\nimport (\n\t\"bytes\"\n\t\"encoding/binary\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"math/rand\"\n\t\"os\"\n\t\"strconv\"\n\t\"strings\"\n\t\"testing\"\n\t\"testing/quick\"\n\n\t\"github.com/boltdb/bolt\"\n)\n\n// Ensure that a bucket that gets a non-existent key returns nil.\nfunc TestBucket_Get_NonExistent(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif v := b.Get([]byte(\"foo\")); v != nil {\n\t\t\tt.Fatal(\"expected nil value\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can read a value that is not flushed yet.\nfunc TestBucket_Get_FromNode(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif v := b.Get([]byte(\"foo\")); !bytes.Equal(v, []byte(\"bar\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket retrieved via Get() returns a nil.\nfunc TestBucket_Get_IncompatibleValue(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err := tx.Bucket([]byte(\"widgets\")).CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\")) != nil {\n\t\t\tt.Fatal(\"expected nil value\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a slice returned from a bucket has a capacity equal to its length.\n// This also allows slices to be appended to since it will require a realloc by Go.\n//\n// https://github.com/boltdb/bolt/issues/544\nfunc TestBucket_Get_Capacity(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Write key to a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"bucket\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn b.Put([]byte(\"key\"), []byte(\"val\"))\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Retrieve value and attempt to append to it.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tk, v := tx.Bucket([]byte(\"bucket\")).Cursor().First()\n\n\t\t// Verify capacity.\n\t\tif len(k) != cap(k) {\n\t\t\tt.Fatalf(\"unexpected key slice capacity: %d\", cap(k))\n\t\t} else if len(v) != cap(v) {\n\t\t\tt.Fatalf(\"unexpected value slice capacity: %d\", cap(v))\n\t\t}\n\n\t\t// Ensure slice can be appended to without a segfault.\n\t\tk = append(k, []byte(\"123\")...)\n\t\tv = append(v, []byte(\"123\")...)\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can write a key/value.\nfunc TestBucket_Put(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tv := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tif !bytes.Equal([]byte(\"bar\"), v) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can rewrite a key in the same transaction.\nfunc TestBucket_Put_Repeat(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"baz\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tif !bytes.Equal([]byte(\"baz\"), value) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", value)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can write a bunch of large values.\nfunc TestBucket_Put_Large(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tcount, factor := 100, 200\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 1; i < count; i++ {\n\t\t\tif err := b.Put([]byte(strings.Repeat(\"0\", i*factor)), []byte(strings.Repeat(\"X\", (count-i)*factor))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 1; i < count; i++ {\n\t\t\tvalue := b.Get([]byte(strings.Repeat(\"0\", i*factor)))\n\t\t\tif !bytes.Equal(value, []byte(strings.Repeat(\"X\", (count-i)*factor))) {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", value)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a database can perform multiple large appends safely.\nfunc TestDB_Put_VeryLarge(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tn, batchN := 400000, 200000\n\tksize, vsize := 8, 500\n\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tfor i := 0; i < n; i += batchN {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"widgets\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tfor j := 0; j < batchN; j++ {\n\t\t\t\tk, v := make([]byte, ksize), make([]byte, vsize)\n\t\t\t\tbinary.BigEndian.PutUint32(k, uint32(i+j))\n\t\t\t\tif err := b.Put(k, v); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n}\n\n// Ensure that a setting a value on a key with a bucket value returns an error.\nfunc TestBucket_Put_IncompatibleValue(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb0, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err := tx.Bucket([]byte(\"widgets\")).CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b0.Put([]byte(\"foo\"), []byte(\"bar\")); err != bolt.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a setting a value while the transaction is closed returns an error.\nfunc TestBucket_Put_Closed(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != bolt.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that setting a value on a read-only bucket returns an error.\nfunc TestBucket_Put_ReadOnly(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != bolt.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can delete an existing key.\nfunc TestBucket_Delete(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Delete([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif v := b.Get([]byte(\"foo\")); v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a large set of keys will work correctly.\nfunc TestBucket_Delete_Large(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif err := b.Put([]byte(strconv.Itoa(i)), []byte(strings.Repeat(\"*\", 1024))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif err := b.Delete([]byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif v := b.Get([]byte(strconv.Itoa(i))); v != nil {\n\t\t\t\tt.Fatalf(\"unexpected value: %v, i=%d\", v, i)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Deleting a very large list of keys will cause the freelist to use overflow.\nfunc TestBucket_Delete_FreelistOverflow(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tk := make([]byte, 16)\n\tfor i := uint64(0); i < 10000; i++ {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"0\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatalf(\"bucket error: %s\", err)\n\t\t\t}\n\n\t\t\tfor j := uint64(0); j < 1000; j++ {\n\t\t\t\tbinary.BigEndian.PutUint64(k[:8], i)\n\t\t\t\tbinary.BigEndian.PutUint64(k[8:], j)\n\t\t\t\tif err := b.Put(k, nil); err != nil {\n\t\t\t\t\tt.Fatalf(\"put error: %s\", err)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Delete all of them in one large transaction\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"0\"))\n\t\tc := b.Cursor()\n\t\tfor k, _ := c.First(); k != nil; k, _ = c.Next() {\n\t\t\tif err := c.Delete(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that accessing and updating nested buckets is ok across transactions.\nfunc TestBucket_Nested(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a widgets bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Create a widgets/foo bucket.\n\t\t_, err = b.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Create a widgets/bar key.\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustCheck()\n\n\t// Update widgets/bar.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"xxxx\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustCheck()\n\n\t// Cause a split.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tvar b = tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < 10000; i++ {\n\t\t\tif err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustCheck()\n\n\t// Insert into widgets/foo/baz.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tvar b = tx.Bucket([]byte(\"widgets\"))\n\t\tif err := b.Bucket([]byte(\"foo\")).Put([]byte(\"baz\"), []byte(\"yyyy\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.MustCheck()\n\n\t// Verify.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvar b = tx.Bucket([]byte(\"widgets\"))\n\t\tif v := b.Bucket([]byte(\"foo\")).Get([]byte(\"baz\")); !bytes.Equal(v, []byte(\"yyyy\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\tif v := b.Get([]byte(\"bar\")); !bytes.Equal(v, []byte(\"xxxx\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\tfor i := 0; i < 10000; i++ {\n\t\t\tif v := b.Get([]byte(strconv.Itoa(i))); !bytes.Equal(v, []byte(strconv.Itoa(i))) {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a bucket using Delete() returns an error.\nfunc TestBucket_Delete_Bucket(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Delete([]byte(\"foo\")); err != bolt.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a key on a read-only bucket returns an error.\nfunc TestBucket_Delete_ReadOnly(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif err := tx.Bucket([]byte(\"widgets\")).Delete([]byte(\"foo\")); err != bolt.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a deleting value while the transaction is closed returns an error.\nfunc TestBucket_Delete_Closed(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := b.Delete([]byte(\"foo\")); err != bolt.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that deleting a bucket causes nested buckets to be deleted.\nfunc TestBucket_DeleteBucket_Nested(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfoo, err := widgets.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tbar, err := foo.CreateBucket([]byte(\"bar\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := bar.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := tx.Bucket([]byte(\"widgets\")).DeleteBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a bucket causes nested buckets to be deleted after they have been committed.\nfunc TestBucket_DeleteBucket_Nested2(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfoo, err := widgets.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tbar, err := foo.CreateBucket([]byte(\"bar\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := bar.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets := tx.Bucket([]byte(\"widgets\"))\n\t\tif widgets == nil {\n\t\t\tt.Fatal(\"expected widgets bucket\")\n\t\t}\n\n\t\tfoo := widgets.Bucket([]byte(\"foo\"))\n\t\tif foo == nil {\n\t\t\tt.Fatal(\"expected foo bucket\")\n\t\t}\n\n\t\tbar := foo.Bucket([]byte(\"bar\"))\n\t\tif bar == nil {\n\t\t\tt.Fatal(\"expected bar bucket\")\n\t\t}\n\n\t\tif v := bar.Get([]byte(\"baz\")); !bytes.Equal(v, []byte(\"bat\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\tif err := tx.DeleteBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) != nil {\n\t\t\tt.Fatal(\"expected bucket to be deleted\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a child bucket with multiple pages causes all pages to get collected.\n// NOTE: Consistency check in bolt_test.DB.Close() will panic if pages not freed properly.\nfunc TestBucket_DeleteBucket_Large(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfoo, err := widgets.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < 1000; i++ {\n\t\t\tif err := foo.Put([]byte(fmt.Sprintf(\"%d\", i)), []byte(fmt.Sprintf(\"%0100d\", i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.DeleteBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a simple value retrieved via Bucket() returns a nil.\nfunc TestBucket_Bucket_IncompatibleValue(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := widgets.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif b := tx.Bucket([]byte(\"widgets\")).Bucket([]byte(\"foo\")); b != nil {\n\t\t\tt.Fatal(\"expected nil bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that creating a bucket on an existing non-bucket key returns an error.\nfunc TestBucket_CreateBucket_IncompatibleValue(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := widgets.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := widgets.CreateBucket([]byte(\"foo\")); err != bolt.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a bucket on an existing non-bucket key returns an error.\nfunc TestBucket_DeleteBucket_IncompatibleValue(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := widgets.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := tx.Bucket([]byte(\"widgets\")).DeleteBucket([]byte(\"foo\")); err != bolt.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure bucket can set and update its sequence number.\nfunc TestBucket_Sequence(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tbkt, err := tx.CreateBucket([]byte(\"0\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Retrieve sequence.\n\t\tif v := bkt.Sequence(); v != 0 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", v)\n\t\t}\n\n\t\t// Update sequence.\n\t\tif err := bkt.SetSequence(1000); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Read sequence again.\n\t\tif v := bkt.Sequence(); v != 1000 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", v)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Verify sequence in separate transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif v := tx.Bucket([]byte(\"0\")).Sequence(); v != 1000 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can return an autoincrementing sequence.\nfunc TestBucket_NextSequence(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\twidgets, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\twoojits, err := tx.CreateBucket([]byte(\"woojits\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Make sure sequence increments.\n\t\tif seq, err := widgets.NextSequence(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if seq != 1 {\n\t\t\tt.Fatalf(\"unexpecte sequence: %d\", seq)\n\t\t}\n\n\t\tif seq, err := widgets.NextSequence(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if seq != 2 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", seq)\n\t\t}\n\n\t\t// Buckets should be separate.\n\t\tif seq, err := woojits.NextSequence(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if seq != 1 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", 1)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket will persist an autoincrementing sequence even if its\n// the only thing updated on the bucket.\n// https://github.com/boltdb/bolt/issues/296\nfunc TestBucket_NextSequence_Persist(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.Bucket([]byte(\"widgets\")).NextSequence(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tseq, err := tx.Bucket([]byte(\"widgets\")).NextSequence()\n\t\tif err != nil {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t} else if seq != 2 {\n\t\t\tt.Fatalf(\"unexpected sequence: %d\", seq)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that retrieving the next sequence on a read-only bucket returns an error.\nfunc TestBucket_NextSequence_ReadOnly(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t_, err := tx.Bucket([]byte(\"widgets\")).NextSequence()\n\t\tif err != bolt.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that retrieving the next sequence for a bucket on a closed database return an error.\nfunc TestBucket_NextSequence_Closed(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif _, err := b.NextSequence(); err != bolt.ErrTxClosed {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a user can loop over all key/value pairs in a bucket.\nfunc TestBucket_ForEach(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"0001\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"0002\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tvar index int\n\t\tif err := b.ForEach(func(k, v []byte) error {\n\t\t\tswitch index {\n\t\t\tcase 0:\n\t\t\t\tif !bytes.Equal(k, []byte(\"bar\")) {\n\t\t\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t\t\t} else if !bytes.Equal(v, []byte(\"0002\")) {\n\t\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t\t}\n\t\t\tcase 1:\n\t\t\t\tif !bytes.Equal(k, []byte(\"baz\")) {\n\t\t\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t\t\t} else if !bytes.Equal(v, []byte(\"0001\")) {\n\t\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t\t}\n\t\t\tcase 2:\n\t\t\t\tif !bytes.Equal(k, []byte(\"foo\")) {\n\t\t\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t\t\t} else if !bytes.Equal(v, []byte(\"0000\")) {\n\t\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t\t}\n\t\t\t}\n\t\t\tindex++\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif index != 3 {\n\t\t\tt.Fatalf(\"unexpected index: %d\", index)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a database can stop iteration early.\nfunc TestBucket_ForEach_ShortCircuit(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"0000\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tvar index int\n\t\tif err := tx.Bucket([]byte(\"widgets\")).ForEach(func(k, v []byte) error {\n\t\t\tindex++\n\t\t\tif bytes.Equal(k, []byte(\"baz\")) {\n\t\t\t\treturn errors.New(\"marker\")\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err == nil || err.Error() != \"marker\" {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\tif index != 2 {\n\t\t\tt.Fatalf(\"unexpected index: %d\", index)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that looping over a bucket on a closed database returns an error.\nfunc TestBucket_ForEach_Closed(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := b.ForEach(func(k, v []byte) error { return nil }); err != bolt.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that an error is returned when inserting with an empty key.\nfunc TestBucket_Put_EmptyKey(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"\"), []byte(\"bar\")); err != bolt.ErrKeyRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\tif err := b.Put(nil, []byte(\"bar\")); err != bolt.ErrKeyRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that an error is returned when inserting with a key that's too large.\nfunc TestBucket_Put_KeyTooLarge(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put(make([]byte, 32769), []byte(\"bar\")); err != bolt.ErrKeyTooLarge {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that an error is returned when inserting a value that's too large.\nfunc TestBucket_Put_ValueTooLarge(t *testing.T) {\n\t// Skip this test on DroneCI because the machine is resource constrained.\n\tif os.Getenv(\"DRONE\") == \"true\" {\n\t\tt.Skip(\"not enough RAM for test\")\n\t}\n\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), make([]byte, bolt.MaxValueSize+1)); err != bolt.ErrValueTooLarge {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a bucket can calculate stats.\nfunc TestBucket_Stats(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Add bucket with fewer keys but one big value.\n\tbigKey := []byte(\"really-big-value\")\n\tfor i := 0; i < 500; i++ {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"woojits\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\n\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"%03d\", i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.Bucket([]byte(\"woojits\")).Put(bigKey, []byte(strings.Repeat(\"*\", 10000))); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tstats := tx.Bucket([]byte(\"woojits\")).Stats()\n\t\tif stats.BranchPageN != 1 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.LeafPageN != 7 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 2 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.KeyN != 501 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t} else if stats.Depth != 2 {\n\t\t\tt.Fatalf(\"unexpected Depth: %d\", stats.Depth)\n\t\t}\n\n\t\tbranchInuse := 16     // branch page header\n\t\tbranchInuse += 7 * 16 // branch elements\n\t\tbranchInuse += 7 * 3  // branch keys (6 3-byte keys)\n\t\tif stats.BranchInuse != branchInuse {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t}\n\n\t\tleafInuse := 7 * 16                      // leaf page header\n\t\tleafInuse += 501 * 16                    // leaf elements\n\t\tleafInuse += 500*3 + len(bigKey)         // leaf keys\n\t\tleafInuse += 1*10 + 2*90 + 3*400 + 10000 // leaf values\n\t\tif stats.LeafInuse != leafInuse {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t}\n\n\t\t// Only check allocations for 4KB pages.\n\t\tif os.Getpagesize() == 4096 {\n\t\t\tif stats.BranchAlloc != 4096 {\n\t\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t\t} else if stats.LeafAlloc != 36864 {\n\t\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t\t}\n\t\t}\n\n\t\tif stats.BucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected BucketN: %d\", stats.BucketN)\n\t\t} else if stats.InlineBucketN != 0 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketN: %d\", stats.InlineBucketN)\n\t\t} else if stats.InlineBucketInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketInuse: %d\", stats.InlineBucketInuse)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a bucket with random insertion utilizes fill percentage correctly.\nfunc TestBucket_Stats_RandomFill(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t} else if os.Getpagesize() != 4096 {\n\t\tt.Skip(\"invalid page size for test\")\n\t}\n\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Add a set of values in random order. It will be the same random\n\t// order so we can maintain consistency between test runs.\n\tvar count int\n\trand := rand.New(rand.NewSource(42))\n\tfor _, i := range rand.Perm(1000) {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"woojits\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tb.FillPercent = 0.9\n\t\t\tfor _, j := range rand.Perm(100) {\n\t\t\t\tindex := (j * 10000) + i\n\t\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"%d000000000000000\", index)), []byte(\"0000000000\")); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t\tcount++\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tstats := tx.Bucket([]byte(\"woojits\")).Stats()\n\t\tif stats.KeyN != 100000 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t}\n\n\t\tif stats.BranchPageN != 98 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.BranchInuse != 130984 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t} else if stats.BranchAlloc != 401408 {\n\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t}\n\n\t\tif stats.LeafPageN != 3412 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.LeafInuse != 4742482 {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t} else if stats.LeafAlloc != 13975552 {\n\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a bucket can calculate stats.\nfunc TestBucket_Stats_Small(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Add a bucket that fits on a single root leaf.\n\t\tb, err := tx.CreateBucket([]byte(\"whozawhats\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"whozawhats\"))\n\t\tstats := b.Stats()\n\t\tif stats.BranchPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.LeafPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.KeyN != 1 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t} else if stats.Depth != 1 {\n\t\t\tt.Fatalf(\"unexpected Depth: %d\", stats.Depth)\n\t\t} else if stats.BranchInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t} else if stats.LeafInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t}\n\n\t\tif os.Getpagesize() == 4096 {\n\t\t\tif stats.BranchAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t\t} else if stats.LeafAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t\t}\n\t\t}\n\n\t\tif stats.BucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected BucketN: %d\", stats.BucketN)\n\t\t} else if stats.InlineBucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketN: %d\", stats.InlineBucketN)\n\t\t} else if stats.InlineBucketInuse != 16+16+6 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketInuse: %d\", stats.InlineBucketInuse)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestBucket_Stats_EmptyBucket(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Add a bucket that fits on a single root leaf.\n\t\tif _, err := tx.CreateBucket([]byte(\"whozawhats\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"whozawhats\"))\n\t\tstats := b.Stats()\n\t\tif stats.BranchPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.LeafPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.KeyN != 0 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t} else if stats.Depth != 1 {\n\t\t\tt.Fatalf(\"unexpected Depth: %d\", stats.Depth)\n\t\t} else if stats.BranchInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t} else if stats.LeafInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t}\n\n\t\tif os.Getpagesize() == 4096 {\n\t\t\tif stats.BranchAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t\t} else if stats.LeafAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t\t}\n\t\t}\n\n\t\tif stats.BucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected BucketN: %d\", stats.BucketN)\n\t\t} else if stats.InlineBucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketN: %d\", stats.InlineBucketN)\n\t\t} else if stats.InlineBucketInuse != 16 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketInuse: %d\", stats.InlineBucketInuse)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a bucket can calculate stats.\nfunc TestBucket_Stats_Nested(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"%02d\", i)), []byte(fmt.Sprintf(\"%02d\", i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tbar, err := b.CreateBucket([]byte(\"bar\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tif err := bar.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tbaz, err := bar.CreateBucket([]byte(\"baz\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tif err := baz.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"foo\"))\n\t\tstats := b.Stats()\n\t\tif stats.BranchPageN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.LeafPageN != 2 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.KeyN != 122 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t} else if stats.Depth != 3 {\n\t\t\tt.Fatalf(\"unexpected Depth: %d\", stats.Depth)\n\t\t} else if stats.BranchInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t}\n\n\t\tfoo := 16            // foo (pghdr)\n\t\tfoo += 101 * 16      // foo leaf elements\n\t\tfoo += 100*2 + 100*2 // foo leaf key/values\n\t\tfoo += 3 + 16        // foo -> bar key/value\n\n\t\tbar := 16      // bar (pghdr)\n\t\tbar += 11 * 16 // bar leaf elements\n\t\tbar += 10 + 10 // bar leaf key/values\n\t\tbar += 3 + 16  // bar -> baz key/value\n\n\t\tbaz := 16      // baz (inline) (pghdr)\n\t\tbaz += 10 * 16 // baz leaf elements\n\t\tbaz += 10 + 10 // baz leaf key/values\n\n\t\tif stats.LeafInuse != foo+bar+baz {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t}\n\n\t\tif os.Getpagesize() == 4096 {\n\t\t\tif stats.BranchAlloc != 0 {\n\t\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t\t} else if stats.LeafAlloc != 8192 {\n\t\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t\t}\n\t\t}\n\n\t\tif stats.BucketN != 3 {\n\t\t\tt.Fatalf(\"unexpected BucketN: %d\", stats.BucketN)\n\t\t} else if stats.InlineBucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketN: %d\", stats.InlineBucketN)\n\t\t} else if stats.InlineBucketInuse != baz {\n\t\t\tt.Fatalf(\"unexpected InlineBucketInuse: %d\", stats.InlineBucketInuse)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a large bucket can calculate stats.\nfunc TestBucket_Stats_Large(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar index int\n\tfor i := 0; i < 100; i++ {\n\t\t// Add bucket with lots of keys.\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"widgets\"))\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tfor i := 0; i < 1000; i++ {\n\t\t\t\tif err := b.Put([]byte(strconv.Itoa(index)), []byte(strconv.Itoa(index))); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t\tindex++\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tdb.MustCheck()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tstats := tx.Bucket([]byte(\"widgets\")).Stats()\n\t\tif stats.BranchPageN != 13 {\n\t\t\tt.Fatalf(\"unexpected BranchPageN: %d\", stats.BranchPageN)\n\t\t} else if stats.BranchOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected BranchOverflowN: %d\", stats.BranchOverflowN)\n\t\t} else if stats.LeafPageN != 1196 {\n\t\t\tt.Fatalf(\"unexpected LeafPageN: %d\", stats.LeafPageN)\n\t\t} else if stats.LeafOverflowN != 0 {\n\t\t\tt.Fatalf(\"unexpected LeafOverflowN: %d\", stats.LeafOverflowN)\n\t\t} else if stats.KeyN != 100000 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t} else if stats.Depth != 3 {\n\t\t\tt.Fatalf(\"unexpected Depth: %d\", stats.Depth)\n\t\t} else if stats.BranchInuse != 25257 {\n\t\t\tt.Fatalf(\"unexpected BranchInuse: %d\", stats.BranchInuse)\n\t\t} else if stats.LeafInuse != 2596916 {\n\t\t\tt.Fatalf(\"unexpected LeafInuse: %d\", stats.LeafInuse)\n\t\t}\n\n\t\tif os.Getpagesize() == 4096 {\n\t\t\tif stats.BranchAlloc != 53248 {\n\t\t\t\tt.Fatalf(\"unexpected BranchAlloc: %d\", stats.BranchAlloc)\n\t\t\t} else if stats.LeafAlloc != 4898816 {\n\t\t\t\tt.Fatalf(\"unexpected LeafAlloc: %d\", stats.LeafAlloc)\n\t\t\t}\n\t\t}\n\n\t\tif stats.BucketN != 1 {\n\t\t\tt.Fatalf(\"unexpected BucketN: %d\", stats.BucketN)\n\t\t} else if stats.InlineBucketN != 0 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketN: %d\", stats.InlineBucketN)\n\t\t} else if stats.InlineBucketInuse != 0 {\n\t\t\tt.Fatalf(\"unexpected InlineBucketInuse: %d\", stats.InlineBucketInuse)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can write random keys and values across multiple transactions.\nfunc TestBucket_Put_Single(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tindex := 0\n\tif err := quick.Check(func(items testdata) bool {\n\t\tdb := MustOpenDB()\n\t\tdefer db.MustClose()\n\n\t\tm := make(map[string][]byte)\n\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor _, item := range items {\n\t\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\tif err := tx.Bucket([]byte(\"widgets\")).Put(item.Key, item.Value); err != nil {\n\t\t\t\t\tpanic(\"put error: \" + err.Error())\n\t\t\t\t}\n\t\t\t\tm[string(item.Key)] = item.Value\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\n\t\t\t// Verify all key/values so far.\n\t\t\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t\t\ti := 0\n\t\t\t\tfor k, v := range m {\n\t\t\t\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(k))\n\t\t\t\t\tif !bytes.Equal(value, v) {\n\t\t\t\t\t\tt.Logf(\"value mismatch [run %d] (%d of %d):\\nkey: %x\\ngot: %x\\nexp: %x\", index, i, len(m), []byte(k), value, v)\n\t\t\t\t\t\tdb.CopyTempFile()\n\t\t\t\t\t\tt.FailNow()\n\t\t\t\t\t}\n\t\t\t\t\ti++\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tindex++\n\t\treturn true\n\t}, nil); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\n// Ensure that a transaction can insert multiple key/value pairs at once.\nfunc TestBucket_Put_Multiple(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tif err := quick.Check(func(items testdata) bool {\n\t\tdb := MustOpenDB()\n\t\tdefer db.MustClose()\n\n\t\t// Bulk insert all values.\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\t\tfor _, item := range items {\n\t\t\t\tif err := b.Put(item.Key, item.Value); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Verify all items exist.\n\t\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\t\tfor _, item := range items {\n\t\t\t\tvalue := b.Get(item.Key)\n\t\t\t\tif !bytes.Equal(item.Value, value) {\n\t\t\t\t\tdb.CopyTempFile()\n\t\t\t\t\tt.Fatalf(\"exp=%x; got=%x\", item.Value, value)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn true\n\t}, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\n// Ensure that a transaction can delete all key/value pairs and return to a single leaf page.\nfunc TestBucket_Delete_Quick(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\tif err := quick.Check(func(items testdata) bool {\n\t\tdb := MustOpenDB()\n\t\tdefer db.MustClose()\n\n\t\t// Bulk insert all values.\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\t\tfor _, item := range items {\n\t\t\t\tif err := b.Put(item.Key, item.Value); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Remove items one at a time and check consistency.\n\t\tfor _, item := range items {\n\t\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\t\treturn tx.Bucket([]byte(\"widgets\")).Delete(item.Key)\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\t// Anything before our deletion index should be nil.\n\t\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t\tif err := tx.Bucket([]byte(\"widgets\")).ForEach(func(k, v []byte) error {\n\t\t\t\tt.Fatalf(\"bucket should be empty; found: %06x\", trunc(k, 3))\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn true\n\t}, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\nfunc ExampleBucket_Put() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Start a write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Set the value \"bar\" for the key \"foo\".\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Read value back in a different read-only transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value of 'foo' is: %s\\n\", value)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value of 'foo' is: bar\n}\n\nfunc ExampleBucket_Delete() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Start a write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Set the value \"bar\" for the key \"foo\".\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Retrieve the key back from the database and verify it.\n\t\tvalue := b.Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value of 'foo' was: %s\\n\", value)\n\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Delete the key in a different write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\treturn tx.Bucket([]byte(\"widgets\")).Delete([]byte(\"foo\"))\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Retrieve the key again.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tif value == nil {\n\t\t\tfmt.Printf(\"The value of 'foo' is now: nil\\n\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value of 'foo' was: bar\n\t// The value of 'foo' is now: nil\n}\n\nfunc ExampleBucket_ForEach() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Insert data into a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"animals\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif err := b.Put([]byte(\"dog\"), []byte(\"fun\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"cat\"), []byte(\"lame\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"liger\"), []byte(\"awesome\")); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Iterate over items in sorted key order.\n\t\tif err := b.ForEach(func(k, v []byte) error {\n\t\t\tfmt.Printf(\"A %s is %s.\\n\", k, v)\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// A cat is lame.\n\t// A dog is fun.\n\t// A liger is awesome.\n}\n"
  },
  {
    "path": "cmd/bolt/main.go",
    "content": "package main\n\nimport (\n\t\"bytes\"\n\t\"encoding/binary\"\n\t\"errors\"\n\t\"flag\"\n\t\"fmt\"\n\t\"io\"\n\t\"io/ioutil\"\n\t\"math/rand\"\n\t\"os\"\n\t\"runtime\"\n\t\"runtime/pprof\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\t\"unicode\"\n\t\"unicode/utf8\"\n\t\"unsafe\"\n\n\t\"github.com/boltdb/bolt\"\n)\n\nvar (\n\t// ErrUsage is returned when a usage message was printed and the process\n\t// should simply exit with an error.\n\tErrUsage = errors.New(\"usage\")\n\n\t// ErrUnknownCommand is returned when a CLI command is not specified.\n\tErrUnknownCommand = errors.New(\"unknown command\")\n\n\t// ErrPathRequired is returned when the path to a Bolt database is not specified.\n\tErrPathRequired = errors.New(\"path required\")\n\n\t// ErrFileNotFound is returned when a Bolt database does not exist.\n\tErrFileNotFound = errors.New(\"file not found\")\n\n\t// ErrInvalidValue is returned when a benchmark reads an unexpected value.\n\tErrInvalidValue = errors.New(\"invalid value\")\n\n\t// ErrCorrupt is returned when a checking a data file finds errors.\n\tErrCorrupt = errors.New(\"invalid value\")\n\n\t// ErrNonDivisibleBatchSize is returned when the batch size can't be evenly\n\t// divided by the iteration count.\n\tErrNonDivisibleBatchSize = errors.New(\"number of iterations must be divisible by the batch size\")\n\n\t// ErrPageIDRequired is returned when a required page id is not specified.\n\tErrPageIDRequired = errors.New(\"page id required\")\n\n\t// ErrPageNotFound is returned when specifying a page above the high water mark.\n\tErrPageNotFound = errors.New(\"page not found\")\n\n\t// ErrPageFreed is returned when reading a page that has already been freed.\n\tErrPageFreed = errors.New(\"page freed\")\n)\n\n// PageHeaderSize represents the size of the bolt.page header.\nconst PageHeaderSize = 16\n\nfunc main() {\n\tm := NewMain()\n\tif err := m.Run(os.Args[1:]...); err == ErrUsage {\n\t\tos.Exit(2)\n\t} else if err != nil {\n\t\tfmt.Println(err.Error())\n\t\tos.Exit(1)\n\t}\n}\n\n// Main represents the main program execution.\ntype Main struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n}\n\n// NewMain returns a new instance of Main connect to the standard input/output.\nfunc NewMain() *Main {\n\treturn &Main{\n\t\tStdin:  os.Stdin,\n\t\tStdout: os.Stdout,\n\t\tStderr: os.Stderr,\n\t}\n}\n\n// Run executes the program.\nfunc (m *Main) Run(args ...string) error {\n\t// Require a command at the beginning.\n\tif len(args) == 0 || strings.HasPrefix(args[0], \"-\") {\n\t\tfmt.Fprintln(m.Stderr, m.Usage())\n\t\treturn ErrUsage\n\t}\n\n\t// Execute command.\n\tswitch args[0] {\n\tcase \"help\":\n\t\tfmt.Fprintln(m.Stderr, m.Usage())\n\t\treturn ErrUsage\n\tcase \"bench\":\n\t\treturn newBenchCommand(m).Run(args[1:]...)\n\tcase \"check\":\n\t\treturn newCheckCommand(m).Run(args[1:]...)\n\tcase \"compact\":\n\t\treturn newCompactCommand(m).Run(args[1:]...)\n\tcase \"dump\":\n\t\treturn newDumpCommand(m).Run(args[1:]...)\n\tcase \"info\":\n\t\treturn newInfoCommand(m).Run(args[1:]...)\n\tcase \"page\":\n\t\treturn newPageCommand(m).Run(args[1:]...)\n\tcase \"pages\":\n\t\treturn newPagesCommand(m).Run(args[1:]...)\n\tcase \"stats\":\n\t\treturn newStatsCommand(m).Run(args[1:]...)\n\tdefault:\n\t\treturn ErrUnknownCommand\n\t}\n}\n\n// Usage returns the help message.\nfunc (m *Main) Usage() string {\n\treturn strings.TrimLeft(`\nBolt is a tool for inspecting bolt databases.\n\nUsage:\n\n\tbolt command [arguments]\n\nThe commands are:\n\n    bench       run synthetic benchmark against bolt\n    check       verifies integrity of bolt database\n    compact     copies a bolt database, compacting it in the process\n    info        print basic info\n    help        print this screen\n    pages       print list of pages with their types\n    stats       iterate over all pages and generate usage stats\n\nUse \"bolt [command] -h\" for more information about a command.\n`, \"\\n\")\n}\n\n// CheckCommand represents the \"check\" command execution.\ntype CheckCommand struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n}\n\n// NewCheckCommand returns a CheckCommand.\nfunc newCheckCommand(m *Main) *CheckCommand {\n\treturn &CheckCommand{\n\t\tStdin:  m.Stdin,\n\t\tStdout: m.Stdout,\n\t\tStderr: m.Stderr,\n\t}\n}\n\n// Run executes the command.\nfunc (cmd *CheckCommand) Run(args ...string) error {\n\t// Parse flags.\n\tfs := flag.NewFlagSet(\"\", flag.ContinueOnError)\n\thelp := fs.Bool(\"h\", false, \"\")\n\tif err := fs.Parse(args); err != nil {\n\t\treturn err\n\t} else if *help {\n\t\tfmt.Fprintln(cmd.Stderr, cmd.Usage())\n\t\treturn ErrUsage\n\t}\n\n\t// Require database path.\n\tpath := fs.Arg(0)\n\tif path == \"\" {\n\t\treturn ErrPathRequired\n\t} else if _, err := os.Stat(path); os.IsNotExist(err) {\n\t\treturn ErrFileNotFound\n\t}\n\n\t// Open database.\n\tdb, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\t// Perform consistency check.\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tvar count int\n\t\tch := tx.Check()\n\tloop:\n\t\tfor {\n\t\t\tselect {\n\t\t\tcase err, ok := <-ch:\n\t\t\t\tif !ok {\n\t\t\t\t\tbreak loop\n\t\t\t\t}\n\t\t\t\tfmt.Fprintln(cmd.Stdout, err)\n\t\t\t\tcount++\n\t\t\t}\n\t\t}\n\n\t\t// Print summary of errors.\n\t\tif count > 0 {\n\t\t\tfmt.Fprintf(cmd.Stdout, \"%d errors found\\n\", count)\n\t\t\treturn ErrCorrupt\n\t\t}\n\n\t\t// Notify user that database is valid.\n\t\tfmt.Fprintln(cmd.Stdout, \"OK\")\n\t\treturn nil\n\t})\n}\n\n// Usage returns the help message.\nfunc (cmd *CheckCommand) Usage() string {\n\treturn strings.TrimLeft(`\nusage: bolt check PATH\n\nCheck opens a database at PATH and runs an exhaustive check to verify that\nall pages are accessible or are marked as freed. It also verifies that no\npages are double referenced.\n\nVerification errors will stream out as they are found and the process will\nreturn after all pages have been checked.\n`, \"\\n\")\n}\n\n// InfoCommand represents the \"info\" command execution.\ntype InfoCommand struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n}\n\n// NewInfoCommand returns a InfoCommand.\nfunc newInfoCommand(m *Main) *InfoCommand {\n\treturn &InfoCommand{\n\t\tStdin:  m.Stdin,\n\t\tStdout: m.Stdout,\n\t\tStderr: m.Stderr,\n\t}\n}\n\n// Run executes the command.\nfunc (cmd *InfoCommand) Run(args ...string) error {\n\t// Parse flags.\n\tfs := flag.NewFlagSet(\"\", flag.ContinueOnError)\n\thelp := fs.Bool(\"h\", false, \"\")\n\tif err := fs.Parse(args); err != nil {\n\t\treturn err\n\t} else if *help {\n\t\tfmt.Fprintln(cmd.Stderr, cmd.Usage())\n\t\treturn ErrUsage\n\t}\n\n\t// Require database path.\n\tpath := fs.Arg(0)\n\tif path == \"\" {\n\t\treturn ErrPathRequired\n\t} else if _, err := os.Stat(path); os.IsNotExist(err) {\n\t\treturn ErrFileNotFound\n\t}\n\n\t// Open the database.\n\tdb, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\t// Print basic database info.\n\tinfo := db.Info()\n\tfmt.Fprintf(cmd.Stdout, \"Page Size: %d\\n\", info.PageSize)\n\n\treturn nil\n}\n\n// Usage returns the help message.\nfunc (cmd *InfoCommand) Usage() string {\n\treturn strings.TrimLeft(`\nusage: bolt info PATH\n\nInfo prints basic information about the Bolt database at PATH.\n`, \"\\n\")\n}\n\n// DumpCommand represents the \"dump\" command execution.\ntype DumpCommand struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n}\n\n// newDumpCommand returns a DumpCommand.\nfunc newDumpCommand(m *Main) *DumpCommand {\n\treturn &DumpCommand{\n\t\tStdin:  m.Stdin,\n\t\tStdout: m.Stdout,\n\t\tStderr: m.Stderr,\n\t}\n}\n\n// Run executes the command.\nfunc (cmd *DumpCommand) Run(args ...string) error {\n\t// Parse flags.\n\tfs := flag.NewFlagSet(\"\", flag.ContinueOnError)\n\thelp := fs.Bool(\"h\", false, \"\")\n\tif err := fs.Parse(args); err != nil {\n\t\treturn err\n\t} else if *help {\n\t\tfmt.Fprintln(cmd.Stderr, cmd.Usage())\n\t\treturn ErrUsage\n\t}\n\n\t// Require database path and page id.\n\tpath := fs.Arg(0)\n\tif path == \"\" {\n\t\treturn ErrPathRequired\n\t} else if _, err := os.Stat(path); os.IsNotExist(err) {\n\t\treturn ErrFileNotFound\n\t}\n\n\t// Read page ids.\n\tpageIDs, err := atois(fs.Args()[1:])\n\tif err != nil {\n\t\treturn err\n\t} else if len(pageIDs) == 0 {\n\t\treturn ErrPageIDRequired\n\t}\n\n\t// Open database to retrieve page size.\n\tpageSize, err := ReadPageSize(path)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Open database file handler.\n\tf, err := os.Open(path)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() { _ = f.Close() }()\n\n\t// Print each page listed.\n\tfor i, pageID := range pageIDs {\n\t\t// Print a separator.\n\t\tif i > 0 {\n\t\t\tfmt.Fprintln(cmd.Stdout, \"===============================================\")\n\t\t}\n\n\t\t// Print page to stdout.\n\t\tif err := cmd.PrintPage(cmd.Stdout, f, pageID, pageSize); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// PrintPage prints a given page as hexadecimal.\nfunc (cmd *DumpCommand) PrintPage(w io.Writer, r io.ReaderAt, pageID int, pageSize int) error {\n\tconst bytesPerLineN = 16\n\n\t// Read page into buffer.\n\tbuf := make([]byte, pageSize)\n\taddr := pageID * pageSize\n\tif n, err := r.ReadAt(buf, int64(addr)); err != nil {\n\t\treturn err\n\t} else if n != pageSize {\n\t\treturn io.ErrUnexpectedEOF\n\t}\n\n\t// Write out to writer in 16-byte lines.\n\tvar prev []byte\n\tvar skipped bool\n\tfor offset := 0; offset < pageSize; offset += bytesPerLineN {\n\t\t// Retrieve current 16-byte line.\n\t\tline := buf[offset : offset+bytesPerLineN]\n\t\tisLastLine := (offset == (pageSize - bytesPerLineN))\n\n\t\t// If it's the same as the previous line then print a skip.\n\t\tif bytes.Equal(line, prev) && !isLastLine {\n\t\t\tif !skipped {\n\t\t\t\tfmt.Fprintf(w, \"%07x *\\n\", addr+offset)\n\t\t\t\tskipped = true\n\t\t\t}\n\t\t} else {\n\t\t\t// Print line as hexadecimal in 2-byte groups.\n\t\t\tfmt.Fprintf(w, \"%07x %04x %04x %04x %04x %04x %04x %04x %04x\\n\", addr+offset,\n\t\t\t\tline[0:2], line[2:4], line[4:6], line[6:8],\n\t\t\t\tline[8:10], line[10:12], line[12:14], line[14:16],\n\t\t\t)\n\n\t\t\tskipped = false\n\t\t}\n\n\t\t// Save the previous line.\n\t\tprev = line\n\t}\n\tfmt.Fprint(w, \"\\n\")\n\n\treturn nil\n}\n\n// Usage returns the help message.\nfunc (cmd *DumpCommand) Usage() string {\n\treturn strings.TrimLeft(`\nusage: bolt dump -page PAGEID PATH\n\nDump prints a hexadecimal dump of a single page.\n`, \"\\n\")\n}\n\n// PageCommand represents the \"page\" command execution.\ntype PageCommand struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n}\n\n// newPageCommand returns a PageCommand.\nfunc newPageCommand(m *Main) *PageCommand {\n\treturn &PageCommand{\n\t\tStdin:  m.Stdin,\n\t\tStdout: m.Stdout,\n\t\tStderr: m.Stderr,\n\t}\n}\n\n// Run executes the command.\nfunc (cmd *PageCommand) Run(args ...string) error {\n\t// Parse flags.\n\tfs := flag.NewFlagSet(\"\", flag.ContinueOnError)\n\thelp := fs.Bool(\"h\", false, \"\")\n\tif err := fs.Parse(args); err != nil {\n\t\treturn err\n\t} else if *help {\n\t\tfmt.Fprintln(cmd.Stderr, cmd.Usage())\n\t\treturn ErrUsage\n\t}\n\n\t// Require database path and page id.\n\tpath := fs.Arg(0)\n\tif path == \"\" {\n\t\treturn ErrPathRequired\n\t} else if _, err := os.Stat(path); os.IsNotExist(err) {\n\t\treturn ErrFileNotFound\n\t}\n\n\t// Read page ids.\n\tpageIDs, err := atois(fs.Args()[1:])\n\tif err != nil {\n\t\treturn err\n\t} else if len(pageIDs) == 0 {\n\t\treturn ErrPageIDRequired\n\t}\n\n\t// Open database file handler.\n\tf, err := os.Open(path)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() { _ = f.Close() }()\n\n\t// Print each page listed.\n\tfor i, pageID := range pageIDs {\n\t\t// Print a separator.\n\t\tif i > 0 {\n\t\t\tfmt.Fprintln(cmd.Stdout, \"===============================================\")\n\t\t}\n\n\t\t// Retrieve page info and page size.\n\t\tp, buf, err := ReadPage(path, pageID)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Print basic page info.\n\t\tfmt.Fprintf(cmd.Stdout, \"Page ID:    %d\\n\", p.id)\n\t\tfmt.Fprintf(cmd.Stdout, \"Page Type:  %s\\n\", p.Type())\n\t\tfmt.Fprintf(cmd.Stdout, \"Total Size: %d bytes\\n\", len(buf))\n\n\t\t// Print type-specific data.\n\t\tswitch p.Type() {\n\t\tcase \"meta\":\n\t\t\terr = cmd.PrintMeta(cmd.Stdout, buf)\n\t\tcase \"leaf\":\n\t\t\terr = cmd.PrintLeaf(cmd.Stdout, buf)\n\t\tcase \"branch\":\n\t\t\terr = cmd.PrintBranch(cmd.Stdout, buf)\n\t\tcase \"freelist\":\n\t\t\terr = cmd.PrintFreelist(cmd.Stdout, buf)\n\t\t}\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// PrintMeta prints the data from the meta page.\nfunc (cmd *PageCommand) PrintMeta(w io.Writer, buf []byte) error {\n\tm := (*meta)(unsafe.Pointer(&buf[PageHeaderSize]))\n\tfmt.Fprintf(w, \"Version:    %d\\n\", m.version)\n\tfmt.Fprintf(w, \"Page Size:  %d bytes\\n\", m.pageSize)\n\tfmt.Fprintf(w, \"Flags:      %08x\\n\", m.flags)\n\tfmt.Fprintf(w, \"Root:       <pgid=%d>\\n\", m.root.root)\n\tfmt.Fprintf(w, \"Freelist:   <pgid=%d>\\n\", m.freelist)\n\tfmt.Fprintf(w, \"HWM:        <pgid=%d>\\n\", m.pgid)\n\tfmt.Fprintf(w, \"Txn ID:     %d\\n\", m.txid)\n\tfmt.Fprintf(w, \"Checksum:   %016x\\n\", m.checksum)\n\tfmt.Fprintf(w, \"\\n\")\n\treturn nil\n}\n\n// PrintLeaf prints the data for a leaf page.\nfunc (cmd *PageCommand) PrintLeaf(w io.Writer, buf []byte) error {\n\tp := (*page)(unsafe.Pointer(&buf[0]))\n\n\t// Print number of items.\n\tfmt.Fprintf(w, \"Item Count: %d\\n\", p.count)\n\tfmt.Fprintf(w, \"\\n\")\n\n\t// Print each key/value.\n\tfor i := uint16(0); i < p.count; i++ {\n\t\te := p.leafPageElement(i)\n\n\t\t// Format key as string.\n\t\tvar k string\n\t\tif isPrintable(string(e.key())) {\n\t\t\tk = fmt.Sprintf(\"%q\", string(e.key()))\n\t\t} else {\n\t\t\tk = fmt.Sprintf(\"%x\", string(e.key()))\n\t\t}\n\n\t\t// Format value as string.\n\t\tvar v string\n\t\tif (e.flags & uint32(bucketLeafFlag)) != 0 {\n\t\t\tb := (*bucket)(unsafe.Pointer(&e.value()[0]))\n\t\t\tv = fmt.Sprintf(\"<pgid=%d,seq=%d>\", b.root, b.sequence)\n\t\t} else if isPrintable(string(e.value())) {\n\t\t\tv = fmt.Sprintf(\"%q\", string(e.value()))\n\t\t} else {\n\t\t\tv = fmt.Sprintf(\"%x\", string(e.value()))\n\t\t}\n\n\t\tfmt.Fprintf(w, \"%s: %s\\n\", k, v)\n\t}\n\tfmt.Fprintf(w, \"\\n\")\n\treturn nil\n}\n\n// PrintBranch prints the data for a leaf page.\nfunc (cmd *PageCommand) PrintBranch(w io.Writer, buf []byte) error {\n\tp := (*page)(unsafe.Pointer(&buf[0]))\n\n\t// Print number of items.\n\tfmt.Fprintf(w, \"Item Count: %d\\n\", p.count)\n\tfmt.Fprintf(w, \"\\n\")\n\n\t// Print each key/value.\n\tfor i := uint16(0); i < p.count; i++ {\n\t\te := p.branchPageElement(i)\n\n\t\t// Format key as string.\n\t\tvar k string\n\t\tif isPrintable(string(e.key())) {\n\t\t\tk = fmt.Sprintf(\"%q\", string(e.key()))\n\t\t} else {\n\t\t\tk = fmt.Sprintf(\"%x\", string(e.key()))\n\t\t}\n\n\t\tfmt.Fprintf(w, \"%s: <pgid=%d>\\n\", k, e.pgid)\n\t}\n\tfmt.Fprintf(w, \"\\n\")\n\treturn nil\n}\n\n// PrintFreelist prints the data for a freelist page.\nfunc (cmd *PageCommand) PrintFreelist(w io.Writer, buf []byte) error {\n\tp := (*page)(unsafe.Pointer(&buf[0]))\n\n\t// Print number of items.\n\tfmt.Fprintf(w, \"Item Count: %d\\n\", p.count)\n\tfmt.Fprintf(w, \"\\n\")\n\n\t// Print each page in the freelist.\n\tids := (*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr))\n\tfor i := uint16(0); i < p.count; i++ {\n\t\tfmt.Fprintf(w, \"%d\\n\", ids[i])\n\t}\n\tfmt.Fprintf(w, \"\\n\")\n\treturn nil\n}\n\n// PrintPage prints a given page as hexadecimal.\nfunc (cmd *PageCommand) PrintPage(w io.Writer, r io.ReaderAt, pageID int, pageSize int) error {\n\tconst bytesPerLineN = 16\n\n\t// Read page into buffer.\n\tbuf := make([]byte, pageSize)\n\taddr := pageID * pageSize\n\tif n, err := r.ReadAt(buf, int64(addr)); err != nil {\n\t\treturn err\n\t} else if n != pageSize {\n\t\treturn io.ErrUnexpectedEOF\n\t}\n\n\t// Write out to writer in 16-byte lines.\n\tvar prev []byte\n\tvar skipped bool\n\tfor offset := 0; offset < pageSize; offset += bytesPerLineN {\n\t\t// Retrieve current 16-byte line.\n\t\tline := buf[offset : offset+bytesPerLineN]\n\t\tisLastLine := (offset == (pageSize - bytesPerLineN))\n\n\t\t// If it's the same as the previous line then print a skip.\n\t\tif bytes.Equal(line, prev) && !isLastLine {\n\t\t\tif !skipped {\n\t\t\t\tfmt.Fprintf(w, \"%07x *\\n\", addr+offset)\n\t\t\t\tskipped = true\n\t\t\t}\n\t\t} else {\n\t\t\t// Print line as hexadecimal in 2-byte groups.\n\t\t\tfmt.Fprintf(w, \"%07x %04x %04x %04x %04x %04x %04x %04x %04x\\n\", addr+offset,\n\t\t\t\tline[0:2], line[2:4], line[4:6], line[6:8],\n\t\t\t\tline[8:10], line[10:12], line[12:14], line[14:16],\n\t\t\t)\n\n\t\t\tskipped = false\n\t\t}\n\n\t\t// Save the previous line.\n\t\tprev = line\n\t}\n\tfmt.Fprint(w, \"\\n\")\n\n\treturn nil\n}\n\n// Usage returns the help message.\nfunc (cmd *PageCommand) Usage() string {\n\treturn strings.TrimLeft(`\nusage: bolt page -page PATH pageid [pageid...]\n\nPage prints one or more pages in human readable format.\n`, \"\\n\")\n}\n\n// PagesCommand represents the \"pages\" command execution.\ntype PagesCommand struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n}\n\n// NewPagesCommand returns a PagesCommand.\nfunc newPagesCommand(m *Main) *PagesCommand {\n\treturn &PagesCommand{\n\t\tStdin:  m.Stdin,\n\t\tStdout: m.Stdout,\n\t\tStderr: m.Stderr,\n\t}\n}\n\n// Run executes the command.\nfunc (cmd *PagesCommand) Run(args ...string) error {\n\t// Parse flags.\n\tfs := flag.NewFlagSet(\"\", flag.ContinueOnError)\n\thelp := fs.Bool(\"h\", false, \"\")\n\tif err := fs.Parse(args); err != nil {\n\t\treturn err\n\t} else if *help {\n\t\tfmt.Fprintln(cmd.Stderr, cmd.Usage())\n\t\treturn ErrUsage\n\t}\n\n\t// Require database path.\n\tpath := fs.Arg(0)\n\tif path == \"\" {\n\t\treturn ErrPathRequired\n\t} else if _, err := os.Stat(path); os.IsNotExist(err) {\n\t\treturn ErrFileNotFound\n\t}\n\n\t// Open database.\n\tdb, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() { _ = db.Close() }()\n\n\t// Write header.\n\tfmt.Fprintln(cmd.Stdout, \"ID       TYPE       ITEMS  OVRFLW\")\n\tfmt.Fprintln(cmd.Stdout, \"======== ========== ====== ======\")\n\n\treturn db.Update(func(tx *bolt.Tx) error {\n\t\tvar id int\n\t\tfor {\n\t\t\tp, err := tx.Page(id)\n\t\t\tif err != nil {\n\t\t\t\treturn &PageError{ID: id, Err: err}\n\t\t\t} else if p == nil {\n\t\t\t\tbreak\n\t\t\t}\n\n\t\t\t// Only display count and overflow if this is a non-free page.\n\t\t\tvar count, overflow string\n\t\t\tif p.Type != \"free\" {\n\t\t\t\tcount = strconv.Itoa(p.Count)\n\t\t\t\tif p.OverflowCount > 0 {\n\t\t\t\t\toverflow = strconv.Itoa(p.OverflowCount)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Print table row.\n\t\t\tfmt.Fprintf(cmd.Stdout, \"%-8d %-10s %-6s %-6s\\n\", p.ID, p.Type, count, overflow)\n\n\t\t\t// Move to the next non-overflow page.\n\t\t\tid += 1\n\t\t\tif p.Type != \"free\" {\n\t\t\t\tid += p.OverflowCount\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t})\n}\n\n// Usage returns the help message.\nfunc (cmd *PagesCommand) Usage() string {\n\treturn strings.TrimLeft(`\nusage: bolt pages PATH\n\nPages prints a table of pages with their type (meta, leaf, branch, freelist).\nLeaf and branch pages will show a key count in the \"items\" column while the\nfreelist will show the number of free pages in the \"items\" column.\n\nThe \"overflow\" column shows the number of blocks that the page spills over\ninto. Normally there is no overflow but large keys and values can cause\na single page to take up multiple blocks.\n`, \"\\n\")\n}\n\n// StatsCommand represents the \"stats\" command execution.\ntype StatsCommand struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n}\n\n// NewStatsCommand returns a StatsCommand.\nfunc newStatsCommand(m *Main) *StatsCommand {\n\treturn &StatsCommand{\n\t\tStdin:  m.Stdin,\n\t\tStdout: m.Stdout,\n\t\tStderr: m.Stderr,\n\t}\n}\n\n// Run executes the command.\nfunc (cmd *StatsCommand) Run(args ...string) error {\n\t// Parse flags.\n\tfs := flag.NewFlagSet(\"\", flag.ContinueOnError)\n\thelp := fs.Bool(\"h\", false, \"\")\n\tif err := fs.Parse(args); err != nil {\n\t\treturn err\n\t} else if *help {\n\t\tfmt.Fprintln(cmd.Stderr, cmd.Usage())\n\t\treturn ErrUsage\n\t}\n\n\t// Require database path.\n\tpath, prefix := fs.Arg(0), fs.Arg(1)\n\tif path == \"\" {\n\t\treturn ErrPathRequired\n\t} else if _, err := os.Stat(path); os.IsNotExist(err) {\n\t\treturn ErrFileNotFound\n\t}\n\n\t// Open database.\n\tdb, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer db.Close()\n\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tvar s bolt.BucketStats\n\t\tvar count int\n\t\tif err := tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\tif bytes.HasPrefix(name, []byte(prefix)) {\n\t\t\t\ts.Add(b.Stats())\n\t\t\t\tcount += 1\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tfmt.Fprintf(cmd.Stdout, \"Aggregate statistics for %d buckets\\n\\n\", count)\n\n\t\tfmt.Fprintln(cmd.Stdout, \"Page count statistics\")\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tNumber of logical branch pages: %d\\n\", s.BranchPageN)\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tNumber of physical branch overflow pages: %d\\n\", s.BranchOverflowN)\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tNumber of logical leaf pages: %d\\n\", s.LeafPageN)\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tNumber of physical leaf overflow pages: %d\\n\", s.LeafOverflowN)\n\n\t\tfmt.Fprintln(cmd.Stdout, \"Tree statistics\")\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tNumber of keys/value pairs: %d\\n\", s.KeyN)\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tNumber of levels in B+tree: %d\\n\", s.Depth)\n\n\t\tfmt.Fprintln(cmd.Stdout, \"Page size utilization\")\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tBytes allocated for physical branch pages: %d\\n\", s.BranchAlloc)\n\t\tvar percentage int\n\t\tif s.BranchAlloc != 0 {\n\t\t\tpercentage = int(float32(s.BranchInuse) * 100.0 / float32(s.BranchAlloc))\n\t\t}\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tBytes actually used for branch data: %d (%d%%)\\n\", s.BranchInuse, percentage)\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tBytes allocated for physical leaf pages: %d\\n\", s.LeafAlloc)\n\t\tpercentage = 0\n\t\tif s.LeafAlloc != 0 {\n\t\t\tpercentage = int(float32(s.LeafInuse) * 100.0 / float32(s.LeafAlloc))\n\t\t}\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tBytes actually used for leaf data: %d (%d%%)\\n\", s.LeafInuse, percentage)\n\n\t\tfmt.Fprintln(cmd.Stdout, \"Bucket statistics\")\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tTotal number of buckets: %d\\n\", s.BucketN)\n\t\tpercentage = 0\n\t\tif s.BucketN != 0 {\n\t\t\tpercentage = int(float32(s.InlineBucketN) * 100.0 / float32(s.BucketN))\n\t\t}\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tTotal number on inlined buckets: %d (%d%%)\\n\", s.InlineBucketN, percentage)\n\t\tpercentage = 0\n\t\tif s.LeafInuse != 0 {\n\t\t\tpercentage = int(float32(s.InlineBucketInuse) * 100.0 / float32(s.LeafInuse))\n\t\t}\n\t\tfmt.Fprintf(cmd.Stdout, \"\\tBytes used for inlined buckets: %d (%d%%)\\n\", s.InlineBucketInuse, percentage)\n\n\t\treturn nil\n\t})\n}\n\n// Usage returns the help message.\nfunc (cmd *StatsCommand) Usage() string {\n\treturn strings.TrimLeft(`\nusage: bolt stats PATH\n\nStats performs an extensive search of the database to track every page\nreference. It starts at the current meta page and recursively iterates\nthrough every accessible bucket.\n\nThe following errors can be reported:\n\n    already freed\n        The page is referenced more than once in the freelist.\n\n    unreachable unfreed\n        The page is not referenced by a bucket or in the freelist.\n\n    reachable freed\n        The page is referenced by a bucket but is also in the freelist.\n\n    out of bounds\n        A page is referenced that is above the high water mark.\n\n    multiple references\n        A page is referenced by more than one other page.\n\n    invalid type\n        The page type is not \"meta\", \"leaf\", \"branch\", or \"freelist\".\n\nNo errors should occur in your database. However, if for some reason you\nexperience corruption, please submit a ticket to the Bolt project page:\n\n  https://github.com/boltdb/bolt/issues\n`, \"\\n\")\n}\n\nvar benchBucketName = []byte(\"bench\")\n\n// BenchCommand represents the \"bench\" command execution.\ntype BenchCommand struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n}\n\n// NewBenchCommand returns a BenchCommand using the\nfunc newBenchCommand(m *Main) *BenchCommand {\n\treturn &BenchCommand{\n\t\tStdin:  m.Stdin,\n\t\tStdout: m.Stdout,\n\t\tStderr: m.Stderr,\n\t}\n}\n\n// Run executes the \"bench\" command.\nfunc (cmd *BenchCommand) Run(args ...string) error {\n\t// Parse CLI arguments.\n\toptions, err := cmd.ParseFlags(args)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Remove path if \"-work\" is not set. Otherwise keep path.\n\tif options.Work {\n\t\tfmt.Fprintf(cmd.Stdout, \"work: %s\\n\", options.Path)\n\t} else {\n\t\tdefer os.Remove(options.Path)\n\t}\n\n\t// Create database.\n\tdb, err := bolt.Open(options.Path, 0666, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdb.NoSync = options.NoSync\n\tdefer db.Close()\n\n\t// Write to the database.\n\tvar results BenchResults\n\tif err := cmd.runWrites(db, options, &results); err != nil {\n\t\treturn fmt.Errorf(\"write: %v\", err)\n\t}\n\n\t// Read from the database.\n\tif err := cmd.runReads(db, options, &results); err != nil {\n\t\treturn fmt.Errorf(\"bench: read: %s\", err)\n\t}\n\n\t// Print results.\n\tfmt.Fprintf(os.Stderr, \"# Write\\t%v\\t(%v/op)\\t(%v op/sec)\\n\", results.WriteDuration, results.WriteOpDuration(), results.WriteOpsPerSecond())\n\tfmt.Fprintf(os.Stderr, \"# Read\\t%v\\t(%v/op)\\t(%v op/sec)\\n\", results.ReadDuration, results.ReadOpDuration(), results.ReadOpsPerSecond())\n\tfmt.Fprintln(os.Stderr, \"\")\n\treturn nil\n}\n\n// ParseFlags parses the command line flags.\nfunc (cmd *BenchCommand) ParseFlags(args []string) (*BenchOptions, error) {\n\tvar options BenchOptions\n\n\t// Parse flagset.\n\tfs := flag.NewFlagSet(\"\", flag.ContinueOnError)\n\tfs.StringVar(&options.ProfileMode, \"profile-mode\", \"rw\", \"\")\n\tfs.StringVar(&options.WriteMode, \"write-mode\", \"seq\", \"\")\n\tfs.StringVar(&options.ReadMode, \"read-mode\", \"seq\", \"\")\n\tfs.IntVar(&options.Iterations, \"count\", 1000, \"\")\n\tfs.IntVar(&options.BatchSize, \"batch-size\", 0, \"\")\n\tfs.IntVar(&options.KeySize, \"key-size\", 8, \"\")\n\tfs.IntVar(&options.ValueSize, \"value-size\", 32, \"\")\n\tfs.StringVar(&options.CPUProfile, \"cpuprofile\", \"\", \"\")\n\tfs.StringVar(&options.MemProfile, \"memprofile\", \"\", \"\")\n\tfs.StringVar(&options.BlockProfile, \"blockprofile\", \"\", \"\")\n\tfs.Float64Var(&options.FillPercent, \"fill-percent\", bolt.DefaultFillPercent, \"\")\n\tfs.BoolVar(&options.NoSync, \"no-sync\", false, \"\")\n\tfs.BoolVar(&options.Work, \"work\", false, \"\")\n\tfs.StringVar(&options.Path, \"path\", \"\", \"\")\n\tfs.SetOutput(cmd.Stderr)\n\tif err := fs.Parse(args); err != nil {\n\t\treturn nil, err\n\t}\n\n\t// Set batch size to iteration size if not set.\n\t// Require that batch size can be evenly divided by the iteration count.\n\tif options.BatchSize == 0 {\n\t\toptions.BatchSize = options.Iterations\n\t} else if options.Iterations%options.BatchSize != 0 {\n\t\treturn nil, ErrNonDivisibleBatchSize\n\t}\n\n\t// Generate temp path if one is not passed in.\n\tif options.Path == \"\" {\n\t\tf, err := ioutil.TempFile(\"\", \"bolt-bench-\")\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"temp file: %s\", err)\n\t\t}\n\t\tf.Close()\n\t\tos.Remove(f.Name())\n\t\toptions.Path = f.Name()\n\t}\n\n\treturn &options, nil\n}\n\n// Writes to the database.\nfunc (cmd *BenchCommand) runWrites(db *bolt.DB, options *BenchOptions, results *BenchResults) error {\n\t// Start profiling for writes.\n\tif options.ProfileMode == \"rw\" || options.ProfileMode == \"w\" {\n\t\tcmd.startProfiling(options)\n\t}\n\n\tt := time.Now()\n\n\tvar err error\n\tswitch options.WriteMode {\n\tcase \"seq\":\n\t\terr = cmd.runWritesSequential(db, options, results)\n\tcase \"rnd\":\n\t\terr = cmd.runWritesRandom(db, options, results)\n\tcase \"seq-nest\":\n\t\terr = cmd.runWritesSequentialNested(db, options, results)\n\tcase \"rnd-nest\":\n\t\terr = cmd.runWritesRandomNested(db, options, results)\n\tdefault:\n\t\treturn fmt.Errorf(\"invalid write mode: %s\", options.WriteMode)\n\t}\n\n\t// Save time to write.\n\tresults.WriteDuration = time.Since(t)\n\n\t// Stop profiling for writes only.\n\tif options.ProfileMode == \"w\" {\n\t\tcmd.stopProfiling()\n\t}\n\n\treturn err\n}\n\nfunc (cmd *BenchCommand) runWritesSequential(db *bolt.DB, options *BenchOptions, results *BenchResults) error {\n\tvar i = uint32(0)\n\treturn cmd.runWritesWithSource(db, options, results, func() uint32 { i++; return i })\n}\n\nfunc (cmd *BenchCommand) runWritesRandom(db *bolt.DB, options *BenchOptions, results *BenchResults) error {\n\tr := rand.New(rand.NewSource(time.Now().UnixNano()))\n\treturn cmd.runWritesWithSource(db, options, results, func() uint32 { return r.Uint32() })\n}\n\nfunc (cmd *BenchCommand) runWritesSequentialNested(db *bolt.DB, options *BenchOptions, results *BenchResults) error {\n\tvar i = uint32(0)\n\treturn cmd.runWritesWithSource(db, options, results, func() uint32 { i++; return i })\n}\n\nfunc (cmd *BenchCommand) runWritesRandomNested(db *bolt.DB, options *BenchOptions, results *BenchResults) error {\n\tr := rand.New(rand.NewSource(time.Now().UnixNano()))\n\treturn cmd.runWritesWithSource(db, options, results, func() uint32 { return r.Uint32() })\n}\n\nfunc (cmd *BenchCommand) runWritesWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error {\n\tresults.WriteOps = options.Iterations\n\n\tfor i := 0; i < options.Iterations; i += options.BatchSize {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, _ := tx.CreateBucketIfNotExists(benchBucketName)\n\t\t\tb.FillPercent = options.FillPercent\n\n\t\t\tfor j := 0; j < options.BatchSize; j++ {\n\t\t\t\tkey := make([]byte, options.KeySize)\n\t\t\t\tvalue := make([]byte, options.ValueSize)\n\n\t\t\t\t// Write key as uint32.\n\t\t\t\tbinary.BigEndian.PutUint32(key, keySource())\n\n\t\t\t\t// Insert key/value.\n\t\t\t\tif err := b.Put(key, value); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc (cmd *BenchCommand) runWritesNestedWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error {\n\tresults.WriteOps = options.Iterations\n\n\tfor i := 0; i < options.Iterations; i += options.BatchSize {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\ttop, err := tx.CreateBucketIfNotExists(benchBucketName)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\ttop.FillPercent = options.FillPercent\n\n\t\t\t// Create bucket key.\n\t\t\tname := make([]byte, options.KeySize)\n\t\t\tbinary.BigEndian.PutUint32(name, keySource())\n\n\t\t\t// Create bucket.\n\t\t\tb, err := top.CreateBucketIfNotExists(name)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tb.FillPercent = options.FillPercent\n\n\t\t\tfor j := 0; j < options.BatchSize; j++ {\n\t\t\t\tvar key = make([]byte, options.KeySize)\n\t\t\t\tvar value = make([]byte, options.ValueSize)\n\n\t\t\t\t// Generate key as uint32.\n\t\t\t\tbinary.BigEndian.PutUint32(key, keySource())\n\n\t\t\t\t// Insert value into subbucket.\n\t\t\t\tif err := b.Put(key, value); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\n// Reads from the database.\nfunc (cmd *BenchCommand) runReads(db *bolt.DB, options *BenchOptions, results *BenchResults) error {\n\t// Start profiling for reads.\n\tif options.ProfileMode == \"r\" {\n\t\tcmd.startProfiling(options)\n\t}\n\n\tt := time.Now()\n\n\tvar err error\n\tswitch options.ReadMode {\n\tcase \"seq\":\n\t\tswitch options.WriteMode {\n\t\tcase \"seq-nest\", \"rnd-nest\":\n\t\t\terr = cmd.runReadsSequentialNested(db, options, results)\n\t\tdefault:\n\t\t\terr = cmd.runReadsSequential(db, options, results)\n\t\t}\n\tdefault:\n\t\treturn fmt.Errorf(\"invalid read mode: %s\", options.ReadMode)\n\t}\n\n\t// Save read time.\n\tresults.ReadDuration = time.Since(t)\n\n\t// Stop profiling for reads.\n\tif options.ProfileMode == \"rw\" || options.ProfileMode == \"r\" {\n\t\tcmd.stopProfiling()\n\t}\n\n\treturn err\n}\n\nfunc (cmd *BenchCommand) runReadsSequential(db *bolt.DB, options *BenchOptions, results *BenchResults) error {\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tt := time.Now()\n\n\t\tfor {\n\t\t\tvar count int\n\n\t\t\tc := tx.Bucket(benchBucketName).Cursor()\n\t\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\t\tif v == nil {\n\t\t\t\t\treturn errors.New(\"invalid value\")\n\t\t\t\t}\n\t\t\t\tcount++\n\t\t\t}\n\n\t\t\tif options.WriteMode == \"seq\" && count != options.Iterations {\n\t\t\t\treturn fmt.Errorf(\"read seq: iter mismatch: expected %d, got %d\", options.Iterations, count)\n\t\t\t}\n\n\t\t\tresults.ReadOps += count\n\n\t\t\t// Make sure we do this for at least a second.\n\t\t\tif time.Since(t) >= time.Second {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n}\n\nfunc (cmd *BenchCommand) runReadsSequentialNested(db *bolt.DB, options *BenchOptions, results *BenchResults) error {\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\tt := time.Now()\n\n\t\tfor {\n\t\t\tvar count int\n\t\t\tvar top = tx.Bucket(benchBucketName)\n\t\t\tif err := top.ForEach(func(name, _ []byte) error {\n\t\t\t\tc := top.Bucket(name).Cursor()\n\t\t\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\t\t\tif v == nil {\n\t\t\t\t\t\treturn ErrInvalidValue\n\t\t\t\t\t}\n\t\t\t\t\tcount++\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\tif options.WriteMode == \"seq-nest\" && count != options.Iterations {\n\t\t\t\treturn fmt.Errorf(\"read seq-nest: iter mismatch: expected %d, got %d\", options.Iterations, count)\n\t\t\t}\n\n\t\t\tresults.ReadOps += count\n\n\t\t\t// Make sure we do this for at least a second.\n\t\t\tif time.Since(t) >= time.Second {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n}\n\n// File handlers for the various profiles.\nvar cpuprofile, memprofile, blockprofile *os.File\n\n// Starts all profiles set on the options.\nfunc (cmd *BenchCommand) startProfiling(options *BenchOptions) {\n\tvar err error\n\n\t// Start CPU profiling.\n\tif options.CPUProfile != \"\" {\n\t\tcpuprofile, err = os.Create(options.CPUProfile)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.Stderr, \"bench: could not create cpu profile %q: %v\\n\", options.CPUProfile, err)\n\t\t\tos.Exit(1)\n\t\t}\n\t\tpprof.StartCPUProfile(cpuprofile)\n\t}\n\n\t// Start memory profiling.\n\tif options.MemProfile != \"\" {\n\t\tmemprofile, err = os.Create(options.MemProfile)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.Stderr, \"bench: could not create memory profile %q: %v\\n\", options.MemProfile, err)\n\t\t\tos.Exit(1)\n\t\t}\n\t\truntime.MemProfileRate = 4096\n\t}\n\n\t// Start fatal profiling.\n\tif options.BlockProfile != \"\" {\n\t\tblockprofile, err = os.Create(options.BlockProfile)\n\t\tif err != nil {\n\t\t\tfmt.Fprintf(cmd.Stderr, \"bench: could not create block profile %q: %v\\n\", options.BlockProfile, err)\n\t\t\tos.Exit(1)\n\t\t}\n\t\truntime.SetBlockProfileRate(1)\n\t}\n}\n\n// Stops all profiles.\nfunc (cmd *BenchCommand) stopProfiling() {\n\tif cpuprofile != nil {\n\t\tpprof.StopCPUProfile()\n\t\tcpuprofile.Close()\n\t\tcpuprofile = nil\n\t}\n\n\tif memprofile != nil {\n\t\tpprof.Lookup(\"heap\").WriteTo(memprofile, 0)\n\t\tmemprofile.Close()\n\t\tmemprofile = nil\n\t}\n\n\tif blockprofile != nil {\n\t\tpprof.Lookup(\"block\").WriteTo(blockprofile, 0)\n\t\tblockprofile.Close()\n\t\tblockprofile = nil\n\t\truntime.SetBlockProfileRate(0)\n\t}\n}\n\n// BenchOptions represents the set of options that can be passed to \"bolt bench\".\ntype BenchOptions struct {\n\tProfileMode   string\n\tWriteMode     string\n\tReadMode      string\n\tIterations    int\n\tBatchSize     int\n\tKeySize       int\n\tValueSize     int\n\tCPUProfile    string\n\tMemProfile    string\n\tBlockProfile  string\n\tStatsInterval time.Duration\n\tFillPercent   float64\n\tNoSync        bool\n\tWork          bool\n\tPath          string\n}\n\n// BenchResults represents the performance results of the benchmark.\ntype BenchResults struct {\n\tWriteOps      int\n\tWriteDuration time.Duration\n\tReadOps       int\n\tReadDuration  time.Duration\n}\n\n// Returns the duration for a single write operation.\nfunc (r *BenchResults) WriteOpDuration() time.Duration {\n\tif r.WriteOps == 0 {\n\t\treturn 0\n\t}\n\treturn r.WriteDuration / time.Duration(r.WriteOps)\n}\n\n// Returns average number of write operations that can be performed per second.\nfunc (r *BenchResults) WriteOpsPerSecond() int {\n\tvar op = r.WriteOpDuration()\n\tif op == 0 {\n\t\treturn 0\n\t}\n\treturn int(time.Second) / int(op)\n}\n\n// Returns the duration for a single read operation.\nfunc (r *BenchResults) ReadOpDuration() time.Duration {\n\tif r.ReadOps == 0 {\n\t\treturn 0\n\t}\n\treturn r.ReadDuration / time.Duration(r.ReadOps)\n}\n\n// Returns average number of read operations that can be performed per second.\nfunc (r *BenchResults) ReadOpsPerSecond() int {\n\tvar op = r.ReadOpDuration()\n\tif op == 0 {\n\t\treturn 0\n\t}\n\treturn int(time.Second) / int(op)\n}\n\ntype PageError struct {\n\tID  int\n\tErr error\n}\n\nfunc (e *PageError) Error() string {\n\treturn fmt.Sprintf(\"page error: id=%d, err=%s\", e.ID, e.Err)\n}\n\n// isPrintable returns true if the string is valid unicode and contains only printable runes.\nfunc isPrintable(s string) bool {\n\tif !utf8.ValidString(s) {\n\t\treturn false\n\t}\n\tfor _, ch := range s {\n\t\tif !unicode.IsPrint(ch) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// ReadPage reads page info & full page data from a path.\n// This is not transactionally safe.\nfunc ReadPage(path string, pageID int) (*page, []byte, error) {\n\t// Find page size.\n\tpageSize, err := ReadPageSize(path)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"read page size: %s\", err)\n\t}\n\n\t// Open database file.\n\tf, err := os.Open(path)\n\tif err != nil {\n\t\treturn nil, nil, err\n\t}\n\tdefer f.Close()\n\n\t// Read one block into buffer.\n\tbuf := make([]byte, pageSize)\n\tif n, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil {\n\t\treturn nil, nil, err\n\t} else if n != len(buf) {\n\t\treturn nil, nil, io.ErrUnexpectedEOF\n\t}\n\n\t// Determine total number of blocks.\n\tp := (*page)(unsafe.Pointer(&buf[0]))\n\toverflowN := p.overflow\n\n\t// Re-read entire page (with overflow) into buffer.\n\tbuf = make([]byte, (int(overflowN)+1)*pageSize)\n\tif n, err := f.ReadAt(buf, int64(pageID*pageSize)); err != nil {\n\t\treturn nil, nil, err\n\t} else if n != len(buf) {\n\t\treturn nil, nil, io.ErrUnexpectedEOF\n\t}\n\tp = (*page)(unsafe.Pointer(&buf[0]))\n\n\treturn p, buf, nil\n}\n\n// ReadPageSize reads page size a path.\n// This is not transactionally safe.\nfunc ReadPageSize(path string) (int, error) {\n\t// Open database file.\n\tf, err := os.Open(path)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\tdefer f.Close()\n\n\t// Read 4KB chunk.\n\tbuf := make([]byte, 4096)\n\tif _, err := io.ReadFull(f, buf); err != nil {\n\t\treturn 0, err\n\t}\n\n\t// Read page size from metadata.\n\tm := (*meta)(unsafe.Pointer(&buf[PageHeaderSize]))\n\treturn int(m.pageSize), nil\n}\n\n// atois parses a slice of strings into integers.\nfunc atois(strs []string) ([]int, error) {\n\tvar a []int\n\tfor _, str := range strs {\n\t\ti, err := strconv.Atoi(str)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\ta = append(a, i)\n\t}\n\treturn a, nil\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nconst maxAllocSize = 0xFFFFFFF\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nconst (\n\tbranchPageFlag   = 0x01\n\tleafPageFlag     = 0x02\n\tmetaPageFlag     = 0x04\n\tfreelistPageFlag = 0x10\n)\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nconst bucketLeafFlag = 0x01\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\ntype pgid uint64\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\ntype txid uint64\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\ntype meta struct {\n\tmagic    uint32\n\tversion  uint32\n\tpageSize uint32\n\tflags    uint32\n\troot     bucket\n\tfreelist pgid\n\tpgid     pgid\n\ttxid     txid\n\tchecksum uint64\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\ntype bucket struct {\n\troot     pgid\n\tsequence uint64\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\ntype page struct {\n\tid       pgid\n\tflags    uint16\n\tcount    uint16\n\toverflow uint32\n\tptr      uintptr\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nfunc (p *page) Type() string {\n\tif (p.flags & branchPageFlag) != 0 {\n\t\treturn \"branch\"\n\t} else if (p.flags & leafPageFlag) != 0 {\n\t\treturn \"leaf\"\n\t} else if (p.flags & metaPageFlag) != 0 {\n\t\treturn \"meta\"\n\t} else if (p.flags & freelistPageFlag) != 0 {\n\t\treturn \"freelist\"\n\t}\n\treturn fmt.Sprintf(\"unknown<%02x>\", p.flags)\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nfunc (p *page) leafPageElement(index uint16) *leafPageElement {\n\tn := &((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[index]\n\treturn n\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nfunc (p *page) branchPageElement(index uint16) *branchPageElement {\n\treturn &((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[index]\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\ntype branchPageElement struct {\n\tpos   uint32\n\tksize uint32\n\tpgid  pgid\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nfunc (n *branchPageElement) key() []byte {\n\tbuf := (*[maxAllocSize]byte)(unsafe.Pointer(n))\n\treturn buf[n.pos : n.pos+n.ksize]\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\ntype leafPageElement struct {\n\tflags uint32\n\tpos   uint32\n\tksize uint32\n\tvsize uint32\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nfunc (n *leafPageElement) key() []byte {\n\tbuf := (*[maxAllocSize]byte)(unsafe.Pointer(n))\n\treturn buf[n.pos : n.pos+n.ksize]\n}\n\n// DO NOT EDIT. Copied from the \"bolt\" package.\nfunc (n *leafPageElement) value() []byte {\n\tbuf := (*[maxAllocSize]byte)(unsafe.Pointer(n))\n\treturn buf[n.pos+n.ksize : n.pos+n.ksize+n.vsize]\n}\n\n// CompactCommand represents the \"compact\" command execution.\ntype CompactCommand struct {\n\tStdin  io.Reader\n\tStdout io.Writer\n\tStderr io.Writer\n\n\tSrcPath   string\n\tDstPath   string\n\tTxMaxSize int64\n}\n\n// newCompactCommand returns a CompactCommand.\nfunc newCompactCommand(m *Main) *CompactCommand {\n\treturn &CompactCommand{\n\t\tStdin:  m.Stdin,\n\t\tStdout: m.Stdout,\n\t\tStderr: m.Stderr,\n\t}\n}\n\n// Run executes the command.\nfunc (cmd *CompactCommand) Run(args ...string) (err error) {\n\t// Parse flags.\n\tfs := flag.NewFlagSet(\"\", flag.ContinueOnError)\n\tfs.SetOutput(ioutil.Discard)\n\tfs.StringVar(&cmd.DstPath, \"o\", \"\", \"\")\n\tfs.Int64Var(&cmd.TxMaxSize, \"tx-max-size\", 65536, \"\")\n\tif err := fs.Parse(args); err == flag.ErrHelp {\n\t\tfmt.Fprintln(cmd.Stderr, cmd.Usage())\n\t\treturn ErrUsage\n\t} else if err != nil {\n\t\treturn err\n\t} else if cmd.DstPath == \"\" {\n\t\treturn fmt.Errorf(\"output file required\")\n\t}\n\n\t// Require database paths.\n\tcmd.SrcPath = fs.Arg(0)\n\tif cmd.SrcPath == \"\" {\n\t\treturn ErrPathRequired\n\t}\n\n\t// Ensure source file exists.\n\tfi, err := os.Stat(cmd.SrcPath)\n\tif os.IsNotExist(err) {\n\t\treturn ErrFileNotFound\n\t} else if err != nil {\n\t\treturn err\n\t}\n\tinitialSize := fi.Size()\n\n\t// Open source database.\n\tsrc, err := bolt.Open(cmd.SrcPath, 0444, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer src.Close()\n\n\t// Open destination database.\n\tdst, err := bolt.Open(cmd.DstPath, fi.Mode(), nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer dst.Close()\n\n\t// Run compaction.\n\tif err := cmd.compact(dst, src); err != nil {\n\t\treturn err\n\t}\n\n\t// Report stats on new size.\n\tfi, err = os.Stat(cmd.DstPath)\n\tif err != nil {\n\t\treturn err\n\t} else if fi.Size() == 0 {\n\t\treturn fmt.Errorf(\"zero db size\")\n\t}\n\tfmt.Fprintf(cmd.Stdout, \"%d -> %d bytes (gain=%.2fx)\\n\", initialSize, fi.Size(), float64(initialSize)/float64(fi.Size()))\n\n\treturn nil\n}\n\nfunc (cmd *CompactCommand) compact(dst, src *bolt.DB) error {\n\t// commit regularly, or we'll run out of memory for large datasets if using one transaction.\n\tvar size int64\n\ttx, err := dst.Begin(true)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer tx.Rollback()\n\n\tif err := cmd.walk(src, func(keys [][]byte, k, v []byte, seq uint64) error {\n\t\t// On each key/value, check if we have exceeded tx size.\n\t\tsz := int64(len(k) + len(v))\n\t\tif size+sz > cmd.TxMaxSize && cmd.TxMaxSize != 0 {\n\t\t\t// Commit previous transaction.\n\t\t\tif err := tx.Commit(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t// Start new transaction.\n\t\t\ttx, err = dst.Begin(true)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tsize = 0\n\t\t}\n\t\tsize += sz\n\n\t\t// Create bucket on the root transaction if this is the first level.\n\t\tnk := len(keys)\n\t\tif nk == 0 {\n\t\t\tbkt, err := tx.CreateBucket(k)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := bkt.SetSequence(seq); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\n\t\t// Create buckets on subsequent levels, if necessary.\n\t\tb := tx.Bucket(keys[0])\n\t\tif nk > 1 {\n\t\t\tfor _, k := range keys[1:] {\n\t\t\t\tb = b.Bucket(k)\n\t\t\t}\n\t\t}\n\n\t\t// If there is no value then this is a bucket call.\n\t\tif v == nil {\n\t\t\tbkt, err := b.CreateBucket(k)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := bkt.SetSequence(seq); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\n\t\t// Otherwise treat it as a key/value pair.\n\t\treturn b.Put(k, v)\n\t}); err != nil {\n\t\treturn err\n\t}\n\n\treturn tx.Commit()\n}\n\n// walkFunc is the type of the function called for keys (buckets and \"normal\"\n// values) discovered by Walk. keys is the list of keys to descend to the bucket\n// owning the discovered key/value pair k/v.\ntype walkFunc func(keys [][]byte, k, v []byte, seq uint64) error\n\n// walk walks recursively the bolt database db, calling walkFn for each key it finds.\nfunc (cmd *CompactCommand) walk(db *bolt.DB, walkFn walkFunc) error {\n\treturn db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\treturn cmd.walkBucket(b, nil, name, nil, b.Sequence(), walkFn)\n\t\t})\n\t})\n}\n\nfunc (cmd *CompactCommand) walkBucket(b *bolt.Bucket, keypath [][]byte, k, v []byte, seq uint64, fn walkFunc) error {\n\t// Execute callback.\n\tif err := fn(keypath, k, v, seq); err != nil {\n\t\treturn err\n\t}\n\n\t// If this is not a bucket then stop.\n\tif v != nil {\n\t\treturn nil\n\t}\n\n\t// Iterate over each child key/value.\n\tkeypath = append(keypath, k)\n\treturn b.ForEach(func(k, v []byte) error {\n\t\tif v == nil {\n\t\t\tbkt := b.Bucket(k)\n\t\t\treturn cmd.walkBucket(bkt, keypath, k, nil, bkt.Sequence(), fn)\n\t\t}\n\t\treturn cmd.walkBucket(b, keypath, k, v, b.Sequence(), fn)\n\t})\n}\n\n// Usage returns the help message.\nfunc (cmd *CompactCommand) Usage() string {\n\treturn strings.TrimLeft(`\nusage: bolt compact [options] -o DST SRC\n\nCompact opens a database at SRC path and walks it recursively, copying keys\nas they are found from all buckets, to a newly created database at DST path.\n\nThe original database is left untouched.\n\nAdditional options include:\n\n\t-tx-max-size NUM\n\t\tSpecifies the maximum size of individual transactions.\n\t\tDefaults to 64KB.\n`, \"\\n\")\n}\n"
  },
  {
    "path": "cmd/bolt/main_test.go",
    "content": "package main_test\n\nimport (\n\t\"bytes\"\n\tcrypto \"crypto/rand\"\n\t\"encoding/binary\"\n\t\"fmt\"\n\t\"io\"\n\t\"io/ioutil\"\n\t\"math/rand\"\n\t\"os\"\n\t\"strconv\"\n\t\"testing\"\n\n\t\"github.com/boltdb/bolt\"\n\t\"github.com/boltdb/bolt/cmd/bolt\"\n)\n\n// Ensure the \"info\" command can print information about a database.\nfunc TestInfoCommand_Run(t *testing.T) {\n\tdb := MustOpen(0666, nil)\n\tdb.DB.Close()\n\tdefer db.Close()\n\n\t// Run the info command.\n\tm := NewMain()\n\tif err := m.Run(\"info\", db.Path); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure the \"stats\" command executes correctly with an empty database.\nfunc TestStatsCommand_Run_EmptyDatabase(t *testing.T) {\n\t// Ignore\n\tif os.Getpagesize() != 4096 {\n\t\tt.Skip(\"system does not use 4KB page size\")\n\t}\n\n\tdb := MustOpen(0666, nil)\n\tdefer db.Close()\n\tdb.DB.Close()\n\n\t// Generate expected result.\n\texp := \"Aggregate statistics for 0 buckets\\n\\n\" +\n\t\t\"Page count statistics\\n\" +\n\t\t\"\\tNumber of logical branch pages: 0\\n\" +\n\t\t\"\\tNumber of physical branch overflow pages: 0\\n\" +\n\t\t\"\\tNumber of logical leaf pages: 0\\n\" +\n\t\t\"\\tNumber of physical leaf overflow pages: 0\\n\" +\n\t\t\"Tree statistics\\n\" +\n\t\t\"\\tNumber of keys/value pairs: 0\\n\" +\n\t\t\"\\tNumber of levels in B+tree: 0\\n\" +\n\t\t\"Page size utilization\\n\" +\n\t\t\"\\tBytes allocated for physical branch pages: 0\\n\" +\n\t\t\"\\tBytes actually used for branch data: 0 (0%)\\n\" +\n\t\t\"\\tBytes allocated for physical leaf pages: 0\\n\" +\n\t\t\"\\tBytes actually used for leaf data: 0 (0%)\\n\" +\n\t\t\"Bucket statistics\\n\" +\n\t\t\"\\tTotal number of buckets: 0\\n\" +\n\t\t\"\\tTotal number on inlined buckets: 0 (0%)\\n\" +\n\t\t\"\\tBytes used for inlined buckets: 0 (0%)\\n\"\n\n\t// Run the command.\n\tm := NewMain()\n\tif err := m.Run(\"stats\", db.Path); err != nil {\n\t\tt.Fatal(err)\n\t} else if m.Stdout.String() != exp {\n\t\tt.Fatalf(\"unexpected stdout:\\n\\n%s\", m.Stdout.String())\n\t}\n}\n\n// Ensure the \"stats\" command can execute correctly.\nfunc TestStatsCommand_Run(t *testing.T) {\n\t// Ignore\n\tif os.Getpagesize() != 4096 {\n\t\tt.Skip(\"system does not use 4KB page size\")\n\t}\n\n\tdb := MustOpen(0666, nil)\n\tdefer db.Close()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create \"foo\" bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"foo\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfor i := 0; i < 10; i++ {\n\t\t\tif err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\n\t\t// Create \"bar\" bucket.\n\t\tb, err = tx.CreateBucket([]byte(\"bar\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tfor i := 0; i < 100; i++ {\n\t\t\tif err := b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\n\t\t// Create \"baz\" bucket.\n\t\tb, err = tx.CreateBucket([]byte(\"baz\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"key\"), []byte(\"value\")); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdb.DB.Close()\n\n\t// Generate expected result.\n\texp := \"Aggregate statistics for 3 buckets\\n\\n\" +\n\t\t\"Page count statistics\\n\" +\n\t\t\"\\tNumber of logical branch pages: 0\\n\" +\n\t\t\"\\tNumber of physical branch overflow pages: 0\\n\" +\n\t\t\"\\tNumber of logical leaf pages: 1\\n\" +\n\t\t\"\\tNumber of physical leaf overflow pages: 0\\n\" +\n\t\t\"Tree statistics\\n\" +\n\t\t\"\\tNumber of keys/value pairs: 111\\n\" +\n\t\t\"\\tNumber of levels in B+tree: 1\\n\" +\n\t\t\"Page size utilization\\n\" +\n\t\t\"\\tBytes allocated for physical branch pages: 0\\n\" +\n\t\t\"\\tBytes actually used for branch data: 0 (0%)\\n\" +\n\t\t\"\\tBytes allocated for physical leaf pages: 4096\\n\" +\n\t\t\"\\tBytes actually used for leaf data: 1996 (48%)\\n\" +\n\t\t\"Bucket statistics\\n\" +\n\t\t\"\\tTotal number of buckets: 3\\n\" +\n\t\t\"\\tTotal number on inlined buckets: 2 (66%)\\n\" +\n\t\t\"\\tBytes used for inlined buckets: 236 (11%)\\n\"\n\n\t// Run the command.\n\tm := NewMain()\n\tif err := m.Run(\"stats\", db.Path); err != nil {\n\t\tt.Fatal(err)\n\t} else if m.Stdout.String() != exp {\n\t\tt.Fatalf(\"unexpected stdout:\\n\\n%s\", m.Stdout.String())\n\t}\n}\n\n// Main represents a test wrapper for main.Main that records output.\ntype Main struct {\n\t*main.Main\n\tStdin  bytes.Buffer\n\tStdout bytes.Buffer\n\tStderr bytes.Buffer\n}\n\n// NewMain returns a new instance of Main.\nfunc NewMain() *Main {\n\tm := &Main{Main: main.NewMain()}\n\tm.Main.Stdin = &m.Stdin\n\tm.Main.Stdout = &m.Stdout\n\tm.Main.Stderr = &m.Stderr\n\treturn m\n}\n\n// MustOpen creates a Bolt database in a temporary location.\nfunc MustOpen(mode os.FileMode, options *bolt.Options) *DB {\n\t// Create temporary path.\n\tf, _ := ioutil.TempFile(\"\", \"bolt-\")\n\tf.Close()\n\tos.Remove(f.Name())\n\n\tdb, err := bolt.Open(f.Name(), mode, options)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\treturn &DB{DB: db, Path: f.Name()}\n}\n\n// DB is a test wrapper for bolt.DB.\ntype DB struct {\n\t*bolt.DB\n\tPath string\n}\n\n// Close closes and removes the database.\nfunc (db *DB) Close() error {\n\tdefer os.Remove(db.Path)\n\treturn db.DB.Close()\n}\n\nfunc TestCompactCommand_Run(t *testing.T) {\n\tvar s int64\n\tif err := binary.Read(crypto.Reader, binary.BigEndian, &s); err != nil {\n\t\tt.Fatal(err)\n\t}\n\trand.Seed(s)\n\n\tdstdb := MustOpen(0666, nil)\n\tdstdb.Close()\n\n\t// fill the db\n\tdb := MustOpen(0666, nil)\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tn := 2 + rand.Intn(5)\n\t\tfor i := 0; i < n; i++ {\n\t\t\tk := []byte(fmt.Sprintf(\"b%d\", i))\n\t\t\tb, err := tx.CreateBucketIfNotExists(k)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := b.SetSequence(uint64(i)); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := fillBucket(b, append(k, '.')); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tdb.Close()\n\t\tt.Fatal(err)\n\t}\n\n\t// make the db grow by adding large values, and delete them.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucketIfNotExists([]byte(\"large_vals\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tn := 5 + rand.Intn(5)\n\t\tfor i := 0; i < n; i++ {\n\t\t\tv := make([]byte, 1000*1000*(1+rand.Intn(5)))\n\t\t\t_, err := crypto.Read(v)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"l%d\", i)), v); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tdb.Close()\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"large_vals\")).Cursor()\n\t\tfor k, _ := c.First(); k != nil; k, _ = c.Next() {\n\t\t\tif err := c.Delete(); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\treturn tx.DeleteBucket([]byte(\"large_vals\"))\n\t}); err != nil {\n\t\tdb.Close()\n\t\tt.Fatal(err)\n\t}\n\tdb.DB.Close()\n\tdefer db.Close()\n\tdefer dstdb.Close()\n\n\tdbChk, err := chkdb(db.Path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tm := NewMain()\n\tif err := m.Run(\"compact\", \"-o\", dstdb.Path, db.Path); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdbChkAfterCompact, err := chkdb(db.Path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdstdbChk, err := chkdb(dstdb.Path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif !bytes.Equal(dbChk, dbChkAfterCompact) {\n\t\tt.Error(\"the original db has been touched\")\n\t}\n\tif !bytes.Equal(dbChk, dstdbChk) {\n\t\tt.Error(\"the compacted db data isn't the same than the original db\")\n\t}\n}\n\nfunc fillBucket(b *bolt.Bucket, prefix []byte) error {\n\tn := 10 + rand.Intn(50)\n\tfor i := 0; i < n; i++ {\n\t\tv := make([]byte, 10*(1+rand.Intn(4)))\n\t\t_, err := crypto.Read(v)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tk := append(prefix, []byte(fmt.Sprintf(\"k%d\", i))...)\n\t\tif err := b.Put(k, v); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\t// limit depth of subbuckets\n\ts := 2 + rand.Intn(4)\n\tif len(prefix) > (2*s + 1) {\n\t\treturn nil\n\t}\n\tn = 1 + rand.Intn(3)\n\tfor i := 0; i < n; i++ {\n\t\tk := append(prefix, []byte(fmt.Sprintf(\"b%d\", i))...)\n\t\tsb, err := b.CreateBucket(k)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := fillBucket(sb, append(k, '.')); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc chkdb(path string) ([]byte, error) {\n\tdb, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer db.Close()\n\tvar buf bytes.Buffer\n\terr = db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\treturn walkBucket(b, name, nil, &buf)\n\t\t})\n\t})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn buf.Bytes(), nil\n}\n\nfunc walkBucket(parent *bolt.Bucket, k []byte, v []byte, w io.Writer) error {\n\tif _, err := fmt.Fprintf(w, \"%d:%x=%x\\n\", parent.Sequence(), k, v); err != nil {\n\t\treturn err\n\t}\n\n\t// not a bucket, exit.\n\tif v != nil {\n\t\treturn nil\n\t}\n\treturn parent.ForEach(func(k, v []byte) error {\n\t\tif v == nil {\n\t\t\treturn walkBucket(parent.Bucket(k), k, nil, w)\n\t\t}\n\t\treturn walkBucket(parent, k, v, w)\n\t})\n}\n"
  },
  {
    "path": "cursor.go",
    "content": "package bolt\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"sort\"\n)\n\n// Cursor represents an iterator that can traverse over all key/value pairs in a bucket in sorted order.\n// Cursors see nested buckets with value == nil.\n// Cursors can be obtained from a transaction and are valid as long as the transaction is open.\n//\n// Keys and values returned from the cursor are only valid for the life of the transaction.\n//\n// Changing data while traversing with a cursor may cause it to be invalidated\n// and return unexpected keys and/or values. You must reposition your cursor\n// after mutating data.\ntype Cursor struct {\n\tbucket *Bucket\n\tstack  []elemRef\n}\n\n// Bucket returns the bucket that this cursor was created from.\nfunc (c *Cursor) Bucket() *Bucket {\n\treturn c.bucket\n}\n\n// First moves the cursor to the first item in the bucket and returns its key and value.\n// If the bucket is empty then a nil key and value are returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) First() (key []byte, value []byte) {\n\t_assert(c.bucket.tx.db != nil, \"tx closed\")\n\tc.stack = c.stack[:0]\n\tp, n := c.bucket.pageNode(c.bucket.root)\n\tc.stack = append(c.stack, elemRef{page: p, node: n, index: 0})\n\tc.first()\n\n\t// If we land on an empty page then move to the next value.\n\t// https://github.com/boltdb/bolt/issues/450\n\tif c.stack[len(c.stack)-1].count() == 0 {\n\t\tc.next()\n\t}\n\n\tk, v, flags := c.keyValue()\n\tif (flags & uint32(bucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n\n}\n\n// Last moves the cursor to the last item in the bucket and returns its key and value.\n// If the bucket is empty then a nil key and value are returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) Last() (key []byte, value []byte) {\n\t_assert(c.bucket.tx.db != nil, \"tx closed\")\n\tc.stack = c.stack[:0]\n\tp, n := c.bucket.pageNode(c.bucket.root)\n\tref := elemRef{page: p, node: n}\n\tref.index = ref.count() - 1\n\tc.stack = append(c.stack, ref)\n\tc.last()\n\tk, v, flags := c.keyValue()\n\tif (flags & uint32(bucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\n// Next moves the cursor to the next item in the bucket and returns its key and value.\n// If the cursor is at the end of the bucket then a nil key and value are returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) Next() (key []byte, value []byte) {\n\t_assert(c.bucket.tx.db != nil, \"tx closed\")\n\tk, v, flags := c.next()\n\tif (flags & uint32(bucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\n// Prev moves the cursor to the previous item in the bucket and returns its key and value.\n// If the cursor is at the beginning of the bucket then a nil key and value are returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) Prev() (key []byte, value []byte) {\n\t_assert(c.bucket.tx.db != nil, \"tx closed\")\n\n\t// Attempt to move back one element until we're successful.\n\t// Move up the stack as we hit the beginning of each page in our stack.\n\tfor i := len(c.stack) - 1; i >= 0; i-- {\n\t\telem := &c.stack[i]\n\t\tif elem.index > 0 {\n\t\t\telem.index--\n\t\t\tbreak\n\t\t}\n\t\tc.stack = c.stack[:i]\n\t}\n\n\t// If we've hit the end then return nil.\n\tif len(c.stack) == 0 {\n\t\treturn nil, nil\n\t}\n\n\t// Move down the stack to find the last element of the last leaf under this branch.\n\tc.last()\n\tk, v, flags := c.keyValue()\n\tif (flags & uint32(bucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\n// Seek moves the cursor to a given key and returns it.\n// If the key does not exist then the next key is used. If no keys\n// follow, a nil key is returned.\n// The returned key and value are only valid for the life of the transaction.\nfunc (c *Cursor) Seek(seek []byte) (key []byte, value []byte) {\n\tk, v, flags := c.seek(seek)\n\n\t// If we ended up after the last element of a page then move to the next one.\n\tif ref := &c.stack[len(c.stack)-1]; ref.index >= ref.count() {\n\t\tk, v, flags = c.next()\n\t}\n\n\tif k == nil {\n\t\treturn nil, nil\n\t} else if (flags & uint32(bucketLeafFlag)) != 0 {\n\t\treturn k, nil\n\t}\n\treturn k, v\n}\n\n// Delete removes the current key/value under the cursor from the bucket.\n// Delete fails if current key/value is a bucket or if the transaction is not writable.\nfunc (c *Cursor) Delete() error {\n\tif c.bucket.tx.db == nil {\n\t\treturn ErrTxClosed\n\t} else if !c.bucket.Writable() {\n\t\treturn ErrTxNotWritable\n\t}\n\n\tkey, _, flags := c.keyValue()\n\t// Return an error if current value is a bucket.\n\tif (flags & bucketLeafFlag) != 0 {\n\t\treturn ErrIncompatibleValue\n\t}\n\tc.node().del(key)\n\n\treturn nil\n}\n\n// seek moves the cursor to a given key and returns it.\n// If the key does not exist then the next key is used.\nfunc (c *Cursor) seek(seek []byte) (key []byte, value []byte, flags uint32) {\n\t_assert(c.bucket.tx.db != nil, \"tx closed\")\n\n\t// Start from root page/node and traverse to correct page.\n\tc.stack = c.stack[:0]\n\tc.search(seek, c.bucket.root)\n\tref := &c.stack[len(c.stack)-1]\n\n\t// If the cursor is pointing to the end of page/node then return nil.\n\tif ref.index >= ref.count() {\n\t\treturn nil, nil, 0\n\t}\n\n\t// If this is a bucket then return a nil value.\n\treturn c.keyValue()\n}\n\n// first moves the cursor to the first leaf element under the last page in the stack.\nfunc (c *Cursor) first() {\n\tfor {\n\t\t// Exit when we hit a leaf page.\n\t\tvar ref = &c.stack[len(c.stack)-1]\n\t\tif ref.isLeaf() {\n\t\t\tbreak\n\t\t}\n\n\t\t// Keep adding pages pointing to the first element to the stack.\n\t\tvar pgid pgid\n\t\tif ref.node != nil {\n\t\t\tpgid = ref.node.inodes[ref.index].pgid\n\t\t} else {\n\t\t\tpgid = ref.page.branchPageElement(uint16(ref.index)).pgid\n\t\t}\n\t\tp, n := c.bucket.pageNode(pgid)\n\t\tc.stack = append(c.stack, elemRef{page: p, node: n, index: 0})\n\t}\n}\n\n// last moves the cursor to the last leaf element under the last page in the stack.\nfunc (c *Cursor) last() {\n\tfor {\n\t\t// Exit when we hit a leaf page.\n\t\tref := &c.stack[len(c.stack)-1]\n\t\tif ref.isLeaf() {\n\t\t\tbreak\n\t\t}\n\n\t\t// Keep adding pages pointing to the last element in the stack.\n\t\tvar pgid pgid\n\t\tif ref.node != nil {\n\t\t\tpgid = ref.node.inodes[ref.index].pgid\n\t\t} else {\n\t\t\tpgid = ref.page.branchPageElement(uint16(ref.index)).pgid\n\t\t}\n\t\tp, n := c.bucket.pageNode(pgid)\n\n\t\tvar nextRef = elemRef{page: p, node: n}\n\t\tnextRef.index = nextRef.count() - 1\n\t\tc.stack = append(c.stack, nextRef)\n\t}\n}\n\n// next moves to the next leaf element and returns the key and value.\n// If the cursor is at the last leaf element then it stays there and returns nil.\nfunc (c *Cursor) next() (key []byte, value []byte, flags uint32) {\n\tfor {\n\t\t// Attempt to move over one element until we're successful.\n\t\t// Move up the stack as we hit the end of each page in our stack.\n\t\tvar i int\n\t\tfor i = len(c.stack) - 1; i >= 0; i-- {\n\t\t\telem := &c.stack[i]\n\t\t\tif elem.index < elem.count()-1 {\n\t\t\t\telem.index++\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\t// If we've hit the root page then stop and return. This will leave the\n\t\t// cursor on the last element of the last page.\n\t\tif i == -1 {\n\t\t\treturn nil, nil, 0\n\t\t}\n\n\t\t// Otherwise start from where we left off in the stack and find the\n\t\t// first element of the first leaf page.\n\t\tc.stack = c.stack[:i+1]\n\t\tc.first()\n\n\t\t// If this is an empty page then restart and move back up the stack.\n\t\t// https://github.com/boltdb/bolt/issues/450\n\t\tif c.stack[len(c.stack)-1].count() == 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\treturn c.keyValue()\n\t}\n}\n\n// search recursively performs a binary search against a given page/node until it finds a given key.\nfunc (c *Cursor) search(key []byte, pgid pgid) {\n\tp, n := c.bucket.pageNode(pgid)\n\tif p != nil && (p.flags&(branchPageFlag|leafPageFlag)) == 0 {\n\t\tpanic(fmt.Sprintf(\"invalid page type: %d: %x\", p.id, p.flags))\n\t}\n\te := elemRef{page: p, node: n}\n\tc.stack = append(c.stack, e)\n\n\t// If we're on a leaf page/node then find the specific node.\n\tif e.isLeaf() {\n\t\tc.nsearch(key)\n\t\treturn\n\t}\n\n\tif n != nil {\n\t\tc.searchNode(key, n)\n\t\treturn\n\t}\n\tc.searchPage(key, p)\n}\n\nfunc (c *Cursor) searchNode(key []byte, n *node) {\n\tvar exact bool\n\tindex := sort.Search(len(n.inodes), func(i int) bool {\n\t\t// TODO(benbjohnson): Optimize this range search. It's a bit hacky right now.\n\t\t// sort.Search() finds the lowest index where f() != -1 but we need the highest index.\n\t\tret := bytes.Compare(n.inodes[i].key, key)\n\t\tif ret == 0 {\n\t\t\texact = true\n\t\t}\n\t\treturn ret != -1\n\t})\n\tif !exact && index > 0 {\n\t\tindex--\n\t}\n\tc.stack[len(c.stack)-1].index = index\n\n\t// Recursively search to the next page.\n\tc.search(key, n.inodes[index].pgid)\n}\n\nfunc (c *Cursor) searchPage(key []byte, p *page) {\n\t// Binary search for the correct range.\n\tinodes := p.branchPageElements()\n\n\tvar exact bool\n\tindex := sort.Search(int(p.count), func(i int) bool {\n\t\t// TODO(benbjohnson): Optimize this range search. It's a bit hacky right now.\n\t\t// sort.Search() finds the lowest index where f() != -1 but we need the highest index.\n\t\tret := bytes.Compare(inodes[i].key(), key)\n\t\tif ret == 0 {\n\t\t\texact = true\n\t\t}\n\t\treturn ret != -1\n\t})\n\tif !exact && index > 0 {\n\t\tindex--\n\t}\n\tc.stack[len(c.stack)-1].index = index\n\n\t// Recursively search to the next page.\n\tc.search(key, inodes[index].pgid)\n}\n\n// nsearch searches the leaf node on the top of the stack for a key.\nfunc (c *Cursor) nsearch(key []byte) {\n\te := &c.stack[len(c.stack)-1]\n\tp, n := e.page, e.node\n\n\t// If we have a node then search its inodes.\n\tif n != nil {\n\t\tindex := sort.Search(len(n.inodes), func(i int) bool {\n\t\t\treturn bytes.Compare(n.inodes[i].key, key) != -1\n\t\t})\n\t\te.index = index\n\t\treturn\n\t}\n\n\t// If we have a page then search its leaf elements.\n\tinodes := p.leafPageElements()\n\tindex := sort.Search(int(p.count), func(i int) bool {\n\t\treturn bytes.Compare(inodes[i].key(), key) != -1\n\t})\n\te.index = index\n}\n\n// keyValue returns the key and value of the current leaf element.\nfunc (c *Cursor) keyValue() ([]byte, []byte, uint32) {\n\tref := &c.stack[len(c.stack)-1]\n\tif ref.count() == 0 || ref.index >= ref.count() {\n\t\treturn nil, nil, 0\n\t}\n\n\t// Retrieve value from node.\n\tif ref.node != nil {\n\t\tinode := &ref.node.inodes[ref.index]\n\t\treturn inode.key, inode.value, inode.flags\n\t}\n\n\t// Or retrieve value from page.\n\telem := ref.page.leafPageElement(uint16(ref.index))\n\treturn elem.key(), elem.value(), elem.flags\n}\n\n// node returns the node that the cursor is currently positioned on.\nfunc (c *Cursor) node() *node {\n\t_assert(len(c.stack) > 0, \"accessing a node with a zero-length cursor stack\")\n\n\t// If the top of the stack is a leaf node then just return it.\n\tif ref := &c.stack[len(c.stack)-1]; ref.node != nil && ref.isLeaf() {\n\t\treturn ref.node\n\t}\n\n\t// Start from root and traverse down the hierarchy.\n\tvar n = c.stack[0].node\n\tif n == nil {\n\t\tn = c.bucket.node(c.stack[0].page.id, nil)\n\t}\n\tfor _, ref := range c.stack[:len(c.stack)-1] {\n\t\t_assert(!n.isLeaf, \"expected branch node\")\n\t\tn = n.childAt(int(ref.index))\n\t}\n\t_assert(n.isLeaf, \"expected leaf node\")\n\treturn n\n}\n\n// elemRef represents a reference to an element on a given page/node.\ntype elemRef struct {\n\tpage  *page\n\tnode  *node\n\tindex int\n}\n\n// isLeaf returns whether the ref is pointing at a leaf page/node.\nfunc (r *elemRef) isLeaf() bool {\n\tif r.node != nil {\n\t\treturn r.node.isLeaf\n\t}\n\treturn (r.page.flags & leafPageFlag) != 0\n}\n\n// count returns the number of inodes or page elements.\nfunc (r *elemRef) count() int {\n\tif r.node != nil {\n\t\treturn len(r.node.inodes)\n\t}\n\treturn int(r.page.count)\n}\n"
  },
  {
    "path": "cursor_test.go",
    "content": "package bolt_test\n\nimport (\n\t\"bytes\"\n\t\"encoding/binary\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\t\"reflect\"\n\t\"sort\"\n\t\"testing\"\n\t\"testing/quick\"\n\n\t\"github.com/boltdb/bolt\"\n)\n\n// Ensure that a cursor can return a reference to the bucket that created it.\nfunc TestCursor_Bucket(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif cb := b.Cursor().Bucket(); !reflect.DeepEqual(cb, b) {\n\t\t\tt.Fatal(\"cursor bucket mismatch\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can seek to the appropriate keys.\nfunc TestCursor_Seek(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"0001\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte(\"0002\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"0003\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err := b.CreateBucket([]byte(\"bkt\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\n\t\t// Exact match should go to the key.\n\t\tif k, v := c.Seek([]byte(\"bar\")); !bytes.Equal(k, []byte(\"bar\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if !bytes.Equal(v, []byte(\"0002\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\t// Inexact match should go to the next key.\n\t\tif k, v := c.Seek([]byte(\"bas\")); !bytes.Equal(k, []byte(\"baz\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if !bytes.Equal(v, []byte(\"0003\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\t// Low key should go to the first key.\n\t\tif k, v := c.Seek([]byte(\"\")); !bytes.Equal(k, []byte(\"bar\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if !bytes.Equal(v, []byte(\"0002\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\t// High key should return no key.\n\t\tif k, v := c.Seek([]byte(\"zzz\")); k != nil {\n\t\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t\t}\n\n\t\t// Buckets should return their key but no value.\n\t\tif k, v := c.Seek([]byte(\"bkt\")); !bytes.Equal(k, []byte(\"bkt\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestCursor_Delete(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tconst count = 1000\n\n\t// Insert every other key between 0 and $count.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor i := 0; i < count; i += 1 {\n\t\t\tk := make([]byte, 8)\n\t\t\tbinary.BigEndian.PutUint64(k, uint64(i))\n\t\t\tif err := b.Put(k, make([]byte, 100)); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"sub\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tbound := make([]byte, 8)\n\t\tbinary.BigEndian.PutUint64(bound, uint64(count/2))\n\t\tfor key, _ := c.First(); bytes.Compare(key, bound) < 0; key, _ = c.Next() {\n\t\t\tif err := c.Delete(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tc.Seek([]byte(\"sub\"))\n\t\tif err := c.Delete(); err != bolt.ErrIncompatibleValue {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tstats := tx.Bucket([]byte(\"widgets\")).Stats()\n\t\tif stats.KeyN != count/2+1 {\n\t\t\tt.Fatalf(\"unexpected KeyN: %d\", stats.KeyN)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can seek to the appropriate keys when there are a\n// large number of keys. This test also checks that seek will always move\n// forward to the next key.\n//\n// Related: https://github.com/boltdb/bolt/pull/187\nfunc TestCursor_Seek_Large(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar count = 10000\n\n\t// Insert every other key between 0 and $count.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < count; i += 100 {\n\t\t\tfor j := i; j < i+100; j += 2 {\n\t\t\t\tk := make([]byte, 8)\n\t\t\t\tbinary.BigEndian.PutUint64(k, uint64(j))\n\t\t\t\tif err := b.Put(k, make([]byte, 100)); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor i := 0; i < count; i++ {\n\t\t\tseek := make([]byte, 8)\n\t\t\tbinary.BigEndian.PutUint64(seek, uint64(i))\n\n\t\t\tk, _ := c.Seek(seek)\n\n\t\t\t// The last seek is beyond the end of the the range so\n\t\t\t// it should return nil.\n\t\t\tif i == count-1 {\n\t\t\t\tif k != nil {\n\t\t\t\t\tt.Fatal(\"expected nil key\")\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\t// Otherwise we should seek to the exact key or the next key.\n\t\t\tnum := binary.BigEndian.Uint64(k)\n\t\t\tif i%2 == 0 {\n\t\t\t\tif num != uint64(i) {\n\t\t\t\t\tt.Fatalf(\"unexpected num: %d\", num)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif num != uint64(i+1) {\n\t\t\t\t\tt.Fatalf(\"unexpected num: %d\", num)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a cursor can iterate over an empty bucket without error.\nfunc TestCursor_EmptyBucket(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tk, v := c.First()\n\t\tif k != nil {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can reverse iterate over an empty bucket without error.\nfunc TestCursor_EmptyBucketReverse(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tk, v := c.Last()\n\t\tif k != nil {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can iterate over a single root with a couple elements.\nfunc TestCursor_Iterate_Leaf(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte{}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte{0}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte{1}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\ttx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdefer func() { _ = tx.Rollback() }()\n\n\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\n\tk, v := c.First()\n\tif !bytes.Equal(k, []byte(\"bar\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{1}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tk, v = c.Next()\n\tif !bytes.Equal(k, []byte(\"baz\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tk, v = c.Next()\n\tif !bytes.Equal(k, []byte(\"foo\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{0}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tk, v = c.Next()\n\tif k != nil {\n\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t} else if v != nil {\n\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t}\n\n\tk, v = c.Next()\n\tif k != nil {\n\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t} else if v != nil {\n\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can iterate in reverse over a single root with a couple elements.\nfunc TestCursor_LeafRootReverse(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte{}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte{0}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte{1}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\ttx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\n\tif k, v := c.Last(); !bytes.Equal(k, []byte(\"foo\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{0}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tif k, v := c.Prev(); !bytes.Equal(k, []byte(\"baz\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tif k, v := c.Prev(); !bytes.Equal(k, []byte(\"bar\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t} else if !bytes.Equal(v, []byte{1}) {\n\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t}\n\n\tif k, v := c.Prev(); k != nil {\n\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t} else if v != nil {\n\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t}\n\n\tif k, v := c.Prev(); k != nil {\n\t\tt.Fatalf(\"expected nil key: %v\", k)\n\t} else if v != nil {\n\t\tt.Fatalf(\"expected nil value: %v\", v)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can restart from the beginning.\nfunc TestCursor_Restart(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"bar\"), []byte{}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte{}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\ttx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\n\tif k, _ := c.First(); !bytes.Equal(k, []byte(\"bar\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t}\n\tif k, _ := c.Next(); !bytes.Equal(k, []byte(\"foo\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t}\n\n\tif k, _ := c.First(); !bytes.Equal(k, []byte(\"bar\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t}\n\tif k, _ := c.Next(); !bytes.Equal(k, []byte(\"foo\")) {\n\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a cursor can skip over empty pages that have been deleted.\nfunc TestCursor_First_EmptyPages(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Create 1000 keys in the \"widgets\" bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tfor i := 0; i < 1000; i++ {\n\t\t\tif err := b.Put(u64tob(uint64(i)), []byte{}); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Delete half the keys and then try to iterate.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < 600; i++ {\n\t\t\tif err := b.Delete(u64tob(uint64(i))); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\n\t\tc := b.Cursor()\n\t\tvar n int\n\t\tfor k, _ := c.First(); k != nil; k, _ = c.Next() {\n\t\t\tn++\n\t\t}\n\t\tif n != 400 {\n\t\t\tt.Fatalf(\"unexpected key count: %d\", n)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx can iterate over all elements in a bucket.\nfunc TestCursor_QuickCheck(t *testing.T) {\n\tf := func(items testdata) bool {\n\t\tdb := MustOpenDB()\n\t\tdefer db.MustClose()\n\n\t\t// Bulk insert all values.\n\t\ttx, err := db.Begin(true)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor _, item := range items {\n\t\t\tif err := b.Put(item.Key, item.Value); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\tif err := tx.Commit(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Sort test data.\n\t\tsort.Sort(items)\n\n\t\t// Iterate over all items and check consistency.\n\t\tvar index = 0\n\t\ttx, err = db.Begin(false)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor k, v := c.First(); k != nil && index < len(items); k, v = c.Next() {\n\t\t\tif !bytes.Equal(k, items[index].Key) {\n\t\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t\t} else if !bytes.Equal(v, items[index].Value) {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t\tindex++\n\t\t}\n\t\tif len(items) != index {\n\t\t\tt.Fatalf(\"unexpected item count: %v, expected %v\", len(items), index)\n\t\t}\n\n\t\tif err := tx.Rollback(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn true\n\t}\n\tif err := quick.Check(f, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\n// Ensure that a transaction can iterate over all elements in a bucket in reverse.\nfunc TestCursor_QuickCheck_Reverse(t *testing.T) {\n\tf := func(items testdata) bool {\n\t\tdb := MustOpenDB()\n\t\tdefer db.MustClose()\n\n\t\t// Bulk insert all values.\n\t\ttx, err := db.Begin(true)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tfor _, item := range items {\n\t\t\tif err := b.Put(item.Key, item.Value); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\tif err := tx.Commit(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\t// Sort test data.\n\t\tsort.Sort(revtestdata(items))\n\n\t\t// Iterate over all items and check consistency.\n\t\tvar index = 0\n\t\ttx, err = db.Begin(false)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor k, v := c.Last(); k != nil && index < len(items); k, v = c.Prev() {\n\t\t\tif !bytes.Equal(k, items[index].Key) {\n\t\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t\t} else if !bytes.Equal(v, items[index].Value) {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t\tindex++\n\t\t}\n\t\tif len(items) != index {\n\t\t\tt.Fatalf(\"unexpected item count: %v, expected %v\", len(items), index)\n\t\t}\n\n\t\tif err := tx.Rollback(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\treturn true\n\t}\n\tif err := quick.Check(f, qconfig()); err != nil {\n\t\tt.Error(err)\n\t}\n}\n\n// Ensure that a Tx cursor can iterate over subbuckets.\nfunc TestCursor_QuickCheck_BucketsOnly(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"baz\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvar names []string\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\tnames = append(names, string(k))\n\t\t\tif v != nil {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t}\n\t\tif !reflect.DeepEqual(names, []string{\"bar\", \"baz\", \"foo\"}) {\n\t\t\tt.Fatalf(\"unexpected names: %+v\", names)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx cursor can reverse iterate over subbuckets.\nfunc TestCursor_QuickCheck_BucketsOnly_Reverse(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif _, err := b.CreateBucket([]byte(\"baz\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvar names []string\n\t\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\t\tfor k, v := c.Last(); k != nil; k, v = c.Prev() {\n\t\t\tnames = append(names, string(k))\n\t\t\tif v != nil {\n\t\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t\t}\n\t\t}\n\t\tif !reflect.DeepEqual(names, []string{\"foo\", \"baz\", \"bar\"}) {\n\t\t\tt.Fatalf(\"unexpected names: %+v\", names)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc ExampleCursor() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Start a read-write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a new bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"animals\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Insert data into a bucket.\n\t\tif err := b.Put([]byte(\"dog\"), []byte(\"fun\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"cat\"), []byte(\"lame\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"liger\"), []byte(\"awesome\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\n\t\t// Create a cursor for iteration.\n\t\tc := b.Cursor()\n\n\t\t// Iterate over items in sorted key order. This starts from the\n\t\t// first key/value pair and updates the k/v variables to the\n\t\t// next key/value on each iteration.\n\t\t//\n\t\t// The loop finishes at the end of the cursor when a nil key is returned.\n\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\tfmt.Printf(\"A %s is %s.\\n\", k, v)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// A cat is lame.\n\t// A dog is fun.\n\t// A liger is awesome.\n}\n\nfunc ExampleCursor_reverse() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Start a read-write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create a new bucket.\n\t\tb, err := tx.CreateBucket([]byte(\"animals\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Insert data into a bucket.\n\t\tif err := b.Put([]byte(\"dog\"), []byte(\"fun\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"cat\"), []byte(\"lame\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"liger\"), []byte(\"awesome\")); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\n\t\t// Create a cursor for iteration.\n\t\tc := b.Cursor()\n\n\t\t// Iterate over items in reverse sorted key order. This starts\n\t\t// from the last key/value pair and updates the k/v variables to\n\t\t// the previous key/value on each iteration.\n\t\t//\n\t\t// The loop finishes at the beginning of the cursor when a nil key\n\t\t// is returned.\n\t\tfor k, v := c.Last(); k != nil; k, v = c.Prev() {\n\t\t\tfmt.Printf(\"A %s is %s.\\n\", k, v)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close the database to release the file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// A liger is awesome.\n\t// A dog is fun.\n\t// A cat is lame.\n}\n"
  },
  {
    "path": "db.go",
    "content": "package bolt\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"hash/fnv\"\n\t\"log\"\n\t\"os\"\n\t\"runtime\"\n\t\"runtime/debug\"\n\t\"strings\"\n\t\"sync\"\n\t\"time\"\n\t\"unsafe\"\n)\n\n// The largest step that can be taken when remapping the mmap.\nconst maxMmapStep = 1 << 30 // 1GB\n\n// The data file format version.\nconst version = 2\n\n// Represents a marker value to indicate that a file is a Bolt DB.\nconst magic uint32 = 0xED0CDAED\n\n// IgnoreNoSync specifies whether the NoSync field of a DB is ignored when\n// syncing changes to a file.  This is required as some operating systems,\n// such as OpenBSD, do not have a unified buffer cache (UBC) and writes\n// must be synchronized using the msync(2) syscall.\nconst IgnoreNoSync = runtime.GOOS == \"openbsd\"\n\n// Default values if not set in a DB instance.\nconst (\n\tDefaultMaxBatchSize  int = 1000\n\tDefaultMaxBatchDelay     = 10 * time.Millisecond\n\tDefaultAllocSize         = 16 * 1024 * 1024\n)\n\n// default page size for db is set to the OS page size.\nvar defaultPageSize = os.Getpagesize()\n\n// DB represents a collection of buckets persisted to a file on disk.\n// All data access is performed through transactions which can be obtained through the DB.\n// All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.\ntype DB struct {\n\t// When enabled, the database will perform a Check() after every commit.\n\t// A panic is issued if the database is in an inconsistent state. This\n\t// flag has a large performance impact so it should only be used for\n\t// debugging purposes.\n\tStrictMode bool\n\n\t// Setting the NoSync flag will cause the database to skip fsync()\n\t// calls after each commit. This can be useful when bulk loading data\n\t// into a database and you can restart the bulk load in the event of\n\t// a system failure or database corruption. Do not set this flag for\n\t// normal use.\n\t//\n\t// If the package global IgnoreNoSync constant is true, this value is\n\t// ignored.  See the comment on that constant for more details.\n\t//\n\t// THIS IS UNSAFE. PLEASE USE WITH CAUTION.\n\tNoSync bool\n\n\t// When true, skips the truncate call when growing the database.\n\t// Setting this to true is only safe on non-ext3/ext4 systems.\n\t// Skipping truncation avoids preallocation of hard drive space and\n\t// bypasses a truncate() and fsync() syscall on remapping.\n\t//\n\t// https://github.com/boltdb/bolt/issues/284\n\tNoGrowSync bool\n\n\t// If you want to read the entire database fast, you can set MmapFlag to\n\t// syscall.MAP_POPULATE on Linux 2.6.23+ for sequential read-ahead.\n\tMmapFlags int\n\n\t// MaxBatchSize is the maximum size of a batch. Default value is\n\t// copied from DefaultMaxBatchSize in Open.\n\t//\n\t// If <=0, disables batching.\n\t//\n\t// Do not change concurrently with calls to Batch.\n\tMaxBatchSize int\n\n\t// MaxBatchDelay is the maximum delay before a batch starts.\n\t// Default value is copied from DefaultMaxBatchDelay in Open.\n\t//\n\t// If <=0, effectively disables batching.\n\t//\n\t// Do not change concurrently with calls to Batch.\n\tMaxBatchDelay time.Duration\n\n\t// AllocSize is the amount of space allocated when the database\n\t// needs to create new pages. This is done to amortize the cost\n\t// of truncate() and fsync() when growing the data file.\n\tAllocSize int\n\n\tpath     string\n\tfile     *os.File\n\tlockfile *os.File // windows only\n\tdataref  []byte   // mmap'ed readonly, write throws SEGV\n\tdata     *[maxMapSize]byte\n\tdatasz   int\n\tfilesz   int // current on disk file size\n\tmeta0    *meta\n\tmeta1    *meta\n\tpageSize int\n\topened   bool\n\trwtx     *Tx\n\ttxs      []*Tx\n\tfreelist *freelist\n\tstats    Stats\n\n\tpagePool sync.Pool\n\n\tbatchMu sync.Mutex\n\tbatch   *batch\n\n\trwlock   sync.Mutex   // Allows only one writer at a time.\n\tmetalock sync.Mutex   // Protects meta page access.\n\tmmaplock sync.RWMutex // Protects mmap access during remapping.\n\tstatlock sync.RWMutex // Protects stats access.\n\n\tops struct {\n\t\twriteAt func(b []byte, off int64) (n int, err error)\n\t}\n\n\t// Read only mode.\n\t// When true, Update() and Begin(true) return ErrDatabaseReadOnly immediately.\n\treadOnly bool\n}\n\n// Path returns the path to currently open database file.\nfunc (db *DB) Path() string {\n\treturn db.path\n}\n\n// GoString returns the Go string representation of the database.\nfunc (db *DB) GoString() string {\n\treturn fmt.Sprintf(\"bolt.DB{path:%q}\", db.path)\n}\n\n// String returns the string representation of the database.\nfunc (db *DB) String() string {\n\treturn fmt.Sprintf(\"DB<%q>\", db.path)\n}\n\n// Open creates and opens a database at the given path.\n// If the file does not exist then it will be created automatically.\n// Passing in nil options will cause Bolt to open the database with the default options.\nfunc Open(path string, mode os.FileMode, options *Options) (*DB, error) {\n\tvar db = &DB{opened: true}\n\n\t// Set default options if no options are provided.\n\tif options == nil {\n\t\toptions = DefaultOptions\n\t}\n\tdb.NoGrowSync = options.NoGrowSync\n\tdb.MmapFlags = options.MmapFlags\n\n\t// Set default values for later DB operations.\n\tdb.MaxBatchSize = DefaultMaxBatchSize\n\tdb.MaxBatchDelay = DefaultMaxBatchDelay\n\tdb.AllocSize = DefaultAllocSize\n\n\tflag := os.O_RDWR\n\tif options.ReadOnly {\n\t\tflag = os.O_RDONLY\n\t\tdb.readOnly = true\n\t}\n\n\t// Open data file and separate sync handler for metadata writes.\n\tdb.path = path\n\tvar err error\n\tif db.file, err = os.OpenFile(db.path, flag|os.O_CREATE, mode); err != nil {\n\t\t_ = db.close()\n\t\treturn nil, err\n\t}\n\n\t// Lock file so that other processes using Bolt in read-write mode cannot\n\t// use the database  at the same time. This would cause corruption since\n\t// the two processes would write meta pages and free pages separately.\n\t// The database file is locked exclusively (only one process can grab the lock)\n\t// if !options.ReadOnly.\n\t// The database file is locked using the shared lock (more than one process may\n\t// hold a lock at the same time) otherwise (options.ReadOnly is set).\n\tif err := flock(db, mode, !db.readOnly, options.Timeout); err != nil {\n\t\t_ = db.close()\n\t\treturn nil, err\n\t}\n\n\t// Default values for test hooks\n\tdb.ops.writeAt = db.file.WriteAt\n\n\t// Initialize the database if it doesn't exist.\n\tif info, err := db.file.Stat(); err != nil {\n\t\treturn nil, err\n\t} else if info.Size() == 0 {\n\t\t// Initialize new files with meta pages.\n\t\tif err := db.init(); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t} else {\n\t\t// Read the first meta page to determine the page size.\n\t\tvar buf [0x1000]byte\n\t\tif _, err := db.file.ReadAt(buf[:], 0); err == nil {\n\t\t\tm := db.pageInBuffer(buf[:], 0).meta()\n\t\t\tif err := m.validate(); err != nil {\n\t\t\t\t// If we can't read the page size, we can assume it's the same\n\t\t\t\t// as the OS -- since that's how the page size was chosen in the\n\t\t\t\t// first place.\n\t\t\t\t//\n\t\t\t\t// If the first page is invalid and this OS uses a different\n\t\t\t\t// page size than what the database was created with then we\n\t\t\t\t// are out of luck and cannot access the database.\n\t\t\t\tdb.pageSize = os.Getpagesize()\n\t\t\t} else {\n\t\t\t\tdb.pageSize = int(m.pageSize)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Initialize page pool.\n\tdb.pagePool = sync.Pool{\n\t\tNew: func() interface{} {\n\t\t\treturn make([]byte, db.pageSize)\n\t\t},\n\t}\n\n\t// Memory map the data file.\n\tif err := db.mmap(options.InitialMmapSize); err != nil {\n\t\t_ = db.close()\n\t\treturn nil, err\n\t}\n\n\t// Read in the freelist.\n\tdb.freelist = newFreelist()\n\tdb.freelist.read(db.page(db.meta().freelist))\n\n\t// Mark the database as opened and return.\n\treturn db, nil\n}\n\n// mmap opens the underlying memory-mapped file and initializes the meta references.\n// minsz is the minimum size that the new mmap can be.\nfunc (db *DB) mmap(minsz int) error {\n\tdb.mmaplock.Lock()\n\tdefer db.mmaplock.Unlock()\n\n\tinfo, err := db.file.Stat()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"mmap stat error: %s\", err)\n\t} else if int(info.Size()) < db.pageSize*2 {\n\t\treturn fmt.Errorf(\"file size too small\")\n\t}\n\n\t// Ensure the size is at least the minimum size.\n\tvar size = int(info.Size())\n\tif size < minsz {\n\t\tsize = minsz\n\t}\n\tsize, err = db.mmapSize(size)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Dereference all mmap references before unmapping.\n\tif db.rwtx != nil {\n\t\tdb.rwtx.root.dereference()\n\t}\n\n\t// Unmap existing data before continuing.\n\tif err := db.munmap(); err != nil {\n\t\treturn err\n\t}\n\n\t// Memory-map the data file as a byte slice.\n\tif err := mmap(db, size); err != nil {\n\t\treturn err\n\t}\n\n\t// Save references to the meta pages.\n\tdb.meta0 = db.page(0).meta()\n\tdb.meta1 = db.page(1).meta()\n\n\t// Validate the meta pages. We only return an error if both meta pages fail\n\t// validation, since meta0 failing validation means that it wasn't saved\n\t// properly -- but we can recover using meta1. And vice-versa.\n\terr0 := db.meta0.validate()\n\terr1 := db.meta1.validate()\n\tif err0 != nil && err1 != nil {\n\t\treturn err0\n\t}\n\n\treturn nil\n}\n\n// munmap unmaps the data file from memory.\nfunc (db *DB) munmap() error {\n\tif err := munmap(db); err != nil {\n\t\treturn fmt.Errorf(\"unmap error: \" + err.Error())\n\t}\n\treturn nil\n}\n\n// mmapSize determines the appropriate size for the mmap given the current size\n// of the database. The minimum size is 32KB and doubles until it reaches 1GB.\n// Returns an error if the new mmap size is greater than the max allowed.\nfunc (db *DB) mmapSize(size int) (int, error) {\n\t// Double the size from 32KB until 1GB.\n\tfor i := uint(15); i <= 30; i++ {\n\t\tif size <= 1<<i {\n\t\t\treturn 1 << i, nil\n\t\t}\n\t}\n\n\t// Verify the requested size is not above the maximum allowed.\n\tif size > maxMapSize {\n\t\treturn 0, fmt.Errorf(\"mmap too large\")\n\t}\n\n\t// If larger than 1GB then grow by 1GB at a time.\n\tsz := int64(size)\n\tif remainder := sz % int64(maxMmapStep); remainder > 0 {\n\t\tsz += int64(maxMmapStep) - remainder\n\t}\n\n\t// Ensure that the mmap size is a multiple of the page size.\n\t// This should always be true since we're incrementing in MBs.\n\tpageSize := int64(db.pageSize)\n\tif (sz % pageSize) != 0 {\n\t\tsz = ((sz / pageSize) + 1) * pageSize\n\t}\n\n\t// If we've exceeded the max size then only grow up to the max size.\n\tif sz > maxMapSize {\n\t\tsz = maxMapSize\n\t}\n\n\treturn int(sz), nil\n}\n\n// init creates a new database file and initializes its meta pages.\nfunc (db *DB) init() error {\n\t// Set the page size to the OS page size.\n\tdb.pageSize = os.Getpagesize()\n\n\t// Create two meta pages on a buffer.\n\tbuf := make([]byte, db.pageSize*4)\n\tfor i := 0; i < 2; i++ {\n\t\tp := db.pageInBuffer(buf[:], pgid(i))\n\t\tp.id = pgid(i)\n\t\tp.flags = metaPageFlag\n\n\t\t// Initialize the meta page.\n\t\tm := p.meta()\n\t\tm.magic = magic\n\t\tm.version = version\n\t\tm.pageSize = uint32(db.pageSize)\n\t\tm.freelist = 2\n\t\tm.root = bucket{root: 3}\n\t\tm.pgid = 4\n\t\tm.txid = txid(i)\n\t\tm.checksum = m.sum64()\n\t}\n\n\t// Write an empty freelist at page 3.\n\tp := db.pageInBuffer(buf[:], pgid(2))\n\tp.id = pgid(2)\n\tp.flags = freelistPageFlag\n\tp.count = 0\n\n\t// Write an empty leaf page at page 4.\n\tp = db.pageInBuffer(buf[:], pgid(3))\n\tp.id = pgid(3)\n\tp.flags = leafPageFlag\n\tp.count = 0\n\n\t// Write the buffer to our data file.\n\tif _, err := db.ops.writeAt(buf, 0); err != nil {\n\t\treturn err\n\t}\n\tif err := fdatasync(db); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// Close releases all database resources.\n// All transactions must be closed before closing the database.\nfunc (db *DB) Close() error {\n\tdb.rwlock.Lock()\n\tdefer db.rwlock.Unlock()\n\n\tdb.metalock.Lock()\n\tdefer db.metalock.Unlock()\n\n\tdb.mmaplock.RLock()\n\tdefer db.mmaplock.RUnlock()\n\n\treturn db.close()\n}\n\nfunc (db *DB) close() error {\n\tif !db.opened {\n\t\treturn nil\n\t}\n\n\tdb.opened = false\n\n\tdb.freelist = nil\n\n\t// Clear ops.\n\tdb.ops.writeAt = nil\n\n\t// Close the mmap.\n\tif err := db.munmap(); err != nil {\n\t\treturn err\n\t}\n\n\t// Close file handles.\n\tif db.file != nil {\n\t\t// No need to unlock read-only file.\n\t\tif !db.readOnly {\n\t\t\t// Unlock the file.\n\t\t\tif err := funlock(db); err != nil {\n\t\t\t\tlog.Printf(\"bolt.Close(): funlock error: %s\", err)\n\t\t\t}\n\t\t}\n\n\t\t// Close the file descriptor.\n\t\tif err := db.file.Close(); err != nil {\n\t\t\treturn fmt.Errorf(\"db file close: %s\", err)\n\t\t}\n\t\tdb.file = nil\n\t}\n\n\tdb.path = \"\"\n\treturn nil\n}\n\n// Begin starts a new transaction.\n// Multiple read-only transactions can be used concurrently but only one\n// write transaction can be used at a time. Starting multiple write transactions\n// will cause the calls to block and be serialized until the current write\n// transaction finishes.\n//\n// Transactions should not be dependent on one another. Opening a read\n// transaction and a write transaction in the same goroutine can cause the\n// writer to deadlock because the database periodically needs to re-mmap itself\n// as it grows and it cannot do that while a read transaction is open.\n//\n// If a long running read transaction (for example, a snapshot transaction) is\n// needed, you might want to set DB.InitialMmapSize to a large enough value\n// to avoid potential blocking of write transaction.\n//\n// IMPORTANT: You must close read-only transactions after you are finished or\n// else the database will not reclaim old pages.\nfunc (db *DB) Begin(writable bool) (*Tx, error) {\n\tif writable {\n\t\treturn db.beginRWTx()\n\t}\n\treturn db.beginTx()\n}\n\nfunc (db *DB) beginTx() (*Tx, error) {\n\t// Lock the meta pages while we initialize the transaction. We obtain\n\t// the meta lock before the mmap lock because that's the order that the\n\t// write transaction will obtain them.\n\tdb.metalock.Lock()\n\n\t// Obtain a read-only lock on the mmap. When the mmap is remapped it will\n\t// obtain a write lock so all transactions must finish before it can be\n\t// remapped.\n\tdb.mmaplock.RLock()\n\n\t// Exit if the database is not open yet.\n\tif !db.opened {\n\t\tdb.mmaplock.RUnlock()\n\t\tdb.metalock.Unlock()\n\t\treturn nil, ErrDatabaseNotOpen\n\t}\n\n\t// Create a transaction associated with the database.\n\tt := &Tx{}\n\tt.init(db)\n\n\t// Keep track of transaction until it closes.\n\tdb.txs = append(db.txs, t)\n\tn := len(db.txs)\n\n\t// Unlock the meta pages.\n\tdb.metalock.Unlock()\n\n\t// Update the transaction stats.\n\tdb.statlock.Lock()\n\tdb.stats.TxN++\n\tdb.stats.OpenTxN = n\n\tdb.statlock.Unlock()\n\n\treturn t, nil\n}\n\nfunc (db *DB) beginRWTx() (*Tx, error) {\n\t// If the database was opened with Options.ReadOnly, return an error.\n\tif db.readOnly {\n\t\treturn nil, ErrDatabaseReadOnly\n\t}\n\n\t// Obtain writer lock. This is released by the transaction when it closes.\n\t// This enforces only one writer transaction at a time.\n\tdb.rwlock.Lock()\n\n\t// Once we have the writer lock then we can lock the meta pages so that\n\t// we can set up the transaction.\n\tdb.metalock.Lock()\n\tdefer db.metalock.Unlock()\n\n\t// Exit if the database is not open yet.\n\tif !db.opened {\n\t\tdb.rwlock.Unlock()\n\t\treturn nil, ErrDatabaseNotOpen\n\t}\n\n\t// Create a transaction associated with the database.\n\tt := &Tx{writable: true}\n\tt.init(db)\n\tdb.rwtx = t\n\n\t// Free any pages associated with closed read-only transactions.\n\tvar minid txid = 0xFFFFFFFFFFFFFFFF\n\tfor _, t := range db.txs {\n\t\tif t.meta.txid < minid {\n\t\t\tminid = t.meta.txid\n\t\t}\n\t}\n\tif minid > 0 {\n\t\tdb.freelist.release(minid - 1)\n\t}\n\n\treturn t, nil\n}\n\n// removeTx removes a transaction from the database.\nfunc (db *DB) removeTx(tx *Tx) {\n\t// Release the read lock on the mmap.\n\tdb.mmaplock.RUnlock()\n\n\t// Use the meta lock to restrict access to the DB object.\n\tdb.metalock.Lock()\n\n\t// Remove the transaction.\n\tfor i, t := range db.txs {\n\t\tif t == tx {\n\t\t\tlast := len(db.txs) - 1\n\t\t\tdb.txs[i] = db.txs[last]\n\t\t\tdb.txs[last] = nil\n\t\t\tdb.txs = db.txs[:last]\n\t\t\tbreak\n\t\t}\n\t}\n\tn := len(db.txs)\n\n\t// Unlock the meta pages.\n\tdb.metalock.Unlock()\n\n\t// Merge statistics.\n\tdb.statlock.Lock()\n\tdb.stats.OpenTxN = n\n\tdb.stats.TxStats.add(&tx.stats)\n\tdb.statlock.Unlock()\n}\n\n// Update executes a function within the context of a read-write managed transaction.\n// If no error is returned from the function then the transaction is committed.\n// If an error is returned then the entire transaction is rolled back.\n// Any error that is returned from the function or returned from the commit is\n// returned from the Update() method.\n//\n// Attempting to manually commit or rollback within the function will cause a panic.\nfunc (db *DB) Update(fn func(*Tx) error) error {\n\tt, err := db.Begin(true)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Make sure the transaction rolls back in the event of a panic.\n\tdefer func() {\n\t\tif t.db != nil {\n\t\t\tt.rollback()\n\t\t}\n\t}()\n\n\t// Mark as a managed tx so that the inner function cannot manually commit.\n\tt.managed = true\n\n\t// If an error is returned from the function then rollback and return error.\n\terr = fn(t)\n\tt.managed = false\n\tif err != nil {\n\t\t_ = t.Rollback()\n\t\treturn err\n\t}\n\n\treturn t.Commit()\n}\n\n// View executes a function within the context of a managed read-only transaction.\n// Any error that is returned from the function is returned from the View() method.\n//\n// Attempting to manually rollback within the function will cause a panic.\nfunc (db *DB) View(fn func(*Tx) error) error {\n\tt, err := db.Begin(false)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Make sure the transaction rolls back in the event of a panic.\n\tdefer func() {\n\t\tif t.db != nil {\n\t\t\tt.rollback()\n\t\t}\n\t}()\n\n\t// Mark as a managed tx so that the inner function cannot manually rollback.\n\tt.managed = true\n\n\t// If an error is returned from the function then pass it through.\n\terr = fn(t)\n\tt.managed = false\n\tif err != nil {\n\t\t_ = t.Rollback()\n\t\treturn err\n\t}\n\n\tif err := t.Rollback(); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// Batch calls fn as part of a batch. It behaves similar to Update,\n// except:\n//\n// 1. concurrent Batch calls can be combined into a single Bolt\n// transaction.\n//\n// 2. the function passed to Batch may be called multiple times,\n// regardless of whether it returns error or not.\n//\n// This means that Batch function side effects must be idempotent and\n// take permanent effect only after a successful return is seen in\n// caller.\n//\n// The maximum batch size and delay can be adjusted with DB.MaxBatchSize\n// and DB.MaxBatchDelay, respectively.\n//\n// Batch is only useful when there are multiple goroutines calling it.\nfunc (db *DB) Batch(fn func(*Tx) error) error {\n\terrCh := make(chan error, 1)\n\n\tdb.batchMu.Lock()\n\tif (db.batch == nil) || (db.batch != nil && len(db.batch.calls) >= db.MaxBatchSize) {\n\t\t// There is no existing batch, or the existing batch is full; start a new one.\n\t\tdb.batch = &batch{\n\t\t\tdb: db,\n\t\t}\n\t\tdb.batch.timer = time.AfterFunc(db.MaxBatchDelay, db.batch.trigger)\n\t}\n\tdb.batch.calls = append(db.batch.calls, call{fn: fn, err: errCh})\n\tif len(db.batch.calls) >= db.MaxBatchSize {\n\t\t// wake up batch, it's ready to run\n\t\tgo db.batch.trigger()\n\t}\n\tdb.batchMu.Unlock()\n\n\terr := <-errCh\n\tif err == trySolo {\n\t\terr = db.Update(fn)\n\t}\n\treturn err\n}\n\ntype call struct {\n\tfn  func(*Tx) error\n\terr chan<- error\n}\n\ntype batch struct {\n\tdb    *DB\n\ttimer *time.Timer\n\tstart sync.Once\n\tcalls []call\n}\n\n// trigger runs the batch if it hasn't already been run.\nfunc (b *batch) trigger() {\n\tb.start.Do(b.run)\n}\n\n// run performs the transactions in the batch and communicates results\n// back to DB.Batch.\nfunc (b *batch) run() {\n\tb.db.batchMu.Lock()\n\tb.timer.Stop()\n\t// Make sure no new work is added to this batch, but don't break\n\t// other batches.\n\tif b.db.batch == b {\n\t\tb.db.batch = nil\n\t}\n\tb.db.batchMu.Unlock()\n\nretry:\n\tfor len(b.calls) > 0 {\n\t\tvar failIdx = -1\n\t\terr := b.db.Update(func(tx *Tx) error {\n\t\t\tfor i, c := range b.calls {\n\t\t\t\tif err := safelyCall(c.fn, tx); err != nil {\n\t\t\t\t\tfailIdx = i\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t})\n\n\t\tif failIdx >= 0 {\n\t\t\t// take the failing transaction out of the batch. it's\n\t\t\t// safe to shorten b.calls here because db.batch no longer\n\t\t\t// points to us, and we hold the mutex anyway.\n\t\t\tc := b.calls[failIdx]\n\t\t\tb.calls[failIdx], b.calls = b.calls[len(b.calls)-1], b.calls[:len(b.calls)-1]\n\t\t\t// tell the submitter re-run it solo, continue with the rest of the batch\n\t\t\tc.err <- trySolo\n\t\t\tcontinue retry\n\t\t}\n\n\t\t// pass success, or bolt internal errors, to all callers\n\t\tfor _, c := range b.calls {\n\t\t\tc.err <- err\n\t\t}\n\t\tbreak retry\n\t}\n}\n\n// trySolo is a special sentinel error value used for signaling that a\n// transaction function should be re-run. It should never be seen by\n// callers.\nvar trySolo = errors.New(\"batch function returned an error and should be re-run solo\")\n\ntype panicked struct {\n\treason interface{}\n}\n\nfunc (p panicked) Error() string {\n\tif err, ok := p.reason.(error); ok {\n\t\treturn err.Error()\n\t}\n\treturn fmt.Sprintf(\"panic: %v\", p.reason)\n}\n\nfunc safelyCall(fn func(*Tx) error, tx *Tx) (err error) {\n\tdefer func() {\n\t\tif p := recover(); p != nil {\n\t\t\terr = panicked{p}\n\t\t}\n\t}()\n\treturn fn(tx)\n}\n\n// Sync executes fdatasync() against the database file handle.\n//\n// This is not necessary under normal operation, however, if you use NoSync\n// then it allows you to force the database file to sync against the disk.\nfunc (db *DB) Sync() error { return fdatasync(db) }\n\n// Stats retrieves ongoing performance stats for the database.\n// This is only updated when a transaction closes.\nfunc (db *DB) Stats() Stats {\n\tdb.statlock.RLock()\n\tdefer db.statlock.RUnlock()\n\treturn db.stats\n}\n\n// This is for internal access to the raw data bytes from the C cursor, use\n// carefully, or not at all.\nfunc (db *DB) Info() *Info {\n\treturn &Info{uintptr(unsafe.Pointer(&db.data[0])), db.pageSize}\n}\n\n// page retrieves a page reference from the mmap based on the current page size.\nfunc (db *DB) page(id pgid) *page {\n\tpos := id * pgid(db.pageSize)\n\treturn (*page)(unsafe.Pointer(&db.data[pos]))\n}\n\n// pageInBuffer retrieves a page reference from a given byte array based on the current page size.\nfunc (db *DB) pageInBuffer(b []byte, id pgid) *page {\n\treturn (*page)(unsafe.Pointer(&b[id*pgid(db.pageSize)]))\n}\n\n// meta retrieves the current meta page reference.\nfunc (db *DB) meta() *meta {\n\t// We have to return the meta with the highest txid which doesn't fail\n\t// validation. Otherwise, we can cause errors when in fact the database is\n\t// in a consistent state. metaA is the one with the higher txid.\n\tmetaA := db.meta0\n\tmetaB := db.meta1\n\tif db.meta1.txid > db.meta0.txid {\n\t\tmetaA = db.meta1\n\t\tmetaB = db.meta0\n\t}\n\n\t// Use higher meta page if valid. Otherwise fallback to previous, if valid.\n\tif err := metaA.validate(); err == nil {\n\t\treturn metaA\n\t} else if err := metaB.validate(); err == nil {\n\t\treturn metaB\n\t}\n\n\t// This should never be reached, because both meta1 and meta0 were validated\n\t// on mmap() and we do fsync() on every write.\n\tpanic(\"bolt.DB.meta(): invalid meta pages\")\n}\n\n// allocate returns a contiguous block of memory starting at a given page.\nfunc (db *DB) allocate(count int) (*page, error) {\n\t// Allocate a temporary buffer for the page.\n\tvar buf []byte\n\tif count == 1 {\n\t\tbuf = db.pagePool.Get().([]byte)\n\t} else {\n\t\tbuf = make([]byte, count*db.pageSize)\n\t}\n\tp := (*page)(unsafe.Pointer(&buf[0]))\n\tp.overflow = uint32(count - 1)\n\n\t// Use pages from the freelist if they are available.\n\tif p.id = db.freelist.allocate(count); p.id != 0 {\n\t\treturn p, nil\n\t}\n\n\t// Resize mmap() if we're at the end.\n\tp.id = db.rwtx.meta.pgid\n\tvar minsz = int((p.id+pgid(count))+1) * db.pageSize\n\tif minsz >= db.datasz {\n\t\tif err := db.mmap(minsz); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"mmap allocate error: %s\", err)\n\t\t}\n\t}\n\n\t// Move the page id high water mark.\n\tdb.rwtx.meta.pgid += pgid(count)\n\n\treturn p, nil\n}\n\n// grow grows the size of the database to the given sz.\nfunc (db *DB) grow(sz int) error {\n\t// Ignore if the new size is less than available file size.\n\tif sz <= db.filesz {\n\t\treturn nil\n\t}\n\n\t// If the data is smaller than the alloc size then only allocate what's needed.\n\t// Once it goes over the allocation size then allocate in chunks.\n\tif db.datasz < db.AllocSize {\n\t\tsz = db.datasz\n\t} else {\n\t\tsz += db.AllocSize\n\t}\n\n\t// Truncate and fsync to ensure file size metadata is flushed.\n\t// https://github.com/boltdb/bolt/issues/284\n\tif !db.NoGrowSync && !db.readOnly {\n\t\tif runtime.GOOS != \"windows\" {\n\t\t\tif err := db.file.Truncate(int64(sz)); err != nil {\n\t\t\t\treturn fmt.Errorf(\"file resize error: %s\", err)\n\t\t\t}\n\t\t}\n\t\tif err := db.file.Sync(); err != nil {\n\t\t\treturn fmt.Errorf(\"file sync error: %s\", err)\n\t\t}\n\t}\n\n\tdb.filesz = sz\n\treturn nil\n}\n\nfunc (db *DB) IsReadOnly() bool {\n\treturn db.readOnly\n}\n\n// Options represents the options that can be set when opening a database.\ntype Options struct {\n\t// Timeout is the amount of time to wait to obtain a file lock.\n\t// When set to zero it will wait indefinitely. This option is only\n\t// available on Darwin and Linux.\n\tTimeout time.Duration\n\n\t// Sets the DB.NoGrowSync flag before memory mapping the file.\n\tNoGrowSync bool\n\n\t// Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to\n\t// grab a shared lock (UNIX).\n\tReadOnly bool\n\n\t// Sets the DB.MmapFlags flag before memory mapping the file.\n\tMmapFlags int\n\n\t// InitialMmapSize is the initial mmap size of the database\n\t// in bytes. Read transactions won't block write transaction\n\t// if the InitialMmapSize is large enough to hold database mmap\n\t// size. (See DB.Begin for more information)\n\t//\n\t// If <=0, the initial map size is 0.\n\t// If initialMmapSize is smaller than the previous database size,\n\t// it takes no effect.\n\tInitialMmapSize int\n}\n\n// DefaultOptions represent the options used if nil options are passed into Open().\n// No timeout is used which will cause Bolt to wait indefinitely for a lock.\nvar DefaultOptions = &Options{\n\tTimeout:    0,\n\tNoGrowSync: false,\n}\n\n// Stats represents statistics about the database.\ntype Stats struct {\n\t// Freelist stats\n\tFreePageN     int // total number of free pages on the freelist\n\tPendingPageN  int // total number of pending pages on the freelist\n\tFreeAlloc     int // total bytes allocated in free pages\n\tFreelistInuse int // total bytes used by the freelist\n\n\t// Transaction stats\n\tTxN     int // total number of started read transactions\n\tOpenTxN int // number of currently open read transactions\n\n\tTxStats TxStats // global, ongoing stats.\n}\n\n// Sub calculates and returns the difference between two sets of database stats.\n// This is useful when obtaining stats at two different points and time and\n// you need the performance counters that occurred within that time span.\nfunc (s *Stats) Sub(other *Stats) Stats {\n\tif other == nil {\n\t\treturn *s\n\t}\n\tvar diff Stats\n\tdiff.FreePageN = s.FreePageN\n\tdiff.PendingPageN = s.PendingPageN\n\tdiff.FreeAlloc = s.FreeAlloc\n\tdiff.FreelistInuse = s.FreelistInuse\n\tdiff.TxN = s.TxN - other.TxN\n\tdiff.TxStats = s.TxStats.Sub(&other.TxStats)\n\treturn diff\n}\n\nfunc (s *Stats) add(other *Stats) {\n\ts.TxStats.add(&other.TxStats)\n}\n\ntype Info struct {\n\tData     uintptr\n\tPageSize int\n}\n\ntype meta struct {\n\tmagic    uint32\n\tversion  uint32\n\tpageSize uint32\n\tflags    uint32\n\troot     bucket\n\tfreelist pgid\n\tpgid     pgid\n\ttxid     txid\n\tchecksum uint64\n}\n\n// validate checks the marker bytes and version of the meta page to ensure it matches this binary.\nfunc (m *meta) validate() error {\n\tif m.magic != magic {\n\t\treturn ErrInvalid\n\t} else if m.version != version {\n\t\treturn ErrVersionMismatch\n\t} else if m.checksum != 0 && m.checksum != m.sum64() {\n\t\treturn ErrChecksum\n\t}\n\treturn nil\n}\n\n// copy copies one meta object to another.\nfunc (m *meta) copy(dest *meta) {\n\t*dest = *m\n}\n\n// write writes the meta onto a page.\nfunc (m *meta) write(p *page) {\n\tif m.root.root >= m.pgid {\n\t\tpanic(fmt.Sprintf(\"root bucket pgid (%d) above high water mark (%d)\", m.root.root, m.pgid))\n\t} else if m.freelist >= m.pgid {\n\t\tpanic(fmt.Sprintf(\"freelist pgid (%d) above high water mark (%d)\", m.freelist, m.pgid))\n\t}\n\n\t// Page id is either going to be 0 or 1 which we can determine by the transaction ID.\n\tp.id = pgid(m.txid % 2)\n\tp.flags |= metaPageFlag\n\n\t// Calculate the checksum.\n\tm.checksum = m.sum64()\n\n\tm.copy(p.meta())\n}\n\n// generates the checksum for the meta.\nfunc (m *meta) sum64() uint64 {\n\tvar h = fnv.New64a()\n\t_, _ = h.Write((*[unsafe.Offsetof(meta{}.checksum)]byte)(unsafe.Pointer(m))[:])\n\treturn h.Sum64()\n}\n\n// _assert will panic with a given formatted message if the given condition is false.\nfunc _assert(condition bool, msg string, v ...interface{}) {\n\tif !condition {\n\t\tpanic(fmt.Sprintf(\"assertion failed: \"+msg, v...))\n\t}\n}\n\nfunc warn(v ...interface{})              { fmt.Fprintln(os.Stderr, v...) }\nfunc warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+\"\\n\", v...) }\n\nfunc printstack() {\n\tstack := strings.Join(strings.Split(string(debug.Stack()), \"\\n\")[2:], \"\\n\")\n\tfmt.Fprintln(os.Stderr, stack)\n}\n"
  },
  {
    "path": "db_test.go",
    "content": "package bolt_test\n\nimport (\n\t\"bytes\"\n\t\"encoding/binary\"\n\t\"errors\"\n\t\"flag\"\n\t\"fmt\"\n\t\"hash/fnv\"\n\t\"io/ioutil\"\n\t\"log\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"regexp\"\n\t\"sort\"\n\t\"strings\"\n\t\"sync\"\n\t\"testing\"\n\t\"time\"\n\t\"unsafe\"\n\n\t\"github.com/boltdb/bolt\"\n)\n\nvar statsFlag = flag.Bool(\"stats\", false, \"show performance stats\")\n\n// version is the data file format version.\nconst version = 2\n\n// magic is the marker value to indicate that a file is a Bolt DB.\nconst magic uint32 = 0xED0CDAED\n\n// pageSize is the size of one page in the data file.\nconst pageSize = 4096\n\n// pageHeaderSize is the size of a page header.\nconst pageHeaderSize = 16\n\n// meta represents a simplified version of a database meta page for testing.\ntype meta struct {\n\tmagic    uint32\n\tversion  uint32\n\t_        uint32\n\t_        uint32\n\t_        [16]byte\n\t_        uint64\n\tpgid     uint64\n\t_        uint64\n\tchecksum uint64\n}\n\n// Ensure that a database can be opened without error.\nfunc TestOpen(t *testing.T) {\n\tpath := tempfile()\n\tdb, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t} else if db == nil {\n\t\tt.Fatal(\"expected db\")\n\t}\n\n\tif s := db.Path(); s != path {\n\t\tt.Fatalf(\"unexpected path: %s\", s)\n\t}\n\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that opening a database with a blank path returns an error.\nfunc TestOpen_ErrPathRequired(t *testing.T) {\n\t_, err := bolt.Open(\"\", 0666, nil)\n\tif err == nil {\n\t\tt.Fatalf(\"expected error\")\n\t}\n}\n\n// Ensure that opening a database with a bad path returns an error.\nfunc TestOpen_ErrNotExists(t *testing.T) {\n\t_, err := bolt.Open(filepath.Join(tempfile(), \"bad-path\"), 0666, nil)\n\tif err == nil {\n\t\tt.Fatal(\"expected error\")\n\t}\n}\n\n// Ensure that opening a file that is not a Bolt database returns ErrInvalid.\nfunc TestOpen_ErrInvalid(t *testing.T) {\n\tpath := tempfile()\n\n\tf, err := os.Create(path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif _, err := fmt.Fprintln(f, \"this is not a bolt database\"); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := f.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tdefer os.Remove(path)\n\n\tif _, err := bolt.Open(path, 0666, nil); err != bolt.ErrInvalid {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that opening a file with two invalid versions returns ErrVersionMismatch.\nfunc TestOpen_ErrVersionMismatch(t *testing.T) {\n\tif pageSize != os.Getpagesize() {\n\t\tt.Skip(\"page size mismatch\")\n\t}\n\n\t// Create empty database.\n\tdb := MustOpenDB()\n\tpath := db.Path()\n\tdefer db.MustClose()\n\n\t// Close database.\n\tif err := db.DB.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read data file.\n\tbuf, err := ioutil.ReadFile(path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Rewrite meta pages.\n\tmeta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize]))\n\tmeta0.version++\n\tmeta1 := (*meta)(unsafe.Pointer(&buf[pageSize+pageHeaderSize]))\n\tmeta1.version++\n\tif err := ioutil.WriteFile(path, buf, 0666); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Reopen data file.\n\tif _, err := bolt.Open(path, 0666, nil); err != bolt.ErrVersionMismatch {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that opening a file with two invalid checksums returns ErrChecksum.\nfunc TestOpen_ErrChecksum(t *testing.T) {\n\tif pageSize != os.Getpagesize() {\n\t\tt.Skip(\"page size mismatch\")\n\t}\n\n\t// Create empty database.\n\tdb := MustOpenDB()\n\tpath := db.Path()\n\tdefer db.MustClose()\n\n\t// Close database.\n\tif err := db.DB.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read data file.\n\tbuf, err := ioutil.ReadFile(path)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Rewrite meta pages.\n\tmeta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize]))\n\tmeta0.pgid++\n\tmeta1 := (*meta)(unsafe.Pointer(&buf[pageSize+pageHeaderSize]))\n\tmeta1.pgid++\n\tif err := ioutil.WriteFile(path, buf, 0666); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Reopen data file.\n\tif _, err := bolt.Open(path, 0666, nil); err != bolt.ErrChecksum {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that opening a database does not increase its size.\n// https://github.com/boltdb/bolt/issues/291\nfunc TestOpen_Size(t *testing.T) {\n\t// Open a data file.\n\tdb := MustOpenDB()\n\tpath := db.Path()\n\tdefer db.MustClose()\n\n\tpagesize := db.Info().PageSize\n\n\t// Insert until we get above the minimum 4MB size.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, _ := tx.CreateBucketIfNotExists([]byte(\"data\"))\n\t\tfor i := 0; i < 10000; i++ {\n\t\t\tif err := b.Put([]byte(fmt.Sprintf(\"%04d\", i)), make([]byte, 1000)); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Close database and grab the size.\n\tif err := db.DB.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tsz := fileSize(path)\n\tif sz == 0 {\n\t\tt.Fatalf(\"unexpected new file size: %d\", sz)\n\t}\n\n\t// Reopen database, update, and check size again.\n\tdb0, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db0.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.Bucket([]byte(\"data\")).Put([]byte{0}, []byte{0}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db0.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tnewSz := fileSize(path)\n\tif newSz == 0 {\n\t\tt.Fatalf(\"unexpected new file size: %d\", newSz)\n\t}\n\n\t// Compare the original size with the new size.\n\t// db size might increase by a few page sizes due to the new small update.\n\tif sz < newSz-5*int64(pagesize) {\n\t\tt.Fatalf(\"unexpected file growth: %d => %d\", sz, newSz)\n\t}\n}\n\n// Ensure that opening a database beyond the max step size does not increase its size.\n// https://github.com/boltdb/bolt/issues/303\nfunc TestOpen_Size_Large(t *testing.T) {\n\tif testing.Short() {\n\t\tt.Skip(\"short mode\")\n\t}\n\n\t// Open a data file.\n\tdb := MustOpenDB()\n\tpath := db.Path()\n\tdefer db.MustClose()\n\n\tpagesize := db.Info().PageSize\n\n\t// Insert until we get above the minimum 4MB size.\n\tvar index uint64\n\tfor i := 0; i < 10000; i++ {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tb, _ := tx.CreateBucketIfNotExists([]byte(\"data\"))\n\t\t\tfor j := 0; j < 1000; j++ {\n\t\t\t\tif err := b.Put(u64tob(index), make([]byte, 50)); err != nil {\n\t\t\t\t\tt.Fatal(err)\n\t\t\t\t}\n\t\t\t\tindex++\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Close database and grab the size.\n\tif err := db.DB.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tsz := fileSize(path)\n\tif sz == 0 {\n\t\tt.Fatalf(\"unexpected new file size: %d\", sz)\n\t} else if sz < (1 << 30) {\n\t\tt.Fatalf(\"expected larger initial size: %d\", sz)\n\t}\n\n\t// Reopen database, update, and check size again.\n\tdb0, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db0.Update(func(tx *bolt.Tx) error {\n\t\treturn tx.Bucket([]byte(\"data\")).Put([]byte{0}, []byte{0})\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db0.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tnewSz := fileSize(path)\n\tif newSz == 0 {\n\t\tt.Fatalf(\"unexpected new file size: %d\", newSz)\n\t}\n\n\t// Compare the original size with the new size.\n\t// db size might increase by a few page sizes due to the new small update.\n\tif sz < newSz-5*int64(pagesize) {\n\t\tt.Fatalf(\"unexpected file growth: %d => %d\", sz, newSz)\n\t}\n}\n\n// Ensure that a re-opened database is consistent.\nfunc TestOpen_Check(t *testing.T) {\n\tpath := tempfile()\n\n\tdb, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb, err = bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that write errors to the meta file handler during initialization are returned.\nfunc TestOpen_MetaInitWriteError(t *testing.T) {\n\tt.Skip(\"pending\")\n}\n\n// Ensure that a database that is too small returns an error.\nfunc TestOpen_FileTooSmall(t *testing.T) {\n\tpath := tempfile()\n\n\tdb, err := bolt.Open(path, 0666, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// corrupt the database\n\tif err := os.Truncate(path, int64(os.Getpagesize())); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb, err = bolt.Open(path, 0666, nil)\n\tif err == nil || err.Error() != \"file size too small\" {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// TestDB_Open_InitialMmapSize tests if having InitialMmapSize large enough\n// to hold data from concurrent write transaction resolves the issue that\n// read transaction blocks the write transaction and causes deadlock.\n// This is a very hacky test since the mmap size is not exposed.\nfunc TestDB_Open_InitialMmapSize(t *testing.T) {\n\tpath := tempfile()\n\tdefer os.Remove(path)\n\n\tinitMmapSize := 1 << 31  // 2GB\n\ttestWriteSize := 1 << 27 // 134MB\n\n\tdb, err := bolt.Open(path, 0666, &bolt.Options{InitialMmapSize: initMmapSize})\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// create a long-running read transaction\n\t// that never gets closed while writing\n\trtx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// create a write transaction\n\twtx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tb, err := wtx.CreateBucket([]byte(\"test\"))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// and commit a large write\n\terr = b.Put([]byte(\"foo\"), make([]byte, testWriteSize))\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdone := make(chan struct{})\n\n\tgo func() {\n\t\tif err := wtx.Commit(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tdone <- struct{}{}\n\t}()\n\n\tselect {\n\tcase <-time.After(5 * time.Second):\n\t\tt.Errorf(\"unexpected that the reader blocks writer\")\n\tcase <-done:\n\t}\n\n\tif err := rtx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a database cannot open a transaction when it's not open.\nfunc TestDB_Begin_ErrDatabaseNotOpen(t *testing.T) {\n\tvar db bolt.DB\n\tif _, err := db.Begin(false); err != bolt.ErrDatabaseNotOpen {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that a read-write transaction can be retrieved.\nfunc TestDB_BeginRW(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t} else if tx == nil {\n\t\tt.Fatal(\"expected tx\")\n\t}\n\n\tif tx.DB() != db.DB {\n\t\tt.Fatal(\"unexpected tx database\")\n\t} else if !tx.Writable() {\n\t\tt.Fatal(\"expected writable tx\")\n\t}\n\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that opening a transaction while the DB is closed returns an error.\nfunc TestDB_BeginRW_Closed(t *testing.T) {\n\tvar db bolt.DB\n\tif _, err := db.Begin(true); err != bolt.ErrDatabaseNotOpen {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\nfunc TestDB_Close_PendingTx_RW(t *testing.T) { testDB_Close_PendingTx(t, true) }\nfunc TestDB_Close_PendingTx_RO(t *testing.T) { testDB_Close_PendingTx(t, false) }\n\n// Ensure that a database cannot close while transactions are open.\nfunc testDB_Close_PendingTx(t *testing.T, writable bool) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Start transaction.\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Open update in separate goroutine.\n\tdone := make(chan struct{})\n\tgo func() {\n\t\tif err := db.Close(); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tclose(done)\n\t}()\n\n\t// Ensure database hasn't closed.\n\ttime.Sleep(100 * time.Millisecond)\n\tselect {\n\tcase <-done:\n\t\tt.Fatal(\"database closed too early\")\n\tdefault:\n\t}\n\n\t// Commit transaction.\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Ensure database closed now.\n\ttime.Sleep(100 * time.Millisecond)\n\tselect {\n\tcase <-done:\n\tdefault:\n\t\tt.Fatal(\"database did not close\")\n\t}\n}\n\n// Ensure a database can provide a transactional block.\nfunc TestDB_Update(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Delete([]byte(\"foo\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tif v := b.Get([]byte(\"foo\")); v != nil {\n\t\t\tt.Fatalf(\"expected nil value, got: %v\", v)\n\t\t}\n\t\tif v := b.Get([]byte(\"baz\")); !bytes.Equal(v, []byte(\"bat\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a closed database returns an error while running a transaction block\nfunc TestDB_Update_Closed(t *testing.T) {\n\tvar db bolt.DB\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != bolt.ErrDatabaseNotOpen {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure a panic occurs while trying to commit a managed transaction.\nfunc TestDB_Update_ManualCommit(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar panicked bool\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tif err := tx.Commit(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}()\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if !panicked {\n\t\tt.Fatal(\"expected panic\")\n\t}\n}\n\n// Ensure a panic occurs while trying to rollback a managed transaction.\nfunc TestDB_Update_ManualRollback(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar panicked bool\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tif err := tx.Rollback(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}()\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if !panicked {\n\t\tt.Fatal(\"expected panic\")\n\t}\n}\n\n// Ensure a panic occurs while trying to commit a managed transaction.\nfunc TestDB_View_ManualCommit(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar panicked bool\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tif err := tx.Commit(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}()\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if !panicked {\n\t\tt.Fatal(\"expected panic\")\n\t}\n}\n\n// Ensure a panic occurs while trying to rollback a managed transaction.\nfunc TestDB_View_ManualRollback(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar panicked bool\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tfunc() {\n\t\t\tdefer func() {\n\t\t\t\tif r := recover(); r != nil {\n\t\t\t\t\tpanicked = true\n\t\t\t\t}\n\t\t\t}()\n\n\t\t\tif err := tx.Rollback(); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t}()\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if !panicked {\n\t\tt.Fatal(\"expected panic\")\n\t}\n}\n\n// Ensure a write transaction that panics does not hold open locks.\nfunc TestDB_Update_Panic(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Panic during update but recover.\n\tfunc() {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tt.Log(\"recover: update\", r)\n\t\t\t}\n\t\t}()\n\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tpanic(\"omg\")\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}()\n\n\t// Verify we can update again.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Verify that our change persisted.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure a database can return an error through a read-only transactional block.\nfunc TestDB_View_Error(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn errors.New(\"xxx\")\n\t}); err == nil || err.Error() != \"xxx\" {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure a read transaction that panics does not hold open locks.\nfunc TestDB_View_Panic(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Panic during view transaction but recover.\n\tfunc() {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tt.Log(\"recover: view\", r)\n\t\t\t}\n\t\t}()\n\n\t\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\t\tt.Fatal(\"expected bucket\")\n\t\t\t}\n\t\t\tpanic(\"omg\")\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}()\n\n\t// Verify that we can still use read transactions.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that DB stats can be returned.\nfunc TestDB_Stats(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tstats := db.Stats()\n\tif stats.TxStats.PageCount != 2 {\n\t\tt.Fatalf(\"unexpected TxStats.PageCount: %d\", stats.TxStats.PageCount)\n\t} else if stats.FreePageN != 0 {\n\t\tt.Fatalf(\"unexpected FreePageN != 0: %d\", stats.FreePageN)\n\t} else if stats.PendingPageN != 2 {\n\t\tt.Fatalf(\"unexpected PendingPageN != 2: %d\", stats.PendingPageN)\n\t}\n}\n\n// Ensure that database pages are in expected order and type.\nfunc TestDB_Consistency(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tfor i := 0; i < 10; i++ {\n\t\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t\tif err := tx.Bucket([]byte(\"widgets\")).Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif p, _ := tx.Page(0); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"meta\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(1); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"meta\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(2); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"free\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(3); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"free\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(4); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"leaf\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(5); p == nil {\n\t\t\tt.Fatal(\"expected page\")\n\t\t} else if p.Type != \"freelist\" {\n\t\t\tt.Fatalf(\"unexpected page type: %s\", p.Type)\n\t\t}\n\n\t\tif p, _ := tx.Page(6); p != nil {\n\t\t\tt.Fatal(\"unexpected page\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that DB stats can be subtracted from one another.\nfunc TestDBStats_Sub(t *testing.T) {\n\tvar a, b bolt.Stats\n\ta.TxStats.PageCount = 3\n\ta.FreePageN = 4\n\tb.TxStats.PageCount = 10\n\tb.FreePageN = 14\n\tdiff := b.Sub(&a)\n\tif diff.TxStats.PageCount != 7 {\n\t\tt.Fatalf(\"unexpected TxStats.PageCount: %d\", diff.TxStats.PageCount)\n\t}\n\n\t// free page stats are copied from the receiver and not subtracted\n\tif diff.FreePageN != 14 {\n\t\tt.Fatalf(\"unexpected FreePageN: %d\", diff.FreePageN)\n\t}\n}\n\n// Ensure two functions can perform updates in a single batch.\nfunc TestDB_Batch(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Iterate over multiple updates in separate goroutines.\n\tn := 2\n\tch := make(chan error)\n\tfor i := 0; i < n; i++ {\n\t\tgo func(i int) {\n\t\t\tch <- db.Batch(func(tx *bolt.Tx) error {\n\t\t\t\treturn tx.Bucket([]byte(\"widgets\")).Put(u64tob(uint64(i)), []byte{})\n\t\t\t})\n\t\t}(i)\n\t}\n\n\t// Check all responses to make sure there's no error.\n\tfor i := 0; i < n; i++ {\n\t\tif err := <-ch; err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Ensure data is correct.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 0; i < n; i++ {\n\t\t\tif v := b.Get(u64tob(uint64(i))); v == nil {\n\t\t\t\tt.Errorf(\"key not found: %d\", i)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestDB_Batch_Panic(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar sentinel int\n\tvar bork = &sentinel\n\tvar problem interface{}\n\tvar err error\n\n\t// Execute a function inside a batch that panics.\n\tfunc() {\n\t\tdefer func() {\n\t\t\tif p := recover(); p != nil {\n\t\t\t\tproblem = p\n\t\t\t}\n\t\t}()\n\t\terr = db.Batch(func(tx *bolt.Tx) error {\n\t\t\tpanic(bork)\n\t\t})\n\t}()\n\n\t// Verify there is no error.\n\tif g, e := err, error(nil); g != e {\n\t\tt.Fatalf(\"wrong error: %v != %v\", g, e)\n\t}\n\t// Verify the panic was captured.\n\tif g, e := problem, bork; g != e {\n\t\tt.Fatalf(\"wrong error: %v != %v\", g, e)\n\t}\n}\n\nfunc TestDB_BatchFull(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tconst size = 3\n\t// buffered so we never leak goroutines\n\tch := make(chan error, size)\n\tput := func(i int) {\n\t\tch <- db.Batch(func(tx *bolt.Tx) error {\n\t\t\treturn tx.Bucket([]byte(\"widgets\")).Put(u64tob(uint64(i)), []byte{})\n\t\t})\n\t}\n\n\tdb.MaxBatchSize = size\n\t// high enough to never trigger here\n\tdb.MaxBatchDelay = 1 * time.Hour\n\n\tgo put(1)\n\tgo put(2)\n\n\t// Give the batch a chance to exhibit bugs.\n\ttime.Sleep(10 * time.Millisecond)\n\n\t// not triggered yet\n\tselect {\n\tcase <-ch:\n\t\tt.Fatalf(\"batch triggered too early\")\n\tdefault:\n\t}\n\n\tgo put(3)\n\n\t// Check all responses to make sure there's no error.\n\tfor i := 0; i < size; i++ {\n\t\tif err := <-ch; err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Ensure data is correct.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 1; i <= size; i++ {\n\t\t\tif v := b.Get(u64tob(uint64(i))); v == nil {\n\t\t\t\tt.Errorf(\"key not found: %d\", i)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc TestDB_BatchTime(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tconst size = 1\n\t// buffered so we never leak goroutines\n\tch := make(chan error, size)\n\tput := func(i int) {\n\t\tch <- db.Batch(func(tx *bolt.Tx) error {\n\t\t\treturn tx.Bucket([]byte(\"widgets\")).Put(u64tob(uint64(i)), []byte{})\n\t\t})\n\t}\n\n\tdb.MaxBatchSize = 1000\n\tdb.MaxBatchDelay = 0\n\n\tgo put(1)\n\n\t// Batch must trigger by time alone.\n\n\t// Check all responses to make sure there's no error.\n\tfor i := 0; i < size; i++ {\n\t\tif err := <-ch; err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t}\n\n\t// Ensure data is correct.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tb := tx.Bucket([]byte(\"widgets\"))\n\t\tfor i := 1; i <= size; i++ {\n\t\t\tif v := b.Get(u64tob(uint64(i))); v == nil {\n\t\t\t\tt.Errorf(\"key not found: %d\", i)\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\nfunc ExampleDB_Update() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Execute several commands within a read-write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Read the value back from a separate read-only transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value of 'foo' is: %s\\n\", value)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release the file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value of 'foo' is: bar\n}\n\nfunc ExampleDB_View() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Insert data into a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"people\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"john\"), []byte(\"doe\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"susy\"), []byte(\"que\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Access data from within a read-only transactional block.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tv := tx.Bucket([]byte(\"people\")).Get([]byte(\"john\"))\n\t\tfmt.Printf(\"John's last name is %s.\\n\", v)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release the file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// John's last name is doe.\n}\n\nfunc ExampleDB_Begin_ReadOnly() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Create a bucket using a read-write transaction.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Create several keys in a transaction.\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tb := tx.Bucket([]byte(\"widgets\"))\n\tif err := b.Put([]byte(\"john\"), []byte(\"blue\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tif err := b.Put([]byte(\"abby\"), []byte(\"red\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tif err := b.Put([]byte(\"zephyr\"), []byte(\"purple\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tif err := tx.Commit(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Iterate over the values in sorted key order.\n\ttx, err = db.Begin(false)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tc := tx.Bucket([]byte(\"widgets\")).Cursor()\n\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\tfmt.Printf(\"%s likes %s\\n\", k, v)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// abby likes red\n\t// john likes blue\n\t// zephyr likes purple\n}\n\nfunc BenchmarkDBBatchAutomatic(b *testing.B) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"bench\"))\n\t\treturn err\n\t}); err != nil {\n\t\tb.Fatal(err)\n\t}\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tstart := make(chan struct{})\n\t\tvar wg sync.WaitGroup\n\n\t\tfor round := 0; round < 1000; round++ {\n\t\t\twg.Add(1)\n\n\t\t\tgo func(id uint32) {\n\t\t\t\tdefer wg.Done()\n\t\t\t\t<-start\n\n\t\t\t\th := fnv.New32a()\n\t\t\t\tbuf := make([]byte, 4)\n\t\t\t\tbinary.LittleEndian.PutUint32(buf, id)\n\t\t\t\t_, _ = h.Write(buf[:])\n\t\t\t\tk := h.Sum(nil)\n\t\t\t\tinsert := func(tx *bolt.Tx) error {\n\t\t\t\t\tb := tx.Bucket([]byte(\"bench\"))\n\t\t\t\t\treturn b.Put(k, []byte(\"filler\"))\n\t\t\t\t}\n\t\t\t\tif err := db.Batch(insert); err != nil {\n\t\t\t\t\tb.Error(err)\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}(uint32(round))\n\t\t}\n\t\tclose(start)\n\t\twg.Wait()\n\t}\n\n\tb.StopTimer()\n\tvalidateBatchBench(b, db)\n}\n\nfunc BenchmarkDBBatchSingle(b *testing.B) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"bench\"))\n\t\treturn err\n\t}); err != nil {\n\t\tb.Fatal(err)\n\t}\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tstart := make(chan struct{})\n\t\tvar wg sync.WaitGroup\n\n\t\tfor round := 0; round < 1000; round++ {\n\t\t\twg.Add(1)\n\t\t\tgo func(id uint32) {\n\t\t\t\tdefer wg.Done()\n\t\t\t\t<-start\n\n\t\t\t\th := fnv.New32a()\n\t\t\t\tbuf := make([]byte, 4)\n\t\t\t\tbinary.LittleEndian.PutUint32(buf, id)\n\t\t\t\t_, _ = h.Write(buf[:])\n\t\t\t\tk := h.Sum(nil)\n\t\t\t\tinsert := func(tx *bolt.Tx) error {\n\t\t\t\t\tb := tx.Bucket([]byte(\"bench\"))\n\t\t\t\t\treturn b.Put(k, []byte(\"filler\"))\n\t\t\t\t}\n\t\t\t\tif err := db.Update(insert); err != nil {\n\t\t\t\t\tb.Error(err)\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}(uint32(round))\n\t\t}\n\t\tclose(start)\n\t\twg.Wait()\n\t}\n\n\tb.StopTimer()\n\tvalidateBatchBench(b, db)\n}\n\nfunc BenchmarkDBBatchManual10x100(b *testing.B) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"bench\"))\n\t\treturn err\n\t}); err != nil {\n\t\tb.Fatal(err)\n\t}\n\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tstart := make(chan struct{})\n\t\tvar wg sync.WaitGroup\n\n\t\tfor major := 0; major < 10; major++ {\n\t\t\twg.Add(1)\n\t\t\tgo func(id uint32) {\n\t\t\t\tdefer wg.Done()\n\t\t\t\t<-start\n\n\t\t\t\tinsert100 := func(tx *bolt.Tx) error {\n\t\t\t\t\th := fnv.New32a()\n\t\t\t\t\tbuf := make([]byte, 4)\n\t\t\t\t\tfor minor := uint32(0); minor < 100; minor++ {\n\t\t\t\t\t\tbinary.LittleEndian.PutUint32(buf, uint32(id*100+minor))\n\t\t\t\t\t\th.Reset()\n\t\t\t\t\t\t_, _ = h.Write(buf[:])\n\t\t\t\t\t\tk := h.Sum(nil)\n\t\t\t\t\t\tb := tx.Bucket([]byte(\"bench\"))\n\t\t\t\t\t\tif err := b.Put(k, []byte(\"filler\")); err != nil {\n\t\t\t\t\t\t\treturn err\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\tif err := db.Update(insert100); err != nil {\n\t\t\t\t\tb.Fatal(err)\n\t\t\t\t}\n\t\t\t}(uint32(major))\n\t\t}\n\t\tclose(start)\n\t\twg.Wait()\n\t}\n\n\tb.StopTimer()\n\tvalidateBatchBench(b, db)\n}\n\nfunc validateBatchBench(b *testing.B, db *DB) {\n\tvar rollback = errors.New(\"sentinel error to cause rollback\")\n\tvalidate := func(tx *bolt.Tx) error {\n\t\tbucket := tx.Bucket([]byte(\"bench\"))\n\t\th := fnv.New32a()\n\t\tbuf := make([]byte, 4)\n\t\tfor id := uint32(0); id < 1000; id++ {\n\t\t\tbinary.LittleEndian.PutUint32(buf, id)\n\t\t\th.Reset()\n\t\t\t_, _ = h.Write(buf[:])\n\t\t\tk := h.Sum(nil)\n\t\t\tv := bucket.Get(k)\n\t\t\tif v == nil {\n\t\t\t\tb.Errorf(\"not found id=%d key=%x\", id, k)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif g, e := v, []byte(\"filler\"); !bytes.Equal(g, e) {\n\t\t\t\tb.Errorf(\"bad value for id=%d key=%x: %s != %q\", id, k, g, e)\n\t\t\t}\n\t\t\tif err := bucket.Delete(k); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\t// should be empty now\n\t\tc := bucket.Cursor()\n\t\tfor k, v := c.First(); k != nil; k, v = c.Next() {\n\t\t\tb.Errorf(\"unexpected key: %x = %q\", k, v)\n\t\t}\n\t\treturn rollback\n\t}\n\tif err := db.Update(validate); err != nil && err != rollback {\n\t\tb.Error(err)\n\t}\n}\n\n// DB is a test wrapper for bolt.DB.\ntype DB struct {\n\t*bolt.DB\n}\n\n// MustOpenDB returns a new, open DB at a temporary location.\nfunc MustOpenDB() *DB {\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn &DB{db}\n}\n\n// Close closes the database and deletes the underlying file.\nfunc (db *DB) Close() error {\n\t// Log statistics.\n\tif *statsFlag {\n\t\tdb.PrintStats()\n\t}\n\n\t// Check database consistency after every test.\n\tdb.MustCheck()\n\n\t// Close database and remove file.\n\tdefer os.Remove(db.Path())\n\treturn db.DB.Close()\n}\n\n// MustClose closes the database and deletes the underlying file. Panic on error.\nfunc (db *DB) MustClose() {\n\tif err := db.Close(); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n// PrintStats prints the database stats\nfunc (db *DB) PrintStats() {\n\tvar stats = db.Stats()\n\tfmt.Printf(\"[db] %-20s %-20s %-20s\\n\",\n\t\tfmt.Sprintf(\"pg(%d/%d)\", stats.TxStats.PageCount, stats.TxStats.PageAlloc),\n\t\tfmt.Sprintf(\"cur(%d)\", stats.TxStats.CursorCount),\n\t\tfmt.Sprintf(\"node(%d/%d)\", stats.TxStats.NodeCount, stats.TxStats.NodeDeref),\n\t)\n\tfmt.Printf(\"     %-20s %-20s %-20s\\n\",\n\t\tfmt.Sprintf(\"rebal(%d/%v)\", stats.TxStats.Rebalance, truncDuration(stats.TxStats.RebalanceTime)),\n\t\tfmt.Sprintf(\"spill(%d/%v)\", stats.TxStats.Spill, truncDuration(stats.TxStats.SpillTime)),\n\t\tfmt.Sprintf(\"w(%d/%v)\", stats.TxStats.Write, truncDuration(stats.TxStats.WriteTime)),\n\t)\n}\n\n// MustCheck runs a consistency check on the database and panics if any errors are found.\nfunc (db *DB) MustCheck() {\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Collect all the errors.\n\t\tvar errors []error\n\t\tfor err := range tx.Check() {\n\t\t\terrors = append(errors, err)\n\t\t\tif len(errors) > 10 {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\t// If errors occurred, copy the DB and print the errors.\n\t\tif len(errors) > 0 {\n\t\t\tvar path = tempfile()\n\t\t\tif err := tx.CopyFile(path, 0600); err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\n\t\t\t// Print errors.\n\t\t\tfmt.Print(\"\\n\\n\")\n\t\t\tfmt.Printf(\"consistency check failed (%d errors)\\n\", len(errors))\n\t\t\tfor _, err := range errors {\n\t\t\t\tfmt.Println(err)\n\t\t\t}\n\t\t\tfmt.Println(\"\")\n\t\t\tfmt.Println(\"db saved to:\")\n\t\t\tfmt.Println(path)\n\t\t\tfmt.Print(\"\\n\\n\")\n\t\t\tos.Exit(-1)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil && err != bolt.ErrDatabaseNotOpen {\n\t\tpanic(err)\n\t}\n}\n\n// CopyTempFile copies a database to a temporary file.\nfunc (db *DB) CopyTempFile() {\n\tpath := tempfile()\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.CopyFile(path, 0600)\n\t}); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(\"db copied to: \", path)\n}\n\n// tempfile returns a temporary file path.\nfunc tempfile() string {\n\tf, err := ioutil.TempFile(\"\", \"bolt-\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tif err := f.Close(); err != nil {\n\t\tpanic(err)\n\t}\n\tif err := os.Remove(f.Name()); err != nil {\n\t\tpanic(err)\n\t}\n\treturn f.Name()\n}\n\n// mustContainKeys checks that a bucket contains a given set of keys.\nfunc mustContainKeys(b *bolt.Bucket, m map[string]string) {\n\tfound := make(map[string]string)\n\tif err := b.ForEach(func(k, _ []byte) error {\n\t\tfound[string(k)] = \"\"\n\t\treturn nil\n\t}); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Check for keys found in bucket that shouldn't be there.\n\tvar keys []string\n\tfor k, _ := range found {\n\t\tif _, ok := m[string(k)]; !ok {\n\t\t\tkeys = append(keys, k)\n\t\t}\n\t}\n\tif len(keys) > 0 {\n\t\tsort.Strings(keys)\n\t\tpanic(fmt.Sprintf(\"keys found(%d): %s\", len(keys), strings.Join(keys, \",\")))\n\t}\n\n\t// Check for keys not found in bucket that should be there.\n\tfor k, _ := range m {\n\t\tif _, ok := found[string(k)]; !ok {\n\t\t\tkeys = append(keys, k)\n\t\t}\n\t}\n\tif len(keys) > 0 {\n\t\tsort.Strings(keys)\n\t\tpanic(fmt.Sprintf(\"keys not found(%d): %s\", len(keys), strings.Join(keys, \",\")))\n\t}\n}\n\nfunc trunc(b []byte, length int) []byte {\n\tif length < len(b) {\n\t\treturn b[:length]\n\t}\n\treturn b\n}\n\nfunc truncDuration(d time.Duration) string {\n\treturn regexp.MustCompile(`^(\\d+)(\\.\\d+)`).ReplaceAllString(d.String(), \"$1\")\n}\n\nfunc fileSize(path string) int64 {\n\tfi, err := os.Stat(path)\n\tif err != nil {\n\t\treturn 0\n\t}\n\treturn fi.Size()\n}\n\nfunc warn(v ...interface{})              { fmt.Fprintln(os.Stderr, v...) }\nfunc warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+\"\\n\", v...) }\n\n// u64tob converts a uint64 into an 8-byte slice.\nfunc u64tob(v uint64) []byte {\n\tb := make([]byte, 8)\n\tbinary.BigEndian.PutUint64(b, v)\n\treturn b\n}\n\n// btou64 converts an 8-byte slice into an uint64.\nfunc btou64(b []byte) uint64 { return binary.BigEndian.Uint64(b) }\n"
  },
  {
    "path": "doc.go",
    "content": "/*\nPackage bolt implements a low-level key/value store in pure Go. It supports\nfully serializable transactions, ACID semantics, and lock-free MVCC with\nmultiple readers and a single writer. Bolt can be used for projects that\nwant a simple data store without the need to add large dependencies such as\nPostgres or MySQL.\n\nBolt is a single-level, zero-copy, B+tree data store. This means that Bolt is\noptimized for fast read access and does not require recovery in the event of a\nsystem crash. Transactions which have not finished committing will simply be\nrolled back in the event of a crash.\n\nThe design of Bolt is based on Howard Chu's LMDB database project.\n\nBolt currently works on Windows, Mac OS X, and Linux.\n\n\nBasics\n\nThere are only a few types in Bolt: DB, Bucket, Tx, and Cursor. The DB is\na collection of buckets and is represented by a single file on disk. A bucket is\na collection of unique keys that are associated with values.\n\nTransactions provide either read-only or read-write access to the database.\nRead-only transactions can retrieve key/value pairs and can use Cursors to\niterate over the dataset sequentially. Read-write transactions can create and\ndelete buckets and can insert and remove keys. Only one read-write transaction\nis allowed at a time.\n\n\nCaveats\n\nThe database uses a read-only, memory-mapped data file to ensure that\napplications cannot corrupt the database, however, this means that keys and\nvalues returned from Bolt cannot be changed. Writing to a read-only byte slice\nwill cause Go to panic.\n\nKeys and values retrieved from the database are only valid for the life of\nthe transaction. When used outside the transaction, these byte slices can\npoint to different data or can point to invalid memory which will cause a panic.\n\n\n*/\npackage bolt\n"
  },
  {
    "path": "errors.go",
    "content": "package bolt\n\nimport \"errors\"\n\n// These errors can be returned when opening or calling methods on a DB.\nvar (\n\t// ErrDatabaseNotOpen is returned when a DB instance is accessed before it\n\t// is opened or after it is closed.\n\tErrDatabaseNotOpen = errors.New(\"database not open\")\n\n\t// ErrDatabaseOpen is returned when opening a database that is\n\t// already open.\n\tErrDatabaseOpen = errors.New(\"database already open\")\n\n\t// ErrInvalid is returned when both meta pages on a database are invalid.\n\t// This typically occurs when a file is not a bolt database.\n\tErrInvalid = errors.New(\"invalid database\")\n\n\t// ErrVersionMismatch is returned when the data file was created with a\n\t// different version of Bolt.\n\tErrVersionMismatch = errors.New(\"version mismatch\")\n\n\t// ErrChecksum is returned when either meta page checksum does not match.\n\tErrChecksum = errors.New(\"checksum error\")\n\n\t// ErrTimeout is returned when a database cannot obtain an exclusive lock\n\t// on the data file after the timeout passed to Open().\n\tErrTimeout = errors.New(\"timeout\")\n)\n\n// These errors can occur when beginning or committing a Tx.\nvar (\n\t// ErrTxNotWritable is returned when performing a write operation on a\n\t// read-only transaction.\n\tErrTxNotWritable = errors.New(\"tx not writable\")\n\n\t// ErrTxClosed is returned when committing or rolling back a transaction\n\t// that has already been committed or rolled back.\n\tErrTxClosed = errors.New(\"tx closed\")\n\n\t// ErrDatabaseReadOnly is returned when a mutating transaction is started on a\n\t// read-only database.\n\tErrDatabaseReadOnly = errors.New(\"database is in read-only mode\")\n)\n\n// These errors can occur when putting or deleting a value or a bucket.\nvar (\n\t// ErrBucketNotFound is returned when trying to access a bucket that has\n\t// not been created yet.\n\tErrBucketNotFound = errors.New(\"bucket not found\")\n\n\t// ErrBucketExists is returned when creating a bucket that already exists.\n\tErrBucketExists = errors.New(\"bucket already exists\")\n\n\t// ErrBucketNameRequired is returned when creating a bucket with a blank name.\n\tErrBucketNameRequired = errors.New(\"bucket name required\")\n\n\t// ErrKeyRequired is returned when inserting a zero-length key.\n\tErrKeyRequired = errors.New(\"key required\")\n\n\t// ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.\n\tErrKeyTooLarge = errors.New(\"key too large\")\n\n\t// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.\n\tErrValueTooLarge = errors.New(\"value too large\")\n\n\t// ErrIncompatibleValue is returned when trying create or delete a bucket\n\t// on an existing non-bucket key or when trying to create or delete a\n\t// non-bucket key on an existing bucket key.\n\tErrIncompatibleValue = errors.New(\"incompatible value\")\n)\n"
  },
  {
    "path": "freelist.go",
    "content": "package bolt\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n\t\"unsafe\"\n)\n\n// freelist represents a list of all pages that are available for allocation.\n// It also tracks pages that have been freed but are still in use by open transactions.\ntype freelist struct {\n\tids     []pgid          // all free and available free page ids.\n\tpending map[txid][]pgid // mapping of soon-to-be free page ids by tx.\n\tcache   map[pgid]bool   // fast lookup of all free and pending page ids.\n}\n\n// newFreelist returns an empty, initialized freelist.\nfunc newFreelist() *freelist {\n\treturn &freelist{\n\t\tpending: make(map[txid][]pgid),\n\t\tcache:   make(map[pgid]bool),\n\t}\n}\n\n// size returns the size of the page after serialization.\nfunc (f *freelist) size() int {\n\tn := f.count()\n\tif n >= 0xFFFF {\n\t\t// The first element will be used to store the count. See freelist.write.\n\t\tn++\n\t}\n\treturn pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * n)\n}\n\n// count returns count of pages on the freelist\nfunc (f *freelist) count() int {\n\treturn f.free_count() + f.pending_count()\n}\n\n// free_count returns count of free pages\nfunc (f *freelist) free_count() int {\n\treturn len(f.ids)\n}\n\n// pending_count returns count of pending pages\nfunc (f *freelist) pending_count() int {\n\tvar count int\n\tfor _, list := range f.pending {\n\t\tcount += len(list)\n\t}\n\treturn count\n}\n\n// copyall copies into dst a list of all free ids and all pending ids in one sorted list.\n// f.count returns the minimum length required for dst.\nfunc (f *freelist) copyall(dst []pgid) {\n\tm := make(pgids, 0, f.pending_count())\n\tfor _, list := range f.pending {\n\t\tm = append(m, list...)\n\t}\n\tsort.Sort(m)\n\tmergepgids(dst, f.ids, m)\n}\n\n// allocate returns the starting page id of a contiguous list of pages of a given size.\n// If a contiguous block cannot be found then 0 is returned.\nfunc (f *freelist) allocate(n int) pgid {\n\tif len(f.ids) == 0 {\n\t\treturn 0\n\t}\n\n\tvar initial, previd pgid\n\tfor i, id := range f.ids {\n\t\tif id <= 1 {\n\t\t\tpanic(fmt.Sprintf(\"invalid page allocation: %d\", id))\n\t\t}\n\n\t\t// Reset initial page if this is not contiguous.\n\t\tif previd == 0 || id-previd != 1 {\n\t\t\tinitial = id\n\t\t}\n\n\t\t// If we found a contiguous block then remove it and return it.\n\t\tif (id-initial)+1 == pgid(n) {\n\t\t\t// If we're allocating off the beginning then take the fast path\n\t\t\t// and just adjust the existing slice. This will use extra memory\n\t\t\t// temporarily but the append() in free() will realloc the slice\n\t\t\t// as is necessary.\n\t\t\tif (i + 1) == n {\n\t\t\t\tf.ids = f.ids[i+1:]\n\t\t\t} else {\n\t\t\t\tcopy(f.ids[i-n+1:], f.ids[i+1:])\n\t\t\t\tf.ids = f.ids[:len(f.ids)-n]\n\t\t\t}\n\n\t\t\t// Remove from the free cache.\n\t\t\tfor i := pgid(0); i < pgid(n); i++ {\n\t\t\t\tdelete(f.cache, initial+i)\n\t\t\t}\n\n\t\t\treturn initial\n\t\t}\n\n\t\tprevid = id\n\t}\n\treturn 0\n}\n\n// free releases a page and its overflow for a given transaction id.\n// If the page is already free then a panic will occur.\nfunc (f *freelist) free(txid txid, p *page) {\n\tif p.id <= 1 {\n\t\tpanic(fmt.Sprintf(\"cannot free page 0 or 1: %d\", p.id))\n\t}\n\n\t// Free page and all its overflow pages.\n\tvar ids = f.pending[txid]\n\tfor id := p.id; id <= p.id+pgid(p.overflow); id++ {\n\t\t// Verify that page is not already free.\n\t\tif f.cache[id] {\n\t\t\tpanic(fmt.Sprintf(\"page %d already freed\", id))\n\t\t}\n\n\t\t// Add to the freelist and cache.\n\t\tids = append(ids, id)\n\t\tf.cache[id] = true\n\t}\n\tf.pending[txid] = ids\n}\n\n// release moves all page ids for a transaction id (or older) to the freelist.\nfunc (f *freelist) release(txid txid) {\n\tm := make(pgids, 0)\n\tfor tid, ids := range f.pending {\n\t\tif tid <= txid {\n\t\t\t// Move transaction's pending pages to the available freelist.\n\t\t\t// Don't remove from the cache since the page is still free.\n\t\t\tm = append(m, ids...)\n\t\t\tdelete(f.pending, tid)\n\t\t}\n\t}\n\tsort.Sort(m)\n\tf.ids = pgids(f.ids).merge(m)\n}\n\n// rollback removes the pages from a given pending tx.\nfunc (f *freelist) rollback(txid txid) {\n\t// Remove page ids from cache.\n\tfor _, id := range f.pending[txid] {\n\t\tdelete(f.cache, id)\n\t}\n\n\t// Remove pages from pending list.\n\tdelete(f.pending, txid)\n}\n\n// freed returns whether a given page is in the free list.\nfunc (f *freelist) freed(pgid pgid) bool {\n\treturn f.cache[pgid]\n}\n\n// read initializes the freelist from a freelist page.\nfunc (f *freelist) read(p *page) {\n\t// If the page.count is at the max uint16 value (64k) then it's considered\n\t// an overflow and the size of the freelist is stored as the first element.\n\tidx, count := 0, int(p.count)\n\tif count == 0xFFFF {\n\t\tidx = 1\n\t\tcount = int(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0])\n\t}\n\n\t// Copy the list of page ids from the freelist.\n\tif count == 0 {\n\t\tf.ids = nil\n\t} else {\n\t\tids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]\n\t\tf.ids = make([]pgid, len(ids))\n\t\tcopy(f.ids, ids)\n\n\t\t// Make sure they're sorted.\n\t\tsort.Sort(pgids(f.ids))\n\t}\n\n\t// Rebuild the page cache.\n\tf.reindex()\n}\n\n// write writes the page ids onto a freelist page. All free and pending ids are\n// saved to disk since in the event of a program crash, all pending ids will\n// become free.\nfunc (f *freelist) write(p *page) error {\n\t// Combine the old free pgids and pgids waiting on an open transaction.\n\n\t// Update the header flag.\n\tp.flags |= freelistPageFlag\n\n\t// The page.count can only hold up to 64k elements so if we overflow that\n\t// number then we handle it by putting the size in the first element.\n\tlenids := f.count()\n\tif lenids == 0 {\n\t\tp.count = uint16(lenids)\n\t} else if lenids < 0xFFFF {\n\t\tp.count = uint16(lenids)\n\t\tf.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:])\n\t} else {\n\t\tp.count = 0xFFFF\n\t\t((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(lenids)\n\t\tf.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:])\n\t}\n\n\treturn nil\n}\n\n// reload reads the freelist from a page and filters out pending items.\nfunc (f *freelist) reload(p *page) {\n\tf.read(p)\n\n\t// Build a cache of only pending pages.\n\tpcache := make(map[pgid]bool)\n\tfor _, pendingIDs := range f.pending {\n\t\tfor _, pendingID := range pendingIDs {\n\t\t\tpcache[pendingID] = true\n\t\t}\n\t}\n\n\t// Check each page in the freelist and build a new available freelist\n\t// with any pages not in the pending lists.\n\tvar a []pgid\n\tfor _, id := range f.ids {\n\t\tif !pcache[id] {\n\t\t\ta = append(a, id)\n\t\t}\n\t}\n\tf.ids = a\n\n\t// Once the available list is rebuilt then rebuild the free cache so that\n\t// it includes the available and pending free pages.\n\tf.reindex()\n}\n\n// reindex rebuilds the free cache based on available and pending free lists.\nfunc (f *freelist) reindex() {\n\tf.cache = make(map[pgid]bool, len(f.ids))\n\tfor _, id := range f.ids {\n\t\tf.cache[id] = true\n\t}\n\tfor _, pendingIDs := range f.pending {\n\t\tfor _, pendingID := range pendingIDs {\n\t\t\tf.cache[pendingID] = true\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "freelist_test.go",
    "content": "package bolt\n\nimport (\n\t\"math/rand\"\n\t\"reflect\"\n\t\"sort\"\n\t\"testing\"\n\t\"unsafe\"\n)\n\n// Ensure that a page is added to a transaction's freelist.\nfunc TestFreelist_free(t *testing.T) {\n\tf := newFreelist()\n\tf.free(100, &page{id: 12})\n\tif !reflect.DeepEqual([]pgid{12}, f.pending[100]) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", []pgid{12}, f.pending[100])\n\t}\n}\n\n// Ensure that a page and its overflow is added to a transaction's freelist.\nfunc TestFreelist_free_overflow(t *testing.T) {\n\tf := newFreelist()\n\tf.free(100, &page{id: 12, overflow: 3})\n\tif exp := []pgid{12, 13, 14, 15}; !reflect.DeepEqual(exp, f.pending[100]) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.pending[100])\n\t}\n}\n\n// Ensure that a transaction's free pages can be released.\nfunc TestFreelist_release(t *testing.T) {\n\tf := newFreelist()\n\tf.free(100, &page{id: 12, overflow: 1})\n\tf.free(100, &page{id: 9})\n\tf.free(102, &page{id: 39})\n\tf.release(100)\n\tf.release(101)\n\tif exp := []pgid{9, 12, 13}; !reflect.DeepEqual(exp, f.ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.ids)\n\t}\n\n\tf.release(102)\n\tif exp := []pgid{9, 12, 13, 39}; !reflect.DeepEqual(exp, f.ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.ids)\n\t}\n}\n\n// Ensure that a freelist can find contiguous blocks of pages.\nfunc TestFreelist_allocate(t *testing.T) {\n\tf := &freelist{ids: []pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}}\n\tif id := int(f.allocate(3)); id != 3 {\n\t\tt.Fatalf(\"exp=3; got=%v\", id)\n\t}\n\tif id := int(f.allocate(1)); id != 6 {\n\t\tt.Fatalf(\"exp=6; got=%v\", id)\n\t}\n\tif id := int(f.allocate(3)); id != 0 {\n\t\tt.Fatalf(\"exp=0; got=%v\", id)\n\t}\n\tif id := int(f.allocate(2)); id != 12 {\n\t\tt.Fatalf(\"exp=12; got=%v\", id)\n\t}\n\tif id := int(f.allocate(1)); id != 7 {\n\t\tt.Fatalf(\"exp=7; got=%v\", id)\n\t}\n\tif id := int(f.allocate(0)); id != 0 {\n\t\tt.Fatalf(\"exp=0; got=%v\", id)\n\t}\n\tif id := int(f.allocate(0)); id != 0 {\n\t\tt.Fatalf(\"exp=0; got=%v\", id)\n\t}\n\tif exp := []pgid{9, 18}; !reflect.DeepEqual(exp, f.ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.ids)\n\t}\n\n\tif id := int(f.allocate(1)); id != 9 {\n\t\tt.Fatalf(\"exp=9; got=%v\", id)\n\t}\n\tif id := int(f.allocate(1)); id != 18 {\n\t\tt.Fatalf(\"exp=18; got=%v\", id)\n\t}\n\tif id := int(f.allocate(1)); id != 0 {\n\t\tt.Fatalf(\"exp=0; got=%v\", id)\n\t}\n\tif exp := []pgid{}; !reflect.DeepEqual(exp, f.ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.ids)\n\t}\n}\n\n// Ensure that a freelist can deserialize from a freelist page.\nfunc TestFreelist_read(t *testing.T) {\n\t// Create a page.\n\tvar buf [4096]byte\n\tpage := (*page)(unsafe.Pointer(&buf[0]))\n\tpage.flags = freelistPageFlag\n\tpage.count = 2\n\n\t// Insert 2 page ids.\n\tids := (*[3]pgid)(unsafe.Pointer(&page.ptr))\n\tids[0] = 23\n\tids[1] = 50\n\n\t// Deserialize page into a freelist.\n\tf := newFreelist()\n\tf.read(page)\n\n\t// Ensure that there are two page ids in the freelist.\n\tif exp := []pgid{23, 50}; !reflect.DeepEqual(exp, f.ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f.ids)\n\t}\n}\n\n// Ensure that a freelist can serialize into a freelist page.\nfunc TestFreelist_write(t *testing.T) {\n\t// Create a freelist and write it to a page.\n\tvar buf [4096]byte\n\tf := &freelist{ids: []pgid{12, 39}, pending: make(map[txid][]pgid)}\n\tf.pending[100] = []pgid{28, 11}\n\tf.pending[101] = []pgid{3}\n\tp := (*page)(unsafe.Pointer(&buf[0]))\n\tif err := f.write(p); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read the page back out.\n\tf2 := newFreelist()\n\tf2.read(p)\n\n\t// Ensure that the freelist is correct.\n\t// All pages should be present and in reverse order.\n\tif exp := []pgid{3, 11, 12, 28, 39}; !reflect.DeepEqual(exp, f2.ids) {\n\t\tt.Fatalf(\"exp=%v; got=%v\", exp, f2.ids)\n\t}\n}\n\nfunc Benchmark_FreelistRelease10K(b *testing.B)    { benchmark_FreelistRelease(b, 10000) }\nfunc Benchmark_FreelistRelease100K(b *testing.B)   { benchmark_FreelistRelease(b, 100000) }\nfunc Benchmark_FreelistRelease1000K(b *testing.B)  { benchmark_FreelistRelease(b, 1000000) }\nfunc Benchmark_FreelistRelease10000K(b *testing.B) { benchmark_FreelistRelease(b, 10000000) }\n\nfunc benchmark_FreelistRelease(b *testing.B, size int) {\n\tids := randomPgids(size)\n\tpending := randomPgids(len(ids) / 400)\n\tb.ResetTimer()\n\tfor i := 0; i < b.N; i++ {\n\t\tf := &freelist{ids: ids, pending: map[txid][]pgid{1: pending}}\n\t\tf.release(1)\n\t}\n}\n\nfunc randomPgids(n int) []pgid {\n\trand.Seed(42)\n\tpgids := make(pgids, n)\n\tfor i := range pgids {\n\t\tpgids[i] = pgid(rand.Int63())\n\t}\n\tsort.Sort(pgids)\n\treturn pgids\n}\n"
  },
  {
    "path": "node.go",
    "content": "package bolt\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"sort\"\n\t\"unsafe\"\n)\n\n// node represents an in-memory, deserialized page.\ntype node struct {\n\tbucket     *Bucket\n\tisLeaf     bool\n\tunbalanced bool\n\tspilled    bool\n\tkey        []byte\n\tpgid       pgid\n\tparent     *node\n\tchildren   nodes\n\tinodes     inodes\n}\n\n// root returns the top-level node this node is attached to.\nfunc (n *node) root() *node {\n\tif n.parent == nil {\n\t\treturn n\n\t}\n\treturn n.parent.root()\n}\n\n// minKeys returns the minimum number of inodes this node should have.\nfunc (n *node) minKeys() int {\n\tif n.isLeaf {\n\t\treturn 1\n\t}\n\treturn 2\n}\n\n// size returns the size of the node after serialization.\nfunc (n *node) size() int {\n\tsz, elsz := pageHeaderSize, n.pageElementSize()\n\tfor i := 0; i < len(n.inodes); i++ {\n\t\titem := &n.inodes[i]\n\t\tsz += elsz + len(item.key) + len(item.value)\n\t}\n\treturn sz\n}\n\n// sizeLessThan returns true if the node is less than a given size.\n// This is an optimization to avoid calculating a large node when we only need\n// to know if it fits inside a certain page size.\nfunc (n *node) sizeLessThan(v int) bool {\n\tsz, elsz := pageHeaderSize, n.pageElementSize()\n\tfor i := 0; i < len(n.inodes); i++ {\n\t\titem := &n.inodes[i]\n\t\tsz += elsz + len(item.key) + len(item.value)\n\t\tif sz >= v {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// pageElementSize returns the size of each page element based on the type of node.\nfunc (n *node) pageElementSize() int {\n\tif n.isLeaf {\n\t\treturn leafPageElementSize\n\t}\n\treturn branchPageElementSize\n}\n\n// childAt returns the child node at a given index.\nfunc (n *node) childAt(index int) *node {\n\tif n.isLeaf {\n\t\tpanic(fmt.Sprintf(\"invalid childAt(%d) on a leaf node\", index))\n\t}\n\treturn n.bucket.node(n.inodes[index].pgid, n)\n}\n\n// childIndex returns the index of a given child node.\nfunc (n *node) childIndex(child *node) int {\n\tindex := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, child.key) != -1 })\n\treturn index\n}\n\n// numChildren returns the number of children.\nfunc (n *node) numChildren() int {\n\treturn len(n.inodes)\n}\n\n// nextSibling returns the next node with the same parent.\nfunc (n *node) nextSibling() *node {\n\tif n.parent == nil {\n\t\treturn nil\n\t}\n\tindex := n.parent.childIndex(n)\n\tif index >= n.parent.numChildren()-1 {\n\t\treturn nil\n\t}\n\treturn n.parent.childAt(index + 1)\n}\n\n// prevSibling returns the previous node with the same parent.\nfunc (n *node) prevSibling() *node {\n\tif n.parent == nil {\n\t\treturn nil\n\t}\n\tindex := n.parent.childIndex(n)\n\tif index == 0 {\n\t\treturn nil\n\t}\n\treturn n.parent.childAt(index - 1)\n}\n\n// put inserts a key/value.\nfunc (n *node) put(oldKey, newKey, value []byte, pgid pgid, flags uint32) {\n\tif pgid >= n.bucket.tx.meta.pgid {\n\t\tpanic(fmt.Sprintf(\"pgid (%d) above high water mark (%d)\", pgid, n.bucket.tx.meta.pgid))\n\t} else if len(oldKey) <= 0 {\n\t\tpanic(\"put: zero-length old key\")\n\t} else if len(newKey) <= 0 {\n\t\tpanic(\"put: zero-length new key\")\n\t}\n\n\t// Find insertion index.\n\tindex := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, oldKey) != -1 })\n\n\t// Add capacity and shift nodes if we don't have an exact match and need to insert.\n\texact := (len(n.inodes) > 0 && index < len(n.inodes) && bytes.Equal(n.inodes[index].key, oldKey))\n\tif !exact {\n\t\tn.inodes = append(n.inodes, inode{})\n\t\tcopy(n.inodes[index+1:], n.inodes[index:])\n\t}\n\n\tinode := &n.inodes[index]\n\tinode.flags = flags\n\tinode.key = newKey\n\tinode.value = value\n\tinode.pgid = pgid\n\t_assert(len(inode.key) > 0, \"put: zero-length inode key\")\n}\n\n// del removes a key from the node.\nfunc (n *node) del(key []byte) {\n\t// Find index of key.\n\tindex := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, key) != -1 })\n\n\t// Exit if the key isn't found.\n\tif index >= len(n.inodes) || !bytes.Equal(n.inodes[index].key, key) {\n\t\treturn\n\t}\n\n\t// Delete inode from the node.\n\tn.inodes = append(n.inodes[:index], n.inodes[index+1:]...)\n\n\t// Mark the node as needing rebalancing.\n\tn.unbalanced = true\n}\n\n// read initializes the node from a page.\nfunc (n *node) read(p *page) {\n\tn.pgid = p.id\n\tn.isLeaf = ((p.flags & leafPageFlag) != 0)\n\tn.inodes = make(inodes, int(p.count))\n\n\tfor i := 0; i < int(p.count); i++ {\n\t\tinode := &n.inodes[i]\n\t\tif n.isLeaf {\n\t\t\telem := p.leafPageElement(uint16(i))\n\t\t\tinode.flags = elem.flags\n\t\t\tinode.key = elem.key()\n\t\t\tinode.value = elem.value()\n\t\t} else {\n\t\t\telem := p.branchPageElement(uint16(i))\n\t\t\tinode.pgid = elem.pgid\n\t\t\tinode.key = elem.key()\n\t\t}\n\t\t_assert(len(inode.key) > 0, \"read: zero-length inode key\")\n\t}\n\n\t// Save first key so we can find the node in the parent when we spill.\n\tif len(n.inodes) > 0 {\n\t\tn.key = n.inodes[0].key\n\t\t_assert(len(n.key) > 0, \"read: zero-length node key\")\n\t} else {\n\t\tn.key = nil\n\t}\n}\n\n// write writes the items onto one or more pages.\nfunc (n *node) write(p *page) {\n\t// Initialize page.\n\tif n.isLeaf {\n\t\tp.flags |= leafPageFlag\n\t} else {\n\t\tp.flags |= branchPageFlag\n\t}\n\n\tif len(n.inodes) >= 0xFFFF {\n\t\tpanic(fmt.Sprintf(\"inode overflow: %d (pgid=%d)\", len(n.inodes), p.id))\n\t}\n\tp.count = uint16(len(n.inodes))\n\n\t// Stop here if there are no items to write.\n\tif p.count == 0 {\n\t\treturn\n\t}\n\n\t// Loop over each item and write it to the page.\n\tb := (*[maxAllocSize]byte)(unsafe.Pointer(&p.ptr))[n.pageElementSize()*len(n.inodes):]\n\tfor i, item := range n.inodes {\n\t\t_assert(len(item.key) > 0, \"write: zero-length inode key\")\n\n\t\t// Write the page element.\n\t\tif n.isLeaf {\n\t\t\telem := p.leafPageElement(uint16(i))\n\t\t\telem.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem)))\n\t\t\telem.flags = item.flags\n\t\t\telem.ksize = uint32(len(item.key))\n\t\t\telem.vsize = uint32(len(item.value))\n\t\t} else {\n\t\t\telem := p.branchPageElement(uint16(i))\n\t\t\telem.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem)))\n\t\t\telem.ksize = uint32(len(item.key))\n\t\t\telem.pgid = item.pgid\n\t\t\t_assert(elem.pgid != p.id, \"write: circular dependency occurred\")\n\t\t}\n\n\t\t// If the length of key+value is larger than the max allocation size\n\t\t// then we need to reallocate the byte array pointer.\n\t\t//\n\t\t// See: https://github.com/boltdb/bolt/pull/335\n\t\tklen, vlen := len(item.key), len(item.value)\n\t\tif len(b) < klen+vlen {\n\t\t\tb = (*[maxAllocSize]byte)(unsafe.Pointer(&b[0]))[:]\n\t\t}\n\n\t\t// Write data for the element to the end of the page.\n\t\tcopy(b[0:], item.key)\n\t\tb = b[klen:]\n\t\tcopy(b[0:], item.value)\n\t\tb = b[vlen:]\n\t}\n\n\t// DEBUG ONLY: n.dump()\n}\n\n// split breaks up a node into multiple smaller nodes, if appropriate.\n// This should only be called from the spill() function.\nfunc (n *node) split(pageSize int) []*node {\n\tvar nodes []*node\n\n\tnode := n\n\tfor {\n\t\t// Split node into two.\n\t\ta, b := node.splitTwo(pageSize)\n\t\tnodes = append(nodes, a)\n\n\t\t// If we can't split then exit the loop.\n\t\tif b == nil {\n\t\t\tbreak\n\t\t}\n\n\t\t// Set node to b so it gets split on the next iteration.\n\t\tnode = b\n\t}\n\n\treturn nodes\n}\n\n// splitTwo breaks up a node into two smaller nodes, if appropriate.\n// This should only be called from the split() function.\nfunc (n *node) splitTwo(pageSize int) (*node, *node) {\n\t// Ignore the split if the page doesn't have at least enough nodes for\n\t// two pages or if the nodes can fit in a single page.\n\tif len(n.inodes) <= (minKeysPerPage*2) || n.sizeLessThan(pageSize) {\n\t\treturn n, nil\n\t}\n\n\t// Determine the threshold before starting a new node.\n\tvar fillPercent = n.bucket.FillPercent\n\tif fillPercent < minFillPercent {\n\t\tfillPercent = minFillPercent\n\t} else if fillPercent > maxFillPercent {\n\t\tfillPercent = maxFillPercent\n\t}\n\tthreshold := int(float64(pageSize) * fillPercent)\n\n\t// Determine split position and sizes of the two pages.\n\tsplitIndex, _ := n.splitIndex(threshold)\n\n\t// Split node into two separate nodes.\n\t// If there's no parent then we'll need to create one.\n\tif n.parent == nil {\n\t\tn.parent = &node{bucket: n.bucket, children: []*node{n}}\n\t}\n\n\t// Create a new node and add it to the parent.\n\tnext := &node{bucket: n.bucket, isLeaf: n.isLeaf, parent: n.parent}\n\tn.parent.children = append(n.parent.children, next)\n\n\t// Split inodes across two nodes.\n\tnext.inodes = n.inodes[splitIndex:]\n\tn.inodes = n.inodes[:splitIndex]\n\n\t// Update the statistics.\n\tn.bucket.tx.stats.Split++\n\n\treturn n, next\n}\n\n// splitIndex finds the position where a page will fill a given threshold.\n// It returns the index as well as the size of the first page.\n// This is only be called from split().\nfunc (n *node) splitIndex(threshold int) (index, sz int) {\n\tsz = pageHeaderSize\n\n\t// Loop until we only have the minimum number of keys required for the second page.\n\tfor i := 0; i < len(n.inodes)-minKeysPerPage; i++ {\n\t\tindex = i\n\t\tinode := n.inodes[i]\n\t\telsize := n.pageElementSize() + len(inode.key) + len(inode.value)\n\n\t\t// If we have at least the minimum number of keys and adding another\n\t\t// node would put us over the threshold then exit and return.\n\t\tif i >= minKeysPerPage && sz+elsize > threshold {\n\t\t\tbreak\n\t\t}\n\n\t\t// Add the element size to the total size.\n\t\tsz += elsize\n\t}\n\n\treturn\n}\n\n// spill writes the nodes to dirty pages and splits nodes as it goes.\n// Returns an error if dirty pages cannot be allocated.\nfunc (n *node) spill() error {\n\tvar tx = n.bucket.tx\n\tif n.spilled {\n\t\treturn nil\n\t}\n\n\t// Spill child nodes first. Child nodes can materialize sibling nodes in\n\t// the case of split-merge so we cannot use a range loop. We have to check\n\t// the children size on every loop iteration.\n\tsort.Sort(n.children)\n\tfor i := 0; i < len(n.children); i++ {\n\t\tif err := n.children[i].spill(); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// We no longer need the child list because it's only used for spill tracking.\n\tn.children = nil\n\n\t// Split nodes into appropriate sizes. The first node will always be n.\n\tvar nodes = n.split(tx.db.pageSize)\n\tfor _, node := range nodes {\n\t\t// Add node's page to the freelist if it's not new.\n\t\tif node.pgid > 0 {\n\t\t\ttx.db.freelist.free(tx.meta.txid, tx.page(node.pgid))\n\t\t\tnode.pgid = 0\n\t\t}\n\n\t\t// Allocate contiguous space for the node.\n\t\tp, err := tx.allocate((node.size() / tx.db.pageSize) + 1)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Write the node.\n\t\tif p.id >= tx.meta.pgid {\n\t\t\tpanic(fmt.Sprintf(\"pgid (%d) above high water mark (%d)\", p.id, tx.meta.pgid))\n\t\t}\n\t\tnode.pgid = p.id\n\t\tnode.write(p)\n\t\tnode.spilled = true\n\n\t\t// Insert into parent inodes.\n\t\tif node.parent != nil {\n\t\t\tvar key = node.key\n\t\t\tif key == nil {\n\t\t\t\tkey = node.inodes[0].key\n\t\t\t}\n\n\t\t\tnode.parent.put(key, node.inodes[0].key, nil, node.pgid, 0)\n\t\t\tnode.key = node.inodes[0].key\n\t\t\t_assert(len(node.key) > 0, \"spill: zero-length node key\")\n\t\t}\n\n\t\t// Update the statistics.\n\t\ttx.stats.Spill++\n\t}\n\n\t// If the root node split and created a new root then we need to spill that\n\t// as well. We'll clear out the children to make sure it doesn't try to respill.\n\tif n.parent != nil && n.parent.pgid == 0 {\n\t\tn.children = nil\n\t\treturn n.parent.spill()\n\t}\n\n\treturn nil\n}\n\n// rebalance attempts to combine the node with sibling nodes if the node fill\n// size is below a threshold or if there are not enough keys.\nfunc (n *node) rebalance() {\n\tif !n.unbalanced {\n\t\treturn\n\t}\n\tn.unbalanced = false\n\n\t// Update statistics.\n\tn.bucket.tx.stats.Rebalance++\n\n\t// Ignore if node is above threshold (25%) and has enough keys.\n\tvar threshold = n.bucket.tx.db.pageSize / 4\n\tif n.size() > threshold && len(n.inodes) > n.minKeys() {\n\t\treturn\n\t}\n\n\t// Root node has special handling.\n\tif n.parent == nil {\n\t\t// If root node is a branch and only has one node then collapse it.\n\t\tif !n.isLeaf && len(n.inodes) == 1 {\n\t\t\t// Move root's child up.\n\t\t\tchild := n.bucket.node(n.inodes[0].pgid, n)\n\t\t\tn.isLeaf = child.isLeaf\n\t\t\tn.inodes = child.inodes[:]\n\t\t\tn.children = child.children\n\n\t\t\t// Reparent all child nodes being moved.\n\t\t\tfor _, inode := range n.inodes {\n\t\t\t\tif child, ok := n.bucket.nodes[inode.pgid]; ok {\n\t\t\t\t\tchild.parent = n\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Remove old child.\n\t\t\tchild.parent = nil\n\t\t\tdelete(n.bucket.nodes, child.pgid)\n\t\t\tchild.free()\n\t\t}\n\n\t\treturn\n\t}\n\n\t// If node has no keys then just remove it.\n\tif n.numChildren() == 0 {\n\t\tn.parent.del(n.key)\n\t\tn.parent.removeChild(n)\n\t\tdelete(n.bucket.nodes, n.pgid)\n\t\tn.free()\n\t\tn.parent.rebalance()\n\t\treturn\n\t}\n\n\t_assert(n.parent.numChildren() > 1, \"parent must have at least 2 children\")\n\n\t// Destination node is right sibling if idx == 0, otherwise left sibling.\n\tvar target *node\n\tvar useNextSibling = (n.parent.childIndex(n) == 0)\n\tif useNextSibling {\n\t\ttarget = n.nextSibling()\n\t} else {\n\t\ttarget = n.prevSibling()\n\t}\n\n\t// If both this node and the target node are too small then merge them.\n\tif useNextSibling {\n\t\t// Reparent all child nodes being moved.\n\t\tfor _, inode := range target.inodes {\n\t\t\tif child, ok := n.bucket.nodes[inode.pgid]; ok {\n\t\t\t\tchild.parent.removeChild(child)\n\t\t\t\tchild.parent = n\n\t\t\t\tchild.parent.children = append(child.parent.children, child)\n\t\t\t}\n\t\t}\n\n\t\t// Copy over inodes from target and remove target.\n\t\tn.inodes = append(n.inodes, target.inodes...)\n\t\tn.parent.del(target.key)\n\t\tn.parent.removeChild(target)\n\t\tdelete(n.bucket.nodes, target.pgid)\n\t\ttarget.free()\n\t} else {\n\t\t// Reparent all child nodes being moved.\n\t\tfor _, inode := range n.inodes {\n\t\t\tif child, ok := n.bucket.nodes[inode.pgid]; ok {\n\t\t\t\tchild.parent.removeChild(child)\n\t\t\t\tchild.parent = target\n\t\t\t\tchild.parent.children = append(child.parent.children, child)\n\t\t\t}\n\t\t}\n\n\t\t// Copy over inodes to target and remove node.\n\t\ttarget.inodes = append(target.inodes, n.inodes...)\n\t\tn.parent.del(n.key)\n\t\tn.parent.removeChild(n)\n\t\tdelete(n.bucket.nodes, n.pgid)\n\t\tn.free()\n\t}\n\n\t// Either this node or the target node was deleted from the parent so rebalance it.\n\tn.parent.rebalance()\n}\n\n// removes a node from the list of in-memory children.\n// This does not affect the inodes.\nfunc (n *node) removeChild(target *node) {\n\tfor i, child := range n.children {\n\t\tif child == target {\n\t\t\tn.children = append(n.children[:i], n.children[i+1:]...)\n\t\t\treturn\n\t\t}\n\t}\n}\n\n// dereference causes the node to copy all its inode key/value references to heap memory.\n// This is required when the mmap is reallocated so inodes are not pointing to stale data.\nfunc (n *node) dereference() {\n\tif n.key != nil {\n\t\tkey := make([]byte, len(n.key))\n\t\tcopy(key, n.key)\n\t\tn.key = key\n\t\t_assert(n.pgid == 0 || len(n.key) > 0, \"dereference: zero-length node key on existing node\")\n\t}\n\n\tfor i := range n.inodes {\n\t\tinode := &n.inodes[i]\n\n\t\tkey := make([]byte, len(inode.key))\n\t\tcopy(key, inode.key)\n\t\tinode.key = key\n\t\t_assert(len(inode.key) > 0, \"dereference: zero-length inode key\")\n\n\t\tvalue := make([]byte, len(inode.value))\n\t\tcopy(value, inode.value)\n\t\tinode.value = value\n\t}\n\n\t// Recursively dereference children.\n\tfor _, child := range n.children {\n\t\tchild.dereference()\n\t}\n\n\t// Update statistics.\n\tn.bucket.tx.stats.NodeDeref++\n}\n\n// free adds the node's underlying page to the freelist.\nfunc (n *node) free() {\n\tif n.pgid != 0 {\n\t\tn.bucket.tx.db.freelist.free(n.bucket.tx.meta.txid, n.bucket.tx.page(n.pgid))\n\t\tn.pgid = 0\n\t}\n}\n\n// dump writes the contents of the node to STDERR for debugging purposes.\n/*\nfunc (n *node) dump() {\n\t// Write node header.\n\tvar typ = \"branch\"\n\tif n.isLeaf {\n\t\ttyp = \"leaf\"\n\t}\n\twarnf(\"[NODE %d {type=%s count=%d}]\", n.pgid, typ, len(n.inodes))\n\n\t// Write out abbreviated version of each item.\n\tfor _, item := range n.inodes {\n\t\tif n.isLeaf {\n\t\t\tif item.flags&bucketLeafFlag != 0 {\n\t\t\t\tbucket := (*bucket)(unsafe.Pointer(&item.value[0]))\n\t\t\t\twarnf(\"+L %08x -> (bucket root=%d)\", trunc(item.key, 4), bucket.root)\n\t\t\t} else {\n\t\t\t\twarnf(\"+L %08x -> %08x\", trunc(item.key, 4), trunc(item.value, 4))\n\t\t\t}\n\t\t} else {\n\t\t\twarnf(\"+B %08x -> pgid=%d\", trunc(item.key, 4), item.pgid)\n\t\t}\n\t}\n\twarn(\"\")\n}\n*/\n\ntype nodes []*node\n\nfunc (s nodes) Len() int           { return len(s) }\nfunc (s nodes) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }\nfunc (s nodes) Less(i, j int) bool { return bytes.Compare(s[i].inodes[0].key, s[j].inodes[0].key) == -1 }\n\n// inode represents an internal node inside of a node.\n// It can be used to point to elements in a page or point\n// to an element which hasn't been added to a page yet.\ntype inode struct {\n\tflags uint32\n\tpgid  pgid\n\tkey   []byte\n\tvalue []byte\n}\n\ntype inodes []inode\n"
  },
  {
    "path": "node_test.go",
    "content": "package bolt\n\nimport (\n\t\"testing\"\n\t\"unsafe\"\n)\n\n// Ensure that a node can insert a key/value.\nfunc TestNode_put(t *testing.T) {\n\tn := &node{inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{meta: &meta{pgid: 1}}}}\n\tn.put([]byte(\"baz\"), []byte(\"baz\"), []byte(\"2\"), 0, 0)\n\tn.put([]byte(\"foo\"), []byte(\"foo\"), []byte(\"0\"), 0, 0)\n\tn.put([]byte(\"bar\"), []byte(\"bar\"), []byte(\"1\"), 0, 0)\n\tn.put([]byte(\"foo\"), []byte(\"foo\"), []byte(\"3\"), 0, leafPageFlag)\n\n\tif len(n.inodes) != 3 {\n\t\tt.Fatalf(\"exp=3; got=%d\", len(n.inodes))\n\t}\n\tif k, v := n.inodes[0].key, n.inodes[0].value; string(k) != \"bar\" || string(v) != \"1\" {\n\t\tt.Fatalf(\"exp=<bar,1>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n.inodes[1].key, n.inodes[1].value; string(k) != \"baz\" || string(v) != \"2\" {\n\t\tt.Fatalf(\"exp=<baz,2>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n.inodes[2].key, n.inodes[2].value; string(k) != \"foo\" || string(v) != \"3\" {\n\t\tt.Fatalf(\"exp=<foo,3>; got=<%s,%s>\", k, v)\n\t}\n\tif n.inodes[2].flags != uint32(leafPageFlag) {\n\t\tt.Fatalf(\"not a leaf: %d\", n.inodes[2].flags)\n\t}\n}\n\n// Ensure that a node can deserialize from a leaf page.\nfunc TestNode_read_LeafPage(t *testing.T) {\n\t// Create a page.\n\tvar buf [4096]byte\n\tpage := (*page)(unsafe.Pointer(&buf[0]))\n\tpage.flags = leafPageFlag\n\tpage.count = 2\n\n\t// Insert 2 elements at the beginning. sizeof(leafPageElement) == 16\n\tnodes := (*[3]leafPageElement)(unsafe.Pointer(&page.ptr))\n\tnodes[0] = leafPageElement{flags: 0, pos: 32, ksize: 3, vsize: 4}  // pos = sizeof(leafPageElement) * 2\n\tnodes[1] = leafPageElement{flags: 0, pos: 23, ksize: 10, vsize: 3} // pos = sizeof(leafPageElement) + 3 + 4\n\n\t// Write data for the nodes at the end.\n\tdata := (*[4096]byte)(unsafe.Pointer(&nodes[2]))\n\tcopy(data[:], []byte(\"barfooz\"))\n\tcopy(data[7:], []byte(\"helloworldbye\"))\n\n\t// Deserialize page into a leaf.\n\tn := &node{}\n\tn.read(page)\n\n\t// Check that there are two inodes with correct data.\n\tif !n.isLeaf {\n\t\tt.Fatal(\"expected leaf\")\n\t}\n\tif len(n.inodes) != 2 {\n\t\tt.Fatalf(\"exp=2; got=%d\", len(n.inodes))\n\t}\n\tif k, v := n.inodes[0].key, n.inodes[0].value; string(k) != \"bar\" || string(v) != \"fooz\" {\n\t\tt.Fatalf(\"exp=<bar,fooz>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n.inodes[1].key, n.inodes[1].value; string(k) != \"helloworld\" || string(v) != \"bye\" {\n\t\tt.Fatalf(\"exp=<helloworld,bye>; got=<%s,%s>\", k, v)\n\t}\n}\n\n// Ensure that a node can serialize into a leaf page.\nfunc TestNode_write_LeafPage(t *testing.T) {\n\t// Create a node.\n\tn := &node{isLeaf: true, inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: &meta{pgid: 1}}}}\n\tn.put([]byte(\"susy\"), []byte(\"susy\"), []byte(\"que\"), 0, 0)\n\tn.put([]byte(\"ricki\"), []byte(\"ricki\"), []byte(\"lake\"), 0, 0)\n\tn.put([]byte(\"john\"), []byte(\"john\"), []byte(\"johnson\"), 0, 0)\n\n\t// Write it to a page.\n\tvar buf [4096]byte\n\tp := (*page)(unsafe.Pointer(&buf[0]))\n\tn.write(p)\n\n\t// Read the page back in.\n\tn2 := &node{}\n\tn2.read(p)\n\n\t// Check that the two pages are the same.\n\tif len(n2.inodes) != 3 {\n\t\tt.Fatalf(\"exp=3; got=%d\", len(n2.inodes))\n\t}\n\tif k, v := n2.inodes[0].key, n2.inodes[0].value; string(k) != \"john\" || string(v) != \"johnson\" {\n\t\tt.Fatalf(\"exp=<john,johnson>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n2.inodes[1].key, n2.inodes[1].value; string(k) != \"ricki\" || string(v) != \"lake\" {\n\t\tt.Fatalf(\"exp=<ricki,lake>; got=<%s,%s>\", k, v)\n\t}\n\tif k, v := n2.inodes[2].key, n2.inodes[2].value; string(k) != \"susy\" || string(v) != \"que\" {\n\t\tt.Fatalf(\"exp=<susy,que>; got=<%s,%s>\", k, v)\n\t}\n}\n\n// Ensure that a node can split into appropriate subgroups.\nfunc TestNode_split(t *testing.T) {\n\t// Create a node.\n\tn := &node{inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: &meta{pgid: 1}}}}\n\tn.put([]byte(\"00000001\"), []byte(\"00000001\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000002\"), []byte(\"00000002\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000003\"), []byte(\"00000003\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000004\"), []byte(\"00000004\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000005\"), []byte(\"00000005\"), []byte(\"0123456701234567\"), 0, 0)\n\n\t// Split between 2 & 3.\n\tn.split(100)\n\n\tvar parent = n.parent\n\tif len(parent.children) != 2 {\n\t\tt.Fatalf(\"exp=2; got=%d\", len(parent.children))\n\t}\n\tif len(parent.children[0].inodes) != 2 {\n\t\tt.Fatalf(\"exp=2; got=%d\", len(parent.children[0].inodes))\n\t}\n\tif len(parent.children[1].inodes) != 3 {\n\t\tt.Fatalf(\"exp=3; got=%d\", len(parent.children[1].inodes))\n\t}\n}\n\n// Ensure that a page with the minimum number of inodes just returns a single node.\nfunc TestNode_split_MinKeys(t *testing.T) {\n\t// Create a node.\n\tn := &node{inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: &meta{pgid: 1}}}}\n\tn.put([]byte(\"00000001\"), []byte(\"00000001\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000002\"), []byte(\"00000002\"), []byte(\"0123456701234567\"), 0, 0)\n\n\t// Split.\n\tn.split(20)\n\tif n.parent != nil {\n\t\tt.Fatalf(\"expected nil parent\")\n\t}\n}\n\n// Ensure that a node that has keys that all fit on a page just returns one leaf.\nfunc TestNode_split_SinglePage(t *testing.T) {\n\t// Create a node.\n\tn := &node{inodes: make(inodes, 0), bucket: &Bucket{tx: &Tx{db: &DB{}, meta: &meta{pgid: 1}}}}\n\tn.put([]byte(\"00000001\"), []byte(\"00000001\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000002\"), []byte(\"00000002\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000003\"), []byte(\"00000003\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000004\"), []byte(\"00000004\"), []byte(\"0123456701234567\"), 0, 0)\n\tn.put([]byte(\"00000005\"), []byte(\"00000005\"), []byte(\"0123456701234567\"), 0, 0)\n\n\t// Split.\n\tn.split(4096)\n\tif n.parent != nil {\n\t\tt.Fatalf(\"expected nil parent\")\n\t}\n}\n"
  },
  {
    "path": "page.go",
    "content": "package bolt\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"sort\"\n\t\"unsafe\"\n)\n\nconst pageHeaderSize = int(unsafe.Offsetof(((*page)(nil)).ptr))\n\nconst minKeysPerPage = 2\n\nconst branchPageElementSize = int(unsafe.Sizeof(branchPageElement{}))\nconst leafPageElementSize = int(unsafe.Sizeof(leafPageElement{}))\n\nconst (\n\tbranchPageFlag   = 0x01\n\tleafPageFlag     = 0x02\n\tmetaPageFlag     = 0x04\n\tfreelistPageFlag = 0x10\n)\n\nconst (\n\tbucketLeafFlag = 0x01\n)\n\ntype pgid uint64\n\ntype page struct {\n\tid       pgid\n\tflags    uint16\n\tcount    uint16\n\toverflow uint32\n\tptr      uintptr\n}\n\n// typ returns a human readable page type string used for debugging.\nfunc (p *page) typ() string {\n\tif (p.flags & branchPageFlag) != 0 {\n\t\treturn \"branch\"\n\t} else if (p.flags & leafPageFlag) != 0 {\n\t\treturn \"leaf\"\n\t} else if (p.flags & metaPageFlag) != 0 {\n\t\treturn \"meta\"\n\t} else if (p.flags & freelistPageFlag) != 0 {\n\t\treturn \"freelist\"\n\t}\n\treturn fmt.Sprintf(\"unknown<%02x>\", p.flags)\n}\n\n// meta returns a pointer to the metadata section of the page.\nfunc (p *page) meta() *meta {\n\treturn (*meta)(unsafe.Pointer(&p.ptr))\n}\n\n// leafPageElement retrieves the leaf node by index\nfunc (p *page) leafPageElement(index uint16) *leafPageElement {\n\tn := &((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[index]\n\treturn n\n}\n\n// leafPageElements retrieves a list of leaf nodes.\nfunc (p *page) leafPageElements() []leafPageElement {\n\tif p.count == 0 {\n\t\treturn nil\n\t}\n\treturn ((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[:]\n}\n\n// branchPageElement retrieves the branch node by index\nfunc (p *page) branchPageElement(index uint16) *branchPageElement {\n\treturn &((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[index]\n}\n\n// branchPageElements retrieves a list of branch nodes.\nfunc (p *page) branchPageElements() []branchPageElement {\n\tif p.count == 0 {\n\t\treturn nil\n\t}\n\treturn ((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[:]\n}\n\n// dump writes n bytes of the page to STDERR as hex output.\nfunc (p *page) hexdump(n int) {\n\tbuf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:n]\n\tfmt.Fprintf(os.Stderr, \"%x\\n\", buf)\n}\n\ntype pages []*page\n\nfunc (s pages) Len() int           { return len(s) }\nfunc (s pages) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }\nfunc (s pages) Less(i, j int) bool { return s[i].id < s[j].id }\n\n// branchPageElement represents a node on a branch page.\ntype branchPageElement struct {\n\tpos   uint32\n\tksize uint32\n\tpgid  pgid\n}\n\n// key returns a byte slice of the node key.\nfunc (n *branchPageElement) key() []byte {\n\tbuf := (*[maxAllocSize]byte)(unsafe.Pointer(n))\n\treturn (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize]\n}\n\n// leafPageElement represents a node on a leaf page.\ntype leafPageElement struct {\n\tflags uint32\n\tpos   uint32\n\tksize uint32\n\tvsize uint32\n}\n\n// key returns a byte slice of the node key.\nfunc (n *leafPageElement) key() []byte {\n\tbuf := (*[maxAllocSize]byte)(unsafe.Pointer(n))\n\treturn (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize:n.ksize]\n}\n\n// value returns a byte slice of the node value.\nfunc (n *leafPageElement) value() []byte {\n\tbuf := (*[maxAllocSize]byte)(unsafe.Pointer(n))\n\treturn (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos+n.ksize]))[:n.vsize:n.vsize]\n}\n\n// PageInfo represents human readable information about a page.\ntype PageInfo struct {\n\tID            int\n\tType          string\n\tCount         int\n\tOverflowCount int\n}\n\ntype pgids []pgid\n\nfunc (s pgids) Len() int           { return len(s) }\nfunc (s pgids) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }\nfunc (s pgids) Less(i, j int) bool { return s[i] < s[j] }\n\n// merge returns the sorted union of a and b.\nfunc (a pgids) merge(b pgids) pgids {\n\t// Return the opposite slice if one is nil.\n\tif len(a) == 0 {\n\t\treturn b\n\t}\n\tif len(b) == 0 {\n\t\treturn a\n\t}\n\tmerged := make(pgids, len(a)+len(b))\n\tmergepgids(merged, a, b)\n\treturn merged\n}\n\n// mergepgids copies the sorted union of a and b into dst.\n// If dst is too small, it panics.\nfunc mergepgids(dst, a, b pgids) {\n\tif len(dst) < len(a)+len(b) {\n\t\tpanic(fmt.Errorf(\"mergepgids bad len %d < %d + %d\", len(dst), len(a), len(b)))\n\t}\n\t// Copy in the opposite slice if one is nil.\n\tif len(a) == 0 {\n\t\tcopy(dst, b)\n\t\treturn\n\t}\n\tif len(b) == 0 {\n\t\tcopy(dst, a)\n\t\treturn\n\t}\n\n\t// Merged will hold all elements from both lists.\n\tmerged := dst[:0]\n\n\t// Assign lead to the slice with a lower starting value, follow to the higher value.\n\tlead, follow := a, b\n\tif b[0] < a[0] {\n\t\tlead, follow = b, a\n\t}\n\n\t// Continue while there are elements in the lead.\n\tfor len(lead) > 0 {\n\t\t// Merge largest prefix of lead that is ahead of follow[0].\n\t\tn := sort.Search(len(lead), func(i int) bool { return lead[i] > follow[0] })\n\t\tmerged = append(merged, lead[:n]...)\n\t\tif n >= len(lead) {\n\t\t\tbreak\n\t\t}\n\n\t\t// Swap lead and follow.\n\t\tlead, follow = follow, lead[n:]\n\t}\n\n\t// Append what's left in follow.\n\t_ = append(merged, follow...)\n}\n"
  },
  {
    "path": "page_test.go",
    "content": "package bolt\n\nimport (\n\t\"reflect\"\n\t\"sort\"\n\t\"testing\"\n\t\"testing/quick\"\n)\n\n// Ensure that the page type can be returned in human readable format.\nfunc TestPage_typ(t *testing.T) {\n\tif typ := (&page{flags: branchPageFlag}).typ(); typ != \"branch\" {\n\t\tt.Fatalf(\"exp=branch; got=%v\", typ)\n\t}\n\tif typ := (&page{flags: leafPageFlag}).typ(); typ != \"leaf\" {\n\t\tt.Fatalf(\"exp=leaf; got=%v\", typ)\n\t}\n\tif typ := (&page{flags: metaPageFlag}).typ(); typ != \"meta\" {\n\t\tt.Fatalf(\"exp=meta; got=%v\", typ)\n\t}\n\tif typ := (&page{flags: freelistPageFlag}).typ(); typ != \"freelist\" {\n\t\tt.Fatalf(\"exp=freelist; got=%v\", typ)\n\t}\n\tif typ := (&page{flags: 20000}).typ(); typ != \"unknown<4e20>\" {\n\t\tt.Fatalf(\"exp=unknown<4e20>; got=%v\", typ)\n\t}\n}\n\n// Ensure that the hexdump debugging function doesn't blow up.\nfunc TestPage_dump(t *testing.T) {\n\t(&page{id: 256}).hexdump(16)\n}\n\nfunc TestPgids_merge(t *testing.T) {\n\ta := pgids{4, 5, 6, 10, 11, 12, 13, 27}\n\tb := pgids{1, 3, 8, 9, 25, 30}\n\tc := a.merge(b)\n\tif !reflect.DeepEqual(c, pgids{1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30}) {\n\t\tt.Errorf(\"mismatch: %v\", c)\n\t}\n\n\ta = pgids{4, 5, 6, 10, 11, 12, 13, 27, 35, 36}\n\tb = pgids{8, 9, 25, 30}\n\tc = a.merge(b)\n\tif !reflect.DeepEqual(c, pgids{4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30, 35, 36}) {\n\t\tt.Errorf(\"mismatch: %v\", c)\n\t}\n}\n\nfunc TestPgids_merge_quick(t *testing.T) {\n\tif err := quick.Check(func(a, b pgids) bool {\n\t\t// Sort incoming lists.\n\t\tsort.Sort(a)\n\t\tsort.Sort(b)\n\n\t\t// Merge the two lists together.\n\t\tgot := a.merge(b)\n\n\t\t// The expected value should be the two lists combined and sorted.\n\t\texp := append(a, b...)\n\t\tsort.Sort(exp)\n\n\t\tif !reflect.DeepEqual(exp, got) {\n\t\t\tt.Errorf(\"\\nexp=%+v\\ngot=%+v\\n\", exp, got)\n\t\t\treturn false\n\t\t}\n\n\t\treturn true\n\t}, nil); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n"
  },
  {
    "path": "quick_test.go",
    "content": "package bolt_test\n\nimport (\n\t\"bytes\"\n\t\"flag\"\n\t\"fmt\"\n\t\"math/rand\"\n\t\"os\"\n\t\"reflect\"\n\t\"testing/quick\"\n\t\"time\"\n)\n\n// testing/quick defaults to 5 iterations and a random seed.\n// You can override these settings from the command line:\n//\n//   -quick.count     The number of iterations to perform.\n//   -quick.seed      The seed to use for randomizing.\n//   -quick.maxitems  The maximum number of items to insert into a DB.\n//   -quick.maxksize  The maximum size of a key.\n//   -quick.maxvsize  The maximum size of a value.\n//\n\nvar qcount, qseed, qmaxitems, qmaxksize, qmaxvsize int\n\nfunc init() {\n\tflag.IntVar(&qcount, \"quick.count\", 5, \"\")\n\tflag.IntVar(&qseed, \"quick.seed\", int(time.Now().UnixNano())%100000, \"\")\n\tflag.IntVar(&qmaxitems, \"quick.maxitems\", 1000, \"\")\n\tflag.IntVar(&qmaxksize, \"quick.maxksize\", 1024, \"\")\n\tflag.IntVar(&qmaxvsize, \"quick.maxvsize\", 1024, \"\")\n\tflag.Parse()\n\tfmt.Fprintln(os.Stderr, \"seed:\", qseed)\n\tfmt.Fprintf(os.Stderr, \"quick settings: count=%v, items=%v, ksize=%v, vsize=%v\\n\", qcount, qmaxitems, qmaxksize, qmaxvsize)\n}\n\nfunc qconfig() *quick.Config {\n\treturn &quick.Config{\n\t\tMaxCount: qcount,\n\t\tRand:     rand.New(rand.NewSource(int64(qseed))),\n\t}\n}\n\ntype testdata []testdataitem\n\nfunc (t testdata) Len() int           { return len(t) }\nfunc (t testdata) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }\nfunc (t testdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == -1 }\n\nfunc (t testdata) Generate(rand *rand.Rand, size int) reflect.Value {\n\tn := rand.Intn(qmaxitems-1) + 1\n\titems := make(testdata, n)\n\tused := make(map[string]bool)\n\tfor i := 0; i < n; i++ {\n\t\titem := &items[i]\n\t\t// Ensure that keys are unique by looping until we find one that we have not already used.\n\t\tfor {\n\t\t\titem.Key = randByteSlice(rand, 1, qmaxksize)\n\t\t\tif !used[string(item.Key)] {\n\t\t\t\tused[string(item.Key)] = true\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\titem.Value = randByteSlice(rand, 0, qmaxvsize)\n\t}\n\treturn reflect.ValueOf(items)\n}\n\ntype revtestdata []testdataitem\n\nfunc (t revtestdata) Len() int           { return len(t) }\nfunc (t revtestdata) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }\nfunc (t revtestdata) Less(i, j int) bool { return bytes.Compare(t[i].Key, t[j].Key) == 1 }\n\ntype testdataitem struct {\n\tKey   []byte\n\tValue []byte\n}\n\nfunc randByteSlice(rand *rand.Rand, minSize, maxSize int) []byte {\n\tn := rand.Intn(maxSize-minSize) + minSize\n\tb := make([]byte, n)\n\tfor i := 0; i < n; i++ {\n\t\tb[i] = byte(rand.Intn(255))\n\t}\n\treturn b\n}\n"
  },
  {
    "path": "simulation_test.go",
    "content": "package bolt_test\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"math/rand\"\n\t\"sync\"\n\t\"testing\"\n\n\t\"github.com/boltdb/bolt\"\n)\n\nfunc TestSimulate_1op_1p(t *testing.T)     { testSimulate(t, 1, 1) }\nfunc TestSimulate_10op_1p(t *testing.T)    { testSimulate(t, 10, 1) }\nfunc TestSimulate_100op_1p(t *testing.T)   { testSimulate(t, 100, 1) }\nfunc TestSimulate_1000op_1p(t *testing.T)  { testSimulate(t, 1000, 1) }\nfunc TestSimulate_10000op_1p(t *testing.T) { testSimulate(t, 10000, 1) }\n\nfunc TestSimulate_10op_10p(t *testing.T)    { testSimulate(t, 10, 10) }\nfunc TestSimulate_100op_10p(t *testing.T)   { testSimulate(t, 100, 10) }\nfunc TestSimulate_1000op_10p(t *testing.T)  { testSimulate(t, 1000, 10) }\nfunc TestSimulate_10000op_10p(t *testing.T) { testSimulate(t, 10000, 10) }\n\nfunc TestSimulate_100op_100p(t *testing.T)   { testSimulate(t, 100, 100) }\nfunc TestSimulate_1000op_100p(t *testing.T)  { testSimulate(t, 1000, 100) }\nfunc TestSimulate_10000op_100p(t *testing.T) { testSimulate(t, 10000, 100) }\n\nfunc TestSimulate_10000op_1000p(t *testing.T) { testSimulate(t, 10000, 1000) }\n\n// Randomly generate operations on a given database with multiple clients to ensure consistency and thread safety.\nfunc testSimulate(t *testing.T, threadCount, parallelism int) {\n\tif testing.Short() {\n\t\tt.Skip(\"skipping test in short mode.\")\n\t}\n\n\trand.Seed(int64(qseed))\n\n\t// A list of operations that readers and writers can perform.\n\tvar readerHandlers = []simulateHandler{simulateGetHandler}\n\tvar writerHandlers = []simulateHandler{simulateGetHandler, simulatePutHandler}\n\n\tvar versions = make(map[int]*QuickDB)\n\tversions[1] = NewQuickDB()\n\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar mutex sync.Mutex\n\n\t// Run n threads in parallel, each with their own operation.\n\tvar wg sync.WaitGroup\n\tvar threads = make(chan bool, parallelism)\n\tvar i int\n\tfor {\n\t\tthreads <- true\n\t\twg.Add(1)\n\t\twritable := ((rand.Int() % 100) < 20) // 20% writers\n\n\t\t// Choose an operation to execute.\n\t\tvar handler simulateHandler\n\t\tif writable {\n\t\t\thandler = writerHandlers[rand.Intn(len(writerHandlers))]\n\t\t} else {\n\t\t\thandler = readerHandlers[rand.Intn(len(readerHandlers))]\n\t\t}\n\n\t\t// Execute a thread for the given operation.\n\t\tgo func(writable bool, handler simulateHandler) {\n\t\t\tdefer wg.Done()\n\n\t\t\t// Start transaction.\n\t\t\ttx, err := db.Begin(writable)\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(\"tx begin: \", err)\n\t\t\t}\n\n\t\t\t// Obtain current state of the dataset.\n\t\t\tmutex.Lock()\n\t\t\tvar qdb = versions[tx.ID()]\n\t\t\tif writable {\n\t\t\t\tqdb = versions[tx.ID()-1].Copy()\n\t\t\t}\n\t\t\tmutex.Unlock()\n\n\t\t\t// Make sure we commit/rollback the tx at the end and update the state.\n\t\t\tif writable {\n\t\t\t\tdefer func() {\n\t\t\t\t\tmutex.Lock()\n\t\t\t\t\tversions[tx.ID()] = qdb\n\t\t\t\t\tmutex.Unlock()\n\n\t\t\t\t\tif err := tx.Commit(); err != nil {\n\t\t\t\t\t\tt.Fatal(err)\n\t\t\t\t\t}\n\t\t\t\t}()\n\t\t\t} else {\n\t\t\t\tdefer func() { _ = tx.Rollback() }()\n\t\t\t}\n\n\t\t\t// Ignore operation if we don't have data yet.\n\t\t\tif qdb == nil {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\t// Execute handler.\n\t\t\thandler(tx, qdb)\n\n\t\t\t// Release a thread back to the scheduling loop.\n\t\t\t<-threads\n\t\t}(writable, handler)\n\n\t\ti++\n\t\tif i > threadCount {\n\t\t\tbreak\n\t\t}\n\t}\n\n\t// Wait until all threads are done.\n\twg.Wait()\n}\n\ntype simulateHandler func(tx *bolt.Tx, qdb *QuickDB)\n\n// Retrieves a key from the database and verifies that it is what is expected.\nfunc simulateGetHandler(tx *bolt.Tx, qdb *QuickDB) {\n\t// Randomly retrieve an existing exist.\n\tkeys := qdb.Rand()\n\tif len(keys) == 0 {\n\t\treturn\n\t}\n\n\t// Retrieve root bucket.\n\tb := tx.Bucket(keys[0])\n\tif b == nil {\n\t\tpanic(fmt.Sprintf(\"bucket[0] expected: %08x\\n\", trunc(keys[0], 4)))\n\t}\n\n\t// Drill into nested buckets.\n\tfor _, key := range keys[1 : len(keys)-1] {\n\t\tb = b.Bucket(key)\n\t\tif b == nil {\n\t\t\tpanic(fmt.Sprintf(\"bucket[n] expected: %v -> %v\\n\", keys, key))\n\t\t}\n\t}\n\n\t// Verify key/value on the final bucket.\n\texpected := qdb.Get(keys)\n\tactual := b.Get(keys[len(keys)-1])\n\tif !bytes.Equal(actual, expected) {\n\t\tfmt.Println(\"=== EXPECTED ===\")\n\t\tfmt.Println(expected)\n\t\tfmt.Println(\"=== ACTUAL ===\")\n\t\tfmt.Println(actual)\n\t\tfmt.Println(\"=== END ===\")\n\t\tpanic(\"value mismatch\")\n\t}\n}\n\n// Inserts a key into the database.\nfunc simulatePutHandler(tx *bolt.Tx, qdb *QuickDB) {\n\tvar err error\n\tkeys, value := randKeys(), randValue()\n\n\t// Retrieve root bucket.\n\tb := tx.Bucket(keys[0])\n\tif b == nil {\n\t\tb, err = tx.CreateBucket(keys[0])\n\t\tif err != nil {\n\t\t\tpanic(\"create bucket: \" + err.Error())\n\t\t}\n\t}\n\n\t// Create nested buckets, if necessary.\n\tfor _, key := range keys[1 : len(keys)-1] {\n\t\tchild := b.Bucket(key)\n\t\tif child != nil {\n\t\t\tb = child\n\t\t} else {\n\t\t\tb, err = b.CreateBucket(key)\n\t\t\tif err != nil {\n\t\t\t\tpanic(\"create bucket: \" + err.Error())\n\t\t\t}\n\t\t}\n\t}\n\n\t// Insert into database.\n\tif err := b.Put(keys[len(keys)-1], value); err != nil {\n\t\tpanic(\"put: \" + err.Error())\n\t}\n\n\t// Insert into in-memory database.\n\tqdb.Put(keys, value)\n}\n\n// QuickDB is an in-memory database that replicates the functionality of the\n// Bolt DB type except that it is entirely in-memory. It is meant for testing\n// that the Bolt database is consistent.\ntype QuickDB struct {\n\tsync.RWMutex\n\tm map[string]interface{}\n}\n\n// NewQuickDB returns an instance of QuickDB.\nfunc NewQuickDB() *QuickDB {\n\treturn &QuickDB{m: make(map[string]interface{})}\n}\n\n// Get retrieves the value at a key path.\nfunc (db *QuickDB) Get(keys [][]byte) []byte {\n\tdb.RLock()\n\tdefer db.RUnlock()\n\n\tm := db.m\n\tfor _, key := range keys[:len(keys)-1] {\n\t\tvalue := m[string(key)]\n\t\tif value == nil {\n\t\t\treturn nil\n\t\t}\n\t\tswitch value := value.(type) {\n\t\tcase map[string]interface{}:\n\t\t\tm = value\n\t\tcase []byte:\n\t\t\treturn nil\n\t\t}\n\t}\n\n\t// Only return if it's a simple value.\n\tif value, ok := m[string(keys[len(keys)-1])].([]byte); ok {\n\t\treturn value\n\t}\n\treturn nil\n}\n\n// Put inserts a value into a key path.\nfunc (db *QuickDB) Put(keys [][]byte, value []byte) {\n\tdb.Lock()\n\tdefer db.Unlock()\n\n\t// Build buckets all the way down the key path.\n\tm := db.m\n\tfor _, key := range keys[:len(keys)-1] {\n\t\tif _, ok := m[string(key)].([]byte); ok {\n\t\t\treturn // Keypath intersects with a simple value. Do nothing.\n\t\t}\n\n\t\tif m[string(key)] == nil {\n\t\t\tm[string(key)] = make(map[string]interface{})\n\t\t}\n\t\tm = m[string(key)].(map[string]interface{})\n\t}\n\n\t// Insert value into the last key.\n\tm[string(keys[len(keys)-1])] = value\n}\n\n// Rand returns a random key path that points to a simple value.\nfunc (db *QuickDB) Rand() [][]byte {\n\tdb.RLock()\n\tdefer db.RUnlock()\n\tif len(db.m) == 0 {\n\t\treturn nil\n\t}\n\tvar keys [][]byte\n\tdb.rand(db.m, &keys)\n\treturn keys\n}\n\nfunc (db *QuickDB) rand(m map[string]interface{}, keys *[][]byte) {\n\ti, index := 0, rand.Intn(len(m))\n\tfor k, v := range m {\n\t\tif i == index {\n\t\t\t*keys = append(*keys, []byte(k))\n\t\t\tif v, ok := v.(map[string]interface{}); ok {\n\t\t\t\tdb.rand(v, keys)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\ti++\n\t}\n\tpanic(\"quickdb rand: out-of-range\")\n}\n\n// Copy copies the entire database.\nfunc (db *QuickDB) Copy() *QuickDB {\n\tdb.RLock()\n\tdefer db.RUnlock()\n\treturn &QuickDB{m: db.copy(db.m)}\n}\n\nfunc (db *QuickDB) copy(m map[string]interface{}) map[string]interface{} {\n\tclone := make(map[string]interface{}, len(m))\n\tfor k, v := range m {\n\t\tswitch v := v.(type) {\n\t\tcase map[string]interface{}:\n\t\t\tclone[k] = db.copy(v)\n\t\tdefault:\n\t\t\tclone[k] = v\n\t\t}\n\t}\n\treturn clone\n}\n\nfunc randKey() []byte {\n\tvar min, max = 1, 1024\n\tn := rand.Intn(max-min) + min\n\tb := make([]byte, n)\n\tfor i := 0; i < n; i++ {\n\t\tb[i] = byte(rand.Intn(255))\n\t}\n\treturn b\n}\n\nfunc randKeys() [][]byte {\n\tvar keys [][]byte\n\tvar count = rand.Intn(2) + 2\n\tfor i := 0; i < count; i++ {\n\t\tkeys = append(keys, randKey())\n\t}\n\treturn keys\n}\n\nfunc randValue() []byte {\n\tn := rand.Intn(8192)\n\tb := make([]byte, n)\n\tfor i := 0; i < n; i++ {\n\t\tb[i] = byte(rand.Intn(255))\n\t}\n\treturn b\n}\n"
  },
  {
    "path": "tx.go",
    "content": "package bolt\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"sort\"\n\t\"strings\"\n\t\"time\"\n\t\"unsafe\"\n)\n\n// txid represents the internal transaction identifier.\ntype txid uint64\n\n// Tx represents a read-only or read/write transaction on the database.\n// Read-only transactions can be used for retrieving values for keys and creating cursors.\n// Read/write transactions can create and remove buckets and create and remove keys.\n//\n// IMPORTANT: You must commit or rollback transactions when you are done with\n// them. Pages can not be reclaimed by the writer until no more transactions\n// are using them. A long running read transaction can cause the database to\n// quickly grow.\ntype Tx struct {\n\twritable       bool\n\tmanaged        bool\n\tdb             *DB\n\tmeta           *meta\n\troot           Bucket\n\tpages          map[pgid]*page\n\tstats          TxStats\n\tcommitHandlers []func()\n\n\t// WriteFlag specifies the flag for write-related methods like WriteTo().\n\t// Tx opens the database file with the specified flag to copy the data.\n\t//\n\t// By default, the flag is unset, which works well for mostly in-memory\n\t// workloads. For databases that are much larger than available RAM,\n\t// set the flag to syscall.O_DIRECT to avoid trashing the page cache.\n\tWriteFlag int\n}\n\n// init initializes the transaction.\nfunc (tx *Tx) init(db *DB) {\n\ttx.db = db\n\ttx.pages = nil\n\n\t// Copy the meta page since it can be changed by the writer.\n\ttx.meta = &meta{}\n\tdb.meta().copy(tx.meta)\n\n\t// Copy over the root bucket.\n\ttx.root = newBucket(tx)\n\ttx.root.bucket = &bucket{}\n\t*tx.root.bucket = tx.meta.root\n\n\t// Increment the transaction id and add a page cache for writable transactions.\n\tif tx.writable {\n\t\ttx.pages = make(map[pgid]*page)\n\t\ttx.meta.txid += txid(1)\n\t}\n}\n\n// ID returns the transaction id.\nfunc (tx *Tx) ID() int {\n\treturn int(tx.meta.txid)\n}\n\n// DB returns a reference to the database that created the transaction.\nfunc (tx *Tx) DB() *DB {\n\treturn tx.db\n}\n\n// Size returns current database size in bytes as seen by this transaction.\nfunc (tx *Tx) Size() int64 {\n\treturn int64(tx.meta.pgid) * int64(tx.db.pageSize)\n}\n\n// Writable returns whether the transaction can perform write operations.\nfunc (tx *Tx) Writable() bool {\n\treturn tx.writable\n}\n\n// Cursor creates a cursor associated with the root bucket.\n// All items in the cursor will return a nil value because all root bucket keys point to buckets.\n// The cursor is only valid as long as the transaction is open.\n// Do not use a cursor after the transaction is closed.\nfunc (tx *Tx) Cursor() *Cursor {\n\treturn tx.root.Cursor()\n}\n\n// Stats retrieves a copy of the current transaction statistics.\nfunc (tx *Tx) Stats() TxStats {\n\treturn tx.stats\n}\n\n// Bucket retrieves a bucket by name.\n// Returns nil if the bucket does not exist.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (tx *Tx) Bucket(name []byte) *Bucket {\n\treturn tx.root.Bucket(name)\n}\n\n// CreateBucket creates a new bucket.\n// Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (tx *Tx) CreateBucket(name []byte) (*Bucket, error) {\n\treturn tx.root.CreateBucket(name)\n}\n\n// CreateBucketIfNotExists creates a new bucket if it doesn't already exist.\n// Returns an error if the bucket name is blank, or if the bucket name is too long.\n// The bucket instance is only valid for the lifetime of the transaction.\nfunc (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) {\n\treturn tx.root.CreateBucketIfNotExists(name)\n}\n\n// DeleteBucket deletes a bucket.\n// Returns an error if the bucket cannot be found or if the key represents a non-bucket value.\nfunc (tx *Tx) DeleteBucket(name []byte) error {\n\treturn tx.root.DeleteBucket(name)\n}\n\n// ForEach executes a function for each bucket in the root.\n// If the provided function returns an error then the iteration is stopped and\n// the error is returned to the caller.\nfunc (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error {\n\treturn tx.root.ForEach(func(k, v []byte) error {\n\t\tif err := fn(k, tx.root.Bucket(k)); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t})\n}\n\n// OnCommit adds a handler function to be executed after the transaction successfully commits.\nfunc (tx *Tx) OnCommit(fn func()) {\n\ttx.commitHandlers = append(tx.commitHandlers, fn)\n}\n\n// Commit writes all changes to disk and updates the meta page.\n// Returns an error if a disk write error occurs, or if Commit is\n// called on a read-only transaction.\nfunc (tx *Tx) Commit() error {\n\t_assert(!tx.managed, \"managed tx commit not allowed\")\n\tif tx.db == nil {\n\t\treturn ErrTxClosed\n\t} else if !tx.writable {\n\t\treturn ErrTxNotWritable\n\t}\n\n\t// TODO(benbjohnson): Use vectorized I/O to write out dirty pages.\n\n\t// Rebalance nodes which have had deletions.\n\tvar startTime = time.Now()\n\ttx.root.rebalance()\n\tif tx.stats.Rebalance > 0 {\n\t\ttx.stats.RebalanceTime += time.Since(startTime)\n\t}\n\n\t// spill data onto dirty pages.\n\tstartTime = time.Now()\n\tif err := tx.root.spill(); err != nil {\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\ttx.stats.SpillTime += time.Since(startTime)\n\n\t// Free the old root bucket.\n\ttx.meta.root.root = tx.root.root\n\n\topgid := tx.meta.pgid\n\n\t// Free the freelist and allocate new pages for it. This will overestimate\n\t// the size of the freelist but not underestimate the size (which would be bad).\n\ttx.db.freelist.free(tx.meta.txid, tx.db.page(tx.meta.freelist))\n\tp, err := tx.allocate((tx.db.freelist.size() / tx.db.pageSize) + 1)\n\tif err != nil {\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\tif err := tx.db.freelist.write(p); err != nil {\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\ttx.meta.freelist = p.id\n\n\t// If the high water mark has moved up then attempt to grow the database.\n\tif tx.meta.pgid > opgid {\n\t\tif err := tx.db.grow(int(tx.meta.pgid+1) * tx.db.pageSize); err != nil {\n\t\t\ttx.rollback()\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Write dirty pages to disk.\n\tstartTime = time.Now()\n\tif err := tx.write(); err != nil {\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\n\t// If strict mode is enabled then perform a consistency check.\n\t// Only the first consistency error is reported in the panic.\n\tif tx.db.StrictMode {\n\t\tch := tx.Check()\n\t\tvar errs []string\n\t\tfor {\n\t\t\terr, ok := <-ch\n\t\t\tif !ok {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\terrs = append(errs, err.Error())\n\t\t}\n\t\tif len(errs) > 0 {\n\t\t\tpanic(\"check fail: \" + strings.Join(errs, \"\\n\"))\n\t\t}\n\t}\n\n\t// Write meta to disk.\n\tif err := tx.writeMeta(); err != nil {\n\t\ttx.rollback()\n\t\treturn err\n\t}\n\ttx.stats.WriteTime += time.Since(startTime)\n\n\t// Finalize the transaction.\n\ttx.close()\n\n\t// Execute commit handlers now that the locks have been removed.\n\tfor _, fn := range tx.commitHandlers {\n\t\tfn()\n\t}\n\n\treturn nil\n}\n\n// Rollback closes the transaction and ignores all previous updates. Read-only\n// transactions must be rolled back and not committed.\nfunc (tx *Tx) Rollback() error {\n\t_assert(!tx.managed, \"managed tx rollback not allowed\")\n\tif tx.db == nil {\n\t\treturn ErrTxClosed\n\t}\n\ttx.rollback()\n\treturn nil\n}\n\nfunc (tx *Tx) rollback() {\n\tif tx.db == nil {\n\t\treturn\n\t}\n\tif tx.writable {\n\t\ttx.db.freelist.rollback(tx.meta.txid)\n\t\ttx.db.freelist.reload(tx.db.page(tx.db.meta().freelist))\n\t}\n\ttx.close()\n}\n\nfunc (tx *Tx) close() {\n\tif tx.db == nil {\n\t\treturn\n\t}\n\tif tx.writable {\n\t\t// Grab freelist stats.\n\t\tvar freelistFreeN = tx.db.freelist.free_count()\n\t\tvar freelistPendingN = tx.db.freelist.pending_count()\n\t\tvar freelistAlloc = tx.db.freelist.size()\n\n\t\t// Remove transaction ref & writer lock.\n\t\ttx.db.rwtx = nil\n\t\ttx.db.rwlock.Unlock()\n\n\t\t// Merge statistics.\n\t\ttx.db.statlock.Lock()\n\t\ttx.db.stats.FreePageN = freelistFreeN\n\t\ttx.db.stats.PendingPageN = freelistPendingN\n\t\ttx.db.stats.FreeAlloc = (freelistFreeN + freelistPendingN) * tx.db.pageSize\n\t\ttx.db.stats.FreelistInuse = freelistAlloc\n\t\ttx.db.stats.TxStats.add(&tx.stats)\n\t\ttx.db.statlock.Unlock()\n\t} else {\n\t\ttx.db.removeTx(tx)\n\t}\n\n\t// Clear all references.\n\ttx.db = nil\n\ttx.meta = nil\n\ttx.root = Bucket{tx: tx}\n\ttx.pages = nil\n}\n\n// Copy writes the entire database to a writer.\n// This function exists for backwards compatibility.\n//\n// Deprecated; Use WriteTo() instead.\nfunc (tx *Tx) Copy(w io.Writer) error {\n\t_, err := tx.WriteTo(w)\n\treturn err\n}\n\n// WriteTo writes the entire database to a writer.\n// If err == nil then exactly tx.Size() bytes will be written into the writer.\nfunc (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {\n\t// Attempt to open reader with WriteFlag\n\tf, err := os.OpenFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\tdefer func() { _ = f.Close() }()\n\n\t// Generate a meta page. We use the same page data for both meta pages.\n\tbuf := make([]byte, tx.db.pageSize)\n\tpage := (*page)(unsafe.Pointer(&buf[0]))\n\tpage.flags = metaPageFlag\n\t*page.meta() = *tx.meta\n\n\t// Write meta 0.\n\tpage.id = 0\n\tpage.meta().checksum = page.meta().sum64()\n\tnn, err := w.Write(buf)\n\tn += int64(nn)\n\tif err != nil {\n\t\treturn n, fmt.Errorf(\"meta 0 copy: %s\", err)\n\t}\n\n\t// Write meta 1 with a lower transaction id.\n\tpage.id = 1\n\tpage.meta().txid -= 1\n\tpage.meta().checksum = page.meta().sum64()\n\tnn, err = w.Write(buf)\n\tn += int64(nn)\n\tif err != nil {\n\t\treturn n, fmt.Errorf(\"meta 1 copy: %s\", err)\n\t}\n\n\t// Move past the meta pages in the file.\n\tif _, err := f.Seek(int64(tx.db.pageSize*2), os.SEEK_SET); err != nil {\n\t\treturn n, fmt.Errorf(\"seek: %s\", err)\n\t}\n\n\t// Copy data pages.\n\twn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2))\n\tn += wn\n\tif err != nil {\n\t\treturn n, err\n\t}\n\n\treturn n, f.Close()\n}\n\n// CopyFile copies the entire database to file at the given path.\n// A reader transaction is maintained during the copy so it is safe to continue\n// using the database while a copy is in progress.\nfunc (tx *Tx) CopyFile(path string, mode os.FileMode) error {\n\tf, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\terr = tx.Copy(f)\n\tif err != nil {\n\t\t_ = f.Close()\n\t\treturn err\n\t}\n\treturn f.Close()\n}\n\n// Check performs several consistency checks on the database for this transaction.\n// An error is returned if any inconsistency is found.\n//\n// It can be safely run concurrently on a writable transaction. However, this\n// incurs a high cost for large databases and databases with a lot of subbuckets\n// because of caching. This overhead can be removed if running on a read-only\n// transaction, however, it is not safe to execute other writer transactions at\n// the same time.\nfunc (tx *Tx) Check() <-chan error {\n\tch := make(chan error)\n\tgo tx.check(ch)\n\treturn ch\n}\n\nfunc (tx *Tx) check(ch chan error) {\n\t// Check if any pages are double freed.\n\tfreed := make(map[pgid]bool)\n\tall := make([]pgid, tx.db.freelist.count())\n\ttx.db.freelist.copyall(all)\n\tfor _, id := range all {\n\t\tif freed[id] {\n\t\t\tch <- fmt.Errorf(\"page %d: already freed\", id)\n\t\t}\n\t\tfreed[id] = true\n\t}\n\n\t// Track every reachable page.\n\treachable := make(map[pgid]*page)\n\treachable[0] = tx.page(0) // meta0\n\treachable[1] = tx.page(1) // meta1\n\tfor i := uint32(0); i <= tx.page(tx.meta.freelist).overflow; i++ {\n\t\treachable[tx.meta.freelist+pgid(i)] = tx.page(tx.meta.freelist)\n\t}\n\n\t// Recursively check buckets.\n\ttx.checkBucket(&tx.root, reachable, freed, ch)\n\n\t// Ensure all pages below high water mark are either reachable or freed.\n\tfor i := pgid(0); i < tx.meta.pgid; i++ {\n\t\t_, isReachable := reachable[i]\n\t\tif !isReachable && !freed[i] {\n\t\t\tch <- fmt.Errorf(\"page %d: unreachable unfreed\", int(i))\n\t\t}\n\t}\n\n\t// Close the channel to signal completion.\n\tclose(ch)\n}\n\nfunc (tx *Tx) checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, ch chan error) {\n\t// Ignore inline buckets.\n\tif b.root == 0 {\n\t\treturn\n\t}\n\n\t// Check every page used by this bucket.\n\tb.tx.forEachPage(b.root, 0, func(p *page, _ int) {\n\t\tif p.id > tx.meta.pgid {\n\t\t\tch <- fmt.Errorf(\"page %d: out of bounds: %d\", int(p.id), int(b.tx.meta.pgid))\n\t\t}\n\n\t\t// Ensure each page is only referenced once.\n\t\tfor i := pgid(0); i <= pgid(p.overflow); i++ {\n\t\t\tvar id = p.id + i\n\t\t\tif _, ok := reachable[id]; ok {\n\t\t\t\tch <- fmt.Errorf(\"page %d: multiple references\", int(id))\n\t\t\t}\n\t\t\treachable[id] = p\n\t\t}\n\n\t\t// We should only encounter un-freed leaf and branch pages.\n\t\tif freed[p.id] {\n\t\t\tch <- fmt.Errorf(\"page %d: reachable freed\", int(p.id))\n\t\t} else if (p.flags&branchPageFlag) == 0 && (p.flags&leafPageFlag) == 0 {\n\t\t\tch <- fmt.Errorf(\"page %d: invalid type: %s\", int(p.id), p.typ())\n\t\t}\n\t})\n\n\t// Check each bucket within this bucket.\n\t_ = b.ForEach(func(k, v []byte) error {\n\t\tif child := b.Bucket(k); child != nil {\n\t\t\ttx.checkBucket(child, reachable, freed, ch)\n\t\t}\n\t\treturn nil\n\t})\n}\n\n// allocate returns a contiguous block of memory starting at a given page.\nfunc (tx *Tx) allocate(count int) (*page, error) {\n\tp, err := tx.db.allocate(count)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\t// Save to our page cache.\n\ttx.pages[p.id] = p\n\n\t// Update statistics.\n\ttx.stats.PageCount++\n\ttx.stats.PageAlloc += count * tx.db.pageSize\n\n\treturn p, nil\n}\n\n// write writes any dirty pages to disk.\nfunc (tx *Tx) write() error {\n\t// Sort pages by id.\n\tpages := make(pages, 0, len(tx.pages))\n\tfor _, p := range tx.pages {\n\t\tpages = append(pages, p)\n\t}\n\t// Clear out page cache early.\n\ttx.pages = make(map[pgid]*page)\n\tsort.Sort(pages)\n\n\t// Write pages to disk in order.\n\tfor _, p := range pages {\n\t\tsize := (int(p.overflow) + 1) * tx.db.pageSize\n\t\toffset := int64(p.id) * int64(tx.db.pageSize)\n\n\t\t// Write out page in \"max allocation\" sized chunks.\n\t\tptr := (*[maxAllocSize]byte)(unsafe.Pointer(p))\n\t\tfor {\n\t\t\t// Limit our write to our max allocation size.\n\t\t\tsz := size\n\t\t\tif sz > maxAllocSize-1 {\n\t\t\t\tsz = maxAllocSize - 1\n\t\t\t}\n\n\t\t\t// Write chunk to disk.\n\t\t\tbuf := ptr[:sz]\n\t\t\tif _, err := tx.db.ops.writeAt(buf, offset); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t// Update statistics.\n\t\t\ttx.stats.Write++\n\n\t\t\t// Exit inner for loop if we've written all the chunks.\n\t\t\tsize -= sz\n\t\t\tif size == 0 {\n\t\t\t\tbreak\n\t\t\t}\n\n\t\t\t// Otherwise move offset forward and move pointer to next chunk.\n\t\t\toffset += int64(sz)\n\t\t\tptr = (*[maxAllocSize]byte)(unsafe.Pointer(&ptr[sz]))\n\t\t}\n\t}\n\n\t// Ignore file sync if flag is set on DB.\n\tif !tx.db.NoSync || IgnoreNoSync {\n\t\tif err := fdatasync(tx.db); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Put small pages back to page pool.\n\tfor _, p := range pages {\n\t\t// Ignore page sizes over 1 page.\n\t\t// These are allocated using make() instead of the page pool.\n\t\tif int(p.overflow) != 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\tbuf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:tx.db.pageSize]\n\n\t\t// See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1\n\t\tfor i := range buf {\n\t\t\tbuf[i] = 0\n\t\t}\n\t\ttx.db.pagePool.Put(buf)\n\t}\n\n\treturn nil\n}\n\n// writeMeta writes the meta to the disk.\nfunc (tx *Tx) writeMeta() error {\n\t// Create a temporary buffer for the meta page.\n\tbuf := make([]byte, tx.db.pageSize)\n\tp := tx.db.pageInBuffer(buf, 0)\n\ttx.meta.write(p)\n\n\t// Write the meta page to file.\n\tif _, err := tx.db.ops.writeAt(buf, int64(p.id)*int64(tx.db.pageSize)); err != nil {\n\t\treturn err\n\t}\n\tif !tx.db.NoSync || IgnoreNoSync {\n\t\tif err := fdatasync(tx.db); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Update statistics.\n\ttx.stats.Write++\n\n\treturn nil\n}\n\n// page returns a reference to the page with a given id.\n// If page has been written to then a temporary buffered page is returned.\nfunc (tx *Tx) page(id pgid) *page {\n\t// Check the dirty pages first.\n\tif tx.pages != nil {\n\t\tif p, ok := tx.pages[id]; ok {\n\t\t\treturn p\n\t\t}\n\t}\n\n\t// Otherwise return directly from the mmap.\n\treturn tx.db.page(id)\n}\n\n// forEachPage iterates over every page within a given page and executes a function.\nfunc (tx *Tx) forEachPage(pgid pgid, depth int, fn func(*page, int)) {\n\tp := tx.page(pgid)\n\n\t// Execute function.\n\tfn(p, depth)\n\n\t// Recursively loop over children.\n\tif (p.flags & branchPageFlag) != 0 {\n\t\tfor i := 0; i < int(p.count); i++ {\n\t\t\telem := p.branchPageElement(uint16(i))\n\t\t\ttx.forEachPage(elem.pgid, depth+1, fn)\n\t\t}\n\t}\n}\n\n// Page returns page information for a given page number.\n// This is only safe for concurrent use when used by a writable transaction.\nfunc (tx *Tx) Page(id int) (*PageInfo, error) {\n\tif tx.db == nil {\n\t\treturn nil, ErrTxClosed\n\t} else if pgid(id) >= tx.meta.pgid {\n\t\treturn nil, nil\n\t}\n\n\t// Build the page info.\n\tp := tx.db.page(pgid(id))\n\tinfo := &PageInfo{\n\t\tID:            id,\n\t\tCount:         int(p.count),\n\t\tOverflowCount: int(p.overflow),\n\t}\n\n\t// Determine the type (or if it's free).\n\tif tx.db.freelist.freed(pgid(id)) {\n\t\tinfo.Type = \"free\"\n\t} else {\n\t\tinfo.Type = p.typ()\n\t}\n\n\treturn info, nil\n}\n\n// TxStats represents statistics about the actions performed by the transaction.\ntype TxStats struct {\n\t// Page statistics.\n\tPageCount int // number of page allocations\n\tPageAlloc int // total bytes allocated\n\n\t// Cursor statistics.\n\tCursorCount int // number of cursors created\n\n\t// Node statistics\n\tNodeCount int // number of node allocations\n\tNodeDeref int // number of node dereferences\n\n\t// Rebalance statistics.\n\tRebalance     int           // number of node rebalances\n\tRebalanceTime time.Duration // total time spent rebalancing\n\n\t// Split/Spill statistics.\n\tSplit     int           // number of nodes split\n\tSpill     int           // number of nodes spilled\n\tSpillTime time.Duration // total time spent spilling\n\n\t// Write statistics.\n\tWrite     int           // number of writes performed\n\tWriteTime time.Duration // total time spent writing to disk\n}\n\nfunc (s *TxStats) add(other *TxStats) {\n\ts.PageCount += other.PageCount\n\ts.PageAlloc += other.PageAlloc\n\ts.CursorCount += other.CursorCount\n\ts.NodeCount += other.NodeCount\n\ts.NodeDeref += other.NodeDeref\n\ts.Rebalance += other.Rebalance\n\ts.RebalanceTime += other.RebalanceTime\n\ts.Split += other.Split\n\ts.Spill += other.Spill\n\ts.SpillTime += other.SpillTime\n\ts.Write += other.Write\n\ts.WriteTime += other.WriteTime\n}\n\n// Sub calculates and returns the difference between two sets of transaction stats.\n// This is useful when obtaining stats at two different points and time and\n// you need the performance counters that occurred within that time span.\nfunc (s *TxStats) Sub(other *TxStats) TxStats {\n\tvar diff TxStats\n\tdiff.PageCount = s.PageCount - other.PageCount\n\tdiff.PageAlloc = s.PageAlloc - other.PageAlloc\n\tdiff.CursorCount = s.CursorCount - other.CursorCount\n\tdiff.NodeCount = s.NodeCount - other.NodeCount\n\tdiff.NodeDeref = s.NodeDeref - other.NodeDeref\n\tdiff.Rebalance = s.Rebalance - other.Rebalance\n\tdiff.RebalanceTime = s.RebalanceTime - other.RebalanceTime\n\tdiff.Split = s.Split - other.Split\n\tdiff.Spill = s.Spill - other.Spill\n\tdiff.SpillTime = s.SpillTime - other.SpillTime\n\tdiff.Write = s.Write - other.Write\n\tdiff.WriteTime = s.WriteTime - other.WriteTime\n\treturn diff\n}\n"
  },
  {
    "path": "tx_test.go",
    "content": "package bolt_test\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\t\"testing\"\n\n\t\"github.com/boltdb/bolt\"\n)\n\n// Ensure that committing a closed transaction returns an error.\nfunc TestTx_Commit_ErrTxClosed(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif _, err := tx.CreateBucket([]byte(\"foo\")); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Commit(); err != bolt.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that rolling back a closed transaction returns an error.\nfunc TestTx_Rollback_ErrTxClosed(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := tx.Rollback(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Rollback(); err != bolt.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that committing a read-only transaction returns an error.\nfunc TestTx_Commit_ErrTxNotWritable(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\ttx, err := db.Begin(false)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Commit(); err != bolt.ErrTxNotWritable {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a transaction can retrieve a cursor on the root bucket.\nfunc TestTx_Cursor(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif _, err := tx.CreateBucket([]byte(\"woojits\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tc := tx.Cursor()\n\t\tif k, v := c.First(); !bytes.Equal(k, []byte(\"widgets\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\tif k, v := c.Next(); !bytes.Equal(k, []byte(\"woojits\")) {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\n\t\tif k, v := c.Next(); k != nil {\n\t\t\tt.Fatalf(\"unexpected key: %v\", k)\n\t\t} else if v != nil {\n\t\t\tt.Fatalf(\"unexpected value: %v\", k)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that creating a bucket with a read-only transaction returns an error.\nfunc TestTx_CreateBucket_ErrTxNotWritable(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"foo\"))\n\t\tif err != bolt.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that creating a bucket on a closed transaction returns an error.\nfunc TestTx_CreateBucket_ErrTxClosed(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif _, err := tx.CreateBucket([]byte(\"foo\")); err != bolt.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that a Tx can retrieve a bucket.\nfunc TestTx_Bucket(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a Tx retrieving a non-existent key returns nil.\nfunc TestTx_Get_NotFound(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif b.Get([]byte(\"no_such_key\")) != nil {\n\t\t\tt.Fatal(\"expected nil value\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can be created and retrieved.\nfunc TestTx_CreateBucket(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Create a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if b == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read the bucket through a separate transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can be created if it doesn't already exist.\nfunc TestTx_CreateBucketIfNotExists(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create bucket.\n\t\tif b, err := tx.CreateBucketIfNotExists([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if b == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\n\t\t// Create bucket again.\n\t\tif b, err := tx.CreateBucketIfNotExists([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t} else if b == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Read the bucket through a separate transaction.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif tx.Bucket([]byte(\"widgets\")) == nil {\n\t\t\tt.Fatal(\"expected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure transaction returns an error if creating an unnamed bucket.\nfunc TestTx_CreateBucketIfNotExists_ErrBucketNameRequired(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucketIfNotExists([]byte{}); err != bolt.ErrBucketNameRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\n\t\tif _, err := tx.CreateBucketIfNotExists(nil); err != bolt.ErrBucketNameRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket cannot be created twice.\nfunc TestTx_CreateBucket_ErrBucketExists(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Create a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Create the same bucket again.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != bolt.ErrBucketExists {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket is created with a non-blank name.\nfunc TestTx_CreateBucket_ErrBucketNameRequired(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif _, err := tx.CreateBucket(nil); err != bolt.ErrBucketNameRequired {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that a bucket can be deleted.\nfunc TestTx_DeleteBucket(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\t// Create a bucket and add a value.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\t// Delete the bucket and make sure we can't get the value.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.DeleteBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif tx.Bucket([]byte(\"widgets\")) != nil {\n\t\t\tt.Fatal(\"unexpected bucket\")\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t// Create the bucket again and make sure there's not a phantom value.\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif v := b.Get([]byte(\"foo\")); v != nil {\n\t\t\tt.Fatalf(\"unexpected phantom value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that deleting a bucket on a closed transaction returns an error.\nfunc TestTx_DeleteBucket_ErrTxClosed(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.Commit(); err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif err := tx.DeleteBucket([]byte(\"foo\")); err != bolt.ErrTxClosed {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t}\n}\n\n// Ensure that deleting a bucket with a read-only transaction returns an error.\nfunc TestTx_DeleteBucket_ReadOnly(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tif err := tx.DeleteBucket([]byte(\"foo\")); err != bolt.ErrTxNotWritable {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that nothing happens when deleting a bucket that doesn't exist.\nfunc TestTx_DeleteBucket_NotFound(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tif err := tx.DeleteBucket([]byte(\"widgets\")); err != bolt.ErrBucketNotFound {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that no error is returned when a tx.ForEach function does not return\n// an error.\nfunc TestTx_ForEach_NoError(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif err := tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\treturn nil\n\t\t}); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that an error is returned when a tx.ForEach function returns an error.\nfunc TestTx_ForEach_WithError(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tmarker := errors.New(\"marker\")\n\t\tif err := tx.ForEach(func(name []byte, b *bolt.Bucket) error {\n\t\t\treturn marker\n\t\t}); err != marker {\n\t\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\n// Ensure that Tx commit handlers are called after a transaction successfully commits.\nfunc TestTx_OnCommit(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar x int\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\ttx.OnCommit(func() { x += 1 })\n\t\ttx.OnCommit(func() { x += 2 })\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t} else if x != 3 {\n\t\tt.Fatalf(\"unexpected x: %d\", x)\n\t}\n}\n\n// Ensure that Tx commit handlers are NOT called after a transaction rolls back.\nfunc TestTx_OnCommit_Rollback(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tvar x int\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\ttx.OnCommit(func() { x += 1 })\n\t\ttx.OnCommit(func() { x += 2 })\n\t\tif _, err := tx.CreateBucket([]byte(\"widgets\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn errors.New(\"rollback this commit\")\n\t}); err == nil || err.Error() != \"rollback this commit\" {\n\t\tt.Fatalf(\"unexpected error: %s\", err)\n\t} else if x != 0 {\n\t\tt.Fatalf(\"unexpected x: %d\", x)\n\t}\n}\n\n// Ensure that the database can be copied to a file path.\nfunc TestTx_CopyFile(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\n\tpath := tempfile()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.CopyFile(path, 0600)\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tdb2, err := bolt.Open(path, 0600, nil)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db2.View(func(tx *bolt.Tx) error {\n\t\tif v := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\")); !bytes.Equal(v, []byte(\"bar\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\tif v := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"baz\")); !bytes.Equal(v, []byte(\"bat\")) {\n\t\t\tt.Fatalf(\"unexpected value: %v\", v)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db2.Close(); err != nil {\n\t\tt.Fatal(err)\n\t}\n}\n\ntype failWriterError struct{}\n\nfunc (failWriterError) Error() string {\n\treturn \"error injected for tests\"\n}\n\ntype failWriter struct {\n\t// fail after this many bytes\n\tAfter int\n}\n\nfunc (f *failWriter) Write(p []byte) (n int, err error) {\n\tn = len(p)\n\tif n > f.After {\n\t\tn = f.After\n\t\terr = failWriterError{}\n\t}\n\tf.After -= n\n\treturn n, err\n}\n\n// Ensure that Copy handles write errors right.\nfunc TestTx_CopyFile_Error_Meta(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.Copy(&failWriter{})\n\t}); err == nil || err.Error() != \"meta 0 copy: error injected for tests\" {\n\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t}\n}\n\n// Ensure that Copy handles write errors right.\nfunc TestTx_CopyFile_Error_Normal(t *testing.T) {\n\tdb := MustOpenDB()\n\tdefer db.MustClose()\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif err := b.Put([]byte(\"baz\"), []byte(\"bat\")); err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.Copy(&failWriter{3 * db.Info().PageSize})\n\t}); err == nil || err.Error() != \"error injected for tests\" {\n\t\tt.Fatalf(\"unexpected error: %v\", err)\n\t}\n}\n\nfunc ExampleTx_Rollback() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Create a bucket.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\t_, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\treturn err\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Set a value for a key.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\treturn tx.Bucket([]byte(\"widgets\")).Put([]byte(\"foo\"), []byte(\"bar\"))\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Update the key but rollback the transaction so it never saves.\n\ttx, err := db.Begin(true)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tb := tx.Bucket([]byte(\"widgets\"))\n\tif err := b.Put([]byte(\"foo\"), []byte(\"baz\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tif err := tx.Rollback(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Ensure that our original value is still set.\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value for 'foo' is still: %s\\n\", value)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value for 'foo' is still: bar\n}\n\nfunc ExampleTx_CopyFile() {\n\t// Open the database.\n\tdb, err := bolt.Open(tempfile(), 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(db.Path())\n\n\t// Create a bucket and a key.\n\tif err := db.Update(func(tx *bolt.Tx) error {\n\t\tb, err := tx.CreateBucket([]byte(\"widgets\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif err := b.Put([]byte(\"foo\"), []byte(\"bar\")); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Copy the database to another file.\n\ttoFile := tempfile()\n\tif err := db.View(func(tx *bolt.Tx) error {\n\t\treturn tx.CopyFile(toFile, 0666)\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer os.Remove(toFile)\n\n\t// Open the cloned database.\n\tdb2, err := bolt.Open(toFile, 0666, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Ensure that the key exists in the copy.\n\tif err := db2.View(func(tx *bolt.Tx) error {\n\t\tvalue := tx.Bucket([]byte(\"widgets\")).Get([]byte(\"foo\"))\n\t\tfmt.Printf(\"The value for 'foo' in the clone is: %s\\n\", value)\n\t\treturn nil\n\t}); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Close database to release file lock.\n\tif err := db.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tif err := db2.Close(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t// Output:\n\t// The value for 'foo' in the clone is: bar\n}\n"
  }
]