aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/debug/SearchChainTextRepresentation.java
blob: 2515281ca96585cb643dbbd370921d8d2fad021f (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.debug;

import com.yahoo.component.chain.Chain;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.ForkingSearcher;
import com.yahoo.search.searchchain.SearchChain;
import com.yahoo.search.searchchain.SearchChainRegistry;

import java.util.Collection;

/**
 * Text representation of a given search chain intended for debugging purposes.
 *
 * @author tonytv
 */
public class SearchChainTextRepresentation {

    private final SearchChainRegistry searchChainRegistry;

    private static class Block {
        private static final String openBlock = " {";
        private static final char closeBlock = '}';
        private final IndentStringBuilder str;
        private final int level;

        Block(IndentStringBuilder str) {
            this.str = str;
            level = str.append(openBlock).newlineAndIndent();
        }

        void close() {
            str.resetIndentLevel(level);
            str.append(closeBlock).newline();
        }
    }

    private final String textRepresentation;

    private void outputChain(IndentStringBuilder str, Chain<Searcher> chain) {
        if (chain == null) {
            str.append(" [Unresolved Searchchain]");
        } else {
            str.append(chain.getId()).append(" [Searchchain] ");
            Block block = new Block(str);

            for (Searcher searcher : chain.components())
                outputSearcher(str, searcher);

            block.close();
        }
    }

    private void outputSearcher(IndentStringBuilder str, Searcher searcher) {
        str.append(searcher.getId()).append(" [Searcher]");
        if ( ! (searcher instanceof ForkingSearcher) ) {
            str.newline();
            return;
        }
        Collection<ForkingSearcher.CommentedSearchChain> chains =
                ((ForkingSearcher)searcher).getSearchChainsForwarded(searchChainRegistry);
        if (chains.isEmpty()) {
            str.newline();
            return;
        }
        Block block = new Block(str);
        for (ForkingSearcher.CommentedSearchChain chain : chains) {
            if (chain.comment != null)
                str.append(chain.comment).newline();
            outputChain(str, chain.searchChain);
        }
        block.close();
    }

    @Override
    public String toString() {
        return textRepresentation;
    }

    public SearchChainTextRepresentation(SearchChain searchChain, SearchChainRegistry searchChainRegistry) {
        this.searchChainRegistry = searchChainRegistry;

        IndentStringBuilder stringBuilder = new IndentStringBuilder();
        outputChain(stringBuilder, searchChain);
        textRepresentation = stringBuilder.toString();
    }

}