Skip to content

Commit 63011d6

Browse files
Merge pull request #72 from nihohit/use-map
Use `Value::Map` to aggregate cluster responses.
2 parents 46e8a9d + 718e292 commit 63011d6

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

redis/src/cluster.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,18 +586,15 @@ where
586586
}
587587
Some(ResponsePolicy::Special) | None => {
588588
// This is our assumption - if there's no coherent way to aggregate the responses, we just map each response to the sender, and pass it to the user.
589-
// TODO - once RESP3 is merged, return a map value here.
590589
// TODO - once Value::Error is merged, we can use join_all and report separate errors and also pass successes.
591590
let results = results
592591
.enumerate()
593592
.map(|(index, result)| {
594593
let addr = addresses[index];
595-
result.map(|val| {
596-
Value::Array(vec![Value::BulkString(addr.as_bytes().to_vec()), val])
597-
})
594+
result.map(|val| (Value::BulkString(addr.as_bytes().to_vec()), val))
598595
})
599596
.collect::<RedisResult<Vec<_>>>()?;
600-
Ok(Value::Array(results))
597+
Ok(Value::Map(results))
601598
}
602599
}
603600
}

redis/src/cluster_async/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -737,17 +737,13 @@ where
737737
}
738738
Some(ResponsePolicy::Special) | None => {
739739
// This is our assumption - if there's no coherent way to aggregate the responses, we just map each response to the sender, and pass it to the user.
740-
// TODO - once RESP3 is merged, return a map value here.
741740
// TODO - once Value::Error is merged, we can use join_all and report separate errors and also pass successes.
742741
future::try_join_all(receivers.into_iter().map(|(addr, receiver)| async move {
743742
let result = convert_result(receiver.await)?;
744-
Ok(Value::Array(vec![
745-
Value::BulkString(addr.as_bytes().to_vec()),
746-
result,
747-
]))
743+
Ok((Value::BulkString(addr.as_bytes().to_vec()), result))
748744
}))
749745
.await
750-
.map(Value::Array)
746+
.map(Value::Map)
751747
}
752748
}
753749
}

redis/tests/test_cluster_async.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@ fn test_async_cluster_route_info_to_nodes() {
130130
let cluster = TestClusterContext::new(12, 1);
131131

132132
let split_to_addresses_and_info = |res| -> (Vec<String>, Vec<String>) {
133-
if let Value::Array(values) = res {
133+
if let Value::Map(values) = res {
134134
let mut pairs: Vec<_> = values
135135
.into_iter()
136-
.map(|value| redis::from_redis_value::<(String, String)>(&value).unwrap())
136+
.map(|(key, value)| {
137+
(
138+
redis::from_redis_value::<String>(&key).unwrap(),
139+
redis::from_redis_value::<String>(&value).unwrap(),
140+
)
141+
})
137142
.collect();
138143
pairs.sort_by(|(address1, _), (address2, _)| address1.cmp(address2));
139144
pairs.into_iter().unzip()
@@ -1472,18 +1477,17 @@ fn test_cluster_fan_out_and_return_map_of_results_for_special_response_policy()
14721477
},
14731478
);
14741479

1475-
// TODO once RESP3 is in, return this as a map
14761480
let mut result = runtime
1477-
.block_on(cmd.query_async::<_, Vec<Vec<String>>>(&mut connection))
1481+
.block_on(cmd.query_async::<_, Vec<(String, String)>>(&mut connection))
14781482
.unwrap();
14791483
result.sort();
14801484
assert_eq!(
14811485
result,
14821486
vec![
1483-
vec![format!("{name}:6379"), "latency: 6379".to_string()],
1484-
vec![format!("{name}:6380"), "latency: 6380".to_string()],
1485-
vec![format!("{name}:6381"), "latency: 6381".to_string()],
1486-
vec![format!("{name}:6382"), "latency: 6382".to_string()]
1487+
(format!("{name}:6379"), "latency: 6379".to_string()),
1488+
(format!("{name}:6380"), "latency: 6380".to_string()),
1489+
(format!("{name}:6381"), "latency: 6381".to_string()),
1490+
(format!("{name}:6382"), "latency: 6382".to_string())
14871491
],
14881492
"{result:?}"
14891493
);

0 commit comments

Comments
 (0)