aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/datatype/documenttype.cpp
blob: 441bf2bc9094e1cd4e261562660ec94835414db5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "documenttype.h"
#include <vespa/document/fieldvalue/document.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/log/log.h>
#include <ostream>

LOG_SETUP(".document.datatype.document");

using vespalib::IllegalArgumentException;
using vespalib::make_string;
using vespalib::stringref;


namespace document {

namespace {
FieldCollection build_field_collection(const std::set<vespalib::string> &fields,
                                       const DocumentType &doc_type)
{
    Field::Set::Builder builder;
    for (const auto & field_name : fields) {
        if (doc_type.hasField(field_name)) {
            builder.add(&doc_type.getField(field_name));
        }
    }
    return FieldCollection(doc_type, builder.build());
}
} // namespace <unnamed>

DocumentType::FieldSet::FieldSet(const vespalib::string & name, Fields fields,
                                 const DocumentType & doc_type)
    : _name(name),
      _fields(fields),
      _field_collection(build_field_collection(fields, doc_type))
{}

DocumentType::DocumentType(stringref name, int32_t id)
    : StructuredDataType(name, id),
      _inheritedTypes(),
      _ownedFields(std::make_shared<StructDataType>(name + ".header")),
      _fields(_ownedFields.get()),
      _fieldSets(),
      _imported_field_names()
{
    if (name != "document") {
        _inheritedTypes.push_back(DataType::DOCUMENT);
    }
}

DocumentType::DocumentType(stringref name, int32_t id, const StructDataType& fields)
    : StructuredDataType(name, id),
      _inheritedTypes(),
      _fields(&fields),
      _fieldSets(),
      _imported_field_names()
{
    if (name != "document") {
        _inheritedTypes.push_back(DataType::DOCUMENT);
    }
}

DocumentType::DocumentType(stringref name)
    : StructuredDataType(name),
      _inheritedTypes(),
      _ownedFields(std::make_shared<StructDataType>(name + ".header")),
      _fields(_ownedFields.get()),
      _fieldSets(),
      _imported_field_names()
{
    if (name != "document") {
        _inheritedTypes.emplace_back(DataType::DOCUMENT);
    }
}

DocumentType::DocumentType(stringref name, const StructDataType& fields)
    : StructuredDataType(name),
      _inheritedTypes(),
      _fields(&fields),
      _fieldSets(),
      _imported_field_names()
{
    if (name != "document") {
        _inheritedTypes.emplace_back(DataType::DOCUMENT);
    }
}

DocumentType & DocumentType::operator=(const DocumentType &) = default;
DocumentType::DocumentType(const DocumentType &) = default;
DocumentType::~DocumentType() = default;

DocumentType &
DocumentType::addFieldSet(const vespalib::string & name, FieldSet::Fields fields)
{
    _fieldSets.emplace(name, FieldSet(name, std::move(fields), *this));
    return *this;
}

const DocumentType::FieldSet *
DocumentType::getFieldSet(const vespalib::string & name) const
{
    auto it = _fieldSets.find(name);
    return (it != _fieldSets.end()) ? & it->second : nullptr;
}

void
DocumentType::addField(const Field& field)
{
    if (_fields->hasField(field.getName())) {
        throw IllegalArgumentException( "A field already exists with name " + field.getName(), VESPA_STRLOC);
    } else if (_fields->hasField(field)) {
        throw IllegalArgumentException(make_string("A field already exists with id %i.", field.getId()), VESPA_STRLOC);
    } else if (!_ownedFields.get()) {
        throw vespalib::IllegalStateException(make_string(
                        "Cannot add field %s to a DocumentType that does not "
                        "own its fields.", field.getName().data()), VESPA_STRLOC);
    }
    _ownedFields->addField(field);
}

void
DocumentType::inherit(const DocumentType &docType) {
    if (docType.getName() == "document") {
        return;
    }
    if (docType.isA(*this)) {
        throw IllegalArgumentException(
                "Document type " + docType.toString() + " already inherits type "
                + toString() + ". Cannot add cyclic dependencies.", VESPA_STRLOC);
    }
    // If we already inherits this type, there is no point in adding it again.
    if (isA(docType)) {
        // If we already directly inherits it, complain
        for (const auto* inherited : _inheritedTypes) {
            if (inherited->equals(docType)) {
                throw IllegalArgumentException(
                        "DocumentType " + getName() + " already inherits "
                        "document type " + docType.getName(), VESPA_STRLOC);
            }
        }
        // Indirectly already inheriting it is oki, as this can happen
        // due to inherited documents inheriting the same type.
        LOG(info, "Document type %s inherits document type %s from multiple "
                  "types.", getName().c_str(), docType.getName().c_str());
        return;
    }
    // Add non-conflicting types.
    Field::Set fs = docType._fields->getFieldSet();
    for (const auto* field : fs) {
        if (!_ownedFields.get()) {
            _ownedFields = std::make_shared<StructDataType>(*_fields);
            _fields = _ownedFields.get();
        }
        _ownedFields->addInheritedField(*field);
    }
    // If we inherit default document type Document.0, remove that if adding
    // another parent, as that has to also inherit Document
    if ((_inheritedTypes.size() == 1) && _inheritedTypes[0]->equals(*DataType::DOCUMENT)) {
        _inheritedTypes.clear();
    }
    _inheritedTypes.push_back(&docType);
}

bool
DocumentType::isA(const DataType& other) const
{
    for (const DocumentType * docType : _inheritedTypes) {
        if (docType->isA(other)) return true;
    }
    return equals(other);
}

FieldValue::UP
DocumentType::createFieldValue() const
{
    return Document::make_without_repo(*this, DocumentId("id::" + getName() + "::"));
}

void
DocumentType::print(std::ostream& out, bool verbose, const std::string& indent) const
{
    out << "DocumentType(" << getName();
    if (verbose) {
        out << ", id " << getId();
    }
    out << ")";
    if (verbose) {
        if (!_inheritedTypes.empty()) {
            auto it = _inheritedTypes.begin();
            out << "\n" << indent << "    : ";
            (*it)->print(out, false, "");
            while (++it != _inheritedTypes.end()) {
                out << ",\n" << indent << "      ";
                (*it)->print(out, false, "");
            }
        }
        out << " {\n" << indent << "  ";
        _fields->print(out, verbose, indent + "  ");
        out << "\n" << indent << "}";
    }
}

bool
DocumentType::equals(const DataType& other) const noexcept
{
    if (&other == this) return true;
    if ( ! DataType::equals(other)) return false;
    const auto* o(dynamic_cast<const DocumentType*>(&other));
    if (o == nullptr) return false;
    if ( ! _fields->equals(*o->_fields)) return false;
    if (_inheritedTypes.size() != o->_inheritedTypes.size()) return false;
    auto it1 = _inheritedTypes.begin();
    auto it2 = o->_inheritedTypes.begin();
    while (it1 != _inheritedTypes.end()) {
        if ( ! (*it1)->equals( **it2)) return false;
        ++it1;
        ++it2;
    }
    // TODO imported fields? like in the Java impl, field sets are not considered either... :I
    return true;
}

const Field&
DocumentType::getField(stringref name) const
{
    return _fields->getField(name);
}

const Field&
DocumentType::getField(int fieldId) const
{
    return _fields->getField(fieldId);
}

Field::Set
DocumentType::getFieldSet() const
{
    return _fields->getFieldSet();
}

bool
DocumentType::has_imported_field_name(const vespalib::string& name) const noexcept {
    return (_imported_field_names.find(name) != _imported_field_names.end());
}

void
DocumentType::add_imported_field_name(const vespalib::string& name) {
    _imported_field_names.insert(name);
}

} // document