Documentation
¶
Overview ¶
Package badgerkv provides a standardized key-value store interface built on top of BadgerDB. It implements the github.com/bborbe/kv interface, offering a clean and consistent API for database operations including transactions, buckets, and key-value operations.
Basic usage:
// Open a file-based database
db, err := badgerkv.OpenPath(ctx, "/path/to/db")
if err != nil {
return err
}
defer db.Close()
// Perform operations within a transaction
err = db.Update(ctx, func(ctx context.Context, tx badgerkv.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte("users"))
if err != nil {
return err
}
return bucket.Put([]byte("user:1"), []byte(`{"name": "John"}`))
})
The package supports both file-based and in-memory databases, with full transaction support and bucket-based data organization.
Index ¶
- func BucketAddKey(bucket libkv.BucketName, key []byte) []byte
- func BucketRemoveKey(bucket libkv.BucketName, key []byte) []byte
- func BucketToPrefix(bucket libkv.BucketName) []byte
- func IsTransactionOpen(ctx context.Context) bool
- func MinMemoryUsageOptions(opts *badger.Options)
- func SetOpenState(ctx context.Context) context.Context
- type Bucket
- type ChangeOptions
- type DB
- type Item
- type Iterator
- type Tx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BucketAddKey ¶
func BucketAddKey(bucket libkv.BucketName, key []byte) []byte
func BucketRemoveKey ¶
func BucketRemoveKey(bucket libkv.BucketName, key []byte) []byte
func BucketToPrefix ¶
func BucketToPrefix(bucket libkv.BucketName) []byte
func IsTransactionOpen ¶
IsTransactionOpen checks if a transaction is currently active in the given context. BadgerKV prevents nested transactions, so this function can be used to verify transaction state before attempting database operations.
func MinMemoryUsageOptions ¶
MinMemoryUsageOptions configures BadgerDB for minimal memory usage. This is useful for resource-constrained environments or when running multiple instances.
Types ¶
type ChangeOptions ¶
type DB ¶
func OpenMemory ¶
func OpenMemory(ctx context.Context, fn ...ChangeOptions) (DB, error)
OpenMemory opens an in-memory BadgerDB database. This is useful for testing or temporary data storage. Optional ChangeOptions functions can be provided to customize BadgerDB options.
Example:
db, err := badgerkv.OpenMemory(ctx)
type Iterator ¶
type Iterator interface {
libkv.Iterator
BucketName() libkv.BucketName
Iterator() *badger.Iterator
}
func NewIterator ¶
func NewIterator( badgerTx *badger.Txn, bucketName libkv.BucketName, ) Iterator
func NewIteratorReverse ¶
func NewIteratorReverse( badgerTx *badger.Txn, bucketName libkv.BucketName, ) Iterator