use std::sync::Mutex;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OperationType {
Insert,
Delete,
}
#[derive(Debug, Clone)]
pub struct BatchEntry<K, V> {
pub operation_type: OperationType,
pub target_key: K,
pub target_value: Option<V>,
}
pub struct BatchCollector<K, V> {
entries: Mutex<Vec<BatchEntry<K, V>>>,
}
impl<K, V> Default for BatchCollector<K, V> {
fn default() -> Self {
Self { entries: Mutex::new(Vec::new()) }
}
}
impl<K: PartialOrd, V> BatchCollector<K, V> {
pub fn append_operation(&self, entry: BatchEntry<K, V>) {
let mut guard = self.entries.lock().unwrap();
guard.push(entry);
}
pub fn extract_and_sort(&self) -> Vec<BatchEntry<K, V>> {
let mut guard = self.entries.lock().unwrap();
let mut extracted = std::mem::take(&mut *guard);
extracted.sort_by(|a, b| {
a.target_key.partial_cmp(&b.target_key).unwrap_or(std::cmp::Ordering::Equal)
});
extracted
}
}