aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/queryeval/blueprint/mysearch.h
blob: 4fa534431237dc807dc4bd652b8b87834533ce4e (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/searchlib/queryeval/blueprint.h>
#include <vespa/searchlib/queryeval/multisearch.h>
#include <vespa/searchlib/fef/termfieldmatchdataarray.h>
#include <vespa/searchlib/fef/matchdata.h>
#include <vespa/vespalib/objects/visit.hpp>

namespace search::queryeval {

//-----------------------------------------------------------------------------

class MySearch : public MultiSearch
{
public:
    using TFMDA = search::fef::TermFieldMatchDataArray;
    using MatchData = search::fef::MatchData;

private:
    vespalib::string _tag;
    bool             _isLeaf;
    bool             _isStrict;
    TFMDA            _match;
    MatchData       *_md;

    std::vector<uint32_t> _handles;

protected:
    void doSeek(uint32_t) override {}
    void doUnpack(uint32_t) override {}

public:
    MySearch(const std::string &tag, bool leaf, bool strict)
        : _tag(tag), _isLeaf(leaf), _isStrict(strict),
          _match(), _md(0) {}

    MySearch(const std::string &tag, const TFMDA &tfmda, bool strict)
        : _tag(tag), _isLeaf(true), _isStrict(strict),
          _match(tfmda), _md(0) {}

    MySearch(const std::string &tag, Children children,
             MatchData *md, bool strict)
      : MultiSearch(std::move(children)),
        _tag(tag), _isLeaf(false), _isStrict(strict),
        _match(), _md(md) {}

    MySearch &add(SearchIterator *search) {
        _children.emplace_back(search);
        return *this;
    }

    MySearch &addHandle(uint32_t handle) {
        _handles.push_back(handle);
        return *this;
    }

    bool verifyAndInferImpl(MatchData &md) {
        bool ok = true;
        if (!_isLeaf) {
            ok &= (_md == &md);
        }
        for (size_t i = 0; i < _children.size(); ++i) {
            MySearch *child = dynamic_cast<MySearch *>(_children[i].get());
            ok &= (child != 0);
            if (child != 0) {
                ok &= child->verifyAndInferImpl(md);
            }
        }
        for (size_t i = 0; i < _match.size(); ++i) {
            search::fef::TermFieldMatchData *tfmd = _match[i];
            _handles.push_back(search::fef::IllegalHandle);
            for (search::fef::TermFieldHandle j = 0; j < md.getNumTermFields(); ++j) {
                if (md.resolveTermField(j) == tfmd) {
                    _handles.back() = j;
                    break;
                }
            }
            ok &= (_handles.back() != search::fef::IllegalHandle);
        }
        return ok;
    }

    static bool verifyAndInfer(SearchIterator *search, MatchData &md) {
        MySearch *self = dynamic_cast<MySearch *>(search);
        if (self == 0) {
            return false;
        } else {
            return self->verifyAndInferImpl(md);
        }
    }

    void visitMembers(vespalib::ObjectVisitor &visitor) const override {
        visit(visitor, "_tag",      _tag);
        visit(visitor, "_isLeaf",   _isLeaf);
        visit(visitor, "_isStrict", _isStrict);
        MultiSearch::visitMembers(visitor);
        visit(visitor, "_handles",  _handles);
    }

    ~MySearch() override {}
};

//-----------------------------------------------------------------------------

class MyLeaf : public SimpleLeafBlueprint
{
    using TFMDA = search::fef::TermFieldMatchDataArray;
    bool _got_global_filter;

public:
    SearchIterator::UP
    createLeafSearch(const TFMDA &tfmda, bool strict) const override
    {
        return std::make_unique<MySearch>("leaf", tfmda, strict);
    }

    MyLeaf()
        : SimpleLeafBlueprint(), _got_global_filter(false)
    {}
    MyLeaf(FieldSpecBaseList fields)
        : SimpleLeafBlueprint(std::move(fields)), _got_global_filter(false)
    {}

    MyLeaf &estimate(uint32_t hits, bool empty = false) {
        setEstimate(HitEstimate(hits, empty));
        return *this;
    }

    MyLeaf &cost_tier(uint32_t value) {
        set_cost_tier(value);
        return *this;
    }
    void set_global_filter(const GlobalFilter &, double) override {
        _got_global_filter = true;
    }
    bool got_global_filter() const { return _got_global_filter; }
    // make public
    using LeafBlueprint::set_want_global_filter;

    SearchIteratorUP createFilterSearch(bool strict, FilterConstraint constraint) const override {
        return create_default_filter(strict, constraint);
    }
};

//-----------------------------------------------------------------------------

class MyLeafSpec
{
private:
    FieldSpecBaseList      _fields;
    Blueprint::HitEstimate _estimate;
    uint32_t               _cost_tier;
    bool                   _want_global_filter;

public:
    explicit MyLeafSpec(uint32_t estHits, bool empty = false)
        : _fields(), _estimate(estHits, empty), _cost_tier(0), _want_global_filter(false) {}

    MyLeafSpec &addField(uint32_t fieldId, uint32_t handle) {
        _fields.add(FieldSpecBase(fieldId, handle));
        return *this;
    }
    MyLeafSpec &cost_tier(uint32_t value) {
        assert(value > 0);
        _cost_tier = value;
        return *this;
    }
    MyLeafSpec &want_global_filter() {
        _want_global_filter = true;
        return *this;
    }
    MyLeaf *create() const {
        return create<MyLeaf>();
    }
    template<typename Leaf>
    Leaf *create() const {
        Leaf *leaf = new Leaf(_fields);
        leaf->estimate(_estimate.estHits, _estimate.empty);
        if (_cost_tier > 0) {
            leaf->cost_tier(_cost_tier);
        }
        leaf->set_want_global_filter(_want_global_filter);
        return leaf;
    }
};

//-----------------------------------------------------------------------------

}