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

#include "tensorfieldvalue.h"
#include <vespa/document/base/exceptions.h>
#include <vespa/document/datatype/tensor_data_type.h>
#include <vespa/vespalib/util/xmlstream.h>
#include <vespa/eval/eval/fast_value.h>
#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/eval/eval/value.h>
#include <ostream>

using vespalib::eval::FastValueBuilderFactory;
using vespalib::eval::TensorSpec;
using vespalib::eval::ValueType;
using namespace vespalib::xml;

namespace document {

namespace {

TensorDataType emptyTensorDataType(ValueType::error_type());

vespalib::string makeWrongTensorTypeMsg(const ValueType &fieldTensorType, const ValueType &tensorType)
{
    return vespalib::make_string("Field tensor type is '%s' but other tensor type is '%s'",
                                 fieldTensorType.to_spec().c_str(),
                                 tensorType.to_spec().c_str());
}

}

TensorFieldValue::TensorFieldValue()
    : TensorFieldValue(emptyTensorDataType)
{
}

TensorFieldValue::TensorFieldValue(const TensorDataType &dataType)
    : FieldValue(Type::TENSOR),
      _dataType(dataType),
      _tensor()
{
}

TensorFieldValue::TensorFieldValue(const TensorFieldValue &rhs)
    : FieldValue(Type::TENSOR),
      _dataType(rhs._dataType),
      _tensor()
{
    if (rhs._tensor) {
        _tensor = FastValueBuilderFactory::get().copy(*rhs._tensor);
    }
}


TensorFieldValue::TensorFieldValue(TensorFieldValue &&rhs)
    : FieldValue(Type::TENSOR),
      _dataType(rhs._dataType),
      _tensor()
{
    _tensor = std::move(rhs._tensor);
}


TensorFieldValue::~TensorFieldValue()
{
}


TensorFieldValue &
TensorFieldValue::operator=(const TensorFieldValue &rhs)
{
    if (this != &rhs) {
        if (&_dataType == &rhs._dataType || !rhs._tensor ||
            _dataType.isAssignableType(rhs._tensor->type())) {
            if (rhs._tensor) {
                _tensor = FastValueBuilderFactory::get().copy(*rhs._tensor);
            } else {
                _tensor.reset();
            }
        } else {
            throw WrongTensorTypeException(makeWrongTensorTypeMsg(_dataType.getTensorType(), rhs._tensor->type()), VESPA_STRLOC);
        }
    }
    return *this;
}


TensorFieldValue &
TensorFieldValue::operator=(std::unique_ptr<vespalib::eval::Value> rhs)
{
    if (!rhs || _dataType.isAssignableType(rhs->type())) {
        _tensor = std::move(rhs);
    } else {
        throw WrongTensorTypeException(makeWrongTensorTypeMsg(_dataType.getTensorType(), rhs->type()), VESPA_STRLOC);
    }
    return *this;
}


void
TensorFieldValue::make_empty_if_not_existing()
{
    if (!_tensor) {
        TensorSpec empty_spec(_dataType.getTensorType().to_spec());
        _tensor = value_from_spec(empty_spec, FastValueBuilderFactory::get());
    }
}


void
TensorFieldValue::accept(FieldValueVisitor &visitor)
{
    visitor.visit(*this);
}


void
TensorFieldValue::accept(ConstFieldValueVisitor &visitor) const
{
    visitor.visit(*this);
}


const DataType *
TensorFieldValue::getDataType() const
{
    return &_dataType;
}

TensorFieldValue*
TensorFieldValue::clone() const
{
    return new TensorFieldValue(*this);
}


void
TensorFieldValue::print(std::ostream& out, bool verbose,
                        const std::string& indent) const
{
    (void) verbose;
    (void) indent;
    out << "{TensorFieldValue: ";
    if (_tensor) {
        out << spec_from_value(*_tensor).to_string();
    } else {
        out << "null";
    }
    out << "}";
}

void
TensorFieldValue::printXml(XmlOutputStream& out) const
{
    out << "{TensorFieldValue::printXml not yet implemented}";
}


FieldValue &
TensorFieldValue::assign(const FieldValue &value)
{
    if (value.isA(Type::TENSOR)) {
        const auto * rhs = static_cast<const TensorFieldValue *>(&value);
        *this = *rhs;
    } else {
        return FieldValue::assign(value);
    }
    return *this;
}


void
TensorFieldValue::assignDeserialized(std::unique_ptr<vespalib::eval::Value> rhs)
{
    if (!rhs || _dataType.isAssignableType(rhs->type())) {
        _tensor = std::move(rhs);
    } else {
        throw WrongTensorTypeException(makeWrongTensorTypeMsg(_dataType.getTensorType(), rhs->type()), VESPA_STRLOC);
    }
}


int
TensorFieldValue::compare(const FieldValue &other) const
{
    if (this == &other) {
        return 0;	// same identity
    }
    int diff = FieldValue::compare(other);
    if (diff != 0) {
        return diff;    // field type mismatch
    }
    const TensorFieldValue & rhs(static_cast<const TensorFieldValue &>(other));
    if (!_tensor) {
        return (rhs._tensor ? -1 : 0);
    }
    if (!rhs._tensor) {
        return 1;
    }
    // equal pointers always means identical
    if (_tensor.get() == rhs._tensor.get()) {
        return 0;
    }
    // compare just the type first:
    auto lhs_type = _tensor->type().to_spec();
    auto rhs_type = rhs._tensor->type().to_spec();
    int type_cmp = lhs_type.compare(rhs_type);
    if (type_cmp != 0) {
        return type_cmp;
    }
    // Compare the actual tensors by converting to TensorSpec strings.
    // TODO: this can be very slow, check if it might be used for anything
    // performance-critical.
    auto lhs_spec = spec_from_value(*_tensor).to_string();
    auto rhs_spec = spec_from_value(*rhs._tensor).to_string();
    return lhs_spec.compare(rhs_spec);
}

} // document