aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/update/fieldupdate.h
blob: e8e83ab3e48aca053f572f74b72fb47bf1e54f47 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class document::FieldUpdate
 * \ingroup update
 *
 * \brief Represent a collection of updates to be performed on a single
 * field in a document.
 *
 * It inherits from Printable to produce human readable output when required.
 * Serialization is done through Serializable and XmlSerializable.
 * Deserialization is specially handled as document type is not serialized with
 * the object.
 */
#pragma once

#include "valueupdate.h"
#include <vespa/document/base/field.h>

namespace document {

class Document;
class DocumentType;

class FieldUpdate
{
public:
    using nbostream = vespalib::nbostream;
    using ValueUpdates = std::vector<std::unique_ptr<ValueUpdate>>;
    using XmlOutputStream = vespalib::xml::XmlOutputStream;

    FieldUpdate(const Field& field);
    FieldUpdate(const FieldUpdate &) = delete;
    FieldUpdate & operator = (const FieldUpdate &) = delete;
    FieldUpdate(FieldUpdate &&) = default;
    FieldUpdate & operator = (FieldUpdate &&) = default;
    ~FieldUpdate();

    /**
     * This is a convenience function to construct a field update directly from
     * a stream by deserializing all its content from the stream.
     *
     * @param type A document type that describes the stream content.
     * @param stream A stream that contains a serialized field update.
     */
    FieldUpdate(const DocumentTypeRepo& repo, const DataType & type, nbostream & stream);

    bool operator==(const FieldUpdate&) const;
    bool operator!=(const FieldUpdate & rhs) const { return ! (*this == rhs); }

    /**
     * Add a value update to this field update.
     *
     * @param update A pointer to the value update to add to this.
     * @return A pointer to this.
     */
    FieldUpdate& addUpdate(std::unique_ptr<ValueUpdate> update) &;
    FieldUpdate&& addUpdate(std::unique_ptr<ValueUpdate> update) &&;

    const ValueUpdate& operator[](int index) const { return *_updates[index]; }
    ValueUpdate& operator[](int index) { return *_updates[index]; }
    size_t size() const { return _updates.size(); }

    /** @return The non-modifieable list of value updates to perform. */
    const ValueUpdates & getUpdates() const { return _updates; }

    const Field& getField() const { return _field; }
    void applyTo(Document& doc) const;
    void print(std::ostream& out, bool verbose, const std::string& indent) const;
    void printXml(XmlOutputStream&) const;

    /**
     * Deserializes the given stream into an instance of an update object.
     * Not a Deserializable, as document type is needed as extra information.
     *
     * @param type A document type that describes the stream content.
     * @param buffer The stream that contains the serialized update object.
     */
    void deserialize(const DocumentTypeRepo& repo, const DocumentType& type, nbostream& stream);
private:
    Field _field;
    ValueUpdates _updates;
};

} // document