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 ¶
- Constants
- Variables
- func Compact(dst, src *DB, txMaxSize int64) error
- type Bucket
- func (b *Bucket) Bucket(name []byte) *Bucket
- func (b *Bucket) CreateBucket(key []byte) (*Bucket, error)
- func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error)
- func (b *Bucket) Cursor() *Cursor
- func (b *Bucket) Delete(key []byte) error
- func (b *Bucket) DeleteBucket(key []byte) error
- func (b *Bucket) ForEach(fn func(k, v []byte) error) error
- func (b *Bucket) ForEachBucket(fn func(k []byte) error) error
- func (b *Bucket) Get(key []byte) []byte
- func (b *Bucket) NextSequence() (uint64, error)
- func (b *Bucket) Put(key []byte, value []byte) error
- func (b *Bucket) Root() pgid
- func (b *Bucket) Sequence() uint64
- func (b *Bucket) SetSequence(v uint64) error
- func (b *Bucket) Stats() BucketStats
- func (b *Bucket) Tx() *Tx
- func (b *Bucket) Writable() bool
- type BucketStats
- type CheckOption
- type Cursor
- func (c *Cursor) Bucket() *Bucket
- func (c *Cursor) Delete() error
- func (c *Cursor) First() (key []byte, value []byte)
- func (c *Cursor) Last() (key []byte, value []byte)
- func (c *Cursor) Next() (key []byte, value []byte)
- func (c *Cursor) Prev() (key []byte, value []byte)
- func (c *Cursor) Seek(seek []byte) (key []byte, value []byte)
- type DB
- func (db *DB) Batch(fn func(*Tx) error) error
- func (db *DB) Begin(writable bool) (*Tx, error)
- func (db *DB) Close() error
- func (db *DB) GoString() string
- func (db *DB) Info() *Info
- func (db *DB) IsReadOnly() bool
- func (db *DB) Path() string
- func (db *DB) Stats() Stats
- func (db *DB) String() string
- func (db *DB) Sync() error
- func (db *DB) Update(fn func(*Tx) error) error
- func (db *DB) View(fn func(*Tx) error) error
- type FreelistType
- type Info
- type KVStringer
- type Options
- type PageInfo
- type Stats
- type Tx
- func (tx *Tx) Bucket(name []byte) *Bucket
- func (tx *Tx) Check() <-chan error
- func (tx *Tx) CheckWithOptions(options ...CheckOption) <-chan error
- func (tx *Tx) Commit() error
- func (tx *Tx) Copy(w io.Writer) error
- func (tx *Tx) CopyFile(path string, mode os.FileMode) error
- func (tx *Tx) CreateBucket(name []byte) (*Bucket, error)
- func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error)
- func (tx *Tx) Cursor() *Cursor
- func (tx *Tx) DB() *DB
- func (tx *Tx) DeleteBucket(name []byte) error
- func (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error
- func (tx *Tx) ID() int
- func (tx *Tx) OnCommit(fn func())
- func (tx *Tx) Page(id int) (*PageInfo, error)
- func (tx *Tx) Rollback() error
- func (tx *Tx) Size() int64
- func (tx *Tx) Stats() TxStats
- func (tx *Tx) Writable() bool
- func (tx *Tx) WriteTo(w io.Writer) (n int64, err error)
- type TxStats
- func (s *TxStats) GetCursorCount() int64
- func (s *TxStats) GetNodeCount() int64
- func (s *TxStats) GetNodeDeref() int64
- func (s *TxStats) GetPageAlloc() int64
- func (s *TxStats) GetPageCount() int64
- func (s *TxStats) GetRebalance() int64
- func (s *TxStats) GetRebalanceTime() time.Duration
- func (s *TxStats) GetSpill() int64
- func (s *TxStats) GetSpillTime() time.Duration
- func (s *TxStats) GetSplit() int64
- func (s *TxStats) GetWrite() int64
- func (s *TxStats) GetWriteTime() time.Duration
- func (s *TxStats) IncCursorCount(delta int64) int64
- func (s *TxStats) IncNodeCount(delta int64) int64
- func (s *TxStats) IncNodeDeref(delta int64) int64
- func (s *TxStats) IncPageAlloc(delta int64) int64
- func (s *TxStats) IncPageCount(delta int64) int64
- func (s *TxStats) IncRebalance(delta int64) int64
- func (s *TxStats) IncRebalanceTime(delta time.Duration) time.Duration
- func (s *TxStats) IncSpill(delta int64) int64
- func (s *TxStats) IncSpillTime(delta time.Duration) time.Duration
- func (s *TxStats) IncSplit(delta int64) int64
- func (s *TxStats) IncWrite(delta int64) int64
- func (s *TxStats) IncWriteTime(delta time.Duration) time.Duration
- func (s *TxStats) Sub(other *TxStats) TxStats
Examples ¶
Constants ¶
const ( // MaxKeySize is the maximum length of a key, in bytes. MaxKeySize = 32768 // MaxValueSize is the maximum length of a value, in bytes. MaxValueSize = (1 << 31) - 2 )
const ( DefaultMaxBatchSize int = 1000 DefaultMaxBatchDelay = 10 * time.Millisecond DefaultAllocSize = 16 * 1024 * 1024 )
Default values if not set in a DB instance.
const ( // FreelistArrayType indicates backend freelist type is array FreelistArrayType = FreelistType("array") // FreelistMapType indicates backend freelist type is hashmap FreelistMapType = FreelistType("hashmap") )
const DefaultFillPercent = 0.5
DefaultFillPercent is the percentage that split pages are filled. This value can be changed by setting Bucket.FillPercent.
const IgnoreNoSync = runtime.GOOS == "openbsd"
IgnoreNoSync specifies whether the NoSync field of a DB is ignored when syncing changes to a file. This is required as some operating systems, such as OpenBSD, do not have a unified buffer cache (UBC) and writes must be synchronized using the msync(2) syscall.
Variables ¶
var ( // ErrDatabaseNotOpen is returned when a DB instance is accessed before it // is opened or after it is closed. ErrDatabaseNotOpen = errors.New("database not open") // ErrDatabaseOpen is returned when opening a database that is // already open. ErrDatabaseOpen = errors.New("database already open") // ErrInvalid is returned when both meta pages on a database are invalid. // This typically occurs when a file is not a bolt database. ErrInvalid = errors.New("invalid database") // ErrInvalidMapping is returned when the database file fails to get mapped. ErrInvalidMapping = errors.New("database isn't correctly mapped") // ErrVersionMismatch is returned when the data file was created with a // different version of Bolt. ErrVersionMismatch = errors.New("version mismatch") // ErrChecksum is returned when either meta page checksum does not match. ErrChecksum = errors.New("checksum error") // ErrTimeout is returned when a database cannot obtain an exclusive lock // on the data file after the timeout passed to Open(). ErrTimeout = errors.New("timeout") )
These errors can be returned when opening or calling methods on a DB.
var ( // ErrTxNotWritable is returned when performing a write operation on a // read-only transaction. ErrTxNotWritable = errors.New("tx not writable") // ErrTxClosed is returned when committing or rolling back a transaction // that has already been committed or rolled back. ErrTxClosed = errors.New("tx closed") // ErrDatabaseReadOnly is returned when a mutating transaction is started on a // read-only database. ErrDatabaseReadOnly = errors.New("database is in read-only mode") // ErrFreePagesNotLoaded is returned when a readonly transaction without // preloading the free pages is trying to access the free pages. ErrFreePagesNotLoaded = errors.New("free pages are not pre-loaded") )
These errors can occur when beginning or committing a Tx.
var ( // ErrBucketNotFound is returned when trying to access a bucket that has // not been created yet. ErrBucketNotFound = errors.New("bucket not found") // ErrBucketExists is returned when creating a bucket that already exists. ErrBucketExists = errors.New("bucket already exists") // ErrBucketNameRequired is returned when creating a bucket with a blank name. ErrBucketNameRequired = errors.New("bucket name required") // ErrKeyRequired is returned when inserting a zero-length key. ErrKeyRequired = errors.New("key required") // ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize. ErrKeyTooLarge = errors.New("key too large") // ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize. ErrValueTooLarge = errors.New("value too large") // ErrIncompatibleValue is returned when trying create or delete a bucket // on an existing non-bucket key or when trying to create or delete a // non-bucket key on an existing bucket key. ErrIncompatibleValue = errors.New("incompatible value") )
These errors can occur when putting or deleting a value or a bucket.
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 ¶ added in v1.3.6
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 { // 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Start a write transaction. if err := db.Update(func(tx *bolt.Tx) error { // Create a bucket. b, err := tx.CreateBucket([]byte("widgets")) if err != nil { return err } // Set the value "bar" for the key "foo". if err := b.Put([]byte("foo"), []byte("bar")); err != nil { return err } // Retrieve the key back from the database and verify it. value := b.Get([]byte("foo")) fmt.Printf("The value of 'foo' was: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Delete the key in a different write transaction. if err := db.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Delete([]byte("foo")) }); err != nil { log.Fatal(err) } // Retrieve the key again. if err := db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) if value == nil { fmt.Printf("The value of 'foo' is now: nil\n") } return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) }
Output: The value of 'foo' was: bar The value of 'foo' is now: nil
func (*Bucket) DeleteBucket ¶
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 ¶
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Insert data into a bucket. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("animals")) if err != nil { return err } if err := b.Put([]byte("dog"), []byte("fun")); err != nil { return err } if err := b.Put([]byte("cat"), []byte("lame")); err != nil { return err } if err := b.Put([]byte("liger"), []byte("awesome")); err != nil { return err } // Iterate over items in sorted key order. if err := b.ForEach(func(k, v []byte) error { fmt.Printf("A %s is %s.\n", k, v) return nil }); err != nil { return err } return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) }
Output: A cat is lame. A dog is fun. A liger is awesome.
func (*Bucket) ForEachBucket ¶ added in v1.3.7
func (*Bucket) Get ¶
Get retrieves the value for a key in the bucket. Returns a nil value if the key does not exist or if the key is a nested bucket. The returned value is only valid for the life of the transaction.
func (*Bucket) NextSequence ¶
NextSequence returns an autoincrementing integer for the bucket.
func (*Bucket) Put ¶
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Start a write transaction. if err := db.Update(func(tx *bolt.Tx) error { // Create a bucket. b, err := tx.CreateBucket([]byte("widgets")) if err != nil { return err } // Set the value "bar" for the key "foo". if err := b.Put([]byte("foo"), []byte("bar")); err != nil { return err } return nil }); err != nil { log.Fatal(err) } // Read value back in a different read-only transaction. if err := db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value of 'foo' is: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) }
Output: The value of 'foo' is: bar
func (*Bucket) Sequence ¶ added in v1.3.2
Sequence returns the current integer for the bucket without incrementing it.
func (*Bucket) SetSequence ¶ added in v1.3.2
SetSequence updates the sequence number for the bucket.
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 ¶ added in v1.3.7
type CheckOption func(options *checkConfig)
func WithKVStringer ¶ added in v1.3.7
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Start a read-write transaction. if err := db.Update(func(tx *bolt.Tx) error { // Create a new bucket. b, err := tx.CreateBucket([]byte("animals")) if err != nil { return err } // Insert data into a bucket. if err := b.Put([]byte("dog"), []byte("fun")); err != nil { log.Fatal(err) } if err := b.Put([]byte("cat"), []byte("lame")); err != nil { log.Fatal(err) } if err := b.Put([]byte("liger"), []byte("awesome")); err != nil { log.Fatal(err) } // Create a cursor for iteration. c := b.Cursor() // Iterate over items in sorted key order. This starts from the // first key/value pair and updates the k/v variables to the // next key/value on each iteration. // // The loop finishes at the end of the cursor when a nil key is returned. for k, v := c.First(); k != nil; k, v = c.Next() { fmt.Printf("A %s is %s.\n", k, v) } return nil }); err != nil { log.Fatal(err) } if err := db.Close(); err != nil { log.Fatal(err) }
Output: A cat is lame. A dog is fun. A liger is awesome.
Example (Reverse) ¶
// Open the database. db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Start a read-write transaction. if err := db.Update(func(tx *bolt.Tx) error { // Create a new bucket. b, err := tx.CreateBucket([]byte("animals")) if err != nil { return err } // Insert data into a bucket. if err := b.Put([]byte("dog"), []byte("fun")); err != nil { log.Fatal(err) } if err := b.Put([]byte("cat"), []byte("lame")); err != nil { log.Fatal(err) } if err := b.Put([]byte("liger"), []byte("awesome")); err != nil { log.Fatal(err) } // Create a cursor for iteration. c := b.Cursor() // Iterate over items in reverse sorted key order. This starts // from the last key/value pair and updates the k/v variables to // the previous key/value on each iteration. // // The loop finishes at the beginning of the cursor when a nil key // is returned. for k, v := c.Last(); k != nil; k, v = c.Prev() { fmt.Printf("A %s is %s.\n", k, v) } return nil }); err != nil { log.Fatal(err) } // Close the database to release the file lock. if err := db.Close(); err != nil { log.Fatal(err) }
Output: A liger is awesome. A dog is fun. A cat is lame.
func (*Cursor) Delete ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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 ¶
Open creates and opens a database at the given path. If the file does not exist then it will be created automatically. Passing in nil options will cause Bolt to open the database with the default options.
func (*DB) Batch ¶
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 ¶
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Create a bucket using a read-write transaction. if err = db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { log.Fatal(err) } // Create several keys in a transaction. tx, err := db.Begin(true) if err != nil { log.Fatal(err) } b := tx.Bucket([]byte("widgets")) if err = b.Put([]byte("john"), []byte("blue")); err != nil { log.Fatal(err) } if err = b.Put([]byte("abby"), []byte("red")); err != nil { log.Fatal(err) } if err = b.Put([]byte("zephyr"), []byte("purple")); err != nil { log.Fatal(err) } if err = tx.Commit(); err != nil { log.Fatal(err) } // Iterate over the values in sorted key order. tx, err = db.Begin(false) if err != nil { log.Fatal(err) } c := tx.Bucket([]byte("widgets")).Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { fmt.Printf("%s likes %s\n", k, v) } if err = tx.Rollback(); err != nil { log.Fatal(err) } if err = db.Close(); err != nil { log.Fatal(err) } } // 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 ¶
Close releases all database resources. It will block waiting for any open transactions to finish before closing the database and returning.
func (*DB) 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) Stats ¶
Stats retrieves ongoing performance stats for the database. This is only updated when a transaction closes.
func (*DB) Sync ¶
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 ¶
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Execute several commands within a read-write transaction. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { return err } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { return err } return nil }); err != nil { log.Fatal(err) } // Read the value back from a separate read-only transaction. if err := db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value of 'foo' is: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Close database to release the file lock. if err := db.Close(); err != nil { log.Fatal(err) } } // 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 ¶
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Insert data into a bucket. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("people")) if err != nil { return err } if err := b.Put([]byte("john"), []byte("doe")); err != nil { return err } if err := b.Put([]byte("susy"), []byte("que")); err != nil { return err } return nil }); err != nil { log.Fatal(err) } // Access data from within a read-only transactional block. if err := db.View(func(tx *bolt.Tx) error { v := tx.Bucket([]byte("people")).Get([]byte("john")) fmt.Printf("John's last name is %s.\n", v) return nil }); err != nil { log.Fatal(err) } // Close database to release the file lock. if err := db.Close(); err != nil { log.Fatal(err) } } // 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 ¶ added in v1.3.2
type FreelistType string
FreelistType is the type of the freelist backend
type KVStringer ¶ added in v1.3.7
KVStringer allows to prepare human-readable diagnostic messages.
func HexKVStringer ¶ added in v1.3.7
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. This option is only // available on Darwin and Linux. Timeout time.Duration // Sets the DB.NoGrowSync flag before memory mapping the file. NoGrowSync bool // Do not sync freelist to disk. This improves the database write performance // under normal operation, but requires a full database re-sync during recovery. NoFreelistSync bool // PreLoadFreelist sets whether to load the free pages when opening // the db file. Note when opening db in write mode, bbolt will always // load the free pages. PreLoadFreelist bool // FreelistType sets the backend freelist type. There are two options. Array which is simple but endures // dramatic performance degradation if database is large and fragmentation in freelist is common. // The alternative one is using hashmap, it is faster in almost all circumstances // but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe. // The default type is array FreelistType FreelistType // Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to // grab a shared lock (UNIX). ReadOnly bool // Sets the DB.MmapFlags flag before memory mapping the file. MmapFlags int // InitialMmapSize is the initial mmap size of the database // in bytes. Read transactions won't block write transaction // if the InitialMmapSize is large enough to hold database mmap // size. (See DB.Begin for more information) // // If <=0, the initial map size is 0. // If initialMmapSize is smaller than the previous database size, // it takes no effect. InitialMmapSize int // PageSize overrides the default OS page size. PageSize int // NoSync sets the initial value of DB.NoSync. Normally this can just be // set directly on the DB itself when returned from Open(), but this option // is useful in APIs which expose Options but not the underlying DB. NoSync bool // OpenFile is used to open files. It defaults to os.OpenFile. This option // is useful for writing hermetic tests. OpenFile func(string, int, os.FileMode) (*os.File, error) // Mlock locks database file in memory when set to true. // It prevents potential page faults, however // used memory can't be reclaimed. (UNIX only) Mlock bool }
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.
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 ¶
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 ¶
Check performs several consistency checks on the database for this transaction. An error is returned if any inconsistency is found.
It can be safely run concurrently on a writable transaction. However, this incurs a high cost for large databases and databases with a lot of subbuckets because of caching. This overhead can be removed if running on a read-only transaction, however, it is not safe to execute other writer transactions at the same time.
func (*Tx) CheckWithOptions ¶ added in v1.3.7
func (tx *Tx) CheckWithOptions(options ...CheckOption) <-chan error
CheckWithOptions allows users to provide a customized `KVStringer` implementation, so that bolt can generate human-readable diagnostic messages.
func (*Tx) Commit ¶
Commit writes all changes to disk and updates the meta page. Returns an error if a disk write error occurs, or if Commit is called on a read-only transaction.
func (*Tx) Copy ¶
Copy writes the entire database to a writer. This function exists for backwards compatibility.
Deprecated; Use WriteTo() instead.
func (*Tx) CopyFile ¶
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Create a bucket and a key. if err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { return err } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { return err } return nil }); err != nil { log.Fatal(err) } // Copy the database to another file. toFile := tempfile() if err := db.View(func(tx *bolt.Tx) error { return tx.CopyFile(toFile, 0666) }); err != nil { log.Fatal(err) } defer os.Remove(toFile) // Open the cloned database. db2, err := bolt.Open(toFile, 0666, nil) if err != nil { log.Fatal(err) } // Ensure that the key exists in the copy. if err := db2.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value for 'foo' in the clone is: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) } if err := db2.Close(); err != nil { log.Fatal(err) }
Output: The value for 'foo' in the clone is: bar
func (*Tx) CreateBucket ¶
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 ¶
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 ¶
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) DeleteBucket ¶
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 ¶
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) OnCommit ¶
func (tx *Tx) OnCommit(fn func())
OnCommit adds a handler function to be executed after the transaction successfully commits.
func (*Tx) Page ¶
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 ¶
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(), 0666, nil) if err != nil { log.Fatal(err) } defer os.Remove(db.Path()) // Create a bucket. if err := db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }); err != nil { log.Fatal(err) } // Set a value for a key. if err := db.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")) }); err != nil { log.Fatal(err) } // Update the key but rollback the transaction so it never saves. tx, err := db.Begin(true) if err != nil { log.Fatal(err) } b := tx.Bucket([]byte("widgets")) if err := b.Put([]byte("foo"), []byte("baz")); err != nil { log.Fatal(err) } if err := tx.Rollback(); err != nil { log.Fatal(err) } // Ensure that our original value is still set. if err := db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value for 'foo' is still: %s\n", value) return nil }); err != nil { log.Fatal(err) } // Close database to release file lock. if err := db.Close(); err != nil { log.Fatal(err) }
Output: The value for 'foo' is still: bar
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 ¶ added in v1.3.7
GetCursorCount returns CursorCount atomically.
func (*TxStats) GetNodeCount ¶ added in v1.3.7
GetNodeCount returns NodeCount atomically.
func (*TxStats) GetNodeDeref ¶ added in v1.3.7
GetNodeDeref returns NodeDeref atomically.
func (*TxStats) GetPageAlloc ¶ added in v1.3.7
GetPageAlloc returns PageAlloc atomically.
func (*TxStats) GetPageCount ¶ added in v1.3.7
GetPageCount returns PageCount atomically.
func (*TxStats) GetRebalance ¶ added in v1.3.7
GetRebalance returns Rebalance atomically.
func (*TxStats) GetRebalanceTime ¶ added in v1.3.7
GetRebalanceTime returns RebalanceTime atomically.
func (*TxStats) GetSpillTime ¶ added in v1.3.7
GetSpillTime returns SpillTime atomically.
func (*TxStats) GetWriteTime ¶ added in v1.3.7
GetWriteTime returns WriteTime atomically.
func (*TxStats) IncCursorCount ¶ added in v1.3.7
IncCursorCount increases CursorCount atomically and return the new value.
func (*TxStats) IncNodeCount ¶ added in v1.3.7
IncNodeCount increases NodeCount atomically and returns the new value.
func (*TxStats) IncNodeDeref ¶ added in v1.3.7
IncNodeDeref increases NodeDeref atomically and returns the new value.
func (*TxStats) IncPageAlloc ¶ added in v1.3.7
IncPageAlloc increases PageAlloc atomically and returns the new value.
func (*TxStats) IncPageCount ¶ added in v1.3.7
IncPageCount increases PageCount atomically and returns the new value.
func (*TxStats) IncRebalance ¶ added in v1.3.7
IncRebalance increases Rebalance atomically and returns the new value.
func (*TxStats) IncRebalanceTime ¶ added in v1.3.7
IncRebalanceTime increases RebalanceTime atomically and returns the new value.
func (*TxStats) IncSpill ¶ added in v1.3.7
IncSpill increases Spill atomically and returns the new value.
func (*TxStats) IncSpillTime ¶ added in v1.3.7
IncSpillTime increases SpillTime atomically and returns the new value.
func (*TxStats) IncSplit ¶ added in v1.3.7
IncSplit increases Split atomically and returns the new value.
func (*TxStats) IncWrite ¶ added in v1.3.7
IncWrite increases Write atomically and returns the new value.
func (*TxStats) IncWriteTime ¶ added in v1.3.7
IncWriteTime increases WriteTime atomically and returns the new value.