aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageapi/mbusprot/serializationhelper.h
blob: 616059aef0bcb71c3583f42da396447cf844f105 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/document/base/globalid.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/util/bytebuffer.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/growablebytebuffer.h>

namespace storage::mbusprot {

class SerializationHelper
{
public:
    static int32_t getInt(document::ByteBuffer& buf) {
        int32_t tmp;
        buf.getIntNetwork(tmp);
        return tmp;
    }

    static int16_t getShort(document::ByteBuffer& buf) {
        int16_t tmp;
        buf.getShortNetwork(tmp);
        return tmp;
    }

    static uint8_t getByte(document::ByteBuffer& buf) {
        uint8_t tmp;
        buf.getByte(tmp);
        return tmp;
    }

    static vespalib::stringref getString(document::ByteBuffer& buf) {
        uint32_t tmp;
        buf.getIntNetwork((int32_t&) tmp);
        const char * p = buf.getBufferAtPos();
        buf.incPos(tmp);
        vespalib::stringref s(p, tmp);
        return s;
    }

    static document::GlobalId getGlobalId(document::ByteBuffer& buf) {
        std::vector<char> buffer(getShort(buf));
        for (uint32_t i=0; i<buffer.size(); ++i) {
            buffer[i] = getByte(buf);
        }
        return document::GlobalId(&buffer[0]);
    }

    static document::Document::UP getDocument(document::ByteBuffer& buf, const document::DocumentTypeRepo& repo)
    {
        uint32_t size = getInt(buf);
        if (size == 0) {
            return document::Document::UP();
        } else {
            vespalib::nbostream stream(buf.getBufferAtPos(), size);
            buf.incPos(size);
            return std::make_unique<document::Document>(repo, stream);
        }
    }

    static void putDocument(document::Document* doc, vespalib::GrowableByteBuffer& buf)
    {
        if (doc) {
            vespalib::nbostream stream;
            doc->serialize(stream);
            buf.putInt(stream.size());
            buf.putBytes(stream.peek(), stream.size());
        } else {
            buf.putInt(0);
        }
    }

};

}