1use std::collections::VecDeque;
8
9use parking_lot_dummy::Mutex;
10
11use crate::divergence::Divergence;
12
13#[derive(Clone, Debug)]
17pub struct DivergenceEntry {
18 pub key: Vec<u8>,
20 pub divergence: Divergence,
22}
23
24#[derive(Debug)]
26pub struct DivergenceLog {
27 capacity: usize,
28 entries: Mutex<VecDeque<DivergenceEntry>>,
29}
30
31impl DivergenceLog {
32 pub fn new(capacity: usize) -> Self {
34 Self {
35 capacity,
36 entries: Mutex::new(VecDeque::with_capacity(capacity)),
37 }
38 }
39
40 pub fn push(&self, entry: DivergenceEntry) {
42 if self.capacity == 0 {
43 return;
44 }
45 let mut g = self.entries.lock();
46 if g.len() == self.capacity {
47 g.pop_front();
48 }
49 g.push_back(entry);
50 }
51
52 pub fn snapshot(&self) -> Vec<DivergenceEntry> {
54 let g = self.entries.lock();
55 g.iter().cloned().collect()
56 }
57
58 pub fn len(&self) -> usize {
60 self.entries.lock().len()
61 }
62
63 pub fn is_empty(&self) -> bool {
65 self.entries.lock().is_empty()
66 }
67}
68
69impl Default for DivergenceLog {
70 fn default() -> Self {
71 Self::new(128)
72 }
73}
74
75mod parking_lot_dummy {
78 use std::sync::Mutex as StdMutex;
79
80 #[derive(Debug)]
81 pub struct Mutex<T>(StdMutex<T>);
82
83 impl<T> Mutex<T> {
84 pub fn new(t: T) -> Self {
85 Self(StdMutex::new(t))
86 }
87 pub fn lock(&self) -> std::sync::MutexGuard<'_, T> {
88 self.0
89 .lock()
90 .unwrap_or_else(std::sync::PoisonError::into_inner)
91 }
92 }
93}