2021-05-06 10:32:14 +03:00
|
|
|
use std::{fmt::Debug, ops::Add};
|
2021-12-09 18:38:46 +03:00
|
|
|
use sum_tree::{Dimension, Edit, Item, KeyedItem, SumTree, Summary};
|
|
|
|
|
|
|
|
pub trait Operation: Clone + Debug {
|
|
|
|
fn lamport_timestamp(&self) -> clock::Lamport;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct OperationItem<T>(T);
|
2021-03-18 22:13:31 +03:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2021-12-09 18:38:46 +03:00
|
|
|
pub struct OperationQueue<T: Operation>(SumTree<OperationItem<T>>);
|
2021-03-18 22:13:31 +03:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
|
2021-10-04 15:34:02 +03:00
|
|
|
pub struct OperationKey(clock::Lamport);
|
2021-03-18 22:13:31 +03:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
|
|
|
pub struct OperationSummary {
|
2021-08-21 01:18:39 +03:00
|
|
|
pub key: OperationKey,
|
|
|
|
pub len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OperationKey {
|
2021-10-04 15:34:02 +03:00
|
|
|
pub fn new(timestamp: clock::Lamport) -> Self {
|
2021-08-21 01:18:39 +03:00
|
|
|
Self(timestamp)
|
|
|
|
}
|
2021-03-18 22:13:31 +03:00
|
|
|
}
|
|
|
|
|
2022-08-11 00:39:24 +03:00
|
|
|
impl<T: Operation> Default for OperationQueue<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
OperationQueue::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 18:38:46 +03:00
|
|
|
impl<T: Operation> OperationQueue<T> {
|
2021-03-18 22:13:31 +03:00
|
|
|
pub fn new() -> Self {
|
2024-09-18 04:43:59 +03:00
|
|
|
OperationQueue(SumTree::default())
|
2021-03-18 22:13:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.summary().len
|
|
|
|
}
|
|
|
|
|
2022-08-11 00:39:24 +03:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
2021-12-09 18:38:46 +03:00
|
|
|
pub fn insert(&mut self, mut ops: Vec<T>) {
|
2021-08-21 01:18:39 +03:00
|
|
|
ops.sort_by_key(|op| op.lamport_timestamp());
|
|
|
|
ops.dedup_by_key(|op| op.lamport_timestamp());
|
2021-12-09 18:38:46 +03:00
|
|
|
self.0.edit(
|
|
|
|
ops.into_iter()
|
|
|
|
.map(|op| Edit::Insert(OperationItem(op)))
|
|
|
|
.collect(),
|
|
|
|
&(),
|
|
|
|
);
|
2021-03-18 22:13:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn drain(&mut self) -> Self {
|
|
|
|
let clone = self.clone();
|
2024-09-18 04:43:59 +03:00
|
|
|
self.0 = SumTree::default();
|
2021-03-18 22:13:31 +03:00
|
|
|
clone
|
|
|
|
}
|
|
|
|
|
2021-12-09 18:38:46 +03:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
2022-01-05 13:52:41 +03:00
|
|
|
self.0.iter().map(|i| &i.0)
|
2021-03-18 22:13:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 10:32:14 +03:00
|
|
|
impl Summary for OperationSummary {
|
2021-05-06 17:13:35 +03:00
|
|
|
type Context = ();
|
|
|
|
|
2024-09-18 04:43:59 +03:00
|
|
|
fn zero(_cx: &()) -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2021-05-06 20:29:38 +03:00
|
|
|
fn add_summary(&mut self, other: &Self, _: &()) {
|
2021-03-18 22:13:31 +03:00
|
|
|
assert!(self.key < other.key);
|
|
|
|
self.key = other.key;
|
|
|
|
self.len += other.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Add<&'a Self> for OperationSummary {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, other: &Self) -> Self {
|
|
|
|
assert!(self.key < other.key);
|
|
|
|
OperationSummary {
|
|
|
|
key: other.key,
|
|
|
|
len: self.len + other.len,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Dimension<'a, OperationSummary> for OperationKey {
|
2024-09-18 04:43:59 +03:00
|
|
|
fn zero(_cx: &()) -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2021-06-01 13:50:10 +03:00
|
|
|
fn add_summary(&mut self, summary: &OperationSummary, _: &()) {
|
2021-03-18 22:13:31 +03:00
|
|
|
assert!(*self <= summary.key);
|
|
|
|
*self = summary.key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 18:38:46 +03:00
|
|
|
impl<T: Operation> Item for OperationItem<T> {
|
2021-08-21 01:18:39 +03:00
|
|
|
type Summary = OperationSummary;
|
|
|
|
|
|
|
|
fn summary(&self) -> Self::Summary {
|
|
|
|
OperationSummary {
|
2021-12-09 18:38:46 +03:00
|
|
|
key: OperationKey::new(self.0.lamport_timestamp()),
|
2021-08-21 01:18:39 +03:00
|
|
|
len: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 18:38:46 +03:00
|
|
|
impl<T: Operation> KeyedItem for OperationItem<T> {
|
2021-08-21 01:18:39 +03:00
|
|
|
type Key = OperationKey;
|
|
|
|
|
|
|
|
fn key(&self) -> Self::Key {
|
2021-12-09 18:38:46 +03:00
|
|
|
OperationKey::new(self.0.lamport_timestamp())
|
2021-08-21 01:18:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:13:31 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_len() {
|
2021-10-04 15:34:02 +03:00
|
|
|
let mut clock = clock::Lamport::new(0);
|
2021-03-18 22:13:31 +03:00
|
|
|
|
|
|
|
let mut queue = OperationQueue::new();
|
|
|
|
assert_eq!(queue.len(), 0);
|
|
|
|
|
|
|
|
queue.insert(vec![
|
2021-12-09 18:38:46 +03:00
|
|
|
TestOperation(clock.tick()),
|
|
|
|
TestOperation(clock.tick()),
|
2021-03-18 22:13:31 +03:00
|
|
|
]);
|
|
|
|
assert_eq!(queue.len(), 2);
|
|
|
|
|
2021-12-09 18:38:46 +03:00
|
|
|
queue.insert(vec![TestOperation(clock.tick())]);
|
2021-03-18 22:13:31 +03:00
|
|
|
assert_eq!(queue.len(), 3);
|
|
|
|
|
|
|
|
drop(queue.drain());
|
|
|
|
assert_eq!(queue.len(), 0);
|
|
|
|
|
2021-12-09 18:38:46 +03:00
|
|
|
queue.insert(vec![TestOperation(clock.tick())]);
|
2021-03-18 22:13:31 +03:00
|
|
|
assert_eq!(queue.len(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2021-10-04 15:34:02 +03:00
|
|
|
struct TestOperation(clock::Lamport);
|
2021-12-09 18:38:46 +03:00
|
|
|
|
|
|
|
impl Operation for TestOperation {
|
|
|
|
fn lamport_timestamp(&self) -> clock::Lamport {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 22:13:31 +03:00
|
|
|
}
|