MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / top_k

Method top_k

nodedb-cluster/src/distributed_vector/merge.rs:77–86  ·  view source on GitHub ↗

Merge all shard results and return the global top-K. Sorts by distance ascending (nearest first) and truncates to `top_k`. Ties are broken by shard_id for deterministic ordering.

(&mut self, top_k: usize)

Source from the content-addressed store, hash-verified

75 /// Sorts by distance ascending (nearest first) and truncates to `top_k`.
76 /// Ties are broken by shard_id for deterministic ordering.
77 pub fn top_k(&mut self, top_k: usize) -> Vec<VectorHit> {
78 self.all_hits.sort_by(|a, b| {
79 a.distance
80 .partial_cmp(&b.distance)
81 .unwrap_or(std::cmp::Ordering::Equal)
82 .then(a.shard_id.cmp(&b.shard_id))
83 });
84 self.all_hits.truncate(top_k);
85 self.all_hits.clone()
86 }
87
88 /// Number of total hits collected (before merge).
89 pub fn total_hits(&self) -> usize {

Callers 5

merge_two_shardsFunction · 0.45
merge_with_failed_shardFunction · 0.45
top_k_truncationFunction · 0.45
merge_top_kMethod · 0.45

Calls 4

partial_cmpMethod · 0.45
cmpMethod · 0.45
truncateMethod · 0.45
cloneMethod · 0.45

Tested by 4

merge_two_shardsFunction · 0.36
merge_with_failed_shardFunction · 0.36
top_k_truncationFunction · 0.36