aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/vespa/documentapi/messagebus/messages/documentstate.cpp
blob: dcaf9eeaac5ccc69a02a7ae3d55c0ced2a129126 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "documentstate.h"
#include <vespa/document/util/bytebuffer.h>
#include <vespa/documentapi/common.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/growablebytebuffer.h>

namespace documentapi {

DocumentState::DocumentState()
    : _timestamp(0), _removeEntry(false) {}

DocumentState::DocumentState(const DocumentState& o)
    : _gid(o._gid), _timestamp(o._timestamp), _removeEntry(o._removeEntry)
{
    if (o._docId.get() != 0) {
        _docId = std::make_unique<document::DocumentId>(*o._docId);
    }
}

DocumentState::DocumentState(const document::DocumentId& id, uint64_t timestamp, bool removeEntry)
    : _docId(new document::DocumentId(id)),
      _gid(_docId->getGlobalId()),
      _timestamp(timestamp),
      _removeEntry(removeEntry)
{
}

DocumentState::DocumentState(const document::GlobalId& gid, uint64_t timestamp, bool removeEntry)
    : _gid(gid), _timestamp(timestamp), _removeEntry(removeEntry) {}

DocumentState::DocumentState(document::ByteBuffer& buf)
    : _docId(), _gid(), _timestamp(0), _removeEntry(false)
{
    uint8_t hasDocId;
    buf.getByte(hasDocId);
    if (hasDocId) {
        vespalib::nbostream stream(buf.getBufferAtPos(), buf.getRemaining());
        _docId = std::make_unique<document::DocumentId>(stream);
        buf.incPos(stream.rp());
    }
    const char* gid = buf.getBufferAtPos();
    buf.incPos(document::GlobalId::LENGTH);
    _gid.set(gid);
    buf.getLongNetwork((int64_t&) _timestamp);
    uint8_t b;
    buf.getByte(b);
    _removeEntry = b > 0;
}

DocumentState&
DocumentState::operator=(const DocumentState& other)
{
    _docId.reset();
    if (other._docId) {
        _docId = std::make_unique<document::DocumentId>(*other._docId);
    }
    _gid = other._gid;
    _timestamp = other._timestamp;
    _removeEntry = other._removeEntry;
    return *this;
}

void DocumentState::serialize(vespalib::GrowableByteBuffer &buf) const
{
    if (_docId.get()) {
        buf.putByte(1);
        string str = _docId->toString();
        buf.putBytes(str.c_str(), str.length() + 1);
    } else {
        buf.putByte(0);
    }
    char* buffer = buf.allocate(document::GlobalId::LENGTH);
    memcpy(buffer, _gid.get(), document::GlobalId::LENGTH);

    buf.putLong(_timestamp);
    buf.putByte(_removeEntry ? 1 : 0);
}

} // documentapi