summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/extractkeywords/simplequerystackitem.h
blob: 810000b09a64d01627b9cf2f66bab1843a07b8b8 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/searchlib/query/weight.h>
#include <vespa/searchlib/util/rawbuf.h>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/searchlib/parsequery/parse.h>

namespace search {

/**
 * An item on the simple query stack.
 *
 * An object of this class represents a single item
 * on the simple query stack. It has a type, which corresponds
 * to the different query stack execution operations. It also
 * provides an arity, and the string values indexName and term, to
 * accomodate the different needs of the operations.
 */
class SimpleQueryStackItem : public ParseItem
{
private:
    SimpleQueryStackItem(const SimpleQueryStackItem &) = delete;
    SimpleQueryStackItem& operator=(const SimpleQueryStackItem &) = delete;
    SimpleQueryStackItem();
public:
    /** Pointer to next item in a linked list. */
    SimpleQueryStackItem *_next;

private:
    query::Weight _weight;
    uint32_t      _uniqueId;
    uint32_t      _arg1;
    double        _arg2;
    double        _arg3;
    uint8_t       _type;
    uint8_t       _flags;

public:
    /** Extra information on each item (creator id) coded in bits 12-19 of _type */
    static inline ItemCreator GetCreator(uint8_t type) { return static_cast<ItemCreator>((type >> 3) & 0x01); }
    /** The old item type now uses only the lower 12 bits in a backward compatible way) */
    static inline ItemType GetType(uint8_t type) { return static_cast<ItemType>(type & 0x1F); }
    inline ItemType Type() const { return GetType(_type); }

    static inline bool GetFeature(uint8_t type, uint8_t feature)
    { return ((type & feature) != 0); }

    static inline bool GetFeature_Weight(uint8_t type)
    { return GetFeature(type, IF_WEIGHT); }

    static inline bool getFeature_UniqueId(uint8_t type)
    { return GetFeature(type, IF_UNIQUEID); }

    static inline bool getFeature_Flags(uint8_t type)
    { return GetFeature(type, IF_FLAGS); }

    inline bool Feature(uint8_t feature) const
    { return GetFeature(_type, feature); }

    inline bool Feature_Weight() const
    { return GetFeature_Weight(_type); }

    inline bool feature_UniqueId() const
    { return getFeature_UniqueId(_type); }

    inline bool feature_Flags() const
    { return getFeature_Flags(_type); }

    static inline bool getFlag(uint8_t flags, uint8_t flag)
    { return ((flags & flag) != 0); }

    /** The number of operands for the operation. */
    uint32_t _arity;
    /** The name of the specified index, or NULL if no index. */
    vespalib::string _indexName;
    /** The specified search term. */
    vespalib::string  _term;

/**
 * Overloaded constructor for SimpleQueryStackItem. Used primarily for
 * the operators, or phrase without indexName.
 *
 * @param type The type of the SimpleQueryStackItem.
 * @param arity The arity of the operation indicated by the SimpleQueryStackItem.
 */
    SimpleQueryStackItem(ItemType type, int arity);

/**
 * Overloaded constructor for SimpleQueryStackItem. Used for PHRASEs.
 *
 * @param type The type of the SimpleQueryStackItem.
 * @param arity The arity of the operation indicated by the SimpleQueryStackItem.
 * @param idx The name of the index of the SimpleQueryStackItem.
 */
    SimpleQueryStackItem(ItemType type, int arity, const char *index);

/**
 * Overloaded constructor for SimpleQueryStackItem. Used for TERMs without index.
 *
 * @param type The type of the SimpleQueryStackItem.
 * @param term The actual term string of the SimpleQueryStackItem.
 */
    SimpleQueryStackItem(ItemType type, const char *term);

/**
 * Destructor for SimpleQueryStackItem.
 */
    ~SimpleQueryStackItem();

/**
 * Set the value of the _term field.
 * @param term The string to set the _term field to.
 */
    void SetTerm(const char *term) { _term = term; }

/**
 * Set the value of the _indexName field.
 * @param idx The string to set the _indexName field to.
 */
    void SetIndex(const char *index) { _indexName = index; }

    /**
     * Set the type of the operator. Use this with caution,
     * as this changes the semantics of the item.
     *
     * @param type The new type.
     */
    void SetType(ItemType type) {
        _type = (_type & ~0x1F) | type;
    }

    /**
     * Get the unique id for this item.
     *
     * @return unique id for this item
     **/
    uint32_t getUniqueId() const { return _uniqueId; }

    /**
     * Encode the item in a binary buffer.
     * @param buf Pointer to a buffer containing the encoded contents.
     */
    void AppendBuffer(RawBuf *buf) const;
};

}