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

#include "stringfieldvalue.h"
#include "literalfieldvalue.hpp"

#include <vespa/document/annotation/spantree.h>
#include <vespa/document/serialization/annotationserializer.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/objects/hexdump.h>
#include <vespa/document/serialization/util.h>
#include <vespa/document/serialization/annotationdeserializer.h>
#include <vespa/document/repo/fixedtyperepo.h>
#include <vespa/document/serialization/vespadocumentserializer.h>
#include <ostream>

using vespalib::nbostream;
using vespalib::ConstBufferRef;
using vespalib::stringref;

namespace document {

StringFieldValue::StringFieldValue(const StringFieldValue & rhs)
    : Parent(rhs),
      _annotationData(rhs.copyAnnotationData())
{
}

StringFieldValue::~StringFieldValue() = default;

StringFieldValue &
StringFieldValue::operator=(const StringFieldValue & rhs)
{
    if (&rhs != this) {
        Parent::operator=(rhs);
        _annotationData = rhs.copyAnnotationData();
    }
    return *this;
}

int
StringFieldValue::compare(const FieldValue& other) const {
    if (other.isA(Type::STRING)) {
        const StringFieldValue &other_s(static_cast<const StringFieldValue &>(other));
        return _value.compare(other_s._value);
    } else {
        return Parent::compare(other);
    }
}

void
StringFieldValue::print(std::ostream& out, bool verbose, const std::string& indent) const {
    if ( ! hasSpanTrees()) {
        Parent::print(out, verbose, indent);
    } else {
        out << "StringFieldValue(\"";
        Parent::print(out, verbose, indent);
        ConstBufferRef buf = getSerializedAnnotations();
        out << "\"\n" << indent << " " << vespalib::HexDump(buf.data(), buf.size());
        if (verbose) {
            out << "\nSpanTree(\n";
            for (const auto & tree: getSpanTrees()) {
                out << "Tree '" << tree->getName() << "':" << tree->toString() << std::endl;
            }
            out << ")\n";
        }
        out << ")";
    }
}

void StringFieldValue::setSpanTrees(ConstBufferRef serialized, const FixedTypeRepo & repo, uint8_t version, bool isSerializedDataLongLived)
{
    _annotationData = std::make_unique<AnnotationData>(serialized, repo, version, isSerializedDataLongLived);
}


void StringFieldValue::setSpanTrees(const SpanTrees & trees, const FixedTypeRepo & repo)
{
    nbostream os;
    putInt1_2_4Bytes(os, trees.size());
    AnnotationSerializer serializer(os);
    for (const auto & tree : trees) {
        serializer.write(*tree);
    }
    setSpanTrees(ConstBufferRef(os.peek(), os.size()), repo, VespaDocumentSerializer::getCurrentVersion(), false);
}
StringFieldValue::SpanTrees StringFieldValue::getSpanTrees() const {
    SpanTrees trees;
    if (hasSpanTrees()) {
        trees = _annotationData->getSpanTrees();
    }
    return trees;
}

void
StringFieldValue::doClearSpanTrees() {
    _annotationData.reset();
}

const SpanTree *
StringFieldValue::findTree(const SpanTrees & trees, stringref name)
{
    for(const auto & tree : trees) {
        if (tree->getName() == name) {
            return tree.get();
        }
    }
    return nullptr;
}

StringFieldValue &
StringFieldValue::operator=(stringref value)
{
    setValue(value);
    _annotationData.reset();
    return *this;
}

FieldValue &
StringFieldValue::assign(const FieldValue & rhs)
{
    if (rhs.isA(Type::STRING)) {
        *this = static_cast<const StringFieldValue &>(rhs);
    } else {
        *this = rhs.getAsString().operator stringref();
    }
    return *this;
}

StringFieldValue::AnnotationData::UP
StringFieldValue::copyAnnotationData() const {
    return hasSpanTrees()
           ? std::make_unique<AnnotationData>(*_annotationData)
           : AnnotationData::UP();
}

StringFieldValue::AnnotationData::AnnotationData(vespalib::ConstBufferRef serialized, const FixedTypeRepo &repo,
                                                 uint8_t version, bool isSerializedDataLongLived)
        : _serialized(serialized),
          _repo(repo.getDocumentTypeRepo()),
          _docType(repo.getDocumentType()),
          _version(version)
{
    if ( ! isSerializedDataLongLived) {
        _backingBlob.assign(serialized.c_str(), serialized.c_str() + serialized.size());
        _serialized = ConstBufferRef(&_backingBlob[0], _backingBlob.size());
    }
}

StringFieldValue::SpanTrees StringFieldValue::AnnotationData::getSpanTrees() const
{
    SpanTrees trees;
    if (hasSpanTrees()) {
        nbostream is(_serialized.data(), _serialized.size());
        size_t tree_count = getInt1_2_4Bytes(is);
        FixedTypeRepo repo(_repo, _docType);
        AnnotationDeserializer deserializer(repo, is, _version);
        for (size_t i = 0; i < tree_count; ++i) {
            trees.emplace_back(deserializer.readSpanTree());
        }
    }
    return trees;
}

StringFieldValue::AnnotationData::AnnotationData(const StringFieldValue::AnnotationData & rhs) :
        AnnotationData(rhs._serialized, FixedTypeRepo(rhs._repo, rhs._docType), rhs._version, false)
{
}

} // document