diskann 0.50.1

DiskANN is a fast approximate nearest neighbor search library for high dimensional data
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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

pub mod defaults;
pub mod experimental;

use std::num::{NonZeroU32, NonZeroUsize};

use thiserror::Error;

use crate::utils::IntoUsize;

////////////////
// Prune Kind //
////////////////

/// The occlusion factor between a vector `i` and an candidate neighbor `k` tracks how close
/// the vectors `i` and `k` are relative to all other vectors and are directly comparable
/// to the build parameter `alpha`.
///
/// A higher occlusion factor means that `j` and `k` are "more similar" than `i` and `k`.
/// The pruning rules are heuristics established such that using higher values of `alpha`
/// yields sparser graphs.
///
/// ```text
/// i (candidate vector) ----------> k
///                                 /
///           j -------------------*
/// ```
///
/// Some details on the implementation are given below.
///
/// ## TriangleInequality (Euclidean and Cosine)
///
/// The occlusion factor is the highest ratio observed between `distance_ik` and
/// `distance_jk` for all candidates `j` that have been successfully added to `i`'s
/// adjacency list.
///
/// If vectors `j` and `k` are close, then the ratio `distance_ik / distance_jk` will be
/// high, potentially excluding `k` from consideration as a neighbor of `i`.
///
/// ## Occluding (Inner Product)
///
/// Remember that the similarity score for inner product is a negative value (for
/// sufficiently similar vectors).
///
/// The rule here states that if the similarity between `j` and `k` is `current_alpha`
/// times better than the similarity between `i` and `k`, then `k` should be removed
/// entirely as a neighbor candidate (achieved by settings its occlusion factor to
/// `f32::MAX`.
///
/// When alpha is greater, this requirement is more stringent, so higher alphas lead
/// to less dense graphs.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum PruneKind {
    TriangleInequality,
    Occluding,
}

const OCCLUDING_MASK: f32 = 0.01;

impl PruneKind {
    /// Construct a new [`PruneKind`] tailored for `metric`.
    ///
    /// For L2 and cosine variants, this returns [`Self::TriangleInequality`].
    /// For inner product variants, this returns [`Self:Occluding`].
    pub fn from_metric(metric: diskann_vector::distance::Metric) -> Self {
        use diskann_vector::distance::Metric;
        match metric {
            Metric::L2 | Metric::Cosine | Metric::CosineNormalized => Self::TriangleInequality,
            Metric::InnerProduct => Self::Occluding,
        }
    }

    /// Run the associate pruning rule.
    ///
    /// See the struct level documentation for [`PruneKind`] for details.
    pub fn update_occlude_factor(
        self,
        distance_ik: f32,
        distance_jk: f32,
        current_factor: f32,
        current_alpha: f32,
    ) -> f32 {
        match self {
            Self::TriangleInequality => {
                if distance_jk == 0.0 {
                    f32::MAX
                } else {
                    current_factor.max(distance_ik / distance_jk)
                }
            }
            Self::Occluding => {
                if distance_jk < current_alpha * distance_ik {
                    current_alpha + OCCLUDING_MASK
                } else {
                    current_factor
                }
            }
        }
    }
}

impl From<diskann_vector::distance::Metric> for PruneKind {
    fn from(metric: diskann_vector::distance::Metric) -> PruneKind {
        Self::from_metric(metric)
    }
}

/// Controls the number of edges considered among an insert batch for the multi-insert API.
///
/// When inserted data is temporally correlated (i.e., elements within a batch are likely
/// to be neighbors of each other), setting this to a high value can help prevent recall
/// degradation due to batching.
///
/// Note that high-levels of intra-batch candidates coupled with high batch sizes can
/// increase insertion time.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum IntraBatchCandidates {
    /// No intra-batch candidates will be considered. This is useful when doing a bulk
    /// ingestion of non-temporally correlated data as neglecting edges within an insert
    /// batch is unlikely to affect recall.
    None,

    /// Consider up `max` candidates within a batch. Candidate generation for a batch item `i`
    /// will be taken from the position-wise neighboring items centered around `i`.
    ///
    /// In the example below, setting `max = 4` will result in the entries 2, 3, 5, and 6
    /// being considered as edge candidates for item `4`.
    ///
    /// ```text
    /// Intra Batch Candidates = 4
    ///
    ///              Intra-Batch Candidates of 4
    ///                   |---|-------|---|
    /// Batch Items:  1   2   3   4   5   6   7   8   9
    ///                           |
    ///                  Item Being Processed
    /// ```
    Max(NonZeroU32),

    /// All elements within a batch will be considered for canidates. This will generate the
    /// highest recall, but come at a performance penalty for large batches.
    #[default]
    All,
}

impl IntraBatchCandidates {
    /// If `value` is non-zero, return `Self::NonZero(value)`. Otherwise, return `Self::None`.
    pub const fn new(value: u32) -> Self {
        match NonZeroU32::new(value) {
            None => Self::None,
            Some(max) => Self::Max(max),
        }
    }

    /// Return the number of candidates to consider out of a batch of size `batch_size`.
    pub fn get(&self, batch_size: usize) -> usize {
        match self {
            Self::None => 0,
            Self::Max(max) => max.get().into_usize().min(batch_size),
            Self::All => batch_size,
        }
    }

    /// Return `true` if `self == IntraBatchParallelism::None`.
    pub const fn is_none(&self) -> bool {
        matches!(self, Self::None)
    }
}

////////////
// Config //
////////////

/// Configuration state for index construction operations.
#[derive(Debug, Clone, PartialEq)]
pub struct Config {
    /// The degree that adjacency lists are pruned to.
    pruned_degree: NonZeroU32,

    /// The maximum degree in the graph. When adjacency lists exceed this degree, a prune
    /// is triggered.
    max_degree: NonZeroU32,

    /// The default search window size to use for build.
    l_build: NonZeroU32,

    /// The alpha value used for pruning.
    alpha: f32,

    /// The pruning occlusion approach to take.
    prune_kind: PruneKind,

    /// The upper-bound of occlusion list sizes.
    max_occlusion_size: NonZeroU32,

    /// Maximum number of backedges applied.
    max_backedges: NonZeroU32,

    /// Max minibatch insert parallelism. Set to Some(x) to use minibatch_insert.
    /// When minibatch_insert receives more than this number of vectors,
    /// it will split the insert into rounds each with at most this many parallel tasks.
    max_minibatch_par: NonZeroU32,

    /// The number of intra-batch candidates to consider during multi-insert.
    intra_batch_candidates: IntraBatchCandidates,

    /// Whether to attempt graph saturation after all prunes.
    saturate_after_prune: bool,

    /// Experiemntal fields.
    experimental_insert_retry: Option<experimental::InsertRetry>,
}

/// Allow conversion from `NonZeroU32` to `NonZeroUsize`.
///
/// LLVM can recognice when this conversion is infallible and will emit code that never
/// panics.
macro_rules! to_nonzero_usize {
    ($($x:tt)*) => {{
        let y: NonZeroU32 = $($x)*;

        const {
            assert!(std::mem::size_of::<NonZeroUsize>() >= std::mem::size_of::<NonZeroU32>())
        };

        // Lint: Infallible on 64-bit systems.
        #[allow(clippy::unwrap_used)]
        <NonZeroUsize as TryFrom<NonZeroU32>>::try_from(y).unwrap()
    }}
}
pub(super) use to_nonzero_usize;

impl Config {
    /// Attempt to construct a [`Config`] from a builder.
    ///
    /// See: [`Builder::build`].
    pub fn try_from_builder(builder: Builder) -> Result<Self, ConfigError> {
        let non_zero_error =
            |param: &'static str, val: usize| -> Result<NonZeroU32, ConfigErrorInner> {
                try_nonzero_u32(val).map_err(|err| ConfigErrorInner::Parameter(param, err))
            };

        // TODO: Error checking for alpha.
        let alpha = builder.alpha.unwrap_or(defaults::ALPHA);

        let pruned_degree = non_zero_error("pruned_degree", builder.pruned_degree)?;

        let max_degree = match builder.max_degree {
            MaxDegree::Value(max) => non_zero_error("max_degree", max)?,
            MaxDegree::Slack(slack) => {
                if slack < 1.0 || !slack.is_finite() {
                    return Err(ConfigErrorInner::Slack(InvalidSlack(slack)).into());
                }
                non_zero_error(
                    "max_degree (from slack)",
                    (slack * pruned_degree.get() as f32) as usize,
                )?
            }
            MaxDegree::Same => pruned_degree,
        };

        if max_degree < pruned_degree {
            return Err(ConfigErrorInner::Degrees(max_degree.get(), pruned_degree.get()).into());
        }

        let l_build = non_zero_error("l_build", builder.l_build)?;

        let max_occlusion_size = match builder.max_occlusion_size {
            Some(max) => non_zero_error("max_occlusion_size", max)?,
            None => defaults::MAX_OCCLUSION_SIZE,
        };

        let max_backedges = match builder.backedge_spec {
            Some(spec) => match spec {
                BackedgeSpec::Ratio(ratio) => {
                    if !ratio.is_finite() || ratio <= 0.0 || ratio > 1.0 {
                        return Err(ConfigErrorInner::BackedgeRatio(ratio).into());
                    }
                    non_zero_error(
                        "backedge_ratio (from slack)",
                        (ratio * pruned_degree.get() as f32).ceil() as usize,
                    )?
                }
                BackedgeSpec::Amount(amount) => non_zero_error("max_backedges", amount)?,
            },
            None => pruned_degree,
        };

        if max_backedges > pruned_degree {
            return Err(
                ConfigErrorInner::Backedges(max_backedges.get(), pruned_degree.get()).into(),
            );
        }

        let max_minibatch_par = match builder.max_minibatch_par {
            Some(par) => non_zero_error("max_minibatch_par", par)?,
            None => defaults::MAX_MINIBATCH_PARALLELISM,
        };

        let intra_batch_candidates = builder
            .intra_batch_candidates
            .unwrap_or(defaults::INTRA_BATCH_CANDIDATES);

        let saturate_after_prune = builder.saturate_after_prune.unwrap_or(false);

        let config = Self {
            pruned_degree,
            max_degree,
            l_build,
            alpha,
            prune_kind: builder.prune_kind,
            max_occlusion_size,
            max_backedges,
            max_minibatch_par,
            intra_batch_candidates,
            saturate_after_prune,
            experimental_insert_retry: builder.insert_retry,
        };
        Ok(config)
    }

    //-----------//
    // Accessors //
    //-----------//

    pub fn pruned_degree(&self) -> NonZeroUsize {
        const { assert!(std::mem::size_of::<usize>() >= std::mem::size_of::<u32>()) };
        // Lint: Infallible on 64-bit systems.
        #[expect(clippy::unwrap_used)]
        self.pruned_degree_u32().try_into().unwrap()
    }

    pub fn max_degree(&self) -> NonZeroUsize {
        to_nonzero_usize!(self.max_degree_u32())
    }

    pub fn l_build(&self) -> NonZeroUsize {
        to_nonzero_usize!(self.l_build_u32())
    }

    pub fn alpha(&self) -> f32 {
        self.alpha
    }

    pub fn prune_kind(&self) -> PruneKind {
        self.prune_kind
    }

    pub fn max_occlusion_size(&self) -> NonZeroUsize {
        to_nonzero_usize!(self.max_occlusion_size_u32())
    }

    pub fn max_backedges(&self) -> NonZeroUsize {
        to_nonzero_usize!(self.max_backedges_u32())
    }

    pub fn max_minibatch_par(&self) -> NonZeroUsize {
        to_nonzero_usize!(self.max_minibatch_par_u32())
    }

    pub fn intra_batch_candidates(&self) -> IntraBatchCandidates {
        self.intra_batch_candidates
    }

    pub fn saturate_after_prune(&self) -> bool {
        self.saturate_after_prune
    }

    pub fn experimental_insert_retry(&self) -> Option<&experimental::InsertRetry> {
        self.experimental_insert_retry.as_ref()
    }

    //---------------//
    // u32 accessors //
    //---------------//

    pub fn pruned_degree_u32(&self) -> NonZeroU32 {
        self.pruned_degree
    }

    pub fn max_degree_u32(&self) -> NonZeroU32 {
        self.max_degree
    }

    pub fn l_build_u32(&self) -> NonZeroU32 {
        self.l_build
    }

    pub fn max_occlusion_size_u32(&self) -> NonZeroU32 {
        self.max_occlusion_size
    }

    pub fn max_backedges_u32(&self) -> NonZeroU32 {
        self.max_backedges
    }

    pub fn max_minibatch_par_u32(&self) -> NonZeroU32 {
        self.max_minibatch_par
    }
}

/// Errors that can occur when building a [`Config`].
///
/// See [`Builder::build`] for possible failure modes.
#[derive(Debug, Clone, Error)]
#[error(transparent)]
pub struct ConfigError {
    #[from]
    inner: ConfigErrorInner,
}

impl From<ConfigError> for crate::ANNError {
    fn from(error: ConfigError) -> Self {
        crate::ANNError::new(crate::ANNErrorKind::IndexConfigError, error)
    }
}

#[derive(Debug, Clone, Error)]
enum ConfigErrorInner {
    #[error("parameter \"{0}\" invalid because {1}")]
    Parameter(&'static str, NotNonZeroU32),
    #[error("parameter \"max_degree\" invalid because {0}")]
    Slack(InvalidSlack),
    #[error("parameter \"max_degree\" ({0}) must not be less than \"pruned_degree\" ({1})")]
    Degrees(u32, u32),
    #[error("parameter \"max_backedges\" ({0}) must not be greater than \"pruned_degree\" ({1})")]
    Backedges(u32, u32),
    #[error("parameter \"backedge_ratio\" ({0}) as ratio invalid because must be in (0.0, 1.0]")]
    BackedgeRatio(f32),
}

fn try_nonzero_u32(x: usize) -> Result<NonZeroU32, NotNonZeroU32> {
    let y: u32 = x.try_into().map_err(|_| NotNonZeroU32(x))?;
    NonZeroU32::new(y).ok_or(NotNonZeroU32(x))
}

#[derive(Debug, Clone)]
struct NotNonZeroU32(usize);

impl std::fmt::Display for NotNonZeroU32 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0 == 0 {
            write!(f, "it cannot be zero")
        } else if self.0 > (u32::MAX as usize) {
            write!(f, "its value ({}) exceeds u32::MAX", self.0)
        } else {
            // Shouldn't reach here.
            Ok(())
        }
    }
}

#[derive(Debug, Clone)]
struct InvalidSlack(f32);

impl std::fmt::Display for InvalidSlack {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if !self.0.is_finite() {
            write!(f, "it must be finite, not {}", self.0)
        } else if self.0 < 1.0 {
            write!(f, "it must be greater than 1.0 (instead, it is {})", self.0)
        } else {
            // Shouldn't reach here.
            Ok(())
        }
    }
}

/// Configuration setting for the maximum graph degree.
///
/// Graph degree is conrolled by two values, the "pruned degree" (what pruning aims to
/// achieve), and the "maximum degree" (the largest an adjacency list can get before pruning
/// is triggered).
///
/// Having this wiggle room greatly reduces the number of prunes required during backedge
/// insertion.
#[derive(Debug, Clone, Copy)]
pub enum MaxDegree {
    /// Specify the maximum degree as an absolute vlue.
    Value(usize),

    /// Specify the maximum degree as a value relative to the pruned degree. This requires
    /// that the contained scaling parameter be larger than or equal to 1.0.
    Slack(f32),

    /// Specify the maximum degree to be the same as the pruned degree. This is not
    /// recommended for insert workloads as it can lead to excessive pruning, but is
    /// suitable for search-only workloads.
    Same,
}

impl MaxDegree {
    /// Construct a new [`MaxDegree`] for the specified exact value. Note that this must
    /// be greater than or equal to the associated pruned degree.
    pub const fn new(value: usize) -> Self {
        Self::Value(value)
    }

    /// Construct a new [`MaxDegree`] as a value relative to the pruned degree. The true
    /// max degree will be calculated as
    /// ```math
    /// floor(pruned_degree * slack)
    /// ```
    /// Note that `slack` must be finite, non-zero, band must not cause the computed max
    /// degree to exceed `u32::MAX`.
    pub const fn slack(slack: f32) -> Self {
        Self::Slack(slack)
    }

    /// Construct a new [`MaxDegree`] using a heuristically reasonable default.
    pub const fn default_slack() -> Self {
        Self::slack(defaults::GRAPH_SLACK_FACTOR)
    }

    /// Construct a new [`MaxDegree`] that will be the same as the pruned degree.
    pub const fn same() -> Self {
        Self::Same
    }
}

// Allow the number of back edges to be specified as either a total amount, or an amount
// relative to the maximum graph degree.
enum BackedgeSpec {
    Ratio(f32),
    Amount(usize),
}

/// A builder for for [`Config`]. Necessary invariants among the fields will be checked
/// upon invoking [`Config::build`].
///
/// See [`Config::build`] for details.
pub struct Builder {
    pruned_degree: usize,
    max_degree: MaxDegree,
    l_build: usize,
    prune_kind: PruneKind,

    // optional //
    alpha: Option<f32>,
    max_occlusion_size: Option<usize>,
    backedge_spec: Option<BackedgeSpec>,
    max_minibatch_par: Option<usize>,
    intra_batch_candidates: Option<IntraBatchCandidates>,
    saturate_after_prune: Option<bool>,

    /// Experimental additions.
    insert_retry: Option<experimental::InsertRetry>,
}

impl Builder {
    /// Construct a new builder with the basic values.
    ///
    /// All other parameters will use their default values.
    ///
    /// # Note
    ///
    /// The choice of [`prune_kind`](PruneKind) is distance-function-dependent and can have a
    /// profoundly negative impact on the graph quality if chosen incorrectly. When in doubt
    /// prefer to use [`diskann_vector::distance::Metric`]'s conversion function like the
    /// example below:
    /// ```rust
    /// use diskann::graph::config;
    /// use diskann_vector::distance::Metric;
    ///
    /// let _ = config::Builder::new(
    ///     10,
    ///     config::MaxDegree::default_slack(),
    ///     20,
    ///     (Metric::L2).into()
    /// );
    /// ```
    pub fn new(
        pruned_degree: usize,
        max_degree: MaxDegree,
        l_build: usize,
        prune_kind: PruneKind,
    ) -> Self {
        Self {
            pruned_degree,
            max_degree,
            l_build,
            prune_kind,
            alpha: None,
            max_occlusion_size: None,
            backedge_spec: None,
            max_minibatch_par: None,
            intra_batch_candidates: None,
            saturate_after_prune: None,
            insert_retry: None,
        }
    }

    /// Construct a new builder with the basic values.
    ///
    /// All other parameters will use their default values.
    ///
    /// A closure `f` can be used to chain additional builder methods inline.
    pub fn new_with<F>(
        pruned_degree: usize,
        max_degree: MaxDegree,
        l_build: usize,
        prune_kind: PruneKind,
        f: F,
    ) -> Self
    where
        F: FnOnce(&mut Self),
    {
        let mut this = Self::new(pruned_degree, max_degree, l_build, prune_kind);
        f(&mut this);
        this
    }

    /// Configure the `alpha` parameter used during pruning.
    ///
    /// Values closer to 1.0 will yield denser graphs at the cost of increased build time.
    pub fn alpha(&mut self, alpha: f32) -> &mut Self {
        self.alpha = Some(alpha);
        self
    }

    /// Configure the search window size used during the search phase of index construction.
    ///
    /// Parameter `size` must be non-zero and not exceed `u32::MAX`.
    pub fn l_build(&mut self, size: usize) -> &mut Self {
        self.l_build = size;
        self
    }

    /// Configure the maximum number of candidates provided to prune.
    ///
    /// Parameter `size` must be non-zero and not exceed `u32::MAX`.
    pub fn max_occlusion_size(&mut self, size: usize) -> &mut Self {
        self.max_occlusion_size = Some(size);
        self
    }

    /// Configure the number of backedges as a ratio of the pruned degree.
    ///
    /// Parameter `ratio` must be in the interval `(0.0, 1.0]`.
    ///
    /// Setting this parameter will invalidate any previous value from [`Self::max_backedges`].
    pub fn backedge_ratio(&mut self, ratio: f32) -> &mut Self {
        self.backedge_spec = Some(BackedgeSpec::Ratio(ratio));
        self
    }

    /// Configure the number of backedges as an absolute value.
    ///
    /// Parameter `max` must be non-zero, not exceed `u32::MAX`, and must not be greater than
    /// `pruned_degree`.
    ///
    /// Setting this parameter will invalidate any previous value from [`Self::backedge_ratio`].
    pub fn max_backedges(&mut self, max: usize) -> &mut Self {
        self.backedge_spec = Some(BackedgeSpec::Amount(max));
        self
    }

    /// Configure that maximum parallelism used by the multi-insert APIs.
    ///
    /// Parameter `par` must be non-zero and not exceed `u32::MAX`.
    pub fn max_minibatch_par(&mut self, par: usize) -> &mut Self {
        self.max_minibatch_par = Some(par);
        self
    }

    /// Configure the intra-batch candidates.
    pub fn intra_batch_candidates(&mut self, candidates: IntraBatchCandidates) -> &mut Self {
        self.intra_batch_candidates = Some(candidates);
        self
    }

    /// Configure whether the adjacency lists are saturated after every pruning step.
    pub fn saturate_after_prune(&mut self, to_saturate: bool) -> &mut Self {
        self.saturate_after_prune = Some(to_saturate);
        self
    }

    /// Enable the experimental insert retry algorithm.
    pub fn insert_retry(&mut self, insert_retry: experimental::InsertRetry) -> &mut Self {
        self.insert_retry = Some(insert_retry);
        self
    }

    /// Attempt to build the config. Fails if:
    ///
    /// * The resolved `max_degree` is less than `pruned_degree`.
    /// * The resolved number of backedges is greater than `pruned_degree`.
    /// * Any parameter is either 0 or exceeds `u32::MAX`.
    pub fn build(self) -> Result<Config, ConfigError> {
        Config::try_from_builder(self)
    }
}

///////////
// Tests //
///////////

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

    const SLACK: MaxDegree = MaxDegree::default_slack();
    const TOO_BIG: usize = 5_000_000_000;

    /// Utility to help check error messages.
    macro_rules! check_msg {
        ($msg:ident, $expected:literal $(,)?) => {
            assert_eq!($msg, $expected, "failed with: {}", $msg,);
        };
    }

    #[test]
    fn test_intra_batch_candidates() {
        assert_eq!(
            IntraBatchCandidates::default(),
            defaults::INTRA_BATCH_CANDIDATES
        );

        assert_eq!(IntraBatchCandidates::new(0), IntraBatchCandidates::None);
        assert_eq!(
            IntraBatchCandidates::new(10),
            IntraBatchCandidates::Max(NonZeroU32::new(10).unwrap())
        );

        // None
        {
            let c = IntraBatchCandidates::None;
            for i in 0..10 {
                assert_eq!(c.get(i), 0);
            }
        }

        // All
        {
            let c = IntraBatchCandidates::All;
            for i in 0..10 {
                assert_eq!(c.get(i), i);
            }
        }

        // Max
        {
            let c = IntraBatchCandidates::new(5);
            for i in 0..10 {
                if i <= 5 {
                    assert_eq!(c.get(i), i);
                } else {
                    assert_eq!(c.get(i), 5);
                }
            }
        }
    }

    #[test]
    fn test_defaults() {
        let prune_kind = PruneKind::TriangleInequality;
        let config = Builder::new(100, SLACK, 50, prune_kind).build().unwrap();

        assert_eq!(config.pruned_degree().get(), 100);
        assert_eq!(
            config.max_degree().get(),
            130,
            "default slack should be 1.3"
        );
        assert_eq!(config.l_build().get(), 50);
        assert_eq!(config.alpha(), defaults::ALPHA);
        assert_eq!(config.prune_kind(), prune_kind);
        assert_eq!(
            config.max_occlusion_size_u32(),
            defaults::MAX_OCCLUSION_SIZE
        );
        assert_eq!(
            config.max_backedges().get(),
            100,
            "backedges should equal pruned degree"
        );
        assert_eq!(
            config.max_minibatch_par_u32(),
            defaults::MAX_MINIBATCH_PARALLELISM
        );
        assert_eq!(
            config.intra_batch_candidates(),
            defaults::INTRA_BATCH_CANDIDATES
        );

        assert_eq!(
            config.saturate_after_prune(),
            defaults::SATURATE_AFTER_PRUNE
        );
        assert!(config.experimental_insert_retry().is_none());
    }

    #[test]
    fn test_pruned_degree() {
        let prune_kind = PruneKind::TriangleInequality;

        for i in [10, 20, 30] {
            let config = Builder::new(i, SLACK, 50, prune_kind).build().unwrap();
            assert_eq!(config.pruned_degree().get(), i);
        }

        let msg = Builder::new(0, SLACK, 50, prune_kind)
            .build()
            .unwrap_err()
            .to_string();
        check_msg!(
            msg,
            "parameter \"pruned_degree\" invalid because it cannot be zero",
        );

        let msg = Builder::new(TOO_BIG, SLACK, 50, prune_kind)
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"pruned_degree\" invalid because its value (5000000000) exceeds u32::MAX",
        );
    }

    #[test]
    fn test_max_degree() {
        let prune_kind = PruneKind::TriangleInequality;
        for i in [10, 20, 30] {
            let config = Builder::new(10, MaxDegree::new(i), 50, prune_kind)
                .build()
                .unwrap();

            assert_eq!(config.max_degree().get(), i);
        }

        for (slack, expected) in [(1.0, 11), (1.2, 13), (1.3, 14)] {
            let config = Builder::new(11, MaxDegree::slack(slack), 50, prune_kind)
                .build()
                .unwrap();

            assert_eq!(config.max_degree().get(), expected);
        }

        // Errors
        let msg = Builder::new(10, MaxDegree::new(0), 50, prune_kind)
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_degree\" invalid because it cannot be zero",
        );

        let msg = Builder::new(10, MaxDegree::new(9), 50, prune_kind)
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_degree\" (9) must not be less than \"pruned_degree\" (10)",
        );

        let msg = Builder::new(10, MaxDegree::slack(0.5), 50, prune_kind)
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_degree\" invalid because it must be greater than 1.0 (instead, it is 0.5)",
        );

        let msg = Builder::new(10, MaxDegree::slack(f32::NAN), 50, prune_kind)
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_degree\" invalid because it must be finite, not NaN",
        );

        let msg = Builder::new(10, MaxDegree::slack(5_000_000_000.0), 50, prune_kind)
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_degree (from slack)\" invalid because its value (49999998976) exceeds u32::MAX",
        );
    }

    #[test]
    fn test_alpha() {
        fn f(v: f32) -> impl FnOnce(&mut Builder) {
            move |b| {
                b.alpha(v);
            }
        }

        let prune_kind = PruneKind::TriangleInequality;
        for i in [1.0, 1.1, 1.2] {
            let config = Builder::new_with(10, SLACK, 10, prune_kind, f(i))
                .build()
                .unwrap();

            assert_eq!(config.alpha(), i);
        }
    }

    #[test]
    fn test_max_occlusion_size() {
        fn f(v: usize) -> impl FnOnce(&mut Builder) {
            move |b| {
                b.max_occlusion_size(v);
            }
        }

        let prune_kind = PruneKind::TriangleInequality;
        for i in [10, 20, 30] {
            let config = Builder::new_with(10, SLACK, 10, prune_kind, f(i))
                .build()
                .unwrap();

            assert_eq!(config.max_occlusion_size().get(), i);
        }

        let msg = Builder::new_with(10, SLACK, 10, prune_kind, f(0))
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_occlusion_size\" invalid because it cannot be zero",
        );

        let msg = Builder::new_with(10, SLACK, 10, prune_kind, f(TOO_BIG))
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_occlusion_size\" invalid because its value (5000000000) exceeds u32::MAX",
        );
    }

    #[test]
    fn test_l_build() {
        let prune_kind = PruneKind::TriangleInequality;
        for i in [10, 20, 30] {
            let config = Builder::new(10, SLACK, i, prune_kind).build().unwrap();

            assert_eq!(config.l_build().get(), i);
        }

        let msg = Builder::new(10, SLACK, 0, prune_kind)
            .build()
            .unwrap_err()
            .to_string();
        check_msg!(
            msg,
            "parameter \"l_build\" invalid because it cannot be zero",
        );

        let msg = Builder::new(10, SLACK, TOO_BIG, prune_kind)
            .build()
            .unwrap_err()
            .to_string();
        check_msg!(
            msg,
            "parameter \"l_build\" invalid because its value (5000000000) exceeds u32::MAX",
        );
    }

    #[test]
    fn test_backedge_ratio() {
        fn f(v: f32) -> impl FnOnce(&mut Builder) {
            move |b| {
                b.backedge_ratio(v);
            }
        }

        let prune_kind = PruneKind::TriangleInequality;

        // Check that the `ceil` operation is performed.
        for (ratio, expected) in [(0.5, 6), (0.8, 9), (1.0, 11)] {
            let config = Builder::new_with(11, SLACK, 10, prune_kind, f(ratio))
                .build()
                .unwrap();

            assert_eq!(config.max_backedges().get(), expected);
        }

        let msg = Builder::new_with(10, SLACK, 10, prune_kind, f(0.0))
            .build()
            .unwrap_err()
            .to_string();
        check_msg!(
            msg,
            "parameter \"backedge_ratio\" (0) as ratio invalid because must be in (0.0, 1.0]",
        );
    }

    #[test]
    fn test_max_backedges() {
        fn f(v: usize) -> impl FnOnce(&mut Builder) {
            move |b| {
                b.max_backedges(v);
            }
        }

        let prune_kind = PruneKind::TriangleInequality;

        // Check that the `ceil` operation is performed.
        for i in [1, 2, 11] {
            let config = Builder::new_with(11, SLACK, 10, prune_kind, f(i))
                .build()
                .unwrap();

            assert_eq!(config.max_backedges().get(), i);
        }

        let msg = Builder::new_with(10, SLACK, 10, prune_kind, f(0))
            .build()
            .unwrap_err()
            .to_string();
        check_msg!(
            msg,
            "parameter \"max_backedges\" invalid because it cannot be zero",
        );

        let msg = Builder::new_with(10, SLACK, 10, prune_kind, f(TOO_BIG))
            .build()
            .unwrap_err()
            .to_string();
        check_msg!(
            msg,
            "parameter \"max_backedges\" invalid because its value (5000000000) exceeds u32::MAX",
        );

        let msg = Builder::new_with(10, SLACK, 10, prune_kind, f(11))
            .build()
            .unwrap_err()
            .to_string();
        check_msg!(
            msg,
            "parameter \"max_backedges\" (11) must not be greater than \"pruned_degree\" (10)",
        );
    }

    #[test]
    fn test_max_minibatch_par() {
        fn f(v: usize) -> impl FnOnce(&mut Builder) {
            move |b| {
                b.max_minibatch_par(v);
            }
        }

        let prune_kind = PruneKind::TriangleInequality;
        for i in [10, 20, 30] {
            let config = Builder::new_with(10, SLACK, 10, prune_kind, f(i))
                .build()
                .unwrap();

            assert_eq!(config.max_minibatch_par().get(), i);
        }

        let msg = Builder::new_with(10, SLACK, 10, prune_kind, f(0))
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_minibatch_par\" invalid because it cannot be zero",
        );

        let msg = Builder::new_with(10, SLACK, 10, prune_kind, f(TOO_BIG))
            .build()
            .unwrap_err()
            .to_string();

        check_msg!(
            msg,
            "parameter \"max_minibatch_par\" invalid because its value (5000000000) exceeds u32::MAX",
        );
    }

    #[test]
    fn test_intra_batch_candidates_builder() {
        fn f(v: IntraBatchCandidates) -> impl FnOnce(&mut Builder) {
            move |b| {
                b.intra_batch_candidates(v);
            }
        }

        // None
        let config = Builder::new_with(
            10,
            SLACK,
            10,
            PruneKind::TriangleInequality,
            f(IntraBatchCandidates::None),
        )
        .build()
        .unwrap();

        assert_eq!(config.intra_batch_candidates(), IntraBatchCandidates::None);

        // All
        let config = Builder::new_with(
            10,
            SLACK,
            10,
            PruneKind::TriangleInequality,
            f(IntraBatchCandidates::All),
        )
        .build()
        .unwrap();

        assert_eq!(config.intra_batch_candidates(), IntraBatchCandidates::All);

        // Max
        for m in [1, 2, 10, 100] {
            let nz = NonZeroU32::new(m).unwrap();
            let config = Builder::new_with(
                10,
                SLACK,
                10,
                PruneKind::TriangleInequality,
                f(IntraBatchCandidates::Max(nz)),
            )
            .build()
            .unwrap();

            assert_eq!(
                config.intra_batch_candidates(),
                IntraBatchCandidates::Max(nz)
            );
        }
    }

    #[test]
    fn test_prune_kind() {
        let config = Builder::new(10, SLACK, 10, PruneKind::TriangleInequality)
            .build()
            .unwrap();

        assert_eq!(config.prune_kind(), PruneKind::TriangleInequality);

        let config = Builder::new(10, SLACK, 10, PruneKind::Occluding)
            .build()
            .unwrap();

        assert_eq!(config.prune_kind(), PruneKind::Occluding);
    }

    #[test]
    fn test_saturate_after_prune() {
        fn f(v: bool) -> impl FnOnce(&mut Builder) {
            move |b| {
                b.saturate_after_prune(v);
            }
        }

        let prune_kind = PruneKind::TriangleInequality;
        for i in [true, false] {
            let config = Builder::new_with(10, SLACK, 10, prune_kind, f(i))
                .build()
                .unwrap();

            assert_eq!(config.saturate_after_prune(), i);
        }
    }

    #[test]
    fn test_experimental() {
        let retry = experimental::InsertRetry::new(
            NonZeroU32::new(3).unwrap(),
            NonZeroU32::new(10).unwrap(),
            true,
        );
        let config = Builder::new_with(10, SLACK, 10, PruneKind::TriangleInequality, |b| {
            b.insert_retry(retry.clone());
        })
        .build()
        .unwrap();

        assert_eq!(config.experimental_insert_retry().unwrap(), &retry);
    }

    //------------//
    // Prune Kind //
    //------------//

    #[test]
    fn test_prune_kind_conversion() {
        let x: PruneKind = diskann_vector::distance::Metric::L2.into();
        assert_eq!(x, PruneKind::TriangleInequality);

        let x: PruneKind = diskann_vector::distance::Metric::Cosine.into();
        assert_eq!(x, PruneKind::TriangleInequality);

        let x: PruneKind = diskann_vector::distance::Metric::CosineNormalized.into();
        assert_eq!(x, PruneKind::TriangleInequality);

        let x: PruneKind = diskann_vector::distance::Metric::InnerProduct.into();
        assert_eq!(x, PruneKind::Occluding);
    }

    #[test]
    fn test_update_occlude_factor() {
        // Triangle Inequality
        let kind = PruneKind::TriangleInequality;

        // Regardless of `distance_ik`, we should yield `f32::MAX` when `distance_jk == 0`.
        for distance_ik in [f32::MIN, -1.2, 0.0, 0.123, 50.0, f32::MAX] {
            assert_eq!(
                kind.update_occlude_factor(distance_ik, 0.0, 1.0, 2.0),
                f32::MAX
            );
        }

        // Behavior of `update_occlude_factor` for these metrics should not depend
        // on `current_alpha`.
        for current_alpha in [1.0, 1.1, 1.2, 1.3] {
            // current factor less than `distance_ik / distance_jk`.
            assert_eq!(
                kind.update_occlude_factor(2.0, 1.0, 1.0, current_alpha),
                2.0
            );
            // current factor equal to `distance_ik / distance_jk`.
            assert_eq!(
                kind.update_occlude_factor(2.0, 1.0, 2.0, current_alpha),
                2.0
            );
            // current factor greater to `distance_ik / distance_jk`.
            assert_eq!(
                kind.update_occlude_factor(2.0, 1.0, 3.0, current_alpha),
                3.0
            );
        }

        // Occluding
        let kind = PruneKind::Occluding;

        // Test `distance_jk > current_alpha * distance_ik`.
        let current_factor = 0.0;
        assert_eq!(
            kind.update_occlude_factor(-2.0, -1.0, current_factor, 3.0),
            current_factor
        );
        assert_eq!(
            kind.update_occlude_factor(-3.0, -2.0, current_factor, 1.0),
            current_factor
        );

        // Test `distance_jk == current_alpha * distance_ik`.
        assert_eq!(
            kind.update_occlude_factor(-3.0, -3.0, current_factor, 1.0),
            current_factor
        );

        // Test `distance_jk < current_alpha * distance_ik`.
        assert_eq!(
            kind.update_occlude_factor(-3.0, -4.0, current_factor, 1.0),
            1.0 + OCCLUDING_MASK,
        );
    }
}