use crate::{GlobalTime, PeerID};
use pergola::{LatticeElt, MaxDef, MaxUnitDefault, Tuple2};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Svw(pub GlobalTime);
#[derive(Clone, Debug, Default, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Srw(pub GlobalTime);
#[derive(Clone, Debug, Default, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Sdw(pub GlobalTime);
#[derive(Clone, Debug, Default, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VWatermark(pub GlobalTime);
#[derive(Clone, Debug, Default, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RWatermark(pub GlobalTime);
impl MaxUnitDefault for Svw {}
impl MaxUnitDefault for Srw {}
#[derive(Clone, Debug, Default, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GroupWatermarks {
pub visibility_watermark: VWatermark,
pub replication_watermark: RWatermark,
}
impl GroupWatermarks {
pub fn new_from_peer_zero_time(peer: PeerID) -> GroupWatermarks {
let z = GlobalTime::time_zero_for(peer);
GroupWatermarks {
visibility_watermark: VWatermark(z.clone()),
replication_watermark: RWatermark(z.clone()),
}
}
}
pub type ServerWatermarksLD = Tuple2<MaxDef<Svw>, MaxDef<Srw>>;
pub type ServerWatermarksLE = LatticeElt<ServerWatermarksLD>;
pub trait ServerWatermarksLEExt {
fn new(svw: Svw, srw: Srw) -> Self;
fn new_from_peer_zero_time(p: PeerID) -> Self;
fn svw(&self) -> &LatticeElt<MaxDef<Svw>>;
fn svw_mut(&mut self) -> &mut LatticeElt<MaxDef<Svw>>;
fn srw(&self) -> &LatticeElt<MaxDef<Srw>>;
fn srw_mut(&mut self) -> &mut LatticeElt<MaxDef<Srw>>;
}
impl ServerWatermarksLEExt for LatticeElt<ServerWatermarksLD> {
fn new(svw: Svw, srw: Srw) -> Self {
LatticeElt::from((svw.into(), srw.into()))
}
fn new_from_peer_zero_time(p: PeerID) -> Self {
let z = GlobalTime::time_zero_for(p);
Self::new(Svw(z), Srw(z))
}
fn svw(&self) -> &LatticeElt<MaxDef<Svw>> {
&self.value.0
}
fn svw_mut(&mut self) -> &mut LatticeElt<MaxDef<Svw>> {
&mut self.value.0
}
fn srw(&self) -> &LatticeElt<MaxDef<Srw>> {
&self.value.1
}
fn srw_mut(&mut self) -> &mut LatticeElt<MaxDef<Srw>> {
&mut self.value.1
}
}