Documentation
¶
Index ¶
- Constants
- Variables
- type Codec
- type Config
- type DB
- func (db *DB) BeginTransaction() error
- func (db *DB) Close() error
- func (db *DB) Commit() error
- func (db *DB) Count(prefix string) int
- func (db *DB) Delete(key string) error
- func (db *DB) Exists(key string) bool
- func (db *DB) FindKeysByPrefix(prefix string) []string
- func (db *DB) Get(key string) (any, error)
- func (db *DB) GetBlob(key string) ([]byte, error)
- func (db *DB) IsMemoryOnly() bool
- func (db *DB) Rollback() error
- func (db *DB) Save() error
- func (db *DB) Scan(prefix string, fn func(key string, value any) bool)
- func (db *DB) Set(key string, value any) error
- func (db *DB) SetEx(key string, value any, ttl time.Duration) error
- func (db *DB) SetWithBlob(key string, value any, blob []byte) error
- func (db *DB) SetWithBlobEx(key string, value any, blob []byte, ttl time.Duration) error
- func (db *DB) TTL(key string) time.Duration
- func (db *DB) UpdateBlob(key string, blob []byte) error
- type JSONCodec
- type MsgpackCodec
- type Snapshot
Constants ¶
const SnapshotVersion = 2
SnapshotVersion is the current snapshot format version
Variables ¶
var ( // ErrNotFound is returned when a key does not exist ErrNotFound = errors.New("key not found") // ErrBlobNotFound is returned when a blob file does not exist ErrBlobNotFound = errors.New("blob not found") // ErrNoBlob is returned when trying to get a blob from a key without one ErrNoBlob = errors.New("key has no associated blob") // ErrDatabaseClosed is returned when operations are called after Close() ErrDatabaseClosed = errors.New("database is closed") // ErrNotInTransaction is returned when Commit/Rollback is called outside transaction mode ErrNotInTransaction = errors.New("not in transaction mode") // ErrAlreadyInTransaction is returned when BeginTransaction is called while already in transaction mode ErrAlreadyInTransaction = errors.New("already in transaction mode") // ErrNoSnapshots is returned when no valid snapshots can be loaded ErrNoSnapshots = errors.New("no valid snapshots found") // ErrMemoryOnly is returned when trying to use blob operations in memory-only mode ErrMemoryOnly = errors.New("blob operations not supported in memory-only mode") )
var ( ErrNotInBatch = ErrNotInTransaction ErrAlreadyInBatch = ErrAlreadyInTransaction )
Backward compatibility aliases
Functions ¶
This section is empty.
Types ¶
type Codec ¶
type Codec interface {
Encode(v any) ([]byte, error)
Decode(data []byte, v any) error
Extension() string // File extension without dot, e.g., "msgpack" or "json"
}
Codec interface for pluggable serialization
type Config ¶
type Config struct {
// Path is the base directory for storage (empty = memory-only mode)
Path string
// MaxSnapshots is the maximum number of snapshot files to keep (default: 5)
MaxSnapshots int
// SaveDebounce is the debounce interval for saves (default: 10s)
SaveDebounce time.Duration
// DisableCompression disables gzip compression for snapshots (default: false, compression enabled)
DisableCompression bool
// TTLCleanupInterval is the interval for TTL cleanup (default: 5m, 0 to disable)
TTLCleanupInterval time.Duration
// Codec is the encoder/decoder (default: MsgpackCodec)
Codec Codec
}
Config holds database configuration
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the main database structure
func Open ¶
Open opens or creates a database at the given path. If path is empty, operates in memory-only mode with no persistence or blob support.
func (*DB) BeginTransaction ¶
BeginTransaction starts transaction mode (defers save until Commit).
func (*DB) FindKeysByPrefix ¶
FindKeysByPrefix returns all non-expired keys matching a prefix.
func (*DB) Get ¶
Get retrieves a value by key. Returns ErrNotFound if the key doesn't exist or has expired.
func (*DB) GetBlob ¶
GetBlob retrieves the blob data for a key. Returns ErrMemoryOnly in memory-only mode. Returns ErrNoBlob if the key has no associated blob.
func (*DB) IsMemoryOnly ¶
IsMemoryOnly returns true if the database is in memory-only mode
func (*DB) Scan ¶ added in v0.3.0
Scan iterates over all non-expired keys matching prefix, calling fn for each. Stops early if fn returns false. The lock is held for the duration of the scan so fn must not call any DB methods.
func (*DB) SetEx ¶
SetEx serialises value using the DB codec and stores it with TTL. Use ttl=0 for no expiration.
func (*DB) SetWithBlob ¶
SetWithBlob stores a value with blob data (no expiry). Returns ErrMemoryOnly in memory-only mode.
func (*DB) SetWithBlobEx ¶
SetWithBlobEx stores a value with blob data and TTL. Returns ErrMemoryOnly in memory-only mode.
type MsgpackCodec ¶
type MsgpackCodec struct{}
MsgpackCodec uses github.com/vmihailenco/msgpack/v5 for serialization
func (MsgpackCodec) Extension ¶
func (c MsgpackCodec) Extension() string