summavy/core/
segment_id.rs

1use std::cmp::{Ord, Ordering};
2use std::error::Error;
3use std::fmt;
4use std::str::FromStr;
5#[cfg(test)]
6use std::sync::atomic;
7
8#[cfg(test)]
9use once_cell::sync::Lazy;
10use serde::{Deserialize, Serialize};
11use uuid::Uuid;
12
13/// Uuid identifying a segment.
14///
15/// Tantivy's segment are identified
16/// by a UUID which is used to prefix the filenames
17/// of all of the file associated with the segment.
18///
19/// In unit test, for reproducibility, the `SegmentId` are
20/// simply generated in an autoincrement fashion.
21#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
22pub struct SegmentId(Uuid);
23
24#[cfg(test)]
25static AUTO_INC_COUNTER: Lazy<atomic::AtomicUsize> = Lazy::new(atomic::AtomicUsize::default);
26
27#[cfg(test)]
28const ZERO_ARRAY: [u8; 8] = [0u8; 8];
29
30// During tests, we generate the segment id in a autoincrement manner
31// for consistency of segment id between run.
32//
33// The order of the test execution is not guaranteed, but the order
34// of segments within a single test is guaranteed.
35#[cfg(test)]
36fn create_uuid() -> Uuid {
37    let new_auto_inc_id = (*AUTO_INC_COUNTER).fetch_add(1, atomic::Ordering::SeqCst);
38    Uuid::from_fields(new_auto_inc_id as u32, 0, 0, &ZERO_ARRAY)
39}
40
41#[cfg(not(test))]
42fn create_uuid() -> Uuid {
43    Uuid::new_v4()
44}
45
46impl SegmentId {
47    #[doc(hidden)]
48    pub fn generate_random() -> SegmentId {
49        SegmentId(create_uuid())
50    }
51
52    /// Returns a shorter identifier of the segment.
53    ///
54    /// We are using UUID4, so only 6 bits are fixed,
55    /// and the rest is random.
56    ///
57    /// Picking the first 8 chars is ok to identify
58    /// segments in a display message (e.g. a5c4dfcb).
59    pub fn short_uuid_string(&self) -> String {
60        self.0.as_simple().to_string()[..8].to_string()
61    }
62
63    /// Returns a segment uuid string.
64    ///
65    /// It consists in 32 lowercase hexadecimal chars
66    /// (e.g. a5c4dfcbdfe645089129e308e26d5523)
67    pub fn uuid_string(&self) -> String {
68        self.0.as_simple().to_string()
69    }
70
71    /// Build a `SegmentId` string from the full uuid string.
72    ///
73    /// E.g. "a5c4dfcbdfe645089129e308e26d5523"
74    pub fn from_uuid_string(uuid_string: &str) -> Result<SegmentId, SegmentIdParseError> {
75        FromStr::from_str(uuid_string)
76    }
77}
78
79/// Error type used when parsing a `SegmentId` from a string fails.
80pub struct SegmentIdParseError(uuid::Error);
81
82impl Error for SegmentIdParseError {}
83
84impl fmt::Debug for SegmentIdParseError {
85    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86        self.0.fmt(f)
87    }
88}
89
90impl fmt::Display for SegmentIdParseError {
91    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92        self.0.fmt(f)
93    }
94}
95
96impl FromStr for SegmentId {
97    type Err = SegmentIdParseError;
98
99    fn from_str(uuid_string: &str) -> Result<Self, SegmentIdParseError> {
100        let uuid = Uuid::parse_str(uuid_string).map_err(SegmentIdParseError)?;
101        Ok(SegmentId(uuid))
102    }
103}
104
105impl fmt::Debug for SegmentId {
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        write!(f, "Seg({:?})", self.short_uuid_string())
108    }
109}
110
111impl fmt::Display for SegmentId {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        write!(f, "Seg({:?})", self.short_uuid_string())
114    }
115}
116
117impl PartialOrd for SegmentId {
118    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
119        Some(self.cmp(other))
120    }
121}
122
123impl Ord for SegmentId {
124    fn cmp(&self, other: &Self) -> Ordering {
125        self.0.as_bytes().cmp(other.0.as_bytes())
126    }
127}
128
129#[cfg(test)]
130mod tests {
131    use super::SegmentId;
132
133    #[test]
134    fn test_to_uuid_string() {
135        let full_uuid = "a5c4dfcbdfe645089129e308e26d5523";
136        let segment_id = SegmentId::from_uuid_string(full_uuid).unwrap();
137        assert_eq!(segment_id.uuid_string(), full_uuid);
138        assert_eq!(segment_id.short_uuid_string(), "a5c4dfcb");
139        // one extra char
140        assert!(SegmentId::from_uuid_string("a5c4dfcbdfe645089129e308e26d5523b").is_err());
141    }
142}