summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/queryeval/blueprint/mysearch.h
blob: 82ef58147c3cb1a842fe88d608c99d9b312c61c8 (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
// Copyright 2017 Yahoo Holdings. 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 SearchIterator
{
public:
    typedef MultiSearch::Children      Children;
    typedef std::vector<SearchIterator::UP>      MyChildren;
    typedef search::fef::TermFieldMatchDataArray TFMDA;
    typedef search::fef::MatchData               MatchData;

private:
    vespalib::string _tag;
    bool             _isLeaf;
    bool             _isStrict;
    MyChildren       _children;
    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), _children(),
          _match(), _md(0) {}

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

    MySearch(const std::string &tag, const Children &children,
             MatchData *md, bool strict)
        : _tag(tag), _isLeaf(false), _isStrict(strict), _children(),
          _match(), _md(md) {
        for (size_t i(0); i < children.size(); i++) {
            _children.emplace_back(children[i]);
        }
    }

    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);
        visit(visitor, "_children", _children);
        visit(visitor, "_handles",  _handles);
    }

    ~MySearch() override {}
};

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

class MyLeaf : public SimpleLeafBlueprint
{
    typedef search::fef::TermFieldMatchDataArray TFMDA;

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

    MyLeaf(const FieldSpecBaseList &fields)
        : SimpleLeafBlueprint(fields)
    {}

    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;
    }
};

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

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

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

    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;
    }
    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);
        }
        return leaf;
    }
};

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

}