aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/docstore/chunk.cpp
blob: 60255af352155b484b1a3035c13f3af542b8c10f (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "chunk.h"
#include "chunkformats.h"
#include <vespa/vespalib/stllike/hash_map.hpp>
#include <vespa/vespalib/util/size_literals.h>

namespace search {

LidMeta
Chunk::append(uint32_t lid, const void * buffer, size_t len)
{
    vespalib::nbostream & os = getData();
    size_t oldSz(os.size());
    std::lock_guard guard(_lock);
    os << lid << static_cast<uint32_t>(len);
    os.write(buffer, len);
    _lids.emplace_back(lid, len, oldSz);
    return LidMeta(lid, len);
}

ssize_t
Chunk::read(uint32_t lid, vespalib::DataBuffer & buffer) const
{
    std::lock_guard guard(_lock);
    vespalib::ConstBufferRef buf = getLid(lid);
    if (buf.size() != 0) {
        buffer.writeBytes(buf.c_str(), buf.size());
    }
    return buf.size();
}

std::pair<size_t, vespalib::alloc::Alloc>
Chunk::read(uint32_t lid) const
{
    std::lock_guard guard(_lock);
    vespalib::ConstBufferRef buf = getLid(lid);
    auto copy = vespalib::alloc::Alloc::alloc(buf.size());
    if (buf.size() != 0) {
        memcpy(copy.get(), buf.data(), buf.size());
    }
    return std::make_pair(buf.size(), std::move(copy));
}

bool
Chunk::hasRoom(size_t len) const
{
    const size_t HeaderSize(2*sizeof(uint32_t));
    const size_t TrailerSize(sizeof(uint64_t));
    // To avoid read races during compacting These buffers must be preallocated.
    // There is always room for at least one element.
    // There is also room as long as neither _lids[] nor _dataBuf[] require reallocation.
    // Remember to account for Header and Trailer space requirement.
    const vespalib::nbostream & os = getData();
    return _lids.empty()
           || (((HeaderSize + TrailerSize + os.size() + len) <= os.capacity())
               && ((_lids.size() + 1) <= _lids.capacity()));
}

size_t
Chunk::getMaxPackSize(CompressionConfig compression) const {
    return _format->getMaxPackSize(compression);
}

void
Chunk::pack(uint64_t lastSerial, vespalib::DataBuffer & compressed, CompressionConfig compression)
{
    _lastSerial = lastSerial;
    std::lock_guard guard(_lock);
    _format->pack(_lastSerial, compressed, compression);
}

Chunk::Chunk(uint32_t id, const Config & config) :
    _id(id),
    _lastSerial(static_cast<uint64_t>(-1l)),
    _format(std::make_unique<ChunkFormatV2>(config.getMaxBytes())),
    _lock()
{
    _lids.reserve(4_Ki/sizeof(Entry));
}

Chunk::Chunk(uint32_t id, const void * buffer, size_t len) :
    _id(id),
    _lastSerial(static_cast<uint64_t>(-1l)),
    _format(ChunkFormat::deserialize(buffer, len))
{
    vespalib::nbostream &os = getData();
    while (os.size() > sizeof(_lastSerial)) {
        uint32_t sz(0);
        uint32_t lid(0);
        ssize_t oldRp(os.rp());
        os >> lid >> sz;
        os.adjustReadPos(sz);
        _lids.emplace_back(lid, sz, oldRp);
    }
    os >> _lastSerial;
}

Chunk::~Chunk() = default;

vespalib::ConstBufferRef
Chunk::getLid(uint32_t lid) const
{
    vespalib::ConstBufferRef buf;
    for (const auto& elem : _lids) {
        if (elem.getLid() == lid) {
#if 1
            uint32_t bLid(0), bLen(0);
            vespalib::nbostream is(getData().data() + elem.getOffset(), elem.size());
            is >> bLid >> bLen;
            assert(bLid == lid);
            assert(bLen == elem.netSize());
            assert((bLen + 2*sizeof(uint32_t)) == elem.size());
#endif
            buf = vespalib::ConstBufferRef(getData().data() + elem.getNetOffset(), elem.netSize());
        }
    }
    return buf;
}

size_t
Chunk::size() const {
    std::lock_guard guard(_lock);
    return getData().size();
}

const vespalib::nbostream &
Chunk::getData() const {
    return _format->getBuffer();
}

vespalib::nbostream &
Chunk::getData() {
    return _format->getBuffer();
}

Chunk::LidList
Chunk::getUniqueLids() const
{
    vespalib::hash_map<uint32_t, Entry> last;
    for (const Entry & e : _lids) {
        last[e.getLid()] = e;
    }
    LidList unique;
    unique.reserve(last.size());
    for (const auto & entry : last) {
        unique.emplace_back(entry.second);
    }
    return unique;
}

vespalib::MemoryUsage
Chunk::getMemoryUsage() const
{
    vespalib::MemoryUsage result;
    std::lock_guard guard(_lock);
    result.incAllocatedBytes(_format->getBuffer().capacity());
    result.incUsedBytes(_format->getBuffer().size());
    result.incAllocatedBytes(sizeof(Entry) * _lids.capacity());
    result.incUsedBytes(sizeof(Entry) * _lids.size());
    return result;
}

vespalib::nbostream &
ChunkMeta::deserialize(vespalib::nbostream & is)
{
    return is >> _offset >> _size >> _lastSerial >> _numEntries;
}

vespalib::nbostream &
ChunkMeta::serialize(vespalib::nbostream & os) const
{
    return os << _offset << _size << _lastSerial << _numEntries;
}

vespalib::nbostream &
LidMeta::deserialize(vespalib::nbostream & is)
{
    return is >> _lid >> _size;
}

vespalib::nbostream &
LidMeta::serialize(vespalib::nbostream & os) const
{
    return os << _lid << _size;
}

} // namespace search