MemTable¶
In-memory sorted tables — the write-side data path.
ActiveMemTable¶
ActiveMemTable()
¶
Bases: MemTable
Public interface to the live mutable memtable.
Wraps a :class:SkipList and adds:
- table_id for the audit chain
- freeze() to produce an immutable snapshot
- Metadata tracking for observability
Create a new empty active memtable with a unique table ID.
Initializes the underlying skip list and sets up metadata tracking for entry count and sequence number range.
Source code in app/memtable/active.py
size_bytes
property
¶
Estimated data size in bytes.
metadata
property
¶
Return a frozen metadata snapshot — safe to pass across threads.
put(key, seq, timestamp_ms, value)
¶
Insert or update key in the underlying SkipList.
Source code in app/memtable/active.py
get(key)
¶
items()
¶
delete(key, seq, timestamp_ms)
¶
Logically delete key. Returns whether the key existed.
Source code in app/memtable/active.py
freeze()
¶
Return a sorted snapshot of all visible entries.
The snapshot_id is assigned by the caller (MemTableManager), not by this method.
Raises :class:SnapshotEmptyError if the table has no entries.
Source code in app/memtable/active.py
ActiveMemTableMeta¶
ActiveMemTableMeta(table_id, size_bytes, entry_count, created_at, seq_first, seq_last)
dataclass
¶
Point-in-time metadata snapshot for an active memtable.
Attributes:
| Name | Type | Description |
|---|---|---|
table_id |
TableID
|
UUID hex identifying this memtable instance. |
size_bytes |
int
|
Estimated data size in bytes (sum of key + value lengths). |
entry_count |
int
|
Total number of put/delete operations applied. |
created_at |
int
|
Creation timestamp in nanoseconds since epoch. |
seq_first |
SeqNum
|
Sequence number of the first write to this table. |
seq_last |
SeqNum
|
Sequence number of the most recent write. |
ImmutableMemTable¶
ImmutableMemTable(snapshot_id, data)
¶
Read-only, sorted snapshot of an :class:ActiveMemTable.
Supports O(1) point lookups via an internal _index dict
and sorted iteration via items() for the flush path.
Create a frozen, read-only memtable snapshot.
After construction, the instance is sealed and any attribute
assignment will raise ImmutableTableAccessError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot_id
|
SnapshotID
|
Unique identifier for this snapshot, typically the
|
required |
data
|
list[tuple[Key, SeqNum, int, Value]]
|
Sorted list of |
required |
Source code in app/memtable/immutable.py
size_bytes
property
¶
Total key + value bytes in this snapshot.
seq_min
property
¶
Smallest sequence number in this snapshot.
seq_max
property
¶
Largest sequence number in this snapshot.
metadata
property
¶
Return a frozen metadata snapshot.
get(key)
¶
O(1) point lookup. Returns (seq, value) or None.
Does not return timestamp_ms — only seq is needed
for MVCC ordering.
Source code in app/memtable/immutable.py
items()
¶
Yield (key, seq, timestamp_ms, value) in sorted key order.
Used by the flush path to iterate entries for SSTable writing.
__len__()
¶
Return the number of entries in this snapshot.
Returns:
| Type | Description |
|---|---|
int
|
Total entry count including tombstones. |
__setattr__(name, value)
¶
Enforce immutability after construction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Attribute name being set. |
required |
value
|
object
|
Value to assign. |
required |
Raises:
| Type | Description |
|---|---|
ImmutableTableAccessError
|
If the instance has been sealed
(construction complete) and the attribute is not the
internal |
Source code in app/memtable/immutable.py
ImmutableMemTableMeta¶
ImmutableMemTableMeta(snapshot_id, source_table_id, size_bytes, entry_count, tombstone_count, frozen_at, seq_min, seq_max)
dataclass
¶
Metadata for a frozen memtable snapshot.
Attributes:
| Name | Type | Description |
|---|---|---|
snapshot_id |
SnapshotID
|
UUID hex identifying this snapshot. |
source_table_id |
TableID
|
Table ID of the active memtable that was frozen. |
size_bytes |
int
|
Total key + value bytes in this snapshot. |
entry_count |
int
|
Number of entries (including tombstones). |
tombstone_count |
int
|
Number of deletion tombstones. |
frozen_at |
int
|
Freeze timestamp in nanoseconds since epoch. |
seq_min |
SeqNum
|
Smallest sequence number in this snapshot. |
seq_max |
SeqNum
|
Largest sequence number in this snapshot. |
SkipList¶
SkipList()
¶
Thread-safe sorted map from :type:Key to (SeqNum, ts, Value).
Concurrent writers lock only the predecessor nodes at the insertion boundary. Readers acquire no locks — boolean flag reads are atomic under CPython's GIL.
Initialize an empty skip list with a sentinel head node.
The head node spans all levels and is never visible to iterators.
Source code in app/memtable/skiplist.py
size_bytes
property
¶
Estimated size in bytes (sum of key + value lengths).
count
property
¶
Number of visible (non-deleted) entries.
put(key, seq, timestamp_ms, value)
¶
Insert or update key in the skip list.
If the key already exists (fully_linked=True, marked=False),
updates seq, timestamp_ms, and value in place. Otherwise inserts
a new node.
Raises :class:SkipListKeyError if key is empty.
Raises :class:SkipListInsertError if retries are exhausted.
Source code in app/memtable/skiplist.py
get(key)
¶
Look up key without acquiring any lock.
Returns (seq, value) or None. Does not return
timestamp_ms — only seq is needed for MVCC ordering.
Source code in app/memtable/skiplist.py
delete(key, seq, timestamp_ms)
¶
Logically delete key by writing a TOMBSTONE.
Returns True if the key existed and was marked, False if
the key was not found (a tombstone is still inserted so the
delete propagates to SSTables).
Source code in app/memtable/skiplist.py
__iter__()
¶
Yield (key, seq, timestamp_ms, value) in sorted key order.
Lock-free. Skips nodes that are not fully_linked or are
marked. Safe to call concurrently with put() and
delete() — boolean flag reads are atomic under the GIL.
Source code in app/memtable/skiplist.py
snapshot()
¶
Materialise the iterator as a sorted list for freezing.
Used by :meth:ActiveMemTable.freeze to produce a stable
point-in-time copy.