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
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
//! FAISS Compatibility Layer
//!
//! This module provides compatibility with Facebook AI Similarity Search (FAISS)
//! library, enabling import/export of vector indexes to/from FAISS format.
//! This allows seamless integration with the broader ML ecosystem.

use crate::{
    hnsw::{HnswConfig, HnswIndex},
    ivf::{IvfConfig, IvfIndex},
    similarity::SimilarityMetric,
    Vector, VectorIndex,
};
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::Path;

/// FAISS index types that we support
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FaissIndexType {
    /// Flat (brute force) index
    IndexFlatL2,
    IndexFlatIP,
    /// IVF with flat quantizer
    IndexIVFFlat,
    /// IVF with product quantization
    IndexIVFPQ,
    /// Hierarchical NSW (HNSW)
    IndexHNSWFlat,
    /// LSH (Locality Sensitive Hashing)
    IndexLSH,
    /// PCA + flat index
    IndexPCAFlat,
}

/// FAISS index metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FaissIndexMetadata {
    pub index_type: FaissIndexType,
    pub dimension: usize,
    pub num_vectors: usize,
    pub metric_type: FaissMetricType,
    pub parameters: HashMap<String, FaissParameter>,
    pub version: String,
    pub created_at: String,
}

/// FAISS metric types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FaissMetricType {
    /// L2 (Euclidean) distance
    L2,
    /// Inner product (dot product)
    InnerProduct,
    /// Cosine similarity (normalized inner product)
    Cosine,
}

/// FAISS parameter values
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FaissParameter {
    Integer(i64),
    Float(f64),
    String(String),
    Boolean(bool),
}

/// FAISS compatibility layer for vector indexes
pub struct FaissCompatibility {
    supported_formats: Vec<FaissIndexType>,
    conversion_cache: HashMap<String, ConversionResult>,
}

/// Result of index conversion
#[derive(Debug, Clone)]
pub struct ConversionResult {
    pub success: bool,
    pub metadata: FaissIndexMetadata,
    pub performance_metrics: ConversionMetrics,
    pub warnings: Vec<String>,
}

/// Conversion performance metrics
#[derive(Debug, Clone, Default)]
pub struct ConversionMetrics {
    pub conversion_time: std::time::Duration,
    pub memory_used: usize,
    pub vectors_processed: usize,
    pub accuracy_preserved: f32, // 0.0 to 1.0
}

/// FAISS export configuration
#[derive(Debug, Clone)]
pub struct FaissExportConfig {
    pub target_format: FaissIndexType,
    pub compression_level: CompressionLevel,
    pub preserve_accuracy: bool,
    pub include_metadata: bool,
    pub chunk_size: usize,
}

/// FAISS import configuration
#[derive(Debug, Clone)]
pub struct FaissImportConfig {
    pub validate_format: bool,
    pub preserve_performance: bool,
    pub rebuild_if_incompatible: bool,
    pub batch_size: usize,
}

/// Compression levels for export
#[derive(Debug, Clone, Copy)]
pub enum CompressionLevel {
    None,
    Low,
    Medium,
    High,
    Maximum,
}

impl Default for FaissExportConfig {
    fn default() -> Self {
        Self {
            target_format: FaissIndexType::IndexHNSWFlat,
            compression_level: CompressionLevel::Medium,
            preserve_accuracy: true,
            include_metadata: true,
            chunk_size: 10000,
        }
    }
}

impl Default for FaissImportConfig {
    fn default() -> Self {
        Self {
            validate_format: true,
            preserve_performance: true,
            rebuild_if_incompatible: false,
            batch_size: 5000,
        }
    }
}

impl FaissCompatibility {
    /// Create a new FAISS compatibility layer
    pub fn new() -> Self {
        Self {
            supported_formats: vec![
                FaissIndexType::IndexFlatL2,
                FaissIndexType::IndexFlatIP,
                FaissIndexType::IndexIVFFlat,
                FaissIndexType::IndexIVFPQ,
                FaissIndexType::IndexHNSWFlat,
                FaissIndexType::IndexLSH,
            ],
            conversion_cache: HashMap::new(),
        }
    }

    /// Export an oxirs-vec index to FAISS format
    pub fn export_to_faiss<T: VectorIndex>(
        &mut self,
        index: &T,
        output_path: &Path,
        config: &FaissExportConfig,
    ) -> Result<ConversionResult> {
        let start_time = std::time::Instant::now();
        let mut warnings = Vec::new();

        // Detect the appropriate FAISS format for the index
        let detected_format = self.detect_optimal_faiss_format(index)?;
        let target_format = if detected_format != config.target_format {
            warnings.push(format!(
                "Requested format {:?} differs from optimal format {:?}",
                config.target_format, detected_format
            ));
            config.target_format
        } else {
            detected_format
        };

        // Create metadata
        let metadata = FaissIndexMetadata {
            index_type: target_format,
            dimension: self.get_index_dimension(index)?,
            num_vectors: self.get_index_size(index)?,
            metric_type: self.convert_similarity_metric(self.get_index_metric(index)?),
            parameters: self.extract_index_parameters(index, target_format)?,
            version: "oxirs-vec-1.0".to_string(),
            created_at: chrono::Utc::now().to_rfc3339(),
        };

        // Export the index data
        self.write_faiss_index(index, output_path, &metadata, config)?;

        let conversion_time = start_time.elapsed();
        let performance_metrics = ConversionMetrics {
            conversion_time,
            memory_used: self.estimate_memory_usage(&metadata),
            vectors_processed: metadata.num_vectors,
            accuracy_preserved: self.estimate_accuracy_preservation(target_format, config),
        };

        let result = ConversionResult {
            success: true,
            metadata,
            performance_metrics,
            warnings,
        };

        // Cache the result
        let cache_key = format!("{:?}-{}", target_format, output_path.display());
        self.conversion_cache.insert(cache_key, result.clone());

        Ok(result)
    }

    /// Import a FAISS index to oxirs-vec format
    pub fn import_from_faiss(
        &mut self,
        input_path: &Path,
        config: &FaissImportConfig,
    ) -> Result<Box<dyn VectorIndex>> {
        // Read and validate FAISS metadata
        let metadata = self.read_faiss_metadata(input_path)?;

        if config.validate_format && !self.is_format_supported(&metadata.index_type) {
            return Err(anyhow!(
                "Unsupported FAISS format: {:?}",
                metadata.index_type
            ));
        }

        // Create appropriate oxirs-vec index based on FAISS type
        match metadata.index_type {
            FaissIndexType::IndexHNSWFlat => self.import_hnsw_index(input_path, &metadata, config),
            FaissIndexType::IndexIVFFlat | FaissIndexType::IndexIVFPQ => {
                self.import_ivf_index(input_path, &metadata, config)
            }
            FaissIndexType::IndexFlatL2 | FaissIndexType::IndexFlatIP => {
                self.import_flat_index(input_path, &metadata, config)
            }
            _ => Err(anyhow!(
                "Import not yet implemented for {:?}",
                metadata.index_type
            )),
        }
    }

    /// Convert HNSW index to FAISS format
    fn export_hnsw_to_faiss(
        &self,
        index: &HnswIndex,
        output_path: &Path,
        metadata: &FaissIndexMetadata,
        config: &FaissExportConfig,
    ) -> Result<()> {
        let file = File::create(output_path)?;
        let mut writer = BufWriter::new(file);

        // Write FAISS header
        self.write_faiss_header(&mut writer, metadata)?;

        // Write HNSW-specific data
        self.write_hnsw_data(&mut writer, index, config)?;

        // Write vectors in chunks
        self.write_vectors_chunked(&mut writer, index, config.chunk_size)?;

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

    /// Convert IVF index to FAISS format
    fn export_ivf_to_faiss(
        &self,
        index: &IvfIndex,
        output_path: &Path,
        metadata: &FaissIndexMetadata,
        config: &FaissExportConfig,
    ) -> Result<()> {
        let file = File::create(output_path)?;
        let mut writer = BufWriter::new(file);

        // Write FAISS header
        self.write_faiss_header(&mut writer, metadata)?;

        // Write IVF-specific data
        self.write_ivf_data(&mut writer, index, config)?;

        // Write centroids and inverted lists
        self.write_ivf_structure(&mut writer, index)?;

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

    /// Import HNSW index from FAISS format
    fn import_hnsw_index(
        &self,
        input_path: &Path,
        metadata: &FaissIndexMetadata,
        config: &FaissImportConfig,
    ) -> Result<Box<dyn VectorIndex>> {
        let file = File::open(input_path)?;
        let mut reader = BufReader::new(file);

        // Skip FAISS header
        self.skip_faiss_header(&mut reader)?;

        // Read HNSW configuration
        let hnsw_config = self.read_hnsw_config(&mut reader, metadata)?;

        // Create new HNSW index
        let mut index = HnswIndex::new(hnsw_config)?;

        // Read and import vectors in batches
        self.import_vectors_batched(&mut reader, &mut index, config.batch_size)?;

        Ok(Box::new(index))
    }

    /// Import IVF index from FAISS format
    fn import_ivf_index(
        &self,
        input_path: &Path,
        metadata: &FaissIndexMetadata,
        config: &FaissImportConfig,
    ) -> Result<Box<dyn VectorIndex>> {
        let file = File::open(input_path)?;
        let mut reader = BufReader::new(file);

        // Skip FAISS header
        self.skip_faiss_header(&mut reader)?;

        // Read IVF configuration
        let ivf_config = self.read_ivf_config(&mut reader, metadata)?;

        // Create new IVF index
        let mut index = IvfIndex::new(ivf_config)?;

        // Read centroids and structure
        self.read_ivf_structure(&mut reader, &mut index)?;

        // Import vectors
        self.import_vectors_batched(&mut reader, &mut index, config.batch_size)?;

        Ok(Box::new(index))
    }

    /// Import flat index from FAISS format
    fn import_flat_index(
        &self,
        input_path: &Path,
        metadata: &FaissIndexMetadata,
        _config: &FaissImportConfig,
    ) -> Result<Box<dyn VectorIndex>> {
        let file = File::open(input_path)?;
        let mut reader = BufReader::new(file);

        // Skip FAISS header
        self.skip_faiss_header(&mut reader)?;

        // Create a simple in-memory index for flat FAISS indexes
        let mut vectors = Vec::new();
        let mut uris = Vec::new();

        // Read all vectors
        for i in 0..metadata.num_vectors {
            let vector = self.read_vector(&mut reader, metadata.dimension)?;
            vectors.push(vector);
            uris.push(format!("faiss_vector_{i}"));
        }

        // Create a simple flat index implementation
        Ok(Box::new(SimpleVectorIndex::new(vectors, uris)))
    }

    /// Detect optimal FAISS format for an index
    fn detect_optimal_faiss_format<T: VectorIndex>(&self, index: &T) -> Result<FaissIndexType> {
        let size = self.get_index_size(index)?;
        let dimension = self.get_index_dimension(index)?;

        // Use heuristics to determine best format
        if size < 10000 {
            // Small datasets - use flat index
            Ok(FaissIndexType::IndexFlatL2)
        } else if dimension > 1000 {
            // High-dimensional - use IVF with PQ
            Ok(FaissIndexType::IndexIVFPQ)
        } else if size > 100000 {
            // Large dataset - use HNSW
            Ok(FaissIndexType::IndexHNSWFlat)
        } else {
            // Medium dataset - use IVF flat
            Ok(FaissIndexType::IndexIVFFlat)
        }
    }

    /// Check if a FAISS format is supported
    fn is_format_supported(&self, format: &FaissIndexType) -> bool {
        self.supported_formats.contains(format)
    }

    /// Convert similarity metric to FAISS metric type
    fn convert_similarity_metric(&self, metric: SimilarityMetric) -> FaissMetricType {
        match metric {
            SimilarityMetric::Cosine => FaissMetricType::Cosine,
            SimilarityMetric::Euclidean => FaissMetricType::L2,
            SimilarityMetric::DotProduct => FaissMetricType::InnerProduct,
            SimilarityMetric::Manhattan => FaissMetricType::L2, // Approximate with L2
            // All other metrics approximate with L2 for FAISS compatibility
            _ => FaissMetricType::L2,
        }
    }

    /// Write FAISS header to file
    fn write_faiss_header(
        &self,
        writer: &mut BufWriter<File>,
        metadata: &FaissIndexMetadata,
    ) -> Result<()> {
        // FAISS magic number
        writer.write_all(b"FAISS")?;

        // Version
        writer.write_all(&1u32.to_le_bytes())?;

        // Index type identifier
        let type_id = self.faiss_type_to_id(metadata.index_type);
        writer.write_all(&type_id.to_le_bytes())?;

        // Dimension
        writer.write_all(&(metadata.dimension as u32).to_le_bytes())?;

        // Number of vectors
        writer.write_all(&(metadata.num_vectors as u64).to_le_bytes())?;

        // Metric type
        let metric_id = self.faiss_metric_to_id(metadata.metric_type);
        writer.write_all(&metric_id.to_le_bytes())?;

        Ok(())
    }

    /// Skip FAISS header when reading
    fn skip_faiss_header(&self, reader: &mut BufReader<File>) -> Result<()> {
        let mut magic = [0u8; 5];
        reader.read_exact(&mut magic)?;

        if &magic != b"FAISS" {
            return Err(anyhow!("Invalid FAISS file format"));
        }

        // Skip version, type, dimension, count, metric
        let mut buffer = [0u8; 21]; // 4 + 4 + 4 + 8 + 1
        reader.read_exact(&mut buffer)?;

        Ok(())
    }

    /// Write HNSW-specific data
    fn write_hnsw_data(
        &self,
        writer: &mut BufWriter<File>,
        index: &HnswIndex,
        _config: &FaissExportConfig,
    ) -> Result<()> {
        // Write HNSW parameters
        let config = index.config();
        writer.write_all(&(config.m as u32).to_le_bytes())?;
        writer.write_all(&(config.m_l0 as u32).to_le_bytes())?;
        writer.write_all(&(config.ef as u32).to_le_bytes())?;
        writer.write_all(&config.ml.to_le_bytes())?;

        Ok(())
    }

    /// Write IVF-specific data
    fn write_ivf_data(
        &self,
        writer: &mut BufWriter<File>,
        index: &IvfIndex,
        _config: &FaissExportConfig,
    ) -> Result<()> {
        // Write IVF parameters
        let config = index.config();
        writer.write_all(&(config.n_clusters as u32).to_le_bytes())?;
        writer.write_all(&(config.n_probes as u32).to_le_bytes())?;

        Ok(())
    }

    /// Write vectors in chunks for memory efficiency
    fn write_vectors_chunked<T: VectorIndex>(
        &self,
        writer: &mut BufWriter<File>,
        index: &T,
        chunk_size: usize,
    ) -> Result<()> {
        let total_vectors = self.get_index_size(index)?;

        for chunk_start in (0..total_vectors).step_by(chunk_size) {
            let chunk_end = std::cmp::min(chunk_start + chunk_size, total_vectors);

            for i in chunk_start..chunk_end {
                if let Some(vector) = self.get_vector_at_index(index, i) {
                    self.write_vector(writer, &vector)?;
                }
            }
        }

        Ok(())
    }

    /// Write a single vector to file
    fn write_vector(&self, writer: &mut BufWriter<File>, vector: &Vector) -> Result<()> {
        let data = vector.as_f32();
        for &value in &data {
            writer.write_all(&value.to_le_bytes())?;
        }
        Ok(())
    }

    /// Read a single vector from file
    fn read_vector(&self, reader: &mut BufReader<File>, dimension: usize) -> Result<Vector> {
        let mut data = vec![0.0f32; dimension];
        for value in &mut data {
            let mut bytes = [0u8; 4];
            reader.read_exact(&mut bytes)?;
            *value = f32::from_le_bytes(bytes);
        }

        Ok(Vector::new(data))
    }

    /// Utility methods for index introspection
    fn get_index_dimension<T: VectorIndex>(&self, _index: &T) -> Result<usize> {
        // This would need to be implemented based on the actual VectorIndex trait
        // For now, return a default dimension
        Ok(768) // Common dimension for transformer embeddings
    }

    fn get_index_size<T: VectorIndex>(&self, _index: &T) -> Result<usize> {
        // This would need to be implemented based on the actual VectorIndex trait
        Ok(0) // Placeholder
    }

    fn get_index_metric<T: VectorIndex>(&self, _index: &T) -> Result<SimilarityMetric> {
        // This would need to be implemented based on the actual VectorIndex trait
        Ok(SimilarityMetric::Cosine) // Default
    }

    fn get_vector_at_index<T: VectorIndex>(&self, _index: &T, _idx: usize) -> Option<Vector> {
        // This would need to be implemented based on the actual VectorIndex trait
        None // Placeholder
    }

    /// Helper methods for format conversion
    fn faiss_type_to_id(&self, faiss_type: FaissIndexType) -> u32 {
        match faiss_type {
            FaissIndexType::IndexFlatL2 => 0,
            FaissIndexType::IndexFlatIP => 1,
            FaissIndexType::IndexIVFFlat => 2,
            FaissIndexType::IndexIVFPQ => 3,
            FaissIndexType::IndexHNSWFlat => 4,
            FaissIndexType::IndexLSH => 5,
            FaissIndexType::IndexPCAFlat => 6,
        }
    }

    fn faiss_metric_to_id(&self, metric: FaissMetricType) -> u8 {
        match metric {
            FaissMetricType::L2 => 0,
            FaissMetricType::InnerProduct => 1,
            FaissMetricType::Cosine => 2,
        }
    }

    fn extract_index_parameters<T: VectorIndex>(
        &self,
        _index: &T,
        _format: FaissIndexType,
    ) -> Result<HashMap<String, FaissParameter>> {
        // Extract relevant parameters based on index type
        let mut params = HashMap::new();
        params.insert(
            "created_by".to_string(),
            FaissParameter::String("oxirs-vec".to_string()),
        );
        Ok(params)
    }

    fn estimate_memory_usage(&self, metadata: &FaissIndexMetadata) -> usize {
        // Rough estimate based on vectors and dimension
        metadata.num_vectors * metadata.dimension * 4 // 4 bytes per float
    }

    fn estimate_accuracy_preservation(
        &self,
        _format: FaissIndexType,
        _config: &FaissExportConfig,
    ) -> f32 {
        // Conservative estimate
        0.95 // 95% accuracy preservation
    }

    // Additional helper methods for reading configurations
    fn read_hnsw_config(
        &self,
        _reader: &mut BufReader<File>,
        _metadata: &FaissIndexMetadata,
    ) -> Result<HnswConfig> {
        // Read HNSW-specific configuration from FAISS file
        Ok(HnswConfig::default()) // Placeholder
    }

    fn read_ivf_config(
        &self,
        _reader: &mut BufReader<File>,
        _metadata: &FaissIndexMetadata,
    ) -> Result<IvfConfig> {
        // Read IVF-specific configuration from FAISS file
        Ok(IvfConfig::default()) // Placeholder
    }

    fn write_ivf_structure(&self, _writer: &mut BufWriter<File>, _index: &IvfIndex) -> Result<()> {
        // Write IVF centroids and inverted lists
        Ok(()) // Placeholder
    }

    fn read_ivf_structure(
        &self,
        _reader: &mut BufReader<File>,
        _index: &mut IvfIndex,
    ) -> Result<()> {
        // Read IVF centroids and structure
        Ok(()) // Placeholder
    }

    fn import_vectors_batched<T: VectorIndex>(
        &self,
        _reader: &mut BufReader<File>,
        _index: &mut T,
        _batch_size: usize,
    ) -> Result<()> {
        // Import vectors in batches for memory efficiency
        Ok(()) // Placeholder
    }

    fn read_faiss_metadata(&self, _input_path: &Path) -> Result<FaissIndexMetadata> {
        // Read and parse FAISS metadata from file
        Ok(FaissIndexMetadata {
            index_type: FaissIndexType::IndexHNSWFlat,
            dimension: 768,
            num_vectors: 0,
            metric_type: FaissMetricType::Cosine,
            parameters: HashMap::new(),
            version: "1.0".to_string(),
            created_at: chrono::Utc::now().to_rfc3339(),
        }) // Placeholder
    }

    fn write_faiss_index<T: VectorIndex>(
        &self,
        index: &T,
        output_path: &Path,
        metadata: &FaissIndexMetadata,
        config: &FaissExportConfig,
    ) -> Result<()> {
        match metadata.index_type {
            FaissIndexType::IndexHNSWFlat => {
                // Cast to HnswIndex for HNSW export
                // This is a simplified approach - in practice, you'd need proper type handling
                if let Some(hnsw_index) = self.try_cast_to_hnsw(index) {
                    self.export_hnsw_to_faiss(hnsw_index, output_path, metadata, config)
                } else {
                    Err(anyhow!("Index is not an HNSW index"))
                }
            }
            FaissIndexType::IndexIVFFlat | FaissIndexType::IndexIVFPQ => {
                // Cast to IvfIndex for IVF export
                if let Some(ivf_index) = self.try_cast_to_ivf(index) {
                    self.export_ivf_to_faiss(ivf_index, output_path, metadata, config)
                } else {
                    Err(anyhow!("Index is not an IVF index"))
                }
            }
            _ => Err(anyhow!(
                "Export format not yet implemented: {:?}",
                metadata.index_type
            )),
        }
    }

    // Helper methods for type casting (simplified approach)
    fn try_cast_to_hnsw<T: VectorIndex>(&self, _index: &T) -> Option<&HnswIndex> {
        // In practice, this would require proper downcasting or a different approach
        None // Placeholder
    }

    fn try_cast_to_ivf<T: VectorIndex>(&self, _index: &T) -> Option<&IvfIndex> {
        // In practice, this would require proper downcasting or a different approach
        None // Placeholder
    }
}

impl Default for FaissCompatibility {
    fn default() -> Self {
        Self::new()
    }
}

/// Simple in-memory vector index for flat FAISS imports
pub struct SimpleVectorIndex {
    vectors: Vec<Vector>,
    uris: Vec<String>,
}

impl SimpleVectorIndex {
    pub fn new(vectors: Vec<Vector>, uris: Vec<String>) -> Self {
        Self { vectors, uris }
    }
}

impl VectorIndex for SimpleVectorIndex {
    fn insert(&mut self, uri: String, vector: Vector) -> Result<()> {
        self.uris.push(uri);
        self.vectors.push(vector);
        Ok(())
    }

    fn search_knn(&self, query: &Vector, k: usize) -> Result<Vec<(String, f32)>> {
        let mut results = Vec::new();

        for (i, vector) in self.vectors.iter().enumerate() {
            let similarity = self.compute_similarity(query, vector);
            results.push((self.uris[i].clone(), similarity));
        }

        // Sort by similarity (descending) and take top k
        results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        results.truncate(k);

        Ok(results)
    }

    fn search_threshold(&self, query: &Vector, threshold: f32) -> Result<Vec<(String, f32)>> {
        let mut results = Vec::new();

        for (i, vector) in self.vectors.iter().enumerate() {
            let similarity = self.compute_similarity(query, vector);
            if similarity >= threshold {
                results.push((self.uris[i].clone(), similarity));
            }
        }

        // Sort by similarity (descending)
        results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        Ok(results)
    }

    fn get_vector(&self, uri: &str) -> Option<&Vector> {
        self.uris
            .iter()
            .position(|u| u == uri)
            .map(|i| &self.vectors[i])
    }
}

impl SimpleVectorIndex {
    fn compute_similarity(&self, v1: &Vector, v2: &Vector) -> f32 {
        // Simple cosine similarity implementation
        let v1_data = v1.as_f32();
        let v2_data = v2.as_f32();

        if v1_data.len() != v2_data.len() {
            return 0.0;
        }

        let dot_product: f32 = v1_data.iter().zip(v2_data.iter()).map(|(a, b)| a * b).sum();
        let magnitude1: f32 = v1_data.iter().map(|x| x * x).sum::<f32>().sqrt();
        let magnitude2: f32 = v2_data.iter().map(|x| x * x).sum::<f32>().sqrt();

        if magnitude1 == 0.0 || magnitude2 == 0.0 {
            0.0
        } else {
            dot_product / (magnitude1 * magnitude2)
        }
    }
}

/// Utility functions for FAISS compatibility
pub mod utils {
    use super::*;

    /// Convert oxirs-vec similarity metric to FAISS metric
    pub fn convert_metric(metric: SimilarityMetric) -> FaissMetricType {
        match metric {
            SimilarityMetric::Cosine => FaissMetricType::Cosine,
            SimilarityMetric::Euclidean => FaissMetricType::L2,
            SimilarityMetric::DotProduct => FaissMetricType::InnerProduct,
            SimilarityMetric::Manhattan => FaissMetricType::L2,
            // All other metrics approximate with L2 for FAISS compatibility
            _ => FaissMetricType::L2,
        }
    }

    /// Get recommended FAISS format for given constraints
    pub fn recommend_faiss_format(
        num_vectors: usize,
        dimension: usize,
        memory_constraint: Option<usize>,
        accuracy_requirement: f32,
    ) -> FaissIndexType {
        if num_vectors < 1000 || accuracy_requirement > 0.99 {
            FaissIndexType::IndexFlatL2
        } else if dimension > 1000 || memory_constraint.is_some_and(|mem| mem < 1024 * 1024 * 1024)
        {
            FaissIndexType::IndexIVFPQ
        } else if num_vectors > 100000 {
            FaissIndexType::IndexHNSWFlat
        } else {
            FaissIndexType::IndexIVFFlat
        }
    }

    /// Estimate memory requirements for FAISS format
    pub fn estimate_memory_requirement(
        format: FaissIndexType,
        num_vectors: usize,
        dimension: usize,
    ) -> usize {
        let base_memory = num_vectors * dimension * 4; // 4 bytes per float

        match format {
            FaissIndexType::IndexFlatL2 | FaissIndexType::IndexFlatIP => base_memory,
            FaissIndexType::IndexIVFFlat => base_memory + (num_vectors / 100) * dimension * 4, // Centroids
            FaissIndexType::IndexIVFPQ => base_memory / 8 + (num_vectors / 100) * dimension * 4, // PQ compression
            FaissIndexType::IndexHNSWFlat => base_memory * 2, // Graph structure overhead
            FaissIndexType::IndexLSH => base_memory / 2,      // Hash table compression
            FaissIndexType::IndexPCAFlat => base_memory, // Assuming no dimension reduction for estimate
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_faiss_compatibility_creation() {
        let faiss_compat = FaissCompatibility::new();
        assert!(!faiss_compat.supported_formats.is_empty());
    }

    #[test]
    fn test_metric_conversion() {
        let faiss_compat = FaissCompatibility::new();

        assert_eq!(
            faiss_compat.convert_similarity_metric(SimilarityMetric::Cosine),
            FaissMetricType::Cosine
        );
        assert_eq!(
            faiss_compat.convert_similarity_metric(SimilarityMetric::Euclidean),
            FaissMetricType::L2
        );
        assert_eq!(
            faiss_compat.convert_similarity_metric(SimilarityMetric::DotProduct),
            FaissMetricType::InnerProduct
        );
    }

    #[test]
    fn test_simple_vector_index() -> Result<()> {
        let vectors = vec![
            Vector::new(vec![1.0, 0.0, 0.0]),
            Vector::new(vec![0.0, 1.0, 0.0]),
            Vector::new(vec![0.0, 0.0, 1.0]),
        ];
        let uris = vec!["v1".to_string(), "v2".to_string(), "v3".to_string()];

        let index = SimpleVectorIndex::new(vectors, uris);

        let query = Vector::new(vec![1.0, 0.0, 0.0]);
        let results = index.search_knn(&query, 2)?;

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].0, "v1"); // Should be most similar to itself
        Ok(())
    }

    #[test]
    fn test_format_recommendation() {
        use crate::faiss_compatibility::utils::recommend_faiss_format;

        // Small dataset
        assert_eq!(
            recommend_faiss_format(100, 128, None, 0.9),
            FaissIndexType::IndexFlatL2
        );

        // Large dataset
        assert_eq!(
            recommend_faiss_format(1000000, 128, None, 0.8),
            FaissIndexType::IndexHNSWFlat
        );

        // High dimensional with memory constraint
        assert_eq!(
            recommend_faiss_format(50000, 2048, Some(512 * 1024 * 1024), 0.8),
            FaissIndexType::IndexIVFPQ
        );
    }
}