aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/orsearch.cpp
blob: fe54f5074a058c59ffe41309be29d69ed4e10adf (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "orsearch.h"
#include "orlikesearch.h"
#include "termwise_helper.h"
#include <vespa/searchlib/common/bitvector.h>

namespace search::queryeval {

namespace {

class FullUnpack
{
public:
    void unpack(uint32_t docid, MultiSearch & search) {
        const MultiSearch::Children & children(search.getChildren());
        size_t sz(children.size());
        for (size_t i(0); i < sz; ++i) {
            if (__builtin_expect(children[i]->getDocId() < docid, false)) {
                children[i]->doSeek(docid);
            }
            if (__builtin_expect(children[i]->getDocId() == docid, false)) {
                children[i]->doUnpack(docid);
            }
        }
    }
    void onRemove(size_t index) { (void) index; }
    void onInsert(size_t index) { (void) index; }
    bool needUnpack(size_t index) const { (void) index; return true; }
};

class SelectiveUnpack
{
public:
    SelectiveUnpack(const UnpackInfo & unpackInfo) :
        _unpackInfo(unpackInfo)
    { }
    void unpack(uint32_t docid, const MultiSearch & search) {
        auto &children = search.getChildren();
        _unpackInfo.each([&children,docid](size_t i) {
                    SearchIterator &child = *children[i];
                    if (__builtin_expect(child.getDocId() < docid, false)) {
                        child.doSeek(docid);
                    }
                    if (__builtin_expect(child.getDocId() == docid, false)) {
                        child.doUnpack(docid);
                    }
                }, children.size());
    }
    void onRemove(size_t index) {
        _unpackInfo.remove(index);
    }
    void onInsert(size_t index) {
        _unpackInfo.insert(index);
    }
    bool needUnpack(size_t index) const {
        return _unpackInfo.needUnpack(index);
    }
private:
    UnpackInfo _unpackInfo;
};

}

BitVector::UP
OrSearch::get_hits(uint32_t begin_id) {
    return TermwiseHelper::orChildren(getChildren().begin(), getChildren().end(), begin_id);
}

void
OrSearch::and_hits_into(BitVector &result, uint32_t begin_id) {
    result.andWith(*get_hits(begin_id));
}
    
void
OrSearch::or_hits_into(BitVector &result, uint32_t begin_id)
{
    TermwiseHelper::orChildren(result, getChildren().begin(), getChildren().end(), begin_id);
}

SearchIterator::UP
OrSearch::create(ChildrenIterators children, bool strict) {
    UnpackInfo unpackInfo;
    unpackInfo.forceAll();
    return create(std::move(children), strict, unpackInfo);
}

SearchIterator::UP
OrSearch::create(ChildrenIterators children, bool strict, const UnpackInfo & unpackInfo) {
    if (strict) {
        if (unpackInfo.unpackAll()) {
            using MyOr = OrLikeSearch<true, FullUnpack>;
            return std::make_unique<MyOr>(std::move(children), FullUnpack());
        } else if(unpackInfo.empty()) {
            using MyOr = OrLikeSearch<true, NoUnpack>;
            return std::make_unique<MyOr>(std::move(children), NoUnpack());
        } else {
            using MyOr = OrLikeSearch<true, SelectiveUnpack>;
            return std::make_unique<MyOr>(std::move(children), SelectiveUnpack(unpackInfo));
        }
    } else {
        if (unpackInfo.unpackAll()) {
            using MyOr = OrLikeSearch<false, FullUnpack>;
            return std::make_unique<MyOr>(std::move(children), FullUnpack());
        } else if(unpackInfo.empty()) {
            using MyOr = OrLikeSearch<false, NoUnpack>;
            return std::make_unique<MyOr>(std::move(children), NoUnpack());
        } else {
            using MyOr = OrLikeSearch<false, SelectiveUnpack>;
            return std::make_unique<MyOr>(std::move(children), SelectiveUnpack(unpackInfo));
        }
    }
}

}