summaryrefslogtreecommitdiffstats
path: root/storageapi/src/vespa/storageapi/mbusprot/protocolserialization5_1.cpp
blob: c1941b475e4e0f7d1fc5db7a6e0dbe22fa6f3487 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "protocolserialization5_1.h"
#include "serializationhelper.h"
#include "storagecommand.h"
#include "storagereply.h"
#include "storageprotocol.h"

#include <vespa/messagebus/blob.h>
#include <vespa/messagebus/blobref.h>
#include <vespa/storageapi/messageapi/storagemessage.h>
#include <vespa/storageapi/message/bucket.h>
#include <vespa/storageapi/message/bucketsplitting.h>
#include <vespa/storageapi/message/persistence.h>
#include <vespa/storageapi/message/multioperation.h>
#include <vespa/vespalib/util/growablebytebuffer.h>
#include <vespa/document/select/orderingspecification.h>
#include <vespa/storageapi/messageapi/returncode.h>

namespace storage {
namespace mbusprot {

api::BucketInfo
ProtocolSerialization5_1::getBucketInfo(document::ByteBuffer& buf) const
{
    uint64_t lastModified(SH::getLong(buf));
    uint32_t crc(SH::getInt(buf));
    uint32_t doccount(SH::getInt(buf));
    uint32_t docsize(SH::getInt(buf));
    uint32_t metacount(SH::getInt(buf));
    uint32_t usedsize(SH::getInt(buf));
    uint8_t flags(SH::getByte(buf));
    bool ready = (flags & BUCKET_READY) != 0;
    bool active = (flags & BUCKET_ACTIVE) != 0;
    return api::BucketInfo(crc, doccount, docsize,
                           metacount, usedsize,
                           ready, active, lastModified);
}

void
ProtocolSerialization5_1::putBucketInfo(
        const api::BucketInfo& info, vespalib::GrowableByteBuffer& buf) const
{
    buf.putLong(info.getLastModified());
    buf.putInt(info.getChecksum());
    buf.putInt(info.getDocumentCount());
    buf.putInt(info.getTotalDocumentSize());
    buf.putInt(info.getMetaCount());
    buf.putInt(info.getUsedFileSize());
    uint8_t flags = (info.isReady() ? BUCKET_READY : 0) |
                    (info.isActive() ? BUCKET_ACTIVE : 0);
    buf.putByte(flags);
}

ProtocolSerialization5_1::ProtocolSerialization5_1(
        const document::DocumentTypeRepo::SP& repo,
        const documentapi::LoadTypeSet& loadTypes)
    : ProtocolSerialization5_0(repo, loadTypes)
{
}

void ProtocolSerialization5_1::onEncode(
        GBBuf& buf, const api::SetBucketStateCommand& msg) const
{
    buf.putLong(msg.getBucketId().getRawId());
    buf.putByte(static_cast<uint8_t>(msg.getState()));
    onEncodeCommand(buf, msg);
}

api::StorageCommand::UP
ProtocolSerialization5_1::onDecodeSetBucketStateCommand(BBuf& buf) const
{
    document::BucketId bucket(SH::getLong(buf));
    api::SetBucketStateCommand::BUCKET_STATE state(
            static_cast<api::SetBucketStateCommand::BUCKET_STATE>(
                    SH::getByte(buf)));
    api::SetBucketStateCommand::UP msg(
            new api::SetBucketStateCommand(bucket, state));
    onDecodeCommand(buf, *msg);
    return api::StorageCommand::UP(msg.release());
}

void ProtocolSerialization5_1::onEncode(
        GBBuf& buf, const api::SetBucketStateReply& msg) const
{
    onEncodeBucketReply(buf, msg);
}

api::StorageReply::UP
ProtocolSerialization5_1::onDecodeSetBucketStateReply(const SCmd& cmd,
                                                      BBuf& buf) const
{
    api::SetBucketStateReply::UP msg(new api::SetBucketStateReply(
                static_cast<const api::SetBucketStateCommand&>(cmd)));
    onDecodeBucketReply(buf, *msg);
    return api::StorageReply::UP(msg.release());
}

void ProtocolSerialization5_1::onEncode(
        GBBuf& buf, const api::GetCommand& msg) const
{
    buf.putString(msg.getDocumentId().toString());
    buf.putLong(msg.getBucketId().getRawId());
    buf.putLong(msg.getBeforeTimestamp());
    buf.putString(msg.getFieldSet());
    onEncodeCommand(buf, msg);
}

api::StorageCommand::UP
ProtocolSerialization5_1::onDecodeGetCommand(BBuf& buf) const
{
    document::DocumentId did(SH::getString(buf));
    document::BucketId bucket(SH::getLong(buf));
    api::Timestamp beforeTimestamp(SH::getLong(buf));
    std::string fieldSet(SH::getString(buf));
    api::GetCommand::UP msg(
            new api::GetCommand(bucket, did, fieldSet, beforeTimestamp));
    onDecodeCommand(buf, *msg);
    return api::StorageCommand::UP(msg.release());
}

void
ProtocolSerialization5_1::onEncode(
        GBBuf& buf, const api::CreateVisitorCommand& msg) const
{
    buf.putString(msg.getLibraryName());
    buf.putString(msg.getInstanceId());
    buf.putString(msg.getDocumentSelection());
    buf.putInt(msg.getVisitorCmdId());
    buf.putString(msg.getControlDestination());
    buf.putString(msg.getDataDestination());
    buf.putInt(msg.getMaximumPendingReplyCount());
    buf.putLong(msg.getFromTime());
    buf.putLong(msg.getToTime());

    buf.putInt(msg.getBuckets().size());
    for (uint32_t i = 0; i < msg.getBuckets().size(); i++) {
        buf.putLong(msg.getBuckets()[i].getRawId());
    }

    buf.putBoolean(msg.visitRemoves());
    buf.putString(msg.getFieldSet());
    buf.putBoolean(msg.visitInconsistentBuckets());
    buf.putInt(msg.getQueueTimeout());

    uint32_t size = msg.getParameters().getSerializedSize();
    char* docBuffer = buf.allocate(size);
    document::ByteBuffer bbuf(docBuffer, size);
    msg.getParameters().serialize(bbuf);

    onEncodeCommand(buf, msg);

    buf.putInt(msg.getVisitorOrdering());
    buf.putInt(msg.getMaxBucketsPerVisitor());
}

api::StorageCommand::UP
ProtocolSerialization5_1::onDecodeCreateVisitorCommand(BBuf& buf) const
{
    vespalib::stringref libraryName = SH::getString(buf);
    vespalib::stringref instanceId = SH::getString(buf);
    vespalib::stringref selection = SH::getString(buf);
    api::CreateVisitorCommand::UP msg(
            new api::CreateVisitorCommand(libraryName, instanceId, selection));
    msg->setVisitorCmdId(SH::getInt(buf));
    msg->setControlDestination(SH::getString(buf));
    msg->setDataDestination(SH::getString(buf));
    msg->setMaximumPendingReplyCount(SH::getInt(buf));

    msg->setFromTime(SH::getLong(buf));
    msg->setToTime(SH::getLong(buf));
    uint32_t count = SH::getInt(buf);

    if (count > buf.getRemaining()) {
            // Trigger out of bounds exception rather than out of memory error
        buf.incPos(count);
    }

    for (uint32_t i = 0; i < count; i++) {
        msg->getBuckets().push_back(document::BucketId(SH::getLong(buf)));
    }

    if (SH::getBoolean(buf)) {
        msg->setVisitRemoves();
    }

    msg->setFieldSet(SH::getString(buf));

    if (SH::getBoolean(buf)) {
        msg->setVisitInconsistentBuckets();
    }
    msg->setQueueTimeout(SH::getInt(buf));
    msg->getParameters().deserialize(getTypeRepo(), buf);

    onDecodeCommand(buf, *msg);
    msg->setVisitorOrdering(
            (document::OrderingSpecification::Order)SH::getInt(buf));
    msg->setMaxBucketsPerVisitor(SH::getInt(buf));
    msg->setVisitorDispatcherVersion(50);
    return api::StorageCommand::UP(msg.release());
}

void ProtocolSerialization5_1::onEncode(
        GBBuf& buf, const api::CreateBucketCommand& msg) const
{
    buf.putLong(msg.getBucketId().getRawId());
    buf.putBoolean(msg.getActive());
    onEncodeBucketInfoCommand(buf, msg);
}

api::StorageCommand::UP
ProtocolSerialization5_1::onDecodeCreateBucketCommand(BBuf& buf) const
{
    document::BucketId bid(SH::getLong(buf));
    bool setActive = SH::getBoolean(buf);
    api::CreateBucketCommand::UP msg(new api::CreateBucketCommand(bid));
    msg->setActive(setActive);
    onDecodeBucketInfoCommand(buf, *msg);
    return api::StorageCommand::UP(msg.release());
}


} // mbusprot
} // storage