surrealmx 0.19.0

An embedded, in-memory, lock-free, transaction-based, key-value database engine
Documentation
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
#![cfg(not(target_arch = "wasm32"))]

use bytes::Bytes;
use std::time::Duration;
use surrealmx::{AolMode, Database, DatabaseOptions, FsyncMode, PersistenceOptions, SnapshotMode};
use tempfile::TempDir;

#[test]
fn test_aol_synchronous_basic() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	// Create database options
	let db_opts = DatabaseOptions::default();

	// Configure synchronous AOL persistence (no snapshots)
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::SynchronousOnCommit)
		.with_snapshot_mode(SnapshotMode::Never)
		.with_fsync_mode(FsyncMode::EveryAppend);

	// Create persistent database with AOL mode
	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Add some data
	{
		let mut tx = db.transaction(true);
		tx.set("key1", "value1").unwrap();
		tx.set("key2", "value2").unwrap();
		tx.commit().unwrap();
	}

	// Add more data in a separate transaction
	{
		let mut tx = db.transaction(true);
		tx.set("key3", "value3").unwrap();
		tx.del("key1").unwrap(); // Delete key1
		tx.commit().unwrap();
	}

	// Verify data is accessible in current session
	{
		let mut tx = db.transaction(false);
		assert_eq!(tx.get("key1").unwrap(), None); // Should be deleted
		assert_eq!(tx.get("key2").unwrap(), Some(Bytes::from("value2")));
		assert_eq!(tx.get("key3").unwrap(), Some(Bytes::from("value3")));
		tx.cancel().unwrap();
	}

	// Verify AOL file exists and snapshot doesn't
	let aol_path = temp_path.join("aol.bin");
	let snapshot_path = temp_path.join("snapshot.bin");

	assert!(aol_path.exists(), "AOL file should exist");
	assert!(!snapshot_path.exists(), "Snapshot file should not exist in AOL-only mode");

	std::thread::sleep(Duration::from_millis(250));

	// Verify the AOL file has content
	let aol_metadata = std::fs::metadata(&aol_path).unwrap();
	assert!(aol_metadata.len() > 0, "AOL file should not be empty");
}

#[test]
fn test_aol_asynchronous_basic() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	// Create database options
	let db_opts = DatabaseOptions::default();

	// Configure asynchronous AOL persistence (no snapshots)
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::AsynchronousAfterCommit)
		.with_snapshot_mode(SnapshotMode::Never)
		.with_fsync_mode(FsyncMode::Never);

	// Create persistent database with AOL mode
	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Add some data
	{
		let mut tx = db.transaction(true);
		tx.set("key1", "value1").unwrap();
		tx.set("key2", "value2").unwrap();
		tx.commit().unwrap();
	}

	// Add more data in a separate transaction
	{
		let mut tx = db.transaction(true);
		tx.set("key3", "value3").unwrap();
		tx.del("key1").unwrap(); // Delete key1
		tx.commit().unwrap();
	}

	// Verify data is accessible in current session
	{
		let mut tx = db.transaction(false);
		assert_eq!(tx.get("key1").unwrap(), None); // Should be deleted
		assert_eq!(tx.get("key2").unwrap(), Some(Bytes::from("value2")));
		assert_eq!(tx.get("key3").unwrap(), Some(Bytes::from("value3")));
		tx.cancel().unwrap();
	}

	// Verify AOL file exists and snapshot doesn't
	let aol_path = temp_path.join("aol.bin");
	let snapshot_path = temp_path.join("snapshot.bin");

	assert!(aol_path.exists(), "AOL file should exist");
	assert!(!snapshot_path.exists(), "Snapshot file should not exist in AOL-only mode");

	std::thread::sleep(Duration::from_millis(250));

	// Verify the AOL file has content
	let aol_metadata = std::fs::metadata(&aol_path).unwrap();
	assert!(aol_metadata.len() > 0, "AOL file should not be empty");
}

#[test]
fn test_aol_recovery() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	// Create database options
	let db_opts = DatabaseOptions::default();

	// Configure AOL persistence
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::SynchronousOnCommit)
		.with_snapshot_mode(SnapshotMode::Never)
		.with_fsync_mode(FsyncMode::EveryAppend);

	// Create first database instance and add data
	{
		let db = Database::new_with_persistence(db_opts.clone(), persistence_opts.clone()).unwrap();

		let mut tx = db.transaction(true);
		tx.set("recover_key1".to_string(), "recover_value1".to_string()).unwrap();
		tx.set("recover_key2".to_string(), "recover_value2".to_string()).unwrap();
		tx.commit().unwrap();

		// Update a key
		let mut tx = db.transaction(true);
		tx.set("recover_key1".to_string(), "updated_value1".to_string()).unwrap();
		tx.set("recover_key3".to_string(), "recover_value3".to_string()).unwrap();
		tx.commit().unwrap();

		// Delete a key
		let mut tx = db.transaction(true);
		tx.del("recover_key2".to_string()).unwrap();
		tx.commit().unwrap();
	} // Database drops here, releasing all resources

	// Create second database instance from the same directory (simulates restart)
	{
		let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

		// Verify data was recovered from AOL
		let mut tx = db.transaction(false);
		assert_eq!(tx.get("recover_key1").unwrap(), Some(Bytes::from("updated_value1")));
		assert_eq!(tx.get("recover_key2").unwrap(), None); // Should be deleted
		assert_eq!(tx.get("recover_key3").unwrap(), Some(Bytes::from("recover_value3")));
		tx.cancel().unwrap();
	}
}

#[test]
fn test_aol_fsync_modes() {
	// Test FsyncMode::EveryAppend
	{
		let temp_dir = TempDir::new().unwrap();
		let temp_path = temp_dir.path();

		let db_opts = DatabaseOptions::default();
		let persistence_opts = PersistenceOptions::new(temp_path)
			.with_aol_mode(AolMode::SynchronousOnCommit)
			.with_fsync_mode(FsyncMode::EveryAppend);

		let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

		let mut tx = db.transaction(true);
		tx.set(&b"key_1"[..], "fsync_every_append").unwrap();
		tx.commit().unwrap(); // Should fsync immediately
	}

	// Test FsyncMode::Interval
	{
		let temp_dir = TempDir::new().unwrap();
		let temp_path = temp_dir.path();

		let db_opts = DatabaseOptions::default();
		let persistence_opts = PersistenceOptions::new(temp_path)
			.with_aol_mode(AolMode::SynchronousOnCommit)
			.with_fsync_mode(FsyncMode::Interval(Duration::from_millis(100)));

		let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

		let mut tx = db.transaction(true);
		tx.set(&b"key_1"[..], "fsync_interval").unwrap();
		tx.commit().unwrap(); // Should not fsync immediately

		// Wait for interval to pass
		std::thread::sleep(Duration::from_millis(200));
	}

	// Test FsyncMode::Never
	{
		let temp_dir = TempDir::new().unwrap();
		let temp_path = temp_dir.path();

		let db_opts = DatabaseOptions::default();
		let persistence_opts = PersistenceOptions::new(temp_path)
			.with_aol_mode(AolMode::SynchronousOnCommit)
			.with_fsync_mode(FsyncMode::Never);

		let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

		let mut tx = db.transaction(true);
		tx.set(&b"key_1"[..], "fsync_never").unwrap();
		tx.commit().unwrap(); // Should never fsync
	}
}

#[test]
fn test_snapshot_manual_creation() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	// Create database options
	let db_opts = DatabaseOptions::default();

	// Configure manual snapshot persistence (no AOL, no automatic snapshots)
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::Never)
		.with_snapshot_mode(SnapshotMode::Never);

	// Create persistent database
	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Add some data
	{
		let mut tx = db.transaction(true);
		tx.set("snap_key1".to_string(), "snap_value1".to_string()).unwrap();
		tx.set("snap_key2".to_string(), "snap_value2".to_string()).unwrap();
		tx.commit().unwrap();
	}

	// Manually create a snapshot
	if let Some(persistence) = db.persistence() {
		persistence.snapshot().unwrap();
	}

	// Verify snapshot file exists and AOL doesn't
	let snapshot_path = temp_path.join("snapshot.bin");
	let aol_path = temp_path.join("aol.bin");

	assert!(snapshot_path.exists(), "Snapshot file should exist");
	assert!(!aol_path.exists(), "AOL file should not exist in snapshot-only mode");

	// Verify the snapshot file has content
	let snapshot_metadata = std::fs::metadata(&snapshot_path).unwrap();
	assert!(snapshot_metadata.len() > 0, "Snapshot file should not be empty");
}

#[test]
fn test_snapshot_only_persistence_basic() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	// Create database options
	let db_opts = DatabaseOptions::default();

	// Configure snapshot-only persistence (no AOL)
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::Never)
		.with_snapshot_mode(SnapshotMode::Interval(Duration::from_secs(60)));

	// Create persistent database with snapshot-only mode
	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Add some data
	{
		let mut tx = db.transaction(true);
		tx.put("key1".to_string(), "value1".to_string()).unwrap();
		tx.put("key2".to_string(), "value2".to_string()).unwrap();
		tx.commit().unwrap();
	}

	// Verify data is accessible in current session
	{
		let mut tx = db.transaction(false);
		assert_eq!(tx.get("key1").unwrap(), Some(Bytes::from("value1")));
		assert_eq!(tx.get("key2").unwrap(), Some(Bytes::from("value2")));
		tx.cancel().unwrap();
	}

	// Trigger a manual snapshot
	if let Some(persistence) = db.persistence() {
		persistence.snapshot().unwrap();
	}

	// Verify snapshot file exists but AOL file doesn't
	let snapshot_path = temp_path.join("snapshot.bin");
	let aol_path = temp_path.join("aol.bin");

	assert!(snapshot_path.exists(), "Snapshot file should exist");
	assert!(!aol_path.exists(), "AOL file should not exist in snapshot-only mode");

	// Verify the snapshot file has content
	let snapshot_metadata = std::fs::metadata(&snapshot_path).unwrap();
	assert!(snapshot_metadata.len() > 0, "Snapshot file should not be empty");

	println!("Successfully created snapshot file with {} bytes", snapshot_metadata.len());
}

#[test]
fn test_snapshot_basic() {
	// Test basic snapshot creation
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	let db_opts = DatabaseOptions::default();
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::Never)
		.with_snapshot_mode(SnapshotMode::Never);

	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Add test data
	{
		let mut tx = db.transaction(true);
		for i in 0..100 {
			tx.set(format!("key_{i}"), format!("value_{i}_with_some_data")).unwrap();
		}
		tx.commit().unwrap();
	}

	// Create snapshot
	if let Some(persistence) = db.persistence() {
		persistence.snapshot().unwrap();
	}

	// Verify snapshot file exists and has content
	let snapshot_path = temp_path.join("snapshot.bin");
	assert!(snapshot_path.exists(), "Snapshot file should exist");

	let metadata = std::fs::metadata(&snapshot_path).unwrap();
	assert!(metadata.len() > 0, "Snapshot file should not be empty");

	println!("Snapshot size = {} bytes", metadata.len());
}

#[test]
fn test_snapshot_recovery() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	let db_opts = DatabaseOptions::default();
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::Never)
		.with_snapshot_mode(SnapshotMode::Never);

	// Create first database instance and add data
	{
		let db = Database::new_with_persistence(db_opts.clone(), persistence_opts.clone()).unwrap();

		// Add initial data
		{
			let mut tx = db.transaction(true);
			tx.set("snapshot_key1".to_string(), "snapshot_value1".to_string()).unwrap();
			tx.set("snapshot_key2".to_string(), "snapshot_value2".to_string()).unwrap();
			tx.commit().unwrap();
		}

		// Update data
		{
			let mut tx = db.transaction(true);
			tx.set("snapshot_key1".to_string(), "updated_snapshot_value1".to_string()).unwrap();
			tx.set("snapshot_key3".to_string(), "snapshot_value3".to_string()).unwrap();
			tx.commit().unwrap();
		}

		// Delete data
		{
			let mut tx = db.transaction(true);
			tx.del("snapshot_key2".to_string()).unwrap();
			tx.commit().unwrap();
		}

		// Create snapshot before closing
		if let Some(persistence) = db.persistence() {
			persistence.snapshot().unwrap();
		}
	} // Database drops here

	// Create second database instance from the same directory (simulates restart)
	{
		let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

		// Verify data was recovered from snapshot
		let mut tx = db.transaction(false);
		assert_eq!(
			tx.get("snapshot_key1").unwrap().as_deref(),
			Some(b"updated_snapshot_value1" as &[u8])
		);
		assert_eq!(tx.get("snapshot_key2").unwrap(), None); // Should be deleted
		assert_eq!(tx.get("snapshot_key3").unwrap().as_deref(), Some(b"snapshot_value3" as &[u8]));
		tx.cancel().unwrap();
	}
}

#[test]
fn test_snapshot_interval() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	let db_opts = DatabaseOptions::default();
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::Never)
		.with_snapshot_mode(SnapshotMode::Interval(Duration::from_millis(100)));

	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Add some data
	{
		let mut tx = db.transaction(true);
		tx.set("interval_key1".to_string(), "interval_value1".to_string()).unwrap();
		tx.commit().unwrap();
	}

	// Wait for snapshot to be created automatically
	std::thread::sleep(Duration::from_millis(200));

	// Add more data
	{
		let mut tx = db.transaction(true);
		tx.set("interval_key2".to_string(), "interval_value2".to_string()).unwrap();
		tx.commit().unwrap();
	}

	// Wait for another snapshot
	std::thread::sleep(Duration::from_millis(200));

	// Verify snapshot file exists
	let snapshot_path = temp_path.join("snapshot.bin");
	assert!(snapshot_path.exists(), "Snapshot file should exist with interval snapshots");
}

#[test]
fn test_combined_aol_and_snapshot() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	let db_opts = DatabaseOptions::default();
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::SynchronousOnCommit)
		.with_snapshot_mode(SnapshotMode::Never) // Manual snapshots
		.with_fsync_mode(FsyncMode::EveryAppend);

	// Create first database instance
	{
		let db = Database::new_with_persistence(db_opts.clone(), persistence_opts.clone()).unwrap();

		// Add initial data (will go to AOL)
		{
			let mut tx = db.transaction(true);
			tx.set("combined_key1".to_string(), "combined_value1".to_string()).unwrap();
			tx.set("combined_key2".to_string(), "combined_value2".to_string()).unwrap();
			tx.commit().unwrap();
		}

		// Create a snapshot (should truncate AOL)
		if let Some(persistence) = db.persistence() {
			persistence.snapshot().unwrap();
		}

		// Add more data after snapshot (will go to AOL)
		{
			let mut tx = db.transaction(true);
			tx.set("combined_key3".to_string(), "combined_value3".to_string()).unwrap();
			tx.set("combined_key1".to_string(), "updated_combined_value1".to_string()).unwrap();
			tx.commit().unwrap();
		}
	} // Database drops here

	// Verify both snapshot and AOL files exist
	let snapshot_path = temp_path.join("snapshot.bin");
	let aol_path = temp_path.join("aol.bin");

	assert!(snapshot_path.exists(), "Snapshot file should exist");
	assert!(aol_path.exists(), "AOL file should exist");

	// Create second database instance (simulates restart)
	{
		let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

		// Verify data was recovered from both snapshot and AOL
		let mut tx = db.transaction(false);
		assert_eq!(
			tx.get("combined_key1").unwrap().as_deref(),
			Some(b"updated_combined_value1" as &[u8])
		);
		assert_eq!(tx.get("combined_key2").unwrap().as_deref(), Some(b"combined_value2" as &[u8]));
		assert_eq!(tx.get("combined_key3").unwrap().as_deref(), Some(b"combined_value3" as &[u8]));
		tx.cancel().unwrap();
	}
}

#[test]
fn test_aol_snapshot_with_truncation() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	let db_opts = DatabaseOptions::default();
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::SynchronousOnCommit)
		.with_snapshot_mode(SnapshotMode::Never)
		.with_fsync_mode(FsyncMode::EveryAppend);

	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Add data that will go to AOL
	{
		let mut tx = db.transaction(true);
		for i in 0..10 {
			tx.set(format!("key_{}", i), format!("value_{}", i)).unwrap();
		}
		tx.commit().unwrap();
	}

	// Check AOL file size before snapshot
	let aol_path = temp_path.join("aol.bin");
	let aol_size_before = std::fs::metadata(&aol_path).unwrap().len();
	assert!(aol_size_before > 0, "AOL should have content before snapshot");

	// Create snapshot (should truncate AOL)
	if let Some(persistence) = db.persistence() {
		persistence.snapshot().unwrap();
	}

	// Check AOL file size after snapshot (should be much smaller or empty)
	let aol_size_after = std::fs::metadata(&aol_path).unwrap().len();
	assert!(aol_size_after < aol_size_before, "AOL should be truncated after snapshot");

	// Add more data after snapshot
	{
		let mut tx = db.transaction(true);
		tx.set("post_snapshot_key".to_string(), "post_snapshot_value".to_string()).unwrap();
		tx.commit().unwrap();
	}

	// Verify snapshot exists
	let snapshot_path = temp_path.join("snapshot.bin");
	assert!(snapshot_path.exists(), "Snapshot file should exist");

	// Verify all data is still accessible
	{
		let mut tx = db.transaction(false);
		for i in 0..10 {
			assert_eq!(
				tx.get(format!("key_{}", i)).unwrap(),
				Some(Bytes::from(format!("value_{}", i)))
			);
		}
		assert_eq!(tx.get("post_snapshot_key").unwrap(), Some(Bytes::from("post_snapshot_value")));
		tx.cancel().unwrap();
	}
}

#[test]
fn test_combined_recovery_complex() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	let db_opts = DatabaseOptions::default();
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::SynchronousOnCommit)
		.with_snapshot_mode(SnapshotMode::Never)
		.with_fsync_mode(FsyncMode::EveryAppend);

	// First session: Add data, snapshot, add more data
	{
		let db = Database::new_with_persistence(db_opts.clone(), persistence_opts.clone()).unwrap();

		// Phase 1: Add initial data
		{
			let mut tx = db.transaction(true);
			tx.set("phase1_key1".to_string(), "phase1_value1".to_string()).unwrap();
			tx.set("phase1_key2".to_string(), "phase1_value2".to_string()).unwrap();
			tx.commit().unwrap();
		}

		// Phase 2: Update and delete some data
		{
			let mut tx = db.transaction(true);
			tx.set("phase1_key1".to_string(), "updated_phase1_value1".to_string()).unwrap();
			tx.del("phase1_key2".to_string()).unwrap();
			tx.set("phase2_key1".to_string(), "phase2_value1".to_string()).unwrap();
			tx.commit().unwrap();
		}

		// Create snapshot
		if let Some(persistence) = db.persistence() {
			persistence.snapshot().unwrap();
		}

		// Phase 3: Add data after snapshot
		{
			let mut tx = db.transaction(true);
			tx.set("phase3_key1".to_string(), "phase3_value1".to_string()).unwrap();
			tx.set("phase1_key1".to_string(), "final_phase1_value1".to_string()).unwrap();
			tx.commit().unwrap();
		}
	} // First session ends

	// Second session: Verify all data is recovered correctly
	{
		let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

		let mut tx = db.transaction(false);
		assert_eq!(
			tx.get("phase1_key1").unwrap().as_deref(),
			Some(b"final_phase1_value1" as &[u8])
		);
		assert_eq!(tx.get("phase1_key2").unwrap(), None); // Should be deleted
		assert_eq!(tx.get("phase2_key1").unwrap(), Some(Bytes::from("phase2_value1")));
		assert_eq!(tx.get("phase3_key1").unwrap(), Some(Bytes::from("phase3_value1")));
		tx.cancel().unwrap();
	}
}

#[test]
fn test_custom_file_paths() {
	// Create a temporary directory for testing
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	// Create custom subdirectories
	let aol_dir = temp_path.join("logs");
	let snapshot_dir = temp_path.join("snapshots");
	std::fs::create_dir_all(&aol_dir).unwrap();
	std::fs::create_dir_all(&snapshot_dir).unwrap();

	let db_opts = DatabaseOptions::default();
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::SynchronousOnCommit)
		.with_snapshot_mode(SnapshotMode::Never)
		.with_aol_path(aol_dir.join("custom.aol"))
		.with_snapshot_path(snapshot_dir.join("custom.snapshot"));

	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Add some data
	{
		let mut tx = db.transaction(true);
		tx.set("custom_key".to_string(), "custom_value".to_string()).unwrap();
		tx.commit().unwrap();
	}

	// Create snapshot
	if let Some(persistence) = db.persistence() {
		persistence.snapshot().unwrap();
	}

	// Verify files exist at custom paths
	let custom_aol_path = aol_dir.join("custom.aol");
	let custom_snapshot_path = snapshot_dir.join("custom.snapshot");

	assert!(custom_aol_path.exists(), "Custom AOL file should exist");
	assert!(custom_snapshot_path.exists(), "Custom snapshot file should exist");
}

#[test]
fn test_persistence_options_builder() {
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	// Test fluent builder pattern
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::AsynchronousAfterCommit)
		.with_snapshot_mode(SnapshotMode::Interval(Duration::from_secs(1)))
		.with_fsync_mode(FsyncMode::Interval(Duration::from_millis(500)));

	// Verify the options were set correctly
	assert_eq!(persistence_opts.aol_mode, AolMode::AsynchronousAfterCommit);
	assert_eq!(persistence_opts.snapshot_mode, SnapshotMode::Interval(Duration::from_secs(1)));
	assert_eq!(persistence_opts.fsync_mode, FsyncMode::Interval(Duration::from_millis(500)));

	// Test that database can be created with these options
	let db_opts = DatabaseOptions::default();
	let _db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();
}

#[test]
fn test_readonly_operations_no_persistence() {
	// Test that read-only operations work even with persistence configured
	let temp_dir = TempDir::new().unwrap();
	let temp_path = temp_dir.path();

	let db_opts = DatabaseOptions::default();
	let persistence_opts = PersistenceOptions::new(temp_path)
		.with_aol_mode(AolMode::SynchronousOnCommit)
		.with_snapshot_mode(SnapshotMode::Never);

	let db = Database::new_with_persistence(db_opts, persistence_opts).unwrap();

	// Perform read-only operations (should not trigger persistence)
	{
		let mut tx = db.transaction(false);
		assert_eq!(tx.get("non_existent_key").unwrap(), None);
		assert!(!tx.exists("non_existent_key").unwrap());
		tx.cancel().unwrap();
	}

	// Verify no persistence files were created for read-only operations
	let aol_path = temp_path.join("aol.bin");
	let snapshot_path = temp_path.join("snapshot.bin");

	// AOL file might exist but should be empty, snapshot should not exist
	if aol_path.exists() {
		let metadata = std::fs::metadata(&aol_path).unwrap();
		assert_eq!(metadata.len(), 0, "AOL file should be empty after read-only operations");
	}
	assert!(!snapshot_path.exists(), "Snapshot file should not exist after read-only operations");
}