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

#include "fieldvalue.h"
#include "arrayfieldvalue.h"
#include "intfieldvalue.h"
#include "floatfieldvalue.h"
#include "stringfieldvalue.h"
#include "rawfieldvalue.h"
#include "longfieldvalue.h"
#include "doublefieldvalue.h"
#include "bytefieldvalue.h"
#include "predicatefieldvalue.h"
#include "iteratorhandler.h"
#include <vespa/document/base/exceptions.h>
#include <vespa/document/serialization/vespadocumentserializer.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/polymorphicarrays.h>
#include <vespa/vespalib/util/xmlstream.h>
#include <sstream>

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

namespace document {

using namespace fieldvalue;

const char *
FieldValue::className() const noexcept {
    switch (type()) {
        case Type::BOOL:
            return "BoolFieldValue";
        case Type::BYTE:
            return "ByteFieldValue";
        case Type::SHORT:
            return "ShortFieldValue";
        case Type::INT:
            return "IntFieldValue";
        case Type::LONG:
            return "LongFieldValue";
        case Type::FLOAT:
            return "FloatFieldValue";
        case Type::DOUBLE:
            return "DoubleFieldValue";
        case Type::STRING:
            return "StringFieldValue";
        case Type::RAW:
            return "RawFieldValue";
        case Type::PREDICATE:
            return "PredicateFieldValue";
        case Type::TENSOR:
            return "TensorFieldValue";
        case Type::ANNOTATION_REFERENCE:
            return "AnnotationReferenceFieldValue";
        case Type::REFERENCE:
            return "ReferenceFieldValue";
        case Type::ARRAY:
            return "ArrayFieldValue";
        case Type::WSET:
            return "WSetFieldValue";
        case Type::MAP:
            return "MapFieldValue";
        case Type::STRUCT:
            return "StructFieldValue";
        case Type::DOCUMENT:
            return "DocumentFieldValue";
        case Type::NONE:
        default:
            abort();
    }
}
void FieldValue::serialize(nbostream &stream) const {
    VespaDocumentSerializer serializer(stream);
    serializer.write(*this);
}

nbostream
FieldValue::serialize() const {
    nbostream stream;
    serialize(stream);
    return stream;
}

size_t
FieldValue::hash() const
{
    vespalib::nbostream os;
    serialize(os);
    return vespalib::hashValue(os.data(), os.size()) ;
}

int
FieldValue::compare(const FieldValue& other) const {
    return getDataType()->cmpId(*other.getDataType());
}

int
FieldValue::fastCompare(const FieldValue& other) const {
    return compare(other);
}

FieldValue&
FieldValue::assign(const FieldValue& value)
{
    throw IllegalArgumentException(
            "Cannot assign value of type " + value.getDataType()->toString()
            + " to value of type " + value.getDataType()->toString(), VESPA_STRLOC);
}

/**
 * Normally, tag names are decided by the parent node, so children will not
 * print their parents. For toXML to work on non-root (non-document) nodes
 * though, we've overwritten toXml to write a start tag for leaf nodes, if
 * they are used as root nodes in toXml call.
 */
std::string
FieldValue::toXml(const std::string& indent) const
{
    std::ostringstream ost;
    XmlOutputStream xos(ost, indent);
    xos << XmlTag("value");
    printXml(xos);
    xos << XmlEndTag();
    return ost.str();
}

// Subtypes should implement the conversion functions that make sense

FieldValue&
FieldValue::operator=(vespalib::stringref)
{
    throw IllegalArgumentException("Cannot assign string to datatype " + getDataType()->toString(), VESPA_STRLOC);
}

char
FieldValue::getAsByte() const
{
    throw InvalidDataTypeConversionException(*getDataType(), *DataType::BYTE, VESPA_STRLOC);
}

int32_t
FieldValue::getAsInt() const
{
    throw InvalidDataTypeConversionException(*getDataType(), *DataType::INT, VESPA_STRLOC);
}

int64_t
FieldValue::getAsLong() const
{
    throw InvalidDataTypeConversionException(*getDataType(), *DataType::LONG, VESPA_STRLOC);
}

float
FieldValue::getAsFloat() const
{
    throw InvalidDataTypeConversionException(*getDataType(), *DataType::FLOAT, VESPA_STRLOC);
}

double
FieldValue::getAsDouble() const
{
    throw InvalidDataTypeConversionException(*getDataType(), *DataType::DOUBLE, VESPA_STRLOC);
}

vespalib::string
FieldValue::getAsString() const
{
    throw InvalidDataTypeConversionException(*getDataType(), *DataType::STRING, VESPA_STRLOC);
}

std::pair<const char*, size_t>
FieldValue::getAsRaw() const
{
    throw InvalidDataTypeConversionException(*getDataType(), *DataType::RAW, VESPA_STRLOC);
}

FieldValue::UP
FieldValue::getNestedFieldValue(PathRange nested) const
{
    return ( ! nested.atEnd() ) ? onGetNestedFieldValue(nested) : FieldValue::UP();
}

FieldValue::UP
FieldValue::onGetNestedFieldValue(PathRange nested) const
{
    (void) nested;
    return FieldValue::UP();
}

ModificationStatus
FieldValue::iterateNested(PathRange nested, IteratorHandler & handler) const
{
    return onIterateNested(nested, handler);
}

ModificationStatus
FieldValue::onIterateNested(PathRange nested, IteratorHandler & handler) const
{
    if (nested.atEnd()) {
        handler.handlePrimitive(-1, *this);
        return handler.modify(const_cast<FieldValue&>(*this));
    } else {
        throw vespalib::IllegalArgumentException("Primitive types can't be iterated through");
    }
}

std::string
FieldValue::toString(bool verbose, const std::string& indent) const
{
    std::ostringstream o;
    print(o, verbose, indent);
    return o.str();
}

using vespalib::ComplexArrayT;
using vespalib::PrimitiveArrayT;

namespace {

class FieldValueFactory : public ComplexArrayT<FieldValue>::Factory
{
public:
    FieldValueFactory(const DataType & dataType) : _dataType(&dataType) { }
    FieldValue * create() override { return _dataType->createFieldValue().release(); }
    FieldValueFactory * clone() const override { return new FieldValueFactory(*this); }
private:
    const DataType * _dataType;
};

}

std::unique_ptr<vespalib::IArrayBase>
FieldValue::createArray(const DataType & baseType)
{
    switch(baseType.getId()) {
    case DataType::T_INT:
        return std::make_unique<PrimitiveArrayT<IntFieldValue, FieldValue>>();
    case DataType::T_FLOAT:
        return std::make_unique<PrimitiveArrayT<FloatFieldValue, FieldValue>>();
    case DataType::T_STRING:
        return std::make_unique<PrimitiveArrayT<StringFieldValue, FieldValue>>();
    case DataType::T_RAW:
        return std::make_unique<PrimitiveArrayT<RawFieldValue, FieldValue>>();
    case DataType::T_LONG:
        return std::make_unique<PrimitiveArrayT<LongFieldValue, FieldValue>>();
    case DataType::T_DOUBLE:
        return std::make_unique<PrimitiveArrayT<DoubleFieldValue, FieldValue>>();
    case DataType::T_BYTE:
        return std::make_unique<PrimitiveArrayT<ByteFieldValue, FieldValue>>();
    default:
        return std::make_unique<ComplexArrayT<FieldValue>>(std::make_unique<FieldValueFactory>(baseType));
    }
}

std::ostream& operator<<(std::ostream& out, const FieldValue & p) {
    p.print(out, false, "");
    return out;
}

XmlOutputStream & operator<<(XmlOutputStream & out, const FieldValue & p) {
    p.printXml(out);
    return out;
}

} // document