use log::info;
mod any_stored_vec;
mod any_vec;
mod compute;
mod importable;
mod readable;
mod readable_cloneable;
mod stored;
mod typed;
mod writable;
use crate::{
AnyStoredVec, AnyVec, Exit, Result, StoredVec, Version, WritableVec,
traits::writable::MAX_CACHE_SIZE,
};
#[derive(Debug)]
#[must_use = "Vector should be stored to keep data accessible"]
pub struct EagerVec<V>(pub(super) V);
impl<V> EagerVec<V>
where
V: StoredVec,
{
fn compute_init<F>(&mut self, version: Version, max_from: V::I, exit: &Exit, f: F) -> Result<()>
where
F: FnMut(&mut Self) -> Result<()>,
{
self.validate_computed_version_or_reset(version)?;
self.truncate_if_needed(max_from)?;
self.repeat_until_complete(exit, f)
}
#[inline]
pub fn batch_end(&self, max_end: usize) -> usize {
let size = size_of::<V::T>().max(1);
let cap = MAX_CACHE_SIZE.div_ceil(size);
(self.len() + cap).min(max_end)
}
pub fn repeat_until_complete<F>(&mut self, exit: &Exit, mut f: F) -> Result<()>
where
F: FnMut(&mut Self) -> Result<()>,
{
loop {
f(self)?;
let batch_limit_reached = self.batch_limit_reached();
if batch_limit_reached {
info!("Batch limit reached, saving to disk...");
}
if self.is_dirty() {
let _lock = exit.lock();
self.write()?;
}
if !batch_limit_reached {
break;
}
}
Ok(())
}
pub fn remove(self) -> Result<()> {
self.0.remove()
}
}