Documentation
¶
Index ¶
- Constants
- Variables
- func Assert(condition bool, msg string, v ...any)
- type Bucket
- func (b *Bucket) Cursor() *Cursor
- func (b *Bucket) Delete(key []byte) (err error)
- func (b *Bucket) FillPercent() float64
- func (b *Bucket) ForEach(fn func(k, v []byte) error) error
- func (b *Bucket) Get(key []byte) []byte
- func (b *Bucket) Inspect() BucketStructure
- func (b *Bucket) Name() string
- func (b *Bucket) Put(key []byte, value []byte) (err error)
- func (b *Bucket) SetBranchNodeSize(maxBytes int)
- func (b *Bucket) SetFillPercent(fillPercent float64)
- func (b *Bucket) SetLeafNodeSize(maxBytes int)
- func (b *Bucket) Stats() BucketStats
- func (b *Bucket) Tx() *Tx
- func (b *Bucket) Writable() bool
- type BucketStats
- type BucketStructure
- 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) (t *Tx, err error)
- func (db *DB) Close() error
- func (db *DB) GoString() string
- func (db *DB) IsReadOnly() bool
- func (db *DB) Logger() Logger
- func (db *DB) Restore(r io.Reader) error
- func (db *DB) RestoreCheckpoint(r io.Reader, opts RestoreOptions) (stats SnapshotStats, err error)
- func (db *DB) Stats() Stats
- func (db *DB) String() string
- func (db *DB) Update(fn func(*Tx) error) error
- func (db *DB) View(fn func(*Tx) error) error
- type DefaultLogger
- func (l *DefaultLogger) Debug(v ...interface{})
- func (l *DefaultLogger) Debugf(format string, v ...interface{})
- func (l *DefaultLogger) EnableDebug()
- func (l *DefaultLogger) EnableTimestamps()
- func (l *DefaultLogger) Error(v ...interface{})
- func (l *DefaultLogger) Errorf(format string, v ...interface{})
- func (l *DefaultLogger) Fatal(v ...interface{})
- func (l *DefaultLogger) Fatalf(format string, v ...interface{})
- func (l *DefaultLogger) Info(v ...interface{})
- func (l *DefaultLogger) Infof(format string, v ...interface{})
- func (l *DefaultLogger) Panic(v ...interface{})
- func (l *DefaultLogger) Panicf(format string, v ...interface{})
- func (l *DefaultLogger) Warning(v ...interface{})
- func (l *DefaultLogger) Warningf(format string, v ...interface{})
- type KVStringer
- type Logger
- type Options
- type RestoreOptions
- type SnapshotMeta
- type SnapshotStats
- type Stats
- type Tx
- func (tx *Tx) Bucket(name []byte) *Bucket
- func (tx *Tx) Check(options ...CheckOption) <-chan error
- func (tx *Tx) Commit() (err 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) Inspect() BucketStructure
- func (tx *Tx) OnCommit(fn func())
- func (tx *Tx) Rollback() error
- func (tx *Tx) Size() int64
- func (tx *Tx) Stats() TxStats
- func (tx *Tx) Writable() bool
- func (tx *Tx) WriteCheckpoint(w io.Writer, meta SnapshotMeta) (SnapshotStats, error)
- func (tx *Tx) WriteTo(w io.Writer) (n int64, err error)
- type TxStats
- func (s *TxStats) GetCommitTime() time.Duration
- func (s *TxStats) GetCursorCount() int64
- func (s *TxStats) GetDirtyNodeCount() int64
- func (s *TxStats) GetNodeCount() int64
- func (s *TxStats) GetNodeDeref() 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) GetWriteBytes() int64
- func (s *TxStats) GetWriteTime() time.Duration
- func (s *TxStats) IncCommitTime(delta time.Duration) time.Duration
- func (s *TxStats) IncCursorCount(delta int64) int64
- func (s *TxStats) IncDirtyNodeCount(delta int64) int64
- func (s *TxStats) IncNodeCount(delta int64) int64
- func (s *TxStats) IncNodeDeref(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) IncWriteBytes(delta int64) int64
- func (s *TxStats) IncWriteTime(delta time.Duration) time.Duration
- func (s *TxStats) Sub(other *TxStats) TxStats
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 )
Default values if not set in a DB instance.
const DefaultFillPercent = 0.5
DefaultFillPercent is the percentage that split nodes are filled.
Variables ¶
var DefaultOptions = &Options{}
DefaultOptions represent the options used if nil options are passed into Open().
Functions ¶
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket is a tx-local handle to one top-level B+tree. A Bucket is never shared across transactions.
func (*Bucket) FillPercent ¶
FillPercent returns the target fill percentage used when splitting BTree nodes.
func (*Bucket) Inspect ¶
func (b *Bucket) Inspect() BucketStructure
Inspect returns the structure of the bucket.
func (*Bucket) SetBranchNodeSize ¶ added in v0.4.0
SetBranchNodeSize sets the branch-node split threshold for this bucket. Non-positive values keep the existing threshold. On writable transactions the setting is committed as bucket metadata and reused by later transactions.
func (*Bucket) SetFillPercent ¶ added in v0.4.0
SetFillPercent sets the target fill percentage used when splitting BTree nodes. Values outside the supported range are clamped.
func (*Bucket) SetLeafNodeSize ¶ added in v0.4.0
SetLeafNodeSize sets the leaf-node split threshold for this bucket. Non-positive values keep the existing threshold. On writable transactions the setting is committed as bucket metadata and reused by later transactions.
type BucketStats ¶
type BucketStats struct {
// Tree statistics.
KeyN int // number of keys/value pairs
Depth int // number of levels in B+tree
// Page size utilization.
BranchInuse int // bytes actually used for branch data
LeafInuse int // bytes actually used for leaf data
}
BucketStats records statistics about resources used by a bucket.
func (*BucketStats) Add ¶
func (s *BucketStats) Add(other BucketStats)
type BucketStructure ¶
type BucketStructure struct {
Name string `json:"name"`
KeyN int `json:"keyN"`
Children []BucketStructure `json:"buckets,omitempty"`
}
BucketStructure records the structure of a bucket.
type CheckOption ¶
type CheckOption func(options *checkConfig)
CheckOption configures Tx.Check.
func WithKVStringer ¶
func WithKVStringer(kvStringer KVStringer) CheckOption
WithKVStringer sets a human-readable renderer for keys/values in error messages.
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.
It has two modes:
- tree mode (bucket != nil): walks a single bucket's B+tree (via Bucket.Cursor());
- directory mode (bucket == nil, dir set): enumerates the top-level bucket NAMES in sorted order. Produced by Tx.Cursor(); used by snapshot/Hash/defrag-style callers that iterate all top-level buckets deterministically.
func (*Cursor) Bucket ¶
Bucket returns the bucket that this cursor was created from. Returns nil for a directory cursor (Tx.Cursor()).
func (*Cursor) First ¶
First moves the cursor to the first item in the bucket and returns its key and value.
func (*Cursor) Last ¶
Last moves the cursor to the last item in the bucket and returns its key and value.
func (*Cursor) Next ¶
Next moves the cursor to the next item in the bucket and returns its key and value.
type DB ¶
type DB struct {
// 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
// contains filtered or unexported fields
}
DB represents a collection of top-level buckets held in memory. 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 an in-memory database. If path points to a BMSP snapshot, the database is restored from it; missing and non-BMSP paths start empty. The mode argument is accepted for API compatibility. Passing in nil options uses the default vmbolt options.
func (*DB) Batch ¶
Batch calls fn as part of a batch. It behaves similar to Update, except concurrent Batch calls can be combined into a single Bolt transaction.
func (*DB) IsReadOnly ¶
func (*DB) RestoreCheckpoint ¶ added in v0.5.2
func (db *DB) RestoreCheckpoint(r io.Reader, opts RestoreOptions) (stats SnapshotStats, err error)
RestoreCheckpoint strictly validates and restores a BMSP v2 checkpoint into an open, writable, empty database. No state is published until the complete header, payload, footer, checksum, metadata, counts, and EOF are validated.
type DefaultLogger ¶
DefaultLogger is a default implementation of the Logger interface.
func (*DefaultLogger) Debug ¶
func (l *DefaultLogger) Debug(v ...interface{})
func (*DefaultLogger) Debugf ¶
func (l *DefaultLogger) Debugf(format string, v ...interface{})
func (*DefaultLogger) EnableDebug ¶
func (l *DefaultLogger) EnableDebug()
func (*DefaultLogger) EnableTimestamps ¶
func (l *DefaultLogger) EnableTimestamps()
func (*DefaultLogger) Error ¶
func (l *DefaultLogger) Error(v ...interface{})
func (*DefaultLogger) Errorf ¶
func (l *DefaultLogger) Errorf(format string, v ...interface{})
func (*DefaultLogger) Fatal ¶
func (l *DefaultLogger) Fatal(v ...interface{})
func (*DefaultLogger) Fatalf ¶
func (l *DefaultLogger) Fatalf(format string, v ...interface{})
func (*DefaultLogger) Info ¶
func (l *DefaultLogger) Info(v ...interface{})
func (*DefaultLogger) Infof ¶
func (l *DefaultLogger) Infof(format string, v ...interface{})
func (*DefaultLogger) Panic ¶
func (l *DefaultLogger) Panic(v ...interface{})
func (*DefaultLogger) Panicf ¶
func (l *DefaultLogger) Panicf(format string, v ...interface{})
func (*DefaultLogger) Warning ¶
func (l *DefaultLogger) Warning(v ...interface{})
func (*DefaultLogger) Warningf ¶
func (l *DefaultLogger) Warningf(format string, v ...interface{})
type KVStringer ¶
KVStringer renders keys/values for human-readable diagnostic messages.
func HexKVStringer ¶
func HexKVStringer() KVStringer
HexKVStringer serializes both key & value to hex representation.
type Logger ¶
type Logger interface {
Debug(v ...interface{})
Debugf(format string, v ...interface{})
Error(v ...interface{})
Errorf(format string, v ...interface{})
Info(v ...interface{})
Infof(format string, v ...interface{})
Warning(v ...interface{})
Warningf(format string, v ...interface{})
Fatal(v ...interface{})
Fatalf(format string, v ...interface{})
Panic(v ...interface{})
Panicf(format string, v ...interface{})
}
type Options ¶
type Options struct {
// ReadOnly opens the database in read-only mode.
ReadOnly bool
// Logger is the logger used for vmbolt.
Logger Logger
// NoStatistics turns off statistics collection.
NoStatistics bool
}
Options represents the options that can be set when opening a database.
type RestoreOptions ¶ added in v0.5.2
type RestoreOptions struct {
ExpectedMeta SnapshotMeta
MaxBytes int64
}
RestoreOptions configures strict checkpoint restoration. A non-zero field in ExpectedMeta must match the corresponding snapshot field. MaxBytes limits total encoded bytes and is checked before length-driven allocations.
type SnapshotMeta ¶ added in v0.5.2
SnapshotMeta binds a checkpoint to one Raft state-machine position.
type SnapshotStats ¶ added in v0.5.2
type SnapshotStats struct {
Bytes int64
BucketCount uint64
KVCount uint64
SHA256 [sha256.Size]byte
}
SnapshotStats describes the exact encoded checkpoint.
type Stats ¶
type Stats struct {
// Put `TxStats` at the first field to ensure it's 64-bit aligned.
TxStats TxStats // global, ongoing stats.
// 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 {
// contains filtered or unexported fields
}
Tx represents a read-only or read/write transaction on the database.
Every transaction pins one globally consistent dbState at Begin time. A write tx mutates tx-local buckets and publishes one new dbState atomically at commit.
func (*Tx) Check ¶
func (tx *Tx) Check(options ...CheckOption) <-chan error
Check performs structural consistency validation of the committed database snapshot pinned by this transaction (tx.base). It validates the immutable in-memory node graph directly via child pointers (no flat index).
Every detected inconsistency is streamed on the returned channel; the channel is closed when validation completes. A well-formed database yields no errors.
Concurrent calls on the same transaction are safe: the published snapshot is immutable, so each Check call walks it independently without locks.
Options: WithKVStringer customizes key/value rendering in error messages.
Scope: only the committed snapshot the transaction pinned is validated. Uncommitted, in-flight write-transaction mutations are NOT validated.
func (*Tx) CreateBucket ¶
CreateBucket creates a new top-level bucket and returns a tx-local handle to it.
func (*Tx) CreateBucketIfNotExists ¶
CreateBucketIfNotExists creates a new bucket if it doesn't already exist.
func (*Tx) Cursor ¶
Cursor returns a directory cursor over the top-level bucket names in sorted order. Tx关闭后调用会返回空Cursor,调用对应方法会Panic,和bbolt语义兼容
func (*Tx) DeleteBucket ¶
DeleteBucket deletes a top-level bucket.
func (*Tx) Inspect ¶
func (tx *Tx) Inspect() BucketStructure
Inspect returns the structure of the database: a synthetic "root" whose children are the top-level buckets.
func (*Tx) OnCommit ¶
func (tx *Tx) OnCommit(fn func())
OnCommit adds a handler function to be executed after the transaction successfully commits.
func (*Tx) WriteCheckpoint ¶ added in v0.5.2
func (tx *Tx) WriteCheckpoint(w io.Writer, meta SnapshotMeta) (SnapshotStats, error)
WriteCheckpoint writes the transaction's pinned view as BMSP v2.
type TxStats ¶
type TxStats struct {
CursorCount int64
NodeCount int64
NodeDeref int64
Rebalance int64
RebalanceTime time.Duration
Split int64
Spill int64
SpillTime time.Duration
Write int64 // number of mutating operations applied to B+tree nodes
WriteBytes int64 // logical key/value bytes passed through Put
WriteTime time.Duration
DirtyNodeCount int64 // dirty nodes observed at commit
CommitTime time.Duration
}
TxStats represents statistics about the actions performed by the transaction.