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

#include "weightedsetfieldvalue.h"
#include <vespa/document/datatype/weightedsetdatatype.h>
#include <vespa/document/datatype/mapdatatype.h>
#include <vespa/document/base/exceptions.h>
#include <vespa/vespalib/util/xmlstream.h>
#include <ostream>

using vespalib::Identifiable;
using vespalib::IllegalArgumentException;
using vespalib::IllegalStateException;
using namespace vespalib::xml;

/// \todo TODO (was warning):  Find a way to search through internal map without duplicating keys to create shared pointers.

namespace document {

using namespace fieldvalue;

namespace {
const DataType &getKeyType(const DataType &type) {
    const WeightedSetDataType *wtype = dynamic_cast<const WeightedSetDataType *>(&type);
    if (!wtype) {
        throw IllegalArgumentException("Cannot generate a weighted set value with non-weighted set "
                                       "type " + type.toString() + ".", VESPA_STRLOC);
    }
    return wtype->getNestedType();
}
}  // namespace

WeightedSetFieldValue::WeightedSetFieldValue(const DataType &type)
    : CollectionFieldValue(Type::WSET, type),
      _map_type(std::make_shared<MapDataType>(getKeyType(type), *DataType::INT)),
      _map(*_map_type)
{ }

WeightedSetFieldValue::WeightedSetFieldValue(const WeightedSetFieldValue &) = default;
WeightedSetFieldValue & WeightedSetFieldValue::operator = (const WeightedSetFieldValue &) = default;
WeightedSetFieldValue::~WeightedSetFieldValue() = default;

void WeightedSetFieldValue::verifyKey(const FieldValue & v)
{
    if (!getNestedType().isValueType(v)) {
        throw InvalidDataTypeException(*v.getDataType(), getNestedType(), VESPA_STRLOC);
    }
}

bool
WeightedSetFieldValue::add(const FieldValue& key, int weight)
{
    verifyKey(key);
    const WeightedSetDataType & wdt(static_cast<const WeightedSetDataType&>(*_type));
    if (wdt.removeIfZero() && (weight == 0)) {
        _map.erase(key);
        return false;
    }
    return _map.insert(FieldValue::UP(key.clone()), IntFieldValue::make(weight));
}

bool
WeightedSetFieldValue::addIgnoreZeroWeight(const FieldValue& key, int32_t weight)
{
    verifyKey(key);
    return _map.insert(FieldValue::UP(key.clone()), IntFieldValue::make(weight));
}

void
WeightedSetFieldValue::push_back(FieldValue::UP key, int weight)
{
    _map.push_back(std::move(key), IntFieldValue::make(weight));
}

void
WeightedSetFieldValue::increment(const FieldValue& key, int val)
{
    verifyKey(key);
    WeightedFieldValueMap::iterator it(_map.find(key));
    const WeightedSetDataType & wdt(static_cast<const WeightedSetDataType&>(*_type));
    if (wdt.createIfNonExistent()) {
        if (it == _map.end()) {
            _map.insert(FieldValue::UP(key.clone()), FieldValue::UP(new IntFieldValue(val)));
        } else {
            IntFieldValue& fv = static_cast<IntFieldValue&>(*it->second);
            fv.setValue(fv.getValue() + val);
            if (wdt.removeIfZero() && fv.getValue() == 0) {
                _map.erase(key);
            }
        }
    } else {
        if (it == _map.end()) {
           throw IllegalStateException("Cannot modify non-existing entry in weightedset without createIfNonExistent set", VESPA_STRLOC);
        }
        IntFieldValue& fv = static_cast<IntFieldValue&>(*it->second);
        fv.setValue(fv.getValue() + val);
        if (wdt.removeIfZero() && fv.getValue() == 0) {
            _map.erase(key);
        }
    }
}

int32_t
WeightedSetFieldValue::get(const FieldValue& key, int32_t defaultValue) const
{
    WeightedFieldValueMap::const_iterator it = find(key);
    return (it == end()
            ? defaultValue
            : static_cast<const IntFieldValue&>(*it->second).getValue());
}

bool
WeightedSetFieldValue::containsValue(const FieldValue& key) const
{
    return _map.contains(key);
}

bool
WeightedSetFieldValue::removeValue(const FieldValue& key)
{
    bool result = _map.erase(key);
    return result;
}

FieldValue&
WeightedSetFieldValue::assign(const FieldValue& value)
{
    if (getDataType()->isValueType(value)) {
        return operator=(static_cast<const WeightedSetFieldValue&>(value));
    }
    return FieldValue::assign(value);
}

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

    const WeightedSetFieldValue& wset(dynamic_cast<const WeightedSetFieldValue&>(other));
    return _map.compare(wset._map);
}

void
WeightedSetFieldValue::printXml(XmlOutputStream& xos) const
{
    for (const auto & entry : _map) {

        const IntFieldValue& fv = static_cast<const IntFieldValue&>(*entry.second);
        xos << XmlTag("item") << XmlAttribute("weight", fv.getValue())
            << *entry.first
            << XmlEndTag();
    }
}

void
WeightedSetFieldValue::print(std::ostream& out, bool verbose, const std::string& indent) const
{
    out << getDataType()->getName() << "(";

    int count = 0;
    for (const auto & entry : _map) {
        if (count++ != 0) {
            out << ",";
        }
        out << "\n" << indent << "  ";
        entry.first->print(out, verbose, indent + "  ");
        const IntFieldValue& fv = static_cast<const IntFieldValue&>(*entry.second);
        out << " - weight " << fv.getValue();
    }
    if (_map.size() > 0) out << "\n" << indent;
    out << ")";
}

WeightedSetFieldValue::const_iterator
WeightedSetFieldValue::find(const FieldValue& key) const
{
    return _map.find(key);
}

WeightedSetFieldValue::iterator
WeightedSetFieldValue::find(const FieldValue& key)
{
    return _map.find(key);
}

ModificationStatus
WeightedSetFieldValue::onIterateNested(PathRange nested, IteratorHandler & handler) const
{
    return _map.iterateNestedImpl(nested, handler, *this);
}

} // document