Skip to content

Commit e23340b

Browse files
authored
Merge pull request #31 from tidesdb/v0-5-0
extend api with column family clone method and transaction reset capa…
2 parents 5484e26 + d2dfdb2 commit e23340b

3 files changed

Lines changed: 131 additions & 3 deletions

File tree

src/tidesdb.lua

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ ffi.cdef[[
117117
int tidesdb_create_column_family(void* db, const char* name, tidesdb_column_family_config_t* config);
118118
int tidesdb_drop_column_family(void* db, const char* name);
119119
int tidesdb_rename_column_family(void* db, const char* old_name, const char* new_name);
120+
int tidesdb_clone_column_family(void* db, const char* source_name, const char* dest_name);
120121
void* tidesdb_get_column_family(void* db, const char* name);
121122
int tidesdb_list_column_families(void* db, char*** names, int* count);
122123

@@ -129,6 +130,7 @@ ffi.cdef[[
129130
int tidesdb_txn_commit(void* txn);
130131
int tidesdb_txn_rollback(void* txn);
131132
void tidesdb_txn_free(void* txn);
133+
int tidesdb_txn_reset(void* txn, int isolation);
132134
int tidesdb_txn_savepoint(void* txn, const char* name);
133135
int tidesdb_txn_rollback_to_savepoint(void* txn, const char* name);
134136
int tidesdb_txn_release_savepoint(void* txn, const char* name);
@@ -703,6 +705,16 @@ function Transaction:rollback()
703705
check_result(result, "failed to rollback transaction")
704706
end
705707

708+
function Transaction:reset(isolation)
709+
if self._closed then
710+
error(TidesDBError.new("Transaction is closed"))
711+
end
712+
713+
local result = lib.tidesdb_txn_reset(self._txn, isolation)
714+
check_result(result, "failed to reset transaction")
715+
self._committed = false
716+
end
717+
706718
function Transaction:savepoint(name)
707719
if self._closed then
708720
error(TidesDBError.new("Transaction is closed"))
@@ -855,6 +867,15 @@ function TidesDB:rename_column_family(old_name, new_name)
855867
check_result(result, "failed to rename column family")
856868
end
857869

870+
function TidesDB:clone_column_family(source_name, dest_name)
871+
if self._closed then
872+
error(TidesDBError.new("Database is closed"))
873+
end
874+
875+
local result = lib.tidesdb_clone_column_family(self._db, source_name, dest_name)
876+
check_result(result, "failed to clone column family")
877+
end
878+
858879
function TidesDB:get_column_family(name)
859880
if self._closed then
860881
error(TidesDBError.new("Database is closed"))
@@ -1011,6 +1032,6 @@ function tidesdb.save_config_to_ini(ini_file, section_name, config)
10111032
end
10121033

10131034
-- Version
1014-
tidesdb._VERSION = "0.3.0"
1035+
tidesdb._VERSION = "0.5.0"
10151036

10161037
return tidesdb

tests/test_tidesdb.lua

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,113 @@ function tests.test_btree_stats_extended()
482482
print("PASS: test_btree_stats_extended")
483483
end
484484

485+
function tests.test_clone_column_family()
486+
local path = "./test_db_clone_cf"
487+
cleanup_db(path)
488+
489+
local db = tidesdb.TidesDB.open(path)
490+
db:create_column_family("source_cf")
491+
local cf = db:get_column_family("source_cf")
492+
493+
-- Insert data into source
494+
local txn = db:begin_txn()
495+
txn:put(cf, "key1", "value1")
496+
txn:put(cf, "key2", "value2")
497+
txn:commit()
498+
txn:free()
499+
500+
-- Clone column family
501+
db:clone_column_family("source_cf", "cloned_cf")
502+
503+
-- Verify cloned column family exists
504+
local cloned_cf = db:get_column_family("cloned_cf")
505+
assert_true(cloned_cf ~= nil, "cloned column family should exist")
506+
507+
-- Verify data is preserved in clone
508+
local read_txn = db:begin_txn()
509+
local v1 = read_txn:get(cloned_cf, "key1")
510+
local v2 = read_txn:get(cloned_cf, "key2")
511+
assert_eq(v1, "value1", "cloned key1 should have correct value")
512+
assert_eq(v2, "value2", "cloned key2 should have correct value")
513+
read_txn:free()
514+
515+
-- Verify source still works independently
516+
local src_txn = db:begin_txn()
517+
local sv1 = src_txn:get(cf, "key1")
518+
assert_eq(sv1, "value1", "source key1 should still exist")
519+
src_txn:free()
520+
521+
-- Verify modifications to clone don't affect source
522+
local write_txn = db:begin_txn()
523+
write_txn:put(cloned_cf, "key3", "value3")
524+
write_txn:commit()
525+
write_txn:free()
526+
527+
local verify_txn = db:begin_txn()
528+
local v3 = verify_txn:get(cloned_cf, "key3")
529+
assert_eq(v3, "value3", "key3 should exist in clone")
530+
local err = assert_error(function()
531+
verify_txn:get(cf, "key3")
532+
end, "key3 should not exist in source")
533+
verify_txn:free()
534+
535+
-- Verify cloning to existing name fails
536+
local clone_err = assert_error(function()
537+
db:clone_column_family("source_cf", "cloned_cf")
538+
end, "cloning to existing name should fail")
539+
540+
db:drop_column_family("source_cf")
541+
db:drop_column_family("cloned_cf")
542+
db:close()
543+
cleanup_db(path)
544+
print("PASS: test_clone_column_family")
545+
end
546+
547+
function tests.test_transaction_reset()
548+
local path = "./test_db_txn_reset"
549+
cleanup_db(path)
550+
551+
local db = tidesdb.TidesDB.open(path)
552+
db:create_column_family("test_cf")
553+
local cf = db:get_column_family("test_cf")
554+
555+
-- Begin transaction and do first batch of work
556+
local txn = db:begin_txn()
557+
txn:put(cf, "key1", "value1")
558+
txn:commit()
559+
560+
-- Reset transaction instead of free + begin
561+
txn:reset(tidesdb.IsolationLevel.READ_COMMITTED)
562+
563+
-- Second batch of work using the same transaction
564+
txn:put(cf, "key2", "value2")
565+
txn:commit()
566+
567+
-- Reset again with different isolation level
568+
txn:reset(tidesdb.IsolationLevel.SERIALIZABLE)
569+
570+
-- Third batch
571+
txn:put(cf, "key3", "value3")
572+
txn:commit()
573+
574+
txn:free()
575+
576+
-- Verify all data was written
577+
local read_txn = db:begin_txn()
578+
local v1 = read_txn:get(cf, "key1")
579+
local v2 = read_txn:get(cf, "key2")
580+
local v3 = read_txn:get(cf, "key3")
581+
assert_eq(v1, "value1", "key1 should exist after reset")
582+
assert_eq(v2, "value2", "key2 should exist after reset")
583+
assert_eq(v3, "value3", "key3 should exist after reset")
584+
read_txn:free()
585+
586+
db:drop_column_family("test_cf")
587+
db:close()
588+
cleanup_db(path)
589+
print("PASS: test_transaction_reset")
590+
end
591+
485592
-- Run all tests
486593
local function run_tests()
487594
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.4.0-1"
2+
version = "0.5.0-1"
33
source = {
44
url = "git://github.com/tidesdb/tidesdb-lua.git",
5-
tag = "v0.4.0"
5+
tag = "v0.5.0"
66
}
77
description = {
88
summary = "Official Lua bindings for TidesDB - A high-performance embedded key-value storage engine",

0 commit comments

Comments
 (0)