Last active
October 24, 2024 04:30
-
-
Save vndee/168d55b65673250cf5b9745808a9d06d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set(self, key: str, value: Any): | |
"""Set a key-value pair""" | |
with self.lock: | |
if not isinstance(key, str): | |
raise ValueError("Key must be a string") | |
# 1. Safety first: Write to WAL | |
self.wal.set(key, value) | |
# 2. Write to memory table (fast!) | |
self.memtable.add(key, value) | |
# 3. If memory table is full, save to disk | |
if self.memtable.is_full(): | |
self._flush_memtable() | |
def _flush_memtable(self): | |
"""Flush memtable to disk as new SSTable""" | |
if not self.memtable.entries: | |
return # Skip if empty | |
# Create new SSTable with a unique name | |
sstable = SSTable(str(self.base_path / f"sstable_{len(self.sstables)}.db")) | |
sstable.write_memtable(self.memtable) | |
# Add to our list of SSTables | |
self.sstables.append(sstable) | |
# Create fresh memory table | |
self.memtable = MemTable() | |
# Create a checkpoint in WAL | |
self.wal.checkpoint() | |
# Compact if we have too many SSTables | |
if len(self.sstables) > self.max_sstables: | |
self._compact() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment