Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/tidesdb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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])
Expand Down Expand Up @@ -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

Expand All @@ -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
79 changes: 79 additions & 0 deletions tests/test_tidesdb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down
4 changes: 2 additions & 2 deletions tidesdb-0.3.1-1.rockspec → tidesdb-0.4.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Loading