summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/dispatch/InterleavedFillInvoker.java
blob: 644e6f17bdb62b5f103ba9c4a7e09864d14ac1bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch;

import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.search.Result;
import com.yahoo.search.result.Hit;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * InterleavedFillInvoker uses multiple {@link FillInvoker} objects to interface with content
 * nodes in parallel. Operationally it first sends requests with all contained invokers and then
 * collects the results.
 *
 * @author ollivir
 */
public class InterleavedFillInvoker extends FillInvoker {
    private final Map<Integer, FillInvoker> invokers;
    private Map<Integer, Result> expectedFillResults = null;

    public InterleavedFillInvoker(Map<Integer, FillInvoker> invokers) {
        this.invokers = invokers;
    }

    @Override
    protected void sendFillRequest(Result result, String summaryClass) {
        expectedFillResults = new HashMap<>();

        for (Iterator<Hit> it = result.hits().deepIterator(); it.hasNext();) {
            Hit hit = it.next();
            if (hit instanceof FastHit) {
                FastHit fhit = (FastHit) hit;
                Result res = expectedFillResults.computeIfAbsent(fhit.getDistributionKey(), dk -> new Result(result.getQuery()));
                res.hits().add(fhit);
            }
        }
        expectedFillResults.forEach((distKey, partialResult) -> {
            FillInvoker invoker = invokers.get(distKey);
            if (invoker != null) {
                invoker.sendFillRequest(partialResult, summaryClass);
            }
        });
    }

    @Override
    protected void getFillResults(Result result, String summaryClass) {
        if (expectedFillResults == null) {
            return;
        }
        expectedFillResults.forEach((distKey, partialResult) -> {
            FillInvoker invoker = invokers.get(distKey);
            if (invoker != null) {
                invoker.getFillResults(partialResult, summaryClass);
            }
        });
    }

    @Override
    protected void release() {
        if (!invokers.isEmpty()) {
            invokers.values().forEach(FillInvoker::close);
            invokers.clear();
        }
    }
}