Skip to content

Commit 5484e26

Browse files
authored
Merge pull request #29 from tidesdb/tdb8
update lua binding library to align with tidesdb 8; i've extended tes…
2 parents e28004f + efe5ec1 commit 5484e26

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

src/tidesdb.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ ffi.cdef[[
6464
uint64_t min_disk_space;
6565
int l1_file_count_trigger;
6666
int l0_queue_stall_threshold;
67+
int use_btree;
6768
} tidesdb_column_family_config_t;
6869

6970
typedef struct {
@@ -90,6 +91,10 @@ ffi.cdef[[
9091
uint64_t* level_key_counts;
9192
double read_amp;
9293
double hit_rate;
94+
int use_btree;
95+
uint64_t btree_total_nodes;
96+
uint32_t btree_max_height;
97+
double btree_avg_height;
9398
} tidesdb_stats_t;
9499

95100
typedef struct {
@@ -358,6 +363,7 @@ function tidesdb.default_column_family_config()
358363
min_disk_space = tonumber(c_config.min_disk_space),
359364
l1_file_count_trigger = c_config.l1_file_count_trigger,
360365
l0_queue_stall_threshold = c_config.l0_queue_stall_threshold,
366+
use_btree = c_config.use_btree ~= 0,
361367
}
362368
end
363369

@@ -383,6 +389,7 @@ local function config_to_c_struct(config)
383389
c_config.min_disk_space = config.min_disk_space or 100 * 1024 * 1024
384390
c_config.l1_file_count_trigger = config.l1_file_count_trigger or 4
385391
c_config.l0_queue_stall_threshold = config.l0_queue_stall_threshold or 20
392+
c_config.use_btree = config.use_btree and 1 or 0
386393

387394
local name = config.comparator_name or "memcmp"
388395
local name_len = math.min(#name, 63)
@@ -577,6 +584,7 @@ function ColumnFamily:get_stats()
577584
min_disk_space = tonumber(c_cfg.min_disk_space),
578585
l1_file_count_trigger = c_cfg.l1_file_count_trigger,
579586
l0_queue_stall_threshold = c_cfg.l0_queue_stall_threshold,
587+
use_btree = c_cfg.use_btree ~= 0,
580588
}
581589
end
582590

@@ -600,6 +608,10 @@ function ColumnFamily:get_stats()
600608
level_key_counts = level_key_counts,
601609
read_amp = c_stats.read_amp,
602610
hit_rate = c_stats.hit_rate,
611+
use_btree = c_stats.use_btree ~= 0,
612+
btree_total_nodes = tonumber(c_stats.btree_total_nodes),
613+
btree_max_height = c_stats.btree_max_height,
614+
btree_avg_height = c_stats.btree_avg_height,
603615
}
604616

605617
lib.tidesdb_free_stats(stats_ptr[0])
@@ -988,6 +1000,7 @@ function tidesdb.load_config_from_ini(ini_file, section_name)
9881000
min_disk_space = tonumber(c_config.min_disk_space),
9891001
l1_file_count_trigger = c_config.l1_file_count_trigger,
9901002
l0_queue_stall_threshold = c_config.l0_queue_stall_threshold,
1003+
use_btree = c_config.use_btree ~= 0,
9911004
}
9921005
end
9931006

@@ -998,6 +1011,6 @@ function tidesdb.save_config_to_ini(ini_file, section_name, config)
9981011
end
9991012

10001013
-- Version
1001-
tidesdb._VERSION = "0.2.0"
1014+
tidesdb._VERSION = "0.3.0"
10021015

10031016
return tidesdb

tests/test_tidesdb.lua

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,85 @@ function tests.test_update_runtime_config()
403403
print("PASS: test_update_runtime_config")
404404
end
405405

406+
function tests.test_use_btree_config()
407+
local path = "./test_db_btree"
408+
cleanup_db(path)
409+
410+
local db = tidesdb.TidesDB.open(path)
411+
412+
-- Create column family with use_btree enabled
413+
local cf_config = tidesdb.default_column_family_config()
414+
cf_config.use_btree = true
415+
db:create_column_family("btree_cf", cf_config)
416+
417+
local cf = db:get_column_family("btree_cf")
418+
419+
-- Insert some data
420+
local txn = db:begin_txn()
421+
txn:put(cf, "key1", "value1")
422+
txn:put(cf, "key2", "value2")
423+
txn:commit()
424+
txn:free()
425+
426+
-- Verify use_btree in stats
427+
local stats = cf:get_stats()
428+
assert_true(stats.use_btree ~= nil, "use_btree should exist in stats")
429+
assert_true(stats.config.use_btree ~= nil, "use_btree should exist in config")
430+
431+
-- Verify B+tree stats fields exist
432+
assert_true(stats.btree_total_nodes ~= nil, "btree_total_nodes should exist")
433+
assert_true(stats.btree_max_height ~= nil, "btree_max_height should exist")
434+
assert_true(stats.btree_avg_height ~= nil, "btree_avg_height should exist")
435+
436+
-- Read back data to verify it works
437+
local read_txn = db:begin_txn()
438+
local value1 = read_txn:get(cf, "key1")
439+
local value2 = read_txn:get(cf, "key2")
440+
assert_eq(value1, "value1", "get key1 with btree")
441+
assert_eq(value2, "value2", "get key2 with btree")
442+
read_txn:free()
443+
444+
db:drop_column_family("btree_cf")
445+
db:close()
446+
cleanup_db(path)
447+
print("PASS: test_use_btree_config")
448+
end
449+
450+
function tests.test_btree_stats_extended()
451+
local path = "./test_db_btree_stats"
452+
cleanup_db(path)
453+
454+
local db = tidesdb.TidesDB.open(path)
455+
456+
-- Create column family without btree (default)
457+
local cf_config = tidesdb.default_column_family_config()
458+
cf_config.use_btree = false
459+
db:create_column_family("block_cf", cf_config)
460+
461+
local cf = db:get_column_family("block_cf")
462+
463+
-- Insert data
464+
local txn = db:begin_txn()
465+
txn:put(cf, "key1", "value1")
466+
txn:commit()
467+
txn:free()
468+
469+
-- Verify stats
470+
local stats = cf:get_stats()
471+
assert_eq(stats.use_btree, false, "use_btree should be false for block-based CF")
472+
assert_eq(stats.config.use_btree, false, "config.use_btree should be false")
473+
474+
-- B+tree stats should still exist but be zero/default for non-btree CF
475+
assert_true(stats.btree_total_nodes ~= nil, "btree_total_nodes should exist")
476+
assert_true(stats.btree_max_height ~= nil, "btree_max_height should exist")
477+
assert_true(stats.btree_avg_height ~= nil, "btree_avg_height should exist")
478+
479+
db:drop_column_family("block_cf")
480+
db:close()
481+
cleanup_db(path)
482+
print("PASS: test_btree_stats_extended")
483+
end
484+
406485
-- Run all tests
407486
local function run_tests()
408487
print("Running TidesDB Lua tests...")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package = "tidesdb"
2-
version = "0.3.1-1"
2+
version = "0.4.0-1"
33
source = {
44
url = "git://github.com/tidesdb/tidesdb-lua.git",
5-
tag = "v0.3.1"
5+
tag = "v0.4.0"
66
}
77
description = {
88
summary = "Official Lua bindings for TidesDB - A high-performance embedded key-value storage engine",

0 commit comments

Comments
 (0)