diskann-inmem 0.56.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
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

//! # Baseline Checking
//!
//! The [`Regression`](diskann_benchmark_runner::benchmark::Regression) provides a means
//! of performing before/after comparisons against previously generated results. However,
//! presentation of these results is largely left to the devices of the implementors.
//!
//! This module provides a means of aggregating all match failures (if any) and presenting
//! all failures as a single unit.

use std::{
    borrow::Cow,
    fmt::{Display, Write},
};

use diskann_benchmark_runner::{benchmark::PassFail, utils::fmt::Table};
use serde::{Serialize, Serializer};

/// Perform a baseline check on `self` and a previously saved result.
pub(crate) trait CheckMatch {
    fn check_match(&self, previous: &Self) -> Match;
}

/// The result of a baseline check.
#[must_use = "this is a result type"]
#[derive(Debug, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum Match {
    /// Successful match.
    Ok,

    /// A mismatch on a specific field.
    Mismatch {
        got: String,
        expected: String,
        remark: Option<Cow<'static, str>>,
    },

    /// A collection of mismatches for an aggregate data type or collection.
    ///
    /// Use [`MatchBuilder`] to easier construction.
    Nested {
        children: Vec<(Key, Match)>,
        remark: Option<Cow<'static, str>>,
    },
}

impl Match {
    /// Return `true` if `self` is [`Match::Ok`].
    #[must_use = "this has no side-effects"]
    pub(crate) fn is_ok(&self) -> bool {
        matches!(self, Self::Ok)
    }

    /// Record a single mismatch between the retrieved value `got` and the `expected` result.
    pub(crate) fn mismatch(got: &dyn Display, expected: &dyn Display) -> Self {
        Self::mismatch_with_remark(got, expected, None)
    }

    /// Record a single mismatch between the retrieved value `got` and the `expected` result
    /// with an additional optional remark.
    ///
    /// The remark can be used for contexts where matches are more complex than simple
    /// equality.
    pub(crate) fn mismatch_with_remark(
        got: &dyn Display,
        expected: &dyn Display,
        remark: Option<Cow<'static, str>>,
    ) -> Self {
        Self::Mismatch {
            expected: expected.to_string(),
            got: got.to_string(),
            remark,
        }
    }

    /// Convert `self` into a [`PassFail`] for regression checks.
    ///
    /// Returns `PassFail::Pass` only if `self.is_ok`.
    pub(crate) fn pass_fail(self) -> PassFail<Self, Self> {
        if self.is_ok() {
            PassFail::Pass(self)
        } else {
            PassFail::Fail(self)
        }
    }
}

impl std::fmt::Display for Match {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Ok => f.write_str("ok"),
            Self::Mismatch {
                got,
                expected,
                remark,
            } => {
                let header = ["got", "expected", "remark"];
                let mut table = Table::new(header, 1);
                let mut row = table.row(0);
                row.insert(got.clone(), 0);
                row.insert(expected.clone(), 1);
                if let Some(remark) = remark {
                    row.insert(remark.clone(), 2);
                }

                table.fmt(f)
            }
            Self::Nested { children, remark } => {
                let mut records = Vec::new();
                if let Some(remark) = remark {
                    records.push(Record {
                        path: String::new(),
                        got: "",
                        expected: "",
                        remark,
                    });
                }

                let mut buf = String::new();
                gather_mismatches(children, &mut records, Stack::new(&mut buf));

                let mut table = Table::new(["path", "got", "expected", "remark"], records.len());
                for (i, r) in records.into_iter().enumerate() {
                    let mut row = table.row(i);
                    row.insert(r.path, 0);
                    row.insert(r.got.to_owned(), 1);
                    row.insert(r.expected.to_owned(), 2);
                    row.insert(r.remark.to_owned(), 3);
                }

                table.fmt(f)
            }
        }
    }
}

fn gather_mismatches<'a>(
    mismatches: &'a [(Key, Match)],
    records: &mut Vec<Record<'a>>,
    mut path: Stack<'_>,
) {
    for (k, m) in mismatches.iter() {
        match m {
            Match::Ok => continue,
            Match::Mismatch {
                got,
                expected,
                remark,
            } => {
                let record = Record {
                    path: path.push(k).get(),
                    got,
                    expected,
                    remark: remark.as_deref().unwrap_or(""),
                };
                records.push(record);
            }
            Match::Nested { children, remark } => {
                let path = path.push(k);

                if let Some(remark) = remark {
                    records.push(Record {
                        path: path.get(),
                        got: "",
                        expected: "",
                        remark,
                    })
                }

                gather_mismatches(children, records, path)
            }
        }
    }
}

#[derive(Debug)]
struct Stack<'a> {
    s: &'a mut String,
    len: usize,
}

impl<'a> Stack<'a> {
    fn new(s: &'a mut String) -> Self {
        s.clear();
        Self { s, len: 0 }
    }

    #[expect(clippy::unwrap_used, reason = "formatting shouldn't be failing here")]
    fn push(&mut self, key: &Key) -> Stack<'_> {
        let len = self.s.len();
        if len == 0 {
            write!(self.s, "{}", key).unwrap();
        } else {
            write!(self.s, ".{}", key).unwrap();
        }

        Stack { s: self.s, len }
    }

    fn get(&self) -> String {
        self.s.clone()
    }
}

impl Drop for Stack<'_> {
    fn drop(&mut self) {
        self.s.truncate(self.len)
    }
}

#[derive(Debug)]
struct Record<'a> {
    path: String,
    got: &'a str,
    expected: &'a str,
    remark: &'a str,
}

/////////
// Key //
/////////

/// A key to develop the full hierarchical path for a match.
///
/// Keys can either be strings or positional indices. The latter are used when traversing
/// arrays.
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) enum Key {
    Str(&'static str),
    Position(usize),
    String(String),
}

impl std::fmt::Display for Key {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Str(s) => f.write_str(s),
            Self::Position(i) => write!(f, "{}", i),
            Self::String(s) => f.write_str(s),
        }
    }
}

impl Serialize for Key {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Str(s) => serializer.serialize_str(s),
            Self::Position(i) => serializer.serialize_u64(*i as u64),
            Self::String(s) => serializer.serialize_str(s),
        }
    }
}

impl From<&'static str> for Key {
    fn from(s: &'static str) -> Key {
        Key::Str(s)
    }
}

impl From<usize> for Key {
    fn from(i: usize) -> Key {
        Key::Position(i)
    }
}

impl From<String> for Key {
    fn from(s: String) -> Key {
        Key::String(s)
    }
}

/////////////
// Builder //
/////////////

/// A utility for building a nested [`Match`].
#[derive(Debug)]
pub(crate) struct MatchBuilder {
    children: Vec<(Key, Match)>,
}

impl MatchBuilder {
    /// Construct a new empty collection of matches.
    pub(crate) fn new() -> Self {
        Self {
            children: Vec::new(),
        }
    }

    /// Push the [`Match`] into the collection only if [`Match::is_ok`] fails.
    pub(crate) fn push(&mut self, key: Key, child: Match) {
        if !child.is_ok() {
            self.children.push((key, child));
        }
    }

    /// Package the collection of matches into a single [`Match`].
    ///
    /// If no failing matches have been aggregated, returns [`Match::Ok`].
    pub(crate) fn finish(self) -> Match {
        self.finish_with_remark(None)
    }

    /// Package the collection of matches into a single [`Match`] with a remark.
    ///
    /// If no failing matches have been aggregated, returns [`Match::Ok`].
    pub(crate) fn finish_with_remark(self, remark: Option<Cow<'static, str>>) -> Match {
        if self.children.is_empty() {
            Match::Ok
        } else {
            Match::Nested {
                children: self.children,
                remark,
            }
        }
    }
}

macro_rules! check_match_impl {
    ($T:ty) => {
        impl CheckMatch for $T {
            fn check_match(
                &self,
                previous: &Self,
            ) -> Match {
                if self == previous {
                    Match::Ok
                } else {
                    Match::mismatch(self, previous)
                }
            }
        }
    };
    ($($Ts:ty),+ $(,)?) => {
        $(check_match_impl!($Ts);)+
    }
}

check_match_impl!(
    bool, u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64, &str, String
);

impl<T> CheckMatch for [T]
where
    T: CheckMatch,
{
    fn check_match(&self, previous: &[T]) -> Match {
        if self.len() != previous.len() {
            return Match::mismatch_with_remark(
                &self.len(),
                &previous.len(),
                Some("number of results is different between runs".into()),
            );
        }

        let mut builder = MatchBuilder::new();
        for (i, (got, expected)) in std::iter::zip(self.iter(), previous.iter()).enumerate() {
            builder.push(Key::from(i), got.check_match(expected));
        }

        builder.finish()
    }
}

impl<T> CheckMatch for Vec<T>
where
    T: CheckMatch,
{
    fn check_match(&self, previous: &Vec<T>) -> Match {
        self.as_slice().check_match(previous.as_slice())
    }
}

//--------//
// Macros //
//--------//

macro_rules! check_all_fields {
    ($self:expr, $prev:expr, { $($field:ident),+ $(,)? } $(,)?) => {{
        let Self { $($field),+ } = $self;
        let mut builder = $crate::support::check::MatchBuilder::new();
        $(
            builder.push(
                stringify!($field).into(),
                <_ as $crate::support::check::CheckMatch>::check_match(
                    $field,
                    &$prev.$field
                ),
            );
        )+
        builder
    }};
}

pub(crate) use check_all_fields;

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

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

    //-------//
    // Match //
    //-------//

    #[test]
    fn match_is_ok() {
        assert!(Match::Ok.is_ok());
        assert!(!Match::mismatch(&1, &2).is_ok());
    }

    #[test]
    fn mismatch_records_got_and_expected() {
        match Match::mismatch(&1, &2) {
            Match::Mismatch {
                got,
                expected,
                remark,
            } => {
                assert_eq!(got, "1");
                assert_eq!(expected, "2");
                assert!(remark.is_none());
            }
            other => panic!("expected Mismatch, got {other:?}"),
        }
    }

    #[test]
    fn mismatch_with_remark_records_remark() {
        match Match::mismatch_with_remark(&"a", &"b", Some("note".into())) {
            Match::Mismatch {
                got,
                expected,
                remark,
            } => {
                assert_eq!(got, "a");
                assert_eq!(expected, "b");
                assert_eq!(remark.as_deref(), Some("note"));
            }
            other => panic!("expected Mismatch, got {other:?}"),
        }
    }

    #[test]
    fn pass_fail_follows_is_ok() {
        assert!(matches!(Match::Ok.pass_fail(), PassFail::Pass(Match::Ok)));

        assert!(matches!(
            Match::mismatch(&1, &2).pass_fail(),
            PassFail::Fail(Match::Mismatch { .. })
        ));

        let mut builder = MatchBuilder::new();
        builder.push(Key::from("test"), Match::mismatch(&1, &2));
        builder.push(Key::from("test2"), Match::mismatch(&2, &3));
        let mismatch = builder.finish();

        assert!(matches!(mismatch, Match::Nested { .. }));
        assert!(matches!(
            mismatch.pass_fail(),
            PassFail::Fail(Match::Nested { .. })
        ));
    }

    //------------//
    // CheckMatch //
    //------------//

    #[test]
    fn primitive_check_match() {
        assert!(1u32.check_match(&1u32).is_ok());
        assert!(!2u32.check_match(&3u32).is_ok());
        assert!("x".check_match(&"x").is_ok());
        assert!(!"x".check_match(&"y").is_ok());
    }

    #[test]
    fn slice_check_match_equal() {
        let a = vec![1u32, 2, 3];
        let b = vec![1u32, 2, 3];
        assert!(a.check_match(&b).is_ok());
    }

    #[test]
    fn slice_check_match_length_mismatch() {
        let a = vec![1u32, 2, 3];
        let b = vec![1u32, 2];
        match a.check_match(&b) {
            Match::Mismatch {
                got,
                expected,
                remark,
            } => {
                assert_eq!(got, "3");
                assert_eq!(expected, "2");
                assert!(remark.is_some());
            }
            other => panic!("expected length Mismatch, got {other:?}"),
        }
    }

    #[test]
    fn slice_check_match_element_mismatch() {
        let a = vec![1u32, 9, 3];
        let b = vec![1u32, 2, 3];
        match a.check_match(&b) {
            Match::Nested { children, .. } => {
                assert_eq!(children.len(), 1);
                assert!(matches!(children[0].0, Key::Position(1)));
            }
            other => panic!("expected Nested, got {other:?}"),
        }
    }

    //--------------//
    // MatchBuilder //
    //--------------//

    #[test]
    fn builder_empty_is_ok() {
        assert!(MatchBuilder::new().finish().is_ok());
    }

    #[test]
    fn builder_skips_ok_matches() {
        let mut builder = MatchBuilder::new();
        builder.push("a".into(), Match::Ok);
        builder.push("b".into(), Match::Ok);
        assert!(builder.finish().is_ok());
    }

    #[test]
    fn builder_collects_failures() {
        let mut builder = MatchBuilder::new();
        builder.push("a".into(), Match::Ok);
        builder.push("b".into(), Match::mismatch(&1, &2));
        match builder.finish() {
            Match::Nested { children, remark } => {
                assert_eq!(children.len(), 1);
                assert!(remark.is_none());
            }
            other => panic!("expected Nested, got {other:?}"),
        }
    }

    #[test]
    fn builder_finish_with_remark() {
        let mut builder = MatchBuilder::new();
        builder.push("b".into(), Match::mismatch(&1, &2));
        match builder.finish_with_remark(Some("ctx".into())) {
            Match::Nested { remark, .. } => assert_eq!(remark.as_deref(), Some("ctx")),
            other => panic!("expected Nested, got {other:?}"),
        }
    }

    //-----//
    // Key //
    //-----//

    #[test]
    fn key_display() {
        assert_eq!(Key::from("field").to_string(), "field");
        assert_eq!(Key::from(7usize).to_string(), "7");
        assert_eq!(Key::from(String::from("owned")).to_string(), "owned");
    }

    #[test]
    fn key_serde() {
        let k = serde_json::to_value(Key::Str("field")).unwrap();
        assert_eq!(k, serde_json::Value::String("field".into()));

        let k = serde_json::to_value(Key::Position(10)).unwrap();
        assert_eq!(k, serde_json::Value::Number(10.into()));

        let k = serde_json::to_value(Key::String("world".into())).unwrap();
        assert_eq!(k, serde_json::Value::String("world".into()));
    }

    //---------//
    // Display //
    //---------//

    #[test]
    fn display_ok() {
        assert_eq!(Match::Ok.to_string(), "ok");
    }

    #[test]
    fn display_nonnested() {
        let mismatch = Match::mismatch_with_remark(&"hello", &1, Some("word".into()));
        let rendered = mismatch.to_string();

        let expected = r#"
  got,   expected,   remark
===========================
hello,          1,     word
"#;
        let expected = expected.strip_prefix('\n').unwrap();

        println!("rendered = {:?}", rendered);

        let mut count = 0;
        for (line, (got, expected)) in
            std::iter::zip(rendered.lines(), expected.lines()).enumerate()
        {
            count += 1;
            assert_eq!(got.trim(), expected.trim(), "failed on line {line}",);
        }
        assert_eq!(count, 3);
    }

    #[test]
    fn display_nested() {
        // Build a nested match and ensure the hierarchical path is rendered.
        let mut inner = MatchBuilder::new();
        inner.push(1usize.into(), Match::mismatch(&9, &2));
        inner.push(
            "test".into(),
            Match::mismatch_with_remark(&9, &2, Some("hello".into())),
        );
        let nested = inner.finish_with_remark(Some("some remark".into()));

        let mut outer = MatchBuilder::new();
        outer.push("results".into(), nested);
        let rendered = outer
            .finish_with_remark(Some("final remarks".into()))
            .to_string();

        let expected = r#"
         path,   got,   expected,          remark
 ================================================
             ,      ,           ,   final remarks
      results,      ,           ,     some remark
    results.1,     9,          2,
 results.test,     9,          2,           hello
 "#;

        let expected = expected.strip_prefix('\n').unwrap();

        println!("rendered = {:?}", rendered);

        let mut count = 0;
        for (line, (got, expected)) in
            std::iter::zip(rendered.lines(), expected.lines()).enumerate()
        {
            count += 1;
            assert_eq!(got.trim(), expected.trim(), "failed on line {line}",);
        }
        assert_eq!(count, 6);
    }

    //-------------------//
    // check_all_fields! //
    //-------------------//

    #[derive(Debug)]
    struct Sample {
        a: u32,
        b: String,
    }

    impl CheckMatch for Sample {
        fn check_match(&self, previous: &Self) -> Match {
            check_all_fields!(self, previous, { a, b }).finish()
        }
    }

    #[test]
    fn check_all_fields_equal() {
        let x = Sample {
            a: 1,
            b: "hi".into(),
        };
        let y = Sample {
            a: 1,
            b: "hi".into(),
        };
        assert!(x.check_match(&y).is_ok());
    }

    #[test]
    fn check_all_fields_reports_changed_field() {
        let x = Sample {
            a: 1,
            b: "hi".into(),
        };
        let y = Sample {
            a: 1,
            b: "bye".into(),
        };
        match x.check_match(&y) {
            Match::Nested { children, .. } => {
                assert_eq!(children.len(), 1);
                assert_eq!(children[0].0.to_string(), "b");
            }
            other => panic!("expected Nested, got {other:?}"),
        }
    }
}