-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_tidesdb.lua
More file actions
1097 lines (887 loc) · 32.6 KB
/
test_tidesdb.lua
File metadata and controls
1097 lines (887 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
TidesDB Lua Bindings Tests
Copyright (C) TidesDB
Licensed under the Mozilla Public License, v. 2.0
]]
local tidesdb = require("tidesdb")
-- Test utilities
local function assert_eq(a, b, msg)
if a ~= b then
error(string.format("%s: expected %s, got %s", msg or "assertion failed", tostring(b), tostring(a)))
end
end
local function assert_true(cond, msg)
if not cond then
error(msg or "assertion failed: expected true")
end
end
local function assert_error(fn, msg)
local ok, err = pcall(fn)
if ok then
error(msg or "expected error but none was raised")
end
return err
end
local function cleanup_db(path)
os.execute("rm -rf " .. path)
end
-- Test cases
local tests = {}
function tests.test_open_close()
local path = "./test_db_open_close"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
assert_true(db ~= nil, "database should be opened")
db:close()
cleanup_db(path)
print("PASS: test_open_close")
end
function tests.test_column_family_operations()
local path = "./test_db_cf"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
-- Create column family
local cf_config = tidesdb.default_column_family_config()
cf_config.compression_algorithm = tidesdb.CompressionAlgorithm.LZ4_COMPRESSION
db:create_column_family("test_cf", cf_config)
-- Get column family
local cf = db:get_column_family("test_cf")
assert_eq(cf.name, "test_cf", "column family name")
-- List column families
local cfs = db:list_column_families()
assert_true(#cfs >= 1, "should have at least one column family")
-- Drop column family
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_column_family_operations")
end
function tests.test_put_get_delete()
local path = "./test_db_crud"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Put
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:put(cf, "key2", "value2")
txn:commit()
txn:free()
-- Get
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")
assert_eq(value2, "value2", "get key2")
read_txn:free()
-- Delete
local del_txn = db:begin_txn()
del_txn:delete(cf, "key1")
del_txn:commit()
del_txn:free()
-- Verify deletion
local verify_txn = db:begin_txn()
local err = assert_error(function()
verify_txn:get(cf, "key1")
end, "should error on deleted key")
verify_txn:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_put_get_delete")
end
function tests.test_transaction_rollback()
local path = "./test_db_rollback"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Put initial value
local txn1 = db:begin_txn()
txn1:put(cf, "key1", "initial")
txn1:commit()
txn1:free()
-- Start transaction and rollback
local txn2 = db:begin_txn()
txn2:put(cf, "key1", "modified")
txn2:rollback()
txn2:free()
-- Verify value unchanged
local read_txn = db:begin_txn()
local value = read_txn:get(cf, "key1")
assert_eq(value, "initial", "value should be unchanged after rollback")
read_txn:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_transaction_rollback")
end
function tests.test_iterator()
local path = "./test_db_iter"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Insert data
local txn = db:begin_txn()
txn:put(cf, "a", "1")
txn:put(cf, "b", "2")
txn:put(cf, "c", "3")
txn:commit()
txn:free()
-- Forward iteration
local read_txn = db:begin_txn()
local iter = read_txn:new_iterator(cf)
iter:seek_to_first()
local count = 0
while iter:valid() do
local key = iter:key()
local value = iter:value()
count = count + 1
iter:next()
end
assert_eq(count, 3, "should iterate over 3 entries")
iter:free()
read_txn:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_iterator")
end
function tests.test_savepoints()
local path = "./test_db_savepoint"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
-- Create savepoint
txn:savepoint("sp1")
txn:put(cf, "key2", "value2")
-- Rollback to savepoint
txn:rollback_to_savepoint("sp1")
-- Commit (only key1 should be written)
txn:commit()
txn:free()
-- Verify
local read_txn = db:begin_txn()
local value1 = read_txn:get(cf, "key1")
assert_eq(value1, "value1", "key1 should exist")
local err = assert_error(function()
read_txn:get(cf, "key2")
end, "key2 should not exist")
read_txn:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_savepoints")
end
function tests.test_isolation_levels()
local path = "./test_db_isolation"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Test different isolation levels
local txn1 = db:begin_txn_with_isolation(tidesdb.IsolationLevel.READ_COMMITTED)
txn1:put(cf, "key1", "value1")
txn1:commit()
txn1:free()
local txn2 = db:begin_txn_with_isolation(tidesdb.IsolationLevel.SERIALIZABLE)
local value = txn2:get(cf, "key1")
assert_eq(value, "value1", "should read committed value")
txn2:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_isolation_levels")
end
function tests.test_stats()
local path = "./test_db_stats"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Insert some data
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:commit()
txn:free()
-- Get stats
local stats = cf:get_stats()
assert_true(stats.num_levels >= 0, "num_levels should be >= 0")
assert_true(stats.memtable_size >= 0, "memtable_size should be >= 0")
-- Test new stats fields
assert_true(stats.total_keys ~= nil, "total_keys should exist")
assert_true(stats.total_data_size ~= nil, "total_data_size should exist")
assert_true(stats.avg_key_size ~= nil, "avg_key_size should exist")
assert_true(stats.avg_value_size ~= nil, "avg_value_size should exist")
assert_true(stats.read_amp ~= nil, "read_amp should exist")
assert_true(stats.hit_rate ~= nil, "hit_rate should exist")
-- Get cache stats
local cache_stats = db:get_cache_stats()
assert_true(cache_stats.total_entries >= 0, "total_entries should be >= 0")
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_stats")
end
function tests.test_rename_column_family()
local path = "./test_db_rename_cf"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("old_cf")
-- Insert data
local cf = db:get_column_family("old_cf")
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:commit()
txn:free()
-- Rename column family
db:rename_column_family("old_cf", "new_cf")
-- Verify old name doesn't exist
local err = assert_error(function()
db:get_column_family("old_cf")
end, "old_cf should not exist after rename")
-- Verify new name exists and data is preserved
local new_cf = db:get_column_family("new_cf")
local read_txn = db:begin_txn()
local value = read_txn:get(new_cf, "key1")
assert_eq(value, "value1", "data should be preserved after rename")
read_txn:free()
db:drop_column_family("new_cf")
db:close()
cleanup_db(path)
print("PASS: test_rename_column_family")
end
function tests.test_is_flushing_compacting()
local path = "./test_db_flush_compact"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Check status (should be false when idle)
local is_flushing = cf:is_flushing()
local is_compacting = cf:is_compacting()
assert_true(is_flushing == false or is_flushing == true, "is_flushing should return boolean")
assert_true(is_compacting == false or is_compacting == true, "is_compacting should return boolean")
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_is_flushing_compacting")
end
function tests.test_backup()
local path = "./test_db_backup"
local backup_path = "./test_db_backup_copy"
cleanup_db(path)
cleanup_db(backup_path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Insert data
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:commit()
txn:free()
-- Create backup
db:backup(backup_path)
db:close()
-- Open backup and verify data
local backup_db = tidesdb.TidesDB.open(backup_path)
local backup_cf = backup_db:get_column_family("test_cf")
local read_txn = backup_db:begin_txn()
local value = read_txn:get(backup_cf, "key1")
assert_eq(value, "value1", "backup should contain original data")
read_txn:free()
backup_db:close()
cleanup_db(path)
cleanup_db(backup_path)
print("PASS: test_backup")
end
function tests.test_checkpoint()
local path = "./test_db_checkpoint"
local checkpoint_path = "./test_db_checkpoint_snap"
cleanup_db(path)
cleanup_db(checkpoint_path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Insert data
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:put(cf, "key2", "value2")
txn:commit()
txn:free()
-- Create checkpoint
db:checkpoint(checkpoint_path)
db:close()
-- Open checkpoint and verify data
local cp_db = tidesdb.TidesDB.open(checkpoint_path)
local cp_cf = cp_db:get_column_family("test_cf")
local read_txn = cp_db:begin_txn()
local v1 = read_txn:get(cp_cf, "key1")
local v2 = read_txn:get(cp_cf, "key2")
assert_eq(v1, "value1", "checkpoint should contain key1")
assert_eq(v2, "value2", "checkpoint should contain key2")
read_txn:free()
-- Verify checkpoint to existing non-empty dir fails
cp_db:close()
local db2 = tidesdb.TidesDB.open(path)
local cf2 = db2:get_column_family("test_cf")
local err = assert_error(function()
db2:checkpoint(checkpoint_path)
end, "checkpoint to non-empty dir should fail")
db2:close()
cleanup_db(path)
cleanup_db(checkpoint_path)
print("PASS: test_checkpoint")
end
function tests.test_update_runtime_config()
local path = "./test_db_runtime_config"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Get current config
local stats = cf:get_stats()
local original_write_buffer_size = stats.config.write_buffer_size
-- Update runtime config
local new_config = tidesdb.default_column_family_config()
new_config.write_buffer_size = 128 * 1024 * 1024 -- 128MB
cf:update_runtime_config(new_config, false) -- don't persist
-- Verify config was updated
local new_stats = cf:get_stats()
assert_eq(new_stats.config.write_buffer_size, 128 * 1024 * 1024, "write_buffer_size should be updated")
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
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
function tests.test_clone_column_family()
local path = "./test_db_clone_cf"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("source_cf")
local cf = db:get_column_family("source_cf")
-- Insert data into source
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:put(cf, "key2", "value2")
txn:commit()
txn:free()
-- Clone column family
db:clone_column_family("source_cf", "cloned_cf")
-- Verify cloned column family exists
local cloned_cf = db:get_column_family("cloned_cf")
assert_true(cloned_cf ~= nil, "cloned column family should exist")
-- Verify data is preserved in clone
local read_txn = db:begin_txn()
local v1 = read_txn:get(cloned_cf, "key1")
local v2 = read_txn:get(cloned_cf, "key2")
assert_eq(v1, "value1", "cloned key1 should have correct value")
assert_eq(v2, "value2", "cloned key2 should have correct value")
read_txn:free()
-- Verify source still works independently
local src_txn = db:begin_txn()
local sv1 = src_txn:get(cf, "key1")
assert_eq(sv1, "value1", "source key1 should still exist")
src_txn:free()
-- Verify modifications to clone don't affect source
local write_txn = db:begin_txn()
write_txn:put(cloned_cf, "key3", "value3")
write_txn:commit()
write_txn:free()
local verify_txn = db:begin_txn()
local v3 = verify_txn:get(cloned_cf, "key3")
assert_eq(v3, "value3", "key3 should exist in clone")
local err = assert_error(function()
verify_txn:get(cf, "key3")
end, "key3 should not exist in source")
verify_txn:free()
-- Verify cloning to existing name fails
local clone_err = assert_error(function()
db:clone_column_family("source_cf", "cloned_cf")
end, "cloning to existing name should fail")
db:drop_column_family("source_cf")
db:drop_column_family("cloned_cf")
db:close()
cleanup_db(path)
print("PASS: test_clone_column_family")
end
function tests.test_transaction_reset()
local path = "./test_db_txn_reset"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Begin transaction and do first batch of work
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:commit()
-- Reset transaction instead of free + begin
txn:reset(tidesdb.IsolationLevel.READ_COMMITTED)
-- Second batch of work using the same transaction
txn:put(cf, "key2", "value2")
txn:commit()
-- Reset again with different isolation level
txn:reset(tidesdb.IsolationLevel.SERIALIZABLE)
-- Third batch
txn:put(cf, "key3", "value3")
txn:commit()
txn:free()
-- Verify all data was written
local read_txn = db:begin_txn()
local v1 = read_txn:get(cf, "key1")
local v2 = read_txn:get(cf, "key2")
local v3 = read_txn:get(cf, "key3")
assert_eq(v1, "value1", "key1 should exist after reset")
assert_eq(v2, "value2", "key2 should exist after reset")
assert_eq(v3, "value3", "key3 should exist after reset")
read_txn:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_transaction_reset")
end
function tests.test_range_cost()
local path = "./test_db_range_cost"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Insert some data
local txn = db:begin_txn()
for i = 1, 20 do
txn:put(cf, string.format("key:%04d", i), string.format("value:%04d", i))
end
txn:commit()
txn:free()
-- Estimate range cost
local cost = cf:range_cost("key:0001", "key:0020")
assert_true(cost ~= nil, "range cost should not be nil")
assert_true(type(cost) == "number", "range cost should be a number")
assert_true(cost >= 0, "range cost should be >= 0")
-- Compare two ranges (wider range should cost >= narrower range)
local cost_wide = cf:range_cost("key:0001", "key:0020")
local cost_narrow = cf:range_cost("key:0005", "key:0010")
assert_true(cost_wide >= 0, "wide range cost should be >= 0")
assert_true(cost_narrow >= 0, "narrow range cost should be >= 0")
-- Key order should not matter
local cost_ab = cf:range_cost("key:0001", "key:0020")
local cost_ba = cf:range_cost("key:0020", "key:0001")
assert_eq(cost_ab, cost_ba, "range cost should be the same regardless of key order")
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_range_cost")
end
function tests.test_commit_hook()
local ffi = require("ffi")
local path = "./test_db_commit_hook"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Track hook invocations
local hook_called = 0
local hook_ops_count = 0
local hook_seq = 0
local hook_keys = {}
local hook_had_delete = false
local hook = ffi.cast("tidesdb_commit_hook_fn", function(ops, num_ops, commit_seq, ctx)
hook_called = hook_called + 1
hook_ops_count = hook_ops_count + num_ops
hook_seq = tonumber(commit_seq)
for i = 0, num_ops - 1 do
local key = ffi.string(ops[i].key, ops[i].key_size)
table.insert(hook_keys, key)
if ops[i].is_delete ~= 0 then
hook_had_delete = true
end
end
return 0
end)
-- Set commit hook
cf:set_commit_hook(hook, nil)
-- Write data (should trigger the hook)
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:put(cf, "key2", "value2")
txn:commit()
txn:free()
assert_true(hook_called >= 1, "commit hook should have been called")
assert_true(hook_ops_count >= 2, "hook should have received at least 2 ops")
assert_true(hook_seq > 0, "commit_seq should be > 0")
-- Verify keys were captured
local found_key1 = false
local found_key2 = false
for _, k in ipairs(hook_keys) do
if k == "key1" then found_key1 = true end
if k == "key2" then found_key2 = true end
end
assert_true(found_key1, "hook should have received key1")
assert_true(found_key2, "hook should have received key2")
-- Test delete operation fires hook
hook_had_delete = false
local del_txn = db:begin_txn()
del_txn:delete(cf, "key1")
del_txn:commit()
del_txn:free()
assert_true(hook_had_delete, "hook should have received a delete operation")
-- Save hook_called count before clearing
local calls_before_clear = hook_called
-- Clear commit hook
cf:clear_commit_hook()
-- Write more data (should NOT trigger the hook)
local txn2 = db:begin_txn()
txn2:put(cf, "key3", "value3")
txn2:commit()
txn2:free()
assert_eq(hook_called, calls_before_clear, "hook should not fire after clearing")
-- Clean up the callback to prevent GC issues
hook:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_commit_hook")
end
function tests.test_delete_column_family()
local path = "./test_db_delete_cf"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Insert data
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:commit()
txn:free()
-- Delete column family by pointer
db:delete_column_family(cf)
-- Verify column family no longer exists
local err = assert_error(function()
db:get_column_family("test_cf")
end, "test_cf should not exist after delete")
db:close()
cleanup_db(path)
print("PASS: test_delete_column_family")
end
function tests.test_iterator_seek()
local path = "./test_db_iter_seek"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Insert ordered data
local txn = db:begin_txn()
txn:put(cf, "key:0001", "val1")
txn:put(cf, "key:0002", "val2")
txn:put(cf, "key:0003", "val3")
txn:put(cf, "key:0004", "val4")
txn:put(cf, "key:0005", "val5")
txn:commit()
txn:free()
-- Test seek to specific key
local read_txn = db:begin_txn()
local iter = read_txn:new_iterator(cf)
iter:seek("key:0003")
assert_true(iter:valid(), "iterator should be valid after seek")
assert_eq(iter:key(), "key:0003", "seek should find exact key")
-- Test seek_for_prev
iter:seek_for_prev("key:0004")
assert_true(iter:valid(), "iterator should be valid after seek_for_prev")
assert_eq(iter:key(), "key:0004", "seek_for_prev should find exact key")
-- Test seek to non-existent key (should find next key >= target)
iter:seek("key:0002x")
assert_true(iter:valid(), "iterator should be valid after seek to non-existent key")
assert_eq(iter:key(), "key:0003", "seek should find next key >= target")
iter:free()
read_txn:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_iterator_seek")
end
function tests.test_multi_cf_transaction()
local path = "./test_db_multi_cf"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path)
db:create_column_family("cf_a")
db:create_column_family("cf_b")
local cf_a = db:get_column_family("cf_a")
local cf_b = db:get_column_family("cf_b")
-- Atomic transaction across two column families
local txn = db:begin_txn()
txn:put(cf_a, "user:1", "Alice")
txn:put(cf_b, "order:1", "user:1|item:A")
txn:commit()
txn:free()
-- Verify both CFs have data
local read_txn = db:begin_txn()
local user = read_txn:get(cf_a, "user:1")
local order = read_txn:get(cf_b, "order:1")
assert_eq(user, "Alice", "cf_a should have user data")
assert_eq(order, "user:1|item:A", "cf_b should have order data")
read_txn:free()
-- Verify independence: data in cf_a is not in cf_b
local verify_txn = db:begin_txn()
local err = assert_error(function()
verify_txn:get(cf_b, "user:1")
end, "user:1 should not exist in cf_b")
verify_txn:free()
db:drop_column_family("cf_a")
db:drop_column_family("cf_b")
db:close()
cleanup_db(path)
print("PASS: test_multi_cf_transaction")
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
function tests.test_sync_wal()
local path = "./test_db_sync_wal"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path, {
log_level = tidesdb.LogLevel.LOG_WARN,
})
local cf_config = tidesdb.default_column_family_config()
cf_config.sync_mode = tidesdb.SyncMode.SYNC_NONE
db:create_column_family("test_cf", cf_config)
local cf = db:get_column_family("test_cf")
-- Write some data
local txn = db:begin_txn()
txn:put(cf, "key1", "value1")
txn:put(cf, "key2", "value2")
txn:commit()
txn:free()
-- Manually sync WAL
cf:sync_wal()
-- Verify data is still readable after sync
local read_txn = db:begin_txn()
local v1 = read_txn:get(cf, "key1")
local v2 = read_txn:get(cf, "key2")
assert_eq(v1, "value1", "key1 should be readable after sync_wal")
assert_eq(v2, "value2", "key2 should be readable after sync_wal")
read_txn:free()
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_sync_wal")
end
function tests.test_purge_cf()
local path = "./test_db_purge_cf"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path, {
log_level = tidesdb.LogLevel.LOG_WARN,
})
db:create_column_family("test_cf")
local cf = db:get_column_family("test_cf")
-- Write some data
local txn = db:begin_txn()
for i = 1, 10 do
txn:put(cf, string.format("key:%04d", i), string.format("value:%04d", i))
end
txn:commit()
txn:free()
-- Purge column family (synchronous flush + compaction)
cf:purge()
-- Verify data is still readable after purge
local read_txn = db:begin_txn()
local v1 = read_txn:get(cf, "key:0001")
local v10 = read_txn:get(cf, "key:0010")
assert_eq(v1, "value:0001", "key:0001 should be readable after purge_cf")
assert_eq(v10, "value:0010", "key:0010 should be readable after purge_cf")
read_txn:free()
-- After purge, flushing and compacting should be done
assert_eq(cf:is_flushing(), false, "should not be flushing after purge")
assert_eq(cf:is_compacting(), false, "should not be compacting after purge")
db:drop_column_family("test_cf")
db:close()
cleanup_db(path)
print("PASS: test_purge_cf")
end
function tests.test_purge_db()
local path = "./test_db_purge_db"
cleanup_db(path)
local db = tidesdb.TidesDB.open(path, {
log_level = tidesdb.LogLevel.LOG_WARN,
})
db:create_column_family("cf_a")
db:create_column_family("cf_b")
local cf_a = db:get_column_family("cf_a")
local cf_b = db:get_column_family("cf_b")
-- Write data to both CFs