Skip to content

Commit

Permalink
refactor: cached filters are now passed to the SearchContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcdostone committed Feb 10, 2025
1 parent 476687e commit c09a835
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
10 changes: 5 additions & 5 deletions crates/app/src/search/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ use lib::{

use super::{Search, SearchContext};

// This is evil, TODO context
pub static CACHED_FILTERS: LazyLock<Mutex<HashMap<String, Plugin>>> =
LazyLock::new(|| HashMap::new().into());

pub const MATCHES_FUNCTION_NAME: &str = "matches";
pub const PARSE_PARAMETERS_FUNCTION_NAME: &str = "parse_parameters";

/// FILTERS are lazy loaded and cached in memory.
pub(crate) static CACHED_FILTERS: LazyLock<Mutex<HashMap<String, Plugin>>> =
LazyLock::new(|| HashMap::new().into());

impl Search for Filter {
fn matches(&self, context: &SearchContext) -> bool {
let mut filters = CACHED_FILTERS.lock().unwrap();
let mut filters = context.filters.lock().unwrap();
let plugin = &mut filters.get_mut(&self.name).unwrap();
let input = FilterInput {
record: context.record.clone(),
Expand Down
11 changes: 10 additions & 1 deletion crates/app/src/search/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! Module implementing the search logic
use std::path::{Path, PathBuf};
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::{LazyLock, Mutex},
};

use extism::{Manifest, Plugin, Wasm};
use filter::{CACHED_FILTERS, PARSE_PARAMETERS_FUNCTION_NAME};
Expand Down Expand Up @@ -34,14 +38,19 @@ pub trait Search {
/// Struct that holds the context of the search.
/// It contains the record that is being searched and the loaded search filters.
pub struct SearchContext<'a> {
/// The record that is being searched.
pub record: &'a KafkaRecord,
/// The search filters that are already loaded in memory.
pub filters: &'a LazyLock<Mutex<HashMap<String, Plugin>>>,
/// The directory containing the search filters
pub filters_directory: PathBuf,
}

impl SearchContext<'_> {
pub fn new<'a>(record: &'a KafkaRecord, filters_directory: &'a Path) -> SearchContext<'a> {
SearchContext {
record,
filters: &CACHED_FILTERS,
filters_directory: filters_directory.to_path_buf(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/command/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use indexmap::IndexMap;
#[command(author, version, about = "A terminal user interface to navigate Kafka topics and search for Kafka records.", name = APPLICATION_NAME, bin_name = APPLICATION_NAME, display_name = APPLICATION_NAME, long_about = None, propagate_version = true, args_conflicts_with_subcommands = true)]
pub struct Cli<T>
where
T: Cluster
T: Cluster,
{
#[command(subcommand)]
pub subcommands: Option<UtilityCommands>,
Expand Down
6 changes: 4 additions & 2 deletions crates/command/src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{fmt::Debug, fmt::Display, str::FromStr};

/// Every cluster identifier must implement this trait to be used in the command line.
pub trait Cluster: Debug + Clone + Sync + Send + 'static + FromStr + Default + Display + FromStr<Err: Display>
{}
pub trait Cluster:
Debug + Clone + Sync + Send + 'static + FromStr + Default + Display + FromStr<Err: Display>
{
}

impl<T> Cluster for T
where
Expand Down

0 comments on commit c09a835

Please sign in to comment.