oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
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
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
//! Write-Ahead Logging (WAL) for crash recovery
//!
//! This module provides comprehensive write-ahead logging for vector index operations,
//! enabling crash recovery and ensuring data durability. The WAL records all modifications
//! before they are applied to the index, allowing the system to recover from crashes by
//! replaying the log.
//!
//! # Features
//!
//! - Transaction-based logging
//! - Automatic crash recovery
//! - Log compaction and checkpointing
//! - Concurrent write support with proper synchronization
//! - Configurable fsync behavior for performance tuning
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────┐
//! │ Index Ops   │
//! └──────┬──────┘
//!//!//! ┌─────────────┐     ┌──────────────┐
//! │ WAL Writer  │────▶│ Log File     │
//! └─────────────┘     └──────────────┘
//!        │                    │
//!        │                    │ (on crash)
//!        ▼                    ▼
//! ┌─────────────┐     ┌──────────────┐
//! │ Index       │◀────│ WAL Recovery │
//! └─────────────┘     └──────────────┘
//! ```

use anyhow::{anyhow, Result};
use oxicode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

/// WAL magic number for file format validation
const WAL_MAGIC: &[u8; 4] = b"WALV"; // WAL Vector

/// WAL format version
const WAL_VERSION: u32 = 1;

/// Write-Ahead Log entry representing a single operation
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub enum WalEntry {
    /// Insert a new vector
    Insert {
        id: String,
        vector: Vec<f32>,
        metadata: Option<HashMap<String, String>>,
        timestamp: u64,
    },
    /// Update an existing vector
    Update {
        id: String,
        vector: Vec<f32>,
        metadata: Option<HashMap<String, String>>,
        timestamp: u64,
    },
    /// Delete a vector
    Delete { id: String, timestamp: u64 },
    /// Batch operation (multiple entries)
    Batch {
        entries: Vec<WalEntry>,
        timestamp: u64,
    },
    /// Checkpoint marker (all operations before this are persisted)
    Checkpoint {
        sequence_number: u64,
        timestamp: u64,
    },
    /// Transaction begin
    BeginTransaction { transaction_id: u64, timestamp: u64 },
    /// Transaction commit
    CommitTransaction { transaction_id: u64, timestamp: u64 },
    /// Transaction abort
    AbortTransaction { transaction_id: u64, timestamp: u64 },
}

impl WalEntry {
    /// Get the timestamp of this entry
    pub fn timestamp(&self) -> u64 {
        match self {
            WalEntry::Insert { timestamp, .. }
            | WalEntry::Update { timestamp, .. }
            | WalEntry::Delete { timestamp, .. }
            | WalEntry::Batch { timestamp, .. }
            | WalEntry::Checkpoint { timestamp, .. }
            | WalEntry::BeginTransaction { timestamp, .. }
            | WalEntry::CommitTransaction { timestamp, .. }
            | WalEntry::AbortTransaction { timestamp, .. } => *timestamp,
        }
    }

    /// Check if this is a checkpoint entry
    pub fn is_checkpoint(&self) -> bool {
        matches!(self, WalEntry::Checkpoint { .. })
    }
}

/// WAL configuration
#[derive(Debug, Clone)]
pub struct WalConfig {
    /// Directory where WAL files are stored
    pub wal_directory: PathBuf,
    /// Maximum size of a single WAL file before rotation (in bytes)
    pub max_file_size: u64,
    /// Whether to call fsync after each write (slower but safer)
    pub sync_on_write: bool,
    /// Checkpoint interval (number of operations)
    pub checkpoint_interval: u64,
    /// Keep this many checkpoint files
    pub checkpoint_retention: usize,
    /// Buffer size for WAL writes
    pub buffer_size: usize,
}

impl Default for WalConfig {
    fn default() -> Self {
        Self {
            wal_directory: PathBuf::from("./wal"),
            max_file_size: 100 * 1024 * 1024, // 100MB
            sync_on_write: false,             // Better performance, acceptable risk
            checkpoint_interval: 10000,
            checkpoint_retention: 3,
            buffer_size: 64 * 1024, // 64KB buffer
        }
    }
}

/// Write-Ahead Log manager
pub struct WalManager {
    config: WalConfig,
    current_file: Arc<Mutex<Option<BufWriter<File>>>>,
    current_file_path: Arc<Mutex<PathBuf>>,
    sequence_number: Arc<Mutex<u64>>,
    last_checkpoint: Arc<Mutex<u64>>,
}

impl WalManager {
    /// Create a new WAL manager
    pub fn new(config: WalConfig) -> Result<Self> {
        // Ensure WAL directory exists
        std::fs::create_dir_all(&config.wal_directory)?;

        let manager = Self {
            config,
            current_file: Arc::new(Mutex::new(None)),
            current_file_path: Arc::new(Mutex::new(PathBuf::new())),
            sequence_number: Arc::new(Mutex::new(0)),
            last_checkpoint: Arc::new(Mutex::new(0)),
        };

        // Open or create the current WAL file
        manager.rotate_wal_file()?;

        Ok(manager)
    }

    /// Append an entry to the WAL
    pub fn append(&self, entry: WalEntry) -> Result<u64> {
        let seq = {
            let mut seq_guard = self
                .sequence_number
                .lock()
                .expect("mutex lock should not be poisoned");
            let seq = *seq_guard;
            *seq_guard += 1;
            seq
        };

        // Write to file
        let needs_checkpoint = {
            let mut file_guard = self
                .current_file
                .lock()
                .expect("mutex lock should not be poisoned");

            if let Some(ref mut writer) = *file_guard {
                // Serialize the entry
                let entry_bytes =
                    oxicode::serde::encode_to_vec(&entry, oxicode::config::standard())
                        .map_err(|e| anyhow!("Failed to serialize WAL entry: {}", e))?;
                let entry_len = entry_bytes.len() as u32;

                // Write sequence number, length, and data
                writer.write_all(&seq.to_le_bytes())?;
                writer.write_all(&entry_len.to_le_bytes())?;
                writer.write_all(&entry_bytes)?;

                if self.config.sync_on_write {
                    writer.flush()?;
                    writer.get_ref().sync_all()?;
                }

                // Check if file rotation is needed
                let needs_rotation = if let Ok(metadata) = writer.get_ref().metadata() {
                    metadata.len() >= self.config.max_file_size
                } else {
                    false
                };

                if needs_rotation {
                    drop(file_guard);
                    self.rotate_wal_file()?;
                }

                // Check if checkpoint is needed
                let last_checkpoint = *self
                    .last_checkpoint
                    .lock()
                    .expect("mutex lock should not be poisoned");
                seq - last_checkpoint >= self.config.checkpoint_interval
            } else {
                return Err(anyhow!("WAL file not open"));
            }
        };

        // Checkpoint outside of lock
        if needs_checkpoint {
            self.checkpoint(seq)?;
        }

        Ok(seq)
    }

    /// Create a checkpoint
    pub fn checkpoint(&self, sequence_number: u64) -> Result<()> {
        tracing::info!("Creating WAL checkpoint at sequence {}", sequence_number);

        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time should be after UNIX_EPOCH")
            .as_secs();

        let checkpoint_entry = WalEntry::Checkpoint {
            sequence_number,
            timestamp,
        };

        // Write checkpoint directly without going through append() to avoid recursion
        let seq = {
            let mut seq_guard = self
                .sequence_number
                .lock()
                .expect("mutex lock should not be poisoned");
            let seq = *seq_guard;
            *seq_guard += 1;
            seq
        };

        {
            let mut file_guard = self
                .current_file
                .lock()
                .expect("mutex lock should not be poisoned");
            if let Some(ref mut writer) = *file_guard {
                let entry_bytes =
                    oxicode::serde::encode_to_vec(&checkpoint_entry, oxicode::config::standard())
                        .map_err(|e| anyhow!("Failed to serialize checkpoint entry: {}", e))?;
                let entry_len = entry_bytes.len() as u32;

                writer.write_all(&seq.to_le_bytes())?;
                writer.write_all(&entry_len.to_le_bytes())?;
                writer.write_all(&entry_bytes)?;

                if self.config.sync_on_write {
                    writer.flush()?;
                    writer.get_ref().sync_all()?;
                }
            }
        }

        let mut last_checkpoint = self
            .last_checkpoint
            .lock()
            .expect("mutex lock should not be poisoned");
        *last_checkpoint = sequence_number;

        // Cleanup old WAL files
        self.cleanup_old_files()?;

        Ok(())
    }

    /// Rotate to a new WAL file
    fn rotate_wal_file(&self) -> Result<()> {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time should be after UNIX_EPOCH")
            .as_secs();

        let filename = format!("wal-{:016x}.log", timestamp);
        let filepath = self.config.wal_directory.join(&filename);

        tracing::info!("Rotating WAL to new file: {:?}", filepath);

        let file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&filepath)?;

        let mut writer = BufWriter::with_capacity(self.config.buffer_size, file);

        // Write WAL file header
        writer.write_all(WAL_MAGIC)?;
        writer.write_all(&WAL_VERSION.to_le_bytes())?;
        writer.write_all(&timestamp.to_le_bytes())?;

        if self.config.sync_on_write {
            writer.flush()?;
            writer.get_ref().sync_all()?;
        }

        let mut file_guard = self
            .current_file
            .lock()
            .expect("mutex lock should not be poisoned");
        let mut path_guard = self
            .current_file_path
            .lock()
            .expect("mutex lock should not be poisoned");

        // Flush and close old file
        if let Some(mut old_writer) = file_guard.take() {
            old_writer.flush()?;
        }

        *file_guard = Some(writer);
        *path_guard = filepath;

        Ok(())
    }

    /// Clean up old WAL files (keep only recent checkpoints)
    fn cleanup_old_files(&self) -> Result<()> {
        let mut wal_files: Vec<_> = std::fs::read_dir(&self.config.wal_directory)?
            .filter_map(|entry| entry.ok())
            .filter(|entry| {
                entry
                    .file_name()
                    .to_str()
                    .map(|s| s.starts_with("wal-") && s.ends_with(".log"))
                    .unwrap_or(false)
            })
            .collect();

        // Sort by filename (timestamp-based)
        wal_files.sort_by_key(|entry| entry.file_name());

        // Keep the most recent files
        if wal_files.len() > self.config.checkpoint_retention {
            let to_remove = wal_files.len() - self.config.checkpoint_retention;
            for entry in wal_files.iter().take(to_remove) {
                tracing::info!("Removing old WAL file: {:?}", entry.path());
                std::fs::remove_file(entry.path())?;
            }
        }

        Ok(())
    }

    /// Recover from WAL files
    pub fn recover(&self) -> Result<Vec<WalEntry>> {
        tracing::info!("Starting WAL recovery");

        let mut all_entries = Vec::new();
        let mut last_checkpoint_seq = 0u64;

        // Find all WAL files
        let mut wal_files: Vec<_> = std::fs::read_dir(&self.config.wal_directory)?
            .filter_map(|entry| entry.ok())
            .filter(|entry| {
                entry
                    .file_name()
                    .to_str()
                    .map(|s| s.starts_with("wal-") && s.ends_with(".log"))
                    .unwrap_or(false)
            })
            .collect();

        // Sort by filename (timestamp-based)
        wal_files.sort_by_key(|entry| entry.file_name());

        // Read all WAL files
        for entry in wal_files {
            let path = entry.path();
            tracing::debug!("Reading WAL file: {:?}", path);

            let file = File::open(&path)?;
            let mut reader = BufReader::new(file);

            // Verify magic number
            let mut magic = [0u8; 4];
            reader.read_exact(&mut magic)?;
            if &magic != WAL_MAGIC {
                tracing::warn!("Invalid WAL file magic number: {:?}", path);
                continue;
            }

            // Read version
            let mut version_bytes = [0u8; 4];
            reader.read_exact(&mut version_bytes)?;
            let version = u32::from_le_bytes(version_bytes);
            if version != WAL_VERSION {
                tracing::warn!("Unsupported WAL version {} in {:?}", version, path);
                continue;
            }

            // Read file timestamp
            let mut timestamp_bytes = [0u8; 8];
            reader.read_exact(&mut timestamp_bytes)?;

            // Read entries with robust error handling for incomplete writes
            loop {
                // Read sequence number
                let mut seq_bytes = [0u8; 8];
                match reader.read_exact(&mut seq_bytes) {
                    Ok(_) => {}
                    Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
                        tracing::debug!("Reached end of WAL file (expected)");
                        break;
                    }
                    Err(e) => return Err(e.into()),
                }
                let seq = u64::from_le_bytes(seq_bytes);

                // Read entry length
                let mut len_bytes = [0u8; 4];
                match reader.read_exact(&mut len_bytes) {
                    Ok(_) => {}
                    Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
                        tracing::warn!(
                            "Incomplete entry at sequence {}: missing length field. Skipping rest of file.",
                            seq
                        );
                        break;
                    }
                    Err(e) => return Err(e.into()),
                }
                let len = u32::from_le_bytes(len_bytes);

                // Sanity check on entry length (prevent excessive memory allocation)
                if len > 100_000_000 {
                    // 100MB max entry size
                    tracing::warn!(
                        "Entry at sequence {} has suspicious length {}. Possibly corrupted. Skipping.",
                        seq,
                        len
                    );
                    break;
                }

                // Read entry data
                let mut entry_bytes = vec![0u8; len as usize];
                match reader.read_exact(&mut entry_bytes) {
                    Ok(_) => {}
                    Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
                        tracing::warn!(
                            "Incomplete entry at sequence {}: expected {} bytes but reached EOF. Skipping rest of file.",
                            seq,
                            len
                        );
                        break;
                    }
                    Err(e) => return Err(e.into()),
                }

                // Deserialize entry
                let entry: WalEntry = match oxicode::serde::decode_from_slice(
                    &entry_bytes,
                    oxicode::config::standard(),
                ) {
                    Ok((e, _)) => e,
                    Err(e) => {
                        tracing::warn!(
                            "Failed to deserialize entry at sequence {}: {}. Skipping entry.",
                            seq,
                            e
                        );
                        continue; // Skip corrupted entry but continue reading
                    }
                };

                // Track last checkpoint
                if let WalEntry::Checkpoint {
                    sequence_number, ..
                } = &entry
                {
                    last_checkpoint_seq = *sequence_number;
                }

                all_entries.push((seq, entry));
            }
        }

        // Filter entries after last checkpoint
        // Note: If last_checkpoint_seq == 0 (no checkpoint), recover all entries including seq 0
        // Otherwise, only recover entries strictly after the checkpoint
        let recovered_entries: Vec<_> = all_entries
            .iter()
            .filter(|(seq, _)| {
                if last_checkpoint_seq == 0 {
                    true // No checkpoint, recover everything
                } else {
                    *seq > last_checkpoint_seq // Checkpoint exists, only after it
                }
            })
            .map(|(_, entry)| entry.clone())
            .collect();

        tracing::info!(
            "Recovered {} entries from WAL (after checkpoint {})",
            recovered_entries.len(),
            last_checkpoint_seq
        );

        // Update sequence number based on the maximum sequence number seen
        if let Some((max_seq, _)) = all_entries.iter().max_by_key(|(seq, _)| seq) {
            let mut seq = self
                .sequence_number
                .lock()
                .expect("mutex lock should not be poisoned");
            *seq = max_seq + 1;
        }

        Ok(recovered_entries)
    }

    /// Flush all pending writes to disk
    pub fn flush(&self) -> Result<()> {
        let mut file_guard = self
            .current_file
            .lock()
            .expect("mutex lock should not be poisoned");
        if let Some(ref mut writer) = *file_guard {
            writer.flush()?;
            writer.get_ref().sync_all()?;
        }
        Ok(())
    }

    /// Get current sequence number
    pub fn current_sequence(&self) -> u64 {
        *self
            .sequence_number
            .lock()
            .expect("mutex lock should not be poisoned")
    }

    /// Get last checkpoint sequence number
    pub fn last_checkpoint_sequence(&self) -> u64 {
        *self
            .last_checkpoint
            .lock()
            .expect("mutex lock should not be poisoned")
    }
}

impl Drop for WalManager {
    fn drop(&mut self) {
        // Ensure all data is flushed on drop
        let _ = self.flush();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;
    use tempfile::TempDir;

    #[test]
    fn test_wal_creation() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let config = WalConfig {
            wal_directory: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let wal = WalManager::new(config)?;
        assert_eq!(wal.current_sequence(), 0);
        Ok(())
    }

    #[test]
    fn test_wal_append() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let config = WalConfig {
            wal_directory: temp_dir.path().to_path_buf(),
            sync_on_write: true,
            ..Default::default()
        };

        let wal = WalManager::new(config)?;

        let entry = WalEntry::Insert {
            id: "vec1".to_string(),
            vector: vec![1.0, 2.0, 3.0],
            metadata: None,
            timestamp: 12345,
        };

        let seq = wal.append(entry)?;
        assert_eq!(seq, 0);
        Ok(())
    }

    #[test]
    fn test_wal_recovery() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let config = WalConfig {
            wal_directory: temp_dir.path().to_path_buf(),
            sync_on_write: true,
            checkpoint_interval: 100,
            ..Default::default()
        };

        // Write some entries
        {
            let wal = WalManager::new(config.clone())?;

            for i in 0..5 {
                let entry = WalEntry::Insert {
                    id: format!("vec{}", i),
                    vector: vec![i as f32, (i * 2) as f32],
                    metadata: None,
                    timestamp: (i + 1) * 1000, // Use unique timestamps
                };
                wal.append(entry)?;
            }

            wal.flush()?;
            // Ensure Drop is called to flush everything
            drop(wal);
        }

        // Small delay to ensure file is written
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Recover
        {
            let wal = WalManager::new(config)?;
            let recovered = wal.recover()?;

            // Should recover 5 entries
            assert_eq!(
                recovered.len(),
                5,
                "Expected exactly 5 entries, got {}",
                recovered.len()
            );

            // Verify all timestamps are present
            let timestamps: Vec<u64> = recovered.iter().map(|e| e.timestamp()).collect();
            assert_eq!(timestamps, vec![1000, 2000, 3000, 4000, 5000]);
        }
        Ok(())
    }

    #[test]
    fn test_wal_checkpoint() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let config = WalConfig {
            wal_directory: temp_dir.path().to_path_buf(),
            sync_on_write: true,
            checkpoint_interval: 3,
            ..Default::default()
        };

        let wal = WalManager::new(config)?;

        // Write entries (should trigger checkpoint)
        for i in 0..5 {
            let entry = WalEntry::Insert {
                id: format!("vec{}", i),
                vector: vec![i as f32],
                metadata: None,
                timestamp: i,
            };
            wal.append(entry)?;
        }

        assert!(wal.last_checkpoint_sequence() > 0);
        Ok(())
    }

    #[test]
    fn test_wal_batch_operation() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let config = WalConfig {
            wal_directory: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let wal = WalManager::new(config)?;

        let batch = WalEntry::Batch {
            entries: vec![
                WalEntry::Insert {
                    id: "vec1".to_string(),
                    vector: vec![1.0],
                    metadata: None,
                    timestamp: 1,
                },
                WalEntry::Update {
                    id: "vec2".to_string(),
                    vector: vec![2.0],
                    metadata: None,
                    timestamp: 2,
                },
            ],
            timestamp: 3,
        };

        wal.append(batch)?;
        wal.flush()?;
        Ok(())
    }

    #[test]
    fn test_wal_transaction() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let config = WalConfig {
            wal_directory: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let wal = WalManager::new(config)?;

        // Begin transaction
        wal.append(WalEntry::BeginTransaction {
            transaction_id: 1,
            timestamp: 100,
        })?;

        // Operations
        wal.append(WalEntry::Insert {
            id: "vec1".to_string(),
            vector: vec![1.0],
            metadata: None,
            timestamp: 101,
        })?;

        // Commit transaction
        wal.append(WalEntry::CommitTransaction {
            transaction_id: 1,
            timestamp: 102,
        })?;

        wal.flush()?;
        Ok(())
    }
}