bbolt

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 17 Imported by: 0

README

bbolt

Go Report Card Go Reference Releases LICENSE

bbolt is a fork of Ben Johnson's Bolt key/value store. The purpose of this fork is to provide the Go community with an active maintenance and development target for Bolt; the goal is improved reliability and stability. bbolt includes bug fixes, performance enhancements, and features not found in Bolt while preserving backwards compatibility with the Bolt API.

Bolt is a pure Go key/value store inspired by Howard Chu's LMDB project. The goal of the project is to provide a simple, fast, and reliable database for projects that don't require a full database server such as Postgres or MySQL.

Since Bolt is meant to be used as such a low-level piece of functionality, simplicity is key. The API will be small and only focus on getting values and setting values. That's it.

Project Status

Bolt is stable, the API is fixed, and the file format is fixed. Full unit test coverage and randomized black box testing are used to ensure database consistency and thread safety. Bolt is currently used in high-load production environments serving databases as large as 1TB. Many companies such as Shopify and Heroku use Bolt-backed services every day.

Project versioning

bbolt uses semantic versioning. API should not change between patch and minor releases. New minor versions may add additional features to the API.

Table of Contents

Getting Started

Installing

To start using Bolt, install Go and run go get:

$ go get go.etcd.io/bbolt@latest

This will retrieve the library and update your go.mod and go.sum files.

To run the command line utility, execute:

$ go run go.etcd.io/bbolt/cmd/bbolt@latest

Run go install to install the bbolt command line utility into your $GOBIN path, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH environment variable is not set.

$ go install go.etcd.io/bbolt/cmd/bbolt@latest
Importing bbolt

To use bbolt as an embedded key-value store, import as:

import bolt "go.etcd.io/bbolt"

db, err := bolt.Open(path, 0600, nil)
if err != nil {
  return err
}
defer db.Close()
Opening a database

The top-level object in Bolt is a DB. It is represented as a single file on your disk and represents a consistent snapshot of your data.

To open your database, simply use the bolt.Open() function:

package main

import (
	"log"

	bolt "go.etcd.io/bbolt"
)

func main() {
	// Open the my.db data file in your current directory.
	// It will be created if it doesn't exist.
	db, err := bolt.Open("my.db", 0600, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	...
}

Please note that Bolt obtains a file lock on the data file so multiple processes cannot open the same database at the same time. Opening an already open Bolt database will cause it to hang until the other process closes it. To prevent an indefinite wait you can pass a timeout option to the Open() function:

db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
Transactions

Bolt allows only one read-write transaction at a time but allows as many read-only transactions as you want at a time. Each transaction has a consistent view of the data as it existed when the transaction started.

Individual transactions and all objects created from them (e.g. buckets, keys) are not thread safe. To work with data in multiple goroutines you must start a transaction for each one or use locking to ensure only one goroutine accesses a transaction at a time. Creating transaction from the DB is thread safe.

Transactions should not depend on one another and generally shouldn't be opened simultaneously in the same goroutine. This can cause a deadlock as the read-write transaction needs to periodically re-map the data file but it cannot do so while any read-only transaction is open. Even a nested read-only transaction can cause a deadlock, as the child transaction can block the parent transaction from releasing its resources.

Read-write transactions

To start a read-write transaction, you can use the DB.Update() function:

err := db.Update(func(tx *bolt.Tx) error {
	...
	return nil
})

Inside the closure, you have a consistent view of the database. You commit the transaction by returning nil at the end. You can also rollback the transaction at any point by returning an error. All database operations are allowed inside a read-write transaction.

Always check the return error as it will report any disk failures that can cause your transaction to not complete. If you return an error within your closure it will be passed through.

Read-only transactions

To start a read-only transaction, you can use the DB.View() function:

err := db.View(func(tx *bolt.Tx) error {
	...
	return nil
})

You also get a consistent view of the database within this closure, however, no mutating operations are allowed within a read-only transaction. You can only retrieve buckets, retrieve values, and copy the database within a read-only transaction.

Batch read-write transactions

Each DB.Update() waits for disk to commit the writes. This overhead can be minimized by combining multiple updates with the DB.Batch() function:

err := db.Batch(func(tx *bolt.Tx) error {
	...
	return nil
})

Concurrent Batch calls are opportunistically combined into larger transactions. Batch is only useful when there are multiple goroutines calling it.

The trade-off is that Batch can call the given function multiple times, if parts of the transaction fail. The function must be idempotent and side effects must take effect only after a successful return from DB.Batch().

For example: don't display messages from inside the function, instead set variables in the enclosing scope:

var id uint64
err := db.Batch(func(tx *bolt.Tx) error {
	// Find last key in bucket, decode as bigendian uint64, increment
	// by one, encode back to []byte, and add new key.
	...
	id = newValue
	return nil
})
if err != nil {
	return ...
}
fmt.Println("Allocated ID %d", id)
Managing transactions manually

The DB.View() and DB.Update() functions are wrappers around the DB.Begin() function. These helper functions will start the transaction, execute a function, and then safely close your transaction if an error is returned. This is the recommended way to use Bolt transactions.

However, sometimes you may want to manually start and end your transactions. You can use the DB.Begin() function directly but please be sure to close the transaction.

// Start a writable transaction.
tx, err := db.Begin(true)
if err != nil {
    return err
}
defer tx.Rollback()

// Use the transaction...
_, err := tx.CreateBucket([]byte("MyBucket"))
if err != nil {
    return err
}

// Commit the transaction and check for error.
if err := tx.Commit(); err != nil {
    return err
}

The first argument to DB.Begin() is a boolean stating if the transaction should be writable.

Using buckets

Buckets are collections of key/value pairs within the database. All keys in a bucket must be unique. You can create a bucket using the Tx.CreateBucket() function:

db.Update(func(tx *bolt.Tx) error {
	b, err := tx.CreateBucket([]byte("MyBucket"))
	if err != nil {
		return fmt.Errorf("create bucket: %s", err)
	}
	return nil
})

You can retrieve an existing bucket using the Tx.Bucket() function:

db.Update(func(tx *bolt.Tx) error {
	b := tx.Bucket([]byte("MyBucket"))
	if b == nil {
		return fmt.Errorf("bucket does not exist")
	}
	return nil
})

You can also create a bucket only if it doesn't exist by using the Tx.CreateBucketIfNotExists() function. It's a common pattern to call this function for all your top-level buckets after you open your database so you can guarantee that they exist for future transactions.

To delete a bucket, simply call the Tx.DeleteBucket() function.

Using key/value pairs

To save a key/value pair to a bucket, use the Bucket.Put() function:

db.Update(func(tx *bolt.Tx) error {
	b := tx.Bucket([]byte("MyBucket"))
	err := b.Put([]byte("answer"), []byte("42"))
	return err
})

This will set the value of the "answer" key to "42" in the MyBucket bucket. To retrieve this value, we can use the Bucket.Get() function:

db.View(func(tx *bolt.Tx) error {
	b := tx.Bucket([]byte("MyBucket"))
	v := b.Get([]byte("answer"))
	fmt.Printf("The answer is: %s\n", v)
	return nil
})

The Get() function does not return an error because its operation is guaranteed to work (unless there is some kind of system failure). If the key exists then it will return its byte slice value. If it doesn't exist then it will return nil. It's important to note that you can have a zero-length value set to a key which is different than the key not existing.

Use the Bucket.Delete() function to delete a key from the bucket:

db.Update(func (tx *bolt.Tx) error {
    b := tx.Bucket([]byte("MyBucket"))
    err := b.Delete([]byte("answer"))
    return err
})

This will delete the key answers from the bucket MyBucket.

Please note that values returned from Get() are only valid while the transaction is open. If you need to use a value outside of the transaction then you must use copy() to copy it to another byte slice.

Autoincrementing integer for the bucket

By using the NextSequence() function, you can let Bolt determine a sequence which can be used as the unique identifier for your key/value pairs. See the example below.

// CreateUser saves u to the store. The new user ID is set on u once the data is persisted.
func (s *Store) CreateUser(u *User) error {
    return s.db.Update(func(tx *bolt.Tx) error {
        // Retrieve the users bucket.
        // This should be created when the DB is first opened.
        b := tx.Bucket([]byte("users"))

        // Generate ID for the user.
        // This returns an error only if the Tx is closed or not writeable.
        // That can't happen in an Update() call so I ignore the error check.
        id, _ := b.NextSequence()
        u.ID = int(id)

        // Marshal user data into bytes.
        buf, err := json.Marshal(u)
        if err != nil {
            return err
        }

        // Persist bytes to users bucket.
        return b.Put(itob(u.ID), buf)
    })
}

// itob returns an 8-byte big endian representation of v.
func itob(v int) []byte {
    b := make([]byte, 8)
    binary.BigEndian.PutUint64(b, uint64(v))
    return b
}

type User struct {
    ID int
    ...
}
Iterating over keys

Bolt stores its keys in byte-sorted order within a bucket. This makes sequential iteration over these keys extremely fast. To iterate over keys we'll use a Cursor:

db.View(func(tx *bolt.Tx) error {
	// Assume bucket exists and has keys
	b := tx.Bucket([]byte("MyBucket"))

	c := b.Cursor()

	for k, v := c.First(); k != nil; k, v = c.Next() {
		fmt.Printf("key=%s, value=%s\n", k, v)
	}

	return nil
})

The cursor allows you to move to a specific point in the list of keys and move forward or backward through the keys one at a time.

The following functions are available on the cursor:

First()  Move to the first key.
Last()   Move to the last key.
Seek()   Move to a specific key.
Next()   Move to the next key.
Prev()   Move to the previous key.

Each of those functions has a return signature of (key []byte, value []byte). When you have iterated to the end of the cursor then Next() will return a nil key. You must seek to a position using First(), Last(), or Seek() before calling Next() or Prev(). If you do not seek to a position then these functions will return a nil key.

During iteration, if the key is non-nil but the value is nil, that means the key refers to a bucket rather than a value. Use Bucket.Bucket() to access the sub-bucket.

Prefix scans

To iterate over a key prefix, you can combine Seek() and bytes.HasPrefix():

db.View(func(tx *bolt.Tx) error {
	// Assume bucket exists and has keys
	c := tx.Bucket([]byte("MyBucket")).Cursor()

	prefix := []byte("1234")
	for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
		fmt.Printf("key=%s, value=%s\n", k, v)
	}

	return nil
})
Range scans

Another common use case is scanning over a range such as a time range. If you use a sortable time encoding such as RFC3339 then you can query a specific date range like this:

db.View(func(tx *bolt.Tx) error {
	// Assume our events bucket exists and has RFC3339 encoded time keys.
	c := tx.Bucket([]byte("Events")).Cursor()

	// Our time range spans the 90's decade.
	min := []byte("1990-01-01T00:00:00Z")
	max := []byte("2000-01-01T00:00:00Z")

	// Iterate over the 90's.
	for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
		fmt.Printf("%s: %s\n", k, v)
	}

	return nil
})

Note that, while RFC3339 is sortable, the Golang implementation of RFC3339Nano does not use a fixed number of digits after the decimal point and is therefore not sortable.

ForEach()

You can also use the function ForEach() if you know you'll be iterating over all the keys in a bucket:

db.View(func(tx *bolt.Tx) error {
	// Assume bucket exists and has keys
	b := tx.Bucket([]byte("MyBucket"))

	b.ForEach(func(k, v []byte) error {
		fmt.Printf("key=%s, value=%s\n", k, v)
		return nil
	})
	return nil
})

Please note that keys and values in ForEach() are only valid while the transaction is open. If you need to use a key or value outside of the transaction, you must use copy() to copy it to another byte slice.

Nested buckets

You can also store a bucket in a key to create nested buckets. The API is the same as the bucket management API on the DB object:

func (*Bucket) CreateBucket(key []byte) (*Bucket, error)
func (*Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error)
func (*Bucket) DeleteBucket(key []byte) error

Say you had a multi-tenant application where the root level bucket was the account bucket. Inside of this bucket was a sequence of accounts which themselves are buckets. And inside the sequence bucket you could have many buckets pertaining to the Account itself (Users, Notes, etc) isolating the information into logical groupings.


// createUser creates a new user in the given account.
func createUser(accountID int, u *User) error {
    // Start the transaction.
    tx, err := db.Begin(true)
    if err != nil {
        return err
    }
    defer tx.Rollback()

    // Retrieve the root bucket for the account.
    // Assume this has already been created when the account was set up.
    root := tx.Bucket([]byte(strconv.FormatUint(accountID, 10)))

    // Setup the users bucket.
    bkt, err := root.CreateBucketIfNotExists([]byte("USERS"))
    if err != nil {
        return err
    }

    // Generate an ID for the new user.
    userID, err := bkt.NextSequence()
    if err != nil {
        return err
    }
    u.ID = userID

    // Marshal and save the encoded user.
    if buf, err := json.Marshal(u); err != nil {
        return err
    } else if err := bkt.Put([]byte(strconv.FormatUint(u.ID, 10)), buf); err != nil {
        return err
    }

    // Commit the transaction.
    if err := tx.Commit(); err != nil {
        return err
    }

    return nil
}

Database backups

Bolt is a single file so it's easy to backup. You can use the Tx.WriteTo() function to write a consistent view of the database to a writer. If you call this from a read-only transaction, it will perform a hot backup and not block your other database reads and writes.

By default, it will use a regular file handle which will utilize the operating system's page cache. See the Tx documentation for information about optimizing for larger-than-RAM datasets.

One common use case is to backup over HTTP so you can use tools like cURL to do database backups:

func BackupHandleFunc(w http.ResponseWriter, req *http.Request) {
	err := db.View(func(tx *bolt.Tx) error {
		w.Header().Set("Content-Type", "application/octet-stream")
		w.Header().Set("Content-Disposition", `attachment; filename="my.db"`)
		w.Header().Set("Content-Length", strconv.Itoa(int(tx.Size())))
		_, err := tx.WriteTo(w)
		return err
	})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

Then you can backup using this command:

$ curl http://localhost/backup > my.db

Or you can open your browser to http://localhost/backup and it will download automatically.

If you want to backup to another file you can use the Tx.CopyFile() helper function.

Statistics

The database keeps a running count of many of the internal operations it performs so you can better understand what's going on. By grabbing a snapshot of these stats at two points in time we can see what operations were performed in that time range.

For example, we could start a goroutine to log stats every 10 seconds:

go func() {
	// Grab the initial stats.
	prev := db.Stats()

	for {
		// Wait for 10s.
		time.Sleep(10 * time.Second)

		// Grab the current stats and diff them.
		stats := db.Stats()
		diff := stats.Sub(&prev)

		// Encode stats to JSON and print to STDERR.
		json.NewEncoder(os.Stderr).Encode(diff)

		// Save stats for the next loop.
		prev = stats
	}
}()

It's also useful to pipe these stats to a service such as statsd for monitoring or to provide an HTTP endpoint that will perform a fixed-length sample.

Read-Only Mode

Sometimes it is useful to create a shared, read-only Bolt database. To this, set the Options.ReadOnly flag when opening your database. Read-only mode uses a shared lock to allow multiple processes to read from the database but it will block any processes from opening the database in read-write mode.

db, err := bolt.Open("my.db", 0600, &bolt.Options{ReadOnly: true})
if err != nil {
	log.Fatal(err)
}
Mobile Use (iOS/Android)

Bolt is able to run on mobile devices by leveraging the binding feature of the gomobile tool. Create a struct that will contain your database logic and a reference to a *bolt.DB with a initializing constructor that takes in a filepath where the database file will be stored. Neither Android nor iOS require extra permissions or cleanup from using this method.

func NewBoltDB(filepath string) *BoltDB {
	db, err := bolt.Open(filepath+"/demo.db", 0600, nil)
	if err != nil {
		log.Fatal(err)
	}

	return &BoltDB{db}
}

type BoltDB struct {
	db *bolt.DB
	...
}

func (b *BoltDB) Path() string {
	return b.db.Path()
}

func (b *BoltDB) Close() {
	b.db.Close()
}

Database logic should be defined as methods on this wrapper struct.

To initialize this struct from the native language (both platforms now sync their local storage to the cloud. These snippets disable that functionality for the database file):

Android
String path;
if (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.LOLLIPOP){
    path = getNoBackupFilesDir().getAbsolutePath();
} else{
    path = getFilesDir().getAbsolutePath();
}
Boltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path)
iOS
- (void)demo {
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
                                                          NSUserDomainMask,
                                                          YES) objectAtIndex:0];
	GoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path);
	[self addSkipBackupAttributeToItemAtPath:demo.path];
	//Some DB Logic would go here
	[demo close];
}

- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
{
    NSURL* URL= [NSURL fileURLWithPath: filePathString];
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

Resources

For more information on getting started with Bolt, check out the following articles:

Comparison with other databases

Postgres, MySQL, & other relational databases

Relational databases structure data into rows and are only accessible through the use of SQL. This approach provides flexibility in how you store and query your data but also incurs overhead in parsing and planning SQL statements. Bolt accesses all data by a byte slice key. This makes Bolt fast to read and write data by key but provides no built-in support for joining values together.

Most relational databases (with the exception of SQLite) are standalone servers that run separately from your application. This gives your systems flexibility to connect multiple application servers to a single database server but also adds overhead in serializing and transporting data over the network. Bolt runs as a library included in your application so all data access has to go through your application's process. This brings data closer to your application but limits multi-process access to the data.

LevelDB, RocksDB

LevelDB and its derivatives (RocksDB, HyperLevelDB) are similar to Bolt in that they are libraries bundled into the application, however, their underlying structure is a log-structured merge-tree (LSM tree). An LSM tree optimizes random writes by using a write ahead log and multi-tiered, sorted files called SSTables. Bolt uses a B+tree internally and only a single file. Both approaches have trade-offs.

If you require a high random write throughput (>10,000 w/sec) or you need to use spinning disks then LevelDB could be a good choice. If your application is read-heavy or does a lot of range scans then Bolt could be a good choice.

One other important consideration is that LevelDB does not have transactions. It supports batch writing of key/values pairs and it supports read snapshots but it will not give you the ability to do a compare-and-swap operation safely. Bolt supports fully serializable ACID transactions.

LMDB

Bolt was originally a port of LMDB so it is architecturally similar. Both use a B+tree, have ACID semantics with fully serializable transactions, and support lock-free MVCC using a single writer and multiple readers.

The two projects have somewhat diverged. LMDB heavily focuses on raw performance while Bolt has focused on simplicity and ease of use. For example, LMDB allows several unsafe actions such as direct writes for the sake of performance. Bolt opts to disallow actions which can leave the database in a corrupted state. The only exception to this in Bolt is DB.NoSync.

There are also a few differences in API. LMDB requires a maximum mmap size when opening an mdb_env whereas Bolt will handle incremental mmap resizing automatically. LMDB overloads the getter and setter functions with multiple flags whereas Bolt splits these specialized cases into their own functions.

Caveats & Limitations

It's important to pick the right tool for the job and Bolt is no exception. Here are a few things to note when evaluating and using Bolt:

  • Bolt is good for read intensive workloads. Sequential write performance is also fast but random writes can be slow. You can use DB.Batch() or add a write-ahead log to help mitigate this issue.

  • Bolt uses a B+tree internally so there can be a lot of random page access. SSDs provide a significant performance boost over spinning disks.

  • Try to avoid long running read transactions. Bolt uses copy-on-write so old pages cannot be reclaimed while an old transaction is using them.

  • Byte slices returned from Bolt are only valid during a transaction. Once the transaction has been committed or rolled back then the memory they point to can be reused by a new page or can be unmapped from virtual memory and you'll see an unexpected fault address panic when accessing it.

  • Bolt uses an exclusive write lock on the database file so it cannot be shared by multiple processes.

  • Be careful when using Bucket.FillPercent. Setting a high fill percent for buckets that have random inserts will cause your database to have very poor page utilization.

  • Use larger buckets in general. Smaller buckets causes poor page utilization once they become larger than the page size (typically 4KB).

  • Bulk loading a lot of random writes into a new bucket can be slow as the page will not split until the transaction is committed. Randomly inserting more than 100,000 key/value pairs into a single new bucket in a single transaction is not advised.

  • Bolt uses a memory-mapped file so the underlying operating system handles the caching of the data. Typically, the OS will cache as much of the file as it can in memory and will release memory as needed to other processes. This means that Bolt can show very high memory usage when working with large databases. However, this is expected and the OS will release memory as needed. Bolt can handle databases much larger than the available physical RAM, provided its memory-map fits in the process virtual address space. It may be problematic on 32-bits systems.

  • The data structures in the Bolt database are memory mapped so the data file will be endian specific. This means that you cannot copy a Bolt file from a little endian machine to a big endian machine and have it work. For most users this is not a concern since most modern CPUs are little endian.

  • Because of the way pages are laid out on disk, Bolt cannot truncate data files and return free pages back to the disk. Instead, Bolt maintains a free list of unused pages within its data file. These free pages can be reused by later transactions. This works well for many use cases as databases generally tend to grow. However, it's important to note that deleting large chunks of data will not allow you to reclaim that space on disk.

    For more information on page allocation, see this comment.

Reading the Source

Bolt is a relatively small code base (<5KLOC) for an embedded, serializable, transactional key/value database so it can be a good starting point for people interested in how databases work.

The best places to start are the main entry points into Bolt:

  • Open() - Initializes the reference to the database. It's responsible for creating the database if it doesn't exist, obtaining an exclusive lock on the file, reading the meta pages, & memory-mapping the file.

  • DB.Begin() - Starts a read-only or read-write transaction depending on the value of the writable argument. This requires briefly obtaining the "meta" lock to keep track of open transactions. Only one read-write transaction can exist at a time so the "rwlock" is acquired during the life of a read-write transaction.

  • Bucket.Put() - Writes a key/value pair into a bucket. After validating the arguments, a cursor is used to traverse the B+tree to the page and position where they key & value will be written. Once the position is found, the bucket materializes the underlying page and the page's parent pages into memory as "nodes". These nodes are where mutations occur during read-write transactions. These changes get flushed to disk during commit.

  • Bucket.Get() - Retrieves a key/value pair from a bucket. This uses a cursor to move to the page & position of a key/value pair. During a read-only transaction, the key and value data is returned as a direct reference to the underlying mmap file so there's no allocation overhead. For read-write transactions, this data may reference the mmap file or one of the in-memory node values.

  • Cursor - This object is simply for traversing the B+tree of on-disk pages or in-memory nodes. It can seek to a specific key, move to the first or last value, or it can move forward or backward. The cursor handles the movement up and down the B+tree transparently to the end user.

  • Tx.Commit() - Converts the in-memory dirty nodes and the list of free pages into pages to be written to disk. Writing to disk then occurs in two phases. First, the dirty pages are written to disk and an fsync() occurs. Second, a new meta page with an incremented transaction ID is written and another fsync() occurs. This two phase write ensures that partially written data pages are ignored in the event of a crash since the meta page pointing to them is never written. Partially written meta pages are invalidated because they are written with a checksum.

If you have additional notes that could be helpful for others, please submit them via pull request.

Other Projects Using Bolt

Below is a list of public, open source projects that use Bolt:

  • Algernon - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend.
  • Bazil - A file system that lets your data reside where it is most convenient for it to reside.
  • bolter - Command-line app for viewing BoltDB file in your terminal.
  • boltcli - the redis-cli for boltdb with Lua script support.
  • BoltHold - An embeddable NoSQL store for Go types built on BoltDB
  • BoltStore - Session store using Bolt.
  • Boltdb Boilerplate - Boilerplate wrapper around bolt aiming to make simple calls one-liners.
  • BoltDbWeb - A web based GUI for BoltDB files.
  • BoltDB Viewer - A BoltDB Viewer Can run on Windows、Linux、Android system.
  • bleve - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.
  • bstore - Database library storing Go values, with referential/unique/nonzero constraints, indices, automatic schema management with struct tags, and a query API.
  • btcwallet - A bitcoin wallet.
  • buckets - a bolt wrapper streamlining simple tx and key scans.
  • cayley - Cayley is an open-source graph database using Bolt as optional backend.
  • ChainStore - Simple key-value interface to a variety of storage engines organized as a chain of operations.
  • 🌰 Chestnut - Chestnut is encrypted storage for Go.
  • Consul - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.
  • DVID - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.
  • dcrwallet - A wallet for the Decred cryptocurrency.
  • drive - drive is an unofficial Google Drive command line client for *NIX operating systems.
  • event-shuttle - A Unix system service to collect and reliably deliver messages to Kafka.
  • Freehold - An open, secure, and lightweight platform for your files and data.
  • Go Report Card - Go code quality report cards as a (free and open source) service.
  • GoWebApp - A basic MVC web application in Go using BoltDB.
  • GoShort - GoShort is a URL shortener written in Golang and BoltDB for persistent key/value storage and for routing it's using high performent HTTPRouter.
  • gopherpit - A web service to manage Go remote import paths with custom domains
  • gokv - Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more)
  • Gitchain - Decentralized, peer-to-peer Git repositories aka "Git meets Bitcoin".
  • InfluxDB - Scalable datastore for metrics, events, and real-time analytics.
  • ipLocator - A fast ip-geo-location-server using bolt with bloom filters.
  • ipxed - Web interface and api for ipxed.
  • Ironsmith - A simple, script-driven continuous integration (build - > test -> release) tool, with no external dependencies
  • Kala - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs.
  • Key Value Access Language (KVAL) - A proposed grammar for key-value datastores offering a bbolt binding.
  • LedisDB - A high performance NoSQL, using Bolt as optional storage.
  • lru - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores.
  • mbuckets - A Bolt wrapper that allows easy operations on multi level (nested) buckets.
  • MetricBase - Single-binary version of Graphite.
  • MuLiFS - Music Library Filesystem creates a filesystem to organise your music files.
  • NATS - NATS Streaming uses bbolt for message and metadata storage.
  • Prometheus Annotation Server - Annotation server for PromDash & Prometheus service monitoring system.
  • Rain - BitTorrent client and library.
  • reef-pi - reef-pi is an award winning, modular, DIY reef tank controller using easy to learn electronics based on a Raspberry Pi.
  • Request Baskets - A web service to collect arbitrary HTTP requests and inspect them via REST API or simple web UI, similar to RequestBin service
  • Seaweed File System - Highly scalable distributed key~file system with O(1) disk read.
  • stow - a persistence manager for objects backed by boltdb.
  • Storm - Simple and powerful ORM for BoltDB.
  • SimpleBolt - A simple way to use BoltDB. Deals mainly with strings.
  • Skybox Analytics - A standalone funnel analysis tool for web analytics.
  • Scuttlebutt - Uses Bolt to store and process all Twitter mentions of GitHub projects.
  • tentacool - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.
  • torrent - Full-featured BitTorrent client package and utilities in Go. BoltDB is a storage backend in development.
  • Wiki - A tiny wiki using Goji, BoltDB and Blackfriday.

If you are using Bolt in a project please send a pull request to add it to the list.

Documentation

Overview

package bbolt implements a low-level key/value store in pure Go. It supports fully serializable transactions, ACID semantics, and lock-free MVCC with multiple readers and a single writer. Bolt can be used for projects that want a simple data store without the need to add large dependencies such as Postgres or MySQL.

Bolt is a single-level, zero-copy, B+tree data store. This means that Bolt is optimized for fast read access and does not require recovery in the event of a system crash. Transactions which have not finished committing will simply be rolled back in the event of a crash.

The design of Bolt is based on Howard Chu's LMDB database project.

Bolt currently works on Windows, Mac OS X, and Linux.

Basics

There are only a few types in Bolt: DB, Bucket, Tx, and Cursor. The DB is a collection of buckets and is represented by a single file on disk. A bucket is a collection of unique keys that are associated with values.

Transactions provide either read-only or read-write access to the database. Read-only transactions can retrieve key/value pairs and can use Cursors to iterate over the dataset sequentially. Read-write transactions can create and delete buckets and can insert and remove keys. Only one read-write transaction is allowed at a time.

Caveats

The database uses a read-only, memory-mapped data file to ensure that applications cannot corrupt the database, however, this means that keys and values returned from Bolt cannot be changed. Writing to a read-only byte slice will cause Go to panic.

Keys and values retrieved from the database are only valid for the life of the transaction. When used outside the transaction, these byte slices can point to different data or can point to invalid memory which will cause a panic.

Index

Examples

Constants

View Source
const (
	// MaxKeySize is the maximum length of a key, in bytes.
	MaxKeySize = 32768

	// MaxValueSize is the maximum length of a value, in bytes.
	MaxValueSize = (1 << 31) - 2
)
View Source
const (
	// FreelistArrayType indicates backend freelist type is array
	FreelistArrayType = FreelistType("array")
	// FreelistMapType indicates backend freelist type is hashmap
	FreelistMapType = FreelistType("hashmap")
)

TODO(ahrtr): eventually we should (step by step)

  1. default to `FreelistMapType`;
  2. remove the `FreelistArrayType`, do not export `FreelistMapType` and remove field `FreelistType' from both `DB` and `Options`;
View Source
const DefaultFillPercent = 0.5

DefaultFillPercent is the percentage that split pages are filled. This value can be changed by setting Bucket.FillPercent.

Variables

View Source
var (
	// ErrDatabaseNotOpen is returned when a DB instance is accessed before it
	// is opened or after it is closed.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrDatabaseNotOpen = errors.ErrDatabaseNotOpen

	// ErrInvalid is returned when both meta pages on a database are invalid.
	// This typically occurs when a file is not a bolt database.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrInvalid = errors.ErrInvalid

	// ErrInvalidMapping is returned when the database file fails to get mapped.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrInvalidMapping = errors.ErrInvalidMapping

	// ErrVersionMismatch is returned when the data file was created with a
	// different version of Bolt.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrVersionMismatch = errors.ErrVersionMismatch

	// ErrChecksum is returned when either meta page checksum does not match.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrChecksum = errors.ErrChecksum

	// ErrTimeout is returned when a database cannot obtain an exclusive lock
	// on the data file after the timeout passed to Open().
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrTimeout = errors.ErrTimeout
)

These errors can be returned when opening or calling methods on a DB.

View Source
var (
	// ErrTxNotWritable is returned when performing a write operation on a
	// read-only transaction.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrTxNotWritable = errors.ErrTxNotWritable

	// ErrTxClosed is returned when committing or rolling back a transaction
	// that has already been committed or rolled back.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrTxClosed = errors.ErrTxClosed

	// ErrDatabaseReadOnly is returned when a mutating transaction is started on a
	// read-only database.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrDatabaseReadOnly = errors.ErrDatabaseReadOnly

	// ErrFreePagesNotLoaded is returned when a readonly transaction without
	// preloading the free pages is trying to access the free pages.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrFreePagesNotLoaded = errors.ErrFreePagesNotLoaded
)

These errors can occur when beginning or committing a Tx.

View Source
var (
	// ErrBucketNotFound is returned when trying to access a bucket that has
	// not been created yet.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrBucketNotFound = errors.ErrBucketNotFound

	// ErrBucketExists is returned when creating a bucket that already exists.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrBucketExists = errors.ErrBucketExists

	// ErrBucketNameRequired is returned when creating a bucket with a blank name.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrBucketNameRequired = errors.ErrBucketNameRequired

	// ErrKeyRequired is returned when inserting a zero-length key.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrKeyRequired = errors.ErrKeyRequired

	// ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrKeyTooLarge = errors.ErrKeyTooLarge

	// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrValueTooLarge = errors.ErrValueTooLarge

	// ErrIncompatibleValue is returned when trying create or delete a bucket
	// on an existing non-bucket key or when trying to create or delete a
	// non-bucket key on an existing bucket key.
	//
	// Deprecated: Use the error variables defined in the bbolt/errors package.
	ErrIncompatibleValue = errors.ErrIncompatibleValue
)

These errors can occur when putting or deleting a value or a bucket.

View Source
var DefaultOptions = &Options{
	Timeout:      0,
	NoGrowSync:   false,
	FreelistType: FreelistArrayType,
}

DefaultOptions represent the options used if nil options are passed into Open(). No timeout is used which will cause Bolt to wait indefinitely for a lock.

Functions

func Compact

func Compact(dst, src *DB, txMaxSize int64) error

Compact will create a copy of the source DB and in the destination DB. This may reclaim space that the source database no longer has use for. txMaxSize can be used to limit the transactions size of this process and may trigger intermittent commits. A value of zero will ignore transaction sizes. TODO: merge with: https://github.com/etcd-io/etcd/blob/b7f0f52a16dbf83f18ca1d803f7892d750366a94/mvcc/backend/backend.go#L349

Types

type Bucket

type Bucket struct {
	*common.InBucket

	// Sets the threshold for filling nodes when they split. By default,
	// the bucket will fill to 50% but it can be useful to increase this
	// amount if you know that your write workloads are mostly append-only.
	//
	// This is non-persisted across transactions so it must be set in every Tx.
	FillPercent float64
	// contains filtered or unexported fields
}

Bucket represents a collection of key/value pairs inside the database.

func (*Bucket) Bucket

func (b *Bucket) Bucket(name []byte) *Bucket

Bucket retrieves a nested bucket by name. Returns nil if the bucket does not exist. The bucket instance is only valid for the lifetime of the transaction.

func (*Bucket) CreateBucket

func (b *Bucket) CreateBucket(key []byte) (*Bucket, error)

CreateBucket creates a new bucket at the given key and returns the new bucket. Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long. The bucket instance is only valid for the lifetime of the transaction.

func (*Bucket) CreateBucketIfNotExists

func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error)

CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it. Returns an error if the bucket name is blank, or if the bucket name is too long. The bucket instance is only valid for the lifetime of the transaction.

func (*Bucket) Cursor

func (b *Bucket) Cursor() *Cursor

Cursor creates a cursor associated with the bucket. The cursor is only valid as long as the transaction is open. Do not use a cursor after the transaction is closed.

func (*Bucket) Delete

func (b *Bucket) Delete(key []byte) error

Delete removes a key from the bucket. If the key does not exist then nothing is done and a nil error is returned. Returns an error if the bucket was created from a read-only transaction.

Example
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
	log.Fatal(err)
}
defer os.Remove(db.Path())

// Start a write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
	// Create a bucket.
	b, err := tx.CreateBucket([]byte("widgets"))
	if err != nil {
		return err
	}

	// Set the value "bar" for the key "foo".
	if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
		return err
	}

	// Retrieve the key back from the database and verify it.
	value := b.Get([]byte("foo"))
	fmt.Printf("The value of 'foo' was: %s\n", value)

	return nil
}); err != nil {
	log.Fatal(err)
}

// Delete the key in a different write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
	return tx.Bucket([]byte("widgets")).Delete([]byte("foo"))
}); err != nil {
	log.Fatal(err)
}

// Retrieve the key again.
if err := db.View(func(tx *bolt.Tx) error {
	value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
	if value == nil {
		fmt.Printf("The value of 'foo' is now: nil\n")
	}
	return nil
}); err != nil {
	log.Fatal(err)
}

// Close database to release file lock.
if err := db.Close(); err != nil {
	log.Fatal(err)
}
Output:

The value of 'foo' was: bar
The value of 'foo' is now: nil

func (*Bucket) DeleteBucket

func (b *Bucket) DeleteBucket(key []byte) error

DeleteBucket deletes a bucket at the given key. Returns an error if the bucket does not exist, or if the key represents a non-bucket value.

func (*Bucket) ForEach

func (b *Bucket) ForEach(fn func(k, v []byte) error) error

ForEach executes a function for each key/value pair in a bucket. Because ForEach uses a Cursor, the iteration over keys is in lexicographical order. If the provided function returns an error then the iteration is stopped and the error is returned to the caller. The provided function must not modify the bucket; this will result in undefined behavior.

Example
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
	log.Fatal(err)
}
defer os.Remove(db.Path())

// Insert data into a bucket.
if err := db.Update(func(tx *bolt.Tx) error {
	b, err := tx.CreateBucket([]byte("animals"))
	if err != nil {
		return err
	}

	if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
		return err
	}
	if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
		return err
	}
	if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
		return err
	}

	// Iterate over items in sorted key order.
	if err := b.ForEach(func(k, v []byte) error {
		fmt.Printf("A %s is %s.\n", k, v)
		return nil
	}); err != nil {
		return err
	}

	return nil
}); err != nil {
	log.Fatal(err)
}

// Close database to release file lock.
if err := db.Close(); err != nil {
	log.Fatal(err)
}
Output:

A cat is lame.
A dog is fun.
A liger is awesome.

func (*Bucket) ForEachBucket

func (b *Bucket) ForEachBucket(fn func(k []byte) error) error

func (*Bucket) Get

func (b *Bucket) Get(key []byte) []byte

Get retrieves the value for a key in the bucket. Returns a nil value if the key does not exist or if the key is a nested bucket. The returned value is only valid for the life of the transaction. The returned memory is owned by bbolt and must never be modified; writing to this memory might corrupt the database.

func (*Bucket) NextSequence

func (b *Bucket) NextSequence() (uint64, error)

NextSequence returns an autoincrementing integer for the bucket.

func (*Bucket) Put

func (b *Bucket) Put(key []byte, value []byte) error

Put sets the value for a key in the bucket. If the key exist then its previous value will be overwritten. Supplied value must remain valid for the life of the transaction. Returns an error if the bucket was created from a read-only transaction, if the key is blank, if the key is too large, or if the value is too large.

Example
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
	log.Fatal(err)
}
defer os.Remove(db.Path())

// Start a write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
	// Create a bucket.
	b, err := tx.CreateBucket([]byte("widgets"))
	if err != nil {
		return err
	}

	// Set the value "bar" for the key "foo".
	if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
		return err
	}
	return nil
}); err != nil {
	log.Fatal(err)
}

// Read value back in a different read-only transaction.
if err := db.View(func(tx *bolt.Tx) error {
	value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
	fmt.Printf("The value of 'foo' is: %s\n", value)
	return nil
}); err != nil {
	log.Fatal(err)
}

// Close database to release file lock.
if err := db.Close(); err != nil {
	log.Fatal(err)
}
Output:

The value of 'foo' is: bar

func (*Bucket) Root

func (b *Bucket) Root() common.Pgid

Root returns the root of the bucket.

func (*Bucket) Sequence

func (b *Bucket) Sequence() uint64

Sequence returns the current integer for the bucket without incrementing it.

func (*Bucket) SetSequence

func (b *Bucket) SetSequence(v uint64) error

SetSequence updates the sequence number for the bucket.

func (*Bucket) Stats

func (b *Bucket) Stats() BucketStats

Stats returns stats on a bucket.

func (*Bucket) Tx

func (b *Bucket) Tx() *Tx

Tx returns the tx of the bucket.

func (*Bucket) Writable

func (b *Bucket) Writable() bool

Writable returns whether the bucket is writable.

type BucketStats

type BucketStats struct {
	// Page count statistics.
	BranchPageN     int // number of logical branch pages
	BranchOverflowN int // number of physical branch overflow pages
	LeafPageN       int // number of logical leaf pages
	LeafOverflowN   int // number of physical leaf overflow pages

	// Tree statistics.
	KeyN  int // number of keys/value pairs
	Depth int // number of levels in B+tree

	// Page size utilization.
	BranchAlloc int // bytes allocated for physical branch pages
	BranchInuse int // bytes actually used for branch data
	LeafAlloc   int // bytes allocated for physical leaf pages
	LeafInuse   int // bytes actually used for leaf data

	// Bucket statistics
	BucketN           int // total number of buckets including the top bucket
	InlineBucketN     int // total number on inlined buckets
	InlineBucketInuse int // bytes used for inlined buckets (also accounted for in LeafInuse)
}

BucketStats records statistics about resources used by a bucket.

func (*BucketStats) Add

func (s *BucketStats) Add(other BucketStats)

type CheckOption

type CheckOption func(options *checkConfig)

func WithKVStringer

func WithKVStringer(kvStringer KVStringer) CheckOption

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

Cursor represents an iterator that can traverse over all key/value pairs in a bucket in lexicographical order. Cursors see nested buckets with value == nil. Cursors can be obtained from a transaction and are valid as long as the transaction is open.

Keys and values returned from the cursor are only valid for the life of the transaction.

Changing data while traversing with a cursor may cause it to be invalidated and return unexpected keys and/or values. You must reposition your cursor after mutating data.

Example
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
	log.Fatal(err)
}
defer os.Remove(db.Path())

// Start a read-write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
	// Create a new bucket.
	b, err := tx.CreateBucket([]byte("animals"))
	if err != nil {
		return err
	}

	// Insert data into a bucket.
	if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
		log.Fatal(err)
	}
	if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
		log.Fatal(err)
	}
	if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
		log.Fatal(err)
	}

	// Create a cursor for iteration.
	c := b.Cursor()

	// Iterate over items in sorted key order. This starts from the
	// first key/value pair and updates the k/v variables to the
	// next key/value on each iteration.
	//
	// The loop finishes at the end of the cursor when a nil key is returned.
	for k, v := c.First(); k != nil; k, v = c.Next() {
		fmt.Printf("A %s is %s.\n", k, v)
	}

	return nil
}); err != nil {
	log.Fatal(err)
}

if err := db.Close(); err != nil {
	log.Fatal(err)
}
Output:

A cat is lame.
A dog is fun.
A liger is awesome.
Example (Reverse)
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
	log.Fatal(err)
}
defer os.Remove(db.Path())

// Start a read-write transaction.
if err := db.Update(func(tx *bolt.Tx) error {
	// Create a new bucket.
	b, err := tx.CreateBucket([]byte("animals"))
	if err != nil {
		return err
	}

	// Insert data into a bucket.
	if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
		log.Fatal(err)
	}
	if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
		log.Fatal(err)
	}
	if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
		log.Fatal(err)
	}

	// Create a cursor for iteration.
	c := b.Cursor()

	// Iterate over items in reverse sorted key order. This starts
	// from the last key/value pair and updates the k/v variables to
	// the previous key/value on each iteration.
	//
	// The loop finishes at the beginning of the cursor when a nil key
	// is returned.
	for k, v := c.Last(); k != nil; k, v = c.Prev() {
		fmt.Printf("A %s is %s.\n", k, v)
	}

	return nil
}); err != nil {
	log.Fatal(err)
}

// Close the database to release the file lock.
if err := db.Close(); err != nil {
	log.Fatal(err)
}
Output:

A liger is awesome.
A dog is fun.
A cat is lame.

func (*Cursor) Bucket

func (c *Cursor) Bucket() *Bucket

Bucket returns the bucket that this cursor was created from.

func (*Cursor) Delete

func (c *Cursor) Delete() error

Delete removes the current key/value under the cursor from the bucket. Delete fails if current key/value is a bucket or if the transaction is not writable.

func (*Cursor) First

func (c *Cursor) First() (key []byte, value []byte)

First moves the cursor to the first item in the bucket and returns its key and value. If the bucket is empty then a nil key and value are returned. The returned key and value are only valid for the life of the transaction.

func (*Cursor) Last

func (c *Cursor) Last() (key []byte, value []byte)

Last moves the cursor to the last item in the bucket and returns its key and value. If the bucket is empty then a nil key and value are returned. The returned key and value are only valid for the life of the transaction.

func (*Cursor) Next

func (c *Cursor) Next() (key []byte, value []byte)

Next moves the cursor to the next item in the bucket and returns its key and value. If the cursor is at the end of the bucket then a nil key and value are returned. The returned key and value are only valid for the life of the transaction.

func (*Cursor) Prev

func (c *Cursor) Prev() (key []byte, value []byte)

Prev moves the cursor to the previous item in the bucket and returns its key and value. If the cursor is at the beginning of the bucket then a nil key and value are returned. The returned key and value are only valid for the life of the transaction.

func (*Cursor) Seek

func (c *Cursor) Seek(seek []byte) (key []byte, value []byte)

Seek moves the cursor to a given key using a b-tree search and returns it. If the key does not exist then the next key is used. If no keys follow, a nil key is returned. The returned key and value are only valid for the life of the transaction.

type DB

type DB struct {

	// When enabled, the database will perform a Check() after every commit.
	// A panic is issued if the database is in an inconsistent state. This
	// flag has a large performance impact so it should only be used for
	// debugging purposes.
	StrictMode bool

	// Setting the NoSync flag will cause the database to skip fsync()
	// calls after each commit. This can be useful when bulk loading data
	// into a database and you can restart the bulk load in the event of
	// a system failure or database corruption. Do not set this flag for
	// normal use.
	//
	// If the package global IgnoreNoSync constant is true, this value is
	// ignored.  See the comment on that constant for more details.
	//
	// THIS IS UNSAFE. PLEASE USE WITH CAUTION.
	NoSync bool

	// When true, skips syncing freelist to disk. This improves the database
	// write performance under normal operation, but requires a full database
	// re-sync during recovery.
	NoFreelistSync bool

	// FreelistType sets the backend freelist type. There are two options. Array which is simple but endures
	// dramatic performance degradation if database is large and fragmentation in freelist is common.
	// The alternative one is using hashmap, it is faster in almost all circumstances
	// but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe.
	// The default type is array
	FreelistType FreelistType

	// When true, skips the truncate call when growing the database.
	// Setting this to true is only safe on non-ext3/ext4 systems.
	// Skipping truncation avoids preallocation of hard drive space and
	// bypasses a truncate() and fsync() syscall on remapping.
	//
	// https://github.com/boltdb/bolt/issues/284
	NoGrowSync bool

	// When `true`, bbolt will always load the free pages when opening the DB.
	// When opening db in write mode, this flag will always automatically
	// set to `true`.
	PreLoadFreelist bool

	// If you want to read the entire database fast, you can set MmapFlag to
	// syscall.MAP_POPULATE on Linux 2.6.23+ for sequential read-ahead.
	MmapFlags int

	// MaxBatchSize is the maximum size of a batch. Default value is
	// copied from DefaultMaxBatchSize in Open.
	//
	// If <=0, disables batching.
	//
	// Do not change concurrently with calls to Batch.
	MaxBatchSize int

	// MaxBatchDelay is the maximum delay before a batch starts.
	// Default value is copied from DefaultMaxBatchDelay in Open.
	//
	// If <=0, effectively disables batching.
	//
	// Do not change concurrently with calls to Batch.
	MaxBatchDelay time.Duration

	// AllocSize is the amount of space allocated when the database
	// needs to create new pages. This is done to amortize the cost
	// of truncate() and fsync() when growing the data file.
	AllocSize int

	// Mlock locks database file in memory when set to true.
	// It prevents major page faults, however used memory can't be reclaimed.
	//
	// Supported only on Unix via mlock/munlock syscalls.
	Mlock bool
	// contains filtered or unexported fields
}

DB represents a collection of buckets persisted to a file on disk. All data access is performed through transactions which can be obtained through the DB. All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.

func Open

func Open(path string, mode os.FileMode, options *Options) (*DB, error)

Open creates and opens a database at the given path with a given file mode. If the file does not exist then it will be created automatically with a given file mode. Passing in nil options will cause Bolt to open the database with the default options. Note: For read/write transactions, ensure the owner has write permission on the created/opened database file, e.g. 0600

func (*DB) Batch

func (db *DB) Batch(fn func(*Tx) error) error

Batch calls fn as part of a batch. It behaves similar to Update, except:

1. concurrent Batch calls can be combined into a single Bolt transaction.

2. the function passed to Batch may be called multiple times, regardless of whether it returns error or not.

This means that Batch function side effects must be idempotent and take permanent effect only after a successful return is seen in caller.

The maximum batch size and delay can be adjusted with DB.MaxBatchSize and DB.MaxBatchDelay, respectively.

Batch is only useful when there are multiple goroutines calling it.

func (*DB) Begin

func (db *DB) Begin(writable bool) (*Tx, error)

Begin starts a new transaction. Multiple read-only transactions can be used concurrently but only one write transaction can be used at a time. Starting multiple write transactions will cause the calls to block and be serialized until the current write transaction finishes.

Transactions should not be dependent on one another. Opening a read transaction and a write transaction in the same goroutine can cause the writer to deadlock because the database periodically needs to re-mmap itself as it grows and it cannot do that while a read transaction is open.

If a long running read transaction (for example, a snapshot transaction) is needed, you might want to set DB.InitialMmapSize to a large enough value to avoid potential blocking of write transaction.

IMPORTANT: You must close read-only transactions after you are finished or else the database will not reclaim old pages.

Example
package main

import (
	"fmt"
	"log"
	"os"

	bolt "go.etcd.io/bbolt"
)

func main() {
	// Open the database.
	db, err := bolt.Open(tempfile(), 0600, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(db.Path())

	// Create a bucket using a read-write transaction.
	if err = db.Update(func(tx *bolt.Tx) error {
		_, err := tx.CreateBucket([]byte("widgets"))
		return err
	}); err != nil {
		log.Fatal(err)
	}

	// Create several keys in a transaction.
	tx, err := db.Begin(true)
	if err != nil {
		log.Fatal(err)
	}
	b := tx.Bucket([]byte("widgets"))
	if err = b.Put([]byte("john"), []byte("blue")); err != nil {
		log.Fatal(err)
	}
	if err = b.Put([]byte("abby"), []byte("red")); err != nil {
		log.Fatal(err)
	}
	if err = b.Put([]byte("zephyr"), []byte("purple")); err != nil {
		log.Fatal(err)
	}
	if err = tx.Commit(); err != nil {
		log.Fatal(err)
	}

	// Iterate over the values in sorted key order.
	tx, err = db.Begin(false)
	if err != nil {
		log.Fatal(err)
	}
	c := tx.Bucket([]byte("widgets")).Cursor()
	for k, v := c.First(); k != nil; k, v = c.Next() {
		fmt.Printf("%s likes %s\n", k, v)
	}

	if err = tx.Rollback(); err != nil {
		log.Fatal(err)
	}

	if err = db.Close(); err != nil {
		log.Fatal(err)
	}

}

// tempfile returns a temporary file path.
func tempfile() string {
	f, err := os.CreateTemp("", "bolt-")
	if err != nil {
		panic(err)
	}
	if err := f.Close(); err != nil {
		panic(err)
	}
	if err := os.Remove(f.Name()); err != nil {
		panic(err)
	}
	return f.Name()
}
Output:

abby likes red
john likes blue
zephyr likes purple

func (*DB) Close

func (db *DB) Close() error

Close releases all database resources. It will block waiting for any open transactions to finish before closing the database and returning.

func (*DB) GoString

func (db *DB) GoString() string

GoString returns the Go string representation of the database.

func (*DB) Info

func (db *DB) Info() *Info

This is for internal access to the raw data bytes from the C cursor, use carefully, or not at all.

func (*DB) IsReadOnly

func (db *DB) IsReadOnly() bool

func (*DB) Path

func (db *DB) Path() string

Path returns the path to currently open database file.

func (*DB) Stats

func (db *DB) Stats() Stats

Stats retrieves ongoing performance stats for the database. This is only updated when a transaction closes.

func (*DB) String

func (db *DB) String() string

String returns the string representation of the database.

func (*DB) Sync

func (db *DB) Sync() error

Sync executes fdatasync() against the database file handle.

This is not necessary under normal operation, however, if you use NoSync then it allows you to force the database file to sync against the disk.

func (*DB) Update

func (db *DB) Update(fn func(*Tx) error) error

Update executes a function within the context of a read-write managed transaction. If no error is returned from the function then the transaction is committed. If an error is returned then the entire transaction is rolled back. Any error that is returned from the function or returned from the commit is returned from the Update() method.

Attempting to manually commit or rollback within the function will cause a panic.

Example
package main

import (
	"fmt"
	"log"
	"os"

	bolt "go.etcd.io/bbolt"
)

func main() {
	// Open the database.
	db, err := bolt.Open(tempfile(), 0600, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(db.Path())

	// Execute several commands within a read-write transaction.
	if err := db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucket([]byte("widgets"))
		if err != nil {
			return err
		}
		if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
			return err
		}
		return nil
	}); err != nil {
		log.Fatal(err)
	}

	// Read the value back from a separate read-only transaction.
	if err := db.View(func(tx *bolt.Tx) error {
		value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
		fmt.Printf("The value of 'foo' is: %s\n", value)
		return nil
	}); err != nil {
		log.Fatal(err)
	}

	// Close database to release the file lock.
	if err := db.Close(); err != nil {
		log.Fatal(err)
	}

}

// tempfile returns a temporary file path.
func tempfile() string {
	f, err := os.CreateTemp("", "bolt-")
	if err != nil {
		panic(err)
	}
	if err := f.Close(); err != nil {
		panic(err)
	}
	if err := os.Remove(f.Name()); err != nil {
		panic(err)
	}
	return f.Name()
}
Output:

The value of 'foo' is: bar

func (*DB) View

func (db *DB) View(fn func(*Tx) error) error

View executes a function within the context of a managed read-only transaction. Any error that is returned from the function is returned from the View() method.

Attempting to manually rollback within the function will cause a panic.

Example
package main

import (
	"fmt"
	"log"
	"os"

	bolt "go.etcd.io/bbolt"
)

func main() {
	// Open the database.
	db, err := bolt.Open(tempfile(), 0600, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(db.Path())

	// Insert data into a bucket.
	if err := db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucket([]byte("people"))
		if err != nil {
			return err
		}
		if err := b.Put([]byte("john"), []byte("doe")); err != nil {
			return err
		}
		if err := b.Put([]byte("susy"), []byte("que")); err != nil {
			return err
		}
		return nil
	}); err != nil {
		log.Fatal(err)
	}

	// Access data from within a read-only transactional block.
	if err := db.View(func(tx *bolt.Tx) error {
		v := tx.Bucket([]byte("people")).Get([]byte("john"))
		fmt.Printf("John's last name is %s.\n", v)
		return nil
	}); err != nil {
		log.Fatal(err)
	}

	// Close database to release the file lock.
	if err := db.Close(); err != nil {
		log.Fatal(err)
	}

}

// tempfile returns a temporary file path.
func tempfile() string {
	f, err := os.CreateTemp("", "bolt-")
	if err != nil {
		panic(err)
	}
	if err := f.Close(); err != nil {
		panic(err)
	}
	if err := os.Remove(f.Name()); err != nil {
		panic(err)
	}
	return f.Name()
}
Output:

John's last name is doe.

type FreelistType

type FreelistType string

FreelistType is the type of the freelist backend

type Info

type Info struct {
	Data     uintptr
	PageSize int
}

type KVStringer

type KVStringer interface {
	KeyToString([]byte) string
	ValueToString([]byte) string
}

KVStringer allows to prepare human-readable diagnostic messages.

func HexKVStringer

func HexKVStringer() KVStringer

HexKVStringer serializes both key & value to hex representation.

type Options

type Options struct {
	// Timeout is the amount of time to wait to obtain a file lock.
	// When set to zero it will wait indefinitely.
	Timeout time.Duration

	// Sets the DB.NoGrowSync flag before memory mapping the file.
	NoGrowSync bool

	// Do not sync freelist to disk. This improves the database write performance
	// under normal operation, but requires a full database re-sync during recovery.
	NoFreelistSync bool

	// PreLoadFreelist sets whether to load the free pages when opening
	// the db file. Note when opening db in write mode, bbolt will always
	// load the free pages.
	PreLoadFreelist bool

	// FreelistType sets the backend freelist type. There are two options. Array which is simple but endures
	// dramatic performance degradation if database is large and fragmentation in freelist is common.
	// The alternative one is using hashmap, it is faster in almost all circumstances
	// but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe.
	// The default type is array
	FreelistType FreelistType

	// Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to
	// grab a shared lock (UNIX).
	ReadOnly bool

	// Sets the DB.MmapFlags flag before memory mapping the file.
	MmapFlags int

	// InitialMmapSize is the initial mmap size of the database
	// in bytes. Read transactions won't block write transaction
	// if the InitialMmapSize is large enough to hold database mmap
	// size. (See DB.Begin for more information)
	//
	// If <=0, the initial map size is 0.
	// If initialMmapSize is smaller than the previous database size,
	// it takes no effect.
	InitialMmapSize int

	// PageSize overrides the default OS page size.
	PageSize int

	// NoSync sets the initial value of DB.NoSync. Normally this can just be
	// set directly on the DB itself when returned from Open(), but this option
	// is useful in APIs which expose Options but not the underlying DB.
	NoSync bool

	// OpenFile is used to open files. It defaults to os.OpenFile. This option
	// is useful for writing hermetic tests.
	OpenFile func(string, int, os.FileMode) (*os.File, error)

	// Mlock locks database file in memory when set to true.
	// It prevents potential page faults, however
	// used memory can't be reclaimed. (UNIX only)
	Mlock bool
}

Options represents the options that can be set when opening a database.

type Stats

type Stats struct {
	// Put `TxStats` at the first field to ensure it's 64-bit aligned. Note
	// that the first word in an allocated struct can be relied upon to be
	// 64-bit aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG.
	// Also refer to discussion in https://github.com/etcd-io/bbolt/issues/577.
	TxStats TxStats // global, ongoing stats.

	// Freelist stats
	FreePageN     int // total number of free pages on the freelist
	PendingPageN  int // total number of pending pages on the freelist
	FreeAlloc     int // total bytes allocated in free pages
	FreelistInuse int // total bytes used by the freelist

	// Transaction stats
	TxN     int // total number of started read transactions
	OpenTxN int // number of currently open read transactions
}

Stats represents statistics about the database.

func (*Stats) Sub

func (s *Stats) Sub(other *Stats) Stats

Sub calculates and returns the difference between two sets of database stats. This is useful when obtaining stats at two different points and time and you need the performance counters that occurred within that time span.

type Tx

type Tx struct {

	// WriteFlag specifies the flag for write-related methods like WriteTo().
	// Tx opens the database file with the specified flag to copy the data.
	//
	// By default, the flag is unset, which works well for mostly in-memory
	// workloads. For databases that are much larger than available RAM,
	// set the flag to syscall.O_DIRECT to avoid trashing the page cache.
	WriteFlag int
	// contains filtered or unexported fields
}

Tx represents a read-only or read/write transaction on the database. Read-only transactions can be used for retrieving values for keys and creating cursors. Read/write transactions can create and remove buckets and create and remove keys.

IMPORTANT: You must commit or rollback transactions when you are done with them. Pages can not be reclaimed by the writer until no more transactions are using them. A long running read transaction can cause the database to quickly grow.

func (*Tx) Bucket

func (tx *Tx) Bucket(name []byte) *Bucket

Bucket retrieves a bucket by name. Returns nil if the bucket does not exist. The bucket instance is only valid for the lifetime of the transaction.

func (*Tx) Check

func (tx *Tx) Check(options ...CheckOption) <-chan error

Check performs several consistency checks on the database for this transaction. An error is returned if any inconsistency is found.

It can be safely run concurrently on a writable transaction. However, this incurs a high cost for large databases and databases with a lot of subbuckets because of caching. This overhead can be removed if running on a read-only transaction, however, it is not safe to execute other writer transactions at the same time.

It also allows users to provide a customized `KVStringer` implementation, so that bolt can generate human-readable diagnostic messages.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit writes all changes to disk, updates the meta page and closes the transaction. Returns an error if a disk write error occurs, or if Commit is called on a read-only transaction.

func (*Tx) Copy deprecated

func (tx *Tx) Copy(w io.Writer) error

Copy writes the entire database to a writer. This function exists for backwards compatibility.

Deprecated: Use WriteTo() instead.

func (*Tx) CopyFile

func (tx *Tx) CopyFile(path string, mode os.FileMode) error

CopyFile copies the entire database to file at the given path. A reader transaction is maintained during the copy so it is safe to continue using the database while a copy is in progress.

Example
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
	log.Fatal(err)
}
defer os.Remove(db.Path())

// Create a bucket and a key.
if err := db.Update(func(tx *bolt.Tx) error {
	b, err := tx.CreateBucket([]byte("widgets"))
	if err != nil {
		return err
	}
	if err := b.Put([]byte("foo"), []byte("bar")); err != nil {
		return err
	}
	return nil
}); err != nil {
	log.Fatal(err)
}

// Copy the database to another file.
toFile := tempfile()
if err := db.View(func(tx *bolt.Tx) error {
	return tx.CopyFile(toFile, 0666)
}); err != nil {
	log.Fatal(err)
}
defer os.Remove(toFile)

// Open the cloned database.
db2, err := bolt.Open(toFile, 0600, nil)
if err != nil {
	log.Fatal(err)
}

// Ensure that the key exists in the copy.
if err := db2.View(func(tx *bolt.Tx) error {
	value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
	fmt.Printf("The value for 'foo' in the clone is: %s\n", value)
	return nil
}); err != nil {
	log.Fatal(err)
}

// Close database to release file lock.
if err := db.Close(); err != nil {
	log.Fatal(err)
}

if err := db2.Close(); err != nil {
	log.Fatal(err)
}
Output:

The value for 'foo' in the clone is: bar

func (*Tx) CreateBucket

func (tx *Tx) CreateBucket(name []byte) (*Bucket, error)

CreateBucket creates a new bucket. Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long. The bucket instance is only valid for the lifetime of the transaction.

func (*Tx) CreateBucketIfNotExists

func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error)

CreateBucketIfNotExists creates a new bucket if it doesn't already exist. Returns an error if the bucket name is blank, or if the bucket name is too long. The bucket instance is only valid for the lifetime of the transaction.

func (*Tx) Cursor

func (tx *Tx) Cursor() *Cursor

Cursor creates a cursor associated with the root bucket. All items in the cursor will return a nil value because all root bucket keys point to buckets. The cursor is only valid as long as the transaction is open. Do not use a cursor after the transaction is closed.

func (*Tx) DB

func (tx *Tx) DB() *DB

DB returns a reference to the database that created the transaction.

func (*Tx) DeleteBucket

func (tx *Tx) DeleteBucket(name []byte) error

DeleteBucket deletes a bucket. Returns an error if the bucket cannot be found or if the key represents a non-bucket value.

func (*Tx) ForEach

func (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error

ForEach executes a function for each bucket in the root. If the provided function returns an error then the iteration is stopped and the error is returned to the caller.

func (*Tx) ID

func (tx *Tx) ID() int

ID returns the transaction id.

func (*Tx) OnCommit

func (tx *Tx) OnCommit(fn func())

OnCommit adds a handler function to be executed after the transaction successfully commits.

func (*Tx) Page

func (tx *Tx) Page(id int) (*common.PageInfo, error)

Page returns page information for a given page number. This is only safe for concurrent use when used by a writable transaction.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback closes the transaction and ignores all previous updates. Read-only transactions must be rolled back and not committed.

Example
// Open the database.
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
	log.Fatal(err)
}
defer os.Remove(db.Path())

// Create a bucket.
if err := db.Update(func(tx *bolt.Tx) error {
	_, err := tx.CreateBucket([]byte("widgets"))
	return err
}); err != nil {
	log.Fatal(err)
}

// Set a value for a key.
if err := db.Update(func(tx *bolt.Tx) error {
	return tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar"))
}); err != nil {
	log.Fatal(err)
}

// Update the key but rollback the transaction so it never saves.
tx, err := db.Begin(true)
if err != nil {
	log.Fatal(err)
}
b := tx.Bucket([]byte("widgets"))
if err := b.Put([]byte("foo"), []byte("baz")); err != nil {
	log.Fatal(err)
}
if err := tx.Rollback(); err != nil {
	log.Fatal(err)
}

// Ensure that our original value is still set.
if err := db.View(func(tx *bolt.Tx) error {
	value := tx.Bucket([]byte("widgets")).Get([]byte("foo"))
	fmt.Printf("The value for 'foo' is still: %s\n", value)
	return nil
}); err != nil {
	log.Fatal(err)
}

// Close database to release file lock.
if err := db.Close(); err != nil {
	log.Fatal(err)
}
Output:

The value for 'foo' is still: bar

func (*Tx) Size

func (tx *Tx) Size() int64

Size returns current database size in bytes as seen by this transaction.

func (*Tx) Stats

func (tx *Tx) Stats() TxStats

Stats retrieves a copy of the current transaction statistics.

func (*Tx) Writable

func (tx *Tx) Writable() bool

Writable returns whether the transaction can perform write operations.

func (*Tx) WriteTo

func (tx *Tx) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes the entire database to a writer. If err == nil then exactly tx.Size() bytes will be written into the writer.

type TxStats

type TxStats struct {
	// Page statistics.
	//
	// DEPRECATED: Use GetPageCount() or IncPageCount()
	PageCount int64 // number of page allocations
	// DEPRECATED: Use GetPageAlloc() or IncPageAlloc()
	PageAlloc int64 // total bytes allocated

	// Cursor statistics.
	//
	// DEPRECATED: Use GetCursorCount() or IncCursorCount()
	CursorCount int64 // number of cursors created

	// Node statistics
	//
	// DEPRECATED: Use GetNodeCount() or IncNodeCount()
	NodeCount int64 // number of node allocations
	// DEPRECATED: Use GetNodeDeref() or IncNodeDeref()
	NodeDeref int64 // number of node dereferences

	// Rebalance statistics.
	//
	// DEPRECATED: Use GetRebalance() or IncRebalance()
	Rebalance int64 // number of node rebalances
	// DEPRECATED: Use GetRebalanceTime() or IncRebalanceTime()
	RebalanceTime time.Duration // total time spent rebalancing

	// Split/Spill statistics.
	//
	// DEPRECATED: Use GetSplit() or IncSplit()
	Split int64 // number of nodes split
	// DEPRECATED: Use GetSpill() or IncSpill()
	Spill int64 // number of nodes spilled
	// DEPRECATED: Use GetSpillTime() or IncSpillTime()
	SpillTime time.Duration // total time spent spilling

	// Write statistics.
	//
	// DEPRECATED: Use GetWrite() or IncWrite()
	Write int64 // number of writes performed
	// DEPRECATED: Use GetWriteTime() or IncWriteTime()
	WriteTime time.Duration // total time spent writing to disk
}

TxStats represents statistics about the actions performed by the transaction.

func (*TxStats) GetCursorCount

func (s *TxStats) GetCursorCount() int64

GetCursorCount returns CursorCount atomically.

func (*TxStats) GetNodeCount

func (s *TxStats) GetNodeCount() int64

GetNodeCount returns NodeCount atomically.

func (*TxStats) GetNodeDeref

func (s *TxStats) GetNodeDeref() int64

GetNodeDeref returns NodeDeref atomically.

func (*TxStats) GetPageAlloc

func (s *TxStats) GetPageAlloc() int64

GetPageAlloc returns PageAlloc atomically.

func (*TxStats) GetPageCount

func (s *TxStats) GetPageCount() int64

GetPageCount returns PageCount atomically.

func (*TxStats) GetRebalance

func (s *TxStats) GetRebalance() int64

GetRebalance returns Rebalance atomically.

func (*TxStats) GetRebalanceTime

func (s *TxStats) GetRebalanceTime() time.Duration

GetRebalanceTime returns RebalanceTime atomically.

func (*TxStats) GetSpill

func (s *TxStats) GetSpill() int64

GetSpill returns Spill atomically.

func (*TxStats) GetSpillTime

func (s *TxStats) GetSpillTime() time.Duration

GetSpillTime returns SpillTime atomically.

func (*TxStats) GetSplit

func (s *TxStats) GetSplit() int64

GetSplit returns Split atomically.

func (*TxStats) GetWrite

func (s *TxStats) GetWrite() int64

GetWrite returns Write atomically.

func (*TxStats) GetWriteTime

func (s *TxStats) GetWriteTime() time.Duration

GetWriteTime returns WriteTime atomically.

func (*TxStats) IncCursorCount

func (s *TxStats) IncCursorCount(delta int64) int64

IncCursorCount increases CursorCount atomically and return the new value.

func (*TxStats) IncNodeCount

func (s *TxStats) IncNodeCount(delta int64) int64

IncNodeCount increases NodeCount atomically and returns the new value.

func (*TxStats) IncNodeDeref

func (s *TxStats) IncNodeDeref(delta int64) int64

IncNodeDeref increases NodeDeref atomically and returns the new value.

func (*TxStats) IncPageAlloc

func (s *TxStats) IncPageAlloc(delta int64) int64

IncPageAlloc increases PageAlloc atomically and returns the new value.

func (*TxStats) IncPageCount

func (s *TxStats) IncPageCount(delta int64) int64

IncPageCount increases PageCount atomically and returns the new value.

func (*TxStats) IncRebalance

func (s *TxStats) IncRebalance(delta int64) int64

IncRebalance increases Rebalance atomically and returns the new value.

func (*TxStats) IncRebalanceTime

func (s *TxStats) IncRebalanceTime(delta time.Duration) time.Duration

IncRebalanceTime increases RebalanceTime atomically and returns the new value.

func (*TxStats) IncSpill

func (s *TxStats) IncSpill(delta int64) int64

IncSpill increases Spill atomically and returns the new value.

func (*TxStats) IncSpillTime

func (s *TxStats) IncSpillTime(delta time.Duration) time.Duration

IncSpillTime increases SpillTime atomically and returns the new value.

func (*TxStats) IncSplit

func (s *TxStats) IncSplit(delta int64) int64

IncSplit increases Split atomically and returns the new value.

func (*TxStats) IncWrite

func (s *TxStats) IncWrite(delta int64) int64

IncWrite increases Write atomically and returns the new value.

func (*TxStats) IncWriteTime

func (s *TxStats) IncWriteTime(delta time.Duration) time.Duration

IncWriteTime increases WriteTime atomically and returns the new value.

func (*TxStats) Sub

func (s *TxStats) Sub(other *TxStats) TxStats

Sub calculates and returns the difference between two sets of transaction stats. This is useful when obtaining stats at two different points and time and you need the performance counters that occurred within that time span.

Directories

Path Synopsis
cmd
Package errors defines the error variables that may be returned during bbolt operations.
Package errors defines the error variables that may be returned during bbolt operations.
internal
common
Copied from https://github.com/etcd-io/etcd/blob/main/client/pkg/verify/verify.go
Copied from https://github.com/etcd-io/etcd/blob/main/client/pkg/verify/verify.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL