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

#pragma once

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

namespace search::docsummary {

class IDocsumEnvironment;

class KeywordExtractor
{
public:

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

private:
    typedef vespalib::hash_set<vespalib::string> Set;
    IDocsumEnvironment       *_env;
    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:
    explicit KeywordExtractor(IDocsumEnvironment * env);
    KeywordExtractor(const KeywordExtractor &) = delete;
    KeywordExtractor& operator=(const KeywordExtractor &) = delete;
    ~KeywordExtractor();


    /**
     * 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 IsLegalIndex(vespalib::stringref idx) const;
};

}