aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/fieldvalue/arrayfieldvalue.cpp
blob: 51bc42d2ceedc0ea6279878e4f965546be891db6 (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
267
268
269
270
271
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "arrayfieldvalue.h"
#include "intfieldvalue.h"
#include "stringfieldvalue.h"
#include "predicatefieldvalue.h"
#include "iteratorhandler.h"
#include <vespa/document/util/serializableexceptions.h>
#include <vespa/vespalib/util/polymorphicarrays.h>
#include <vespa/vespalib/util/xmlstream.h>
#include <vespa/log/log.h>
#include <ostream>

LOG_SETUP(".document.fieldvalue.array");

using namespace vespalib::xml;
using vespalib::make_string;

namespace document {

using vespalib::IllegalArgumentException;
using fieldvalue::IndexValue;
using fieldvalue::ModificationStatus;
using fieldvalue::IteratorHandler;
using fieldvalue::VariableMap;

ArrayFieldValue::ArrayFieldValue(const DataType &type)
    : CollectionFieldValue(Type::ARRAY, type),
      _array()
{
    _array.reset(static_cast<IArray *>(createArray(getNestedType()).release()));
}

ArrayFieldValue::ArrayFieldValue(const ArrayFieldValue& other)
    : CollectionFieldValue(other),
      _array(other._array->clone())
{
}

ArrayFieldValue::~ArrayFieldValue() = default;

ArrayFieldValue&
ArrayFieldValue::operator=(const ArrayFieldValue& other)
{
    if (this != &other) {
        verifyType(other);
        ArrayFieldValue copy(other);
        swap(copy);
    }
    return *this;
}

void
ArrayFieldValue::remove(uint32_t index)
{
    if (_array->size() <= index) {
        throw IllegalArgumentException(make_string(
                "Cannot remove index %u from an array of size %lu.",
                index, (unsigned long)_array->size()), VESPA_STRLOC);
    }
    _array->erase(array().begin() + index);
}

bool
ArrayFieldValue::addValue(const FieldValue& value)
{
    if (getNestedType().isValueType(value)) {
        _array->push_back(value);
    } else {
        throw IllegalArgumentException(make_string(
                "Cannot add value of type %s to array containing type %s.",
                value.getDataType()->toString().c_str(),
                getNestedType().toString().c_str()), VESPA_STRLOC);
    }
    return true;
}

bool
ArrayFieldValue::containsValue(const FieldValue& value) const
{
    if (getNestedType().isValueType(value)) {
        for (IArray::const_iterator it(array().begin()), mt(array().end()); it != mt; ++it) {
            if (*it == value) {
                return true;
            }
        }
        return false;
    } else {
        throw IllegalArgumentException(make_string(
                "Value of type %s can't possibly be in array of type %s.",
                value.getDataType()->toString().c_str(),
                getDataType()->toString().c_str()), VESPA_STRLOC);
    }
}

bool
ArrayFieldValue::removeValue(const FieldValue& value)
{
    if (getNestedType().isValueType(value)) {
        size_t oldSize = _array->size();
        IArray::iterator it = array().begin();
        while (it != array().end()) {
            if (*it == value) {
                it = _array->erase(it);
            } else {
                ++it;
            }
        }
        return (oldSize != _array->size());
    } else {
        throw IllegalArgumentException(make_string(
                "Value of type %s can't possibly be in array of type %s.",
                value.getDataType()->toString().c_str(),
                getDataType()->toString().c_str()), VESPA_STRLOC);
    }
}

FieldValue&
ArrayFieldValue::assign(const FieldValue& value)
{
    if (value.getDataType()->equals(*getDataType())) {
        const ArrayFieldValue& val(static_cast<const ArrayFieldValue&>(value));
        operator=(val);
        return *this;
    } else {
        return FieldValue::assign(value);
    }
}

int
ArrayFieldValue::compare(const FieldValue& o) const
{
    int diff = CollectionFieldValue::compare(o);
    if (diff != 0) return diff;

    const ArrayFieldValue& other(static_cast<const ArrayFieldValue&>(o));

    if (size() != other.size()) return (size() - other.size());
    for (uint32_t i=0, n=size(); i<n; ++i) {
        diff = array()[i].compare(other.array()[i]);
        if (diff != 0) return diff;
    }
    return 0;
}

void
ArrayFieldValue::printXml(XmlOutputStream& xos) const
{
    for (uint32_t i=0, n=_array->size(); i<n; ++i) {
        xos << XmlTag("item");
        array()[i].printXml(xos);
        xos << XmlEndTag();
    }
}

void
ArrayFieldValue::print(std::ostream& out, bool verbose,
                       const std::string& indent) const
{
    out << "Array(size: " << _array->size();
    try {
        for (uint32_t i=0, n=_array->size(); i<n; ++i) {
            out << ",\n" << indent << "  ";
            array()[i].print(out, verbose, indent + "  ");
        }
    } catch (const DeserializeException & e) {
        out << ",\n" << indent << "(Deserialization failed)";
    }
    out << "\n" << indent << ")";
}

fieldvalue::ModificationStatus
ArrayFieldValue::iterateSubset(int startPos, int endPos,
                               vespalib::stringref variable,
                               PathRange nested,
                               fieldvalue::IteratorHandler& handler) const
{
    fieldvalue::ModificationStatus retVal = ModificationStatus::NOT_MODIFIED;

    std::vector<int> indicesToRemove;

    for (int i = startPos; i <= endPos && i < static_cast<int>(_array->size()); ++i) {
        handler.setArrayIndex(i);
        if (!variable.empty()) {
            handler.getVariables()[variable] = IndexValue(i);
        }

        ModificationStatus status = array()[i].iterateNested(nested, handler);

        if (status == ModificationStatus::REMOVED) {
            indicesToRemove.push_back(i);
            retVal = ModificationStatus::MODIFIED;
        } else if (status == ModificationStatus::MODIFIED) {
            retVal = status;
        }
    }

    if (!variable.empty()) {
        handler.getVariables().erase(variable);
    }

    for (auto i = indicesToRemove.rbegin(); i != indicesToRemove.rend(); ++i) {
        const_cast<ArrayFieldValue&>(*this).remove(*i);
    }

    return retVal;
}

fieldvalue::ModificationStatus
ArrayFieldValue::onIterateNested(PathRange nested, IteratorHandler & handler) const
{
    IteratorHandler::CollectionScope autoScope(handler, *this);
    LOG(spam, "iterating over ArrayFieldValue %s", toString().c_str());

    if (! nested.atEnd()) {
        const FieldPathEntry & fpe = nested.cur();
        switch (fpe.getType()) {
        case FieldPathEntry::ARRAY_INDEX: {
            LOG(spam, "ARRAY_INDEX");
            return iterateSubset(fpe.getIndex(), fpe.getIndex(), "", nested.next(), handler);
        }
        case FieldPathEntry::VARIABLE:
        {
            LOG(spam, "VARIABLE");
            VariableMap::iterator iter = handler.getVariables().find(fpe.getVariableName());
            if (iter != handler.getVariables().end()) {
                int idx = iter->second.index;

                if (idx == -1) {
                    throw IllegalArgumentException(
                            "Mismatch between variables - trying to iterate through map "
                            "and array with the same variable.");
                }

                if (idx < (int)_array->size()) {
                    return iterateSubset(idx, idx, "", nested.next(), handler);
                } else {
                    return ModificationStatus::NOT_MODIFIED;
                }
            } else {
                return iterateSubset(0, static_cast<int>(_array->size()) - 1,
                                     fpe.getVariableName(), nested.next(), handler);
            }
            break;
        }
        default:
            break;
        }
        return iterateSubset(0, static_cast<int>(_array->size()) - 1, "", nested, handler);
    } else {
        ModificationStatus status = handler.modify(const_cast<ArrayFieldValue&>(*this));

        if (status == ModificationStatus::REMOVED) {
            return status;
        }

        if (handler.handleComplex(*this)) {
            if (iterateSubset(0, static_cast<int>(_array->size()) - 1, "", nested, handler)
                != ModificationStatus::NOT_MODIFIED)
            {
                status = ModificationStatus::MODIFIED;
            }
        }

        return status;
    }
}

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

} // document