1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use std::{
convert::TryInto,
error,
fmt,
result,
str::FromStr,
sync::atomic::{AtomicUsize, Ordering},
time::SystemTime,
};
use hex::{self, FromHexError};
use rand::{thread_rng, Rng};
use lazy_static::lazy_static;
const TIMESTAMP_SIZE: usize = 4;
const PROCESS_ID_SIZE: usize = 5;
const COUNTER_SIZE: usize = 3;
const TIMESTAMP_OFFSET: usize = 0;
const PROCESS_ID_OFFSET: usize = TIMESTAMP_OFFSET + TIMESTAMP_SIZE;
const COUNTER_OFFSET: usize = PROCESS_ID_OFFSET + PROCESS_ID_SIZE;
const MAX_U24: usize = 0xFF_FFFF;
lazy_static! {
static ref OID_COUNTER: AtomicUsize = AtomicUsize::new(thread_rng().gen_range(0, MAX_U24 + 1));
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Error {
#[non_exhaustive]
InvalidHexStringCharacter { c: char, index: usize, hex: String },
#[non_exhaustive]
InvalidHexStringLength { length: usize, hex: String },
}
pub type Result<T> = result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::InvalidHexStringCharacter { c, index, hex } => {
write!(
fmt,
"invalid character '{}' was found at index {} in the provided hex string: \
\"{}\"",
c, index, hex
)
}
Error::InvalidHexStringLength { length, hex } => {
write!(
fmt,
"provided hex string representation must be exactly 12 bytes, instead got: \
\"{}\", length {}",
hex, length
)
}
}
}
}
impl error::Error for Error {}
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct ObjectId {
id: [u8; 12],
}
impl Default for ObjectId {
fn default() -> Self {
Self::new()
}
}
impl FromStr for ObjectId {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Self::parse_str(s)
}
}
impl From<[u8; 12]> for ObjectId {
fn from(bytes: [u8; 12]) -> Self {
Self { id: bytes }
}
}
impl ObjectId {
pub fn new() -> ObjectId {
let timestamp = ObjectId::gen_timestamp();
let process_id = ObjectId::gen_process_id();
let counter = ObjectId::gen_count();
let mut buf: [u8; 12] = [0; 12];
buf[TIMESTAMP_OFFSET..(TIMESTAMP_SIZE + TIMESTAMP_OFFSET)]
.clone_from_slice(×tamp[..TIMESTAMP_SIZE]);
buf[PROCESS_ID_OFFSET..(PROCESS_ID_SIZE + PROCESS_ID_OFFSET)]
.clone_from_slice(&process_id[..PROCESS_ID_SIZE]);
buf[COUNTER_OFFSET..(COUNTER_SIZE + COUNTER_OFFSET)]
.clone_from_slice(&counter[..COUNTER_SIZE]);
ObjectId::from_bytes(buf)
}
pub const fn from_bytes(bytes: [u8; 12]) -> ObjectId {
ObjectId { id: bytes }
}
pub fn parse_str(s: impl AsRef<str>) -> Result<ObjectId> {
let s = s.as_ref();
let bytes: Vec<u8> = hex::decode(s.as_bytes()).map_err(|e| match e {
FromHexError::InvalidHexCharacter { c, index } => Error::InvalidHexStringCharacter {
c,
index,
hex: s.to_string(),
},
FromHexError::InvalidStringLength | FromHexError::OddLength => {
Error::InvalidHexStringLength {
length: s.len(),
hex: s.to_string(),
}
}
})?;
if bytes.len() != 12 {
Err(Error::InvalidHexStringLength {
length: s.len(),
hex: s.to_string(),
})
} else {
let mut byte_array: [u8; 12] = [0; 12];
byte_array[..].copy_from_slice(&bytes[..]);
Ok(ObjectId::from_bytes(byte_array))
}
}
pub fn timestamp(&self) -> crate::DateTime {
let mut buf = [0; 4];
buf.copy_from_slice(&self.id[0..4]);
let seconds_since_epoch = u32::from_be_bytes(buf);
crate::DateTime::from_millis(seconds_since_epoch as i64 * 1000)
}
pub const fn bytes(&self) -> [u8; 12] {
self.id
}
pub fn to_hex(self) -> String {
hex::encode(self.id)
}
fn gen_timestamp() -> [u8; 4] {
let timestamp: u32 = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("system clock is before 1970")
.as_secs()
.try_into()
.unwrap();
timestamp.to_be_bytes()
}
fn gen_process_id() -> [u8; 5] {
lazy_static! {
static ref BUF: [u8; 5] = {
let rng = thread_rng().gen_range(0, MAX_U24) as u32;
let mut buf: [u8; 5] = [0; 5];
buf[0..4].copy_from_slice(&rng.to_be_bytes());
buf
};
}
*BUF
}
fn gen_count() -> [u8; 3] {
let u_counter = OID_COUNTER.fetch_add(1, Ordering::SeqCst);
let u = u_counter % (MAX_U24 + 1);
let u_int = u as u64;
let buf = u_int.to_be_bytes();
let buf_u24: [u8; 3] = [buf[5], buf[6], buf[7]];
buf_u24
}
}
impl fmt::Display for ObjectId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_hex())
}
}
impl fmt::Debug for ObjectId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("ObjectId").field(&self.to_hex()).finish()
}
}
#[cfg(test)]
use crate::tests::LOCK;
#[test]
fn count_generated_is_big_endian() {
let _guard = LOCK.run_exclusively();
let start = 1_122_866;
OID_COUNTER.store(start, Ordering::SeqCst);
let count_bytes = ObjectId::gen_count();
let mut buf: [u8; 4] = [0; 4];
buf[1..=COUNTER_SIZE].clone_from_slice(&count_bytes[..COUNTER_SIZE]);
let count = u32::from_be_bytes(buf);
assert_eq!(start as u32, count);
let oid = ObjectId::new();
assert_eq!(0x11u8, oid.bytes()[COUNTER_OFFSET]);
assert_eq!(0x22u8, oid.bytes()[COUNTER_OFFSET + 1]);
assert_eq!(0x33u8, oid.bytes()[COUNTER_OFFSET + 2]);
}
#[test]
fn test_counter_overflow_u24_max() {
let _guard = LOCK.run_exclusively();
let start = MAX_U24;
OID_COUNTER.store(start, Ordering::SeqCst);
let oid = ObjectId::new();
assert_eq!(0xFFu8, oid.bytes()[COUNTER_OFFSET]);
assert_eq!(0xFFu8, oid.bytes()[COUNTER_OFFSET + 1]);
assert_eq!(0xFFu8, oid.bytes()[COUNTER_OFFSET + 2]);
let oid_new = ObjectId::new();
assert_eq!(0x00u8, oid_new.bytes()[COUNTER_OFFSET]);
assert_eq!(0x00u8, oid_new.bytes()[COUNTER_OFFSET + 1]);
assert_eq!(0x00u8, oid_new.bytes()[COUNTER_OFFSET + 2]);
}
#[test]
fn test_counter_overflow_usize_max() {
let _guard = LOCK.run_exclusively();
let start = usize::max_value();
OID_COUNTER.store(start, Ordering::SeqCst);
let oid = ObjectId::new();
assert_eq!(0xFFu8, oid.bytes()[COUNTER_OFFSET]);
assert_eq!(0xFFu8, oid.bytes()[COUNTER_OFFSET + 1]);
assert_eq!(0xFFu8, oid.bytes()[COUNTER_OFFSET + 2]);
let oid_new = ObjectId::new();
assert_eq!(0x00u8, oid_new.bytes()[COUNTER_OFFSET]);
assert_eq!(0x00u8, oid_new.bytes()[COUNTER_OFFSET + 1]);
assert_eq!(0x00u8, oid_new.bytes()[COUNTER_OFFSET + 2]);
}
#[cfg(test)]
mod test {
use chrono::{offset::TimeZone, Utc};
#[test]
fn test_display() {
let id = super::ObjectId::parse_str("53e37d08776f724e42000000").unwrap();
assert_eq!(format!("{}", id), "53e37d08776f724e42000000")
}
#[test]
fn test_debug() {
let id = super::ObjectId::parse_str("53e37d08776f724e42000000").unwrap();
assert_eq!(
format!("{:?}", id),
"ObjectId(\"53e37d08776f724e42000000\")"
);
assert_eq!(
format!("{:#?}", id),
"ObjectId(\n \"53e37d08776f724e42000000\",\n)"
);
}
#[test]
fn test_timestamp() {
let id = super::ObjectId::parse_str("000000000000000000000000").unwrap();
assert_eq!(
Utc.ymd(1970, 1, 1).and_hms(0, 0, 0),
id.timestamp().to_chrono()
);
let id = super::ObjectId::parse_str("7FFFFFFF0000000000000000").unwrap();
assert_eq!(
Utc.ymd(2038, 1, 19).and_hms(3, 14, 7),
id.timestamp().to_chrono()
);
let id = super::ObjectId::parse_str("800000000000000000000000").unwrap();
assert_eq!(
Utc.ymd(2038, 1, 19).and_hms(3, 14, 8),
id.timestamp().to_chrono()
);
let id = super::ObjectId::parse_str("FFFFFFFF0000000000000000").unwrap();
assert_eq!(
Utc.ymd(2106, 2, 7).and_hms(6, 28, 15),
id.timestamp().to_chrono()
);
}
}