diskann 0.51.0

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

use super::helpers::{create_2d_unit_square, generate_2d_square_adjacency_list, setup_2d_square};
use crate::{
    graph::{self, AdjacencyList, DiskANNIndex, test::provider as test_provider},
    neighbor::Neighbor,
    provider::{Delete, NeighborAccessor},
};
use std::sync::Arc;

fn setup_paged_search_test() -> (
    Arc<DiskANNIndex<test_provider::Provider>>,
    test_provider::Strategy,
    test_provider::Context,
    Vec<f32>,
) {
    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let strategy = test_provider::Strategy::new();
    let ctx = test_provider::Context::new();
    let query_point = vec![0.1, 0.2];
    (index, strategy, ctx, query_point)
}

#[test]
fn query_label_provider_on_visit_default() {
    use crate::graph::index::{QueryLabelProvider, QueryVisitDecision};

    #[derive(Debug)]
    struct BasicValidation;

    impl QueryLabelProvider<u32> for BasicValidation {
        fn is_match(&self, id: u32) -> bool {
            id.is_multiple_of(2)
        }
    }

    let filter = BasicValidation;
    assert!(matches!(
        filter.on_visit(Neighbor::new(0, 1.0)),
        QueryVisitDecision::Accept(_)
    ));
    assert!(matches!(
        filter.on_visit(Neighbor::new(1, 1.0)),
        QueryVisitDecision::Reject
    ));
}

#[tokio::test(flavor = "current_thread")]
async fn test_count_reachable_nodes() {
    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let mut accessor = index.provider().neighbors();
    let starting_point = [4];

    let initial_result = index
        .count_reachable_nodes(&starting_point, &mut accessor)
        .await
        .unwrap();

    assert_eq!(initial_result, 5);

    let ctx = test_provider::Context::default();
    let strat = test_provider::Strategy::new();

    index
        .inplace_delete(
            strat.clone(),
            &ctx,
            &3,
            3,
            graph::InplaceDeleteMethod::OneHop,
        )
        .await
        .unwrap();

    let post_delete_result = index
        .count_reachable_nodes(&starting_point, &mut accessor)
        .await
        .unwrap();

    assert_eq!(post_delete_result, 4);
}

#[tokio::test(flavor = "current_thread")]
async fn test_count_unreachable_isolated_nodes() {
    let adjacency_list = vec![
        AdjacencyList::from_iter_untrusted([]),
        AdjacencyList::from_iter_untrusted([]),
        AdjacencyList::from_iter_untrusted([]),
        AdjacencyList::from_iter_untrusted([]),
        AdjacencyList::from_iter_untrusted([]),
    ];
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 1);
    let mut accessor = index.provider().neighbors();
    let starting_point = [4];

    let count = index
        .count_reachable_nodes(&starting_point, &mut accessor)
        .await
        .unwrap();
    assert_eq!(count, 1);
}

#[tokio::test(flavor = "current_thread")]
async fn test_get_degree_stats() {
    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let mut accessor = index.provider().neighbors();
    let stats = index.get_degree_stats(&mut accessor).await.unwrap();

    assert_eq!(stats.max_degree, 2);
    assert_eq!(stats.min_degree, 2);
    assert_eq!(stats.avg_degree, 2.0);
    assert_eq!(stats.cnt_less_than_two, 0);
}

#[tokio::test(flavor = "current_thread")]
async fn test_prune_range() {
    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 1);
    let ctx = test_provider::Context::default();
    let strat = test_provider::Strategy::new();

    index.prune_range(&strat, &ctx, 0..4).await.unwrap();

    let accessor = index.provider().neighbors();
    let mut list = AdjacencyList::new();
    for node in 0u32..4 {
        accessor.get_neighbors(node, &mut list).await.unwrap();
        assert!(
            list.len() <= 1,
            "node {node} should have degree <= 1 after prune, got {}",
            list.len()
        );
    }

    // Start node (4) is outside the pruned range — should be unchanged.
    accessor.get_neighbors(4, &mut list).await.unwrap();
    assert_eq!(
        list.len(),
        4,
        "start node should be untouched by prune_range(0..4)"
    );
}

/// Multiple start points should union their reachable sets.
#[tokio::test(flavor = "current_thread")]
async fn test_count_reachable_nodes_multiple_starts() {
    // Two disconnected components: nodes 0,1 connected to each other via start (4),
    // nodes 2,3 connected to each other but not to 0,1 or start.
    let adjacency_list = vec![
        AdjacencyList::from_iter_untrusted([1, 4]),
        AdjacencyList::from_iter_untrusted([0, 4]),
        AdjacencyList::from_iter_untrusted([3]),
        AdjacencyList::from_iter_untrusted([2]),
        AdjacencyList::from_iter_untrusted([0, 1]),
    ];
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let mut accessor = index.provider().neighbors();

    // From start (4) alone: reaches 4, 0, 1 = 3
    let from_start = index
        .count_reachable_nodes(&[4], &mut accessor)
        .await
        .unwrap();
    assert_eq!(from_start, 3);

    // From node 2 alone: reaches 2, 3 = 2
    let from_two = index
        .count_reachable_nodes(&[2], &mut accessor)
        .await
        .unwrap();
    assert_eq!(from_two, 2);

    // From both: union = all 5
    let from_both = index
        .count_reachable_nodes(&[4, 2], &mut accessor)
        .await
        .unwrap();
    assert_eq!(from_both, 5);
}

/// Degree stats with mixed degrees exercises min/max/avg/cnt_less_than_two independently.
#[tokio::test(flavor = "current_thread")]
async fn test_get_degree_stats_mixed() {
    // Node 0: degree 0 (isolated), Node 1: degree 1, Node 2: degree 2, Node 3: degree 2
    let adjacency_list = vec![
        AdjacencyList::from_iter_untrusted([]),
        AdjacencyList::from_iter_untrusted([4]),
        AdjacencyList::from_iter_untrusted([3, 4]),
        AdjacencyList::from_iter_untrusted([2, 4]),
        AdjacencyList::from_iter_untrusted([1, 2, 3]),
    ];
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let mut accessor = index.provider().neighbors();
    let stats = index.get_degree_stats(&mut accessor).await.unwrap();

    assert_eq!(stats.max_degree, 2);
    assert_eq!(stats.min_degree, 0);
    // avg = (0 + 1 + 2 + 2) / 4 = 1.25
    assert_eq!(stats.avg_degree, 1.25);
    // nodes with degree < 2: node 0 (degree 0) and node 1 (degree 1)
    assert_eq!(stats.cnt_less_than_two, 2);
}

#[tokio::test(flavor = "current_thread")]
async fn validate_basic_paged_search() {
    let (index, strategy, ctx, query_point) = setup_paged_search_test();

    let k = 2;
    let mut state = index
        .start_paged_search(strategy, &ctx, query_point.as_slice(), k)
        .await
        .unwrap();

    let mut output = vec![Neighbor::default(); k];
    let count = index
        .next_search_results(&ctx, &mut state, k, &mut output)
        .await
        .unwrap();

    assert_eq!(count, k);
    assert_eq!(output[0].id, 0);
    assert_eq!(output[1].id, 1);
    assert!(output[..count].iter().all(|n| n.id != 4)); // ensure start point isn't in the
    // output
}

#[tokio::test(flavor = "current_thread")]
async fn validate_paged_search_multiple() {
    let (index, strategy, ctx, query_point) = setup_paged_search_test();

    let k = 1;
    let mut state = index
        .start_paged_search(strategy, &ctx, query_point.as_slice(), k)
        .await
        .unwrap();

    let mut output = vec![Neighbor::default()];
    let mut visited: Vec<u32> = Vec::new();
    for _ in 0..4 {
        let count = index
            .next_search_results(&ctx, &mut state, k, &mut output)
            .await
            .unwrap();
        assert_eq!(count, 1);
        visited.push(output[0].id);
    }

    use std::collections::HashSet;
    let unique: HashSet<_> = visited.iter().collect();
    assert_eq!(unique.len(), visited.len());
}

#[tokio::test(flavor = "current_thread")]
async fn test_paged_search_error_cases() {
    let (index, strategy, ctx, query_point) = setup_paged_search_test();

    let l_value = 2;
    let mut state = index
        .start_paged_search(strategy, &ctx, query_point.as_slice(), l_value)
        .await
        .unwrap();

    let mut output = vec![Neighbor::default(); 2];

    // k > l_value
    let result = index
        .next_search_results(&ctx, &mut state, l_value + 1, &mut output)
        .await;
    assert!(result.is_err());

    // k == 0
    let result = index
        .next_search_results(&ctx, &mut state, 0, &mut output)
        .await;
    assert!(result.is_err());

    // output buffer too small for requested k
    let mut small_output = vec![Neighbor::default(); 1];
    let result = index
        .next_search_results(&ctx, &mut state, 2, &mut small_output)
        .await;
    assert!(result.is_err());
}

#[tokio::test(flavor = "current_thread")]
async fn validate_basic_paged_search_with_init_ids() {
    let (index, strategy, ctx, query_point) = setup_paged_search_test();

    let k = 2;

    // Seed search from nodes 0 and 1 instead of using the default start point
    let init_ids: &[u32] = &[0, 1];
    let mut state = index
        .start_paged_search_with_init_ids(strategy, &ctx, query_point.as_slice(), k, Some(init_ids))
        .await
        .unwrap();

    let mut output = vec![Neighbor::default(); k];
    let count = index
        .next_search_results(&ctx, &mut state, k, &mut output)
        .await
        .unwrap();

    assert_eq!(count, k);
    // Init IDs (0, 1) are filtered out as start points; results are from remaining nodes
    assert!(
        output[..count]
            .iter()
            .all(|n| n.id != 0 && n.id != 1 && n.id != 4)
    );
}

#[tokio::test(flavor = "current_thread")]
async fn validate_paged_search_with_init_ids_multiple() {
    let (index, strategy, ctx, query_point) = setup_paged_search_test();

    let k = 1;

    let init_ids: &[u32] = &[0, 1];
    let mut state = index
        .start_paged_search_with_init_ids(strategy, &ctx, query_point.as_slice(), k, Some(init_ids))
        .await
        .unwrap();

    let mut output = vec![Neighbor::default()];
    let mut visited: Vec<u32> = Vec::new();
    for _ in 0..2 {
        let count = index
            .next_search_results(&ctx, &mut state, k, &mut output)
            .await
            .unwrap();
        assert_eq!(count, 1);
        visited.push(output[0].id);
    }

    use std::collections::HashSet;
    let unique: HashSet<_> = visited.iter().collect();
    assert_eq!(unique.len(), visited.len());
}

#[tokio::test(flavor = "current_thread")]
async fn test_paged_search_with_init_ids_error_cases() {
    let (index, strategy, ctx, query_point) = setup_paged_search_test();

    let l_value = 2;

    let init_ids: &[u32] = &[0, 1];
    let mut state = index
        .start_paged_search_with_init_ids(
            strategy,
            &ctx,
            query_point.as_slice(),
            l_value,
            Some(init_ids),
        )
        .await
        .unwrap();

    let mut output = vec![Neighbor::default(); 2];

    // k > l_value
    let result = index
        .next_search_results(&ctx, &mut state, l_value + 1, &mut output)
        .await;
    assert!(result.is_err());

    // k == 0
    let result = index
        .next_search_results(&ctx, &mut state, 0, &mut output)
        .await;
    assert!(result.is_err());

    // output buffer too small for requested k
    let mut small_output = vec![Neighbor::default(); 1];
    let result = index
        .next_search_results(&ctx, &mut state, 2, &mut small_output)
        .await;
    assert!(result.is_err());
}

#[tokio::test(flavor = "current_thread")]
async fn test_is_any_neighbor_deleted() {
    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let ctx = test_provider::Context::new();
    let mut accessor = index.provider().neighbors();

    // Before any deletion, no node should have deleted neighbors.
    let result = index
        .is_any_neighbor_deleted(&ctx, &mut accessor, 2)
        .await
        .unwrap();
    assert!(!result, "no neighbors should be deleted yet");

    // Delete node 3 — node 2 has neighbors [3, 4], so it should now return true.
    index.provider().delete(&ctx, &3).await.unwrap();

    let result = index
        .is_any_neighbor_deleted(&ctx, &mut accessor, 2)
        .await
        .unwrap();
    assert!(result, "node 2 should detect deleted neighbor 3");

    // Node 0 has neighbors [1, 4] — neither deleted, should still return false.
    let result = index
        .is_any_neighbor_deleted(&ctx, &mut accessor, 0)
        .await
        .unwrap();
    assert!(!result, "node 0 has no deleted neighbors");
}

#[tokio::test(flavor = "current_thread")]
async fn test_drop_deleted_neighbors() {
    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let ctx = test_provider::Context::new();
    let mut accessor = index.provider().neighbors();

    // Delete node 3, then drop deleted neighbors from node 2 (neighbors: [3, 4]).
    index.provider().delete(&ctx, &3).await.unwrap();

    let result = index
        .drop_deleted_neighbors(&ctx, &mut accessor, 2, false)
        .await
        .unwrap();
    assert_eq!(result, graph::ConsolidateKind::Complete);

    // Node 2 should no longer reference deleted node 3.
    let mut list = AdjacencyList::new();
    accessor.get_neighbors(2, &mut list).await.unwrap();
    assert!(
        !list.contains(3),
        "node 2 should not reference deleted node 3"
    );
    assert!(
        list.contains(4),
        "node 2 should still reference start node 4"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn test_drop_deleted_neighbors_only_orphans() {
    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let ctx = test_provider::Context::new();
    let mut accessor = index.provider().neighbors();

    // Delete node 3 but don't clear its adjacency list — it's not an orphan.
    index.provider().delete(&ctx, &3).await.unwrap();

    let result = index
        .drop_deleted_neighbors(&ctx, &mut accessor, 2, true)
        .await
        .unwrap();
    assert_eq!(result, graph::ConsolidateKind::Complete);

    // With only_orphans=true, node 3 still has a non-empty adjacency list,
    // so it should be kept in node 2's neighbor list.
    let mut list = AdjacencyList::new();
    accessor.get_neighbors(2, &mut list).await.unwrap();
    assert!(
        list.contains(3),
        "non-orphan deleted neighbor should be retained"
    );
    assert!(
        list.contains(4),
        "node 2 should still reference start node 4"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn test_drop_deleted_neighbors_noop() {
    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let ctx = test_provider::Context::new();
    let mut accessor = index.provider().neighbors();

    // No deletions — should be a no-op.
    let result = index
        .drop_deleted_neighbors(&ctx, &mut accessor, 0, false)
        .await
        .unwrap();
    assert_eq!(result, graph::ConsolidateKind::Complete);
}

#[tokio::test(flavor = "current_thread")]
async fn test_flat_search_basic() {
    use crate::graph::search::Knn;
    use crate::graph::search_output_buffer::IdDistance;

    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let strategy = test_provider::Strategy::new();
    let ctx = test_provider::Context::new();

    // Query near origin — node 0 at (0,0) is closest.
    // l_value must cover all 5 points (4 data + 1 start) so the working set
    // doesn't drop any before the post-processor runs.
    let query = [0.1_f32, 0.1];
    let params = Knn::new(4, 5, None).unwrap();

    let mut ids = [0u32; 4];
    let mut distances = [0.0f32; 4];
    let mut output = IdDistance::new(&mut ids, &mut distances);

    let stats = index
        .flat_search(
            &strategy,
            &ctx,
            query.as_slice(),
            &|_| true,
            &params,
            &mut output,
        )
        .await
        .unwrap();

    // FilterStartPoints removes the start node, leaving 4 data nodes.
    assert_eq!(stats.result_count, 4);
    let results: std::collections::HashSet<u32> =
        ids[..stats.result_count as usize].iter().copied().collect();
    for id in 0..4u32 {
        assert!(results.contains(&id), "data node {id} should be in results");
    }
}

#[tokio::test(flavor = "current_thread")]
async fn test_flat_search_with_filter() {
    use crate::graph::search::Knn;
    use crate::graph::search_output_buffer::IdDistance;

    let adjacency_list = generate_2d_square_adjacency_list();
    let index = setup_2d_square(create_2d_unit_square(), adjacency_list, 4);
    let strategy = test_provider::Strategy::new();
    let ctx = test_provider::Context::new();

    // Query near origin, but filter out node 0.
    let query = [0.1_f32, 0.1];
    let params = Knn::new(2, 4, None).unwrap();

    let mut ids = [0u32; 2];
    let mut distances = [0.0f32; 2];
    let mut output = IdDistance::new(&mut ids, &mut distances);

    let stats = index
        .flat_search(
            &strategy,
            &ctx,
            query.as_slice(),
            &|ext_id: &u32| *ext_id != 0,
            &params,
            &mut output,
        )
        .await
        .unwrap();

    assert_eq!(stats.result_count, 2);
    assert!(
        !ids[..stats.result_count as usize].contains(&0),
        "node 0 should be filtered out"
    );
    // Nodes 1, 2, 3 remain — closest two to (0.1, 0.1) are 1 (1,0) and 2 (0,1).
    assert!(ids.contains(&1), "node 1 should be present");
    assert!(ids.contains(&2), "node 2 should be present");
}