Documentation
¶
Index ¶
- Constants
- Variables
- func ApplyProfile(opts *Options, profile Profile)
- func VacuumIndexOffline(opts Options) error
- type Batch
- type DB
- func (db *DB) AcquireSnapshot() *Snapshot
- func (db *DB) Checkpoint() error
- func (db *DB) Close() error
- func (db *DB) CompactCandidates(opts compaction.Options) error
- func (db *DB) CompactIndex() error
- func (db *DB) Delete(key []byte) error
- func (db *DB) DeleteRange(start, end []byte) error
- func (db *DB) DeleteSync(key []byte) error
- func (db *DB) DurabilityMode() string
- func (db *DB) FragmentationReport() (map[string]string, error)
- func (db *DB) Get(key []byte) ([]byte, error)
- func (db *DB) GetAppend(key, dst []byte) ([]byte, error)
- func (db *DB) GetUnsafe(key []byte) ([]byte, error)
- func (db *DB) Has(key []byte) (bool, error)
- func (db *DB) Iterator(start, end []byte) (Iterator, error)
- func (db *DB) NewBatch() Batch
- func (db *DB) NewBatchWithSize(size int) Batch
- func (db *DB) Print() error
- func (db *DB) ReverseIterator(start, end []byte) (Iterator, error)
- func (db *DB) Set(key, value []byte) error
- func (db *DB) SetSync(key, value []byte) error
- func (db *DB) Stats() map[string]string
- func (db *DB) VacuumIndexOnline(ctx context.Context) error
- type Iterator
- type Mode
- type Options
- type Profile
- type Snapshot
Examples ¶
Constants ¶
const ( // ModeCached opens TreeDB with the write-back caching layer enabled. ModeCached = db.ModeCached // ModeBackend opens TreeDB in backend-only mode (no caching layer). ModeBackend = db.ModeBackend )
Variables ¶
var ( // ErrLocked indicates the database directory is already opened by another process. ErrLocked = db.ErrLocked // ErrUnsafeOptions indicates unsafe durability/integrity options were set without acknowledgement. ErrUnsafeOptions = db.ErrUnsafeOptions // ErrMemtableFull indicates the cached memtable has reached its hard cap. ErrMemtableFull = caching.ErrMemtableFull // ErrMemtableValueLogPointers indicates memtable value-log pointers require WAL/value-log enabled. ErrMemtableValueLogPointers = caching.ErrMemtableValueLogPointers // ErrClosed indicates the DB handle has been closed. ErrClosed = errors.New("treedb: db is closed") // ErrKeyNotFound indicates the key does not exist. ErrKeyNotFound = tree.ErrKeyNotFound )
Functions ¶
func ApplyProfile ¶
ApplyProfile applies a profile to opts without overwriting explicit caller overrides.
For numeric/duration fields, "explicit override" means the field is already set to a non-zero value. For background intervals, note TreeDB conventions:
- `0` means "use default"
- `<0` means "disable"
For booleans, Go does not provide a way to distinguish “unset” from “explicit false”, so profiles set boolean policy knobs to match the profile. If you want the opposite policy, apply the profile and then override the boolean explicitly.
func VacuumIndexOffline ¶
VacuumIndexOffline rewrites `index.db` into a fresh file and swaps it in. This is intended to reclaim space and restore locality after long churn.
It is an offline operation: it acquires the exclusive open lock for opts.Dir.
Types ¶
type Batch ¶
type Batch interface {
Set(key, value []byte) error
Delete(key []byte) error
Write() error
WriteSync() error
Close() error
Replay(func(batch.Entry) error) error
GetByteSize() (int, error)
}
Batch is the public batch contract returned by TreeDB. Both cached and backend implementations satisfy it.
Example ¶
package main
import (
"fmt"
"os"
treedb "github.com/snissn/gomap/TreeDB"
)
func main() {
dir, err := os.MkdirTemp("", "treedb-batch-example-")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
db, err := treedb.Open(treedb.Options{Dir: dir})
if err != nil {
panic(err)
}
defer db.Close()
batch := db.NewBatch()
if batch == nil {
panic("batch creation failed")
}
batch.Set([]byte("key1"), []byte("value1"))
batch.Set([]byte("key2"), []byte("value2"))
batch.Delete([]byte("key1"))
// WriteSync ensures durability.
if err := batch.WriteSync(); err != nil {
panic(err)
}
val, _ := db.Get([]byte("key2"))
fmt.Println("key2:", string(val))
val, _ = db.Get([]byte("key1"))
fmt.Println("key1:", val)
}
Output: key2: value2 key1: []
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the public TreeDB handle. It can represent either cached mode (default) or backend-only mode depending on Options.
func Open ¶
Open opens TreeDB. By default it enables caching (write-back layer). To open the backend-only engine, set opts.Mode = ModeBackend.
Example ¶
package main
import (
"fmt"
"os"
treedb "github.com/snissn/gomap/TreeDB"
)
func main() {
dir, err := os.MkdirTemp("", "treedb-example-")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
db, err := treedb.Open(treedb.Options{Dir: dir, ChunkSize: 64 * 1024})
if err != nil {
panic(err)
}
defer db.Close()
if err := db.SetSync([]byte("k"), []byte("v")); err != nil {
panic(err)
}
val, err := db.Get([]byte("k"))
if err != nil {
panic(err)
}
fmt.Println(string(val))
}
Output: v
Example (BackendMode) ¶
package main
import (
"fmt"
"os"
treedb "github.com/snissn/gomap/TreeDB"
)
func main() {
dir, err := os.MkdirTemp("", "treedb-backend-example-")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
opts := treedb.Options{Dir: dir, ChunkSize: 64 * 1024, Mode: treedb.ModeBackend}
db, err := treedb.Open(opts)
if err != nil {
panic(err)
}
defer db.Close()
if err := db.SetSync([]byte("k"), []byte("v")); err != nil {
panic(err)
}
val, err := db.Get([]byte("k"))
if err != nil {
panic(err)
}
fmt.Println(string(val))
}
Output: v
func OpenBackend ¶
OpenBackend opens TreeDB in backend-only mode (no caching).
func OpenCached ¶
OpenCached is an explicit cached-mode opener (alias of Open with ModeCached).
func (*DB) AcquireSnapshot ¶
AcquireSnapshot returns a new snapshot.
func (*DB) Checkpoint ¶
Checkpoint forces a durable backend boundary and trims cached-mode WAL segments, so long-running cached-mode workloads do not accumulate unbounded `wal/` growth.
In cached mode this flushes queued memtables with backend sync and resets the WAL to a fresh segment. In backend mode it forces a sync boundary.
func (*DB) CompactCandidates ¶
func (db *DB) CompactCandidates(opts compaction.Options) error
CompactCandidates runs slab compaction based on the provided selection options. In cached mode it will also perform bounded flush assist when the caching layer is under backpressure, so compaction does not starve the foreground flush path.
func (*DB) CompactIndex ¶
CompactIndex performs an in-place index vacuum (bulk rebuild) on the backend. In cached mode it first drains the caching layer so the backend reflects all buffered writes before rebuilding.
func (*DB) DeleteRange ¶
DeleteRange removes all keys in the range [start, end).
This is primarily used by benchmark suites and maintenance tooling. In cached mode, it may use fast paths that avoid per-key tombstones when safe.
func (*DB) DeleteSync ¶
DeleteSync removes a key and forces a durability boundary.
func (*DB) DurabilityMode ¶
DurabilityMode reports the effective durability/integrity policy string.
func (*DB) FragmentationReport ¶
FragmentationReport returns best-effort structural stats about the on-disk user index that help diagnose scan regressions after churn.
Note: In cached mode this reflects the backend state only; queued memtables are not included unless the caller has explicitly drained the cache (e.g. via close+reopen or a maintenance operation that drains).
func (*DB) GetAppend ¶
GetAppend appends the value for the key to dst and returns the new slice. It avoids internal allocations by using the provided buffer. If the key is not found, it returns dst and ErrKeyNotFound.
func (*DB) GetUnsafe ¶
GetUnsafe returns the value for a key.
Semantics: Returns a safe copy of the value. For zero-copy views tied to a snapshot lifetime, use AcquireSnapshot().GetUnsafe.
func (*DB) NewBatchWithSize ¶
NewBatchWithSize creates a new batch with a hint for the expected entry size.
func (*DB) ReverseIterator ¶
ReverseIterator returns a reverse iterator over the range [start, end).
func (*DB) SetSync ¶
SetSync writes a key/value pair and forces a durability boundary. With Options.RelaxedSync enabled, Sync operations are crash-consistent only (no fsync) and may not survive power loss.
type Iterator ¶
type Iterator interface {
Valid() bool
Next()
Key() []byte
Value() []byte
KeyCopy(dst []byte) []byte
ValueCopy(dst []byte) []byte
Close() error
Error() error
}
Iterator is the public iterator contract returned by TreeDB.
Semantics (performance-first; callers must treat slices as read-only):
- Key() and Value() return views valid until the next Next()/Close().
- Use KeyCopy/ValueCopy if you need stable bytes.
Example ¶
package main
import (
"fmt"
"os"
treedb "github.com/snissn/gomap/TreeDB"
)
func main() {
dir, err := os.MkdirTemp("", "treedb-iter-example-")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
db, err := treedb.Open(treedb.Options{Dir: dir})
if err != nil {
panic(err)
}
defer db.Close()
// Insert keys in random order
db.SetSync([]byte("b"), []byte("2"))
db.SetSync([]byte("a"), []byte("1"))
db.SetSync([]byte("c"), []byte("3"))
// Iterate over all keys
it, err := db.Iterator(nil, nil)
if err != nil {
panic(err)
}
defer it.Close()
for ; it.Valid(); it.Next() {
fmt.Printf("%s: %s\n", it.Key(), it.Value())
}
}
Output: a: 1 b: 2 c: 3
type Options ¶
Options configures TreeDB. It is re-exported from TreeDB/db for convenience.
func OptionsFor ¶
OptionsFor returns a copy of Options pre-filled for the given Profile.
The returned Options still follow TreeDB's normal defaulting rules for fields left as zero values (e.g. ChunkSize, KeepRecent, backpressure thresholds).
type Profile ¶
type Profile string
Profile is a documented, high-level preset for TreeDB Options.
Why profiles exist ------------------ TreeDB exposes many low-level knobs because different workloads want different trade-offs (durability vs throughput, steady-state vs benchmark determinism, background maintenance vs predictable latency).
In practice, most callers want one of a small number of "bundles":
- "Durable": the safest defaults; favors crash-recovery and integrity.
- "Fast": higher throughput by relaxing durability/integrity knobs.
- "Bench": a deterministic variant intended for benchmarking; disables background workers that can otherwise inject "random" work (e.g. index vacuum firing mid-run).
Profiles are intentionally conservative:
- They set the *meaningful policy knobs* (durability/integrity/background), while leaving the many throughput/capacity tuning knobs at their usual defaults.
- They do not require TreeDB internals to infer "intent" from combinations of flags. You pick the intent explicitly.
How to use ----------
New DB (recommended): opts := treedb.OptionsFor(treedb.ProfileDurable, "/path/to/db") opts.FlushThreshold = 128 << 20 // optional tuning db, err := treedb.Open(opts)
Existing Options (merge without clobbering explicit overrides): opts := treedb.Options{Dir: "/path/to/db"} treedb.ApplyProfile(&opts, treedb.ProfileBench) opts.FlushThreshold = 64 << 20 // explicit overrides always win db, err := treedb.Open(opts)
Note: Profiles are a convenience API. TreeDB still honors the established "0 uses a default; <0 disables" convention for background knobs, and callers can always override any field directly after applying a profile.
const ( // ProfileDurable is the recommended default for production use when you care // about durability and corruption detection. // // It keeps WAL and checksums enabled and leaves background maintenance at // their default settings. ProfileDurable Profile = "durable" // ProfileFast prioritizes throughput by relaxing durability/integrity knobs. // // This profile is appropriate when: // - you are running on top of an external durability boundary (e.g. you // snapshot at higher layers), or // - you are exploring performance limits, and crashes/corruption detection // are acceptable trade-offs. // // Background maintenance is left enabled by default; it is generally helpful // for keeping the index compact and read-friendly. ProfileFast Profile = "fast" // ProfileBench is a "fast + deterministic" profile intended specifically for // benchmarking. // // It disables background workers that can inject heavy work mid-run (e.g. // background index vacuum). This makes comparisons more stable across runs. // // IMPORTANT: This is not a recommended production profile. ProfileBench Profile = "bench" )