@@ -275,6 +275,28 @@ class _CCacheStats(Structure):
275275 ]
276276
277277
278+ class _CDbStats (Structure ):
279+ """C structure for tidesdb_db_stats_t."""
280+
281+ _fields_ = [
282+ ("num_column_families" , c_int ),
283+ ("total_memory" , c_uint64 ),
284+ ("available_memory" , c_uint64 ),
285+ ("resolved_memory_limit" , c_size_t ),
286+ ("memory_pressure_level" , c_int ),
287+ ("flush_pending_count" , c_int ),
288+ ("total_memtable_bytes" , c_int64 ),
289+ ("total_immutable_count" , c_int ),
290+ ("total_sstable_count" , c_int ),
291+ ("total_data_size_bytes" , c_uint64 ),
292+ ("num_open_sstables" , c_int ),
293+ ("global_seq" , c_uint64 ),
294+ ("txn_memory_bytes" , c_int64 ),
295+ ("compaction_queue_size" , c_size_t ),
296+ ("flush_queue_size" , c_size_t ),
297+ ]
298+
299+
278300_lib .tidesdb_default_column_family_config .argtypes = []
279301_lib .tidesdb_default_column_family_config .restype = _CColumnFamilyConfig
280302
@@ -451,6 +473,18 @@ class _CCacheStats(Structure):
451473_lib .tidesdb_delete_column_family .argtypes = [c_void_p , c_void_p ]
452474_lib .tidesdb_delete_column_family .restype = c_int
453475
476+ _lib .tidesdb_purge_cf .argtypes = [c_void_p ]
477+ _lib .tidesdb_purge_cf .restype = c_int
478+
479+ _lib .tidesdb_purge .argtypes = [c_void_p ]
480+ _lib .tidesdb_purge .restype = c_int
481+
482+ _lib .tidesdb_sync_wal .argtypes = [c_void_p ]
483+ _lib .tidesdb_sync_wal .restype = c_int
484+
485+ _lib .tidesdb_get_db_stats .argtypes = [c_void_p , POINTER (_CDbStats )]
486+ _lib .tidesdb_get_db_stats .restype = c_int
487+
454488
455489@dataclass
456490class Config :
@@ -565,6 +599,27 @@ class CacheStats:
565599 num_partitions : int
566600
567601
602+ @dataclass
603+ class DbStats :
604+ """Database-level aggregate statistics."""
605+
606+ num_column_families : int
607+ total_memory : int
608+ available_memory : int
609+ resolved_memory_limit : int
610+ memory_pressure_level : int
611+ flush_pending_count : int
612+ total_memtable_bytes : int
613+ total_immutable_count : int
614+ total_sstable_count : int
615+ total_data_size_bytes : int
616+ num_open_sstables : int
617+ global_seq : int
618+ txn_memory_bytes : int
619+ compaction_queue_size : int
620+ flush_queue_size : int
621+
622+
568623@dataclass
569624class CommitOp :
570625 """A single operation from a committed transaction batch."""
@@ -816,6 +871,48 @@ def is_compacting(self) -> bool:
816871 """Check if a compaction operation is in progress for this column family."""
817872 return bool (_lib .tidesdb_is_compacting (self ._cf ))
818873
874+ def purge (self ) -> None :
875+ """
876+ Force a synchronous flush and aggressive compaction for this column family.
877+
878+ Unlike flush_memtable() and compact() (which are non-blocking), purge blocks
879+ until all flush and compaction I/O is complete.
880+
881+ Behavior:
882+ 1. Waits for any in-progress flush to complete
883+ 2. Force-flushes the active memtable (even if below threshold)
884+ 3. Waits for flush I/O to fully complete
885+ 4. Waits for any in-progress compaction to complete
886+ 5. Triggers synchronous compaction inline (bypasses the compaction queue)
887+ 6. Waits for any queued compaction to drain
888+
889+ Use cases:
890+ - Before backup or checkpoint: ensure all data is on disk and compacted
891+ - After bulk deletes: reclaim space immediately by compacting away tombstones
892+ - Manual maintenance: force a clean state during a maintenance window
893+ - Pre-shutdown: ensure all pending work is complete before closing
894+ """
895+ result = _lib .tidesdb_purge_cf (self ._cf )
896+ if result != TDB_SUCCESS :
897+ raise TidesDBError .from_code (result , "failed to purge column family" )
898+
899+ def sync_wal (self ) -> None :
900+ """
901+ Force an immediate fsync of the active write-ahead log for this column family.
902+
903+ This is useful for explicit durability control when using SYNC_NONE or
904+ SYNC_INTERVAL modes.
905+
906+ Use cases:
907+ - Application-controlled durability: sync the WAL at specific points
908+ - Pre-checkpoint: ensure all buffered WAL data is on disk
909+ - Graceful shutdown: flush WAL buffers before closing
910+ - Critical writes: force durability for specific high-value writes
911+ """
912+ result = _lib .tidesdb_sync_wal (self ._cf )
913+ if result != TDB_SUCCESS :
914+ raise TidesDBError .from_code (result , "failed to sync WAL" )
915+
819916 def update_runtime_config (self , config : ColumnFamilyConfig , persist_to_disk : bool = True ) -> None :
820917 """
821918 Update runtime-safe configuration settings for this column family.
@@ -1656,6 +1753,62 @@ def delete_column_family(self, cf: ColumnFamily) -> None:
16561753 if result != TDB_SUCCESS :
16571754 raise TidesDBError .from_code (result , "failed to delete column family" )
16581755
1756+ def purge (self ) -> None :
1757+ """
1758+ Force a synchronous flush and aggressive compaction for all column families,
1759+ then drain both the global flush and compaction queues.
1760+
1761+ Unlike flush_memtable() and compact() (which are non-blocking), purge blocks
1762+ until all work is complete across all column families.
1763+
1764+ Behavior:
1765+ 1. Calls purge on each column family
1766+ 2. Drains the global flush queue
1767+ 3. Drains the global compaction queue
1768+
1769+ Raises:
1770+ TidesDBError: If purge fails for any column family
1771+ """
1772+ if self ._closed :
1773+ raise TidesDBError ("Database is closed" )
1774+
1775+ result = _lib .tidesdb_purge (self ._db )
1776+ if result != TDB_SUCCESS :
1777+ raise TidesDBError .from_code (result , "failed to purge database" )
1778+
1779+ def get_db_stats (self ) -> DbStats :
1780+ """
1781+ Get aggregate statistics across the entire database instance.
1782+
1783+ Returns:
1784+ DbStats instance with database-level statistics
1785+ """
1786+ if self ._closed :
1787+ raise TidesDBError ("Database is closed" )
1788+
1789+ c_stats = _CDbStats ()
1790+ result = _lib .tidesdb_get_db_stats (self ._db , ctypes .byref (c_stats ))
1791+ if result != TDB_SUCCESS :
1792+ raise TidesDBError .from_code (result , "failed to get database stats" )
1793+
1794+ return DbStats (
1795+ num_column_families = c_stats .num_column_families ,
1796+ total_memory = c_stats .total_memory ,
1797+ available_memory = c_stats .available_memory ,
1798+ resolved_memory_limit = c_stats .resolved_memory_limit ,
1799+ memory_pressure_level = c_stats .memory_pressure_level ,
1800+ flush_pending_count = c_stats .flush_pending_count ,
1801+ total_memtable_bytes = c_stats .total_memtable_bytes ,
1802+ total_immutable_count = c_stats .total_immutable_count ,
1803+ total_sstable_count = c_stats .total_sstable_count ,
1804+ total_data_size_bytes = c_stats .total_data_size_bytes ,
1805+ num_open_sstables = c_stats .num_open_sstables ,
1806+ global_seq = c_stats .global_seq ,
1807+ txn_memory_bytes = c_stats .txn_memory_bytes ,
1808+ compaction_queue_size = c_stats .compaction_queue_size ,
1809+ flush_queue_size = c_stats .flush_queue_size ,
1810+ )
1811+
16591812 def __enter__ (self ) -> TidesDB :
16601813 return self
16611814
0 commit comments