aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/update/fieldpathupdate.cpp
blob: 1f919be1bbe10bbe792b4121c4542a7b344b5aaa (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "fieldpathupdates.h"
#include <vespa/document/datatype/datatype.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/fieldvalue/iteratorhandler.h>
#include <vespa/document/select/constant.h>
#include <vespa/document/select/parser.h>
#include <vespa/document/select/parsing_failed_exception.h>
#include <vespa/document/util/serializableexceptions.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <ostream>

#include <vespa/log/log.h>
LOG_SETUP(".document.update.fieldpathupdate");

using document::select::ParsingFailedException;
using vespalib::make_string;
using vespalib::IllegalArgumentException;

namespace document {

using namespace fieldvalue;

namespace {

std::unique_ptr<select::Node>
parseDocumentSelection(vespalib::stringref query, const DocumentTypeRepo& repo)
{
    BucketIdFactory factory;
    try {
        select::Parser parser(repo, factory);
        return parser.parse(query);
    } catch (const ParsingFailedException &e) {
        LOG(warning, "Failed to parse selection for field path update: %s", e.getMessage().c_str());
        return std::make_unique<select::Constant>(false);
    }
}

}  // namespace

FieldPathUpdate::FieldPathUpdate(FieldPathUpdateType type) noexcept
    : _type(type),
      _originalFieldPath(),
      _originalWhereClause()
{ }

FieldPathUpdate::FieldPathUpdate(const FieldPathUpdate &) = default;
FieldPathUpdate & FieldPathUpdate::operator =(const FieldPathUpdate &) = default;

FieldPathUpdate::FieldPathUpdate(FieldPathUpdateType type, stringref fieldPath, stringref whereClause)
    : _type(type),
      _originalFieldPath(fieldPath),
      _originalWhereClause(whereClause)
{ }

FieldPathUpdate::~FieldPathUpdate() = default;

bool
FieldPathUpdate::operator==(const FieldPathUpdate& other) const
{
    return (_type == other._type)
            && (other._originalFieldPath == _originalFieldPath)
            && (other._originalWhereClause == _originalWhereClause);
}

void
FieldPathUpdate::applyTo(Document& doc) const
{
    std::unique_ptr<IteratorHandler> handler(getIteratorHandler(doc, *doc.getRepo()));

    FieldPath path;
    doc.getDataType()->buildFieldPath(path, _originalFieldPath);
    if (_originalWhereClause.empty()) {
        doc.iterateNested(path, *handler);
    } else {
        std::unique_ptr<select::Node> whereClause = parseDocumentSelection(_originalWhereClause, *doc.getRepo());
        select::ResultList results = whereClause->contains(doc);
        for (auto i = results.rbegin(); i != results.rend(); ++i) {
            LOG(spam, "vars = %s", handler->getVariables().toString().c_str());
            if (*i->second == select::Result::True) {
                handler->setVariables(std::move(i->first));
                doc.iterateNested(path, *handler);
            }
        }
    }
}

void
FieldPathUpdate::print(std::ostream& out, bool, const std::string& indent) const
{
    out << indent << "fieldPath='" << _originalFieldPath << "',\n"
        << indent << "whereClause='" << _originalWhereClause << "'";
}

void
FieldPathUpdate::checkCompatibility(const FieldValue& fv, const DataType & type) const
{
    FieldPath path;
    type.buildFieldPath(path, _originalFieldPath);
    if ( !getResultingDataType(path).isValueType(fv)) {
        throw IllegalArgumentException(
                make_string("Cannot update a '%s' field with a '%s' value",
                            getResultingDataType(path).toString().c_str(),
                            fv.getDataType()->toString().c_str()),
                VESPA_STRLOC);
    }
}

const DataType&
FieldPathUpdate::getResultingDataType(const FieldPath & path) const
{
    if (path.empty()) {
        throw vespalib::IllegalStateException("Cannot get resulting data type from an empty field path", VESPA_STRLOC);
    }
    return path.back().getDataType();
}

vespalib::stringref
FieldPathUpdate::getString(nbostream & stream)
{
    uint32_t sz(0);
    stream >> sz;

    vespalib::stringref s(stream.peek(), sz - 1);
    stream.adjustReadPos(sz);
    return s;
}

void
FieldPathUpdate::deserialize(const DocumentTypeRepo&, const DataType&, nbostream & stream)
{
    _originalFieldPath = getString(stream);
    _originalWhereClause = getString(stream);
}

std::unique_ptr<FieldPathUpdate>
FieldPathUpdate::createInstance(const DocumentTypeRepo& repo, const DataType &type, nbostream& stream)
{
    unsigned char updateType = 0;
    stream >> updateType;

    std::unique_ptr<FieldPathUpdate> update;
    switch (updateType) {
    case 0:
        update.reset(new AssignFieldPathUpdate());
        break;
    case 1:
        update.reset(new RemoveFieldPathUpdate());
        break;
    case 2:
        update.reset(new AddFieldPathUpdate());
        break;
    default:
        throw DeserializeException(make_string("Unknown fieldpath update type: %d", updateType), VESPA_STRLOC);
    }
    update->deserialize(repo, type, stream);
    return update;
}

}