use crate::bloom::NgramBloom;
use crate::error::{Error, Result};
use crate::filter::{ByteFilter, NgramFilter};
use crate::index::{BlockIndex, CandidateRange};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileBloomIndex {
inner: BlockIndex,
file_bloom: NgramBloom,
}
impl FileBloomIndex {
pub fn try_new(inner: BlockIndex) -> Result<Self> {
if inner.block_count() == 0 {
return Err(Error::EmptyBlockIndex);
}
let file_bloom = NgramBloom::union_of(&inner.blooms)?;
Ok(Self { inner, file_bloom })
}
#[must_use]
pub fn inner(&self) -> &BlockIndex {
&self.inner
}
#[must_use]
pub fn file_bloom(&self) -> &NgramBloom {
&self.file_bloom
}
#[must_use]
pub fn into_inner(self) -> BlockIndex {
self.inner
}
#[must_use]
pub fn block_size(&self) -> usize {
self.inner.block_size()
}
#[must_use]
pub fn block_count(&self) -> usize {
self.inner.block_count()
}
#[must_use]
pub fn total_data_length(&self) -> usize {
self.inner.total_data_length()
}
#[must_use]
pub fn candidate_blocks_byte(&self, filter: &ByteFilter) -> Vec<CandidateRange> {
self.inner.candidate_blocks_byte(filter)
}
#[must_use]
pub fn candidate_blocks_ngram(&self, filter: &NgramFilter) -> Vec<CandidateRange> {
if !filter.matches_bloom(&self.file_bloom) {
return Vec::new();
}
self.inner.candidate_blocks_ngram(filter)
}
#[must_use]
pub fn candidate_blocks(
&self,
byte_filter: &ByteFilter,
ngram_filter: &NgramFilter,
) -> Vec<CandidateRange> {
if !ngram_filter.matches_bloom(&self.file_bloom) {
return Vec::new();
}
self.inner.candidate_blocks(byte_filter, ngram_filter)
}
#[allow(clippy::cast_precision_loss)]
#[must_use]
pub fn selectivity(&self, ranges: &[CandidateRange]) -> f64 {
self.inner.selectivity(ranges)
}
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
self.inner.to_bytes()
}
}