aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/vespa/documentapi/messagebus/routablefactories60.h
blob: c0cbc4868ebd3d564a652855f9f9b6a9a39c3e33 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// @author Vegard Sjonfjell
#pragma once

#include <vespa/document/util/bytebuffer.h>
#include <vespa/vespalib/util/growablebytebuffer.h>
#include "iroutablefactory.h"
#include <vespa/documentapi/messagebus/messages/putdocumentmessage.h>
#include <vespa/documentapi/messagebus/messages/removedocumentmessage.h>
#include <vespa/documentapi/messagebus/messages/updatedocumentmessage.h>

namespace document { class DocumentTypeRepo; }

/**
 * Utility class for invoking setApproxSize on a DocumentMessage with the delta
 * between the read position of a ByteBuffer at construction and destruction
 * time. The assumption being made is that the in-memory footprint of a message
 * is reasonably close to its wire-serialized form.
 */
class ScopedApproxSizeSetter {
public:
    ScopedApproxSizeSetter(documentapi::DocumentMessage& msg,
                           const document::ByteBuffer& buf)
        : _msg(msg),
          _buf(buf),
          _posBefore(_buf.getPos())
    {
    }

    ~ScopedApproxSizeSetter() {
        _msg.setApproxSize(static_cast<uint32_t>(_buf.getPos() - _posBefore));
    }

private:
    documentapi::DocumentMessage& _msg;
    const document::ByteBuffer& _buf;
    const size_t _posBefore;
};

namespace documentapi {

template<typename MessageType, typename FactoryType>
DocumentMessage::UP
decodeMessage(const FactoryType * self, document::ByteBuffer & buf) {
    auto msg = std::make_unique<MessageType>();
    ScopedApproxSizeSetter sizeSetter(*msg, buf);
    self->decodeInto(*msg, buf);
    return msg;
}

/**
 * This class encapsulates all the {@link RoutableFactory} classes needed to implement factories for the document
 * routable. When adding new factories to this class, please KEEP THE THEM ORDERED alphabetically like they are now.
 */
class RoutableFactories60 {
public:
    RoutableFactories60() = delete;

    /**
     * Implements the shared factory logic required for {@link DocumentMessage} objects, and it offers a more
     * convenient interface for implementing {@link RoutableFactory}.
     */
    class DocumentMessageFactory : public IRoutableFactory {
    protected:
        /**
         * This method encodes the given message into the given byte buffer. You are guaranteed to only receive messages of
         * the type that this factory was registered for.
         *
         * This method is NOT exception safe. Return false to signal failure.
         *
         * @param msg The message to encode.
         * @param buf The byte buffer to write to.
         * @return True if the message was encoded.
         */
        virtual bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const = 0;

        /**
         * This method decodes a message from the given byte buffer. You are guaranteed to only receive byte buffers
         * generated by a previous call to {@link #doEncode(DocumentMessage, GrowableByteBuffer)}.
         *
         * This method is NOT exception safe. Return null to signal failure.
         *
         * @param buf The byte buffer to read from.
         * @return The decoded message.
         */
        virtual DocumentMessage::UP doDecode(document::ByteBuffer &buf) const = 0;

    public:
        bool encode(const mbus::Routable &obj, vespalib::GrowableByteBuffer &out) const override;
        mbus::Routable::UP decode(document::ByteBuffer &in) const override;
    };

    /**
     * Implements the shared factory logic required for {@link DocumentReply} objects, and it offers a more
     * convenient interface for implementing {@link RoutableFactory}.
     */
    class DocumentReplyFactory : public IRoutableFactory {
    protected:
        /**
         * This method encodes the given reply into the given byte buffer. You are guaranteed to only receive
         * replies of the type that this factory was registered for.
         *
         * This method is NOT exception safe. Return false to signal failure.
         *
         * @param reply The reply to encode.
         * @param buf The byte buffer to write to.
         * @return True if the message was encoded.
         */
        virtual bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const = 0;

        /**
         * This method decodes a reply from the given byte buffer. You are guaranteed to only receive byte buffers
         * generated by a previous call to {@link #doEncode(DocumentReply, GrowableByteBuffer)}.
         *
         * This method is NOT exception safe. Return null to signal failure.
         *
         * @param buf The byte buffer to read from.
         * @return The decoded reply.
         */
        virtual DocumentReply::UP doDecode(document::ByteBuffer &buf) const = 0;

    public:
        bool encode(const mbus::Routable &obj, vespalib::GrowableByteBuffer &out) const override;
        mbus::Routable::UP decode(document::ByteBuffer &in) const override;
    };

    ////////////////////////////////////////////////////////////////////////////////
    //
    // Factories
    //
    ////////////////////////////////////////////////////////////////////////////////
    class CreateVisitorMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;

        virtual bool encodeBucketSpace(vespalib::stringref bucketSpace, vespalib::GrowableByteBuffer& buf) const;
        virtual string decodeBucketSpace(document::ByteBuffer&) const;
    public:
        CreateVisitorMessageFactory() noexcept : DocumentMessageFactory() {}
    };
    class CreateVisitorReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class DestroyVisitorMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class DestroyVisitorReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class DocumentIgnoredReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class DocumentListMessageFactory : public DocumentMessageFactory {
        const document::DocumentTypeRepo &_repo;
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    public:
        DocumentListMessageFactory(const document::DocumentTypeRepo &r) noexcept
            : _repo(r) {}
    };
    class DocumentListReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class EmptyBucketsMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class EmptyBucketsReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class GetBucketListMessageFactory : public DocumentMessageFactory {
        virtual bool encodeBucketSpace(vespalib::stringref bucketSpace, vespalib::GrowableByteBuffer& buf) const;
        virtual string decodeBucketSpace(document::ByteBuffer&) const;
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class GetBucketListReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class GetBucketStateMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class GetBucketStateReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class GetDocumentMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class GetDocumentReplyFactory : public DocumentReplyFactory {
        const document::DocumentTypeRepo &_repo;
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &msg, vespalib::GrowableByteBuffer &buf) const override;
    public:
        GetDocumentReplyFactory(const document::DocumentTypeRepo &r) noexcept : _repo(r) {}
    };
    class MapVisitorMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    public:
        MapVisitorMessageFactory() noexcept : DocumentMessageFactory()  {}
    };
    class MapVisitorReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class PutDocumentMessageFactory : public DocumentMessageFactory {
    protected:
        const document::DocumentTypeRepo &_repo;
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override {
            return decodeMessage<PutDocumentMessage>(this, buf);
        }

        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    public:
        void decodeInto(PutDocumentMessage & msg, document::ByteBuffer & buf) const;
        PutDocumentMessageFactory(const document::DocumentTypeRepo &r) noexcept : _repo(r) {}
    };
    class PutDocumentReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class RemoveDocumentMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override {
            return decodeMessage<RemoveDocumentMessage>(this, buf);
        }

        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    public:
        void decodeInto(RemoveDocumentMessage & msg, document::ByteBuffer & buf) const;
    };
    class RemoveDocumentReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class RemoveLocationMessageFactory : public DocumentMessageFactory {
        const document::DocumentTypeRepo &_repo;
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    public:
        RemoveLocationMessageFactory(const document::DocumentTypeRepo &r) noexcept : _repo(r) {}
    };
    class RemoveLocationReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class StatBucketMessageFactory : public DocumentMessageFactory {
        virtual bool encodeBucketSpace(vespalib::stringref bucketSpace, vespalib::GrowableByteBuffer& buf) const;
        virtual string decodeBucketSpace(document::ByteBuffer&) const;
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class StatBucketReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class StatDocumentMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class StatDocumentReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class UpdateDocumentMessageFactory : public DocumentMessageFactory {
    protected:
        const document::DocumentTypeRepo &_repo;
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override {
            return decodeMessage<UpdateDocumentMessage>(this, buf);
        }

        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    public:
        void decodeInto(UpdateDocumentMessage & msg, document::ByteBuffer & buf) const;
        UpdateDocumentMessageFactory(const document::DocumentTypeRepo &r) noexcept : _repo(r) {}
    };
    class UpdateDocumentReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class VisitorInfoMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class VisitorInfoReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class WrongDistributionReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };
    class QueryResultMessageFactory : public DocumentMessageFactory {
    protected:
        DocumentMessage::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentMessage &msg, vespalib::GrowableByteBuffer &buf) const override;
    };
    class QueryResultReplyFactory : public DocumentReplyFactory {
    protected:
        DocumentReply::UP doDecode(document::ByteBuffer &buf) const override;
        bool doEncode(const DocumentReply &reply, vespalib::GrowableByteBuffer &buf) const override;
    };

    ///////////////////////////////////////////////////////////////////////////
    //
    // Utilities
    //
    ///////////////////////////////////////////////////////////////////////////

    /**
     * This is a complement for the vespalib::GrowableByteBuffer.putString() method.
     *
     * @param in The byte buffer to read from.
     * @return The decoded string.
     */
    static string decodeString(document::ByteBuffer &in);

    /**
     * This is a complement for the vespalib::GrowableByteBuffer.putBoolean() method.
     *
     * @param in The byte buffer to read from.
     * @return The decoded bool.
     */
    static bool decodeBoolean(document::ByteBuffer &in);

    /**
     * Convenience method to decode a 32-bit int from the given byte buffer.
     *
     * @param in The byte buffer to read from.
     * @return The decoded int.
     */
    static int32_t decodeInt(document::ByteBuffer &in);

    /**
     * Convenience method to decode a 64-bit int from the given byte buffer.
     *
     * @param in The byte buffer to read from.
     * @return The decoded int.
     */
    static int64_t decodeLong(document::ByteBuffer &in);

    /**
     * Convenience method to decode a document id from the given byte buffer.
     *
     * @param in The byte buffer to read from.
     * @return The decoded document id.
     */
    static document::DocumentId decodeDocumentId(document::ByteBuffer &in);

    /**
     * Convenience method to encode a document id to the given byte buffer.
     *
     * @param id  The document id to encode.
     * @param out The byte buffer to write to.
     */
    static void encodeDocumentId(const document::DocumentId &id,
                                 vespalib::GrowableByteBuffer &out);

    static void decodeTasCondition(DocumentMessage & docMsg, document::ByteBuffer & buf);
    static void encodeTasCondition(vespalib::GrowableByteBuffer & buf, const DocumentMessage & docMsg);
    static void doEncodeBucketSpace(vespalib::stringref bucketSpace, vespalib::GrowableByteBuffer& buf);
    static string doDecodeBucketSpace(document::ByteBuffer&);
};

}