aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/expression/resultnodes.cpp
blob: c591e305afa0470b81c723fb83c5e432fdd78cd8 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "integerresultnode.h"
#include "floatresultnode.h"
#include "stringresultnode.h"
#include "rawresultnode.h"
#include "enumresultnode.h"
#include "nullresultnode.h"
#include "positiveinfinityresultnode.h"
#include <vespa/vespalib/locale/c.h>
#include <cmath>
#include <vespa/vespalib/objects/visit.hpp>
#include <vespa/vespalib/objects/serializer.hpp>
#include <vespa/vespalib/objects/deserializer.hpp>
#include <stdexcept>

namespace search::expression {

using vespalib::nbo;
using vespalib::Serializer;
using vespalib::Deserializer;
using vespalib::make_string;
using vespalib::Identifiable;
using vespalib::BufferRef;
using vespalib::ConstBufferRef;

IMPLEMENT_ABSTRACT_RESULTNODE(ResultNode,        Identifiable);
IMPLEMENT_ABSTRACT_RESULTNODE(SingleResultNode,  ResultNode);
IMPLEMENT_ABSTRACT_RESULTNODE(NumericResultNode, SingleResultNode);
IMPLEMENT_ABSTRACT_RESULTNODE(IntegerResultNode, NumericResultNode);
IMPLEMENT_RESULTNODE(StringResultNode,           SingleResultNode);
IMPLEMENT_RESULTNODE(NullResultNode,             SingleResultNode);
IMPLEMENT_RESULTNODE(PositiveInfinityResultNode, SingleResultNode);
IMPLEMENT_RESULTNODE(RawResultNode,              SingleResultNode);
IMPLEMENT_RESULTNODE(BoolResultNode,             IntegerResultNode);
IMPLEMENT_RESULTNODE(Int8ResultNode,             IntegerResultNode);
IMPLEMENT_RESULTNODE(Int16ResultNode,            IntegerResultNode);
IMPLEMENT_RESULTNODE(Int32ResultNode,            IntegerResultNode);
IMPLEMENT_RESULTNODE(Int64ResultNode,            IntegerResultNode);
IMPLEMENT_RESULTNODE(EnumResultNode,             IntegerResultNode);
IMPLEMENT_RESULTNODE(FloatResultNode,            NumericResultNode);

namespace {

const vespalib::string TRUE = "true";
const vespalib::string FALSE = "false";

size_t hashBuf(const void *s, size_t sz)
{
    size_t result(0);
    const size_t * value = static_cast<const size_t *>(s);
    for(size_t i(0), m(sz/sizeof(size_t)); i < m; i++) {
        result ^= value[i];
    }
    unsigned left(sz%sizeof(size_t));
    if (left) {
        size_t lastValue(0);
        memcpy(&lastValue, static_cast<const char *>(s)+sz-left, left);
        result ^= lastValue;
    }
    return result;
}

template <typename T>
int cmpNum(T a, T b) {
    return a < b ? -1 : a > b ? 1 : 0;
}

}


void ResultNode::sort() {}

void ResultNode::reverse() {}

void ResultNode::negate()
{
    throw std::runtime_error(make_string("Class %s does not implement 'negate'", getClass().name()));
}

ResultSerializer & ResultNode::onSerializeResult(ResultSerializer & os) const
{
    os.proxyPut(*this);
    return os;
}

ResultDeserializer & ResultNode::onDeserializeResult(ResultDeserializer & is)
{
    is.proxyGet(*this);
    return is;
}

int64_t FloatResultNode::onGetInteger(size_t ) const {
    if (std::isnan(_value)) {
        return std::numeric_limits<int64_t>::min();
    } else {
        return static_cast<int64_t>(std::round(_value));
    }
}

double  FloatResultNode::onGetFloat(size_t index)   const { (void) index; return _value; }
void FloatResultNode::add(const ResultNode & b)      { _value += b.getFloat(); }
void FloatResultNode::negate()                       { _value = - _value; }
void FloatResultNode::multiply(const ResultNode & b) { _value *= b.getFloat(); }
void FloatResultNode::divide(const ResultNode & b)   {
    double val = b.getFloat();
    _value = (val == 0.0) ? 0.0 : (_value / val);
}
void FloatResultNode::modulo(const ResultNode & b)   { _value = ResultNode::getInteger() % b.getInteger(); }
void FloatResultNode::min(const ResultNode & b)      { double t(b.getFloat()); if (t < _value) { _value = t; } }
void FloatResultNode::max(const ResultNode & b)      { double t(b.getFloat()); if (t > _value) { _value = t; } }
void FloatResultNode::set(const ResultNode & rhs)    { _value = rhs.getFloat(); }
Serializer & FloatResultNode::onSerialize(Serializer & os) const { os << _value; return os; }
Deserializer & FloatResultNode::onDeserialize(Deserializer & is) { is >> _value; return is; }

void
FloatResultNode::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    visit(visitor, "value", _value);
}

ResultNode::ConstBufferRef
FloatResultNode::onGetString(size_t index, ResultNode::BufferRef buf) const
{
    (void) index;
    int numWritten = std::min(buf.size(), (size_t)std::max(0, snprintf(buf.str(), buf.size(), "%g", _value)));
    return ConstBufferRef(buf.str(), numWritten);
}

bool
FloatResultNode::isNan() const
{
    return std::isnan(_value);
}

int
FloatResultNode::onCmp(const Identifiable & b) const
{
    const FloatResultNode & rhs(static_cast<const FloatResultNode &>(b));
    if (isNan()) {
        return rhs.isNan() ? 0 : -1;
    } else {
        if (rhs.isNan()) {
            return 1;
        } else {
            return (_value > rhs._value) ? 1 : (_value < rhs._value) ? -1 : 0;
        }
    }
}

void StringResultNode::setMin() { _value.clear(); }
void StringResultNode::setMax() { _value.clear(); _value.append(char(-1)); }
void RawResultNode::setMin() { _value.clear(); }
void RawResultNode::setMax() { _value.push_back(-1); }
void FloatResultNode::setMin() { _value = -std::numeric_limits<double>::max(); }
void FloatResultNode::setMax() { _value = std::numeric_limits<double>::max(); }

void NullResultNode::setMin() { }
void NullResultNode::setMax() { }
void    NullResultNode::add(const ResultNode & b) { (void) b; }
void    NullResultNode::min(const ResultNode & b) { (void) b; }
void    NullResultNode::max(const ResultNode & b) { (void) b; }
int64_t NullResultNode::onGetInteger(size_t index) const { (void) index; return 0; }
double  NullResultNode::onGetFloat(size_t index)   const { (void) index; return 0.0; }
int     NullResultNode::onCmp(const Identifiable & b) const { (void) b; return (b.getClass().id() == NullResultNode::classId) ? 0 : 1; }
void    NullResultNode::set(const ResultNode & rhs) { (void) rhs; }
size_t  NullResultNode::hash() const { return 0; }
ResultNode::ConstBufferRef NullResultNode::onGetString(size_t index, ResultNode::BufferRef buf) const { (void) index; return buf; }
void PositiveInfinityResultNode::setMin() { }
void PositiveInfinityResultNode::setMax() { }
void    PositiveInfinityResultNode::add(const ResultNode & b) { (void) b; }
void    PositiveInfinityResultNode::min(const ResultNode & b) { (void) b; }
void    PositiveInfinityResultNode::max(const ResultNode & b) { (void) b; }
int64_t PositiveInfinityResultNode::onGetInteger(size_t index) const { (void) index; return 0; }
double  PositiveInfinityResultNode::onGetFloat(size_t index)   const { (void) index; return 0.0; }
void    PositiveInfinityResultNode::set(const ResultNode & rhs) { (void) rhs; }
size_t  PositiveInfinityResultNode::hash() const { return 0; }
ResultNode::ConstBufferRef PositiveInfinityResultNode::onGetString(size_t index, ResultNode::BufferRef buf) const { (void) index; return buf; }

int PositiveInfinityResultNode::onCmp(const Identifiable & b) const
{
    if (b.inherits(PositiveInfinityResultNode::classId)) {
        return 0;
    }
    return 1;
}

int64_t StringResultNode::onGetInteger(size_t index) const { (void) index; return strtoll(_value.c_str(), nullptr, 0); }
double  StringResultNode::onGetFloat(size_t index)   const { (void) index; return vespalib::locale::c::strtod(_value.c_str(), nullptr); }
Serializer &
StringResultNode::onSerialize(Serializer & os) const
{
    os << _value;
    return os;
}

int
StringResultNode::onCmp(const Identifiable & b) const
{
    if (b.inherits(PositiveInfinityResultNode::classId)) {
        return -1;
    } else {
        const StringResultNode & sb(static_cast<const StringResultNode &>(b));
        size_t sz(std::min(_value.size(), sb._value.size()));
        int result = memcmp(_value.c_str(), sb._value.c_str(), sz);
        if (result == 0) {
            result = _value.size() < sb._value.size() ? -1 : _value.size() > sb._value.size() ? 1 : 0;
        }
        return result;
    }
}

Deserializer &
StringResultNode::onDeserialize(Deserializer & is)
{
    is >> _value;
    return is;
}


void
RawResultNode::add(const ResultNode & b)
{
    char buf[32];
    ConstBufferRef s(b.getString(BufferRef(buf, sizeof(buf))));
    const uint8_t *raw = static_cast<const uint8_t *>(s.data());

    size_t i(0);
    for (; i < _value.size() && i < s.size(); i++) {
        _value[i] += raw[i];
    }
    if (i < s.size()) {
        for (; i < s.size(); i++) {
            _value.push_back(raw[i]);
        }
    }

}

void
RawResultNode::min(const ResultNode & b)
{
    int res = cmp(b);
    if (res > 0) {
        set(b);
    }
}

void
RawResultNode::max(const ResultNode & b)
{
    int res = cmp(b);
    if (res < 0) {
        set(b);
    }
}

void
RawResultNode::negate()
{
    for (size_t i(0); i < _value.size(); i++) {
        _value[i] = - _value[i];
    }
}

void
StringResultNode::add(const ResultNode & b)
{
    char buf[32];
    ConstBufferRef s(b.getString(BufferRef(buf, sizeof(buf))));
    vespalib::stringref bs(s.c_str(), s.size());
    size_t i(0);
    for (; i < _value.length() && i < bs.length(); i++) {
        _value[i] += bs[i];
    }
    if (i < bs.length()) {
        // XXX: Should have some way of appending with iterators
        _value.append(bs.data() + i, (bs.length() - i));
    }
}

void
StringResultNode::min(const ResultNode & b)
{
    char buf[32];
    ConstBufferRef s(b.getString(BufferRef(buf, sizeof(buf))));
    vespalib::stringref bs(s.c_str(), s.size());
    if (_value > bs) {
        _value = bs;
    }
}

void
StringResultNode::max(const ResultNode & b)
{
    char buf[32];
    ConstBufferRef s(b.getString(BufferRef(buf, sizeof(buf))));
    vespalib::stringref bs(s.c_str(), s.size());
    if (_value < bs) {
        _value = bs;
    }
}

void
StringResultNode::negate()
{
    for (size_t i(0); i < _value.length(); i++) {
        _value[i] = - _value[i];
    }
}

void
StringResultNode::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    visit(visitor, "value", _value);
}

ResultNode::ConstBufferRef
StringResultNode::onGetString(size_t index, ResultNode::BufferRef ) const {
    (void) index;
    return ConstBufferRef(_value.c_str(), _value.size());
}

void
StringResultNode::set(const ResultNode & rhs)
{
    char buf[32];
    ConstBufferRef b(rhs.getString(BufferRef(buf, sizeof(buf))));
    _value.assign(b.c_str(), b.size());
}

StringResultNode & StringResultNode::append(const ResultNode & rhs)
{
    char buf[32];
    ConstBufferRef b(rhs.getString(BufferRef(buf, sizeof(buf))));
    _value.append(b.c_str(), b.size());
    return *this;
}

size_t StringResultNode::hash() const { return hashBuf(_value.c_str(),  _value.size()); }

size_t
StringResultNode::hash(const void * buf) const
{
    const vespalib::string & s = *static_cast<const vespalib::string *>(buf);
    return hashBuf(s.c_str(), s.size());
}

int64_t
RawResultNode::onGetInteger(size_t index) const
{
    (void) index;
    union {
        int64_t _int64;
        uint8_t _bytes[8];
    } nbo;
    nbo._int64 = 0;
    memcpy(nbo._bytes, _value.data(), std::min(sizeof(nbo._bytes), _value.size()));
    return nbo::n2h(nbo._int64);
}

double  RawResultNode::onGetFloat(size_t index) const
{
    (void) index;
    union {
        double  _double;
        uint8_t _bytes[8];
    } nbo;
    nbo._double = 0;
    memcpy(nbo._bytes, _value.data(), std::min(sizeof(nbo._bytes), _value.size()));
    return nbo::n2h(nbo._double);
}

Serializer & RawResultNode::onSerialize(Serializer & os) const
{
    os << _value;
    return os;
}

ResultSerializer & RawResultNode::onSerializeResult(ResultSerializer & os) const
{
    return os.putResult(*this);
}

int RawResultNode::onCmp(const Identifiable & b) const
{
    if (b.inherits(PositiveInfinityResultNode::classId)) {
        return -1;
    } else {
        const RawResultNode & rb( static_cast<const RawResultNode &>(b) );
        size_t min_sz = std::min(_value.size(), rb._value.size());
        if (min_sz == 0) {
            return cmpNum(_value.size(), rb._value.size());
        }
        int result = memcmp(_value.data(), rb._value.data(), min_sz);
        if (result == 0) {
            return cmpNum(_value.size(), rb._value.size());
        }
        return result;
    }
}

size_t RawResultNode::hash() const { return hashBuf(_value.data(), _value.size()); }

size_t
RawResultNode::hash(const void * buf) const
{
    const std::vector<uint8_t> & s = *static_cast<const std::vector<uint8_t> *>(buf);
    return hashBuf(s.data(), s.size());
}

Deserializer &
RawResultNode::onDeserialize(Deserializer & is)
{
    is >> _value;
    return is;
}

ResultDeserializer &
RawResultNode::onDeserializeResult(ResultDeserializer & is)
{
    return is.getResult(*this);
}

void
RawResultNode::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    visit(visitor, "value", _value);
}

void
RawResultNode::set(const ResultNode & rhs)
{
    char buf[32];
    ConstBufferRef b(rhs.getString(BufferRef(buf, sizeof(buf))));
    setBuffer(b.data(), b.size());
}
void
RawResultNode::setBuffer(const void *buf, size_t sz)
{
    _value.resize(sz + 1);
    if (sz > 0) {
        memcpy(_value.data(), buf, sz);
    }
    _value.back() = 0;
    _value.resize(sz);
}

ResultNode::ConstBufferRef
RawResultNode::onGetString(size_t, BufferRef ) const {
    return ConstBufferRef(_value.data(), _value.size());
}

ResultNode::ConstBufferRef
EnumResultNode::onGetString(size_t, BufferRef buf) const {
    int numWritten(std::min(buf.size(), (size_t)std::max(0, snprintf(buf.str(), buf.size(), "%" PRId64, getValue()))));
    return ConstBufferRef(buf.str(), numWritten);
}

ResultNode::ConstBufferRef
BoolResultNode::onGetString(size_t, BufferRef) const {
    return getValue() ? ConstBufferRef(TRUE.data(), TRUE.size()) : ConstBufferRef(FALSE.data(), FALSE.size());
}

ResultNode::ConstBufferRef
Int8ResultNode::onGetString(size_t, BufferRef buf) const {
    int numWritten(std::min(buf.size(), (size_t)std::max(0, snprintf(buf.str(), buf.size(), "%d", getValue()))));
    return ConstBufferRef(buf.str(), numWritten);
}

ResultNode::ConstBufferRef
Int16ResultNode::onGetString(size_t, BufferRef buf) const {
    int numWritten(std::min(buf.size(), (size_t)std::max(0, snprintf(buf.str(), buf.size(), "%d", getValue()))));
    return ConstBufferRef(buf.str(), numWritten);
}

ResultNode::ConstBufferRef
Int32ResultNode::onGetString(size_t, BufferRef buf) const {
    int numWritten(std::min(buf.size(), (size_t)std::max(0, snprintf(buf.str(), buf.size(), "%d", getValue()))));
    return ConstBufferRef(buf.str(), numWritten);
}

ResultNode::ConstBufferRef
Int64ResultNode::onGetString(size_t, BufferRef buf) const {
    int numWritten(std::min(buf.size(), (size_t)std::max(0, snprintf(buf.str(), buf.size(), "%" PRId64, getValue()))));
    return ConstBufferRef(buf.str(), numWritten);
}

}