use std::{cell::RefCell, sync::Arc};
use kingfisher_vectorscan::{BlockDatabase, BlockScanner};
use thread_local::ThreadLocal;
pub struct ScannerPool {
vsdb: Arc<BlockDatabase>, pool: ThreadLocal<RefCell<BlockScanner<'static>>>,
}
impl ScannerPool {
pub fn new(vsdb: Arc<BlockDatabase>) -> Self {
Self { vsdb, pool: ThreadLocal::new() }
}
#[inline]
pub fn with<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut BlockScanner<'_>) -> R,
{
let cell = self.pool.get_or(|| {
RefCell::new(
BlockScanner::new(unsafe { &*(self.vsdb.as_ref() as *const _) })
.expect("Vectorscan scratch alloc"),
)
});
f(&mut cell.borrow_mut())
}
}