Engine¶
The main entry point for the kiwi-db library.
LSMEngine¶
LSMEngine()
¶
Bases: StorageEngine
Central entry point for the kiwidb key-value store.
Use the :meth:open classmethod to create an instance — never
call __init__ directly.
Source code in app/engine/lsm_engine.py
data_root
property
¶
Return the root data directory path.
log_port
property
¶
Return the TCP log broadcast port, or 0 if disabled.
config
property
¶
Return the live config instance.
open(data_root='./data', config_path=None)
async
classmethod
¶
Open or create an LSM engine rooted at data_root.
Parameters¶
data_root:
Root data directory for WAL, logs, and SSTables.
config_path:
Optional override for config file location.
Defaults to <data_root>/config.json.
Source code in app/engine/lsm_engine.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
put(key, value)
async
¶
Write a key-value pair.
Atomic under write_lock: seq generation + WAL append +
memtable put + maybe_freeze.
Source code in app/engine/lsm_engine.py
delete(key)
async
¶
Delete a key by writing a tombstone.
Source code in app/engine/lsm_engine.py
flush()
async
¶
Force-flush the active memtable to an SSTable.
Freezes the current memtable regardless of size/entry thresholds
and queues it for the flush pipeline. Returns True if a
snapshot was created, False if the memtable was empty.
Source code in app/engine/lsm_engine.py
get(key)
async
¶
Read a value by key. Returns None if not found or deleted.
Checks memtable first, then SSTables.
Source code in app/engine/lsm_engine.py
update_config(key, value)
¶
Update a config field, persist to disk, return (old, new).
stats()
¶
Return a snapshot of engine statistics.
Source code in app/engine/lsm_engine.py
show_mem(table_id=None)
¶
Show memtable contents.
Parameters¶
table_id:
Optional. Pass a table ID (for the active memtable) or
a snapshot ID (for an immutable memtable) to see its full
entry list. When omitted or None, returns a summary
listing all active and immutable memtables without entries.
Returns¶
dict In listing mode (no table_id)::
{
"active": {"table_id": ..., "entry_count": ..., ...},
"immutable": [{"snapshot_id": ..., ...}, ...],
}
In detail mode (with *table_id*)::
{
"type": "active" | "immutable",
"table_id": ...,
"entries": [{"key": ..., "seq": ..., ...}, ...],
}
Source code in app/engine/lsm_engine.py
show_disk(file_id=None)
¶
Show SSTable contents on disk.
Parameters¶
file_id:
Optional. Pass a file ID to see the full record list
for that SSTable. When omitted or None, returns a
summary listing all SSTables organised by level.
Returns¶
dict In listing mode (no file_id)::
{"L0": [{"file_id": ..., "record_count": ..., ...}, ...]}
In detail mode (with *file_id*)::
{"file_id": ..., "entries": [...], ...}
Source code in app/engine/lsm_engine.py
close()
async
¶
Flush and close the engine. Idempotent.
Source code in app/engine/lsm_engine.py
EngineStats¶
EngineStats(key_count, seq, wal_entry_count, data_root, active_table_id, active_size_bytes, immutable_queue_len, immutable_snapshots, l0_sstable_count)
dataclass
¶
Snapshot of engine statistics.
Attributes:
| Name | Type | Description |
|---|---|---|
key_count |
int
|
Number of keys in the active memtable. |
seq |
SeqNum
|
Current sequence number (highest generated so far). |
wal_entry_count |
int
|
Number of entries in the write-ahead log. |
data_root |
str
|
Filesystem path to the engine's data directory. |
active_table_id |
str
|
UUID hex of the current active memtable. |
active_size_bytes |
int
|
Estimated byte size of the active memtable. |
immutable_queue_len |
int
|
Number of frozen snapshots awaiting flush. |
immutable_snapshots |
list[str]
|
Snapshot IDs of queued immutable memtables. |
l0_sstable_count |
int
|
Number of Level-0 SSTables on disk. |