Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor mget method improved readability and efficiency #3061

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,45 +256,32 @@ public Flux<KeyValue<K, V>> mget(K... keys) {

@SuppressWarnings({ "unchecked", "rawtypes" })
public Flux<KeyValue<K, V>> mget(Iterable<K> keys) {

List<K> keyList = LettuceLists.newList(keys);
Map<Integer, List<K>> partitioned = SlotHash.partition(codec, keyList);

if (partitioned.size() < 2) {
return super.mget(keyList);
}

List<Publisher<KeyValue<K, V>>> publishers = new ArrayList<>();

for (Map.Entry<Integer, List<K>> entry : partitioned.entrySet()) {
publishers.add(super.mget(entry.getValue()));
}

Flux<KeyValue<K, V>> fluxes = Flux.mergeSequential(publishers);
List<Publisher<KeyValue<K, V>>> publishers = partitioned.values().stream().map(super::mget)
.collect(Collectors.toList());

Mono<List<KeyValue<K, V>>> map = fluxes.collectList().map(vs -> {

KeyValue<K, V>[] values = new KeyValue[vs.size()];
return Flux.mergeSequential(publishers).collectList().map(results -> {
KeyValue<K, V>[] values = new KeyValue[keyList.size()];
int offset = 0;
for (Map.Entry<Integer, List<K>> entry : partitioned.entrySet()) {

for (List<K> partitionKeys : partitioned.values()) {
for (int i = 0; i < keyList.size(); i++) {

int index = entry.getValue().indexOf(keyList.get(i));
if (index == -1) {
continue;
int index = partitionKeys.indexOf(keyList.get(i));
if (index != -1) {
values[i] = results.get(offset + index);
}

values[i] = vs.get(offset + index);
}

offset += entry.getValue().size();
offset += partitionKeys.size();
}

return Arrays.asList(values);
});

return map.flatMapIterable(keyValues -> keyValues);
}).flatMapMany(Flux::fromIterable);
}

@Override
Expand Down
Loading