Involved Source Filesbolt_arm64.gobolt_unix.goboltsync_unix.gobucket.gocursor.godb.go
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.
errors.gofreelist.gofreelist_hmap.gonode.gopage.gotx.gounsafe.go
Package-Level Type Names (total 32, in which 11 are exported)
/* sort exporteds by: | */
Bucket represents a collection of key/value pairs inside the database.
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.
bucket*bucket
// page id of the bucket's root-level page
// monotonically incrementing, used by NextSequence()
// subbucket cache
// node cache
// inline page reference
// materialized node for the root page.
// the associated transaction
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.
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.
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.
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.
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.
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.
ForEach executes a function for each key/value pair in a bucket.
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.
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.
NextSequence returns an autoincrementing integer for the bucket.
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.
Root returns the root of the bucket.
Sequence returns the current integer for the bucket without incrementing it.
SetSequence updates the sequence number for the bucket.
Stat returns stats on a bucket.
Tx returns the tx of the bucket.
Writable returns whether the bucket is writable.
(*T) _forEachPageNode(pgid pgid, depth int, fn func(*page, *node, int))
dereference removes all references to the old mmap.
forEachPage iterates over every page in a bucket, including inline pages.
forEachPageNode iterates over every page (or node) in a bucket.
This also includes inline pages.
free recursively frees all pages in the bucket.
inlineable returns true if a bucket is small enough to be written inline
and if it contains no subbuckets. Otherwise returns false.
Returns the maximum total size of a bucket to make it a candidate for inlining.
node creates a node from a page and associates it with a given parent.
Helper method that re-interprets a sub-bucket value
from a parent into a Bucket
pageNode returns the in-memory node, if it exists.
Otherwise returns the underlying page.
rebalance attempts to balance all nodes.
spill writes all the nodes for this bucket to dirty pages.
write allocates and writes a bucket to a byte slice.
func (*Bucket).Bucket(name []byte) *Bucket
func (*Bucket).CreateBucket(key []byte) (*Bucket, error)
func (*Bucket).CreateBucketIfNotExists(key []byte) (*Bucket, error)
func (*Cursor).Bucket() *Bucket
func (*Tx).Bucket(name []byte) *Bucket
func (*Tx).CreateBucket(name []byte) (*Bucket, error)
func (*Tx).CreateBucketIfNotExists(name []byte) (*Bucket, error)
func newBucket(tx *Tx) Bucket
func (*Bucket).openBucket(value []byte) *Bucket
func (*Tx).checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, ch chan error)
BucketStats records statistics about resources used by a bucket.
Page size utilization.
// bytes allocated for physical branch pages
// bytes actually used for branch data
// number of physical branch overflow pages
Page count statistics.
// number of logical branch pages
Bucket statistics
// total number of buckets including the top bucket
// number of levels in B+tree
// bytes used for inlined buckets (also accounted for in LeafInuse)
// total number on inlined buckets
Tree statistics.
// number of keys/value pairs
// bytes allocated for physical leaf pages
// bytes actually used for leaf data
// number of physical leaf overflow pages
// number of logical leaf pages
(*T) Add(other BucketStats)
func (*Bucket).Stats() BucketStats
func (*BucketStats).Add(other BucketStats)
Cursor represents an iterator that can traverse over all key/value pairs in a bucket in sorted 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.
bucket*Bucketstack[]elemRef
Bucket returns the bucket that this cursor was created from.
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.
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.
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.
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.
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.
Seek moves the cursor to a given key 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.
first moves the cursor to the first leaf element under the last page in the stack.
keyValue returns the key and value of the current leaf element.
last moves the cursor to the last leaf element under the last page in the stack.
next moves to the next leaf element and returns the key and value.
If the cursor is at the last leaf element then it stays there and returns nil.
node returns the node that the cursor is currently positioned on.
nsearch searches the leaf node on the top of the stack for a key.
search recursively performs a binary search against a given page/node until it finds a given key.
(*T) searchNode(key []byte, n *node)(*T) searchPage(key []byte, p *page)
seek moves the cursor to a given key and returns it.
If the key does not exist then the next key is used.
func (*Bucket).Cursor() *Cursor
func (*Tx).Cursor() *Cursor
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.
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.
FreelistType sets the backend freelist type. There are two options. Array which is simple but endures
dramatic performance degradation if database is large and framentation 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
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.
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.
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.
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.
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
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.
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.
batch*batchbatchMusync.Mutexdata*[281474976710655]byte
// mmap'ed readonly, write throws SEGV
dataszintfile*os.File
// current on disk file size
freelist*freelistfreelistLoadsync.Oncemeta0*metameta1*meta
// Protects meta page access.
// Protects mmap access during remapping.
openFilefunc(string, int, os.FileMode) (*os.File, error)openedboolopsstruct{writeAt func(b []byte, off int64) (n int, err error)}pagePoolsync.PoolpageSizeintpathstring
Read only mode.
When true, Update() and Begin(true) return ErrDatabaseReadOnly immediately.
// Allows only one writer at a time.
rwtx*Tx
// Protects stats access.
statsStatstxs[]*Tx
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.
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.
Close releases all database resources.
It will block waiting for any open transactions to finish
before closing the database and returning.
GoString returns the Go string representation of the database.
This is for internal access to the raw data bytes from the C cursor, use
carefully, or not at all.
(*T) IsReadOnly() bool
Path returns the path to currently open database file.
Stats retrieves ongoing performance stats for the database.
This is only updated when a transaction closes.
String returns the string representation of the database.
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.
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.
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.
allocate returns a contiguous block of memory starting at a given page.
(*T) beginRWTx() (*Tx, error)(*T) beginTx() (*Tx, error)(*T) close() error
freePages releases any pages associated with closed read-only transactions.
(*T) freepages() []pgid
grow grows the size of the database to the given sz.
(*T) hasSyncedFreelist() bool
init creates a new database file and initializes its meta pages.
loadFreelist reads the freelist if it is synced, or reconstructs it
by scanning the DB if it is not synced. It assumes there are no
concurrent accesses being made to the freelist.
meta retrieves the current meta page reference.
mmap opens the underlying memory-mapped file and initializes the meta references.
minsz is the minimum size that the new mmap can be.
mmapSize determines the appropriate size for the mmap given the current size
of the database. The minimum size is 32KB and doubles until it reaches 1GB.
Returns an error if the new mmap size is greater than the max allowed.
munmap unmaps the data file from memory.
page retrieves a page reference from the mmap based on the current page size.
pageInBuffer retrieves a page reference from a given byte array based on the current page size.
removeTx removes a transaction from the database.
*T : fmt.GoStringer
*T : fmt.Stringer
*T : io.Closer
*T : src.elv.sh/pkg/eval/vals.Stringer
*T : context.stringer
*T : os/signal.stringer
*T : runtime.stringer
func Open(path string, mode os.FileMode, options *Options) (*DB, error)
func (*Tx).DB() *DB
func src.elv.sh/pkg/store.dbWithDefaultOptions(dbname string) (*DB, error)
func src.elv.sh/pkg/store.NewStoreFromDB(db *DB) (store.DBStore, error)
func fdatasync(db *DB) error
func flock(db *DB, exclusive bool, timeout time.Duration) error
func funlock(db *DB) error
func mmap(db *DB, sz int) error
func munmap(db *DB) error
func (*Tx).init(db *DB)
Options represents the options that can be set when opening a database.
FreelistType sets the backend freelist type. There are two options. Array which is simple but endures
dramatic performance degradation if database is large and framentation 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
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.
Sets the DB.MmapFlags flag before memory mapping the file.
Do not sync freelist to disk. This improves the database write performance
under normal operation, but requires a full database re-sync during recovery.
Sets the DB.NoGrowSync flag before memory mapping the file.
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.
OpenFile is used to open files. It defaults to os.OpenFile. This option
is useful for writing hermetic tests.
PageSize overrides the default OS page size.
Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to
grab a shared lock (UNIX).
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.
func Open(path string, mode os.FileMode, options *Options) (*DB, error)
var DefaultOptions *Options
Stats represents statistics about the database.
// total bytes allocated in free pages
Freelist stats
// total number of free pages on the freelist
// total bytes used by the freelist
// number of currently open read transactions
// total number of pending pages on the freelist
Transaction stats
// total number of started read transactions
// global, ongoing 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.
func (*DB).Stats() Stats
func (*Stats).Sub(other *Stats) Stats
func (*Stats).Sub(other *Stats) Stats
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.
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.
commitHandlers[]func()db*DBmanagedboolmeta*metapagesmap[pgid]*pagerootBucketstatsTxStatswritablebool
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.
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.
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.
Copy writes the entire database to a writer.
This function exists for backwards compatibility.
Deprecated; Use WriteTo() instead.
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.
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.
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.
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.
DB returns a reference to the database that created the transaction.
DeleteBucket deletes a bucket.
Returns an error if the bucket cannot be found or if the key represents a non-bucket value.
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.
ID returns the transaction id.
OnCommit adds a handler function to be executed after the transaction successfully commits.
Page returns page information for a given page number.
This is only safe for concurrent use when used by a writable transaction.
Rollback closes the transaction and ignores all previous updates. Read-only
transactions must be rolled back and not committed.
Size returns current database size in bytes as seen by this transaction.
Stats retrieves a copy of the current transaction statistics.
Writable returns whether the transaction can perform write operations.
WriteTo writes the entire database to a writer.
If err == nil then exactly tx.Size() bytes will be written into the writer.
allocate returns a contiguous block of memory starting at a given page.
(*T) check(ch chan error)(*T) checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, ch chan error)(*T) close()(*T) commitFreelist() error
forEachPage iterates over every page within a given page and executes a function.
init initializes the transaction.
nonPhysicalRollback is called when user calls Rollback directly, in this case we do not need to reload the free pages from disk.
page returns a reference to the page with a given id.
If page has been written to then a temporary buffered page is returned.
rollback needs to reload the free pages from disk in case some system error happens like fsync error.
write writes any dirty pages to disk.
writeMeta writes the meta to the disk.
*T : io.WriterTo
func (*Bucket).Tx() *Tx
func (*DB).Begin(writable bool) (*Tx, error)
func (*DB).beginRWTx() (*Tx, error)
func (*DB).beginTx() (*Tx, error)
func newBucket(tx *Tx) Bucket
func safelyCall(fn func(*Tx) error, tx *Tx) (err error)
func (*DB).removeTx(tx *Tx)
TxStats represents statistics about the actions performed by the transaction.
Cursor statistics.
// number of cursors created
Node statistics
// number of node allocations
// number of node dereferences
// total bytes allocated
Page statistics.
// number of page allocations
Rebalance statistics.
// number of node rebalances
// total time spent rebalancing
// number of nodes spilled
// total time spent spilling
Split/Spill statistics.
// number of nodes split
Write statistics.
// number of writes performed
// total time spent writing to disk
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.
(*T) add(other *TxStats)
func (*Tx).Stats() TxStats
func (*TxStats).Sub(other *TxStats) TxStats
func (*TxStats).Sub(other *TxStats) TxStats
func (*TxStats).add(other *TxStats)
calls[]calldb*DBstartsync.Oncetimer*time.Timer
run performs the transactions in the batch and communicates results
back to DB.Batch.
trigger runs the batch if it hasn't already been run.
branchPageElement represents a node on a branch page.
ksizeuint32pgidpgidposuint32
key returns a byte slice of the node key.
bucket represents the on-file representation of a bucket.
This is stored as the "value" of a bucket key. If the bucket is small enough,
then its root page can be stored inline in the "value", after the bucket
header. In the case of inline buckets, the "root" will be 0.
// page id of the bucket's root-level page
// monotonically incrementing, used by NextSequence()
elemRef represents a reference to an element on a given page/node.
indexintnode*nodepage*page
count returns the number of inodes or page elements.
isLeaf returns whether the ref is pointing at a leaf page/node.
freelist represents a list of all pages that are available for allocation.
It also tracks pages that have been freed but are still in use by open transactions.
// the freelist allocate func
// mapping of txid that allocated a pgid.
// key is end pgid, value is its span size
// fast lookup of all free and pending page ids.
// key is start pgid, value is its span size
// the function which gives you free page number
// freelist type
// key is the size of continuous pages(span), value is a set which contains the starting pgids of same size
// get free pgids func
// all free and available free page ids.
// the mergeSpan func
// mapping of soon-to-be free page ids by tx.
// readIDs func reads list of pages and init the freelist
(*T) addSpan(start pgid, size uint64)
arrayAllocate returns the starting page id of a contiguous list of pages of a given size.
If a contiguous block cannot be found then 0 is returned.
arrayFreeCount returns count of free pages(array version)
(*T) arrayGetFreePageIDs() []pgid
arrayMergeSpans try to merge list of pages(represented by pgids) with existing spans but using array
arrayReadIDs initializes the freelist from a given list of ids.
copyall copies a list of all free ids and all pending ids in one sorted list.
f.count returns the minimum length required for dst.
count returns count of pages on the freelist
(*T) delSpan(start pgid, size uint64)
free releases a page and its overflow for a given transaction id.
If the page is already free then a panic will occur.
freed returns whether a given page is in the free list.
hashmapAllocate serves the same purpose as arrayAllocate, but use hashmap as backend
hashmapFreeCount returns count of free pages(hashmap version)
hashmapGetFreePageIDs returns the sorted free page ids
hashmapMergeSpans try to merge list of pages(represented by pgids) with existing spans
hashmapReadIDs reads pgids as input an initial the freelist(hashmap version)
initial from pgids using when use hashmap version
pgids must be sorted
mergeWithExistingSpan merges pid to the existing free spans, try to merge it backward and forward
noSyncReload reads the freelist from pgids and filters out pending items.
pending_count returns count of pending pages
read initializes the freelist from a freelist page.
reindex rebuilds the free cache based on available and pending free lists.
release moves all page ids for a transaction id (or older) to the freelist.
releaseRange moves pending pages allocated within an extent [begin,end] to the free list.
reload reads the freelist from a page and filters out pending items.
rollback removes the pages from a given pending tx.
size returns the size of the page after serialization.
write writes the page ids onto a freelist page. All free and pending ids are
saved to disk since in the event of a program crash, all pending ids will
become free.
func newFreelist(freelistType FreelistType) *freelist
inode represents an internal node inside of a node.
It can be used to point to elements in a page or point
to an element which hasn't been added to a page yet.
flagsuint32key[]bytepgidpgidvalue[]byte
leafPageElement represents a node on a leaf page.
flagsuint32ksizeuint32posuint32vsizeuint32
key returns a byte slice of the node key.
value returns a byte slice of the node value.
node represents an in-memory, deserialized page.
bucket*BucketchildrennodesinodesinodesisLeafboolkey[]byteparent*nodepgidpgidspilledboolunbalancedbool
childAt returns the child node at a given index.
childIndex returns the index of a given child node.
del removes a key from the node.
dereference causes the node to copy all its inode key/value references to heap memory.
This is required when the mmap is reallocated so inodes are not pointing to stale data.
free adds the node's underlying page to the freelist.
minKeys returns the minimum number of inodes this node should have.
nextSibling returns the next node with the same parent.
numChildren returns the number of children.
pageElementSize returns the size of each page element based on the type of node.
prevSibling returns the previous node with the same parent.
put inserts a key/value.
read initializes the node from a page.
rebalance attempts to combine the node with sibling nodes if the node fill
size is below a threshold or if there are not enough keys.
removes a node from the list of in-memory children.
This does not affect the inodes.
root returns the top-level node this node is attached to.
size returns the size of the node after serialization.
sizeLessThan returns true if the node is less than a given size.
This is an optimization to avoid calculating a large node when we only need
to know if it fits inside a certain page size.
spill writes the nodes to dirty pages and splits nodes as it goes.
Returns an error if dirty pages cannot be allocated.
split breaks up a node into multiple smaller nodes, if appropriate.
This should only be called from the spill() function.
splitIndex finds the position where a page will fill a given threshold.
It returns the index as well as the size of the first page.
This is only be called from split().
splitTwo breaks up a node into two smaller nodes, if appropriate.
This should only be called from the split() function.
write writes the items onto one or more pages.
func (*Bucket).node(pgid pgid, parent *node) *node
func (*Bucket).pageNode(id pgid) (*page, *node)
func (*Cursor).node() *node
func (*Bucket).node(pgid pgid, parent *node) *node
func (*Cursor).searchNode(key []byte, n *node)
countuint16flagsuint16idpgidoverflowuint32
branchPageElement retrieves the branch node by index
branchPageElements retrieves a list of branch nodes.
dump writes n bytes of the page to STDERR as hex output.
leafPageElement retrieves the leaf node by index
leafPageElements retrieves a list of leaf nodes.
meta returns a pointer to the metadata section of the page.
typ returns a human readable page type string used for debugging.
func (*Bucket).pageNode(id pgid) (*page, *node)
func (*DB).allocate(txid txid, count int) (*page, error)
func (*DB).page(id pgid) *page
func (*DB).pageInBuffer(b []byte, id pgid) *page
func (*Tx).allocate(count int) (*page, error)
func (*Tx).page(id pgid) *page
func (*Cursor).searchPage(key []byte, p *page)
func (*Tx).checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, ch chan error)
txPending holds a list of pgids and corresponding allocation txns
that are pending to be freed.
// txids allocating the ids
ids[]pgid
// beginning txid of last matching releaseRange
Package-Level Functions (total 17, in which 1 are exported)
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.
_assert will panic with a given formatted message if the given condition is false.
cloneBytes returns a copy of a given slice.
fdatasync flushes written data to a file descriptor.
flock acquires an advisory lock on a file descriptor.
funlock releases an advisory lock on a file descriptor.
NOTE: This function is copied from stdlib because it is not available on darwin.
mergepgids copies the sorted union of a and b into dst.
If dst is too small, it panics.
mmap memory maps a DB's data file.
munmap unmaps a DB's data file from memory.
newBucket returns a new bucket associated with a transaction.
newFreelist returns an empty, initialized freelist.
unsafeSlice modifies the data, len, and cap of a slice variable pointed to by
the slice parameter. This helper should be used over other direct
manipulation of reflect.SliceHeader to prevent misuse, namely, converting
from reflect.SliceHeader to a Go slice type.
Package-Level Variables (total 19, in which 17 are exported)
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.
ErrBucketExists is returned when creating a bucket that already exists.
ErrBucketNameRequired is returned when creating a bucket with a blank name.
ErrBucketNotFound is returned when trying to access a bucket that has
not been created yet.
ErrChecksum is returned when either meta page checksum does not match.
ErrDatabaseNotOpen is returned when a DB instance is accessed before it
is opened or after it is closed.
ErrDatabaseOpen is returned when opening a database that is
already open.
ErrDatabaseReadOnly is returned when a mutating transaction is started on a
read-only database.
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.
ErrInvalid is returned when both meta pages on a database are invalid.
This typically occurs when a file is not a bolt database.
ErrKeyRequired is returned when inserting a zero-length key.
ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.
ErrTimeout is returned when a database cannot obtain an exclusive lock
on the data file after the timeout passed to Open().
ErrTxClosed is returned when committing or rolling back a transaction
that has already been committed or rolled back.
ErrTxNotWritable is returned when performing a write operation on a
read-only transaction.
ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
ErrVersionMismatch is returned when the data file was created with a
different version of Bolt.
default page size for db is set to the OS page size.
trySolo is a special sentinel error value used for signaling that a
transaction function should be re-run. It should never be seen by
callers.
Package-Level Constants (total 28, in which 9 are exported)
Default values if not set in a DB instance.
DefaultFillPercent is the percentage that split pages are filled.
This value can be changed by setting Bucket.FillPercent.
Default values if not set in a DB instance.
Default values if not set in a DB instance.
FreelistArrayType indicates backend freelist type is array
FreelistMapType indicates backend freelist type is hashmap
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.
MaxKeySize is the maximum length of a key, in bytes.
MaxValueSize is the maximum length of a value, in bytes.