operation_queue.rs

  1use std::{fmt::Debug, ops::Add};
  2use sum_tree::{Dimension, Edit, Item, KeyedItem, SumTree, Summary};
  3
  4pub trait Operation: Clone + Debug {
  5    fn lamport_timestamp(&self) -> clock::Lamport;
  6}
  7
  8#[derive(Clone, Debug)]
  9struct OperationItem<T>(T);
 10
 11#[derive(Clone, Debug)]
 12pub struct OperationQueue<T: Operation>(SumTree<OperationItem<T>>);
 13
 14#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
 15pub struct OperationKey(clock::Lamport);
 16
 17#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
 18pub struct OperationSummary {
 19    pub key: OperationKey,
 20    pub len: usize,
 21}
 22
 23impl OperationKey {
 24    pub fn new(timestamp: clock::Lamport) -> Self {
 25        Self(timestamp)
 26    }
 27}
 28
 29impl<T: Operation> Default for OperationQueue<T> {
 30    fn default() -> Self {
 31        OperationQueue::new()
 32    }
 33}
 34
 35impl<T: Operation> OperationQueue<T> {
 36    pub fn new() -> Self {
 37        OperationQueue(SumTree::new())
 38    }
 39
 40    pub fn len(&self) -> usize {
 41        self.0.summary().len
 42    }
 43
 44    pub fn is_empty(&self) -> bool {
 45        self.len() == 0
 46    }
 47
 48    pub fn insert(&mut self, mut ops: Vec<T>) {
 49        ops.sort_by_key(|op| op.lamport_timestamp());
 50        ops.dedup_by_key(|op| op.lamport_timestamp());
 51        self.0.edit(
 52            ops.into_iter()
 53                .map(|op| Edit::Insert(OperationItem(op)))
 54                .collect(),
 55            &(),
 56        );
 57    }
 58
 59    pub fn drain(&mut self) -> Self {
 60        let clone = self.clone();
 61        self.0 = SumTree::new();
 62        clone
 63    }
 64
 65    pub fn iter(&self) -> impl Iterator<Item = &T> {
 66        self.0.iter().map(|i| &i.0)
 67    }
 68}
 69
 70impl Summary for OperationSummary {
 71    type Context = ();
 72
 73    fn add_summary(&mut self, other: &Self, _: &()) {
 74        assert!(self.key < other.key);
 75        self.key = other.key;
 76        self.len += other.len;
 77    }
 78}
 79
 80impl<'a> Add<&'a Self> for OperationSummary {
 81    type Output = Self;
 82
 83    fn add(self, other: &Self) -> Self {
 84        assert!(self.key < other.key);
 85        OperationSummary {
 86            key: other.key,
 87            len: self.len + other.len,
 88        }
 89    }
 90}
 91
 92impl<'a> Dimension<'a, OperationSummary> for OperationKey {
 93    fn add_summary(&mut self, summary: &OperationSummary, _: &()) {
 94        assert!(*self <= summary.key);
 95        *self = summary.key;
 96    }
 97}
 98
 99impl<T: Operation> Item for OperationItem<T> {
100    type Summary = OperationSummary;
101
102    fn summary(&self) -> Self::Summary {
103        OperationSummary {
104            key: OperationKey::new(self.0.lamport_timestamp()),
105            len: 1,
106        }
107    }
108}
109
110impl<T: Operation> KeyedItem for OperationItem<T> {
111    type Key = OperationKey;
112
113    fn key(&self) -> Self::Key {
114        OperationKey::new(self.0.lamport_timestamp())
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn test_len() {
124        let mut clock = clock::Lamport::new(0);
125
126        let mut queue = OperationQueue::new();
127        assert_eq!(queue.len(), 0);
128
129        queue.insert(vec![
130            TestOperation(clock.tick()),
131            TestOperation(clock.tick()),
132        ]);
133        assert_eq!(queue.len(), 2);
134
135        queue.insert(vec![TestOperation(clock.tick())]);
136        assert_eq!(queue.len(), 3);
137
138        drop(queue.drain());
139        assert_eq!(queue.len(), 0);
140
141        queue.insert(vec![TestOperation(clock.tick())]);
142        assert_eq!(queue.len(), 1);
143    }
144
145    #[derive(Clone, Debug, Eq, PartialEq)]
146    struct TestOperation(clock::Lamport);
147
148    impl Operation for TestOperation {
149        fn lamport_timestamp(&self) -> clock::Lamport {
150            self.0
151        }
152    }
153}