pub struct NgramFilter { /* private fields */ }Expand description
A filter based on required n-grams (2-byte subsequences).
A block passes if all n-grams from at least one pattern might be present.
§Example
use flashsieve::NgramFilter;
let filter = NgramFilter::from_patterns(&[b"abc".as_slice()]);
// Blocks that might contain "ab" AND "bc" will matchImplementations§
Source§impl NgramFilter
impl NgramFilter
Sourcepub fn from_patterns(patterns: &[&[u8]]) -> Self
pub fn from_patterns(patterns: &[&[u8]]) -> Self
Build a filter from a set of patterns.
Patterns shorter than two bytes contribute zero n-grams and therefore match any bloom filter.
§Examples
use flashsieve::{NgramFilter, NgramBloom};
let filter = NgramFilter::from_patterns(&[b"hello".as_slice()]);
let bloom = NgramBloom::from_block(b"hello world", 1024).unwrap();
assert!(filter.matches_bloom(&bloom));Sourcepub fn matches_bloom(&self, bloom: &NgramBloom) -> bool
pub fn matches_bloom(&self, bloom: &NgramBloom) -> bool
Check if a bloom filter passes this filter.
Returns true when all 2-byte n-grams from at least one source pattern
might be present in the bloom filter.
§Example
use flashsieve::{NgramFilter, NgramBloom};
let filter = NgramFilter::from_patterns(&[b"hello".as_slice()]);
let bloom = NgramBloom::from_block(b"hello world", 1024).unwrap();
assert!(filter.matches_bloom(&bloom));Sourcepub fn quick_reject(&self, data: &[u8]) -> bool
pub fn quick_reject(&self, data: &[u8]) -> bool
Fast-path heuristic that checks only the first 4KB of data.
This avoids building a full bloom filter for large files when checking a few n-grams.
This is a heuristic with possible false positives, but never false negatives for patterns that appear entirely within the first 4KB.
§Example
use flashsieve::NgramFilter;
let filter = NgramFilter::from_patterns(&[b"hello".as_slice()]);
let data = b"hello world";
assert!(filter.quick_reject(data));Sourcepub fn matches_bloom_pair(&self, b1: &NgramBloom, b2: &NgramBloom) -> bool
pub fn matches_bloom_pair(&self, b1: &NgramBloom, b2: &NgramBloom) -> bool
Check if the union of two bloom filters passes this filter.
§Example
use flashsieve::{NgramFilter, NgramBloom};
let filter = NgramFilter::from_patterns(&[b"ab".as_slice()]);
let b1 = NgramBloom::from_block(b"ab", 1024).unwrap();
let b2 = NgramBloom::from_block(b"xy", 1024).unwrap();
assert!(filter.matches_bloom_pair(&b1, &b2));Sourcepub fn matches_bloom_multi(&self, blooms: &[NgramBloom]) -> bool
pub fn matches_bloom_multi(&self, blooms: &[NgramBloom]) -> bool
Check if the union of multiple consecutive bloom filters passes this filter.
Returns true when all 2-byte n-grams from at least one source pattern
might be present in the union of the provided blooms.
§Example
use flashsieve::{NgramFilter, NgramBloom};
let filter = NgramFilter::from_patterns(&[b"ab".as_slice()]);
let b1 = NgramBloom::from_block(b"ab", 1024).unwrap();
let b2 = NgramBloom::from_block(b"cd", 1024).unwrap();
let b3 = NgramBloom::from_block(b"ef", 1024).unwrap();
assert!(filter.matches_bloom_multi(&[b1, b2, b3]));Trait Implementations§
Source§impl Clone for NgramFilter
impl Clone for NgramFilter
Source§fn clone(&self) -> NgramFilter
fn clone(&self) -> NgramFilter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more