aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/parsequery/stackdumpiterator.h
blob: bf6e3abf1eadb59221a7a22f27d1ba4c99a9dc83 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "parse.h"
#include <vespa/vespalib/stllike/string.h>
#include <memory>

namespace search::query {

class PredicateQueryTerm;
class TermVector;

}

namespace search {
/**
 * An iterator to be used on a buffer that is a stack dump
 * of a SimpleQueryStack.
 */
class SimpleQueryStackDumpIterator
{
private:
    /** Pointer to the start of the input buffer */
    const char *_buf;
    /** Pointer to just past the input buffer */
    const char *_bufEnd;
    /** Pointer to the position of the current item in the buffer */
    uint32_t    _currPos;
    /** Pointer to after the current item */
    uint32_t    _currEnd;
    /** The type of the current item */
    ParseItem::ItemType _currType;
    /** flags of the current item **/
    uint8_t _currFlags;
    /** Rank weight of current item **/
    query::Weight _currWeight;
    /** unique id of the current item **/
    uint32_t _currUniqueId;
    /** The arity of the current item */
    uint32_t _currArity;
    /** The index name (field name) in the current item */
    vespalib::stringref _curr_index_name;
    /** The term in the current item */
    vespalib::stringref _curr_term;
    int64_t             _curr_integer_term;

    /* extra arguments */
    uint32_t _extraIntArg1;
    uint32_t _extraIntArg2;
    uint32_t _extraIntArg3;
    double   _extraDoubleArg4;
    double   _extraDoubleArg5;
    /** The predicate query specification */
    std::unique_ptr<query::PredicateQueryTerm> _predicate_query_term;
    std::unique_ptr<query::TermVector>         _terms;

    VESPA_DLL_LOCAL vespalib::stringref read_stringref(const char *&p);
    VESPA_DLL_LOCAL uint64_t readCompressedPositiveInt(const char *&p);
    VESPA_DLL_LOCAL int64_t readCompressedInt(const char *&p);
    template <typename T>
    VESPA_DLL_LOCAL T read_value(const char*& p);
    VESPA_DLL_LOCAL void readPredicate(const char *&p);
    VESPA_DLL_LOCAL void readNN(const char *&p);
    VESPA_DLL_LOCAL void readComplexTerm(const char *& p);
    VESPA_DLL_LOCAL void readFuzzy(const char *&p);
    VESPA_DLL_LOCAL void read_string_in(const char*& p);
    VESPA_DLL_LOCAL void read_numeric_in(const char*& p);
    VESPA_DLL_LOCAL bool readNext();
public:
    /**
     * Make an iterator on a buffer. To get the first item, next must be called.
     */
    explicit SimpleQueryStackDumpIterator(vespalib::stringref buf);
    SimpleQueryStackDumpIterator(const SimpleQueryStackDumpIterator &) = delete;
    SimpleQueryStackDumpIterator& operator=(const SimpleQueryStackDumpIterator &) = delete;
    ~SimpleQueryStackDumpIterator();

    vespalib::stringref getStack() const noexcept { return vespalib::stringref(_buf, _bufEnd - _buf); }
    size_t getPosition() const noexcept { return _currPos; }

    /**
     * Moves to the next item in the buffer.
     *
     * @return true if there is a new item, false if there are no more items
     * or if there was errors in extracting the next item.
     */
    bool next();

    /**
     * Get the type of the current item.
     * @return the type.
     */
    ParseItem::ItemType getType() const noexcept { return _currType; }
    /**
     * Get the type of the current item.
     * @return the type.
     */
    ParseItem::ItemCreator getCreator() const { return ParseItem::GetCreator(_currFlags); }

    /**
     * Get the rank weight of the current item.
     *
     * @return rank weight.
     **/
    query::Weight GetWeight() const noexcept { return _currWeight; }

    /**
     * Get the unique id of the current item.
     *
     * @return unique id of current item
     **/
    uint32_t getUniqueId() const noexcept { return _currUniqueId; }

    // Get the flags of the current item.
    [[nodiscard]] bool hasNoRankFlag() const noexcept {
        return (_currFlags & ParseItem::IFLAG_NORANK) != 0;
    }
    [[nodiscard]] bool hasSpecialTokenFlag() const noexcept {
        return (_currFlags & ParseItem::IFLAG_SPECIALTOKEN) != 0;
    }
    [[nodiscard]] bool hasNoPositionDataFlag() const noexcept {
        return (_currFlags & ParseItem::IFLAG_NOPOSITIONDATA) != 0;
    }
    [[nodiscard]] bool has_prefix_match_semantics() const noexcept {
        return (_currFlags & ParseItem::IFLAG_PREFIX_MATCH) != 0;
    }

    uint32_t getArity() const noexcept { return _currArity; }

    uint32_t getNearDistance() const noexcept { return _extraIntArg1; }
    uint32_t getTargetHits() const noexcept { return _extraIntArg1; }
    double getDistanceThreshold() const noexcept { return _extraDoubleArg4; }
    double getScoreThreshold() const noexcept { return _extraDoubleArg4; }
    double getThresholdBoostFactor() const noexcept { return _extraDoubleArg5; }
    bool getAllowApproximate() const noexcept { return (_extraIntArg2 != 0); }
    uint32_t getExploreAdditionalHits() const noexcept { return _extraIntArg3; }

    // fuzzy match arguments (see also: has_prefix_match_semantics() for fuzzy prefix matching)
    [[nodiscard]] uint32_t fuzzy_max_edit_distance() const noexcept { return _extraIntArg1; }
    [[nodiscard]] uint32_t fuzzy_prefix_lock_length() const noexcept { return _extraIntArg2; }

    std::unique_ptr<query::PredicateQueryTerm> getPredicateQueryTerm();
    std::unique_ptr<query::TermVector> get_terms();

    vespalib::stringref getIndexName() const noexcept { return _curr_index_name; }
    vespalib::stringref getTerm() const noexcept { return _curr_term; }
    int64_t getIntergerTerm() const noexcept { return _curr_integer_term; }

    static vespalib::stringref DEFAULT_INDEX;
};

}