aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/searchchain/SearchChainRegistry.java
blob: 3fce9610eb3971892cb85ce488fe05c26fde1310 (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
102
103
104
105
106
107
108
109
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.searchchain;

import com.yahoo.component.AbstractComponent;
import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.chain.Chain;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.processing.execution.chain.ChainRegistry;
import com.yahoo.search.Searcher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Contains a reference to all currently known search chains.
 * Searchers can be fetched from this from multiple threads.
 * <p>
 * A registry can exist in two states:
 * <ul>
 * <li>not frozen - in this state it can be edited freely by calling {@link #register}
 * <li>frozen - in this state any attempt at modification throws an IlegalStateException
 * </ul>
 * Registries start in the first state, moves to the second on calling freeze and stays in that
 * state for the rest of their lifetime.
 *
 * @author bratseth
 */
public class SearchChainRegistry extends ChainRegistry<Searcher> {

    private final SearcherRegistry searcherRegistry;

    @Override
    public void freeze() {
        super.freeze();
        getSearcherRegistry().freeze();
    }

    public SearchChainRegistry() {
        searcherRegistry = new SearcherRegistry();
        searcherRegistry.freeze();
    }

    public SearchChainRegistry(ComponentRegistry<? extends AbstractComponent> allComponentRegistry) {
        this.searcherRegistry = setupSearcherRegistry(allComponentRegistry);
    }

    public void register(Chain<Searcher> component) {
        super.register(component.getId(), component);
    }

    public Chain<Searcher> unregister(Chain<Searcher> component) {
        return super.unregister(component.getId());
    }

    private SearcherRegistry setupSearcherRegistry(ComponentRegistry<? extends AbstractComponent> allComponents) {
        SearcherRegistry registry = new SearcherRegistry();
        for (AbstractComponent component : allComponents.allComponents()) {
            if (component instanceof Searcher) {
                registry.register((Searcher) component);
            }
        }
        //just freeze this right away
        registry.freeze();
        return registry;
    }

    public SearcherRegistry getSearcherRegistry() {
        return searcherRegistry;
    }

    @Override
    public SearchChain getComponent(ComponentId id) {
        Chain<Searcher> chain = super.getComponent(id);
        return asSearchChain(chain);
    }

    @Override
    public SearchChain getComponent(ComponentSpecification specification) {
        return asSearchChain(super.getComponent(specification));
    }

    public final Chain<Searcher> getChain(String componentSpecification) {
        return super.getComponent(new ComponentSpecification(componentSpecification));
    }

    public final Chain<Searcher> getChain(ComponentId id) {
         return super.getComponent(id);
     }


    @Override
    public SearchChain getComponent(String componentSpecification) {
        return getComponent(new ComponentSpecification(componentSpecification));
    }

    private SearchChain asSearchChain(Chain<Searcher> chain) {
        if (chain == null) {
            return null;
        } else if (chain instanceof SearchChain) {
            return (SearchChain) chain;
        } else {
            return new SearchChain(chain);
        }
    }


}