summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/search/DispatchGroup.java
blob: 384f77737c1fcf59f74757e8c1b85cdce6948265 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.search;

import java.util.Map;
import java.util.TreeMap;

/**
 * Class representing a group of @link{SearchInterface} nodes and a set of @link{Dispatch} nodes.
 *
 * Each @link{Dispatch} has a reference to an instance of this class and use it when producing config.
 *
 * @author baldersheim
 */
public class DispatchGroup {

    private final Map<Integer, Map<Integer, SearchInterface>> searchers = new TreeMap<>();

    final private IndexedSearchCluster sc;

    public DispatchGroup(IndexedSearchCluster sc) {
        this.sc = sc;
    }

    DispatchGroup addSearcher(SearchInterface search) {
        Map<Integer, SearchInterface> rows = searchers.get(search.getNodeSpec().partitionId());
        if (rows == null) {
            rows = new TreeMap<>();
            rows.put(search.getNodeSpec().groupIndex(), search);
            searchers.put(search.getNodeSpec().partitionId(), rows);
        } else {
            if (rows.containsKey(search.getNodeSpec().groupIndex())) {
                throw new IllegalArgumentException("Already contains a search node with row id '" + search.getNodeSpec().groupIndex() + "'");
            }
            rows.put(search.getNodeSpec().groupIndex(), search);
        }
        return this;
    }

    public Iterable getSearchersIterable() {
        return new Iterable(searchers);
    }

    public int getRowBits() {
        return sc.getRowBits();
    }

    public int getNumPartitions() {
        return searchers.size();
    }

    public boolean useFixedRowInDispatch() {
        return sc.useFixedRowInDispatch();
    }

    public int getSearchableCopies() { return sc.getSearchableCopies(); }

    public int getMaxNodesDownPerFixedRow() {
        return sc.getMaxNodesDownPerFixedRow();
    }

    static class Iterator implements java.util.Iterator<SearchInterface> {
        private java.util.Iterator<Map<Integer, SearchInterface>> it1;
        private java.util.Iterator<SearchInterface> it2;
        Iterator(Map<Integer, Map<Integer, SearchInterface> > s) {
            it1 = s.values().iterator();
            if (it1.hasNext()) {
                it2 = it1.next().values().iterator();
            }
        }
        @Override
        public boolean hasNext() {
            if (it2 == null) {
                return false;
            }
            while (!it2.hasNext() && it1.hasNext()) {
                it2 = it1.next().values().iterator();
            }
            return it2.hasNext();
        }

        @Override
        public SearchInterface next() {
            return it2.next();
        }

        @Override
        public void remove() {
            throw new IllegalStateException("'remove' not implemented");
        }
    }

    public static class Iterable implements java.lang.Iterable<SearchInterface> {
        final Map<Integer, Map<Integer, SearchInterface> > searchers;
        Iterable(Map<Integer, Map<Integer, SearchInterface> > searchers) { this.searchers = searchers; }
        @Override
        public java.util.Iterator<SearchInterface> iterator() {
            return new Iterator(searchers);
        }
    }

}