aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageapi/mbusprot/storagereply.cpp
blob: 78895ef1893ff3d19b934f4e1cb9b39376341dc9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "storagereply.h"
#include "storagecommand.h"
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/objects/nbostream.h>


using vespalib::alloc::Alloc;
using vespalib::IllegalStateException;

namespace storage::mbusprot {

StorageReply::StorageReply(mbus::BlobRef data, const ProtocolSerialization& serializer)
    : _serializer(&serializer),
      _sz(data.size()),
      _buffer(Alloc::alloc(_sz)),
      _mbusType(0),
      _reply()
{
    memcpy(_buffer.get(), data.data(), _sz);
    vespalib::nbostream nbo(data.data(), _sz);
    nbo >> _mbusType;
}

StorageReply::StorageReply(api::StorageReply::SP reply)
    : _serializer(0),
      _sz(0),
      _buffer(),
      _mbusType(reply->getType().getId()),
      _reply(std::move(reply))
{}

StorageReply::~StorageReply() = default;

void
StorageReply::deserialize() const
{
    if (_reply.get()) return;
    StorageReply& reply(const_cast<StorageReply&>(*this));
    mbus::Message::UP msg(reply.getMessage());
    if (msg.get() == 0) {
        throw IllegalStateException("Cannot deserialize storage reply before message have been set", VESPA_STRLOC);
    }
    const StorageCommand* cmd(dynamic_cast<const StorageCommand*>(msg.get()));
    reply.setMessage(std::move(msg));
    if (cmd == 0) {
        throw IllegalStateException("Storage reply get message did not return a storage command", VESPA_STRLOC);
    }
    mbus::BlobRef blobRef(static_cast<char *>(_buffer.get()), _sz);
    _reply = _serializer->decodeReply(blobRef, *cmd->getCommand())->getReply();
    Alloc().swap(_buffer);
}

}