diskann-benchmark 0.55.0

DiskANN3 is a composable library for bringing scalable, accurate and cost-effective vector indexing to multiple databases.
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
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */
use std::num::{NonZero, NonZeroUsize};

use crate::inputs::{
    as_input, exhaustive,
    graph_index::{DynamicRunbookParams, IndexBuild, SearchPhase, TopkSearchPhase},
    write_field, Example, PRINT_WIDTH,
};
use diskann::graph::config;
use diskann_benchmark_runner::{utils::datatype::DataType, Checker};
use diskann_bftree::BfTreeProviderParameters;
use serde::{Deserialize, Serialize};

// ─── BfTree Store Configuration ───────────────────────────────────────────────

/// Configuration for a single bf_tree store instance.
///
/// Required fields control memory sizing before data spills to disk.
/// Optional fields tune internal behavior and default to bf_tree's defaults.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct BfTreeStoreConfig {
    /// Size of the circular buffer (in-memory write cache) in bytes.
    pub(crate) cb_size_byte: usize,

    /// Size of leaf pages in bytes.
    pub(crate) leaf_page_size: usize,

    /// Maximum record size that can be stored in the circular buffer.
    #[serde(deserialize_with = "Deserialize::deserialize")]
    pub(crate) cb_max_record_size: Option<usize>,

    /// Minimum record size for the circular buffer.
    #[serde(deserialize_with = "Deserialize::deserialize")]
    pub(crate) cb_min_record_size: Option<usize>,

    /// Probability (0-100) of promoting a read record to the front of the buffer.
    #[serde(deserialize_with = "Deserialize::deserialize")]
    pub(crate) read_promotion_rate: Option<usize>,

    /// Probability (0-100) of promoting a scanned record to the front of the buffer.
    #[serde(deserialize_with = "Deserialize::deserialize")]
    pub(crate) scan_promotion_rate: Option<usize>,

    /// Ratio of buffer used before copy-on-access kicks in.
    #[serde(deserialize_with = "Deserialize::deserialize")]
    pub(crate) cb_copy_on_access_ratio: Option<f64>,

    /// Whether to cache full pages on read.
    #[serde(deserialize_with = "Deserialize::deserialize")]
    pub(crate) read_record_cache: Option<bool>,

    /// If true, only use the in-memory circular buffer (no disk pages).
    #[serde(deserialize_with = "Deserialize::deserialize")]
    pub(crate) cache_only: Option<bool>,

    /// Whether to enable CPR snapshot support for this store.
    pub(crate) use_snapshot: bool,
}

impl BfTreeStoreConfig {
    pub(crate) fn validate(&self) -> anyhow::Result<()> {
        // bf-tree requires:
        //   cache-only mode:     cb_size_byte >= 4 * leaf_page_size
        //   non cache-only mode: cb_size_byte >= 2 * leaf_page_size
        let multiplier = if self.cache_only.unwrap_or(false) {
            4
        } else {
            2
        };
        let min_cb = multiplier * self.leaf_page_size;
        anyhow::ensure!(
            self.cb_size_byte >= min_cb,
            "cb_size_byte ({}) must be at least {} * leaf_page_size ({}) = {} bytes",
            self.cb_size_byte,
            multiplier,
            self.leaf_page_size,
            min_cb,
        );
        Ok(())
    }

    pub(crate) fn into_config(self) -> bf_tree::Config {
        let mut c = bf_tree::Config::default();
        c.cb_size_byte(self.cb_size_byte);
        c.leaf_page_size(self.leaf_page_size);
        if let Some(v) = self.cb_max_record_size {
            c.cb_max_record_size(v);
        }
        if let Some(v) = self.cb_min_record_size {
            c.cb_min_record_size(v);
        }
        if let Some(v) = self.read_promotion_rate {
            c.read_promotion_rate(v);
        }
        if let Some(v) = self.scan_promotion_rate {
            c.scan_promotion_rate(v);
        }
        if let Some(v) = self.cb_copy_on_access_ratio {
            c.cb_copy_on_access_ratio(v);
        }
        if let Some(v) = self.read_record_cache {
            c.read_record_cache(v);
        }
        if let Some(v) = self.cache_only {
            c.cache_only(v);
        }
        c.use_snapshot(self.use_snapshot);
        c
    }

    /// Backfills fields with "None" from the config json with the actual defaults so the display
    /// function will print helpful information in the json.
    /// Some of the fields do not have accessors, so their values will be manually populated with
    /// the publicly listed defaults for bf-tree v0.4.9.
    pub(crate) fn fill_defaults(&mut self) {
        let defaults = bf_tree::Config::default();
        // fill optional field with available config field accessors
        self.cb_max_record_size
            .get_or_insert(defaults.get_cb_max_record_size());

        // these are the fields without accessors from the defaults, fill with known defaults from
        // bf-tree (v0.4.9)
        self.cb_min_record_size.get_or_insert(4);
        self.read_promotion_rate
            .get_or_insert(if cfg!(debug_assertions) { 50 } else { 30 });
        self.scan_promotion_rate
            .get_or_insert(if cfg!(debug_assertions) { 50 } else { 30 });
        self.cb_copy_on_access_ratio.get_or_insert(0.1);
        self.read_record_cache.get_or_insert(true);
        self.cache_only.get_or_insert(false);
    }
}

impl Default for BfTreeStoreConfig {
    fn default() -> Self {
        Self {
            cb_size_byte: 32 * 1024 * 1024, // 32MB
            leaf_page_size: 4096,
            cb_max_record_size: None,
            cb_min_record_size: None,
            read_promotion_rate: None,
            scan_promotion_rate: None,
            cb_copy_on_access_ratio: None,
            read_record_cache: None,
            cache_only: None,
            use_snapshot: false,
        }
    }
}

impl Example for BfTreeStoreConfig {
    fn example() -> Self {
        Self::default()
    }
}

impl std::fmt::Display for BfTreeStoreConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write_field!(f, "cb_size_byte", self.cb_size_byte)?;
        write_field!(f, "leaf_page_size", self.leaf_page_size)?;
        if let Some(v) = self.cb_max_record_size {
            write_field!(f, "cb_max_record_size", v)?;
        }
        if let Some(v) = self.cb_min_record_size {
            write_field!(f, "cb_min_record_size", v)?;
        }
        if let Some(v) = self.read_promotion_rate {
            write_field!(f, "read_promotion_rate", v)?;
        }
        if let Some(v) = self.scan_promotion_rate {
            write_field!(f, "scan_promotion_rate", v)?;
        }
        if let Some(v) = self.cb_copy_on_access_ratio {
            write_field!(f, "cb_copy_on_access_ratio", v)?;
        }
        if let Some(v) = self.read_record_cache {
            write_field!(f, "read_record_cache", v)?;
        }
        if let Some(v) = self.cache_only {
            write_field!(f, "cache_only", v)?;
        }
        Ok(())
    }
}

/// Validate that all present store configs agree on `use_snapshot`.
///
/// Returns the agreed-upon value. If no configs are present or none set
/// `use_snapshot`, returns `false`. If configs disagree, returns an error.
fn reconcile_use_snapshot(configs: &[(&str, &Option<BfTreeStoreConfig>)]) -> anyhow::Result<bool> {
    let mut resolved: Option<bool> = None;

    for (name, config) in configs {
        if let Some(cfg) = config {
            match resolved {
                Some(prev) if prev != cfg.use_snapshot => {
                    anyhow::bail!(
                        "{name}.use_snapshot ({}) disagrees with a prior store config ({prev})",
                        cfg.use_snapshot
                    );
                }
                None => {
                    resolved = Some(cfg.use_snapshot);
                }
                _ => {}
            }
        }
    }

    Ok(resolved.unwrap_or(false))
}

/// Shared helper to construct [`BfTreeProviderParameters`] from common fields.
fn bftree_parameters_from(
    build: &IndexBuild,
    num_points: usize,
    dim: usize,
    vector_store_config: &Option<BfTreeStoreConfig>,
    neighbor_store_config: &Option<BfTreeStoreConfig>,
    quant_store_config: &Option<BfTreeStoreConfig>,
) -> anyhow::Result<BfTreeProviderParameters> {
    let use_snapshot = reconcile_use_snapshot(&[
        ("vector_store_config", vector_store_config),
        ("neighbor_store_config", neighbor_store_config),
        ("quant_store_config", quant_store_config),
    ])?;

    let physical_max_degree = (build.max_degree() as f32 * build.graph_slack_factor()) as u32;
    Ok(BfTreeProviderParameters {
        max_points: num_points,
        max_degree: physical_max_degree,
        num_start_points: NonZero::new(build.start_point_strategy().count()).unwrap(),
        dim,
        metric: build.distance().into(),
        vector_provider_config: vector_store_config
            .clone()
            .unwrap_or_default()
            .into_config(),
        neighbor_list_provider_config: neighbor_store_config
            .clone()
            .unwrap_or_default()
            .into_config(),
        quant_vector_provider_config: quant_store_config.clone().unwrap_or_default().into_config(),
        graph_params: None,
        use_snapshot,
    })
}

as_input!(BfTreeFullPrecisionBuild);

#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct BfTreeFullPrecisionBuild {
    build: IndexBuild,
    search_phase: SearchPhase,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    vector_store_config: Option<BfTreeStoreConfig>,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    neighbor_store_config: Option<BfTreeStoreConfig>,
}

impl BfTreeFullPrecisionBuild {
    pub(crate) const fn tag() -> &'static str {
        "graph-index-build-bftree-full-precision"
    }

    pub(crate) fn try_as_config(&self) -> anyhow::Result<config::Builder> {
        self.build.try_as_config()
    }

    pub(crate) fn data_type(&self) -> DataType {
        self.build.data_type()
    }

    pub(crate) fn search_phase(&self) -> &SearchPhase {
        &self.search_phase
    }

    pub(crate) fn build(&self) -> &IndexBuild {
        &self.build
    }

    pub(crate) fn bftree_parameters(
        &self,
        num_points: usize,
        dim: usize,
    ) -> anyhow::Result<BfTreeProviderParameters> {
        bftree_parameters_from(
            &self.build,
            num_points,
            dim,
            &self.vector_store_config,
            &self.neighbor_store_config,
            &None,
        )
    }
    pub(crate) fn validate(&mut self, checker: &mut Checker) -> Result<(), anyhow::Error> {
        self.build.validate(checker)?;
        self.search_phase.validate(checker)?;
        if let Some(cfg) = &mut self.neighbor_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }
        if let Some(cfg) = &mut self.vector_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }
        Ok(())
    }
}

impl Example for BfTreeFullPrecisionBuild {
    fn example() -> Self {
        let build = IndexBuild::example();

        Self {
            build,
            search_phase: SearchPhase::Topk(TopkSearchPhase::example()),
            vector_store_config: None,
            neighbor_store_config: None,
        }
    }
}

impl std::fmt::Display for BfTreeFullPrecisionBuild {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Graph Index Bf_Tree Full-Precision Build")?;
        if cfg!(not(feature = "bftree")) {
            writeln!(f, "Requires the `bftree` feature")?;
        }
        write_field!(f, "tag", Self::tag())?;

        writeln!(f)?;
        self.build.summarize_fields(f)?;

        if let Some(ref cfg) = self.vector_store_config {
            writeln!(f, "\n  Vector Store:")?;
            write!(f, "{}", cfg)?;
        }
        if let Some(ref cfg) = self.neighbor_store_config {
            writeln!(f, "\n  Neighbor Store:")?;
            write!(f, "{}", cfg)?;
        }

        Ok(())
    }
}

as_input!(BfTreeDynamicRun);

#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct BfTreeDynamicRun {
    build: IndexBuild,
    search_phase: SearchPhase,
    runbook_params: DynamicRunbookParams,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    vector_store_config: Option<BfTreeStoreConfig>,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    neighbor_store_config: Option<BfTreeStoreConfig>,
}

impl BfTreeDynamicRun {
    pub(crate) const fn tag() -> &'static str {
        "graph-index-stream-bftree-full-precision"
    }

    pub(crate) fn try_as_config(&self) -> anyhow::Result<config::Builder> {
        self.build.try_as_config()
    }

    pub(crate) fn data_type(&self) -> DataType {
        self.build.data_type()
    }

    pub(crate) fn search_phase(&self) -> &SearchPhase {
        &self.search_phase
    }

    pub(crate) fn build(&self) -> &IndexBuild {
        &self.build
    }

    pub(crate) fn runbook_params(&self) -> &DynamicRunbookParams {
        &self.runbook_params
    }

    pub(crate) fn bftree_parameters(
        &self,
        num_points: usize,
        dim: usize,
    ) -> anyhow::Result<BfTreeProviderParameters> {
        bftree_parameters_from(
            &self.build,
            num_points,
            dim,
            &self.vector_store_config,
            &self.neighbor_store_config,
            &None,
        )
    }
    pub(crate) fn validate(&mut self, checker: &mut Checker) -> Result<(), anyhow::Error> {
        self.build.validate(checker)?;
        self.search_phase.validate(checker)?;
        self.runbook_params.validate(checker)?;
        if let Some(cfg) = &mut self.vector_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }
        if let Some(cfg) = &mut self.neighbor_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }
        Ok(())
    }
}

impl Example for BfTreeDynamicRun {
    fn example() -> Self {
        let build = IndexBuild::example();

        Self {
            build,
            search_phase: SearchPhase::Topk(TopkSearchPhase::example()),
            runbook_params: DynamicRunbookParams::example_immediate(),
            vector_store_config: None,
            neighbor_store_config: None,
        }
    }
}

impl std::fmt::Display for BfTreeDynamicRun {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Graph Index Bf_Tree Full-Precision Streaming")?;
        if cfg!(not(feature = "bftree")) {
            writeln!(f, "Requires the `bftree` feature")?;
        }
        write_field!(f, "tag", Self::tag())?;

        writeln!(f)?;
        self.build.summarize_fields(f)?;

        if let Some(ref cfg) = self.vector_store_config {
            writeln!(f, "\n  Vector Store:")?;
            write!(f, "{}", cfg)?;
        }
        if let Some(ref cfg) = self.neighbor_store_config {
            writeln!(f, "\n  Neighbor Store:")?;
            write!(f, "{}", cfg)?;
        }

        Ok(())
    }
}

// ─── Spherical Quantization ───────────────────────────────────────────────────

as_input!(BfTreeSphericalBuild);

#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct BfTreeSphericalBuild {
    build: IndexBuild,
    search_phase: SearchPhase,
    seed: u64,
    transform_kind: exhaustive::TransformKind,
    num_bits: NonZeroUsize,
    pre_scale: Option<exhaustive::PreScale>,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    vector_store_config: Option<BfTreeStoreConfig>,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    neighbor_store_config: Option<BfTreeStoreConfig>,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    quant_store_config: Option<BfTreeStoreConfig>,
}

impl BfTreeSphericalBuild {
    pub(crate) const fn tag() -> &'static str {
        "graph-index-build-bftree-spherical-quantization"
    }

    pub(crate) fn try_as_config(&self) -> anyhow::Result<config::Builder> {
        self.build.try_as_config()
    }

    pub(crate) fn bftree_parameters(
        &self,
        num_points: usize,
        dim: usize,
    ) -> anyhow::Result<BfTreeProviderParameters> {
        bftree_parameters_from(
            &self.build,
            num_points,
            dim,
            &self.vector_store_config,
            &self.neighbor_store_config,
            &self.quant_store_config,
        )
    }

    pub(crate) fn data_type(&self) -> DataType {
        self.build.data_type()
    }

    pub(crate) fn search_phase(&self) -> &SearchPhase {
        &self.search_phase
    }

    pub(crate) fn build(&self) -> &IndexBuild {
        &self.build
    }

    pub(crate) fn seed(&self) -> u64 {
        self.seed
    }

    pub(crate) fn transform_kind(&self) -> &exhaustive::TransformKind {
        &self.transform_kind
    }

    pub(crate) fn num_bits(&self) -> NonZeroUsize {
        self.num_bits
    }

    pub(crate) fn pre_scale(&self) -> Option<&exhaustive::PreScale> {
        self.pre_scale.as_ref()
    }

    pub(crate) fn validate(&mut self, checker: &mut Checker) -> anyhow::Result<()> {
        self.build.validate(checker)?;
        self.search_phase.validate(checker)?;
        if let Some(cfg) = &mut self.vector_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }
        if let Some(cfg) = &mut self.neighbor_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }
        if let Some(cfg) = &mut self.quant_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }

        match self.num_bits.get() {
            1 | 2 | 4 => {}
            n => anyhow::bail!("{n} bits are not supported for spherical quantization"),
        }

        Ok(())
    }
}

impl Example for BfTreeSphericalBuild {
    fn example() -> Self {
        Self {
            build: IndexBuild::example(),
            search_phase: SearchPhase::Topk(TopkSearchPhase::example()),
            seed: 42,
            transform_kind: exhaustive::TransformKind::Null,
            num_bits: NonZeroUsize::new(1).unwrap(),
            pre_scale: None,
            vector_store_config: None,
            neighbor_store_config: None,
            quant_store_config: None,
        }
    }
}

impl std::fmt::Display for BfTreeSphericalBuild {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Graph Index Bf_Tree Spherical Quantization Build")?;
        if cfg!(not(feature = "bftree")) {
            writeln!(f, "Requires the `bftree` feature")?;
        }
        write_field!(f, "tag", Self::tag())?;
        write_field!(f, "num_bits", self.num_bits)?;
        write_field!(f, "seed", self.seed)?;
        write_field!(f, "transform_kind", self.transform_kind)?;

        writeln!(f)?;
        self.build.summarize_fields(f)?;

        if let Some(ref cfg) = self.vector_store_config {
            writeln!(f, "\n  Vector Store:")?;
            write!(f, "{}", cfg)?;
        }
        if let Some(ref cfg) = self.neighbor_store_config {
            writeln!(f, "\n  Neighbor Store:")?;
            write!(f, "{}", cfg)?;
        }
        if let Some(ref cfg) = self.quant_store_config {
            writeln!(f, "\n  Quant Store:")?;
            write!(f, "{}", cfg)?;
        }

        Ok(())
    }
}

// ─── Spherical Streaming ──────────────────────────────────────────────────────

as_input!(BfTreeSphericalDynamicRun);

#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct BfTreeSphericalDynamicRun {
    build: IndexBuild,
    search_phase: SearchPhase,
    runbook_params: DynamicRunbookParams,
    seed: u64,
    transform_kind: exhaustive::TransformKind,
    num_bits: NonZeroUsize,
    pre_scale: Option<exhaustive::PreScale>,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    vector_store_config: Option<BfTreeStoreConfig>,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    neighbor_store_config: Option<BfTreeStoreConfig>,
    #[serde(deserialize_with = "Deserialize::deserialize")]
    quant_store_config: Option<BfTreeStoreConfig>,
}

impl BfTreeSphericalDynamicRun {
    pub(crate) const fn tag() -> &'static str {
        "graph-index-stream-bftree-spherical-quantization"
    }

    pub(crate) fn try_as_config(&self) -> anyhow::Result<config::Builder> {
        self.build.try_as_config()
    }

    pub(crate) fn data_type(&self) -> DataType {
        self.build.data_type()
    }

    pub(crate) fn search_phase(&self) -> &SearchPhase {
        &self.search_phase
    }

    pub(crate) fn build(&self) -> &IndexBuild {
        &self.build
    }

    pub(crate) fn runbook_params(&self) -> &DynamicRunbookParams {
        &self.runbook_params
    }

    pub(crate) fn seed(&self) -> u64 {
        self.seed
    }

    pub(crate) fn transform_kind(&self) -> &exhaustive::TransformKind {
        &self.transform_kind
    }

    pub(crate) fn num_bits(&self) -> NonZeroUsize {
        self.num_bits
    }

    pub(crate) fn pre_scale(&self) -> Option<&exhaustive::PreScale> {
        self.pre_scale.as_ref()
    }

    pub(crate) fn bftree_parameters(
        &self,
        num_points: usize,
        dim: usize,
    ) -> anyhow::Result<BfTreeProviderParameters> {
        bftree_parameters_from(
            &self.build,
            num_points,
            dim,
            &self.vector_store_config,
            &self.neighbor_store_config,
            &self.quant_store_config,
        )
    }

    pub(crate) fn validate(&mut self, checker: &mut Checker) -> anyhow::Result<()> {
        self.build.validate(checker)?;
        self.search_phase.validate(checker)?;
        self.runbook_params.validate(checker)?;
        if let Some(cfg) = &mut self.vector_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }
        if let Some(cfg) = &mut self.neighbor_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }
        if let Some(cfg) = &mut self.quant_store_config {
            cfg.fill_defaults();
            cfg.validate()?;
        }

        match self.num_bits.get() {
            1 | 2 | 4 => {}
            n => anyhow::bail!("{n} bits are not supported for spherical quantization"),
        }

        Ok(())
    }
}

impl Example for BfTreeSphericalDynamicRun {
    fn example() -> Self {
        Self {
            build: IndexBuild::example(),
            search_phase: SearchPhase::Topk(TopkSearchPhase::example()),
            runbook_params: DynamicRunbookParams::example_immediate(),
            seed: 42,
            transform_kind: exhaustive::TransformKind::Null,
            num_bits: NonZeroUsize::new(1).unwrap(),
            pre_scale: None,
            vector_store_config: None,
            neighbor_store_config: None,
            quant_store_config: None,
        }
    }
}

impl std::fmt::Display for BfTreeSphericalDynamicRun {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Graph Index Bf_Tree Spherical Quantization Streaming")?;
        if cfg!(not(feature = "bftree")) {
            writeln!(f, "Requires the `bftree` feature")?;
        }
        write_field!(f, "tag", Self::tag())?;
        write_field!(f, "num_bits", self.num_bits)?;
        write_field!(f, "seed", self.seed)?;
        write_field!(f, "transform_kind", self.transform_kind)?;

        writeln!(f)?;
        self.build.summarize_fields(f)?;

        if let Some(ref cfg) = self.vector_store_config {
            writeln!(f, "\n  Vector Store:")?;
            write!(f, "{}", cfg)?;
        }
        if let Some(ref cfg) = self.neighbor_store_config {
            writeln!(f, "\n  Neighbor Store:")?;
            write!(f, "{}", cfg)?;
        }
        if let Some(ref cfg) = self.quant_store_config {
            writeln!(f, "\n  Quant Store:")?;
            write!(f, "{}", cfg)?;
        }

        Ok(())
    }
}