Skip to content

Commit 4a6cf2b

Browse files
authored
Merge pull request #36 from tidesdb/tdb8
align python ffi library with tidesdb 9 fully
2 parents 5e5a79d + 5240d91 commit 4a6cf2b

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "tidesdb"
7-
version = "0.7.0"
7+
version = "0.8.0"
88
description = "Official Python bindings for TidesDB - A high-performance embedded key-value storage engine"
99
readme = "README.md"
1010
requires-python = ">=3.10"

src/tidesdb/tidesdb.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
c_int,
3333
c_size_t,
3434
c_uint8,
35+
c_uint32,
3536
c_uint64,
3637
c_void_p,
3738
)
@@ -194,6 +195,7 @@ class _CColumnFamilyConfig(Structure):
194195
("min_disk_space", c_uint64),
195196
("l1_file_count_trigger", c_int),
196197
("l0_queue_stall_threshold", c_int),
198+
("use_btree", c_int),
197199
]
198200

199201

@@ -228,6 +230,10 @@ class _CStats(Structure):
228230
("level_key_counts", POINTER(c_uint64)),
229231
("read_amp", c_double),
230232
("hit_rate", c_double),
233+
("use_btree", c_int),
234+
("btree_total_nodes", c_uint64),
235+
("btree_max_height", c_uint32),
236+
("btree_avg_height", c_double),
231237
]
232238

233239

@@ -429,6 +435,7 @@ class ColumnFamilyConfig:
429435
min_disk_space: int = 100 * 1024 * 1024
430436
l1_file_count_trigger: int = 4
431437
l0_queue_stall_threshold: int = 20
438+
use_btree: bool = False
432439

433440
def _to_c_struct(self) -> _CColumnFamilyConfig:
434441
"""Convert to C structure."""
@@ -452,6 +459,7 @@ def _to_c_struct(self) -> _CColumnFamilyConfig:
452459
c_config.min_disk_space = self.min_disk_space
453460
c_config.l1_file_count_trigger = self.l1_file_count_trigger
454461
c_config.l0_queue_stall_threshold = self.l0_queue_stall_threshold
462+
c_config.use_btree = 1 if self.use_btree else 0
455463

456464
name_bytes = self.comparator_name.encode("utf-8")[:TDB_MAX_COMPARATOR_NAME - 1]
457465
name_bytes = name_bytes + b"\x00" * (TDB_MAX_COMPARATOR_NAME - len(name_bytes))
@@ -475,6 +483,10 @@ class Stats:
475483
level_key_counts: list[int]
476484
read_amp: float
477485
hit_rate: float
486+
use_btree: bool = False
487+
btree_total_nodes: int = 0
488+
btree_max_height: int = 0
489+
btree_avg_height: float = 0.0
478490
config: ColumnFamilyConfig | None = None
479491

480492

@@ -520,6 +532,7 @@ def default_column_family_config() -> ColumnFamilyConfig:
520532
min_disk_space=c_config.min_disk_space,
521533
l1_file_count_trigger=c_config.l1_file_count_trigger,
522534
l0_queue_stall_threshold=c_config.l0_queue_stall_threshold,
535+
use_btree=bool(c_config.use_btree),
523536
)
524537

525538

@@ -760,6 +773,7 @@ def get_stats(self) -> Stats:
760773
min_disk_space=c_cfg.min_disk_space,
761774
l1_file_count_trigger=c_cfg.l1_file_count_trigger,
762775
l0_queue_stall_threshold=c_cfg.l0_queue_stall_threshold,
776+
use_btree=bool(c_cfg.use_btree),
763777
)
764778

765779
stats = Stats(
@@ -774,6 +788,10 @@ def get_stats(self) -> Stats:
774788
level_key_counts=level_key_counts,
775789
read_amp=c_stats.read_amp,
776790
hit_rate=c_stats.hit_rate,
791+
use_btree=bool(c_stats.use_btree),
792+
btree_total_nodes=c_stats.btree_total_nodes,
793+
btree_max_height=c_stats.btree_max_height,
794+
btree_avg_height=c_stats.btree_avg_height,
777795
config=config,
778796
)
779797

tests/test_tidesdb.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ def test_create_with_config(self, db):
100100

101101
db.drop_column_family("custom_cf")
102102

103+
def test_create_with_btree_config(self, db):
104+
"""Test creating column family with B+tree format enabled."""
105+
config = tidesdb.default_column_family_config()
106+
config.use_btree = True
107+
108+
db.create_column_family("btree_cf", config)
109+
cf = db.get_column_family("btree_cf")
110+
assert cf is not None
111+
112+
stats = cf.get_stats()
113+
assert stats.config is not None
114+
assert stats.config.use_btree is True
115+
assert stats.use_btree is True
116+
117+
db.drop_column_family("btree_cf")
118+
119+
def test_default_config_use_btree(self, db):
120+
"""Test that default config has use_btree=False."""
121+
config = tidesdb.default_column_family_config()
122+
assert config.use_btree is False
123+
103124
def test_list_column_families(self, db):
104125
"""Test listing column families."""
105126
db.create_column_family("cf1")
@@ -312,6 +333,22 @@ def test_column_family_stats(self, db, cf):
312333
assert stats.num_levels >= 0
313334
assert stats.memtable_size >= 0
314335

336+
def test_column_family_stats_btree_fields(self, db, cf):
337+
"""Test that B+tree stats fields are present."""
338+
with db.begin_txn() as txn:
339+
txn.put(cf, b"key1", b"value1")
340+
txn.commit()
341+
342+
stats = cf.get_stats()
343+
# B+tree stats should be present (even if 0 for non-btree CF)
344+
assert isinstance(stats.use_btree, bool)
345+
assert isinstance(stats.btree_total_nodes, int)
346+
assert isinstance(stats.btree_max_height, int)
347+
assert isinstance(stats.btree_avg_height, float)
348+
assert stats.btree_total_nodes >= 0
349+
assert stats.btree_max_height >= 0
350+
assert stats.btree_avg_height >= 0.0
351+
315352
def test_cache_stats(self, db):
316353
"""Test getting cache statistics."""
317354
stats = db.get_cache_stats()

0 commit comments

Comments
 (0)