kitedb 0.2.18

High-performance embedded graph database
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
//! NAPI bindings for Vector Search
//!
//! Exposes IVF and IVF-PQ indexes to Node.js/Bun.

use napi::bindgen_prelude::*;
use napi_derive::napi;
use std::sync::RwLock;

use crate::api::vector_search::{
  SimilarOptions as RustSimilarOptions, VectorIndex as RustVectorIndex,
  VectorIndexError as RustVectorIndexError, VectorIndexOptions as RustVectorIndexOptions,
  VectorIndexStats as RustVectorIndexStats, VectorSearchHit as RustVectorSearchHit,
};
use crate::vector::{
  DistanceMetric as RustDistanceMetric, IvfConfig as RustIvfConfig, IvfIndex as RustIvfIndex,
  IvfPqConfig as RustIvfPqConfig, IvfPqIndex as RustIvfPqIndex, MultiQueryAggregation,
  PqConfig as RustPqConfig, SearchOptions as RustSearchOptions, VectorManifest, VectorSearchResult,
};

// ============================================================================
// Distance Metric
// ============================================================================

/// Distance metric for vector similarity
#[napi(string_enum)]
#[derive(Debug)]
pub enum JsDistanceMetric {
  /// Cosine similarity (1 - cosine)
  Cosine,
  /// Euclidean (L2) distance
  Euclidean,
  /// Dot product (negated for distance)
  DotProduct,
}

impl From<JsDistanceMetric> for RustDistanceMetric {
  fn from(m: JsDistanceMetric) -> Self {
    match m {
      JsDistanceMetric::Cosine => RustDistanceMetric::Cosine,
      JsDistanceMetric::Euclidean => RustDistanceMetric::Euclidean,
      JsDistanceMetric::DotProduct => RustDistanceMetric::DotProduct,
    }
  }
}

impl From<RustDistanceMetric> for JsDistanceMetric {
  fn from(m: RustDistanceMetric) -> Self {
    match m {
      RustDistanceMetric::Cosine => JsDistanceMetric::Cosine,
      RustDistanceMetric::Euclidean => JsDistanceMetric::Euclidean,
      RustDistanceMetric::DotProduct => JsDistanceMetric::DotProduct,
    }
  }
}

// ============================================================================
// Aggregation Method
// ============================================================================

/// Aggregation method for multi-query search
#[napi(string_enum)]
pub enum JsAggregation {
  /// Minimum distance (best match)
  Min,
  /// Maximum distance (worst match)
  Max,
  /// Average distance
  Avg,
  /// Sum of distances
  Sum,
}

impl From<JsAggregation> for MultiQueryAggregation {
  fn from(a: JsAggregation) -> Self {
    match a {
      JsAggregation::Min => MultiQueryAggregation::Min,
      JsAggregation::Max => MultiQueryAggregation::Max,
      JsAggregation::Avg => MultiQueryAggregation::Avg,
      JsAggregation::Sum => MultiQueryAggregation::Sum,
    }
  }
}

// ============================================================================
// IVF Configuration
// ============================================================================

/// Configuration for IVF index
#[napi(object)]
#[derive(Debug, Default)]
pub struct JsIvfConfig {
  /// Number of clusters (default: 100)
  pub n_clusters: Option<i32>,
  /// Number of clusters to probe during search (default: 10)
  pub n_probe: Option<i32>,
  /// Distance metric (default: Cosine)
  pub metric: Option<JsDistanceMetric>,
}

impl From<JsIvfConfig> for RustIvfConfig {
  fn from(c: JsIvfConfig) -> Self {
    let mut config = RustIvfConfig::default();
    if let Some(n) = c.n_clusters {
      config.n_clusters = n as usize;
    }
    if let Some(n) = c.n_probe {
      config.n_probe = n as usize;
    }
    if let Some(m) = c.metric {
      config.metric = m.into();
    }
    config
  }
}

// ============================================================================
// PQ Configuration
// ============================================================================

/// Configuration for Product Quantization
#[napi(object)]
#[derive(Debug, Default)]
pub struct JsPqConfig {
  /// Number of subspaces (must divide dimensions evenly)
  pub num_subspaces: Option<i32>,
  /// Number of centroids per subspace (default: 256)
  pub num_centroids: Option<i32>,
  /// Max k-means iterations for training (default: 25)
  pub max_iterations: Option<i32>,
}

impl From<JsPqConfig> for RustPqConfig {
  fn from(c: JsPqConfig) -> Self {
    let mut config = RustPqConfig::default();
    if let Some(n) = c.num_subspaces {
      config.num_subspaces = n as usize;
    }
    if let Some(n) = c.num_centroids {
      config.num_centroids = n as usize;
    }
    if let Some(n) = c.max_iterations {
      config.max_iterations = n as usize;
    }
    config
  }
}

// ============================================================================
// Search Options
// ============================================================================

/// Options for vector search
#[napi(object)]
#[derive(Debug, Default)]
pub struct JsSearchOptions {
  /// Number of clusters to probe (overrides index default)
  pub n_probe: Option<i32>,
  /// Minimum similarity threshold (0-1)
  pub threshold: Option<f64>,
}

// ============================================================================
// Search Result
// ============================================================================

/// Result of a vector search
#[napi(object)]
pub struct JsSearchResult {
  /// Vector ID
  pub vector_id: i64,
  /// Associated node ID
  pub node_id: i64,
  /// Distance from query
  pub distance: f64,
  /// Similarity score (0-1, higher is more similar)
  pub similarity: f64,
}

impl From<VectorSearchResult> for JsSearchResult {
  fn from(r: VectorSearchResult) -> Self {
    JsSearchResult {
      vector_id: r.vector_id as i64,
      node_id: r.node_id as i64,
      distance: r.distance as f64,
      similarity: r.similarity as f64,
    }
  }
}

// ============================================================================
// IVF Index Statistics
// ============================================================================

/// Statistics for IVF index
#[napi(object)]
pub struct JsIvfStats {
  /// Whether the index is trained
  pub trained: bool,
  /// Number of clusters
  pub n_clusters: i32,
  /// Total vectors in the index
  pub total_vectors: i64,
  /// Average vectors per cluster
  pub avg_vectors_per_cluster: f64,
  /// Number of empty clusters
  pub empty_cluster_count: i32,
  /// Minimum cluster size
  pub min_cluster_size: i32,
  /// Maximum cluster size
  pub max_cluster_size: i32,
}

// ============================================================================
// IVF Index NAPI Wrapper
// ============================================================================

/// IVF (Inverted File) index for approximate nearest neighbor search
#[napi]
pub struct JsIvfIndex {
  inner: RwLock<RustIvfIndex>,
}

#[napi]
impl JsIvfIndex {
  /// Create a new IVF index
  #[napi(constructor)]
  pub fn new(dimensions: i32, config: Option<JsIvfConfig>) -> Result<JsIvfIndex> {
    let rust_config = config.unwrap_or_default().into();
    Ok(JsIvfIndex {
      inner: RwLock::new(RustIvfIndex::new(dimensions as usize, rust_config)),
    })
  }

  /// Get the number of dimensions
  #[napi(getter)]
  pub fn dimensions(&self) -> Result<i32> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    Ok(index.dimensions as i32)
  }

  /// Check if the index is trained
  #[napi(getter)]
  pub fn trained(&self) -> Result<bool> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    Ok(index.trained)
  }

  /// Add training vectors
  ///
  /// Call this before train() with representative vectors from your dataset.
  #[napi]
  pub fn add_training_vectors(&self, vectors: Vec<f64>, num_vectors: i32) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let vectors_f32: Vec<f32> = vectors.iter().map(|&v| v as f32).collect();
    index
      .add_training_vectors(&vectors_f32, num_vectors as usize)
      .map_err(|e| Error::from_reason(format!("Failed to add training vectors: {e}")))
  }

  /// Train the index on added training vectors
  ///
  /// This runs k-means clustering to create the inverted file structure.
  #[napi]
  pub fn train(&self) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    index
      .train()
      .map_err(|e| Error::from_reason(format!("Failed to train index: {e}")))
  }

  /// Insert a vector into the index
  ///
  /// The index must be trained first.
  #[napi]
  pub fn insert(&self, vector_id: i64, vector: Vec<f64>) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    index
      .insert(vector_id as u64, &vector_f32)
      .map_err(|e| Error::from_reason(format!("Failed to insert vector: {e}")))
  }

  /// Delete a vector from the index
  ///
  /// Requires the vector data to determine which cluster to remove from.
  #[napi]
  pub fn delete(&self, vector_id: i64, vector: Vec<f64>) -> Result<bool> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    Ok(index.delete(vector_id as u64, &vector_f32))
  }

  /// Clear all data from the index
  #[napi]
  pub fn clear(&self) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    index.clear();
    Ok(())
  }

  /// Search for k nearest neighbors
  ///
  /// Requires a VectorManifest to look up actual vector data.
  #[napi]
  pub fn search(
    &self,
    manifest_json: String,
    query: Vec<f64>,
    k: i32,
    options: Option<JsSearchOptions>,
  ) -> Result<Vec<JsSearchResult>> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;

    // Parse manifest from JSON
    let manifest: VectorManifest = serde_json::from_str(&manifest_json)
      .map_err(|e| Error::from_reason(format!("Failed to parse manifest: {e}")))?;

    let query_f32: Vec<f32> = query.iter().map(|&v| v as f32).collect();

    let rust_options = options.map(|o| RustSearchOptions {
      n_probe: o.n_probe.map(|n| n as usize),
      filter: None,
      threshold: o.threshold.map(|t| t as f32),
    });

    let results = index.search(&manifest, &query_f32, k as usize, rust_options);
    Ok(results.into_iter().map(|r| r.into()).collect())
  }

  /// Search with multiple query vectors
  ///
  /// Aggregates results using the specified method.
  #[napi]
  pub fn search_multi(
    &self,
    manifest_json: String,
    queries: Vec<Vec<f64>>,
    k: i32,
    aggregation: JsAggregation,
    options: Option<JsSearchOptions>,
  ) -> Result<Vec<JsSearchResult>> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;

    // Parse manifest from JSON
    let manifest: VectorManifest = serde_json::from_str(&manifest_json)
      .map_err(|e| Error::from_reason(format!("Failed to parse manifest: {e}")))?;

    let queries_f32: Vec<Vec<f32>> = queries
      .iter()
      .map(|q| q.iter().map(|&v| v as f32).collect())
      .collect();

    let query_refs: Vec<&[f32]> = queries_f32.iter().map(|q| q.as_slice()).collect();

    let rust_options = options.map(|o| RustSearchOptions {
      n_probe: o.n_probe.map(|n| n as usize),
      filter: None,
      threshold: o.threshold.map(|t| t as f32),
    });

    let results = index.search_multi(
      &manifest,
      &query_refs,
      k as usize,
      aggregation.into(),
      rust_options,
    );
    Ok(results.into_iter().map(|r| r.into()).collect())
  }

  /// Get index statistics
  #[napi]
  pub fn stats(&self) -> Result<JsIvfStats> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let s = index.stats();
    Ok(JsIvfStats {
      trained: s.trained,
      n_clusters: s.n_clusters as i32,
      total_vectors: s.total_vectors as i64,
      avg_vectors_per_cluster: s.avg_vectors_per_cluster as f64,
      empty_cluster_count: s.empty_cluster_count as i32,
      min_cluster_size: s.min_cluster_size as i32,
      max_cluster_size: s.max_cluster_size as i32,
    })
  }

  /// Serialize the index to bytes
  #[napi]
  pub fn serialize(&self) -> Result<Buffer> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let bytes = crate::vector::ivf::serialize::serialize_ivf(&index);
    Ok(Buffer::from(bytes))
  }

  /// Deserialize an index from bytes
  #[napi(factory)]
  pub fn deserialize(data: Buffer) -> Result<JsIvfIndex> {
    let index = crate::vector::ivf::serialize::deserialize_ivf(&data)
      .map_err(|e| Error::from_reason(format!("Failed to deserialize: {e}")))?;
    Ok(JsIvfIndex {
      inner: RwLock::new(index),
    })
  }
}

// ============================================================================
// IVF-PQ Index NAPI Wrapper
// ============================================================================

/// IVF-PQ combined index for memory-efficient approximate nearest neighbor search
#[napi]
pub struct JsIvfPqIndex {
  inner: RwLock<RustIvfPqIndex>,
}

#[napi]
impl JsIvfPqIndex {
  /// Create a new IVF-PQ index
  #[napi(constructor)]
  pub fn new(
    dimensions: i32,
    ivf_config: Option<JsIvfConfig>,
    pq_config: Option<JsPqConfig>,
    use_residuals: Option<bool>,
  ) -> Result<JsIvfPqIndex> {
    let config = RustIvfPqConfig {
      ivf: ivf_config.unwrap_or_default().into(),
      pq: pq_config.unwrap_or_default().into(),
      use_residuals: use_residuals.unwrap_or(true),
    };

    let index = RustIvfPqIndex::new(dimensions as usize, config)
      .map_err(|e| Error::from_reason(format!("Failed to create index: {e}")))?;

    Ok(JsIvfPqIndex {
      inner: RwLock::new(index),
    })
  }

  /// Get the number of dimensions
  #[napi(getter)]
  pub fn dimensions(&self) -> Result<i32> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    Ok(index.dimensions as i32)
  }

  /// Check if the index is trained
  #[napi(getter)]
  pub fn trained(&self) -> Result<bool> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    Ok(index.trained)
  }

  /// Add training vectors
  #[napi]
  pub fn add_training_vectors(&self, vectors: Vec<f64>, num_vectors: i32) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let vectors_f32: Vec<f32> = vectors.iter().map(|&v| v as f32).collect();
    index
      .add_training_vectors(&vectors_f32, num_vectors as usize)
      .map_err(|e| Error::from_reason(format!("Failed to add training vectors: {e}")))
  }

  /// Train the index
  #[napi]
  pub fn train(&self) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    index
      .train()
      .map_err(|e| Error::from_reason(format!("Failed to train index: {e}")))
  }

  /// Insert a vector
  #[napi]
  pub fn insert(&self, vector_id: i64, vector: Vec<f64>) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    index
      .insert(vector_id as u64, &vector_f32)
      .map_err(|e| Error::from_reason(format!("Failed to insert vector: {e}")))
  }

  /// Delete a vector
  ///
  /// Requires the vector data to determine which cluster to remove from.
  #[napi]
  pub fn delete(&self, vector_id: i64, vector: Vec<f64>) -> Result<bool> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    Ok(index.delete(vector_id as u64, &vector_f32))
  }

  /// Clear the index
  #[napi]
  pub fn clear(&self) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    index.clear();
    Ok(())
  }

  /// Search for k nearest neighbors using PQ distance approximation
  #[napi]
  pub fn search(
    &self,
    manifest_json: String,
    query: Vec<f64>,
    k: i32,
    options: Option<JsSearchOptions>,
  ) -> Result<Vec<JsSearchResult>> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;

    // Parse manifest from JSON
    let manifest: VectorManifest = serde_json::from_str(&manifest_json)
      .map_err(|e| Error::from_reason(format!("Failed to parse manifest: {e}")))?;

    let query_f32: Vec<f32> = query.iter().map(|&v| v as f32).collect();

    let rust_options = options.map(|o| crate::vector::ivf_pq::IvfPqSearchOptions {
      n_probe: o.n_probe.map(|n| n as usize),
      filter: None,
      threshold: o.threshold.map(|t| t as f32),
    });

    let results = index.search(&manifest, &query_f32, k as usize, rust_options);
    Ok(results.into_iter().map(|r| r.into()).collect())
  }

  /// Search with multiple query vectors
  #[napi]
  pub fn search_multi(
    &self,
    manifest_json: String,
    queries: Vec<Vec<f64>>,
    k: i32,
    aggregation: JsAggregation,
    options: Option<JsSearchOptions>,
  ) -> Result<Vec<JsSearchResult>> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;

    // Parse manifest from JSON
    let manifest: VectorManifest = serde_json::from_str(&manifest_json)
      .map_err(|e| Error::from_reason(format!("Failed to parse manifest: {e}")))?;

    let queries_f32: Vec<Vec<f32>> = queries
      .iter()
      .map(|q| q.iter().map(|&v| v as f32).collect())
      .collect();

    let query_refs: Vec<&[f32]> = queries_f32.iter().map(|q| q.as_slice()).collect();

    let rust_options = options.map(|o| crate::vector::ivf_pq::IvfPqSearchOptions {
      n_probe: o.n_probe.map(|n| n as usize),
      filter: None,
      threshold: o.threshold.map(|t| t as f32),
    });

    let results = index.search_multi(
      &manifest,
      &query_refs,
      k as usize,
      aggregation.into(),
      rust_options,
    );
    Ok(results.into_iter().map(|r| r.into()).collect())
  }

  /// Get index statistics
  #[napi]
  pub fn stats(&self) -> Result<JsIvfStats> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let s = index.stats();
    Ok(JsIvfStats {
      trained: s.trained,
      n_clusters: s.n_clusters as i32,
      total_vectors: s.total_vectors as i64,
      avg_vectors_per_cluster: s.avg_vectors_per_cluster as f64,
      empty_cluster_count: s.empty_cluster_count as i32,
      min_cluster_size: s.min_cluster_size as i32,
      max_cluster_size: s.max_cluster_size as i32,
    })
  }

  /// Serialize the index to bytes
  #[napi]
  pub fn serialize(&self) -> Result<Buffer> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let bytes = crate::vector::ivf_pq::serialize_ivf_pq(&index);
    Ok(Buffer::from(bytes))
  }

  /// Deserialize an index from bytes
  #[napi(factory)]
  pub fn deserialize(data: Buffer) -> Result<JsIvfPqIndex> {
    let index = crate::vector::ivf_pq::deserialize_ivf_pq(&data)
      .map_err(|e| Error::from_reason(format!("Failed to deserialize: {e}")))?;
    Ok(JsIvfPqIndex {
      inner: RwLock::new(index),
    })
  }
}

// ============================================================================
// Brute Force Search (for small datasets or verification)
// ============================================================================

/// Brute force search result
#[napi(object)]
pub struct JsBruteForceResult {
  pub node_id: i64,
  pub distance: f64,
  pub similarity: f64,
}

/// Perform brute-force search over all vectors
///
/// Useful for small datasets or verifying IVF results.
#[napi]
pub fn brute_force_search(
  vectors: Vec<Vec<f64>>,
  node_ids: Vec<i64>,
  query: Vec<f64>,
  k: i32,
  metric: Option<JsDistanceMetric>,
) -> Result<Vec<JsBruteForceResult>> {
  if vectors.len() != node_ids.len() {
    return Err(Error::from_reason(
      "vectors and node_ids must have same length",
    ));
  }

  let metric = metric.unwrap_or(JsDistanceMetric::Cosine);
  let rust_metric: RustDistanceMetric = metric.into();
  let distance_fn = rust_metric.distance_fn();

  let query_f32: Vec<f32> = query.iter().map(|&v| v as f32).collect();

  let mut results: Vec<(i64, f32)> = vectors
    .iter()
    .zip(node_ids.iter())
    .map(|(v, &node_id)| {
      let v_f32: Vec<f32> = v.iter().map(|&x| x as f32).collect();
      let dist = distance_fn(&query_f32, &v_f32);
      (node_id, dist)
    })
    .collect();

  // Sort by distance
  results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
  results.truncate(k as usize);

  Ok(
    results
      .into_iter()
      .map(|(node_id, distance)| JsBruteForceResult {
        node_id,
        distance: distance as f64,
        similarity: rust_metric.distance_to_similarity(distance) as f64,
      })
      .collect(),
  )
}

// =============================================================================
// High-level VectorIndex API
// =============================================================================

/// Options for creating a vector index
#[napi(object)]
pub struct VectorIndexOptions {
  /// Vector dimensions (required)
  pub dimensions: i32,
  /// Distance metric (default: Cosine)
  pub metric: Option<JsDistanceMetric>,
  /// Vectors per row group (default: 1024)
  pub row_group_size: Option<i32>,
  /// Vectors per fragment before sealing (default: 100_000)
  pub fragment_target_size: Option<i32>,
  /// Whether to auto-normalize vectors (default: true for cosine)
  pub normalize: Option<bool>,
  /// IVF index configuration
  pub ivf: Option<JsIvfConfig>,
  /// Minimum training vectors before index training (default: 1000)
  pub training_threshold: Option<i32>,
  /// Maximum node IDs to cache for search results (default: 10_000)
  pub cache_max_size: Option<i32>,
}

impl VectorIndexOptions {
  fn into_rust(self) -> Result<RustVectorIndexOptions> {
    if self.dimensions <= 0 {
      return Err(Error::from_reason("dimensions must be positive"));
    }

    let mut options = RustVectorIndexOptions::new(self.dimensions as usize);

    if let Some(metric) = self.metric {
      options = options.with_metric(metric.into());
    }

    if let Some(row_group_size) = self.row_group_size {
      if row_group_size <= 0 {
        return Err(Error::from_reason("rowGroupSize must be positive"));
      }
      options = options.with_row_group_size(row_group_size as usize);
    }

    if let Some(fragment_target_size) = self.fragment_target_size {
      if fragment_target_size <= 0 {
        return Err(Error::from_reason("fragmentTargetSize must be positive"));
      }
      options = options.with_fragment_target_size(fragment_target_size as usize);
    }

    if let Some(ivf) = self.ivf {
      if let Some(n_clusters) = ivf.n_clusters {
        if n_clusters <= 0 {
          return Err(Error::from_reason("ivf.nClusters must be positive"));
        }
        options = options.with_n_clusters(n_clusters as usize);
      }

      if let Some(n_probe) = ivf.n_probe {
        if n_probe <= 0 {
          return Err(Error::from_reason("ivf.nProbe must be positive"));
        }
        options = options.with_n_probe(n_probe as usize);
      }
    }

    if let Some(training_threshold) = self.training_threshold {
      if training_threshold <= 0 {
        return Err(Error::from_reason("trainingThreshold must be positive"));
      }
      options = options.with_training_threshold(training_threshold as usize);
    }

    if let Some(cache_max_size) = self.cache_max_size {
      if cache_max_size <= 0 {
        return Err(Error::from_reason("cacheMaxSize must be positive"));
      }
      options = options.with_cache_max_size(cache_max_size as usize);
    }

    if let Some(normalize) = self.normalize {
      options = options.with_normalize(normalize);
    }

    Ok(options)
  }
}

/// Options for similarity search
#[napi(object)]
pub struct SimilarOptions {
  /// Number of results to return
  pub k: i32,
  /// Minimum similarity threshold (0-1 for cosine)
  pub threshold: Option<f64>,
  /// Number of clusters to probe for IVF (default: 10)
  pub n_probe: Option<i32>,
}

impl SimilarOptions {
  fn into_rust(self) -> Result<RustSimilarOptions> {
    if self.k <= 0 {
      return Err(Error::from_reason("k must be positive"));
    }

    let mut options = RustSimilarOptions::new(self.k as usize);
    if let Some(threshold) = self.threshold {
      options = options.with_threshold(threshold as f32);
    }
    if let Some(n_probe) = self.n_probe {
      if n_probe <= 0 {
        return Err(Error::from_reason("nProbe must be positive"));
      }
      options = options.with_n_probe(n_probe as usize);
    }
    Ok(options)
  }
}

/// Search result hit
#[napi(object)]
pub struct VectorSearchHit {
  pub node_id: i64,
  pub distance: f64,
  pub similarity: f64,
}

impl From<RustVectorSearchHit> for VectorSearchHit {
  fn from(hit: RustVectorSearchHit) -> Self {
    VectorSearchHit {
      node_id: hit.node_id as i64,
      distance: hit.distance as f64,
      similarity: hit.similarity as f64,
    }
  }
}

/// Vector index statistics
#[napi(object)]
pub struct VectorIndexStats {
  pub total_vectors: i64,
  pub live_vectors: i64,
  pub dimensions: i32,
  pub metric: JsDistanceMetric,
  pub index_trained: bool,
  pub index_clusters: Option<i32>,
}

impl From<RustVectorIndexStats> for VectorIndexStats {
  fn from(stats: RustVectorIndexStats) -> Self {
    VectorIndexStats {
      total_vectors: stats.total_vectors as i64,
      live_vectors: stats.live_vectors as i64,
      dimensions: stats.dimensions as i32,
      metric: stats.metric.into(),
      index_trained: stats.index_trained,
      index_clusters: stats.index_clusters.map(|v| v as i32),
    }
  }
}

fn map_vector_index_error(err: RustVectorIndexError) -> Error {
  Error::from_reason(err.to_string())
}

/// High-level vector index for similarity search
#[napi]
pub struct VectorIndex {
  inner: RwLock<RustVectorIndex>,
}

#[napi]
impl VectorIndex {
  /// Create a new vector index
  #[napi(constructor)]
  pub fn new(options: VectorIndexOptions) -> Result<Self> {
    let options = options.into_rust()?;
    Ok(VectorIndex {
      inner: RwLock::new(RustVectorIndex::new(options)),
    })
  }

  /// Set/update a vector for a node
  #[napi]
  pub fn set(&self, node_id: i64, vector: Vec<f64>) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let vector_f32: Vec<f32> = vector.iter().map(|&v| v as f32).collect();
    index
      .set(node_id as u64, &vector_f32)
      .map_err(map_vector_index_error)
  }

  /// Get the vector for a node (if any)
  #[napi]
  pub fn get(&self, node_id: i64) -> Result<Option<Vec<f64>>> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    Ok(
      index
        .get(node_id as u64)
        .map(|v| v.iter().map(|&x| x as f64).collect()),
    )
  }

  /// Delete the vector for a node
  #[napi]
  pub fn delete(&self, node_id: i64) -> Result<bool> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    index.delete(node_id as u64).map_err(map_vector_index_error)
  }

  /// Check if a node has a vector
  #[napi]
  pub fn has(&self, node_id: i64) -> Result<bool> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    Ok(index.has(node_id as u64))
  }

  /// Build/rebuild the IVF index for faster search
  #[napi]
  pub fn build_index(&self) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    index.build_index().map_err(map_vector_index_error)
  }

  /// Search for similar vectors
  #[napi]
  pub fn search(&self, query: Vec<f64>, options: SimilarOptions) -> Result<Vec<VectorSearchHit>> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    let query_f32: Vec<f32> = query.iter().map(|&v| v as f32).collect();
    let options = options.into_rust()?;
    let hits = index
      .search(&query_f32, options)
      .map_err(map_vector_index_error)?;
    Ok(hits.into_iter().map(VectorSearchHit::from).collect())
  }

  /// Get index statistics
  #[napi]
  pub fn stats(&self) -> Result<VectorIndexStats> {
    let index = self
      .inner
      .read()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    Ok(VectorIndexStats::from(index.stats()))
  }

  /// Clear all vectors and reset the index
  #[napi]
  pub fn clear(&self) -> Result<()> {
    let mut index = self
      .inner
      .write()
      .map_err(|e| Error::from_reason(e.to_string()))?;
    index.clear();
    Ok(())
  }
}

/// Create a new vector index
#[napi]
pub fn create_vector_index(options: VectorIndexOptions) -> Result<VectorIndex> {
  VectorIndex::new(options)
}