LSM & Durability
StorageMode.Lsm is DataVo's durable storage engine, built on a log-structured merge tree with a single directory-wide commit log (commit.wal), arena-backed MemTables, and memory-mapped immutable SSTables.
using DataVo.Core;
using DataVo.Core.StorageEngine.Config;
using var db = new DataVoContext(new DataVoConfig
{
StorageMode = StorageMode.Lsm,
DiskStoragePath = "./datavo_lsm_data",
LsmStrictFsync = true, // default: wait for stable storage on commit
ClusteredPrimaryKeys = true // default: store single INT/GUID PK tables clustered
});
db.Execute("CREATE DATABASE App; USE App;");
db.Execute("CREATE TABLE players (id INT PRIMARY KEY, name VARCHAR, score INT)");
db.Execute("INSERT INTO players (id, name, score) VALUES (1, 'Ada', 250)");Write Path and SSTables
Every statement (or explicit COMMIT) appends one checksummed record to commit.wal, then applies mutations to each table's arena-backed active MemTable. When a MemTable reaches 32 MB, DataVo freezes it, swaps in a fresh MemTable, and flushes the frozen generation on a background thread into an immutable .sst file.
Each SSTable is written sequentially to a temporary file, fsynced, atomically renamed, and published in one manifest edit before older commit.wal records are pruned:
- 4 KiB data blocks + Bloom filters: Sorted entries with a sparse block index and per-file blocked Bloom filter let point lookups skip absent files and touch at most one block on a hit.
- Memory-mapped reads (
LsmReadMode):LsmReadMode.Auto(default) memory-maps.sstfiles on 64-bit platforms so reads hit the OS page cache with no syscall per lookup. UseLsmReadMode.PositionalReadto force standard file reads. - Out-of-core columnar segments (v8): Tables with a
USING COLUMNAR([Columnar]) index store 4,096-row columnar chunks, NULL/NaN masks, tombstone runs, and per-chunk min/max zone maps inside each.sstfile.
Durability Settings & IoSchedulerMode
| Setting | Default | Behavior | When to Use |
|---|---|---|---|
LsmStrictFsync = true | Yes | Forces commit.wal to stable storage before acknowledging writes; concurrent commits share fsyncs | Production data that must survive power loss or OS crash |
LsmStrictFsync = false | No | Acknowledges after OS-buffered WAL append; recent writes can be lost on power/kernel failure | Caches, rebuildable data, and throughput ceiling benchmarks |
ClusteredPrimaryKeys | true | Stores single INT or GUID PK tables directly under their key (no separate PK index) | Keep true for fast PK seeks and range scans (set at CREATE TABLE) |
IoSchedulerMode | Off | Controls legacy Disk I/O scheduling (Off, PoolingOnly, or reserved GroupCommit) | Only relevant for legacy StorageMode.Disk (Lsm uses its own commit log) |
When reporting or comparing write throughput, always pair the number with its durability setting (for example,
LSM Strictvs.LSM Relaxed).
Crash Atomicity and Visibility
In StorageMode.Lsm, every auto-commit statement and every explicit transaction COMMIT is written to commit.wal as a single checksummed record:
- All-or-nothing recovery: On startup after a crash, recovery replays each complete record atomically and ignores any truncated tail. Multi-row
UPDATEs and multi-table transactions are never half-applied. - Durable visibility: Row changes, secondary index updates, and reactive notifications become visible to other sessions only after the commit record is durable. Write locks are held until publish, and any failed statement writes no record and leaves no partial state.
Reopening a Directory
Closing and reopening a DataVoContext on the same DiskStoragePath restores the catalog (Catalog.xml), SSTables, commit log, and transaction-ID watermark:
var config = new DataVoConfig
{
StorageMode = StorageMode.Lsm,
DiskStoragePath = "./datavo_lsm_data"
};
using (var db = new DataVoContext(config))
{
db.Execute("CREATE DATABASE App; USE App;");
db.Execute("CREATE TABLE players (id INT PRIMARY KEY, name VARCHAR)");
db.Execute("INSERT INTO players VALUES (1, 'Ada')");
}
using var reopened = new DataVoContext(config);
reopened.Execute("USE App");
var row = reopened.Execute("SELECT name FROM players WHERE id = 1")[0].Data[0]; // "Ada"Index Recovery on Open
| Index Type | Behavior on Reopen |
|---|---|
Clustered PK (INT/GUID) | Stored directly as the SSTable row key; zero rebuild |
| B-tree secondary indexes | Stored as keyed SSTables under .idx and updated in the same commit record; zero rebuild |
USING COLUMNAR | Stored inside .sst (v8 segments); only the unflushed tail stays in memory; zero rebuild |
USING HNSW | Loads snapshot written on clean close and catches up from commit.wal; rebuilds if missing, corrupt, or >50% changed |
USING FLAT | Rebuilt sequentially from table rows on open |
Like Disk mode, an LSM directory is owned by one process at a time via datavo.lock, while multiple contexts inside the same process share one engine (requiring identical settings). Note that prior to v1.0, the on-disk LSM format does not guarantee cross-version binary compatibility.
See Also
- Storage Modes: Compare
InMemory,Lsm,Disk,Wasm, andCustom - Transactions & MVCC: Snapshot isolation and atomic multi-statement commits
- Configuration Reference: Full
DataVoConfigreference