summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/extractkeywords/simplequerystackitem.cpp
blob: c28446cf01fddc71b1e016f85818f51493b48a37 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simplequerystackitem.h"
#include <vespa/vespalib/objects/nbo.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <cassert>

namespace search {

SimpleQueryStackItem::SimpleQueryStackItem()
  : _next(NULL),
    _arg1(0),
    _arg2(0),
    _arg3(0),
    _type(ITEM_UNDEF),
    _arity(0),
    _indexName(),
    _term()
{}

namespace {

void assert_term_type(ParseItem::ItemType type) {
    assert(type == ParseItem::ITEM_TERM ||
           type == ParseItem::ITEM_NUMTERM ||
           type == ParseItem::ITEM_NEAREST_NEIGHBOR ||
           type == ParseItem::ITEM_GEO_LOCATION_TERM ||
           type == ParseItem::ITEM_PREFIXTERM ||
           type == ParseItem::ITEM_SUBSTRINGTERM ||
           type == ParseItem::ITEM_SUFFIXTERM ||
           type == ParseItem::ITEM_PURE_WEIGHTED_STRING ||
           type == ParseItem::ITEM_PURE_WEIGHTED_LONG ||
           type == ParseItem::ITEM_EXACTSTRINGTERM ||
           type == ParseItem::ITEM_PREDICATE_QUERY);
    (void) type;
}

void assert_arity_type(ParseItem::ItemType type) {
    // types with arity, but without an index name:
    assert(type == ParseItem::ITEM_OR ||
           type == ParseItem::ITEM_WEAK_AND ||
           type == ParseItem::ITEM_EQUIV ||
           type == ParseItem::ITEM_AND ||
           type == ParseItem::ITEM_NOT ||
           type == ParseItem::ITEM_RANK ||
           type == ParseItem::ITEM_ANY ||
           type == ParseItem::ITEM_NEAR ||
           type == ParseItem::ITEM_ONEAR);
    (void) type;
}

void assert_arity_and_index_type(ParseItem::ItemType type) {
    // types with arity and an index name:
    assert(type == ParseItem::ITEM_PHRASE ||
           type == ParseItem::ITEM_SAME_ELEMENT ||
           type == ParseItem::ITEM_WEIGHTED_SET ||
           type == ParseItem::ITEM_DOT_PRODUCT ||
           type == ParseItem::ITEM_WAND ||
           type == ParseItem::ITEM_WORD_ALTERNATIVES);
    (void) type;
}

int64_t term_as_n64(vespalib::stringref term) {
    int64_t tmp;
    vespalib::asciistream generatedTerm(term);
    generatedTerm >> tmp;
    return vespalib::nbo::n2h(tmp);
}

} // namespace <unnamed>


SimpleQueryStackItem::SimpleQueryStackItem(ItemType type, int arity) : SimpleQueryStackItem()
{
    assert_arity_type(type);
    SetType(type);
    _arity = arity;
}

SimpleQueryStackItem::SimpleQueryStackItem(ItemType type, int arity, const char *idx) : SimpleQueryStackItem()
{
    assert_arity_and_index_type(type);
    SetType(type);
    _arity = arity;
    SetIndex(idx);
}

SimpleQueryStackItem::SimpleQueryStackItem(ItemType type, const char *term) : SimpleQueryStackItem()
{
    assert_term_type(type);
    SetType(type);
    SetTerm(term);
}

SimpleQueryStackItem::~SimpleQueryStackItem()
{
    delete _next;
}

void
SimpleQueryStackItem::AppendBuffer(RawBuf *buf) const
{
    // Calculate lengths
    uint32_t indexLen = _indexName.size();
    uint32_t termLen = _term.size();
    double nboVal = 0.0;

    // Put the values into the buffer.
    buf->append(_type);
    switch (Type()) {
    case ITEM_OR:
    case ITEM_EQUIV:
    case ITEM_AND:
    case ITEM_NOT:
    case ITEM_RANK:
    case ITEM_ANY:
        buf->appendCompressedPositiveNumber(_arity);
        break;
    case ITEM_NEAR:
    case ITEM_ONEAR:
        buf->appendCompressedPositiveNumber(_arity);
        buf->appendCompressedPositiveNumber(_arg1);
        break;
    case ITEM_SAME_ELEMENT:
    case ITEM_WEIGHTED_SET:
    case ITEM_DOT_PRODUCT:
    case ITEM_PHRASE:
        buf->appendCompressedPositiveNumber(_arity);
        buf->appendCompressedPositiveNumber(indexLen);
        buf->append(_indexName.c_str(), indexLen);
        break;
    case ITEM_WORD_ALTERNATIVES:
        buf->appendCompressedPositiveNumber(indexLen);
        buf->append(_indexName.c_str(), indexLen);
        buf->appendCompressedPositiveNumber(_arity);
        break;
    case ITEM_WEAK_AND:
        buf->appendCompressedPositiveNumber(_arity);
        buf->appendCompressedPositiveNumber(_arg1);
        buf->appendCompressedPositiveNumber(indexLen);
        buf->append(_indexName.c_str(), indexLen);
        break;
    case ITEM_WAND:
        buf->appendCompressedPositiveNumber(_arity);
        buf->appendCompressedPositiveNumber(indexLen);
        buf->append(_indexName.c_str(), indexLen);
        buf->appendCompressedPositiveNumber(_arg1); // targetNumHits
        nboVal = vespalib::nbo::n2h(_arg2);
        buf->append(&nboVal, sizeof(nboVal)); // scoreThreshold
        nboVal = vespalib::nbo::n2h(_arg3);
        buf->append(&nboVal, sizeof(nboVal)); // thresholdBoostFactor
        break;
    case ITEM_TERM:
    case ITEM_NUMTERM:
    case ITEM_GEO_LOCATION_TERM:
    case ITEM_PREFIXTERM:
    case ITEM_SUBSTRINGTERM:
    case ITEM_EXACTSTRINGTERM:
    case ITEM_SUFFIXTERM:
    case ITEM_REGEXP:
        buf->appendCompressedPositiveNumber(indexLen);
        buf->append(_indexName.c_str(), indexLen);
        buf->appendCompressedPositiveNumber(termLen);
        buf->append(_term.c_str(), termLen);
        break;
    case ITEM_TRUE:
    case ITEM_FALSE:
        // no content
        break;
    case ITEM_PURE_WEIGHTED_STRING:
        buf->appendCompressedPositiveNumber(termLen);
        buf->append(_term.c_str(), termLen);
        break;
    case ITEM_PURE_WEIGHTED_LONG:
        {
            int64_t tmp = term_as_n64(_term);
            buf->append(&tmp, sizeof(int64_t));
        }
        break;
    case ITEM_NEAREST_NEIGHBOR:
        buf->appendCompressedPositiveNumber(indexLen);
        buf->append(_indexName.c_str(), indexLen);
        buf->appendCompressedPositiveNumber(termLen);
        buf->append(_term.c_str(), termLen);
        buf->appendCompressedPositiveNumber(_arg1); // targetNumHits
        buf->appendCompressedPositiveNumber(_arg2); // allow_approximate
        buf->appendCompressedPositiveNumber(_arg3); // explore_additional_hits
        break;
    case ITEM_PREDICATE_QUERY: // not handled at all here
    case ITEM_MAX:
    case ITEM_UNDEF:
        abort();
        break;
    }
}

}