prolly-map 0.3.0

Content-addressed versioned map storage primitives.
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
use super::{
    adaptive_should_stop, engine::insert_top_k, AdaptiveContext, FrontierEntry, PreparedFilter,
    SearchCandidate, SearchRequest,
};
use crate::prolly::cid::Cid;
use crate::prolly::error::Error;
use crate::prolly::proximity::distance::{prepare_vector, query_score};
use crate::prolly::proximity::storage::quantized::ScalarQuantized;
use crate::prolly::proximity::storage::vector::ExternalVector;
use crate::prolly::proximity::storage::{
    Descriptor, PhysicalNodeKind, ProximityNode, StoredRecord, VectorRef,
};
use crate::prolly::proximity::{
    DistanceMetric, Neighbor, ProximitySearchStats, ProximityTree, SearchBackend, SearchCompletion,
    SearchPolicy, SearchResult,
};
use crate::prolly::store::AsyncStore;
use crate::prolly::AsyncProlly;
use std::collections::{BTreeMap, BinaryHeap, HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Instant;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AsyncIoConfig {
    pub max_in_flight_reads: usize,
    pub prefetch_window: usize,
    pub max_buffered_bytes: usize,
}

impl Default for AsyncIoConfig {
    fn default() -> Self {
        Self {
            max_in_flight_reads: 8,
            prefetch_window: 16,
            max_buffered_bytes: 8 * 1024 * 1024,
        }
    }
}

impl AsyncIoConfig {
    fn validate(&self) -> Result<(), Error> {
        if self.max_in_flight_reads == 0
            || self.prefetch_window == 0
            || self.max_buffered_bytes == 0
        {
            return Err(Error::InvalidProximitySearch {
                reason: "async I/O limits must be greater than zero".to_owned(),
            });
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Default)]
pub struct CancellationToken(Arc<AtomicBool>);

impl CancellationToken {
    pub fn cancel(&self) {
        self.0.store(true, Ordering::Release);
    }

    pub fn is_cancelled(&self) -> bool {
        self.0.load(Ordering::Acquire)
    }
}

#[derive(Clone, Debug, Default)]
pub struct AsyncSearchControl {
    pub io: AsyncIoConfig,
    pub cancellation: Option<CancellationToken>,
    pub deadline: Option<Instant>,
}

pub struct AsyncProximityMap<S: AsyncStore> {
    store: S,
    directory: AsyncProlly<S>,
    tree: ProximityTree,
}

impl<S> AsyncProximityMap<S>
where
    S: AsyncStore + Clone,
    S::Error: Send + Sync,
{
    pub async fn load(store: S, descriptor_cid: Cid) -> Result<Self, Error> {
        let descriptor_bytes = load_content(&store, &descriptor_cid).await?;
        let descriptor = Descriptor::decode(&descriptor_bytes)?;
        let root_bytes = load_content(&store, &descriptor.proximity_root).await?;
        let root = ProximityNode::decode(&root_bytes, descriptor.config.dimensions)?;
        if root.subtree_count != descriptor.count {
            return Err(Error::InvalidProximityObject {
                kind: "descriptor",
                reason: "record count disagrees with proximity root".to_owned(),
            });
        }
        let directory = AsyncProlly::new(store.clone(), descriptor.directory.config.clone());
        Ok(Self {
            store,
            directory,
            tree: ProximityTree {
                directory: descriptor.directory,
                proximity_root: descriptor.proximity_root,
                descriptor: descriptor_cid,
                count: descriptor.count,
                config: descriptor.config,
            },
        })
    }

    pub fn tree(&self) -> &ProximityTree {
        &self.tree
    }

    pub async fn search(
        &self,
        request: SearchRequest<'_>,
        control: AsyncSearchControl,
    ) -> Result<SearchResult, Error> {
        request.validate()?;
        control.io.validate()?;
        if matches!(
            request.backend,
            SearchBackend::ProductQuantized | SearchBackend::Hnsw
        ) {
            return Err(Error::InvalidProximitySearch {
                reason: "requested backend requires a validated accelerator sidecar".to_owned(),
            });
        }
        let filter = PreparedFilter::new(request.filter.clone(), &self.tree.directory)?;
        let query = prepare_vector(
            self.tree.config.metric,
            request.query,
            self.tree.config.dimensions,
        )?;
        let use_scalar_quantization =
            crate::prolly::proximity::accelerator::sq8::enabled(&self.tree.config, request.policy);
        let mut stats = ProximitySearchStats::default();
        let mut frontier = BinaryHeap::new();
        frontier.push(FrontierEntry {
            bound: 0.0,
            score: 0.0,
            key: Vec::new(),
            cid: self.tree.proximity_root.clone(),
            expected_level: None,
        });
        let mut candidates = Vec::<SearchCandidate>::new();
        let mut score_cache = BTreeMap::<Vec<u8>, f64>::new();
        let mut visited = HashSet::new();
        let mut levels = HashSet::new();
        let mut buffered = HashMap::<Cid, Vec<u8>>::new();
        let mut buffered_bytes = 0usize;
        let mut last_fanout = 0usize;
        let mut completion = SearchCompletion::Exact;

        while let Some(next) = frontier.peek() {
            if let Some(stopped) = stop_reason(&control) {
                completion = stopped;
                break;
            }
            if !use_scalar_quantization
                && self.tree.config.metric == DistanceMetric::L2Squared
                && candidates.len() == request.k
                && next.bound > candidates.last().expect("full top-k").score
            {
                break;
            }
            if let SearchPolicy::Adaptive(quality) = request.policy {
                if candidates.last().is_some_and(|worst| {
                    let overlapping = frontier
                        .iter()
                        .filter(|entry| entry.bound <= worst.score)
                        .count();
                    adaptive_should_stop(
                        quality,
                        AdaptiveContext {
                            results: candidates.len(),
                            k: request.k,
                            frontier_bound: next.bound,
                            worst_score: worst.score,
                            overlapping_clusters: overlapping,
                            logical_level: next.expected_level.unwrap_or(u8::MAX),
                            last_fanout,
                            cluster_count: frontier.len(),
                        },
                    )
                }) {
                    completion = SearchCompletion::ApproximatePolicySatisfied;
                    break;
                }
            }
            if request
                .budget
                .max_nodes
                .is_some_and(|maximum| stats.nodes_read >= maximum)
            {
                completion = SearchCompletion::BudgetExhausted;
                break;
            }

            prefetch(
                &self.store,
                &frontier,
                &control,
                &mut buffered,
                &mut buffered_bytes,
                &mut stats,
            )
            .await?;
            if let Some(stopped) = stop_reason(&control) {
                completion = stopped;
                break;
            }
            let next = frontier.pop().expect("peeked frontier");
            if !visited.insert(next.cid.clone()) {
                return Err(Error::InvalidProximityObject {
                    kind: "node",
                    reason: "cycle or repeated child ownership".to_owned(),
                });
            }
            let bytes = match buffered.remove(&next.cid) {
                Some(bytes) => {
                    buffered_bytes -= bytes.len();
                    bytes
                }
                None => {
                    let bytes = load_content(&self.store, &next.cid).await?;
                    stats.physical_bytes_read += bytes.len();
                    bytes
                }
            };
            if let Some(stopped) = stop_reason(&control) {
                completion = stopped;
                break;
            }
            let mut node = ProximityNode::decode(&bytes, self.tree.config.dimensions)?;
            let mut committed = bytes.len();
            for entry in &mut node.entries {
                let VectorRef::External(cid) = &entry.vector else {
                    continue;
                };
                let bytes = load_content(&self.store, cid).await?;
                if let Some(stopped) = stop_reason(&control) {
                    completion = stopped;
                    break;
                }
                stats.physical_bytes_read += bytes.len();
                committed += bytes.len();
                let external = ExternalVector::decode(&bytes)?;
                if external.vector.len() != self.tree.config.dimensions as usize {
                    return Err(Error::InvalidProximityObject {
                        kind: "vector",
                        reason: "external vector dimension mismatch".to_owned(),
                    });
                }
                entry.vector = VectorRef::Inline(external.vector);
            }
            let quantizer = if use_scalar_quantization && node.kind.has_children(node.level) {
                let config = self
                    .tree
                    .config
                    .scalar_quantization
                    .as_ref()
                    .expect("checked scalar quantization configuration");
                let cid = node
                    .quantizer
                    .as_ref()
                    .ok_or_else(|| Error::InvalidProximityObject {
                        kind: "quantizer",
                        reason: "configured node has no scalar quantizer".to_owned(),
                    })?;
                let bytes = load_content(&self.store, cid).await?;
                stats.physical_bytes_read += bytes.len();
                committed += bytes.len();
                let quantizer = ScalarQuantized::decode(&bytes)?;
                if quantizer.dimensions != self.tree.config.dimensions
                    || quantizer.group_size != config.group_size
                {
                    return Err(Error::InvalidProximityObject {
                        kind: "quantizer",
                        reason: "quantizer configuration disagrees with descriptor".to_owned(),
                    });
                }
                if quantizer.entry_count != node.entries.len() as u64 {
                    return Err(Error::InvalidProximityObject {
                        kind: "quantizer",
                        reason: "quantizer entry count disagrees with node".to_owned(),
                    });
                }
                Some(quantizer)
            } else {
                None
            };
            if completion != SearchCompletion::Exact {
                break;
            }
            if next
                .expected_level
                .is_some_and(|expected| node.level != expected)
            {
                return Err(Error::InvalidProximityObject {
                    kind: "node",
                    reason: "child has an unexpected logical level".to_owned(),
                });
            }
            if request
                .budget
                .max_committed_bytes
                .is_some_and(|maximum| stats.committed_bytes.saturating_add(committed) > maximum)
            {
                completion = SearchCompletion::BudgetExhausted;
                break;
            }
            stats.nodes_read += 1;
            stats.bytes_read += committed;
            stats.committed_bytes += committed;
            levels.insert(node.level);
            stats.levels_visited = levels.len();
            last_fanout = node.entries.len();

            for (entry_index, entry) in node.entries.iter().enumerate() {
                if let Some(stopped) = stop_reason(&control) {
                    completion = stopped;
                    break;
                }
                if node.kind.has_children(node.level) {
                    if !filter.intersects(&entry.min_key, &entry.max_key) {
                        continue;
                    }
                    let child = entry.child.clone().expect("validated internal child");
                    let representative_score = if let Some(quantizer) = &quantizer {
                        if distance_budget_exhausted(&request, &stats) {
                            completion = SearchCompletion::BudgetExhausted;
                            break;
                        }
                        stats.quantized_distance_evaluations += 1;
                        quantizer.approximate_score(self.tree.config.metric, &query, entry_index)?
                    } else {
                        match score_cache.get(&entry.key) {
                            Some(score) => *score,
                            None => {
                                if distance_budget_exhausted(&request, &stats) {
                                    completion = SearchCompletion::BudgetExhausted;
                                    break;
                                }
                                stats.distance_evaluations += 1;
                                let value = query_score(
                                    request.kernel,
                                    self.tree.config.metric,
                                    &query,
                                    entry.vector.inline()?,
                                );
                                score_cache.insert(entry.key.clone(), value);
                                value
                            }
                        }
                    };
                    let bound = if quantizer.is_none()
                        && self.tree.config.metric == DistanceMetric::L2Squared
                    {
                        crate::prolly::proximity::distance::canonical::l2_lower_bound_down(
                            representative_score,
                            entry.covering_radius,
                        )
                    } else {
                        representative_score
                    };
                    if request
                        .budget
                        .max_frontier_entries
                        .is_some_and(|maximum| frontier.len() >= maximum)
                    {
                        completion = SearchCompletion::BudgetExhausted;
                        break;
                    }
                    frontier.push(FrontierEntry {
                        bound,
                        score: representative_score,
                        key: entry.key.clone(),
                        cid: child,
                        expected_level: Some(if node.kind == PhysicalNodeKind::OverflowDirectory {
                            node.level
                        } else {
                            node.level - 1
                        }),
                    });
                    stats.frontier_peak = stats.frontier_peak.max(frontier.len());
                } else if filter.contains(&entry.key) {
                    let leaf_score = match score_cache.get(&entry.key) {
                        Some(score) => *score,
                        None => {
                            if distance_budget_exhausted(&request, &stats) {
                                completion = SearchCompletion::BudgetExhausted;
                                break;
                            }
                            stats.distance_evaluations += 1;
                            let value = query_score(
                                request.kernel,
                                self.tree.config.metric,
                                &query,
                                entry.vector.inline()?,
                            );
                            score_cache.insert(entry.key.clone(), value);
                            value
                        }
                    };
                    insert_top_k(
                        &mut candidates,
                        SearchCandidate {
                            key: entry.key.clone(),
                            vector: entry.vector.inline()?.to_vec(),
                            score: leaf_score,
                        },
                        request.k,
                    );
                }
            }
            if completion != SearchCompletion::Exact {
                break;
            }
        }

        if use_scalar_quantization {
            stats.reranked_candidates = candidates.len();
        }
        let mut neighbors = Vec::with_capacity(candidates.len());
        for candidate in candidates {
            let bytes = self
                .directory
                .get(&self.tree.directory, &candidate.key)
                .await?
                .ok_or_else(|| Error::InvalidProximityObject {
                    kind: "node",
                    reason: "leaf key is absent from exact directory".to_owned(),
                })?;
            let record = StoredRecord::decode(&bytes, self.tree.config.dimensions)?;
            if record.vector != candidate.vector {
                return Err(Error::InvalidProximityObject {
                    kind: "node",
                    reason: "leaf vector disagrees with exact directory".to_owned(),
                });
            }
            neighbors.push(Neighbor {
                key: candidate.key,
                value: record.value,
                distance: candidate.score,
            });
        }
        Ok(SearchResult {
            neighbors,
            stats,
            completion,
        })
    }
}

async fn prefetch<S>(
    store: &S,
    frontier: &BinaryHeap<FrontierEntry>,
    control: &AsyncSearchControl,
    buffered: &mut HashMap<Cid, Vec<u8>>,
    buffered_bytes: &mut usize,
    stats: &mut ProximitySearchStats,
) -> Result<(), Error>
where
    S: AsyncStore,
    S::Error: Send + Sync,
{
    let mut ordered = frontier.clone();
    let mut cids = Vec::new();
    let limit = control
        .io
        .prefetch_window
        .min(control.io.max_in_flight_reads);
    while cids.len() < limit {
        let Some(entry) = ordered.pop() else { break };
        if !buffered.contains_key(&entry.cid) {
            cids.push(entry.cid);
        }
    }
    if cids.is_empty() {
        return Ok(());
    }
    let keys: Vec<_> = cids.iter().map(Cid::as_bytes).collect();
    let values = store
        .batch_get_ordered_unique(&keys)
        .await
        .map_err(|error| Error::Store(Box::new(error)))?;
    for (cid, value) in cids.into_iter().zip(values) {
        let bytes = value.ok_or_else(|| Error::NotFound(cid.clone()))?;
        let actual = Cid::from_bytes(&bytes);
        if actual != cid {
            return Err(Error::CidMismatch {
                expected: cid,
                actual,
            });
        }
        stats.physical_bytes_read += bytes.len();
        if buffered_bytes.saturating_add(bytes.len()) <= control.io.max_buffered_bytes {
            *buffered_bytes += bytes.len();
            buffered.insert(cid, bytes);
        }
    }
    Ok(())
}

fn stop_reason(control: &AsyncSearchControl) -> Option<SearchCompletion> {
    if control
        .cancellation
        .as_ref()
        .is_some_and(CancellationToken::is_cancelled)
    {
        Some(SearchCompletion::Cancelled)
    } else if control
        .deadline
        .is_some_and(|deadline| Instant::now() >= deadline)
    {
        Some(SearchCompletion::DeadlineExceeded)
    } else {
        None
    }
}

fn distance_budget_exhausted(request: &SearchRequest<'_>, stats: &ProximitySearchStats) -> bool {
    request
        .budget
        .max_distance_evaluations
        .is_some_and(|maximum| {
            stats
                .distance_evaluations
                .saturating_add(stats.quantized_distance_evaluations)
                >= maximum
        })
}

async fn load_content<S>(store: &S, cid: &Cid) -> Result<Vec<u8>, Error>
where
    S: AsyncStore,
    S::Error: Send + Sync,
{
    let bytes = store
        .get(cid.as_bytes())
        .await
        .map_err(|error| Error::Store(Box::new(error)))?
        .ok_or_else(|| Error::NotFound(cid.clone()))?;
    let actual = Cid::from_bytes(&bytes);
    if actual != *cid {
        return Err(Error::CidMismatch {
            expected: cid.clone(),
            actual,
        });
    }
    Ok(bytes)
}