Skip to content

Commit 38a30dc

Browse files
authored
refactor mget method improved readability and efficiency (#3061)
* refactor mget method improved readability and efficiency * refactor mget method improved readability and efficiency
1 parent ce92e6a commit 38a30dc

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

src/main/java/io/lettuce/core/cluster/RedisAdvancedClusterReactiveCommandsImpl.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -284,45 +284,32 @@ public Flux<KeyValue<K, V>> mget(K... keys) {
284284

285285
@SuppressWarnings({ "unchecked", "rawtypes" })
286286
public Flux<KeyValue<K, V>> mget(Iterable<K> keys) {
287-
288287
List<K> keyList = LettuceLists.newList(keys);
289288
Map<Integer, List<K>> partitioned = SlotHash.partition(codec, keyList);
290289

291290
if (partitioned.size() < 2) {
292291
return super.mget(keyList);
293292
}
294293

295-
List<Publisher<KeyValue<K, V>>> publishers = new ArrayList<>();
296-
297-
for (Map.Entry<Integer, List<K>> entry : partitioned.entrySet()) {
298-
publishers.add(super.mget(entry.getValue()));
299-
}
300-
301-
Flux<KeyValue<K, V>> fluxes = Flux.mergeSequential(publishers);
294+
List<Publisher<KeyValue<K, V>>> publishers = partitioned.values().stream().map(super::mget)
295+
.collect(Collectors.toList());
302296

303-
Mono<List<KeyValue<K, V>>> map = fluxes.collectList().map(vs -> {
304-
305-
KeyValue<K, V>[] values = new KeyValue[vs.size()];
297+
return Flux.mergeSequential(publishers).collectList().map(results -> {
298+
KeyValue<K, V>[] values = new KeyValue[keyList.size()];
306299
int offset = 0;
307-
for (Map.Entry<Integer, List<K>> entry : partitioned.entrySet()) {
308300

301+
for (List<K> partitionKeys : partitioned.values()) {
309302
for (int i = 0; i < keyList.size(); i++) {
310-
311-
int index = entry.getValue().indexOf(keyList.get(i));
312-
if (index == -1) {
313-
continue;
303+
int index = partitionKeys.indexOf(keyList.get(i));
304+
if (index != -1) {
305+
values[i] = results.get(offset + index);
314306
}
315-
316-
values[i] = vs.get(offset + index);
317307
}
318-
319-
offset += entry.getValue().size();
308+
offset += partitionKeys.size();
320309
}
321310

322311
return Arrays.asList(values);
323-
});
324-
325-
return map.flatMapIterable(keyValues -> keyValues);
312+
}).flatMapMany(Flux::fromIterable);
326313
}
327314

328315
@Override

0 commit comments

Comments
 (0)