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

#include "weightedsetdatatype.h"
#include "mapdatatype.h"
#include <vespa/document/fieldvalue/weightedsetfieldvalue.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <ostream>

namespace document {

namespace {

vespalib::string
createName(const DataType& nestedType, bool create, bool remove)
{
    if (nestedType.getId() == DataType::T_STRING && create && remove) {
        return "Tag";
    }
    vespalib::asciistream ost;
    ost << "WeightedSet<" << nestedType.getName() << ">";
    if (create) {
        ost << ";Add";
    }
    if (remove) {
        ost << ";Remove";
    }
    return ost.str();
}

}

WeightedSetDataType::WeightedSetDataType(const DataType& nested, bool createIfNon, bool remove)
    : CollectionDataType(createName(nested, createIfNon, remove), nested),
      _createIfNonExistent(createIfNon),
      _removeIfZero(remove)
{
}

WeightedSetDataType::WeightedSetDataType(const DataType& nested, bool createIfNon, bool remove, int id)
    : CollectionDataType(createName(nested, createIfNon, remove), nested, id),
      _createIfNonExistent(createIfNon),
      _removeIfZero(remove)
{
}

WeightedSetDataType::~WeightedSetDataType() = default;

FieldValue::UP
WeightedSetDataType::createFieldValue() const
{
    return std::make_unique<WeightedSetFieldValue>(*this);

}

void
WeightedSetDataType::print(std::ostream& out, bool verbose, const std::string& indent) const
{
    if (getNestedType().equals(*DataType::STRING) &&
        _createIfNonExistent && _removeIfZero)
    {
        out << "Tag()";
    } else {
        out << "WeightedSetDataType(";
        getNestedType().print(out, verbose, indent + "    ");
        if (_createIfNonExistent) {
            out << ", autoIfNonExistent";
        }
        if (_removeIfZero) {
            out << ", removeIfZero";
        }
        out << ", id " << getId() << ")";
    }
}

bool
WeightedSetDataType::equals(const DataType& other) const noexcept
{
    if (this == &other) return true;
    if ( ! CollectionDataType::equals(other) || !other.isWeightedSet()) return false;
    const WeightedSetDataType & w(static_cast<const WeightedSetDataType &>(other));
    return (_createIfNonExistent == w._createIfNonExistent) && (_removeIfZero == w._removeIfZero);
}

void
WeightedSetDataType::onBuildFieldPath(FieldPath & path, vespalib::stringref remainFieldName) const
{
    MapDataType::buildFieldPathImpl(path, *this, remainFieldName, getNestedType(), *DataType::INT);
}

} // document