aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/serialization/vespadocumentserializer.cpp
blob: 4a1af74d04809a02207df57028d92ec13a6ba0ce (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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "annotationserializer.h"
#include "slime_output_to_vector.h"
#include "util.h"
#include "vespadocumentserializer.h"
#include <vespa/document/datatype/weightedsetdatatype.h>
#include <vespa/document/fieldset/fieldsets.h>
#include <vespa/document/fieldvalue/annotationreferencefieldvalue.h>
#include <vespa/document/fieldvalue/arrayfieldvalue.h>
#include <vespa/document/fieldvalue/boolfieldvalue.h>
#include <vespa/document/fieldvalue/bytefieldvalue.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/fieldvalue/doublefieldvalue.h>
#include <vespa/document/fieldvalue/floatfieldvalue.h>
#include <vespa/document/fieldvalue/intfieldvalue.h>
#include <vespa/document/fieldvalue/longfieldvalue.h>
#include <vespa/document/fieldvalue/mapfieldvalue.h>
#include <vespa/document/fieldvalue/predicatefieldvalue.h>
#include <vespa/document/fieldvalue/rawfieldvalue.h>
#include <vespa/document/fieldvalue/referencefieldvalue.h>
#include <vespa/document/fieldvalue/shortfieldvalue.h>
#include <vespa/document/fieldvalue/stringfieldvalue.h>
#include <vespa/document/fieldvalue/tensorfieldvalue.h>
#include <vespa/document/fieldvalue/weightedsetfieldvalue.h>
#include <vespa/document/update/fieldpathupdates.h>
#include <vespa/document/update/updates.h>
#include <vespa/document/util/bytebuffer.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/vespalib/data/databuffer.h>
#include <vespa/vespalib/data/slime/binary_format.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/compressor.h>

using std::make_pair;
using std::pair;
using std::vector;
using vespalib::nbostream;
using vespalib::stringref;
using vespalib::string;
using vespalib::slime::BinaryFormat;
using vespalib::compression::CompressionConfig;

namespace document {

VespaDocumentSerializer::VespaDocumentSerializer(nbostream &stream)
    : _stream(stream) {
}

void
VespaDocumentSerializer::writeFieldValue(const FieldValue &value) {
    write(value);
}

void
VespaDocumentSerializer::writeSerializedData(const void *buf, size_t length) {
    _stream.write(buf, length);
}

void
VespaDocumentSerializer::write(const ValueUpdate &value) {
    value.accept(*this);
}

void
VespaDocumentSerializer::write(const FieldPathUpdate &value) {
    value.accept(*this);
}

void
VespaDocumentSerializer::write(const FieldValue &value) {
    value.accept(*this);
}

void
VespaDocumentSerializer::write(const DocumentId &value) {
    string id_string = value.getScheme().toString();
    _stream.write(id_string.data(), id_string.size());
    _stream << static_cast<uint8_t>(0);
}

void VespaDocumentSerializer::write(const DocumentType &value) {
    _stream.write(value.getName().data(), value.getName().size());

    _stream << static_cast<uint8_t>(0)
            << static_cast<uint16_t>(0);  // version
}

namespace {

uint8_t
getContentCode(bool hasContent)
{
    return 0x01u |  // Document type is always present
           (hasContent ? 0x02u : 0x00u);   // Payload ?
}

}

void
VespaDocumentSerializer::write(const Document &value) {
    nbostream doc_stream;
    VespaDocumentSerializer doc_serializer(doc_stream);
    doc_serializer.write(value.getId());

    bool hasContent = ! value.getFields().empty();
    doc_stream << getContentCode(hasContent);
    doc_serializer.write(value.getType());

    if ( hasContent ) {
        if (!structNeedsReserialization(value.getFields())) {
            doc_serializer.writeUnchanged(value.getFields().getFields());
        } else {
            doc_serializer.write(value.getFields(), AllFields());
        }
    }

    const uint16_t version = serialize_version;
    _stream << version << static_cast<uint32_t>(doc_stream.size());
    _stream.write(doc_stream.peek(), doc_stream.size());
}

void
VespaDocumentSerializer::visit(const StructFieldValue &value)
{
    if (!structNeedsReserialization(value)) {
        writeUnchanged(value.getFields());
    } else {
        write(value, AllFields());
    }
}

void
VespaDocumentSerializer::write(const AnnotationReferenceFieldValue &value)
{
    putInt1_2_4Bytes(_stream, value.getAnnotationIndex());
}

void VespaDocumentSerializer::write(const ArrayFieldValue &value) {
    putInt1_2_4Bytes(_stream, value.size());
    for (const auto & item : value) {
       item.accept(*this);
    }
}

void
VespaDocumentSerializer::write(const MapFieldValue &value) {
    putInt1_2_4Bytes(_stream, value.size());
    for (const auto & entry : value) {
        (*entry.first).accept(*this);
        (*entry.second).accept(*this);
    }
}

void
VespaDocumentSerializer::write(const BoolFieldValue &value) {
    _stream << value.getValue();
}

void
VespaDocumentSerializer::write(const ByteFieldValue &value) {
    _stream << value.getValue();
}

void
VespaDocumentSerializer::write(const DoubleFieldValue &value) {
    _stream << value.getValue();
}

void
VespaDocumentSerializer::write(const FloatFieldValue &value) {
    _stream << value.getValue();
}

void
VespaDocumentSerializer::write(const IntFieldValue &value) {
    _stream << static_cast<uint32_t>(value.getValue());
}

void
VespaDocumentSerializer::write(const LongFieldValue &value) {
    _stream << static_cast<uint64_t>(value.getValue());
}

void
VespaDocumentSerializer::write(const PredicateFieldValue &value) {
    SlimeOutputToVector output;
    vespalib::slime::BinaryFormat::encode(value.getSlime(), output);
    _stream << static_cast<uint32_t>(output.size());
    _stream.write(output.data(), output.size());
}

void
VespaDocumentSerializer::write(const RawFieldValue &value) {
    _stream << static_cast<uint32_t>(value.getValueRef().size());
    _stream.write(value.getValueRef().data(), value.getValueRef().size());
}

void
VespaDocumentSerializer::write(const ShortFieldValue &value) {
    _stream << static_cast<uint16_t>(value.getValue());
}

void
VespaDocumentSerializer::write(const StringFieldValue &value) {
    uint8_t coding = (value.hasSpanTrees() << 6u);
    _stream << coding;
    putInt1_4Bytes(_stream, value.getValueRef().size() + 1);
    _stream.write(value.getValueRef().data(), value.getValueRef().size());
    _stream << static_cast<uint8_t>(0);  // add null-termination.
    if (value.hasSpanTrees()) {
        vespalib::ConstBufferRef buffer = value.getSerializedAnnotations();
        _stream << static_cast<uint32_t>(buffer.size());
        _stream.write(buffer.data(), buffer.size());
    }
}

namespace {
void
serializeFields(const StructFieldValue &value, nbostream &stream,
                vector<pair<uint32_t, uint32_t> > &field_info, const FieldSet& fieldSet)
{
    VespaDocumentSerializer serializer(stream);
    for (StructuredFieldValue::const_iterator it(value.begin()), e(value.end()); it != e; ++it) {
        if (!fieldSet.contains(it.field())) {
            continue;
        }
        size_t original_size = stream.size();
        int id = it.field().getId();
        if (!value.serializeField(id, VespaDocumentSerializer::getCurrentVersion(), serializer)) {
            continue;
        }
        size_t field_size = stream.size() - original_size;
        field_info.emplace_back(it.field().getId(), field_size);
    }
}

void
putFieldInfo(nbostream &output, const vector<pair<uint32_t, uint32_t> > &field_info) {
    putInt1_4Bytes(output, field_info.size());
    for (const auto & field : field_info) {
        putInt1_4Bytes(output, field.first);
        putInt2_4_8Bytes(output, field.second);
    }
}
}  // namespace

/**
 * Reserialize if value has been modified since deserialization
 * or we are bumping version
 * or compression type has changed AND config says compress.
 * The last and is to make sure that we do not decompress a document
 * unintentionally.
 */
bool
VespaDocumentSerializer::structNeedsReserialization(const StructFieldValue &value)
{
    if (value.hasChanged()) {
        return true;
    }

    if (value.getVersion() != serialize_version) {
        return true;
    }

    return false;
}

void
VespaDocumentSerializer::writeUnchanged(const SerializableArray &value) {
    vector<pair<uint32_t, uint32_t> > field_info;
    const std::vector<SerializableArray::Entry>& entries = value.getEntries();

    field_info.reserve(entries.size());
    for(const auto & entry : entries) {
        field_info.emplace_back(entry.id(), entry.size());
    }

    const ByteBuffer* buffer = value.getSerializedBuffer();
    uint32_t sz = (buffer != nullptr) ? buffer->getLength() : 0;
    size_t estimatedRequiredSpace = sz + 4 + 1 + 8 + 4 + field_info.size()*12;
    _stream.reserve(_stream.size() + estimatedRequiredSpace);
    _stream << sz;
    _stream << static_cast<uint8_t>(CompressionConfig::NONE);
    putFieldInfo(_stream, field_info);
    if (sz) {
        _stream.write(buffer->getBuffer(), buffer->getLength());
    }
}

void
VespaDocumentSerializer::write(const StructFieldValue &value, const FieldSet& fieldSet)
{
    nbostream value_stream;
    vector<pair<uint32_t, uint32_t> > field_info;
    serializeFields(value, value_stream, field_info, fieldSet);

    _stream << static_cast<uint32_t>(value_stream.size());
    _stream << static_cast<uint8_t>(CompressionConfig::NONE);
    putFieldInfo(_stream, field_info);
    _stream.write(value_stream.data(), value_stream.size());
}

void
VespaDocumentSerializer::write(const WeightedSetFieldValue &value) {
    auto type = static_cast<const WeightedSetDataType *>(value.getDataType());
    _stream << static_cast<uint32_t>(type->getNestedType().getId());
    _stream << static_cast<uint32_t>(value.size());
    for (const auto & entry : value) {
        nbostream stream;
        VespaDocumentSerializer serializer(stream);
        serializer.write(*entry.first);
        serializer.write(*entry.second);
        _stream << static_cast<uint32_t>(stream.size());  // This is unused
        _stream.write(stream.peek(), stream.size());
    }
}


void
VespaDocumentSerializer::write(const TensorFieldValue &value) {
    vespalib::nbostream tmpStream;
    auto tensor = value.getAsTensorPtr();
    if (tensor) {
        encode_value(*tensor, tmpStream);
        assert( ! tmpStream.empty());
        _stream.putInt1_4Bytes(tmpStream.size());
        _stream.write(tmpStream.peek(), tmpStream.size());
    } else {
        _stream.putInt1_4Bytes(0);
    }
}

void
VespaDocumentSerializer::write(const ReferenceFieldValue& value) {
    _stream << static_cast<uint8_t>(value.hasValidDocumentId() ? 1 : 0);
    if (value.hasValidDocumentId()) {
       write(value.getDocumentId());
    }
}

namespace {
    const uint8_t CONTENT_HASVALUE(0x01);
}

void
VespaDocumentSerializer::writeHEAD(const DocumentUpdate &value)
{
    if (!value._needHardReserialize) {
        _stream.write(value._backing.peek(), value._backing.size());
        return;
    }
    write(value.getId());
    _stream.write(value.getType().getName().c_str(), value.getType().getName().size() + 1);
    _stream << static_cast<uint16_t>(0);
    _stream << static_cast<uint32_t>(value._updates.size());
    for (const auto & update : value._updates) {
        write(update);
    }
    _stream << static_cast<uint32_t>(value.serializeFlags(value._fieldPathUpdates.size()));
    for (const auto & update : value._fieldPathUpdates) {
        _stream << update->getSerializedType();
        write(*update);
    }
}

void
VespaDocumentSerializer::write(const FieldUpdate &value)
{
    _stream << static_cast<int32_t>(value.getField().getId());
    _stream << static_cast<int32_t>(value.size());
    for (size_t i(0), m(value.size()); i < m; i++) {
        write(value[i]);
    }
}

void
VespaDocumentSerializer::write(const RemoveValueUpdate &value)
{
    _stream << uint32_t(ValueUpdate::Remove);
    write(value.getKey());
}


void
VespaDocumentSerializer::write(const AddValueUpdate &value)
{
    _stream << uint32_t(ValueUpdate::Add);
    write(value.getValue());
    _stream << static_cast<int32_t>(value.getWeight());
}

void
VespaDocumentSerializer::write(const ArithmeticValueUpdate &value)
{
    _stream << uint32_t(ValueUpdate::Arithmetic);
    _stream << static_cast<uint32_t>(value.getOperator());
    _stream << static_cast<double>(value.getOperand());
}

void
VespaDocumentSerializer::write(const AssignValueUpdate &value)
{
    _stream << uint32_t(ValueUpdate::Assign);
    if (value.hasValue()) {
        _stream << static_cast<uint8_t>(CONTENT_HASVALUE);
        write(value.getValue());
    } else {
        _stream << static_cast<uint8_t>(0);
    }
}

void
VespaDocumentSerializer::write(const ClearValueUpdate &value)
{
    (void) value;
    _stream << uint32_t(ValueUpdate::Clear);
}

void VespaDocumentSerializer::write(const MapValueUpdate &value)
{
    _stream << uint32_t(ValueUpdate::Map);
    write(value.getKey());
    write(value.getUpdate());
}

namespace {

// We must ensure that string passed is always zero-terminated, so take in
// string instead of stringref. No extra allocs; function only ever called with
// string arguments.
void
writeStringWithZeroTermination(nbostream & os, const vespalib::string& s)
{
    uint32_t sz(s.size() + 1);
    os << sz;
    os.write(s.c_str(), sz);
}

void
writeFieldPath(nbostream & os, const FieldPathUpdate & value)
{
    writeStringWithZeroTermination(os, value.getOriginalFieldPath());
    writeStringWithZeroTermination(os, value.getOriginalWhereClause());
}

}

void
VespaDocumentSerializer::write(const AddFieldPathUpdate &value)
{
    writeFieldPath(_stream, value);
    write(value.getValues());
}

void
VespaDocumentSerializer::write(const AssignFieldPathUpdate &value)
{
    writeFieldPath(_stream, value);
    uint8_t flags = 0;
    flags |= value.getRemoveIfZero() ? AssignFieldPathUpdate::REMOVE_IF_ZERO : 0;
    flags |= value.getCreateMissingPath() ? AssignFieldPathUpdate::CREATE_MISSING_PATH : 0;
    flags |= (! value.hasValue()) ?  AssignFieldPathUpdate::ARITHMETIC_EXPRESSION : 0;
    _stream << flags;
    if (value.hasValue()) {
        write(value.getValue());
    } else {
        writeStringWithZeroTermination(_stream, value.getExpression());
    }

}

void
VespaDocumentSerializer::write(const RemoveFieldPathUpdate &value)
{
    writeFieldPath(_stream, value);
}

namespace {

uint8_t
encode_operation_id(const TensorModifyUpdate& update)
{
    uint8_t op = static_cast<uint8_t>(update.getOperation());
    uint8_t CREATE_FLAG = 0b10000000;
    if (update.get_default_cell_value().has_value()) {
        op |= CREATE_FLAG;
    }
    return op;
}

}

void
VespaDocumentSerializer::write(const TensorModifyUpdate &value)
{
    _stream << uint32_t(ValueUpdate::TensorModify);
    _stream << encode_operation_id(value);
    if (value.get_default_cell_value().has_value()) {
        _stream << value.get_default_cell_value().value();
    }
    write(value.getTensor());
}

void
VespaDocumentSerializer::visit(const TensorModifyUpdate &value)
{
    write(value);
}

void
VespaDocumentSerializer::write(const TensorAddUpdate &value)
{
    _stream << uint32_t(ValueUpdate::TensorAdd);
    write(value.getTensor());
}

void
VespaDocumentSerializer::visit(const TensorAddUpdate &value)
{
    write(value);
}

void
VespaDocumentSerializer::write(const TensorRemoveUpdate &value)
{
    _stream << uint32_t(ValueUpdate::TensorRemove);
    write(value.getTensor());
}

void
VespaDocumentSerializer::visit(const TensorRemoveUpdate &value)
{
    write(value);
}

}  // namespace document