Skip to content

Commit 2fe98ef

Browse files
authored
Merge pull request #42 from tidesdb/0-5-5
extend lua library to match c library db.h functionality availability
2 parents a991d9f + 9774d53 commit 2fe98ef

3 files changed

Lines changed: 124 additions & 3 deletions

File tree

src/tidesdb.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ ffi.cdef[[
134134
// Column family functions
135135
int tidesdb_create_column_family(void* db, const char* name, tidesdb_column_family_config_t* config);
136136
int tidesdb_drop_column_family(void* db, const char* name);
137+
int tidesdb_delete_column_family(void* db, void* cf);
137138
int tidesdb_rename_column_family(void* db, const char* old_name, const char* new_name);
138139
int tidesdb_clone_column_family(void* db, const char* source_name, const char* dest_name);
139140
void* tidesdb_get_column_family(void* db, const char* name);
@@ -909,6 +910,15 @@ function TidesDB:drop_column_family(name)
909910
check_result(result, "failed to drop column family")
910911
end
911912

913+
function TidesDB:delete_column_family(cf)
914+
if self._closed then
915+
error(TidesDBError.new("Database is closed"))
916+
end
917+
918+
local result = lib.tidesdb_delete_column_family(self._db, cf._cf)
919+
check_result(result, "failed to delete column family")
920+
end
921+
912922
function TidesDB:rename_column_family(old_name, new_name)
913923
if self._closed then
914924
error(TidesDBError.new("Database is closed"))
@@ -1092,6 +1102,6 @@ function tidesdb.save_config_to_ini(ini_file, section_name, config)
10921102
end
10931103

10941104
-- Version
1095-
tidesdb._VERSION = "0.5.4"
1105+
tidesdb._VERSION = "0.5.5"
10961106

10971107
return tidesdb

tests/test_tidesdb.lua

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,117 @@ function tests.test_commit_hook()
762762
print("PASS: test_commit_hook")
763763
end
764764

765+
function tests.test_delete_column_family()
766+
local path = "./test_db_delete_cf"
767+
cleanup_db(path)
768+
769+
local db = tidesdb.TidesDB.open(path)
770+
db:create_column_family("test_cf")
771+
local cf = db:get_column_family("test_cf")
772+
773+
-- Insert data
774+
local txn = db:begin_txn()
775+
txn:put(cf, "key1", "value1")
776+
txn:commit()
777+
txn:free()
778+
779+
-- Delete column family by pointer
780+
db:delete_column_family(cf)
781+
782+
-- Verify column family no longer exists
783+
local err = assert_error(function()
784+
db:get_column_family("test_cf")
785+
end, "test_cf should not exist after delete")
786+
787+
db:close()
788+
cleanup_db(path)
789+
print("PASS: test_delete_column_family")
790+
end
791+
792+
function tests.test_iterator_seek()
793+
local path = "./test_db_iter_seek"
794+
cleanup_db(path)
795+
796+
local db = tidesdb.TidesDB.open(path)
797+
db:create_column_family("test_cf")
798+
local cf = db:get_column_family("test_cf")
799+
800+
-- Insert ordered data
801+
local txn = db:begin_txn()
802+
txn:put(cf, "key:0001", "val1")
803+
txn:put(cf, "key:0002", "val2")
804+
txn:put(cf, "key:0003", "val3")
805+
txn:put(cf, "key:0004", "val4")
806+
txn:put(cf, "key:0005", "val5")
807+
txn:commit()
808+
txn:free()
809+
810+
-- Test seek to specific key
811+
local read_txn = db:begin_txn()
812+
local iter = read_txn:new_iterator(cf)
813+
814+
iter:seek("key:0003")
815+
assert_true(iter:valid(), "iterator should be valid after seek")
816+
assert_eq(iter:key(), "key:0003", "seek should find exact key")
817+
818+
-- Test seek_for_prev
819+
iter:seek_for_prev("key:0004")
820+
assert_true(iter:valid(), "iterator should be valid after seek_for_prev")
821+
assert_eq(iter:key(), "key:0004", "seek_for_prev should find exact key")
822+
823+
-- Test seek to non-existent key (should find next key >= target)
824+
iter:seek("key:0002x")
825+
assert_true(iter:valid(), "iterator should be valid after seek to non-existent key")
826+
assert_eq(iter:key(), "key:0003", "seek should find next key >= target")
827+
828+
iter:free()
829+
read_txn:free()
830+
831+
db:drop_column_family("test_cf")
832+
db:close()
833+
cleanup_db(path)
834+
print("PASS: test_iterator_seek")
835+
end
836+
837+
function tests.test_multi_cf_transaction()
838+
local path = "./test_db_multi_cf"
839+
cleanup_db(path)
840+
841+
local db = tidesdb.TidesDB.open(path)
842+
db:create_column_family("cf_a")
843+
db:create_column_family("cf_b")
844+
local cf_a = db:get_column_family("cf_a")
845+
local cf_b = db:get_column_family("cf_b")
846+
847+
-- Atomic transaction across two column families
848+
local txn = db:begin_txn()
849+
txn:put(cf_a, "user:1", "Alice")
850+
txn:put(cf_b, "order:1", "user:1|item:A")
851+
txn:commit()
852+
txn:free()
853+
854+
-- Verify both CFs have data
855+
local read_txn = db:begin_txn()
856+
local user = read_txn:get(cf_a, "user:1")
857+
local order = read_txn:get(cf_b, "order:1")
858+
assert_eq(user, "Alice", "cf_a should have user data")
859+
assert_eq(order, "user:1|item:A", "cf_b should have order data")
860+
read_txn:free()
861+
862+
-- Verify independence: data in cf_a is not in cf_b
863+
local verify_txn = db:begin_txn()
864+
local err = assert_error(function()
865+
verify_txn:get(cf_b, "user:1")
866+
end, "user:1 should not exist in cf_b")
867+
verify_txn:free()
868+
869+
db:drop_column_family("cf_a")
870+
db:drop_column_family("cf_b")
871+
db:close()
872+
cleanup_db(path)
873+
print("PASS: test_multi_cf_transaction")
874+
end
875+
765876
function tests.test_max_memory_usage()
766877
local path = "./test_db_max_mem"
767878
cleanup_db(path)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package = "tidesdb"
2-
version = "0.5.4-1"
2+
version = "0.5.5-1"
33
source = {
44
url = "git://github.com/tidesdb/tidesdb-lua.git",
5-
tag = "v0.5.4"
5+
tag = "v0.5.5"
66
}
77
description = {
88
summary = "Official Lua bindings for TidesDB - A high-performance embedded key-value storage engine",

0 commit comments

Comments
 (0)