zapdb
v1.21.5
Documentation
pkg.go.dev ↗Constants
Name
const Name = "zapdb"
Types
Config
type Config struct {
// SyncWrites is a POINTER so the config can express three states: absent
// (nil, keep the built-in default), explicitly true, and explicitly false.
// As a plain bool the zero value was indistinguishable from "unset", so
// applyConfig could only ever turn syncing ON and `"syncWrites": false`
// was silently ignored.
SyncWrites *bool `json:"syncWrites"`
NumCompactors int `json:"numCompactors"`
NumMemtables int `json:"numMemtables"`
MemTableSize int64 `json:"memTableSize"`
ValueLogFileSize int64 `json:"valueLogFileSize"`
ValueLogMaxEntries uint32 `json:"valueLogMaxEntries"`
BlockCacheSize int64 `json:"blockCacheSize"`
IndexCacheSize int64 `json:"indexCacheSize"`
BloomFalsePositive float64 `json:"bloomFalsePositive"`
ValueThreshold int `json:"valueThreshold"`
Compression string `json:"compression"`
ZSTDCompressionLevel int `json:"zstdCompressionLevel"`
}
Config represents BadgerDB configuration
Database
type Database struct {
// contains filtered or unexported fields
}
Database is a badgerdb backed database
New
func New(file string, configBytes []byte, namespace string, metrics metric.Registerer) (*Database, error)
New returns a new badgerdb-backed database
Database.Backup
func (d *Database) Backup(w io.Writer, since uint64) (uint64, error)
Backup implements the database.Database interface
Database.Close
func (d *Database) Close() error
Close implements the Database interface
Database.Compact
func (d *Database) Compact(start []byte, limit []byte) error
Compact implements the Database interface
Database.Delete
func (d *Database) Delete(key []byte) error
Delete implements the Database interface
Database.DropPrefix
func (d *Database) DropPrefix(prefixes ...[]byte) error
DropPrefix implements database.PrefixDropper.
Database.Empty
func (d *Database) Empty() (bool, error)
Empty returns true if the database doesn't contain any keys (but not deleted keys)
Database.Get
func (d *Database) Get(key []byte) ([]byte, error)
Get implements the Database interface
Database.GetSequence
func (d *Database) GetSequence(key []byte, bandwidth uint64) (database.Sequence, error)
GetSequence implements database.Sequencer.
The returned *zapdb.Sequence already has Next() (uint64, error) and Release() error, so it satisfies database.Sequence as-is.
Database.GetSnapshot
func (d *Database) GetSnapshot() (database.Database, error)
GetSnapshot implements the database.Database interface
Database.Has
func (d *Database) Has(key []byte) (bool, error)
Has implements the Database interface
Database.HealthCheck
func (d *Database) HealthCheck(ctx context.Context) (interface{}, error)
HealthCheck returns nil if the database is healthy, non-nil otherwise.
Database.Len
func (d *Database) Len() (int, error)
Len returns the number of keys in the database
Database.Load
func (d *Database) Load(r io.Reader) error
Load implements the database.Database interface
Database.NewBatch
func (d *Database) NewBatch() database.Batch
NewBatch implements the Database interface
Database.NewIterator
func (d *Database) NewIterator() database.Iterator
NewIterator implements the Database interface
Database.NewIteratorWithPrefix
func (d *Database) NewIteratorWithPrefix(prefix []byte) database.Iterator
NewIteratorWithPrefix implements the Database interface
Database.NewIteratorWithStart
func (d *Database) NewIteratorWithStart(start []byte) database.Iterator
NewIteratorWithStart implements the Database interface
Database.NewIteratorWithStartAndPrefix
func (d *Database) NewIteratorWithStartAndPrefix(start, prefix []byte) database.Iterator
NewIteratorWithStartAndPrefix implements the Database interface
Database.Put
func (d *Database) Put(key []byte, value []byte) error
Put implements the Database interface
Database.PutWithTTL
func (d *Database) PutWithTTL(key, value []byte, ttl time.Duration) error
PutWithTTL implements database.TTLWriter outside of an explicit transaction. It opens a write txn, sets the entry with the TTL, and commits.
Database.StartReplicator
func (d *Database) StartReplicator(ctx context.Context) error
StartReplicator starts encrypted streaming replication to S3 if configured. Reads config from REPLICATE_* env vars. No-op if REPLICATE_S3_ENDPOINT is not set. Implements database.Replicatable.
On boot, if the database is empty and a snapshot exists in S3, it is restored (latest full snapshot + newer incrementals) synchronously before this returns, so a fresh node comes up on the latest replicated state instead of importing or replaying per-node. The continuous backup loop (full snapshot hourly, incremental every second) then runs in the background for the life of ctx. This call does NOT block. Set REPLICATE_RESTORE_ON_BOOT=false to skip restore (backup-only).
Database.Sync
func (d *Database) Sync() error
Sync implements the database.Syncer interface It flushes all buffered writes to persistent storage.
Database.Update
func (d *Database) Update(fn func(txn database.Txn) error) error
Update runs fn in a writable badger transaction. On non-nil error from fn the transaction is rolled back; on nil the transaction is committed. badger handles conflict retries internally via View/Update — we surface any commit error to the caller.
Database.View
func (d *Database) View(fn func(txn database.Txn) error) error
View runs fn in a read-only badger transaction.
GPUCacheConfig
type GPUCacheConfig struct {
// GPUMemoryBudget is the maximum GPU memory to use for caching in bytes.
// Set to 0 to disable GPU caching (default).
// Typical values: 256MB for light use, 1-4GB for consensus/DEX workloads.
GPUMemoryBudget uint64 `json:"gpuMemoryBudget,omitempty"`
// Backend selects the GPU backend: "auto", "metal", "cuda", "webgpu", "cpu".
// Default: "auto" (best available).
Backend string `json:"gpuBackend,omitempty"`
// DeviceIndex selects which GPU device to use (-1 for default).
DeviceIndex int `json:"gpuDeviceIndex,omitempty"`
// InitialCapacity is the initial number of hash table slots (power of 2).
// Default: 1048576 (1M slots). Each slot is 64 bytes.
InitialCapacity uint32 `json:"gpuInitialCapacity,omitempty"`
// EvictionThreshold triggers cache eviction when load factor exceeds this.
// Default: 0.90. Range: 0.5 to 0.95.
EvictionThreshold float32 `json:"gpuEvictionThreshold,omitempty"`
// PromoteOnMiss controls whether disk reads are promoted into GPU cache.
// Default: true. Set to false for write-heavy workloads where reads are rare.
PromoteOnMiss bool `json:"gpuPromoteOnMiss,omitempty"`
// WriteThrough ensures every Put is immediately persisted to disk.
// Default: true. Set to false for higher write throughput at risk of
// data loss on crash (dirty entries are flushed periodically).
WriteThrough bool `json:"gpuWriteThrough,omitempty"`
// MaxKeySize is the maximum key size in bytes. Default: 1024.
MaxKeySize uint32 `json:"gpuMaxKeySize,omitempty"`
// MaxValueSize is the maximum value size in bytes. Default: 65536.
MaxValueSize uint32 `json:"gpuMaxValueSize,omitempty"`
}
GPUCacheConfig configures opt-in GPU-accelerated caching for ZapDB. When GPUMemoryBudget > 0, the database keeps hot key-value pairs in GPU memory for fast access. Reads check GPU first, writes go through to disk.
Set GPUMemoryBudget to 0 (default) to disable GPU caching entirely.
DefaultGPUCacheConfig
func DefaultGPUCacheConfig() GPUCacheConfig
DefaultGPUCacheConfig returns a GPUCacheConfig with GPU caching disabled. To enable, set GPUMemoryBudget > 0.
ParseGPUConfig
func ParseGPUConfig(configBytes []byte) GPUCacheConfig
ParseGPUConfig extracts GPU cache configuration from the database config JSON bytes. If the config contains gpuMemoryBudget > 0, GPU caching is enabled. This allows GPU mode to be activated via the standard database config mechanism (--db-config-content or --db-config-file).
Example config JSON:
{
"syncWrites": false,
"gpuMemoryBudget": 536870912,
"gpuBackend": "auto",
"gpuPromoteOnMiss": true
}
GPUCacheConfig.Enabled
func (c *GPUCacheConfig) Enabled() bool
Enabled returns true if GPU caching is configured.