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

#include "tensor_remove_update.h"
#include "tensor_partial_update.h"
#include <vespa/document/base/exceptions.h>
#include <vespa/document/datatype/tensor_data_type.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/fieldvalue/tensorfieldvalue.h>
#include <vespa/document/serialization/vespadocumentdeserializer.h>
#include <vespa/eval/eval/fast_value.h>
#include <vespa/eval/eval/value.h>
#include <vespa/vespalib/util/xmlstream.h>
#include <ostream>

using vespalib::IllegalArgumentException;
using vespalib::IllegalStateException;
using vespalib::make_string;
using vespalib::eval::Value;
using vespalib::eval::ValueType;
using vespalib::eval::CellType;
using vespalib::eval::FastValueBuilderFactory;

namespace document {

namespace {

std::unique_ptr<const TensorDataType>
convertToCompatibleType(const TensorDataType &tensorType)
{
    std::vector<ValueType::Dimension> list;
    for (const auto &dim : tensorType.getTensorType().dimensions()) {
        if (dim.is_mapped()) {
            list.emplace_back(dim.name);
        }
    }
    return std::make_unique<const TensorDataType>(ValueType::make_type(tensorType.getTensorType().cell_type(), std::move(list)));
}

}

TensorRemoveUpdate::TensorRemoveUpdate()
    : ValueUpdate(TensorRemove),
      TensorUpdate(),
      _tensorType(),
      _tensor()
{
}

TensorRemoveUpdate::TensorRemoveUpdate(std::unique_ptr<TensorFieldValue> tensor)
    : ValueUpdate(TensorRemove),
      TensorUpdate(),
      _tensorType(std::make_unique<TensorDataType>(dynamic_cast<const TensorDataType &>(*tensor->getDataType()))),
      _tensor(std::move(tensor))
{
}

TensorRemoveUpdate::~TensorRemoveUpdate() = default;

bool
TensorRemoveUpdate::operator==(const ValueUpdate &other) const
{
    if (other.getType() != TensorRemove) {
        return false;
    }
    const TensorRemoveUpdate& o(static_cast<const TensorRemoveUpdate&>(other));
    if (*_tensor != *o._tensor) {
        return false;
    }
    return true;
}

void
TensorRemoveUpdate::checkCompatibility(const Field &field) const
{
    if ( ! field.getDataType().isTensor()) {
        throw IllegalArgumentException(make_string("Cannot perform tensor remove update on non-tensor field '%s'",
                                                   field.getName().data()), VESPA_STRLOC);
    }
}

std::unique_ptr<vespalib::eval::Value>
TensorRemoveUpdate::applyTo(const vespalib::eval::Value &tensor) const
{
    return apply_to(tensor, FastValueBuilderFactory::get());
}

std::unique_ptr<vespalib::eval::Value>
TensorRemoveUpdate::apply_to(const Value &old_tensor,
                             const ValueBuilderFactory &factory) const
{
    if (auto addressTensor = _tensor->getAsTensorPtr()) {
        return TensorPartialUpdate::remove(old_tensor, *addressTensor, factory);
    }
    return {};
}

bool
TensorRemoveUpdate::applyTo(FieldValue &value) const
{
    if (value.isA(FieldValue::Type::TENSOR)) {
        TensorFieldValue &tensorFieldValue = static_cast<TensorFieldValue &>(value);
        auto oldTensor = tensorFieldValue.getAsTensorPtr();
        if (oldTensor) {
            auto newTensor = applyTo(*oldTensor);
            if (newTensor) {
                tensorFieldValue = std::move(newTensor);
            }
        }
    } else {
        vespalib::string err = make_string("Unable to perform a tensor remove update on a '%s' field value",
                                           value.className());
        throw IllegalStateException(err, VESPA_STRLOC);
    }
    return true;
}

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

void
TensorRemoveUpdate::print(std::ostream &out, bool verbose, const std::string &indent) const
{
    out << indent << "TensorRemoveUpdate(";
    if (_tensor) {
        _tensor->print(out, verbose, indent);
    }
    out << ")";
}

namespace {

void
verifyAddressTensorIsSparse(const Value *addressTensor)
{
    if (addressTensor == nullptr) {
        throw IllegalStateException("Address tensor is not set", VESPA_STRLOC);
    }
    if (addressTensor->type().is_sparse()) {
        return;
    }
    auto err = make_string("Expected address tensor to be sparse, but has type '%s'",
                           addressTensor->type().to_spec().c_str());
    throw IllegalStateException(err, VESPA_STRLOC);
}

void
verify_tensor_type_dimensions_are_subset_of(const ValueType& lhs_type,
                                            const ValueType& rhs_type)
{
    for (const auto& dim : lhs_type.dimensions()) {
        if (rhs_type.dimension_index(dim.name) == ValueType::Dimension::npos) {
            auto err = make_string("Unexpected type '%s' for address tensor. "
                                   "Expected dimensions to be a subset of '%s'",
                                   lhs_type.to_spec().c_str(), rhs_type.to_spec().c_str());
            throw IllegalStateException(err, VESPA_STRLOC);
        }
    }
}

}

void
TensorRemoveUpdate::deserialize(const DocumentTypeRepo &repo, const DataType &type, nbostream &stream)
{
    VespaDocumentDeserializer deserializer(repo, stream, Document::getNewestSerializationVersion());
    auto tensor = deserializer.readTensor();
    verifyAddressTensorIsSparse(tensor.get());
    auto compatible_type = convertToCompatibleType(dynamic_cast<const TensorDataType &>(type));
    verify_tensor_type_dimensions_are_subset_of(tensor->type(), compatible_type->getTensorType());
    _tensorType = std::make_unique<const TensorDataType>(tensor->type());
    _tensor = std::make_unique<TensorFieldValue>(*_tensorType);
    _tensor->assignDeserialized(std::move(tensor));
}

}