ate_crypto/spec/
primary_key.rs

1use std::mem::size_of;
2use std::cell::RefCell;
3use serde::*;
4
5use crate::AteHash;
6
7/// All event and data objects within ATE have a primary key that uniquely represents
8/// it and allows it to be indexed and referenced. This primary key can be derived from
9/// other input data like strings or numbers in order to make object lookups that are
10/// static (e.g. root nodes)
11#[allow(dead_code)]
12#[derive(Serialize, Deserialize, Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)]
13pub struct PrimaryKey {
14    key: u64,
15}
16
17impl Default for PrimaryKey {
18    fn default() -> PrimaryKey {
19        PrimaryKey::generate()
20    }
21}
22
23impl PrimaryKey {
24    thread_local! {
25        static CURRENT: RefCell<Option<PrimaryKey>> = RefCell::new(None)
26    }
27
28    pub fn current_get() -> Option<PrimaryKey> {
29        PrimaryKey::CURRENT.with(|key| {
30            let key = key.borrow();
31            return key.clone();
32        })
33    }
34
35    pub fn current_set(val: Option<PrimaryKey>) -> Option<PrimaryKey> {
36        PrimaryKey::CURRENT.with(|key| {
37            let mut key = key.borrow_mut();
38            match val {
39                Some(a) => key.replace(a),
40                None => key.take(),
41            }
42        })
43    }
44
45    #[allow(dead_code)]
46    pub fn generate() -> PrimaryKey {
47        PrimaryKey {
48            key: fastrand::u64(..),
49        }
50    }
51
52    #[allow(dead_code)]
53    pub fn new(key: u64) -> PrimaryKey {
54        PrimaryKey { key: key }
55    }
56
57    #[allow(dead_code)]
58    pub fn sizeof() -> u64 {
59        size_of::<u64>() as u64
60    }
61
62    pub fn as_hex_string(&self) -> String {
63        format!("{:X?}", self.key).to_string()
64    }
65
66    pub fn as_fixed_hex_string(&self) -> String {
67        let hex = format!("{:016X?}", self.key).to_string();
68        let hex = hex.to_lowercase();
69        format!("{}", &hex[..16])
70    }
71
72    pub fn as_u64(&self) -> u64 {
73        self.key
74    }
75
76    pub fn from_ext(val: AteHash, min: u64, max: u64) -> PrimaryKey {
77        let v = val.val;
78        let bytes = [v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]];
79
80        let range = max - min;
81        let key = (u64::from_be_bytes(bytes) % range) + min;
82        PrimaryKey { key }
83    }
84}
85
86impl From<AteHash> for PrimaryKey {
87    fn from(val: AteHash) -> PrimaryKey {
88        let v = val.val;
89        let bytes = [v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]];
90        PrimaryKey {
91            key: u64::from_be_bytes(bytes),
92        }
93    }
94}
95
96impl From<String> for PrimaryKey {
97    fn from(val: String) -> PrimaryKey {
98        PrimaryKey::from(AteHash::from(val))
99    }
100}
101
102impl From<&'static str> for PrimaryKey {
103    fn from(val: &'static str) -> PrimaryKey {
104        PrimaryKey::from(AteHash::from(val))
105    }
106}
107
108impl From<u64> for PrimaryKey {
109    fn from(val: u64) -> PrimaryKey {
110        PrimaryKey { key: val }
111    }
112}
113
114impl std::fmt::Display for PrimaryKey {
115    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116        write!(f, "{}", self.as_hex_string())
117    }
118}
119
120#[test]
121fn test_key_hex() {
122    let key = PrimaryKey::from(1 as u64);
123    assert_eq!(key.as_fixed_hex_string(), "0000000000000001".to_string());
124}