use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use bytes::Bytes;
use tokio::sync::{Mutex, Notify};
use crate::types::BlockKey;
#[derive(Debug, Clone)]
pub struct FlushJob {
pub key: BlockKey,
pub absolute_offset: u64,
pub data: Bytes,
pub crc32c: u32,
}
pub struct FlushCoalescer {
pending: Mutex<HashMap<BlockKey, FlushJob>>,
inflight: Mutex<HashSet<BlockKey>>,
notify: Notify,
closed: AtomicBool,
}
impl FlushCoalescer {
pub fn new() -> Arc<Self> {
Arc::new(Self {
pending: Mutex::new(HashMap::new()),
inflight: Mutex::new(HashSet::new()),
notify: Notify::new(),
closed: AtomicBool::new(false),
})
}
pub async fn submit(&self, job: FlushJob) {
{
let mut pending = self.pending.lock().await;
pending.insert(job.key, job);
}
self.notify.notify_waiters();
}
pub fn close(&self) {
self.closed.store(true, Ordering::SeqCst);
self.notify.notify_waiters();
}
pub async fn is_idle(&self) -> bool {
self.pending.lock().await.is_empty() && self.inflight.lock().await.is_empty()
}
pub async fn take_batch(&self, max: usize) -> Option<Vec<FlushJob>> {
let max = max.max(1);
loop {
{
let mut pending = self.pending.lock().await;
let mut inflight = self.inflight.lock().await;
if !pending.is_empty() {
let mut batch = Vec::with_capacity(max.min(pending.len()));
let keys: Vec<BlockKey> = pending.keys().copied().take(max).collect();
for key in keys {
if let Some(job) = pending.remove(&key) {
inflight.insert(key);
batch.push(job);
}
}
return Some(batch);
}
if self.closed.load(Ordering::SeqCst) && inflight.is_empty() {
return None;
}
}
self.notify.notified().await;
}
}
pub async fn complete_ok(&self, key: BlockKey) -> bool {
self.inflight.lock().await.remove(&key);
let clear_memory = !self.pending.lock().await.contains_key(&key);
self.notify.notify_waiters();
clear_memory
}
pub async fn complete_err_requeue(&self, job: FlushJob) {
self.inflight.lock().await.remove(&job.key);
{
let mut pending = self.pending.lock().await;
pending.entry(job.key).or_insert(job);
}
self.notify.notify_waiters();
}
pub async fn complete_err_drop(&self, key: BlockKey) {
self.inflight.lock().await.remove(&key);
self.notify.notify_waiters();
}
}