Skip to content

Commit

Permalink
clippy, fmt, test typo
Browse files Browse the repository at this point in the history
Added tests for naive filter
Fixed the NaiveFilterOperand from_str constructor
  • Loading branch information
Nekit2217 committed Jul 20, 2024
1 parent 164de42 commit 2c2fbea
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 131 deletions.
21 changes: 10 additions & 11 deletions benches/filters.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs::File;
use std::io::{self, BufRead, BufReader};

use criterion::{black_box, Criterion, criterion_group, criterion_main};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use serde_json::Value;

use kafka_delta_ingest::{Filter, FilterEngine, FilterError, FilterFactory};
Expand All @@ -25,33 +25,32 @@ fn filtering(filter: &Box<dyn Filter>, values: &Vec<Value>) {
for v in values.into_iter() {
match filter.filter(v) {
Ok(_) => {}
Err(e) => {
match e {
FilterError::FilterSkipMessage => {}
_ => panic!("something wrong"),
}
}
Err(e) => match e {
FilterError::FilterSkipMessage => {}
_ => panic!("something wrong"),
},
};
}
}

fn naive_filter_benchmark(c: &mut Criterion) {
let values = read_json_file(SOURCE_PATH).unwrap();
let filter = FilterFactory::try_build(&FilterEngine::Naive, &vec!("method=='GET'".to_string())).expect("wrong");
let filter = FilterFactory::try_build(&FilterEngine::Naive, &vec!["method=='GET'".to_string()])
.expect("wrong");
c.bench_function("naive_filter_benchmark", |b| {
b.iter(|| filtering(&filter, black_box(&values)))
});
}


fn jmespath_filter_benchmark(c: &mut Criterion) {
let values = read_json_file(SOURCE_PATH).unwrap();
let filter = FilterFactory::try_build(&FilterEngine::Jmespath, &vec!("method=='GET'".to_string())).expect("wrong");
let filter =
FilterFactory::try_build(&FilterEngine::Jmespath, &vec!["method=='GET'".to_string()])
.expect("wrong");
c.bench_function("jmespath_filter_benchmark", |b| {
b.iter(|| filtering(&filter, black_box(&values)))
});
}


criterion_group!(benches, naive_filter_benchmark, jmespath_filter_benchmark);
criterion_main!(benches);
9 changes: 4 additions & 5 deletions src/filters/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use jmespatch::JmespathError;
use serde_json;

use crate::filters::naive_filter::error::NaiveFilterError;

Expand Down Expand Up @@ -37,11 +36,11 @@ pub enum FilterError {
source: serde_json::Error,
},

/// Not found filter engine
#[error("Not found filter engine: {reason}")]
/// Filter engine not found
#[error("Filter engine not found: {name}")]
NotFound {
///
reason: String
/// Wrong name
name: String,
},

/// Error returned for skipping message
Expand Down
6 changes: 4 additions & 2 deletions src/filters/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use crate::filters::FilterError;
/// Trait for implementing a filter mechanism
pub trait Filter: Send {
/// Constructor
fn from_filters(filters: &Vec<String>) -> Result<Self, FilterError> where Self: Sized;
fn from_filters(filters: &[String]) -> Result<Self, FilterError>
where
Self: Sized;

/// A function that filters a message. If any of the filters fail, it throws an error;
/// if all filters pass, it returns nothing.
fn filter(&self, message: &Value) -> Result<(), FilterError>;
}
}
27 changes: 12 additions & 15 deletions src/filters/filter_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,26 @@ pub enum FilterEngine {
/// Filter for simple comparisons that works a little faster
Naive,
/// Filter for complex comparisons
Jmespath
Jmespath,
}

/// Factory for creating and managing filters
pub struct FilterFactory {}
impl FilterFactory {
/// Factory for creating filter instances
pub fn try_build(
filter_engine: &FilterEngine, filters: &Vec<String>
filter_engine: &FilterEngine,
filters: &[String],
) -> Result<Box<dyn Filter>, FilterError> {
match filter_engine {
FilterEngine::Naive => {
match NaiveFilter::from_filters(filters) {
Ok(f) => {Ok(Box::new(f))}
Err(e) => {Err(e)}
}
}
FilterEngine::Jmespath => {
match JmespathFilter::from_filters(filters) {
Ok(f) => {Ok(Box::new(f))}
Err(e) => {Err(e)}
}
}
FilterEngine::Naive => match NaiveFilter::from_filters(filters) {
Ok(f) => Ok(Box::new(f)),
Err(e) => Err(e),
},
FilterEngine::Jmespath => match JmespathFilter::from_filters(filters) {
Ok(f) => Ok(Box::new(f)),
Err(e) => Err(e),
},
}
}
}
}
4 changes: 2 additions & 2 deletions src/filters/jmespath_filter/custom_functions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::convert::TryFrom;
use std::sync::Arc;

use jmespatch::{Context, ErrorReason, JmespathError, Rcvar, Variable};
use jmespatch::functions::{ArgumentType, CustomFunction, Signature};
use jmespatch::{Context, ErrorReason, JmespathError, Rcvar, Variable};

/// Custom function to compare two string values in a case-insensitive manner
fn eq_ignore_case(args: &[Rcvar], context: &mut Context) -> Result<Rcvar, JmespathError> {
Expand Down Expand Up @@ -38,4 +38,4 @@ pub fn create_eq_ignore_case_function() -> CustomFunction {
Signature::new(vec![ArgumentType::String, ArgumentType::String], None),
Box::new(eq_ignore_case),
)
}
}
46 changes: 25 additions & 21 deletions src/filters/jmespath_filter/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use jmespatch::{Expression, Runtime};
use serde_json::Value;

use crate::filters::filter::Filter;
use crate::filters::FilterError;
use crate::filters::jmespath_filter::custom_functions::create_eq_ignore_case_function;
use crate::filters::FilterError;

lazy_static! {
static ref FILTER_RUNTIME: Runtime = {
Expand All @@ -14,7 +14,6 @@ lazy_static! {
};
}


/// Implementation of the [Filter] trait for complex checks, such as checking for
/// the presence of a key in an object or comparing the second value in an array
/// or check array length.
Expand All @@ -24,7 +23,7 @@ pub struct JmespathFilter {
}

impl Filter for JmespathFilter {
fn from_filters(filters: &Vec<String>) -> Result<Self, FilterError> {
fn from_filters(filters: &[String]) -> Result<Self, FilterError> {
let filters = filters
.iter()
.map(|f| {
Expand All @@ -48,18 +47,17 @@ impl Filter for JmespathFilter {
match filter.search(message) {
Err(e) => return Err(FilterError::JmespathError { source: e }),
Ok(v) => {
if v.as_boolean().unwrap() == false {
if !v.as_boolean().unwrap() {
return Err(FilterError::FilterSkipMessage);
}
}
};
}

return Ok(());
Ok(())
}
}


#[cfg(test)]
mod tests {
use std::fs::File;
Expand Down Expand Up @@ -89,22 +87,22 @@ mod tests {

for v in values.into_iter() {
match filter.filter(&v) {
Ok(_) => { passed_messages += 1 }
Err(FilterError::FilterSkipMessage) => {
filtered_messages += 1
}
Err(e) => panic!("{}", e)
Ok(_) => passed_messages += 1,
Err(FilterError::FilterSkipMessage) => filtered_messages += 1,
Err(e) => panic!("{}", e),
}
}

return (passed_messages, filtered_messages)
return (passed_messages, filtered_messages);
}
#[test]
fn equal() {
let values = read_json_file(SOURCE_PATH).unwrap();
let filter = match JmespathFilter::from_filters(&vec!["session_id=='a8a3d0e3-7b4e-4f17-b264-76cb792bdb96'".to_string()]) {
let filter = match JmespathFilter::from_filters(&vec![
"session_id=='a8a3d0e3-7b4e-4f17-b264-76cb792bdb96'".to_string(),
]) {
Ok(f) => f,
Err(e) => panic!("{}", e)
Err(e) => panic!("{}", e),
};

let (passed_messages, filtered_messages) = run_filter(&filter, &values);
Expand All @@ -115,9 +113,11 @@ mod tests {
#[test]
fn eq_ignore_case() {
let values = read_json_file(SOURCE_PATH).unwrap();
let filter = match JmespathFilter::from_filters(&vec!["eq_ignore_case(method, 'get')".to_string()]) {
let filter = match JmespathFilter::from_filters(&vec![
"eq_ignore_case(method, 'get')".to_string()
]) {
Ok(f) => f,
Err(e) => panic!("{}", e)
Err(e) => panic!("{}", e),
};

let (passed_messages, filtered_messages) = run_filter(&filter, &values);
Expand All @@ -129,9 +129,11 @@ mod tests {
#[test]
fn or_condition() {
let values = read_json_file(SOURCE_PATH).unwrap();
let filter = match JmespathFilter::from_filters(&vec!["(status == `404` || method == 'GET')".to_string()]) {
let filter = match JmespathFilter::from_filters(&vec![
"(status == `404` || method == 'GET')".to_string(),
]) {
Ok(f) => f,
Err(e) => panic!("{}", e)
Err(e) => panic!("{}", e),
};

let (passed_messages, filtered_messages) = run_filter(&filter, &values);
Expand All @@ -158,14 +160,16 @@ mod tests {
.iter()
.map(|line| serde_json::from_str::<Value>(&line).unwrap())
.collect();
let filter = match JmespathFilter::from_filters(&vec!["!contains(keys(@), 'status') || (status == '1' && age >= `26`)".to_string()]) {
let filter = match JmespathFilter::from_filters(&vec![
"!contains(keys(@), 'status') || (status == '1' && age >= `26`)".to_string(),
]) {
Ok(f) => f,
Err(e) => panic!("{}", e)
Err(e) => panic!("{}", e),
};

let (passed_messages, filtered_messages) = run_filter(&filter, &values);

assert_eq!(7, passed_messages);
assert_eq!(3, filtered_messages);
}
}
}
2 changes: 1 addition & 1 deletion src/filters/jmespath_filter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub(super) mod filter;
mod custom_functions;
pub(super) mod filter;
5 changes: 2 additions & 3 deletions src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ pub use filter_factory::{FilterEngine, FilterFactory};
pub(crate) use jmespath_filter::filter::JmespathFilter;
pub(crate) use naive_filter::filter::NaiveFilter;

mod naive_filter;
mod jmespath_filter;
mod error;
mod filter;
mod filter_factory;

mod jmespath_filter;
mod naive_filter;
4 changes: 1 addition & 3 deletions src/filters/naive_filter/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use serde_json;

#[derive(thiserror::Error, Debug)]
pub enum NaiveFilterError {
/// Error from [`serde_json`]
Expand All @@ -9,7 +7,7 @@ pub enum NaiveFilterError {
#[from]
source: serde_json::Error,
},

/// Error occurs when trying to execute a filter
#[error("NaiveFilter execution error: {reason}")]
RuntimeError { reason: String },
Expand Down
Loading

0 comments on commit 2c2fbea

Please sign in to comment.