Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeling committed Oct 23, 2023
1 parent dcda394 commit 0dc1132
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion kuksa_databroker/databroker/src/broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
55 changes: 37 additions & 18 deletions kuksa_databroker/databroker/src/grpc/kuksa_val_v1/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,37 +375,56 @@ impl proto::val_server::Val for broker::DataBroker {
));
}

let mut entries: HashMap<i32, HashSet<broker::Field>> = HashMap::new();
let mut valid_requests: HashMap<String, (regex::Regex, HashSet<broker::Field>)> =
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;
}

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<i32, HashSet<broker::Field>> = 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;
Expand Down

0 comments on commit 0dc1132

Please sign in to comment.