Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: try upgrade regex-automata #3575

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 1 addition & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ prost = "0.12"
raft-engine = { version = "0.4.1", default-features = false }
rand = "0.8"
regex = "1.8"
regex-automata = { version = "0.2", features = ["transducer"] }
regex-automata = { version = "0.4" }
reqwest = { version = "0.11", default-features = false, features = [
"json",
"rustls-tls-native-roots",
Expand Down
2 changes: 1 addition & 1 deletion src/index/src/inverted_index/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub enum Error {
#[snafu(display("Failed to parse regex DFA"))]
ParseDFA {
#[snafu(source)]
error: Box<regex_automata::dfa::Error>,
error: Box<regex_automata::dfa::dense::BuildError>,
location: Location,
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ use std::mem::size_of;
use fst::map::OpBuilder;
use fst::{IntoStreamer, Streamer};
use regex_automata::dfa::dense::DFA;
use regex_automata::dfa::Automaton;
use regex_automata::util::primitives::StateID;
use regex_automata::util::start::Config;
use regex_automata::Anchored;
use snafu::{ensure, ResultExt};

use crate::inverted_index::error::{
Expand All @@ -32,7 +36,53 @@ pub struct IntersectionFstApplier {
ranges: Vec<Range>,

/// A list of `Dfa` compiled from regular expression patterns.
dfas: Vec<DFA<Vec<u32>>>,
dfas: Vec<DfaFstAutomaton>,
}

#[derive(Debug)]
struct DfaFstAutomaton(DFA<Vec<u32>>);
tisonkun marked this conversation as resolved.
Show resolved Hide resolved

impl fst::Automaton for DfaFstAutomaton {
type State = StateID;

#[inline]
fn start(&self) -> Self::State {
let config = Config::new().anchored(Anchored::No);
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
self.0.start_state(&config).unwrap()
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
}

#[inline]
fn is_match(&self, state: &Self::State) -> bool {
self.0.is_match_state(*state)
}

#[inline]
fn can_match(&self, state: &Self::State) -> bool {
!self.0.is_dead_state(*state)
}

#[inline]
fn accept_eof(&self, state: &StateID) -> Option<StateID> {
if self.0.is_match_state(*state) {
return Some(*state);
}
Some(self.0.next_eoi_state(*state))
}

#[inline]
fn accept(&self, state: &Self::State, byte: u8) -> Self::State {
if self.0.is_match_state(*state) {
return *state;
}
self.0.next_state(*state, byte)
}
}

impl IntersectionFstApplier {
fn new(ranges: Vec<Range>, dfas: Vec<DFA<Vec<u32>>>) -> Self {
let dfas = dfas.into_iter().map(DfaFstAutomaton).collect();
Self { ranges, dfas }
}
}

impl FstApplier for IntersectionFstApplier {
Expand Down Expand Up @@ -86,7 +136,7 @@ impl FstApplier for IntersectionFstApplier {

size += self.dfas.capacity() * size_of::<DFA<Vec<u32>>>();
for dfa in &self.dfas {
size += dfa.memory_usage();
size += dfa.0.memory_usage();
}
size
}
Expand Down Expand Up @@ -119,7 +169,7 @@ impl IntersectionFstApplier {
}
}

Ok(Self { dfas, ranges })
Ok(Self::new(ranges, dfas))
}
}

Expand Down Expand Up @@ -365,18 +415,15 @@ mod tests {

#[test]
fn test_intersection_fst_applier_memory_usage() {
let applier = IntersectionFstApplier {
ranges: vec![],
dfas: vec![],
};
let applier = IntersectionFstApplier::new(vec![], vec![]);

assert_eq!(applier.memory_usage(), 0);

let dfa = DFA::new("^abc$").unwrap();
assert_eq!(dfa.memory_usage(), 320);

let applier = IntersectionFstApplier {
ranges: vec![Range {
let applier = IntersectionFstApplier::new(
vec![Range {
lower: Some(Bound {
value: b"aa".to_vec(),
inclusive: true,
Expand All @@ -386,9 +433,8 @@ mod tests {
inclusive: true,
}),
}],
dfas: vec![dfa],
};

vec![dfa],
);
assert_eq!(
applier.memory_usage(),
size_of::<Range>() + 4 + size_of::<DFA<Vec<u32>>>() + 320
Expand Down