datumesh_http/find_order.rs
1//! The order of `/find` results (CONTRACT-http-listing L5, *[25 Nov]*): a true order.
2//!
3//! [`today_cmp`] is today's key (what `find.rs` sorts by in 0.5.x); [`order`] is the sort by [`sort_keys`] and
4//! [`cmp_sort_keys`].
5//!
6//! 1. **Group.** Results that share a subject form one group; a result in no subject is a group of its own.
7//! 2. **Place the groups.** Each group takes the place its best member has by today's key (match type, then score
8//! descending, then kind, then name); groups are sorted by that key.
9//! 3. **Order inside a group** by catalogue rank (1 first); equal ranks by today's key.
10//!
11//! A result's subject, for the order, is the one subject (of the catalogue job's `source(k)` cells) in which it has its
12//! lowest rank; on a tie, the subject whose key encoding is first in byte order. With no subjects at all, the order is
13//! exactly today's.
14
15use std::cmp::Ordering;
16
17/// A result's place in a subject: the subject's key encoding (`<host>/catalog/subject(domain:"..",...)`) and the result's
18/// catalogue rank in it (1 first).
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub struct SubjectRank {
21 /// The subject's key encoding.
22 pub subject: String,
23 /// The catalogue rank, from 1.
24 pub rank: u32,
25}
26
27/// What the order knows of one result. Names are unique within one answer.
28#[derive(Clone, Debug, PartialEq)]
29pub struct FindKey {
30 /// 0 exact, 1 text, 2 vector.
31 pub match_type: u8,
32 /// The score (higher first); finite.
33 pub score: f64,
34 /// 0 class, 1 method, 2 function.
35 pub kind: u8,
36 /// The full name.
37 pub name: String,
38 /// The result's subject and rank, when it is in one.
39 pub subject: Option<SubjectRank>,
40}
41
42/// Today's key (0.5.x): match type, then score descending, then kind, then name.
43pub fn today_cmp(a: &FindKey, b: &FindKey) -> Ordering {
44 a.match_type
45 .cmp(&b.match_type)
46 .then(b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal))
47 .then(a.kind.cmp(&b.kind))
48 .then(a.name.cmp(&b.name))
49}
50
51/// The key of one result in the grouped order, given the whole answer (see [`sort_keys`]). Opaque.
52#[derive(Clone, Debug, PartialEq)]
53pub struct SortKey {
54 /// The best member of the result's group, by today's key: the group's place.
55 place: FindKey,
56 /// The result's catalogue rank inside its group (0 for a result in no subject).
57 rank: u32,
58 /// The result itself, by today's key (the tie-break inside a group).
59 own: FindKey,
60}
61
62/// Each result's key in the grouped order of the answer `results` (in the same order as `results`). Comparing two keys
63/// with [`cmp_sort_keys`] is a total order on the answer's results.
64pub fn sort_keys(results: &[FindKey]) -> Vec<SortKey> {
65 // The group of a result: its subject's key encoding, or its own name when it is in no subject (names are unique).
66 let group_of = |k: &FindKey| -> String {
67 match &k.subject {
68 Some(s) => format!("s:{}", s.subject),
69 None => format!("n:{}", k.name),
70 }
71 };
72 let mut best: std::collections::HashMap<String, FindKey> = Default::default();
73 for k in results {
74 let g = group_of(k);
75 match best.get(&g) {
76 Some(b) if today_cmp(b, k) != Ordering::Greater => {}
77 _ => {
78 best.insert(g, k.clone());
79 }
80 }
81 }
82 results
83 .iter()
84 .map(|k| SortKey {
85 place: best[&group_of(k)].clone(),
86 rank: k.subject.as_ref().map(|s| s.rank).unwrap_or(0),
87 own: k.clone(),
88 })
89 .collect()
90}
91
92/// The comparison of two keys of one answer: the groups by their places (today's key of the best members), then inside a
93/// group by catalogue rank, then by today's key. Two results share a place exactly when they share a group, and names are
94/// unique, so the comparison is total.
95pub fn cmp_sort_keys(a: &SortKey, b: &SortKey) -> Ordering {
96 if a.place.name == b.place.name {
97 a.rank.cmp(&b.rank).then_with(|| today_cmp(&a.own, &b.own))
98 } else {
99 today_cmp(&a.place, &b.place)
100 }
101}
102
103/// The indices of `results` in the order of `/find` (steps 1 to 3). The same multiset of results in any input order
104/// gives the same sequence of names.
105pub fn order(results: &[FindKey]) -> Vec<usize> {
106 let keys = sort_keys(results);
107 let mut idx: Vec<usize> = (0..results.len()).collect();
108 idx.sort_by(|&a, &b| cmp_sort_keys(&keys[a], &keys[b]));
109 idx
110}