From efe5ec11795154a98c4052b2cbece4e9ffabec1d Mon Sep 17 00:00:00 2001 From: Alex Gaetano Padula Date: Mon, 2 Feb 2026 14:35:26 -0500 Subject: [PATCH] update lua binding library to align with tidesdb 8; i've extended tests as well for lsmb+ --- src/tidesdb.lua | 15 +++- tests/test_tidesdb.lua | 79 +++++++++++++++++++ ...3.1-1.rockspec => tidesdb-0.4.0-1.rockspec | 4 +- 3 files changed, 95 insertions(+), 3 deletions(-) rename tidesdb-0.3.1-1.rockspec => tidesdb-0.4.0-1.rockspec (95%) diff --git a/src/tidesdb.lua b/src/tidesdb.lua index cd3ef15..ed7c99f 100644 --- a/src/tidesdb.lua +++ b/src/tidesdb.lua @@ -64,6 +64,7 @@ ffi.cdef[[ uint64_t min_disk_space; int l1_file_count_trigger; int l0_queue_stall_threshold; + int use_btree; } tidesdb_column_family_config_t; typedef struct { @@ -90,6 +91,10 @@ ffi.cdef[[ uint64_t* level_key_counts; double read_amp; double hit_rate; + int use_btree; + uint64_t btree_total_nodes; + uint32_t btree_max_height; + double btree_avg_height; } tidesdb_stats_t; typedef struct { @@ -358,6 +363,7 @@ function tidesdb.default_column_family_config() min_disk_space = tonumber(c_config.min_disk_space), l1_file_count_trigger = c_config.l1_file_count_trigger, l0_queue_stall_threshold = c_config.l0_queue_stall_threshold, + use_btree = c_config.use_btree ~= 0, } end @@ -383,6 +389,7 @@ local function config_to_c_struct(config) c_config.min_disk_space = config.min_disk_space or 100 * 1024 * 1024 c_config.l1_file_count_trigger = config.l1_file_count_trigger or 4 c_config.l0_queue_stall_threshold = config.l0_queue_stall_threshold or 20 + c_config.use_btree = config.use_btree and 1 or 0 local name = config.comparator_name or "memcmp" local name_len = math.min(#name, 63) @@ -577,6 +584,7 @@ function ColumnFamily:get_stats() min_disk_space = tonumber(c_cfg.min_disk_space), l1_file_count_trigger = c_cfg.l1_file_count_trigger, l0_queue_stall_threshold = c_cfg.l0_queue_stall_threshold, + use_btree = c_cfg.use_btree ~= 0, } end @@ -600,6 +608,10 @@ function ColumnFamily:get_stats() level_key_counts = level_key_counts, read_amp = c_stats.read_amp, hit_rate = c_stats.hit_rate, + use_btree = c_stats.use_btree ~= 0, + btree_total_nodes = tonumber(c_stats.btree_total_nodes), + btree_max_height = c_stats.btree_max_height, + btree_avg_height = c_stats.btree_avg_height, } lib.tidesdb_free_stats(stats_ptr[0]) @@ -988,6 +1000,7 @@ function tidesdb.load_config_from_ini(ini_file, section_name) min_disk_space = tonumber(c_config.min_disk_space), l1_file_count_trigger = c_config.l1_file_count_trigger, l0_queue_stall_threshold = c_config.l0_queue_stall_threshold, + use_btree = c_config.use_btree ~= 0, } end @@ -998,6 +1011,6 @@ function tidesdb.save_config_to_ini(ini_file, section_name, config) end -- Version -tidesdb._VERSION = "0.2.0" +tidesdb._VERSION = "0.3.0" return tidesdb diff --git a/tests/test_tidesdb.lua b/tests/test_tidesdb.lua index bf0b97d..954332d 100644 --- a/tests/test_tidesdb.lua +++ b/tests/test_tidesdb.lua @@ -403,6 +403,85 @@ function tests.test_update_runtime_config() print("PASS: test_update_runtime_config") end +function tests.test_use_btree_config() + local path = "./test_db_btree" + cleanup_db(path) + + local db = tidesdb.TidesDB.open(path) + + -- Create column family with use_btree enabled + local cf_config = tidesdb.default_column_family_config() + cf_config.use_btree = true + db:create_column_family("btree_cf", cf_config) + + local cf = db:get_column_family("btree_cf") + + -- Insert some data + local txn = db:begin_txn() + txn:put(cf, "key1", "value1") + txn:put(cf, "key2", "value2") + txn:commit() + txn:free() + + -- Verify use_btree in stats + local stats = cf:get_stats() + assert_true(stats.use_btree ~= nil, "use_btree should exist in stats") + assert_true(stats.config.use_btree ~= nil, "use_btree should exist in config") + + -- Verify B+tree stats fields exist + assert_true(stats.btree_total_nodes ~= nil, "btree_total_nodes should exist") + assert_true(stats.btree_max_height ~= nil, "btree_max_height should exist") + assert_true(stats.btree_avg_height ~= nil, "btree_avg_height should exist") + + -- Read back data to verify it works + local read_txn = db:begin_txn() + local value1 = read_txn:get(cf, "key1") + local value2 = read_txn:get(cf, "key2") + assert_eq(value1, "value1", "get key1 with btree") + assert_eq(value2, "value2", "get key2 with btree") + read_txn:free() + + db:drop_column_family("btree_cf") + db:close() + cleanup_db(path) + print("PASS: test_use_btree_config") +end + +function tests.test_btree_stats_extended() + local path = "./test_db_btree_stats" + cleanup_db(path) + + local db = tidesdb.TidesDB.open(path) + + -- Create column family without btree (default) + local cf_config = tidesdb.default_column_family_config() + cf_config.use_btree = false + db:create_column_family("block_cf", cf_config) + + local cf = db:get_column_family("block_cf") + + -- Insert data + local txn = db:begin_txn() + txn:put(cf, "key1", "value1") + txn:commit() + txn:free() + + -- Verify stats + local stats = cf:get_stats() + assert_eq(stats.use_btree, false, "use_btree should be false for block-based CF") + assert_eq(stats.config.use_btree, false, "config.use_btree should be false") + + -- B+tree stats should still exist but be zero/default for non-btree CF + assert_true(stats.btree_total_nodes ~= nil, "btree_total_nodes should exist") + assert_true(stats.btree_max_height ~= nil, "btree_max_height should exist") + assert_true(stats.btree_avg_height ~= nil, "btree_avg_height should exist") + + db:drop_column_family("block_cf") + db:close() + cleanup_db(path) + print("PASS: test_btree_stats_extended") +end + -- Run all tests local function run_tests() print("Running TidesDB Lua tests...") diff --git a/tidesdb-0.3.1-1.rockspec b/tidesdb-0.4.0-1.rockspec similarity index 95% rename from tidesdb-0.3.1-1.rockspec rename to tidesdb-0.4.0-1.rockspec index 8805eeb..9e9ed0e 100644 --- a/tidesdb-0.3.1-1.rockspec +++ b/tidesdb-0.4.0-1.rockspec @@ -1,8 +1,8 @@ package = "tidesdb" -version = "0.3.1-1" +version = "0.4.0-1" source = { url = "git://github.com/tidesdb/tidesdb-lua.git", - tag = "v0.3.1" + tag = "v0.4.0" } description = { summary = "Official Lua bindings for TidesDB - A high-performance embedded key-value storage engine",