Skip to main content

sonic/store/
identifiers.rs

1// Sonic
2//
3// Fast, lightweight and schema-less search backend
4// Copyright: 2019, Valerian Saliou <valerian@valeriansaliou.name>
5// License: Mozilla Public License v2.0 (MPL v2.0)
6
7use std::hash::Hasher;
8use twox_hash::XxHash32;
9
10pub type StoreObjectIID = u32;
11pub type StoreObjectOID<'a> = &'a str;
12pub type StoreTermHashed = u32;
13
14pub struct StoreTermHash;
15
16pub enum StoreMetaKey {
17    IIDIncr,
18}
19
20pub enum StoreMetaValue {
21    IIDIncr(StoreObjectIID),
22}
23
24impl StoreMetaKey {
25    pub fn as_u32(&self) -> u32 {
26        match self {
27            StoreMetaKey::IIDIncr => 0,
28        }
29    }
30}
31
32impl StoreTermHash {
33    pub fn from(term: &str) -> StoreTermHashed {
34        let mut hasher = XxHash32::with_seed(0);
35
36        hasher.write(term.as_bytes());
37
38        hasher.finish() as u32
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn it_converts_meta_key_to_u32() {
48        assert_eq!(StoreMetaKey::IIDIncr.as_u32(), 0);
49    }
50
51    #[test]
52    fn it_hashes_term() {
53        assert_eq!(StoreTermHash::from("hash:1"), 3637660813);
54        assert_eq!(StoreTermHash::from("hash:2"), 3577985381);
55    }
56}