jin 0.1.0

Approximate Nearest Neighbor Search: HNSW, DiskANN, IVF-PQ, ScaNN, quantization
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
//! Index factory for creating ANN indexes from string descriptions.
//!
//! Inspired by Faiss's `index_factory` pattern, this module provides a simple
//! string-based API for creating different ANN index types.
//!
//! # Usage
//!
//! ```rust,ignore
//! use jin::ann::{index_factory, ANNIndex};
//!
//! // Create HNSW index (requires "hnsw" feature)
//! let mut index = index_factory(128, "HNSW32")?;
//! index.add(0, vec![0.1; 128])?;
//! index.build()?;
//! let results = index.search(&[0.1; 128], 10)?;
//! ```
//!
//! # Supported Index Types
//!
//! - `"HNSW{m}"` - Hierarchical Navigable Small World (e.g., "HNSW32")
//! - `"NSW{m}"` - Flat Navigable Small World (e.g., "NSW32")
//! - `"IVF{n},PQ{m}"` - Inverted File Index with Product Quantization (e.g., "IVF1024,PQ8")
//! - `"SCANN{n}"` - Anisotropic Vector Quantization with k-means (e.g., "SCANN256")
//!
//! **Note:** Tree-based methods (KD-Tree, Ball Tree, K-Means Tree, Random Projection Tree) are not
//! supported via the factory pattern due to complex parameter structures. Create them directly
//! using their respective constructors.
//!
//! # Future Support
//!
//! - `"PCA{d},..."` - PCA preprocessing (e.g., "PCA64,IVF1024,PQ8")
//! - Composite indexes with preprocessing pipelines

use crate::ann::ANNIndex;
use crate::RetrieveError;

// Make sure AnyANNIndex is visible
#[cfg(any(
    feature = "hnsw",
    feature = "nsw",
    feature = "ivf_pq",
    feature = "scann",
    feature = "dense"
))]
pub use self::AnyANNIndex as ANNIndexEnum;

/// Type-erased ANN index container.
///
/// This enum allows storing different index types in a single variable,
/// enabling polymorphic usage through the `ANNIndex` trait.
#[derive(Debug)]
pub enum AnyANNIndex {
    #[cfg(feature = "hnsw")]
    HNSW(crate::hnsw::HNSWIndex),

    #[cfg(feature = "nsw")]
    NSW(crate::nsw::NSWIndex),

    #[cfg(feature = "ivf_pq")]
    IVFPQ(crate::ivf_pq::IVFPQIndex),

    #[cfg(feature = "scann")]
    SCANN(crate::scann::search::SCANNIndex),

    #[cfg(feature = "kmeans_tree")]
    KMeansTree(crate::classic::trees::kmeans_tree::KMeansTreeIndex),
}

impl ANNIndex for AnyANNIndex {
    fn add(&mut self, doc_id: u32, vector: Vec<f32>) -> Result<(), RetrieveError> {
        match self {
            #[cfg(feature = "hnsw")]
            AnyANNIndex::HNSW(idx) => idx.add(doc_id, vector),

            #[cfg(feature = "nsw")]
            AnyANNIndex::NSW(idx) => idx.add(doc_id, vector),

            #[cfg(feature = "ivf_pq")]
            AnyANNIndex::IVFPQ(idx) => idx.add(doc_id, vector),

            #[cfg(feature = "scann")]
            AnyANNIndex::SCANN(idx) => idx.add(doc_id, vector),

            #[cfg(feature = "kmeans_tree")]
            AnyANNIndex::KMeansTree(idx) => idx.add(doc_id, vector),
        }
    }

    fn add_slice(&mut self, doc_id: u32, vector: &[f32]) -> Result<(), RetrieveError> {
        match self {
            #[cfg(feature = "hnsw")]
            AnyANNIndex::HNSW(idx) => idx.add_slice(doc_id, vector),

            #[cfg(feature = "nsw")]
            AnyANNIndex::NSW(idx) => idx.add_slice(doc_id, vector),

            #[cfg(feature = "ivf_pq")]
            AnyANNIndex::IVFPQ(idx) => idx.add_slice(doc_id, vector),

            #[cfg(feature = "scann")]
            AnyANNIndex::SCANN(idx) => idx.add_slice(doc_id, vector),

            #[cfg(feature = "kmeans_tree")]
            AnyANNIndex::KMeansTree(idx) => idx.add_slice(doc_id, vector),
        }
    }

    fn build(&mut self) -> Result<(), RetrieveError> {
        match self {
            #[cfg(feature = "hnsw")]
            AnyANNIndex::HNSW(idx) => idx.build(),

            #[cfg(feature = "nsw")]
            AnyANNIndex::NSW(idx) => idx.build(),

            #[cfg(feature = "ivf_pq")]
            AnyANNIndex::IVFPQ(idx) => idx.build(),

            #[cfg(feature = "scann")]
            AnyANNIndex::SCANN(idx) => idx.build(),

            #[cfg(feature = "kmeans_tree")]
            AnyANNIndex::KMeansTree(idx) => idx.build(),
        }
    }

    fn search(&self, query: &[f32], k: usize) -> Result<Vec<(u32, f32)>, RetrieveError> {
        match self {
            #[cfg(feature = "hnsw")]
            AnyANNIndex::HNSW(idx) => {
                // Use default ef_search (50) - HNSW search requires ef_search parameter
                // Note: We use a reasonable default. For custom ef_search, use HNSWIndex directly
                // or implement a method to get default ef_search from params
                idx.search(query, k, 50) // Default ef_search
            }

            #[cfg(feature = "nsw")]
            AnyANNIndex::NSW(idx) => idx.search(query, k, idx.params.ef_search),

            #[cfg(feature = "ivf_pq")]
            AnyANNIndex::IVFPQ(idx) => idx.search(query, k),

            #[cfg(feature = "scann")]
            AnyANNIndex::SCANN(idx) => idx.search(query, k),

            #[cfg(feature = "kmeans_tree")]
            AnyANNIndex::KMeansTree(idx) => idx.search(query, k),
        }
    }

    fn size_bytes(&self) -> usize {
        match self {
            #[cfg(feature = "hnsw")]
            AnyANNIndex::HNSW(idx) => idx.size_bytes(),

            #[cfg(feature = "nsw")]
            AnyANNIndex::NSW(idx) => idx.size_bytes(),

            #[cfg(feature = "ivf_pq")]
            AnyANNIndex::IVFPQ(idx) => idx.size_bytes(),

            #[cfg(feature = "scann")]
            AnyANNIndex::SCANN(idx) => idx.size_bytes(),

            #[cfg(feature = "kmeans_tree")]
            AnyANNIndex::KMeansTree(idx) => idx.size_bytes(),
        }
    }

    fn stats(&self) -> crate::ann::ANNStats {
        match self {
            #[cfg(feature = "hnsw")]
            AnyANNIndex::HNSW(idx) => idx.stats(),

            #[cfg(feature = "nsw")]
            AnyANNIndex::NSW(idx) => idx.stats(),

            #[cfg(feature = "ivf_pq")]
            AnyANNIndex::IVFPQ(idx) => idx.stats(),

            #[cfg(feature = "scann")]
            AnyANNIndex::SCANN(idx) => idx.stats(),

            #[cfg(feature = "kmeans_tree")]
            AnyANNIndex::KMeansTree(idx) => idx.stats(),
        }
    }

    fn dimension(&self) -> usize {
        match self {
            #[cfg(feature = "hnsw")]
            AnyANNIndex::HNSW(idx) => idx.dimension(),

            #[cfg(feature = "nsw")]
            AnyANNIndex::NSW(idx) => idx.dimension(),

            #[cfg(feature = "ivf_pq")]
            AnyANNIndex::IVFPQ(idx) => idx.dimension(),

            #[cfg(feature = "scann")]
            AnyANNIndex::SCANN(idx) => idx.dimension(),

            #[cfg(feature = "kmeans_tree")]
            AnyANNIndex::KMeansTree(idx) => idx.dimension(),
        }
    }

    fn num_vectors(&self) -> usize {
        match self {
            #[cfg(feature = "hnsw")]
            AnyANNIndex::HNSW(idx) => idx.num_vectors(),

            #[cfg(feature = "nsw")]
            AnyANNIndex::NSW(idx) => idx.num_vectors(),

            #[cfg(feature = "ivf_pq")]
            AnyANNIndex::IVFPQ(idx) => idx.num_vectors(),

            #[cfg(feature = "scann")]
            AnyANNIndex::SCANN(idx) => idx.num_vectors(),

            #[cfg(feature = "kmeans_tree")]
            AnyANNIndex::KMeansTree(idx) => idx.num_vectors(),
        }
    }
}

/// Create an ANN index from a factory string.
///
/// The factory string describes the index type and parameters in a simple format.
/// This is inspired by Faiss's `index_factory` pattern.
///
/// # Supported Formats
///
/// - `"HNSW{m}"` - HNSW with m connections (e.g., "HNSW32")
/// - `"NSW{m}"` - Flat NSW with m connections (e.g., "NSW32")
/// - `"IVF{n},PQ{m}"` - IVF-PQ with n clusters and m codebooks (e.g., "IVF1024,PQ8")
/// - `"SCANN{n}"` - SCANN with n partitions (e.g., "SCANN256")
///
/// # Examples
///
/// ```rust,ignore
/// use jin::ann::factory::index_factory;
/// use jin::ann::ANNIndex;
///
/// // HNSW index
/// let mut index = index_factory(128, "HNSW32")?;
/// let v0 = vec![0.1; 128];
/// index.add_slice(0, &v0)?;
/// index.build()?;
/// let results = index.search(&[0.15; 128], 10)?;
/// ```
///
/// # Errors
///
/// Returns `RetrieveError` if:
/// - The factory string is invalid or unsupported
/// - Required features are not enabled
/// - Parameters are invalid (e.g., dimension = 0, m = 0)
/// - Dimension mismatch between factory and vectors
pub fn index_factory(dimension: usize, factory_string: &str) -> Result<AnyANNIndex, RetrieveError> {
    // Validate dimension
    if dimension == 0 {
        return Err(RetrieveError::Other(
            "Dimension must be greater than 0".to_string(),
        ));
    }

    let factory_string = factory_string.trim();

    // Validate empty string
    if factory_string.is_empty() {
        return Err(RetrieveError::Other(
            "Factory string cannot be empty".to_string(),
        ));
    }

    // Parse HNSW: "HNSW{m}" or "HNSW{m},{m_max}"
    if let Some(rest) = factory_string.strip_prefix("HNSW") {
        #[cfg(not(feature = "hnsw"))]
        {
            let _ = rest; // Suppress unused warning
            return Err(RetrieveError::Other(
                "HNSW feature not enabled. Add 'hnsw' feature to Cargo.toml".to_string(),
            ));
        }

        #[cfg(feature = "hnsw")]
        {
            if rest.is_empty() {
                return Err(RetrieveError::Other(
                    "HNSW format: HNSW{m} or HNSW{m},{m_max}".to_string(),
                ));
            }

            let parts: Vec<&str> = rest.split(',').collect();

            let m = parts[0].parse::<usize>().map_err(|_| {
                RetrieveError::Other(format!(
                    "Invalid HNSW parameter: '{}'. Expected number.",
                    parts[0]
                ))
            })?;

            if m == 0 {
                return Err(RetrieveError::Other(
                    "HNSW m parameter must be greater than 0".to_string(),
                ));
            }

            let m_max = if parts.len() > 1 {
                let m_max_val = parts[1].parse::<usize>().map_err(|_| {
                    RetrieveError::Other(format!(
                        "Invalid HNSW m_max parameter: '{}'. Expected number.",
                        parts[1]
                    ))
                })?;
                if m_max_val == 0 {
                    return Err(RetrieveError::Other(
                        "HNSW m_max parameter must be greater than 0".to_string(),
                    ));
                }
                m_max_val
            } else {
                m // Default m_max = m
            };

            let index = crate::hnsw::HNSWIndex::new(dimension, m, m_max)?;
            return Ok(AnyANNIndex::HNSW(index));
        }
    }

    // Parse NSW: "NSW{m}"
    if let Some(rest) = factory_string.strip_prefix("NSW") {
        #[cfg(not(feature = "nsw"))]
        {
            let _ = rest; // Suppress unused warning
            return Err(RetrieveError::Other(
                "NSW feature not enabled. Add 'nsw' feature to Cargo.toml".to_string(),
            ));
        }

        #[cfg(feature = "nsw")]
        {
            if rest.is_empty() {
                return Err(RetrieveError::Other("NSW format: NSW{m}".to_string()));
            }

            let m = rest.parse::<usize>().map_err(|_| {
                RetrieveError::Other(format!(
                    "Invalid NSW parameter: '{}'. Expected number.",
                    rest
                ))
            })?;

            if m == 0 {
                return Err(RetrieveError::Other(
                    "NSW m parameter must be greater than 0".to_string(),
                ));
            }

            let index = crate::nsw::NSWIndex::new(dimension, m, m)?;
            return Ok(AnyANNIndex::NSW(index));
        }
    }

    // Parse IVF-PQ: "IVF{n},PQ{m}" or "IVF{n},PQ{m}x{b}" (m codebooks, b bits)
    if factory_string.starts_with("IVF") {
        #[cfg(not(feature = "ivf_pq"))]
        return Err(RetrieveError::Other(
            "IVF-PQ feature not enabled. Add 'ivf_pq' feature to Cargo.toml".to_string(),
        ));

        #[cfg(feature = "ivf_pq")]
        {
            let parts: Vec<&str> = factory_string.split(',').collect();
            if parts.len() < 2 {
                return Err(RetrieveError::Other(
                    "IVF-PQ format: IVF{n},PQ{m} or IVF{n},PQ{m}x{b}".to_string(),
                ));
            }

            // Parse IVF{n}
            let ivf_part = parts[0].trim();
            if !ivf_part.starts_with("IVF") {
                return Err(RetrieveError::Other(format!(
                    "Invalid IVF format: '{}'. Expected IVF{{n}}.",
                    ivf_part
                )));
            }

            if ivf_part.len() == 3 {
                return Err(RetrieveError::Other(
                    "IVF format: IVF{n} where n is number of clusters".to_string(),
                ));
            }

            let num_clusters = ivf_part[3..].parse::<usize>().map_err(|_| {
                RetrieveError::Other(format!(
                    "Invalid IVF cluster count: '{}'. Expected number.",
                    &ivf_part[3..]
                ))
            })?;

            if num_clusters == 0 {
                return Err(RetrieveError::Other(
                    "IVF num_clusters must be greater than 0".to_string(),
                ));
            }

            // Parse PQ{m} or PQ{m}x{b}
            let pq_part = parts[1].trim();
            if !pq_part.starts_with("PQ") {
                return Err(RetrieveError::Other(format!(
                    "Invalid PQ format: '{}'. Expected PQ{{m}} or PQ{{m}}x{{b}}.",
                    pq_part
                )));
            }

            if pq_part.len() == 2 {
                return Err(RetrieveError::Other(
                    "PQ format: PQ{m} or PQ{m}x{b}".to_string(),
                ));
            }

            let pq_rest = &pq_part[2..];
            let (num_codebooks, codebook_size) = if pq_rest.contains('x') {
                let pq_parts: Vec<&str> = pq_rest.split('x').collect();
                if pq_parts.len() != 2 {
                    return Err(RetrieveError::Other(format!(
                        "Invalid PQ format: '{}'. Expected PQ{{m}}x{{b}}.",
                        pq_part
                    )));
                }
                let num_codebooks = pq_parts[0].trim().parse::<usize>().map_err(|_| {
                    RetrieveError::Other(format!(
                        "Invalid PQ codebook count: '{}'. Expected number.",
                        pq_parts[0]
                    ))
                })?;
                if num_codebooks == 0 {
                    return Err(RetrieveError::Other(
                        "PQ num_codebooks must be greater than 0".to_string(),
                    ));
                }
                let bits = pq_parts[1].trim().parse::<usize>().map_err(|_| {
                    RetrieveError::Other(format!(
                        "Invalid PQ bits: '{}'. Expected number.",
                        pq_parts[1]
                    ))
                })?;
                if bits > 16 {
                    return Err(RetrieveError::Other(format!(
                        "PQ bits ({}) exceeds maximum (16)",
                        bits
                    )));
                }
                let codebook_size = 1 << bits; // 2^bits
                (num_codebooks, codebook_size)
            } else {
                // Default: PQ8 means 8 codebooks, 256 size (8 bits)
                let num_codebooks = pq_rest.trim().parse::<usize>().map_err(|_| {
                    RetrieveError::Other(format!(
                        "Invalid PQ codebook count: '{}'. Expected number.",
                        pq_rest
                    ))
                })?;
                if num_codebooks == 0 {
                    return Err(RetrieveError::Other(
                        "PQ num_codebooks must be greater than 0".to_string(),
                    ));
                }
                (num_codebooks, 256) // Default 8 bits = 256
            };

            // Validate dimension is divisible by num_codebooks for PQ
            if dimension % num_codebooks != 0 {
                return Err(RetrieveError::Other(format!(
                    "Dimension ({}) must be divisible by num_codebooks ({}) for PQ",
                    dimension, num_codebooks
                )));
            }

            use crate::ivf_pq::IVFPQParams;
            let params = IVFPQParams {
                num_clusters,
                nprobe: (num_clusters / 10).clamp(1, 100), // Default nprobe
                num_codebooks,
                codebook_size,
                use_opq: false,
                #[cfg(feature = "id-compression")]
                id_compression: None,
                #[cfg(feature = "id-compression")]
                compression_threshold: 100,
            };

            let index = crate::ivf_pq::IVFPQIndex::new(dimension, params)?;
            return Ok(AnyANNIndex::IVFPQ(index));
        }
    }

    // Parse SCANN: "SCANN{n}"
    if let Some(rest) = factory_string.strip_prefix("SCANN") {
        #[cfg(not(feature = "scann"))]
        {
            let _ = rest; // Suppress unused warning
            return Err(RetrieveError::Other(
                "SCANN feature not enabled. Add 'scann' feature to Cargo.toml".to_string(),
            ));
        }

        #[cfg(feature = "scann")]
        {
            if rest.is_empty() {
                return Err(RetrieveError::Other("SCANN format: SCANN{n}".to_string()));
            }

            let num_partitions = rest.trim().parse::<usize>().map_err(|_| {
                RetrieveError::Other(format!(
                    "Invalid SCANN parameter: '{}'. Expected number.",
                    rest
                ))
            })?;

            if num_partitions == 0 {
                return Err(RetrieveError::Other(
                    "SCANN num_partitions must be greater than 0".to_string(),
                ));
            }

            use crate::scann::search::SCANNParams;
            let params = SCANNParams {
                num_partitions,
                num_reorder: 100,   // Default reranking count
                num_codebooks: 8,   // Default: 8 subspaces
                codebook_size: 256, // Default: 8-bit quantization (256 codewords)
            };

            let index = crate::scann::search::SCANNIndex::new(dimension, params)?;
            return Ok(AnyANNIndex::SCANN(index));
        }
    }

    Err(RetrieveError::Other(format!(
        "Unsupported index factory string: '{}'. Supported: HNSW{{m}}, NSW{{m}}, IVF{{n}},PQ{{m}}, SCANN{{n}}",
        factory_string
    )))
}

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

    #[test]
    fn test_hnsw_factory() {
        #[cfg(feature = "hnsw")]
        {
            // Valid cases
            let index = index_factory(128, "HNSW32");
            assert!(index.is_ok());

            let index = index_factory(128, "HNSW16,32");
            assert!(index.is_ok());

            // Edge cases
            let index = index_factory(0, "HNSW32");
            assert!(index.is_err());

            let index = index_factory(128, "HNSW0");
            assert!(index.is_err());

            let index = index_factory(128, "HNSW");
            assert!(index.is_err());

            let index = index_factory(128, "HNSWabc");
            assert!(index.is_err());
        }
    }

    #[test]
    fn test_nsw_factory() {
        #[cfg(feature = "nsw")]
        {
            let index = index_factory(128, "NSW32");
            assert!(index.is_ok());

            // Edge cases
            let index = index_factory(128, "NSW0");
            assert!(index.is_err());

            let index = index_factory(128, "NSW");
            assert!(index.is_err());
        }
    }

    #[test]
    fn test_ivf_pq_factory() {
        #[cfg(feature = "ivf_pq")]
        {
            // Valid cases
            let index = index_factory(128, "IVF1024,PQ8");
            assert!(index.is_ok());

            let index = index_factory(128, "IVF1024,PQ8x8");
            assert!(index.is_ok());

            // Edge cases
            let index = index_factory(128, "IVF0,PQ8");
            assert!(index.is_err());

            let index = index_factory(128, "IVF1024,PQ0");
            assert!(index.is_err());

            let index = index_factory(128, "IVF,PQ8");
            assert!(index.is_err());

            let index = index_factory(128, "IVF1024,PQ");
            assert!(index.is_err());

            let index = index_factory(128, "IVF1024");
            assert!(index.is_err());

            // Dimension not divisible by codebooks
            let index = index_factory(100, "IVF1024,PQ8"); // 100 % 8 != 0
            assert!(index.is_err());
        }
    }

    #[test]
    fn test_scann_factory() {
        #[cfg(feature = "scann")]
        {
            let index = index_factory(128, "SCANN256");
            assert!(index.is_ok());

            // Edge cases
            let index = index_factory(128, "SCANN0");
            assert!(index.is_err());

            let index = index_factory(128, "SCANN");
            assert!(index.is_err());
        }
    }

    #[test]
    fn test_invalid_factory() {
        // Invalid index type
        let result = index_factory(128, "Invalid");
        assert!(result.is_err());

        // Empty string
        let result = index_factory(128, "");
        assert!(result.is_err());

        // Whitespace only
        let result = index_factory(128, "   ");
        assert!(result.is_err());

        // Zero dimension
        let result = index_factory(0, "HNSW32");
        assert!(result.is_err());
    }

    #[test]
    fn test_factory_usage() {
        #[cfg(feature = "hnsw")]
        {
            let mut index = index_factory(128, "HNSW32").unwrap();

            // Add vectors
            for i in 0..10 {
                let vec = vec![0.1; 128];
                assert!(index.add(i, vec).is_ok());
            }

            // Build
            assert!(index.build().is_ok());

            // Search
            let query = vec![0.15; 128];
            let results = index.search(&query, 5);
            assert!(results.is_ok());
            let results = results.unwrap();
            assert!(!results.is_empty());
        }
    }

    #[test]
    fn test_factory_whitespace_handling() {
        #[cfg(feature = "hnsw")]
        {
            // Should handle whitespace
            let index1 = index_factory(128, "HNSW32");
            let index2 = index_factory(128, "  HNSW32  ");
            assert_eq!(index1.is_ok(), index2.is_ok());
        }
    }
}