aboutsummaryrefslogtreecommitdiffstats
path: root/persistence/src/vespa/persistence/spi/result.h
blob: 75f9a8e7ef8c0cb1bb349dcb8f6906d2a1aa948e (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
318
319
320
321
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "bucketinfo.h"
#include "bucket.h"
#include <vespa/document/bucket/bucketidlist.h>

namespace storage::spi {

class DocEntry;

class Result {
public:
    using UP = std::unique_ptr<Result>;

    enum class ErrorType {
        NONE,
        TRANSIENT_ERROR,
        PERMANENT_ERROR,
        TIMESTAMP_EXISTS,
        FATAL_ERROR,
        RESOURCE_EXHAUSTED,
        ERROR_COUNT
    };

    /**
     * Constructor to use for a result where there is no error.
     */
    Result() noexcept : _errorCode(ErrorType::NONE), _errorMessage() {}

    /**
     * Constructor to use when an error has been detected.
     */
    Result(ErrorType error, const vespalib::string& errorMessage) noexcept
        : _errorCode(error),
          _errorMessage(errorMessage)
    {}

    Result(const Result &);
    Result(Result&&) noexcept;
    Result & operator = (const Result &);
    Result& operator=(Result&&) noexcept;

    virtual ~Result();

    bool operator==(const Result& o) const {
        return _errorCode == o._errorCode
               && _errorMessage == o._errorMessage;
    }

    bool hasError() const {
        return _errorCode != ErrorType::NONE;
    }

    ErrorType getErrorCode() const {
        return _errorCode;
    }

    const vespalib::string& getErrorMessage() const {
        return _errorMessage;
    }

    vespalib::string toString() const;

private:
    ErrorType _errorCode;
    vespalib::string _errorMessage;
};

std::ostream & operator << (std::ostream & os, const Result & r);

std::ostream & operator << (std::ostream & os, const Result::ErrorType &errorCode);

class BucketInfoResult final : public Result {
public:
    /**
     * Constructor to use for a result where an error has been detected.
     * The service layer will not update the bucket information in this case,
     * so it should not be returned either.
     */
    BucketInfoResult(ErrorType error, const vespalib::string& errorMessage)
        : Result(error, errorMessage) {};

    /**
     * Constructor to use when the write operation was successful,
     * and the bucket info was modified.
     */
    BucketInfoResult(const BucketInfo& info) : _info(info) {}

    const BucketInfo& getBucketInfo() const {
        return _info;
    }

private:
    BucketInfo _info;
};

class UpdateResult final : public Result
{
public:
    /**
     * Constructor to use for a result where an error has been detected.
     * The service layer will not update the bucket information in this case,
     * so it should not be returned either.
     */
    UpdateResult(ErrorType error, const vespalib::string& errorMessage)
        : Result(error, errorMessage),
          _existingTimestamp(0) { }

    /**
     * Constructor to use when no document to update was found.
     */
    UpdateResult()
        : _existingTimestamp(0) { }

    /**
     * Constructor to use when the update was successful.
     */
    UpdateResult(Timestamp existingTimestamp)
        : _existingTimestamp(existingTimestamp) {}

    Timestamp getExistingTimestamp() const { return _existingTimestamp; }

private:
    // Set to 0 if non-existing.
    Timestamp _existingTimestamp;
};

class RemoveResult : public Result
{
public:
    /**
     * Constructor to use for a result where an error has been detected.
     * The service layer will not update the bucket information in this case,
     * so it should not be returned either.
     */
    RemoveResult(ErrorType error, const vespalib::string& errorMessage) noexcept
        : Result(error, errorMessage),
          _numRemoved(0)
    { }

    explicit RemoveResult(bool found) noexcept
            : RemoveResult(found ? 1u : 0u) { }
    explicit RemoveResult(uint32_t numRemoved) noexcept
        : _numRemoved(numRemoved) { }
    bool wasFound() const { return _numRemoved > 0; }
    uint32_t num_removed() const { return _numRemoved; }
    void inc_num_removed(uint32_t add) { _numRemoved += add; }

private:
    uint32_t _numRemoved;
};

class GetResult final : public Result {
public:
    /**
     * Constructor to use when there was an error retrieving the document.
     * Not finding the document is not an error in this context.
     */
    GetResult(ErrorType error, const vespalib::string& errorMessage)
        : Result(error, errorMessage),
          _timestamp(0),
          _is_tombstone(false)
    {
    }

    /**
     * Constructor to use when we didn't find the document in question.
     */
    GetResult()
        : _timestamp(0),
          _doc(),
          _is_tombstone(false)
    {
    }
    GetResult(GetResult &&) noexcept = default;
    GetResult & operator=(GetResult &&) noexcept = default;

    /**
     * Constructor to use when we found the document asked for.
     *
     * @param doc The document we found
     * @param timestamp The timestamp with which the document was stored.
     */
    GetResult(DocumentUP doc, Timestamp timestamp);

    static GetResult make_for_tombstone(Timestamp removed_at_ts) {
        return GetResult(removed_at_ts, true);
    }

    static GetResult make_for_metadata_only(Timestamp removed_at_ts) {
        return GetResult(removed_at_ts, false);
    }

    ~GetResult() override;

    [[nodiscard]] Timestamp getTimestamp() const {
        return _timestamp;
    }

    [[nodiscard]] bool hasDocument() const {
        return (_doc.get() != nullptr);
    }

    [[nodiscard]] bool is_tombstone() const noexcept {
        return _is_tombstone;
    }

    const Document& getDocument() const {
        return *_doc;
    }

    Document& getDocument() {
        return *_doc;
    }

    const DocumentSP & getDocumentPtr() {
        return _doc;
    }

private:
    // Explicitly creates a metadata only GetResult with no document, optionally a tombstone (remove entry).
    GetResult(Timestamp removed_at_ts, bool is_tombstone);

    Timestamp  _timestamp;
    DocumentSP _doc;
    bool       _is_tombstone;
};

class BucketIdListResult final : public Result {
public:
    using List = document::bucket::BucketIdList;

    /**
     * Constructor used when there was an error listing the buckets.
     */
    BucketIdListResult(ErrorType error, const vespalib::string& errorMessage)
        : Result(error, errorMessage) {}

    /**
     * Constructor used when the bucket listing was successful.
     *
     * @param list The list of bucket ids this partition has. Is swapped with
     * the list internal to this object.
     */
    BucketIdListResult(List list)
        : Result(),
          _info(std::move(list))
    { }
    BucketIdListResult()
        : Result(),
          _info()
    { }
    BucketIdListResult(BucketIdListResult &&) noexcept = default;
    BucketIdListResult & operator =(BucketIdListResult &&) noexcept = default;
    ~BucketIdListResult();

    const List& getList() const { return _info; }
    List& getList() { return _info; }

private:
    List _info;
};

class CreateIteratorResult : public Result {
public:
    /**
     * Constructor used when there was an error creating the iterator.
     */
    CreateIteratorResult(ErrorType error, const vespalib::string& errorMessage) noexcept
        : Result(error, errorMessage),
          _iterator(0) { }

    /**
     * Constructor used when the iterator state was successfully created.
     */
    CreateIteratorResult(const IteratorId& id) noexcept
        : _iterator(id)
    { }

    const IteratorId& getIteratorId() const { return _iterator; }

private:
    IteratorId _iterator;
};

class IterateResult final : public Result {
public:
    using List = std::vector<std::unique_ptr<DocEntry>>;

    /**
     * Constructor used when there was an error creating the iterator.
     */
    IterateResult(ErrorType error, const vespalib::string& errorMessage);

    /**
     * Constructor used when the iteration was successful.
     * For performance concerns, the entries in the input vector
     * are swapped with the internal vector.
     *
     * @param completed Set to true if iteration has been completed.
     */
    IterateResult(List entries, bool completed);

    IterateResult(const IterateResult &) = delete;
    IterateResult(IterateResult &&rhs) noexcept;
    IterateResult &operator=(IterateResult &&rhs) noexcept;

    ~IterateResult();

    const List& getEntries() const { return _entries; }
    List steal_entries();
    bool isCompleted() const { return _completed; }

private:
    bool _completed;
    List _entries;
};

}