Skip to main content

luci/core/
doc_id.rs

1/// Segment-local document identifier.
2///
3/// Range: `0..max_doc-1` within a segment. All index structures within a
4/// segment share this ID space — see [[architecture-segment-layout]].
5///
6/// `Copy` because DocIds are passed by value constantly in the hot scoring
7/// loop. Public inner field because callers frequently need the raw `u32`
8/// for array indexing, bitset operations, and serialization.
9#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct DocId(pub u32);
11
12/// Sentinel value indicating no more matching documents.
13///
14/// Guaranteed to be greater than any valid `DocId`. This means the maximum
15/// valid `doc_count` per segment is `u32::MAX - 1` (~4.29 billion) — see
16/// [[architecture-segment-layout#Document Count Fields]].
17pub const NO_MORE_DOCS: DocId = DocId(u32::MAX);
18
19impl DocId {
20    pub const fn new(id: u32) -> Self {
21        Self(id)
22    }
23
24    pub const fn as_u32(self) -> u32 {
25        self.0
26    }
27}
28
29impl std::fmt::Display for DocId {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        write!(f, "{}", self.0)
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn no_more_docs_is_greater_than_any_valid_doc_id() {
41        assert!(NO_MORE_DOCS > DocId(0));
42        assert!(NO_MORE_DOCS > DocId(u32::MAX - 1));
43    }
44
45    #[test]
46    fn ordering() {
47        assert!(DocId(0) < DocId(1));
48        assert!(DocId(1) < DocId(100));
49        assert!(DocId(100) < DocId(u32::MAX - 1));
50    }
51
52    #[test]
53    fn as_u32_round_trips() {
54        assert_eq!(DocId::new(42).as_u32(), 42);
55        assert_eq!(DocId(0).as_u32(), 0);
56    }
57
58    #[test]
59    fn display() {
60        assert_eq!(format!("{}", DocId(123)), "123");
61    }
62}