aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/parsequery/stackdumpiterator.h
blob: dece4ecc0b6f76db492c5bac697742bfa3a33c19 (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
// 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/searchlib/query/tree/predicate_query_term.h>
#include <vespa/vespalib/stllike/string.h>

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 */
    query::PredicateQueryTerm::UP _predicate_query_term;

    VESPA_DLL_LOCAL vespalib::stringref read_stringref(const char *&p);
    VESPA_DLL_LOCAL uint64_t readCompressedPositiveInt(const char *&p);
    VESPA_DLL_LOCAL bool readPredicate(const char *&p);
    VESPA_DLL_LOCAL bool readNN(const char *&p);
    VESPA_DLL_LOCAL bool readComplexTerm(const char *& p);
    VESPA_DLL_LOCAL bool readFuzzy(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 { 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 { 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 { return _currWeight; }

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

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

    uint32_t getArity() const { return _currArity; }

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

    // fuzzy match arguments
    uint32_t getFuzzyMaxEditDistance() const { return _extraIntArg1; }
    uint32_t getFuzzyPrefixLength() const { return _extraIntArg2; }

    query::PredicateQueryTerm::UP getPredicateQueryTerm() { return std::move(_predicate_query_term); }

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

}