aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/documentmetastore/documentmetastoresaver.cpp
blob: b844d5400a9c740aa1df3f82085c4e67d1774aa4 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "documentmetastoresaver.h"
#include <vespa/searchlib/util/bufferwriter.h>
#include "document_meta_store_versions.h"
#include <vespa/searchlib/attribute/iattributesavetarget.h>

using vespalib::GenerationHandler;
using search::IAttributeSaveTarget;

namespace proton {

namespace {

/*
 * Functor class to write meta data for a single lid. Note that during
 * a background save with active feeding, timestamp, bucket used bits
 * and size might reflect future values due to missing snapshot
 * properties in RcuVector. Size might also reflect a mix between
 * current and future value due to non-atomic access.
 */
class WriteMetaData
{
    using MetaDataView = DocumentMetaStoreSaver::MetaDataView;
    using GlobalId = documentmetastore::IStore::GlobalId;
    using BucketId = documentmetastore::IStore::BucketId;
    using Timestamp = documentmetastore::IStore::Timestamp;
    search::BufferWriter &_datWriter;
    MetaDataView _metaDataView;
    bool _writeDocSize;
public:
    WriteMetaData(search::BufferWriter &datWriter, MetaDataView metaDataView, bool writeDocSize)
        : _datWriter(datWriter),
          _metaDataView(metaDataView),
          _writeDocSize(writeDocSize)
    { }

    void operator()(documentmetastore::GidToLidMapKey key) {
        auto lid = key.get_lid();
        assert(lid < _metaDataView.size());
        const RawDocumentMetaData &metaData = _metaDataView[lid];
        const GlobalId &gid = metaData.getGid();
        // 6 bits used for bucket bits
        uint8_t bucketUsedBits = metaData.getBucketUsedBits();
        assert(BucketId::validUsedBits(bucketUsedBits));
        assert((bucketUsedBits >> BucketId::CountBits) == 0);
        Timestamp timestamp = metaData.getTimestamp();
        search::BufferWriter &datWriter(_datWriter);
        datWriter.write(&lid, sizeof(lid));
        datWriter.write(gid.get(), GlobalId::LENGTH);
        datWriter.write(&bucketUsedBits, sizeof(bucketUsedBits));
        if (_writeDocSize) {
            uint32_t docSize = metaData.getDocSize();
            assert(docSize < (1u << 24));
            uint8_t docSizeLow = docSize;
            uint16_t docSizeHigh = docSize >> 8;
            datWriter.write(&docSizeLow, sizeof(docSizeLow));
            datWriter.write(&docSizeHigh, sizeof(docSizeHigh));
        }
        datWriter.write(&timestamp, sizeof(timestamp));
    }
};


}


DocumentMetaStoreSaver::
DocumentMetaStoreSaver(vespalib::GenerationHandler::Guard &&guard,
                       const search::attribute::AttributeHeader &header,
                       const GidIterator &gidIterator,
                       MetaDataView metaDataView)
    : AttributeSaver(std::move(guard), header),
      _gidIterator(gidIterator),
      _metaDataView(metaDataView),
      _writeDocSize(true)
{
    if (header.getVersion() == documentmetastore::NO_DOCUMENT_SIZE_TRACKING_VERSION) {
        _writeDocSize = false;
    }
}


DocumentMetaStoreSaver::~DocumentMetaStoreSaver() = default;


bool
DocumentMetaStoreSaver::onSave(IAttributeSaveTarget &saveTarget)
{
    // write <lid,gid> pairs, sorted on gid
    std::unique_ptr<search::BufferWriter>
        datWriter(saveTarget.datWriter().allocBufferWriter());
    _gidIterator.foreach_key(WriteMetaData(*datWriter, _metaDataView, _writeDocSize));
    datWriter->flush();
    return true;
}


}  // namespace proton