aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/fieldvalue/document.cpp
blob: 2fb1611f6ffa077961586e19140350e7ba38b9c1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "document.h"
#include "structuredcache.h"
#include <vespa/document/datatype/documenttype.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/serialization/vespadocumentdeserializer.h>
#include <vespa/document/serialization/vespadocumentserializer.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/document/util/serializableexceptions.h>
#include <vespa/document/fieldset/fieldsets.h>
#include <vespa/vespalib/data/databuffer.h>
#include <vespa/vespalib/util/xmlstream.h>
#include <vespa/vespalib/stllike/hash_map.hpp>
#include <cassert>
#include <sstream>

using vespalib::nbostream;
using vespalib::make_string;
using vespalib::IllegalArgumentException;
using vespalib::IllegalStateException;
using namespace vespalib::xml;

namespace document {
namespace {

void documentTypeError(vespalib::stringref name) __attribute__((noinline));
void throwTypeMismatch(vespalib::stringref type, vespalib::stringref docidType) __attribute__((noinline));

void documentTypeError(vespalib::stringref name) {
    throw IllegalArgumentException(make_string("Cannot generate a document with non-document type %s.",
                                               vespalib::string(name).c_str()), VESPA_STRLOC);
}

void throwTypeMismatch(vespalib::stringref type, vespalib::stringref docidType) {
    throw IllegalArgumentException(make_string("Trying to create a document with type %s that don't match the id (type %s).",
                                               vespalib::string(type).c_str(), vespalib::string(docidType).c_str()),
                                   VESPA_STRLOC);
}

}  // namespace

const DataType &
Document::verifyDocumentType(const DataType *type) {
    if (!type) {
        documentTypeError("null");
    } else if ( ! type->isDocument()) {
        documentTypeError(type->toString());
    }
    return *type;
}

void
Document::verifyIdAndType(const DocumentId & id, const DataType *type) {
    verifyDocumentType(type);
    if (id.hasDocType() && (id.getDocType() != type->getName())) {
        throwTypeMismatch(type->getName(), id.getDocType());
    }
}

void
Document::setType(const DataType & type) {
    StructuredFieldValue::setType(type);
    _fields.setType(getType().getFieldsType());
}

Document::Document()
    : StructuredFieldValue(Type::DOCUMENT, *DataType::DOCUMENT),
      _id(),
      _fields(getType().getFieldsType()),
      _backingBuffer(),
      _lastModified(0)
{
    _fields.setDocumentType(getType());
}

Document::Document(const Document& rhs)
    : StructuredFieldValue(rhs),
      _id(rhs._id),
      _fields(rhs._fields),
      _backingBuffer(),
      _lastModified(rhs._lastModified)
{}

Document::Document(const DataType &type, DocumentId documentId)
    : StructuredFieldValue(Type::DOCUMENT, verifyDocumentType(&type)),
      _id(std::move(documentId)),
      _fields(getType().getFieldsType()),
      _backingBuffer(),
      _lastModified(0)
{
    _fields.setDocumentType(getType());
    if (_id.hasDocType() && (_id.getDocType() != type.getName())) {
        throwTypeMismatch(type.getName(), _id.getDocType());
    }
}

Document::UP
Document::make_without_repo(const DataType& type, DocumentId id)
{
    // Must use new as the constructor is private.
    return Document::UP(new Document(type, id));
}

Document::Document(const DocumentTypeRepo& repo, const DataType &type, DocumentId documentId)
    : StructuredFieldValue(Type::DOCUMENT, verifyDocumentType(&type)),
      _id(std::move(documentId)),
      _fields(repo, getType().getFieldsType()),
      _backingBuffer(),
      _lastModified(0)
{
    _fields.setDocumentType(getType());
    if (_id.hasDocType() && (_id.getDocType() != type.getName())) {
        throwTypeMismatch(type.getName(), _id.getDocType());
    }
}

void Document::setRepo(const DocumentTypeRepo& repo)
{
    _fields.setRepo(repo);
}

Document::Document(const DocumentTypeRepo& repo, vespalib::nbostream & is)
    : StructuredFieldValue(Type::DOCUMENT, *DataType::DOCUMENT),
      _id(),
      _fields(static_cast<const DocumentType &>(getType()).getFieldsType()),
      _backingBuffer(),
      _lastModified(0)
{
    deserialize(repo, is);
}

Document::Document(const DocumentTypeRepo& repo, vespalib::DataBuffer && backingBuffer)
    : StructuredFieldValue(Type::DOCUMENT, *DataType::DOCUMENT),
      _id(),
      _fields(static_cast<const DocumentType &>(getType()).getFieldsType()),
      _backingBuffer(),
      _lastModified(0)
{
    if (backingBuffer.referencesExternalData()) {
        vespalib::nbostream is(backingBuffer.getData(), backingBuffer.getDataLen());
        deserialize(repo, is);
    } else {
        vespalib::nbostream_longlivedbuf is(backingBuffer.getData(), backingBuffer.getDataLen());
        deserialize(repo, is);
        _backingBuffer = std::make_unique<vespalib::DataBuffer>(std::move(backingBuffer));
    }
}

Document::Document(Document &&) noexcept = default;
Document::~Document() noexcept = default;

Document &
Document::operator =(Document &&rhs) noexcept {
    assert( ! _cache && ! rhs._cache);
    _id = std::move(rhs._id);
    _fields = std::move(rhs._fields);
    _backingBuffer = std::move(rhs._backingBuffer);
    _lastModified = rhs._lastModified;
    StructuredFieldValue::operator=(std::move(rhs));
    return *this;
}

Document &
Document::operator =(const Document &rhs) {
    if (this == &rhs) return *this;
    assert( ! _cache && ! rhs._cache);
    _id = rhs._id;
    _fields = rhs._fields;
    _lastModified = rhs._lastModified;
    StructuredFieldValue::operator=(rhs);
    _backingBuffer.reset();
    return *this;
}

const DocumentType&
Document::getType() const {
    return static_cast<const DocumentType &>(StructuredFieldValue::getType());
}

void
Document::clear()
{
    _fields.clear();
}

void
Document::setFieldValue(const Field& field, FieldValue::UP data)
{
    _fields.setFieldValue(field, std::move(data));
}

FieldValue&
Document::assign(const FieldValue& value)
{
    /// \todo TODO (was warning):  This type checking doesnt work with the way assign is used.
//    if (*value.getDataType() == *_type) {
    auto & other(dynamic_cast<const Document&>(value));
    *this = Document(other);
    return *this;
//    }
//    return FieldValue::assign(value); // Generates exception
}

int
Document::compare(const FieldValue& other) const
{
    int diff = StructuredFieldValue::compare(other);
    if (diff != 0) {
        return diff;
    }
    auto & doc(static_cast<const Document&>(other));
    vespalib::string id1 = _id.toString();
    vespalib::string id2 = doc._id.toString();
    if (id1 != id2) {
        return (id1 < id2 ? -1 : 1);
    }
    return _fields.compare(doc._fields);
}

void
Document::print(std::ostream& out, bool verbose,
                const std::string& indent) const
{
    if (!verbose) {
        out << "Document(" << getId() << ", " << getType() << ")";
    } else {
        out << "Document(" << getId() << "\n" << indent << "  ";
        getType().print(out, true, indent + "  ");
        for (const_iterator it = begin(); it != end(); ++it) {
            out << "\n" << indent << "  " << it.field().getName() << ": ";
            getValue(it.field())->print(out, true, indent + "  ");
        }
        out << "\n" << indent << ")";
    }
}

void
Document::printXml(XmlOutputStream& xos) const
{
    xos << XmlTag("document")
        << XmlAttribute("documenttype", getType().getName())
        << XmlAttribute("documentid", getId().toString());
    if (_lastModified != 0) {
        xos << XmlAttribute("lastmodifiedtime", _lastModified);
    }
    _fields.printXml(xos);
    xos << XmlEndTag();
}

std::string
Document::toXml(const std::string& indent) const
{
    std::ostringstream ost;
    XmlOutputStream xos(ost, indent);
    printXml(xos);
    return ost.str();
}

void Document::serializeHeader(nbostream& stream) const {
    VespaDocumentSerializer serializer(stream);
    serializer.write(*this);
}

void Document::deserialize(const DocumentTypeRepo& repo, vespalib::nbostream & os) {
    VespaDocumentDeserializer deserializer(repo, os, 0);
    try {
        deserializer.read(*this);
    } catch (const IllegalStateException &e) {
        throw DeserializeException(vespalib::string("Buffer out of bounds: ") + e.what());
    }
}

void Document::deserialize(const DocumentTypeRepo& repo, vespalib::nbostream & header, vespalib::nbostream & body) {
    deserializeHeader(repo, header);
    deserializeBody(repo, body);
}

void Document::deserializeHeader(const DocumentTypeRepo& repo, vespalib::nbostream & stream) {
    VespaDocumentDeserializer deserializer(repo, stream, 0);
    deserializer.read(*this);
}

void Document::deserializeBody(const DocumentTypeRepo& repo, vespalib::nbostream & stream) {
    VespaDocumentDeserializer deserializer(repo, stream, getFields().getVersion());
    deserializer.readStructNoReset(getFields());
}

StructuredFieldValue::StructuredIterator::UP
Document::getIterator(const Field* first) const
{
    return _fields.getIterator(first);
}

void
Document::beginTransaction() {
    _cache = std::make_unique<StructuredCache>();
}
void
Document::commitTransaction() {
    for (auto & e : *_cache) {
        if (e.second.status == fieldvalue::ModificationStatus::REMOVED) {
            removeFieldValue(e.first);
        } else if (e.second.status == fieldvalue::ModificationStatus::MODIFIED) {
            setFieldValue(e.first, std::move(e.second.value));
        }
    }
    _cache.reset();
}

} // document