aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/fieldvalue/stringfieldvalue.h
blob: 7684fa89f531f3ebf84b519c0b8ca3afa03cd012 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class document::StringFieldValue
 * \ingroup fieldvalue
 *
 * \brief Wrapper for string field values.
 */
#pragma once

#include "literalfieldvalue.h"
#include <vespa/document/annotation/spantree.h>
#include <vespa/vespalib/stllike/hash_map.h>
#include <vespa/vespalib/util/buffer.h>

namespace document {

class FixedTypeRepo;
class DocumentTypeRepo;

class StringFieldValue final : public LiteralFieldValue<StringFieldValue, DataType::T_STRING> {
public:
    using Parent = LiteralFieldValue<StringFieldValue, DataType::T_STRING>;
    using SpanTrees = std::vector<SpanTree::UP>;

    StringFieldValue() : Parent(Type::STRING), _annotationData() { }
    StringFieldValue(const vespalib::stringref &value)
            : Parent(Type::STRING, value), _annotationData() { }

    StringFieldValue(const StringFieldValue &rhs);

    StringFieldValue &operator=(const StringFieldValue &rhs);
    StringFieldValue &operator=(vespalib::stringref value) override;
    ~StringFieldValue() override;

    FieldValue &assign(const FieldValue &) override;

    void accept(FieldValueVisitor &visitor) override { visitor.visit(*this); }
    void accept(ConstFieldValueVisitor &visitor) const override { visitor.visit(*this); }
    StringFieldValue *clone() const override { return new StringFieldValue(*this); }
    int compare(const FieldValue &other) const override;
    void print(std::ostream &out, bool verbose, const std::string &indent) const override;
    void setSpanTrees(vespalib::ConstBufferRef serialized, const FixedTypeRepo &repo, uint8_t version,
                      bool isSerializedDataLongLived);
    void setSpanTrees(const SpanTrees &trees, const FixedTypeRepo &repo);
    SpanTrees getSpanTrees() const;
    vespalib::ConstBufferRef getSerializedAnnotations() const {
        return _annotationData ? _annotationData->getSerializedAnnotations() : vespalib::ConstBufferRef();
    }
    bool hasSpanTrees() const { return _annotationData ? _annotationData->hasSpanTrees() : false; }
    static const SpanTree *findTree(const SpanTrees &trees, vespalib::stringref name);
    void clearSpanTrees() {
        if (_annotationData) {
            doClearSpanTrees();
        }
    }

    using LiteralFieldValueB::operator=;
    static std::unique_ptr<StringFieldValue> make(vespalib::stringref value) { return std::make_unique<StringFieldValue>(value); }
    static std::unique_ptr<StringFieldValue> make() { return StringFieldValue::make(""); }
private:
    void doClearSpanTrees();

    class AnnotationData {
    public:
        using BackingBlob = std::vector<char>;
        using UP = std::unique_ptr<AnnotationData>;
        VESPA_DLL_LOCAL AnnotationData(const AnnotationData & rhs);
        AnnotationData & operator = (const AnnotationData &) = delete;
        VESPA_DLL_LOCAL AnnotationData(vespalib::ConstBufferRef serialized, const FixedTypeRepo &repo,
                                       uint8_t version, bool isSerializedDataLongLived);

        [[nodiscard]] bool hasSpanTrees() const { return _serialized.size() > 0u; }
        [[nodiscard]] vespalib::ConstBufferRef getSerializedAnnotations() const { return _serialized; }
        [[nodiscard]] VESPA_DLL_LOCAL SpanTrees getSpanTrees() const;
    private:
        vespalib::ConstBufferRef _serialized;
        BackingBlob              _backingBlob;
        const DocumentTypeRepo  &_repo;
        const DocumentType      &_docType;
        uint8_t                  _version;
    };
    VESPA_DLL_LOCAL AnnotationData::UP copyAnnotationData() const;
    AnnotationData::UP _annotationData;
};

} // document