aboutsummaryrefslogtreecommitdiffstats
path: root/persistence/src/vespa/persistence/spi/docentry.cpp
blob: f0329e8cc5e70fc9b9ef1d763f199c56540c1a4b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "docentry.h"
#include <vespa/document/fieldvalue/document.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <sstream>

namespace storage::spi {

namespace {

class DocEntryWithId final : public DocEntry {
public:
    DocEntryWithId(Timestamp t, DocumentMetaEnum metaEnum, const DocumentId &docId);
    ~DocEntryWithId();
    vespalib::string toString() const override;
    const DocumentId* getDocumentId() const override { return & _documentId; }
    vespalib::stringref getDocumentType() const override { return _documentId.getDocType(); }
    GlobalId getGid() const override { return _documentId.getGlobalId(); }
private:
    DocumentId _documentId;
};

class DocEntryWithTypeAndGid final : public DocEntry {
public:
    DocEntryWithTypeAndGid(Timestamp t, DocumentMetaEnum metaEnum, vespalib::stringref docType, GlobalId gid);
    ~DocEntryWithTypeAndGid();
    vespalib::string toString() const override;
    vespalib::stringref getDocumentType() const override { return _type; }
    GlobalId getGid() const override { return _gid; }
private:
    vespalib::string _type;
    GlobalId         _gid;
};

class DocEntryWithDoc final : public DocEntry {
public:
    DocEntryWithDoc(Timestamp t, DocumentUP doc);

    /**
     * Constructor that can be used by providers that already know
     * the serialized size of the document, so the potentially expensive
     * call to getSerializedSize can be avoided. This value shall be the size of the document _before_
     * any field filtering is performed.
     */
    DocEntryWithDoc(Timestamp t, DocumentUP doc, size_t serializedDocumentSize);
    ~DocEntryWithDoc();
    vespalib::string toString() const override;
    const Document* getDocument() const override { return _document.get(); }
    const DocumentId* getDocumentId() const override { return &_document->getId(); }
    DocumentUP releaseDocument() override { return std::move(_document); }
    vespalib::stringref getDocumentType() const override { return _document->getId().getDocType(); }
    GlobalId getGid() const override { return _document->getId().getGlobalId(); }
private:
    DocumentUP _document;
};

DocEntryWithDoc::DocEntryWithDoc(Timestamp t, DocumentUP doc)
    : DocEntry(t, DocumentMetaEnum::NONE, doc->serialize().size()),
      _document(std::move(doc))
{ }

DocEntryWithDoc::DocEntryWithDoc(Timestamp t, DocumentUP doc, size_t serializedDocumentSize)
    : DocEntry(t, DocumentMetaEnum::NONE, serializedDocumentSize),
      _document(std::move(doc))
{ }

DocEntryWithId::DocEntryWithId(Timestamp t, DocumentMetaEnum metaEnum, const DocumentId& docId)
    : DocEntry(t, metaEnum, docId.getSerializedSize()),
      _documentId(docId)
{ }

DocEntryWithTypeAndGid::DocEntryWithTypeAndGid(Timestamp t, DocumentMetaEnum metaEnum, vespalib::stringref docType, GlobalId gid)
    : DocEntry(t, metaEnum, docType.size() + sizeof(gid)),
      _type(docType),
      _gid(gid)
{ }

DocEntryWithTypeAndGid::~DocEntryWithTypeAndGid() = default;
DocEntryWithId::~DocEntryWithId() = default;
DocEntryWithDoc::~DocEntryWithDoc() = default;

vespalib::string
DocEntryWithId::toString() const
{
    std::ostringstream out;
    out << "DocEntry(" << getTimestamp() << ", " << int(getMetaEnum()) << ", " << _documentId << ")";
    return out.str();
}

vespalib::string
DocEntryWithTypeAndGid::toString() const
{
    std::ostringstream out;
    out << "DocEntry(" << getTimestamp() << ", " << int(getMetaEnum()) << ", " << _type << ", " << _gid << ")";
    return out.str();
}

vespalib::string
DocEntryWithDoc::toString() const
{
    std::ostringstream out;
    out << "DocEntry(" << getTimestamp() << ", " << int(getMetaEnum()) << ", ";
    if (_document.get()) {
        out << "Doc(" << _document->getId() << ")";
    } else {
        out << "metadata only";
    }
    out << ")";
    return out.str();
}

}

DocEntry::UP
DocEntry::create(Timestamp t, DocumentMetaEnum metaEnum) {
    return UP(new DocEntry(t, metaEnum));
}
DocEntry::UP
DocEntry::create(Timestamp t, DocumentMetaEnum metaEnum, const DocumentId &docId) {
    return std::make_unique<DocEntryWithId>(t, metaEnum, docId);
}
DocEntry::UP
DocEntry::create(Timestamp t, DocumentMetaEnum metaEnum, vespalib::stringref docType, GlobalId gid) {
    return std::make_unique<DocEntryWithTypeAndGid>(t, metaEnum, docType, gid);
}
DocEntry::UP
DocEntry::create(Timestamp t, DocumentUP doc) {
    return std::make_unique<DocEntryWithDoc>(t, std::move(doc));
}
DocEntry::UP
DocEntry::create(Timestamp t, DocumentUP doc, SizeType serializedDocumentSize) {
    return std::make_unique<DocEntryWithDoc>(t, std::move(doc), serializedDocumentSize);
}

DocEntry::~DocEntry() = default;

DocumentUP
DocEntry::releaseDocument() {
    return {};
}

vespalib::string
DocEntry::toString() const
{
    std::ostringstream out;
    out << "DocEntry(" << _timestamp << ", " << int(_metaEnum) << ", metadata only)";
    return out.str();
}

std::ostream &
operator << (std::ostream & os, const DocEntry & r) {
    return os << r.toString();
}

}