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

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

namespace document {

namespace {
vespalib::string createName(const DataType& keyType, const DataType& valueType)
{
    vespalib::asciistream ost;
    ost << "Map<" << keyType.getName() << "," << valueType.getName() << ">";
    return ost.str();
}
}  // namespace

MapDataType::MapDataType(const DataType &key, const DataType &value) noexcept
    : DataType(createName(key, value)),
      _keyType(&key),
      _valueType(&value) {
}

MapDataType::MapDataType(const DataType &key, const DataType &value, int id) noexcept
    : DataType(createName(key, value), id),
      _keyType(&key),
      _valueType(&value) {
}

MapDataType::~MapDataType() = default;

FieldValue::UP MapDataType::createFieldValue() const {
    return std::make_unique<MapFieldValue>(*this);
}

void
MapDataType::print(std::ostream& out, bool verbose,
                   const std::string& indent) const
{
    out << "MapDataType(";
    getKeyType().print(out, verbose, indent + "    ");
    out << ", ";
    getValueType().print(out, verbose, indent + "    ");
    out << ", id " << getId() << ")";
}

bool
MapDataType::equals(const DataType& other) const noexcept
{
    if (this == &other) return true;
    if (!DataType::equals(other)) return false;
    const MapDataType * w = other.cast_map();
    return w && _keyType->equals(*w->_keyType) && _valueType->equals(*w->_valueType);
}

void
MapDataType::buildFieldPathImpl(FieldPath & path, const DataType &dataType,
                                vespalib::stringref remainFieldName,
                                const DataType &keyType, const DataType &valueType)
{
    if (!remainFieldName.empty() && remainFieldName[0] == '{') {
        vespalib::stringref rest = remainFieldName;
        vespalib::string keyValue = FieldPathEntry::parseKey(rest);

        valueType.buildFieldPath(path, (rest[0] == '.') ? rest.substr(1) : rest);

        if (remainFieldName[1] == '$') {
            path.insert(path.begin(), std::make_unique<FieldPathEntry>(valueType, keyValue.substr(1)));
        } else {
            FieldValue::UP fv = keyType.createFieldValue();
            *fv = keyValue;
            path.insert(path.begin(), std::make_unique<FieldPathEntry>(valueType, dataType, std::move(fv)));
        }
    } else if (memcmp(remainFieldName.data(), "key", 3) == 0) {
        size_t endPos = 3;
        if (remainFieldName[endPos] == '.') {
            endPos++;
        }

        keyType.buildFieldPath(path, remainFieldName.substr(endPos));

        path.insert(path.begin(), std::make_unique<FieldPathEntry>(dataType, keyType, valueType, true, false));
    } else if (memcmp(remainFieldName.data(), "value", 5) == 0) {
        size_t endPos = 5;
        if (remainFieldName[endPos] == '.') {
            endPos++;
        }

        valueType.buildFieldPath(path, remainFieldName.substr(endPos));

        path.insert(path.begin(), std::make_unique<FieldPathEntry>(dataType, keyType, valueType, false, true));
    } else {
        keyType.buildFieldPath(path, remainFieldName);
    }
}

void
MapDataType::onBuildFieldPath(FieldPath & fieldPath, vespalib::stringref remainFieldName) const
{
    buildFieldPathImpl(fieldPath, *this, remainFieldName, getKeyType(), getValueType());
}

}  // namespace document