aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/vespa/documentapi/messagebus/messages/updatedocumentmessage.h
blob: e4a528dacddb9e908467c3040fa960a4d8a45568 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "testandsetmessage.h"
#include <optional>

namespace document { class DocumentUpdate; }

namespace documentapi {

class UpdateDocumentMessage : public TestAndSetMessage {
private:
    using DocumentUpdateSP = std::shared_ptr<document::DocumentUpdate>;
    DocumentUpdateSP    _documentUpdate;
    uint64_t            _oldTime;
    uint64_t            _newTime;
    std::optional<bool> _create_if_missing;

protected:
    DocumentReply::UP doCreateReply() const override;

public:
    /**
     * Convenience typedef.
     */
    using UP = std::unique_ptr<UpdateDocumentMessage>;
    using SP = std::shared_ptr<UpdateDocumentMessage>;

    /**
     * Constructs a new document message for deserialization.
     */
    UpdateDocumentMessage();
    ~UpdateDocumentMessage() override;

    /**
     * Constructs a new document update message.
     *
     * @param documentUpdate The document update to perform.
     */
    explicit UpdateDocumentMessage(DocumentUpdateSP documentUpdate);

    /**
     * Returns the document update to perform.
     *
     * @return The update.
     */
    [[nodiscard]] DocumentUpdateSP stealDocumentUpdate() const { return std::move(_documentUpdate); }
    const document::DocumentUpdate & getDocumentUpdate() const { return *_documentUpdate; }
    /**
     * Sets the document update to perform.
     *
     * @param documentUpdate The document update to set.
     */
    void setDocumentUpdate(DocumentUpdateSP documentUpdate);

    /**
     * Returns the timestamp required for this update to be applied.
     *
     * @return The document timestamp.
     */
    [[nodiscard]] uint64_t getOldTimestamp() const noexcept { return _oldTime; }

    /**
     * Sets the timestamp required for this update to be applied.
     *
     * @param time The timestamp to set.
     */
    void setOldTimestamp(uint64_t time) noexcept { _oldTime = time; }

    /**
     * Returns the timestamp to assign to the updated document.
     *
     * @return The document timestamp.
     */
    [[nodiscard]] uint64_t getNewTimestamp() const noexcept { return _newTime; }

    /**
     * Sets the timestamp to assign to the updated document.
     *
     * @param time The timestamp to set.
     */
    void setNewTimestamp(uint64_t time) { _newTime = time; }

    void set_cached_create_if_missing(bool create) noexcept {
        _create_if_missing = create;
    }

    [[nodiscard]] bool has_cached_create_if_missing() const noexcept {
        return _create_if_missing.has_value();
    }
    // Note: iff has_cached_create_if_missing() == false, this will trigger a deserialization of the
    // underlying DocumentUpdate instance, which might throw an exception on deserialization failure.
    // Otherwise, this is noexcept.
    [[nodiscard]] bool create_if_missing() const;

    bool hasSequenceId() const override;
    uint64_t getSequenceId() const override;
    uint32_t getType() const override;
    string toString() const override { return "updatedocumentmessage"; }
};

}