2021-05-18 09:31:40 +00:00
|
|
|
use smallvec::SmallVec;
|
2021-03-10 04:00:51 +00:00
|
|
|
use std::cmp::{self, Ordering};
|
|
|
|
use std::ops::{Add, AddAssign};
|
2021-04-09 09:02:46 +00:00
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub type ReplicaId = u16;
|
2021-05-18 09:31:40 +00:00
|
|
|
pub type Seq = u32;
|
2021-04-07 17:56:15 +00:00
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, Ord, PartialOrd)]
|
|
|
|
pub struct Local {
|
|
|
|
pub replica_id: ReplicaId,
|
2021-04-07 17:56:15 +00:00
|
|
|
pub value: Seq,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
|
|
|
pub struct Lamport {
|
2021-04-07 17:56:15 +00:00
|
|
|
pub value: Seq,
|
2021-03-10 04:00:51 +00:00
|
|
|
pub replica_id: ReplicaId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Local {
|
|
|
|
pub fn new(replica_id: ReplicaId) -> Self {
|
|
|
|
Self {
|
|
|
|
replica_id,
|
|
|
|
value: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self) -> Self {
|
|
|
|
let timestamp = *self;
|
|
|
|
self.value += 1;
|
|
|
|
timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn observe(&mut self, timestamp: Self) {
|
|
|
|
if timestamp.replica_id == self.replica_id {
|
|
|
|
self.value = cmp::max(self.value, timestamp.value + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Add<&'a Self> for Local {
|
|
|
|
type Output = Local;
|
|
|
|
|
|
|
|
fn add(self, other: &'a Self) -> Self::Output {
|
|
|
|
cmp::max(&self, other).clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AddAssign<&'a Local> for Local {
|
|
|
|
fn add_assign(&mut self, other: &Self) {
|
|
|
|
if *self < *other {
|
|
|
|
*self = other.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:27:54 +00:00
|
|
|
#[derive(Clone, Debug, Default, Hash, Eq, PartialEq)]
|
2021-05-18 09:31:40 +00:00
|
|
|
pub struct Global(SmallVec<[Local; 3]>);
|
2021-04-09 09:02:46 +00:00
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
impl Global {
|
|
|
|
pub fn new() -> Self {
|
2021-04-09 09:02:46 +00:00
|
|
|
Self::default()
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:56:15 +00:00
|
|
|
pub fn get(&self, replica_id: ReplicaId) -> Seq {
|
2021-05-18 09:31:40 +00:00
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.find(|t| t.replica_id == replica_id)
|
|
|
|
.map_or(0, |t| t.value)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn observe(&mut self, timestamp: Local) {
|
2021-05-18 09:31:40 +00:00
|
|
|
if let Some(entry) = self
|
|
|
|
.0
|
|
|
|
.iter_mut()
|
|
|
|
.find(|t| t.replica_id == timestamp.replica_id)
|
|
|
|
{
|
|
|
|
entry.value = cmp::max(entry.value, timestamp.value);
|
|
|
|
} else {
|
|
|
|
self.0.push(timestamp);
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn observe_all(&mut self, other: &Self) {
|
2021-05-18 09:31:40 +00:00
|
|
|
for timestamp in other.0.iter() {
|
|
|
|
self.observe(*timestamp);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn observed(&self, timestamp: Local) -> bool {
|
|
|
|
self.get(timestamp.replica_id) >= timestamp.value
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn changed_since(&self, other: &Self) -> bool {
|
2021-05-18 09:31:40 +00:00
|
|
|
self.0.iter().any(|t| t.value > other.get(t.replica_id))
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for Global {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
let mut global_ordering = Ordering::Equal;
|
|
|
|
|
2021-05-18 09:31:40 +00:00
|
|
|
for timestamp in self.0.iter().chain(other.0.iter()) {
|
|
|
|
let ordering = self
|
|
|
|
.get(timestamp.replica_id)
|
|
|
|
.cmp(&other.get(timestamp.replica_id));
|
2021-03-10 04:00:51 +00:00
|
|
|
if ordering != Ordering::Equal {
|
|
|
|
if global_ordering == Ordering::Equal {
|
|
|
|
global_ordering = ordering;
|
|
|
|
} else if ordering != global_ordering {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(global_ordering)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lamport {
|
|
|
|
pub fn new(replica_id: ReplicaId) -> Self {
|
|
|
|
Self {
|
|
|
|
value: 1,
|
|
|
|
replica_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self) -> Self {
|
|
|
|
let timestamp = *self;
|
|
|
|
self.value += 1;
|
|
|
|
timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn observe(&mut self, timestamp: Self) {
|
|
|
|
self.value = cmp::max(self.value, timestamp.value) + 1;
|
|
|
|
}
|
|
|
|
}
|