summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/searchsummary/docsummary/resultpacker.cpp
blob: 292bb16589314895051eee26e29a8fcc4a1fd8e9 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "resultpacker.h"
#include <vespa/searchcommon/common/undefinedvalues.h>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.docsummary.resultpacker");

namespace search {
namespace docsummary {

void
ResultPacker::WarnType(ResType type) const
{
    LOG(debug,
        "ResultPacker: got '%s', expected '%s' "
        "(fields are binary compatible)",
        GetResTypeName(type),
        GetResTypeName(_cfgEntry->_type));
}

bool ResultPacker::CheckEntry(ResType type)
{
    if (_error)
        return false;

    bool rc = (_cfgEntry != NULL &&
               IsBinaryCompatible(_cfgEntry->_type, type));

    if (rc) {
        if (_cfgEntry->_type != type) {
            WarnType(type);
        }
        _cfgEntry = _resClass->GetEntry(++_entryIdx);
    } else {
        SetFormatError(type);
    }

    return rc;
}

void
ResultPacker::SetFormatError(ResType type)
{
    _error = true;

    if (_cfgEntry != NULL) {
        LOG(error,
            "ResultPacker: format error: got '%s', expected '%s'",
            GetResTypeName(type),
            GetResTypeName(_cfgEntry->_type));
    } else {
        LOG(error,
            "ResultPacker: format error: "
            "got '%s', no more fields expected", GetResTypeName(type));
    }
}


ResultPacker::ResultPacker(const ResultConfig *resConfig)
    : _buf(32768),
      _cbuf(32768),
      _resConfig(resConfig),
      _resClass(NULL),
      _entryIdx(0),
      _cfgEntry(NULL),
      _error(true)
{
}


ResultPacker::~ResultPacker()
{
}

void
ResultPacker::InitPlain()
{
    _buf.reset();
}

bool
ResultPacker::Init(uint32_t classID)
{
    _buf.reset();
    _resClass = (_resConfig != NULL) ?
                _resConfig->LookupResultClass(classID) : NULL;
    _entryIdx = 0;
    if (_resClass != NULL) {
        uint32_t id = _resClass->GetClassID();
        _buf.append(&id, sizeof(id));
        _cfgEntry = _resClass->GetEntry(_entryIdx);
        _error = false;
    } else {
        _cfgEntry = NULL;
        _error = true;

        LOG(error, "ResultPacker: resultclass %d does not exist", classID);
    }

    return !_error;
}


bool
ResultPacker::AddEmpty()
{
    if (!_error && _cfgEntry != NULL) {
        switch (_cfgEntry->_type) {
        case RES_INT:         return AddInteger(search::attribute::getUndefined<int32_t>());
        case RES_SHORT:       return AddShort(search::attribute::getUndefined<int16_t>());
        case RES_BYTE:        return AddByte(search::attribute::getUndefined<int8_t>());
        case RES_FLOAT:       return AddFloat(search::attribute::getUndefined<float>());
        case RES_DOUBLE:      return AddDouble(search::attribute::getUndefined<double>());
        case RES_INT64:       return AddInt64(search::attribute::getUndefined<int64_t>());
        case RES_STRING:      return AddString(NULL, 0);
        case RES_DATA:        return AddData(NULL, 0);
        case RES_XMLSTRING:
        case RES_JSONSTRING:
        case RES_FEATUREDATA:
        case RES_LONG_STRING: return AddLongString(NULL, 0);
        case RES_TENSOR:      return AddSerializedTensor(NULL, 0);
        case RES_LONG_DATA:   return AddLongData(NULL, 0);
        }
    }
    return AddInteger(0); // to provoke error condition
}


bool
ResultPacker::AddByte(uint8_t value)
{
    if (CheckEntry(RES_BYTE))
        AddByteForce(value);
    return !_error;
}

void
ResultPacker::AddByteForce(uint8_t value)
{
    _buf.append(&value, sizeof(value));
}

bool
ResultPacker::AddShort(uint16_t value)
{
    if (CheckEntry(RES_SHORT))
        AddShortForce(value);
    return !_error;
}

void
ResultPacker::AddShortForce(uint16_t value)
{
    _buf.append(&value, sizeof(value));
}


bool
ResultPacker::AddInteger(uint32_t value)
{
    if (CheckEntry(RES_INT))
        AddIntegerForce(value);
    return !_error;
}

void
ResultPacker::AddIntegerForce(uint32_t value)
{
    _buf.append(&value, sizeof(value));
}


bool
ResultPacker::AddFloat(float value)
{
    if (CheckEntry(RES_FLOAT))
        _buf.append(&value, sizeof(value));
    return !_error;
}


bool
ResultPacker::AddDouble(double value)
{
    if (CheckEntry(RES_DOUBLE))
        _buf.append(&value, sizeof(value));
    return !_error;
}


bool
ResultPacker::AddInt64(uint64_t value)
{
    if (CheckEntry(RES_INT64))
        _buf.append(&value, sizeof(value));
    return !_error;
}


bool
ResultPacker::AddString(const char *str, uint32_t slen)
{
    if (CheckEntry(RES_STRING))
        AddStringForce(str, slen);
    return !_error;
}

void
ResultPacker::AddStringForce(const char *str, uint32_t slen)
{
    uint16_t len = slen;
    _buf.append(&len, sizeof(len));
    _buf.append(str, len);
}


bool
ResultPacker::AddData(const char *buf, uint32_t buflen)
{
    if (CheckEntry(RES_DATA)) {
        uint16_t len = buflen;
        _buf.append(&len, sizeof(len));
        _buf.append(buf, len);
    }
    return !_error;
}


bool
ResultPacker::AddLongString(const char *str, uint32_t slen)
{
    if (CheckEntry(RES_LONG_STRING)) {
        _buf.append(&slen, sizeof(slen));
        _buf.append(str, slen);
    }
    return !_error;
}


bool
ResultPacker::AddLongData(const char *buf, uint32_t buflen)
{
    if (CheckEntry(RES_LONG_DATA)) {
        _buf.append(&buflen, sizeof(buflen));
        _buf.append(buf, buflen);
    }
    return !_error;
}


bool
ResultPacker::AddSerializedTensor(const char *buf, uint32_t buflen)
{
    if (CheckEntry(RES_TENSOR)) {
        _buf.append(&buflen, sizeof(buflen));
        _buf.append(buf, buflen);
    }
    return !_error;
}


bool
ResultPacker::GetDocsumBlob(const char **buf, uint32_t *buflen)
{
    if (!_error &&
        _entryIdx != _resClass->GetNumEntries())
    {
        _error = true;
        LOG(error,
            "ResultPacker: format error: %d fields are missing",
            _resClass->GetNumEntries() - _entryIdx);
    }
    if (_error) {
        *buf    = NULL;
        *buflen = 0;
        return false;
    } else {
        *buf    = _buf.GetDrainPos();
        *buflen = _buf.GetUsedLen();
        return true;
    }
}

void
ResultPacker::GetDocsumBlobForce(const char **buf, uint32_t *buflen)
{
    *buf    = _buf.GetDrainPos();
    *buflen = _buf.GetUsedLen();
}

}
}