aboutsummaryrefslogtreecommitdiffstats
path: root/storageapi/src/vespa/storageapi/message/persistence.h
blob: 1fdd5c369bf7b912875f02f8a67d55223fb1d944 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @file persistence.h
 *
 * Persistence related commands, like put, get & remove
 */
#pragma once

#include <vespa/storageapi/messageapi/bucketinforeply.h>
#include <vespa/storageapi/defs.h>
#include <vespa/document/base/documentid.h>
#include <vespa/documentapi/messagebus/messages/testandsetcondition.h>

namespace document {
    class DocumentUpdate;
    class Document;
}
namespace storage::api {

using documentapi::TestAndSetCondition;
using DocumentSP = std::shared_ptr<document::Document>;

class TestAndSetCommand : public BucketInfoCommand {
    TestAndSetCondition _condition;
public:
    TestAndSetCommand(const MessageType & messageType, const document::Bucket &bucket);
    ~TestAndSetCommand() override;

    void setCondition(const TestAndSetCondition & condition) { _condition = condition; }
    const TestAndSetCondition & getCondition() const { return _condition; }

    /**
     * Uniform interface to get document id
     * Used by test and set to retrieve already existing document
     */
    virtual const document::DocumentId & getDocumentId() const = 0;
};

/**
 * @class PutCommand
 * @ingroup message
 *
 * @brief Command for adding a document to the storage system.
 */
class PutCommand : public TestAndSetCommand {
    DocumentSP _doc;
    Timestamp  _timestamp;
    Timestamp  _updateTimestamp;

public:
    PutCommand(const document::Bucket &bucket, const DocumentSP&, Timestamp);
    ~PutCommand() override;

    void setTimestamp(Timestamp ts) { _timestamp = ts; }

    /**
     * If set, this PUT will only update the header of an existing document,
     * rather than writing an entire new PUT. It will only perform the write if
     * there exists a document already with the given timestamp.
     */
    void setUpdateTimestamp(Timestamp ts) { _updateTimestamp = ts; }
    Timestamp getUpdateTimestamp() const { return _updateTimestamp; }

    const DocumentSP& getDocument() const { return _doc; }
    const document::DocumentId& getDocumentId() const override;
    Timestamp getTimestamp() const { return _timestamp; }

    vespalib::string getSummary() const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    DECLARE_STORAGECOMMAND(PutCommand, onPut);
};

/**
 * @class PutReply
 * @ingroup message
 *
 * @brief Reply of a put command.
 */
class PutReply : public BucketInfoReply {
    document::DocumentId _docId;
    DocumentSP _document; // Not serialized
    Timestamp _timestamp;
    Timestamp _updateTimestamp;
    bool _wasFound;

public:
    explicit PutReply(const PutCommand& cmd, bool wasFound = true);
    ~PutReply() override;

    const document::DocumentId& getDocumentId() const { return _docId; }
    bool hasDocument() const { return _document.get(); }
    const DocumentSP& getDocument() const { return _document; }
    Timestamp getTimestamp() const { return _timestamp; };
    Timestamp getUpdateTimestamp() const { return _updateTimestamp; }

    bool wasFound() const { return _wasFound; }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    DECLARE_STORAGEREPLY(PutReply, onPutReply)
};

/**
 * @class UpdateCommand
 * @ingroup message
 *
 * @brief Command for updating a document to the storage system.
 */
class UpdateCommand : public TestAndSetCommand {
    std::shared_ptr<document::DocumentUpdate> _update;
    Timestamp _timestamp;
    Timestamp _oldTimestamp;

public:
    UpdateCommand(const document::Bucket &bucket,
                  const std::shared_ptr<document::DocumentUpdate>&, Timestamp);
    ~UpdateCommand() override;

    void setTimestamp(Timestamp ts) { _timestamp = ts; }
    void setOldTimestamp(Timestamp ts) { _oldTimestamp = ts; }

    const std::shared_ptr<document::DocumentUpdate>& getUpdate() const { return _update; }
    const document::DocumentId& getDocumentId() const override;
    Timestamp getTimestamp() const { return _timestamp; }
    Timestamp getOldTimestamp() const { return _oldTimestamp; }

    vespalib::string getSummary() const override;

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    DECLARE_STORAGECOMMAND(UpdateCommand, onUpdate);
};

/**
 * @class UpdateReply
 * @ingroup message
 *
 * @brief Reply of a update command.
 */
class UpdateReply : public BucketInfoReply {
    document::DocumentId _docId;
    Timestamp _timestamp;
    Timestamp _oldTimestamp;
    uint16_t _consistentNode;

public:
    UpdateReply(const UpdateCommand& cmd, Timestamp oldTimestamp = 0);
    ~UpdateReply() override;

    void setOldTimestamp(Timestamp ts) { _oldTimestamp = ts; }

    const document::DocumentId& getDocumentId() const { return _docId; }
    Timestamp getTimestamp() const { return _timestamp; }
    Timestamp getOldTimestamp() const { return _oldTimestamp; }

    bool wasFound() const { return (_oldTimestamp != 0); }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    /**
     * If this update was inconsistent (multiple different timestamps returned),
     * set the "best" node.
     */
    void setNodeWithNewestTimestamp(uint16_t node) { _consistentNode = node; }

    uint16_t getNodeWithNewestTimestamp() { return _consistentNode; }

    DECLARE_STORAGEREPLY(UpdateReply, onUpdateReply)
};


/**
 * @class GetCommand
 * @ingroup message
 *
 * @brief Command for returning a single document.
 *
 * Normally, the newest version of a document is retrieved. The timestamp can
 * be used to retrieve the newest copy, which is not newer than the given
 * timestamp.
 */
class GetCommand : public BucketInfoCommand {
    document::DocumentId _docId;
    Timestamp _beforeTimestamp;
    vespalib::string _fieldSet;

public:
    GetCommand(const document::Bucket &bucket, const document::DocumentId&,
               vespalib::stringref fieldSet, Timestamp before = MAX_TIMESTAMP);
    ~GetCommand() override;
    void setBeforeTimestamp(Timestamp ts) { _beforeTimestamp = ts; }
    const document::DocumentId& getDocumentId() const { return _docId; }
    Timestamp getBeforeTimestamp() const { return _beforeTimestamp; }
    const vespalib::string& getFieldSet() const { return _fieldSet; }
    void setFieldSet(vespalib::stringref fieldSet) { _fieldSet = fieldSet; }

    vespalib::string getSummary() const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    api::LockingRequirements lockingRequirements() const noexcept override {
        return api::LockingRequirements::Shared;
    }

    DECLARE_STORAGECOMMAND(GetCommand, onGet)
};

/**
 * @class GetReply
 * @ingroup message
 *
 * @brief Reply for a get command.
 */
class GetReply : public BucketInfoReply {
    document::DocumentId _docId; // In case of not found, we want id still
    vespalib::string _fieldSet;
    DocumentSP _doc; // Null pointer if not found
    Timestamp _beforeTimestamp;
    Timestamp _lastModifiedTime;

public:
    GetReply(const GetCommand& cmd,
             const DocumentSP& doc = DocumentSP(),
             Timestamp lastModified = 0);
    ~GetReply() override;

    const DocumentSP& getDocument() const { return _doc; }
    const document::DocumentId& getDocumentId() const { return _docId; }
    const vespalib::string& getFieldSet() const { return _fieldSet; }

    Timestamp getLastModifiedTimestamp() const { return _lastModifiedTime; }
    Timestamp getBeforeTimestamp() const { return _beforeTimestamp; }

    bool wasFound() const { return (_doc.get() != 0); }
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    DECLARE_STORAGEREPLY(GetReply, onGetReply)
};

/**
 * @class RemoveCommand
 * @ingroup message
 *
 * @brief Command for removing a document.
 */
class RemoveCommand : public TestAndSetCommand {
    document::DocumentId _docId;
    Timestamp _timestamp;

public:
    RemoveCommand(const document::Bucket &bucket, const document::DocumentId& docId, Timestamp timestamp);
    ~RemoveCommand() override;

    void setTimestamp(Timestamp ts) { _timestamp = ts; }
    const document::DocumentId& getDocumentId() const override { return _docId; }
    Timestamp getTimestamp() const { return _timestamp; }
    vespalib::string getSummary() const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    DECLARE_STORAGECOMMAND(RemoveCommand, onRemove)
};

/**
 * @class RemoveReply
 * @ingroup message
 *
 * @brief Reply for a remove command.
 */
class RemoveReply : public BucketInfoReply {
    document::DocumentId _docId;
    Timestamp _timestamp;
    Timestamp _oldTimestamp;
public:
    explicit RemoveReply(const RemoveCommand& cmd, Timestamp oldTimestamp = 0);
    ~RemoveReply() override;

    const document::DocumentId& getDocumentId() const { return _docId; }
    Timestamp getTimestamp() { return _timestamp; };
    Timestamp getOldTimestamp() const { return _oldTimestamp; }
    void setOldTimestamp(Timestamp oldTimestamp) { _oldTimestamp = oldTimestamp; }
    bool wasFound() const { return (_oldTimestamp != 0); }
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    DECLARE_STORAGEREPLY(RemoveReply, onRemoveReply)
};

/**
 * @class RevertCommand
 * @ingroup message
 *
 * @brief Command for reverting a write or remove operation.
 */
class RevertCommand : public BucketInfoCommand {
    std::vector<Timestamp> _tokens;
public:
    RevertCommand(const document::Bucket &bucket,
                  const std::vector<Timestamp>& revertTokens);
    ~RevertCommand() override;
    const std::vector<Timestamp>& getRevertTokens() const { return _tokens; }
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    DECLARE_STORAGECOMMAND(RevertCommand, onRevert)
};

/**
 * @class RevertReply
 * @ingroup message
 *
 * @brief Reply for a revert command.
 */
class RevertReply : public BucketInfoReply {
    std::vector<Timestamp> _tokens;
public:
    explicit RevertReply(const RevertCommand& cmd);
    ~RevertReply() override;
    const std::vector<Timestamp>& getRevertTokens() const { return _tokens; }
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    DECLARE_STORAGEREPLY(RevertReply, onRevertReply)
};

}