diff --git a/src/tidesdb.lua b/src/tidesdb.lua index 2678b2c..8a0dd46 100644 --- a/src/tidesdb.lua +++ b/src/tidesdb.lua @@ -93,6 +93,7 @@ ffi.cdef[[ size_t max_open_sstables; int log_to_file; size_t log_truncation_at; + size_t max_memory_usage; } tidesdb_config_t; typedef struct { @@ -361,6 +362,7 @@ function tidesdb.default_config() max_open_sstables = 256, log_to_file = false, log_truncation_at = 24 * 1024 * 1024, + max_memory_usage = 0, } end @@ -848,6 +850,7 @@ function TidesDB.new(config) c_config.max_open_sstables = config.max_open_sstables or 256 c_config.log_to_file = config.log_to_file and 1 or 0 c_config.log_truncation_at = config.log_truncation_at or 24 * 1024 * 1024 + c_config.max_memory_usage = config.max_memory_usage or 0 local db_ptr = ffi.new("void*[1]") local result = lib.tidesdb_open(c_config, db_ptr) @@ -868,6 +871,7 @@ function TidesDB.open(path, options) max_open_sstables = options.max_open_sstables or 256, log_to_file = options.log_to_file or false, log_truncation_at = options.log_truncation_at or 24 * 1024 * 1024, + max_memory_usage = options.max_memory_usage or 0, } return TidesDB.new(config) end diff --git a/tests/test_tidesdb.lua b/tests/test_tidesdb.lua index 6473374..6be9be5 100644 --- a/tests/test_tidesdb.lua +++ b/tests/test_tidesdb.lua @@ -762,6 +762,43 @@ function tests.test_commit_hook() print("PASS: test_commit_hook") end +function tests.test_max_memory_usage() + local path = "./test_db_max_mem" + cleanup_db(path) + + -- Test default config includes max_memory_usage + local default_cfg = tidesdb.default_config() + assert_true(default_cfg.max_memory_usage ~= nil, "max_memory_usage should exist in default config") + + -- Test opening database with default max_memory_usage (0 = unlimited) + local db1 = tidesdb.TidesDB.open(path) + assert_true(db1 ~= nil, "database should open with default max_memory_usage") + db1:close() + cleanup_db(path) + + -- Test opening database with custom max_memory_usage + local db2 = tidesdb.TidesDB.open(path, { + max_memory_usage = 512 * 1024 * 1024 -- 512 MB + }) + assert_true(db2 ~= nil, "database should open with custom max_memory_usage") + db2:close() + cleanup_db(path) + + -- Test with TidesDB.new() constructor + local config = { + db_path = path, + num_flush_threads = 2, + num_compaction_threads = 2, + max_memory_usage = 256 * 1024 * 1024 -- 256 MB + } + local db3 = tidesdb.TidesDB.new(config) + assert_true(db3 ~= nil, "database should be created with TidesDB.new() and max_memory_usage") + db3:close() + + cleanup_db(path) + print("PASS: test_max_memory_usage") +end + -- Run all tests local function run_tests() print("Running TidesDB Lua tests...")