Skip to content

Instantly share code, notes, and snippets.

@vndee
Last active October 24, 2024 04:30
Show Gist options
  • Save vndee/168d55b65673250cf5b9745808a9d06d to your computer and use it in GitHub Desktop.
Save vndee/168d55b65673250cf5b9745808a9d06d to your computer and use it in GitHub Desktop.
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