refactored sequence_id to sequence and switched to wrapping shifts

This commit is contained in:
cbax 2024-09-29 14:33:08 -04:00
parent f13e04ff85
commit 74d9e36b99
3 changed files with 31 additions and 48 deletions

2
Cargo.lock generated
View file

@ -4,7 +4,7 @@ version = 3
[[package]] [[package]]
name = "icechip" name = "icechip"
version = "0.1.0" version = "0.1.1"
dependencies = [ dependencies = [
"tinyrand", "tinyrand",
] ]

View file

@ -1,9 +1,9 @@
[package] [package]
name = "icechip" name = "icechip"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
description = "A mostly true to spec Twitter Snowflake implementation, with multiple modes." description = "A mostly true to spec Twitter Snowflake implementation, with multiple modes."
repository = "https://git.cbax.dev/icechip" repository = "https://git.cbax.dev/cbax/icechip"
readme = "README.md" readme = "README.md"
license = "MIT" license = "MIT"
publish = true publish = true

View file

@ -13,21 +13,15 @@ pub struct Icechip<T> {
version: Version, version: Version,
timestamp: T, timestamp: T,
machine_id: T, machine_id: T,
sequence_id: T, sequence: T,
epoch: Option<u128>, epoch: Option<u128>,
} }
/*impl fmt::Display for Icechip<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.into::<T>());
}
}*/
impl Into<u64> for Icechip<u64> { impl Into<u64> for Icechip<u64> {
fn into(self) -> u64 { fn into(self) -> u64 {
let mut ret: u64 = self.timestamp.checked_shl(23).unwrap(); let mut ret: u64 = self.timestamp.wrapping_shl(23);
ret = ret + self.machine_id.checked_shl(12).unwrap(); ret = ret + self.machine_id.wrapping_shl(12);
ret = ret + self.sequence_id; ret = ret + self.sequence;
return ret; return ret;
} }
} }
@ -36,9 +30,9 @@ impl From<u64> for Icechip<u64> {
fn from(value: u64) -> Icechip<u64> { fn from(value: u64) -> Icechip<u64> {
return Icechip { return Icechip {
version: Version::ENHANCED, version: Version::ENHANCED,
timestamp: value.checked_shr(23).unwrap(), timestamp: value.wrapping_shr(23),
machine_id: value.checked_shl(41).unwrap().checked_shr(53).unwrap(), machine_id: value.wrapping_shl(41).wrapping_shr(53),
sequence_id: value.checked_shl(52).unwrap().checked_shr(52).unwrap(), sequence: value.wrapping_shl(52).wrapping_shr(52),
epoch: None, epoch: None,
}; };
} }
@ -46,9 +40,9 @@ impl From<u64> for Icechip<u64> {
impl Into<u128> for Icechip<u128> { impl Into<u128> for Icechip<u128> {
fn into(self) -> u128 { fn into(self) -> u128 {
let mut ret: u128 = self.timestamp.checked_shl(64).unwrap(); let mut ret: u128 = self.timestamp.wrapping_shl(64);
ret = ret + self.machine_id.checked_shl(32).unwrap(); ret = ret + self.machine_id.wrapping_shl(32);
ret = ret + self.sequence_id; ret = ret + self.sequence;
return ret; return ret;
} }
} }
@ -57,15 +51,9 @@ impl From<u128> for Icechip<u128> {
fn from(value: u128) -> Icechip<u128> { fn from(value: u128) -> Icechip<u128> {
return Icechip { return Icechip {
version: Version::EXTENDED, version: Version::EXTENDED,
timestamp: value.checked_shr(64).unwrap(), timestamp: value.wrapping_shr(64),
machine_id: value machine_id: value.wrapping_shr(32).wrapping_shl(64).wrapping_shr(64),
.checked_shr(32) sequence: value.wrapping_shl(96).wrapping_shr(96),
.unwrap()
.checked_shl(64)
.unwrap()
.checked_shr(64)
.unwrap(),
sequence_id: value.checked_shl(96).unwrap().checked_shr(96).unwrap(),
epoch: None, epoch: None,
}; };
} }
@ -84,8 +72,8 @@ impl<T: Clone> Icechip<T> {
return self.machine_id.clone(); return self.machine_id.clone();
} }
pub fn sequence_id(&self) -> T { pub fn sequence(&self) -> T {
return self.sequence_id.clone(); return self.sequence.clone();
} }
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
@ -99,13 +87,8 @@ impl Icechip<u64> {
return Icechip { return Icechip {
version: Version::ENHANCED, version: Version::ENHANCED,
timestamp: 0, timestamp: 0,
machine_id: rand machine_id: rand.next_u64().wrapping_shl(41).wrapping_shr(53),
.next_u64() sequence: 0,
.checked_shl(41)
.unwrap()
.checked_shr(53)
.unwrap(),
sequence_id: 0,
epoch, epoch,
} }
.tick(); .tick();
@ -116,11 +99,11 @@ impl Icechip<u64> {
Ok(o) => { Ok(o) => {
let o = o.as_millis(); let o = o.as_millis();
let curr_ts = match self.epoch { let curr_ts = match self.epoch {
Some(s) => (o - s).checked_shl(23).unwrap().checked_shr(23).unwrap() as u64, Some(s) => (o - s).wrapping_shl(23).wrapping_shr(23) as u64,
None => o.checked_shl(23).unwrap().checked_shr(23).unwrap() as u64, None => o.wrapping_shl(23).wrapping_shr(23) as u64,
}; };
if self.timestamp == curr_ts { if self.timestamp == curr_ts {
if (self.sequence_id + 1) > 1023 { if (self.sequence + 1) > 1023 {
std::thread::sleep(std::time::Duration::from_millis(1)); std::thread::sleep(std::time::Duration::from_millis(1));
return self.tick(); return self.tick();
} else { } else {
@ -128,7 +111,7 @@ impl Icechip<u64> {
version: self.version.clone(), version: self.version.clone(),
timestamp: self.timestamp.clone(), timestamp: self.timestamp.clone(),
machine_id: self.machine_id.clone(), machine_id: self.machine_id.clone(),
sequence_id: self.sequence_id.clone() + 1, sequence: self.sequence.clone() + 1,
epoch: self.epoch.clone(), epoch: self.epoch.clone(),
}; };
} }
@ -137,7 +120,7 @@ impl Icechip<u64> {
version: self.version.clone(), version: self.version.clone(),
timestamp: curr_ts.clone(), timestamp: curr_ts.clone(),
machine_id: self.machine_id.clone(), machine_id: self.machine_id.clone(),
sequence_id: 0, sequence: 0,
epoch: self.epoch.clone(), epoch: self.epoch.clone(),
}; };
} }
@ -154,7 +137,7 @@ impl Icechip<u64> {
version: Version::ENHANCED, version: Version::ENHANCED,
timestamp: 0, timestamp: 0,
machine_id, machine_id,
sequence_id: 0, sequence: 0,
epoch, epoch,
} }
.tick(); .tick();
@ -172,7 +155,7 @@ impl Icechip<u128> {
version: Version::EXTENDED, version: Version::EXTENDED,
timestamp: 0, timestamp: 0,
machine_id: rand.next_u128(), machine_id: rand.next_u128(),
sequence_id: 0, sequence: 0,
epoch, epoch,
} }
.tick(); .tick();
@ -187,7 +170,7 @@ impl Icechip<u128> {
None => o, None => o,
}; };
if self.timestamp == curr_ts { if self.timestamp == curr_ts {
if (self.sequence_id + 1) > u32::MAX as u128 { if (self.sequence + 1) > u32::MAX as u128 {
std::thread::sleep(std::time::Duration::from_millis(1)); std::thread::sleep(std::time::Duration::from_millis(1));
return self.tick(); return self.tick();
} else { } else {
@ -195,7 +178,7 @@ impl Icechip<u128> {
version: self.version.clone(), version: self.version.clone(),
timestamp: self.timestamp.clone(), timestamp: self.timestamp.clone(),
machine_id: self.machine_id.clone(), machine_id: self.machine_id.clone(),
sequence_id: self.sequence_id.clone() + 1, sequence: self.sequence.clone() + 1,
epoch: self.epoch.clone(), epoch: self.epoch.clone(),
}; };
} }
@ -204,7 +187,7 @@ impl Icechip<u128> {
version: self.version.clone(), version: self.version.clone(),
timestamp: o.clone(), timestamp: o.clone(),
machine_id: self.machine_id.clone(), machine_id: self.machine_id.clone(),
sequence_id: 0, sequence: 0,
epoch: self.epoch.clone(), epoch: self.epoch.clone(),
}; };
} }
@ -221,7 +204,7 @@ impl Icechip<u128> {
version: Version::EXTENDED, version: Version::EXTENDED,
timestamp: 0, timestamp: 0,
machine_id, machine_id,
sequence_id: 0, sequence: 0,
epoch, epoch,
} }
.tick(); .tick();