From 0dc1132a2abd5c127b30b9bd00627a8ebfe0e3bf Mon Sep 17 00:00:00 2001 From: Rafael RL Date: Mon, 23 Oct 2023 12:19:53 +0200 Subject: [PATCH] Optimization --- kuksa_databroker/databroker/src/broker.rs | 2 +- .../databroker/src/grpc/kuksa_val_v1/val.rs | 55 +++++++++++++------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/kuksa_databroker/databroker/src/broker.rs b/kuksa_databroker/databroker/src/broker.rs index a9fbceeb..4c96721c 100644 --- a/kuksa_databroker/databroker/src/broker.rs +++ b/kuksa_databroker/databroker/src/broker.rs @@ -79,7 +79,7 @@ pub struct Entry { pub metadata: Metadata, } -#[derive(Debug, PartialEq, Eq, Hash)] +#[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum Field { Datapoint, ActuatorTarget, diff --git a/kuksa_databroker/databroker/src/grpc/kuksa_val_v1/val.rs b/kuksa_databroker/databroker/src/grpc/kuksa_val_v1/val.rs index dcbc2f7f..67cfc676 100644 --- a/kuksa_databroker/databroker/src/grpc/kuksa_val_v1/val.rs +++ b/kuksa_databroker/databroker/src/grpc/kuksa_val_v1/val.rs @@ -375,9 +375,10 @@ impl proto::val_server::Val for broker::DataBroker { )); } - let mut entries: HashMap> = HashMap::new(); + let mut valid_requests: HashMap)> = + HashMap::new(); - for entry in request.entries { + for entry in &request.entries { if entry.path.contains('*') && !glob::is_valid_pattern(&entry.path) { tonic::Status::new(tonic::Code::InvalidArgument, "Invalid Pattern Argument"); continue; @@ -385,27 +386,45 @@ impl proto::val_server::Val for broker::DataBroker { let regex_exp = glob::to_regex(&entry.path); if let Ok(regex) = regex_exp { + let mut fields = HashSet::new(); + for id in &entry.fields { + if let Some(field) = proto::Field::from_i32(*id) { + match field { + proto::Field::Value => { + fields.insert(broker::Field::Datapoint); + } + proto::Field::ActuatorTarget => { + fields.insert(broker::Field::ActuatorTarget); + } + _ => { + // Just ignore other fields for now + } + } + }; + } + valid_requests.insert(entry.path.clone(), (regex, fields)); + } + } + + let mut entries: HashMap> = HashMap::new(); + + if !valid_requests.is_empty() { + for (_, (regex, fields_set)) in valid_requests { broker .for_each_entry(|for_each_entry| { let entry_path = &for_each_entry.metadata().path; if regex.is_match(entry_path) { - let mut fields = HashSet::new(); - for id in &entry.fields { - if let Some(field) = proto::Field::from_i32(*id) { - match field { - proto::Field::Value => { - fields.insert(broker::Field::Datapoint); - } - proto::Field::ActuatorTarget => { - fields.insert(broker::Field::ActuatorTarget); - } - _ => { - // Just ignore other fields for now - } - } - }; + match entries.get(&for_each_entry.metadata().id) { + Some(fields) => { + let joined_set: HashSet<_> = + fields.union(&fields_set).cloned().collect(); + entries.insert(for_each_entry.metadata().id, joined_set); + } + None => { + entries + .insert(for_each_entry.metadata().id, fields_set.clone()); + } } - entries.insert(for_each_entry.metadata().id, fields); } }) .await;