Skip to content

Commit

Permalink
optimise the check
Browse files Browse the repository at this point in the history
Signed-off-by: Shoham Elias <[email protected]>
  • Loading branch information
shohamazon committed Aug 11, 2024
1 parent d5acd12 commit 0301a96
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,24 +584,28 @@ pub(crate) fn convert_to_expected_type(
}

Value::Array(outer_array) if matches!(outer_array[0], Value::Array(_) | Value::Map(_) ) => {
let first_inner_item = &outer_array[0];
let mut converted_map = convert_to_expected_type(first_inner_item.clone(), Some(ExpectedReturnType::FunctionStatsReturnType))?;

for inner_item in outer_array {
if let Value::Map(map) = converted_map.clone() {
if let Some((key, running_script_value)) = map.first() {
if key == &Value::BulkString(b"running_script".into()) && running_script_value != &Value::Nil {
// we have found a response with an active running script
// the server can only run a single script / function at a time
// we can return this nodes response
break;
}
}
// Helper function to check if an item contains a non-null `running_script`
fn contains_running_script(value: &Value) -> bool {
match value {
Value::Array(inner_array) => inner_array.get(1) != Some(&Value::Nil),
Value::Map(inner_map) => inner_map.get(0).map(|(_, v)| v) != Some(&Value::Nil),
_ => false,
}
}

let mut converted_map: Option<Value> = None;

for inner_item in &outer_array {
if contains_running_script(inner_item) {
converted_map = Some(convert_to_expected_type(inner_item.clone(), Some(ExpectedReturnType::FunctionStatsReturnType))?);
// we have found a response with an active running script
// the server can only run a single script / function at a time
break; // We can return this node's response
}
converted_map = convert_to_expected_type(inner_item.clone(), Some(ExpectedReturnType::FunctionStatsReturnType))?;
}

Ok(converted_map)
// Return the converted map if found, otherwise convert the first item in the array
Ok(converted_map.unwrap_or_else(|| convert_to_expected_type(outer_array[0].clone(), Some(ExpectedReturnType::FunctionStatsReturnType)).unwrap()))
},
Value::Array(mut array) if array.len() == 4 => {
let mut result: Vec<(Value, Value)> = Vec::with_capacity(2);
Expand Down Expand Up @@ -2469,11 +2473,6 @@ mod tests {

#[test]
fn convert_function_stats_standalone_response() {
assert!(matches!(
expected_type_for_cmd(redis::cmd("FUNCTION").arg("STATS")),
Some(ExpectedReturnType::FunctionStatsReturnType)
));

let resp2_response = Value::Array(vec![
Value::Array(vec![
Value::BulkString(b"running_script".into()),
Expand Down

0 comments on commit 0301a96

Please sign in to comment.