aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/searchsummary/docsummary/legacy_query_term_filter.h
blob: dfda568bd9b7834cbac64667c69b68bc299006cd (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "i_query_term_filter.h"
#include <vespa/vespalib/stllike/hash_set.h>

namespace search::docsummary {

class LegacyQueryTermFilter : public IQueryTermFilter
{
public:

    class IndexPrefix
    {
        vespalib::string  _prefix;
    public:
        explicit IndexPrefix(const char *prefix) noexcept;
        ~IndexPrefix();
        bool Match(const char *idxName) const;
        const vespalib::string& get_prefix() const noexcept { return _prefix; }
    };

private:
    using Set = vespalib::hash_set<vespalib::string>;
    std::vector<IndexPrefix>  _legalPrefixes;
    Set                       _legalIndexes;

    bool isLegalIndexPrefix(const char *idxName) const {
        for (auto& prefix : _legalPrefixes ) {
            if (prefix.Match(idxName)) {
                return true;
            }
        }
        return false;
    }

    void addLegalIndexPrefix(const char *prefix) {
        _legalPrefixes.emplace_back(prefix);
    }

    void addLegalIndexName(const char *idxName) {
        _legalIndexes.insert(idxName);
    }
    bool isLegalIndexName(const char *idxName) const;
public:
    LegacyQueryTermFilter();
    LegacyQueryTermFilter(const LegacyQueryTermFilter &) = delete;
    LegacyQueryTermFilter& operator=(const LegacyQueryTermFilter &) = delete;
    ~LegacyQueryTermFilter();


    /**
     * Parse the input string as a ';' separated list of index names and
     * index name prefixes. A '*' following a token in the list denotes
     * that the token is an index name prefix. Add the index names and
     * index name prefixes to the set of legal values.
     *
     * @param spec list of legal index names and prefixes.
     **/
    void addLegalIndexSpec(const char *spec);


    /**
     * Create a spec on the same format as accepted by the @ref
     * addLegalIndexSpec method. Freeing the returned spec is the
     * responsibility of the caller of this method.
     *
     * @return spec defining legal index names and prefixes.
     **/
    vespalib::string getLegalIndexSpec();


    /**
     * Determine wether the given index name is legal by checking it
     * against the current set of legal index names and index name
     * prefixes held by this object.
     *
     * @return true if the given index name is legal.
     **/
    bool use_view(vespalib::stringref idx) const override;
};

}