aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/andnotsearch.h
blob: 393f86316a741eaf00f33cd01fea3175fac9873d (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vector>
#include "multisearch.h"
#include <vespa/searchlib/attribute/attributeiterators.h>
#include <vespa/searchlib/attribute/singlesmallnumericattribute.h>

namespace search::queryeval {

/**
 * A simple implementation of the AndNot search operation.
 **/
class AndNotSearch : public MultiSearch
{
protected:
    void doSeek(uint32_t docid) override;
    void doUnpack(uint32_t docid) override;
    Trinary is_strict() const override { return Trinary::False; }

    /**
     * Create a new AndNot Search with the given children.
     *A AndNot has no strictness assumptions about its children.
     *
     * @param children the search objects we are andnot'ing
     **/
    AndNotSearch(const Children & children) : MultiSearch(children) { }

public:
    // Caller takes ownership of the returned SearchIterator.
    static SearchIterator *create(const Children &children, bool strict);

    std::unique_ptr<BitVector> get_hits(uint32_t begin_id) override;
    void or_hits_into(BitVector &result, uint32_t begin_id) override;
    SearchIterator * positive() const { return getChildren()[0]; }

private:
    bool isAndNot() const override { return true; }
    bool needUnpack(size_t index) const override {
        return index == 0;
    }
};

class AndNotSearchStrictBase : public AndNotSearch
{
protected:
    AndNotSearchStrictBase(const Children & children) : AndNotSearch(children) { }
private:
    Trinary is_strict() const override { return Trinary::True; }
    UP andWith(UP filter, uint32_t estimate) override;
};

/**
 * This is a specialized andnot iterator you get when you have no andnot's in you query and only get the blacklist blueprint.
 * This one is now constructed at getSearch() phase. However this should be better handled in the AndNotBlueprint.
 */
class OptimizedAndNotForBlackListing : public AndNotSearchStrictBase
{
private:
    // This is the actual iterator that should be produced by the documentmetastore in searchcore, but that
    // will probably be changed later on. An ordinary bitvector could be even better as that would open up for more optimizations.
    //typedef FilterAttributeIteratorT<SingleValueSmallNumericAttribute::SingleSearchContext> BlackListIterator;
    typedef AttributeIteratorT<SingleValueSmallNumericAttribute::SingleSearchContext> BlackListIterator;
public:
    OptimizedAndNotForBlackListing(const MultiSearch::Children & children);
    static bool isBlackListIterator(const SearchIterator * iterator);

    uint32_t seekFast(uint32_t docid) {
        return internalSeek<true>(docid);
    }
    void initRange(uint32_t beginid, uint32_t endid) override;
private:
    BlackListIterator * blackList() { return static_cast<BlackListIterator *>(getChildren()[1]); }
    template<bool doSeekOnly>
    uint32_t internalSeek(uint32_t docid) {
        uint32_t curr(docid);
        while (true) {
            if (doSeekOnly) {
                positive()->doSeek(curr);
            } else {
                positive()->seek(curr);
            }
            if ( ! positive()->isAtEnd() ) {
                curr = positive()->getDocId();
                if (! blackList()->seekFast(curr)) {
                    return curr;
                }
                curr++;
            } else {
                return search::endDocId;
            }
        }
    }
    void doSeek(uint32_t docid) override;
    void doUnpack(uint32_t docid) override;
};

}