aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/docstore/documentstore.cpp
blob: 7d585007d76607602f7363999073df863e4358bf (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "documentstore.h"
#include "visitcache.h"
#include "ibucketizer.h"
#include "value.h"
#include <vespa/document/fieldvalue/document.h>
#include <vespa/vespalib/stllike/cache.hpp>
#include <vespa/vespalib/data/databuffer.h>
#include <vespa/vespalib/util/compressor.h>
#include <vespa/vespalib/util/size_literals.h>

#include <vespa/log/log.h>

LOG_SETUP(".searchlib.docstore.documentstore");

using document::DocumentTypeRepo;
using vespalib::CacheStats;
using vespalib::compression::CompressionConfig;

namespace search {

namespace {

class DocumentVisitorAdapter : public IBufferVisitor
{
public:
    DocumentVisitorAdapter(const DocumentTypeRepo & repo, IDocumentVisitor & visitor) :
        _repo(repo),
        _visitor(visitor)
    { }
    void visit(uint32_t lid, vespalib::ConstBufferRef buf) override;
private:
    const DocumentTypeRepo & _repo;
    IDocumentVisitor & _visitor;
};

void
DocumentVisitorAdapter::visit(uint32_t lid, vespalib::ConstBufferRef buf) {
    if (buf.size() > 0) {
        vespalib::nbostream is(buf.c_str(), buf.size());
        _visitor.visit(lid, std::make_unique<document::Document>(_repo, is));
    }
}

}

using vespalib::nbostream;

namespace docstore {

class BackingStore {
public:
    BackingStore(IDataStore &store, CompressionConfig compression) :
        _backingStore(store),
        _compression(compression)
    { }

    bool read(DocumentIdT key, Value &value) const;
    void visit(const IDocumentStore::LidVector &lids, const DocumentTypeRepo &repo, IDocumentVisitor &visitor) const;
    void write(DocumentIdT, const Value &);
    void erase(DocumentIdT) {}
    CompressionConfig getCompression() const { return _compression.load(std::memory_order_relaxed); }
    void reconfigure(CompressionConfig compression);
private:
    IDataStore &_backingStore;
    std::atomic<CompressionConfig> _compression;
};

void
BackingStore::visit(const IDocumentStore::LidVector &lids, const DocumentTypeRepo &repo,
                    IDocumentVisitor &visitor) const {
    DocumentVisitorAdapter adapter(repo, visitor);
    _backingStore.read(lids, adapter);
}

bool
BackingStore::read(DocumentIdT key, Value &value) const {
    bool found(false);
    vespalib::DataBuffer buf(4_Ki);
    ssize_t len = _backingStore.read(key, buf);
    if (len > 0) {
        value.set(std::move(buf), len, getCompression());
        found = true;
    }
    return found;
}

void
BackingStore::write(DocumentIdT lid, const Value & value)
{
    Value::Result buf = value.decompressed();
    assert(buf.second);
    _backingStore.write(value.getSyncToken(), lid, buf.first.getData(), buf.first.getDataLen());
}

void
BackingStore::reconfigure(CompressionConfig compression) {
    _compression.store(compression, std::memory_order_relaxed);
}

using CacheParams = vespalib::CacheParam<
        vespalib::LruParam<DocumentIdT, docstore::Value>,
        docstore::BackingStore,
        vespalib::zero<DocumentIdT>,
        vespalib::size<docstore::Value> >;

class Cache : public vespalib::cache<CacheParams> {
public:
    Cache(BackingStore & b, size_t maxBytes) : vespalib::cache<CacheParams>(b, maxBytes) { }
};

}

using docstore::Value;

bool
DocumentStore::Config::operator == (const Config &rhs) const {
    return  (_maxCacheBytes == rhs._maxCacheBytes) &&
            (_updateStrategy == rhs._updateStrategy) &&
            (_compression == rhs._compression);
}


DocumentStore::DocumentStore(const Config & config, IDataStore & store)
    : IDocumentStore(),
      _backingStore(store),
      _store(std::make_unique<docstore::BackingStore>(_backingStore, config.getCompression())),
      _cache(std::make_unique<docstore::Cache>(*_store, config.getMaxCacheBytes())),
      _visitCache(std::make_unique<docstore::VisitCache>(store, config.getMaxCacheBytes(), config.getCompression())),
      _updateStrategy(config.updateStrategy()),
      _uncached_lookups(0)
{ }

DocumentStore::~DocumentStore() = default;

void
DocumentStore::reconfigure(const Config & config) {
    _cache->setCapacityBytes(config.getMaxCacheBytes());
    _store->reconfigure(config.getCompression());
    _visitCache->reconfigure(config.getMaxCacheBytes(), config.getCompression());
    _updateStrategy.store(config.updateStrategy(), std::memory_order_relaxed);
}

bool
DocumentStore::useCache() const {
    return (_cache->capacityBytes() != 0) && (_cache->capacity() != 0);
}

DocumentStore::Config::UpdateStrategy DocumentStore::updateStrategy() const {
    return _updateStrategy.load(std::memory_order_relaxed);
}

void
DocumentStore::visit(const LidVector & lids, const DocumentTypeRepo &repo, IDocumentVisitor & visitor) const
{
    if (useCache() && visitor.allowVisitCaching()) {
        docstore::BlobSet blobSet = _visitCache->read(lids).getBlobSet();
        DocumentVisitorAdapter adapter(repo, visitor);
        for (DocumentIdT lid : lids) {
            adapter.visit(lid, blobSet.get(lid));
        }
    } else {
        _store->visit(lids, repo, visitor);
    }
}

std::unique_ptr<document::Document>
DocumentStore::read(DocumentIdT lid, const DocumentTypeRepo &repo) const
{
    Value value;
    if (useCache()) {
        value = _cache->read(lid);
        if (value.empty()) {
            return std::unique_ptr<document::Document>();
        }
        Value::Result result = value.decompressed();
        if ( result.second ) {
            return std::make_unique<document::Document>(repo, std::move(result.first));
        } else {
            LOG(warning, "Summary cache for lid %u is corrupt. Invalidating and reading directly from backing store", lid);
            _cache->invalidate(lid);
        }
    }

    _uncached_lookups.fetch_add(1);
    _store->read(lid, value);
    if ( ! value.empty() ) {
        Value::Result result = value.decompressed();
        assert(result.second);
        return std::make_unique<document::Document>(repo, std::move(result.first));
    }
    return std::unique_ptr<document::Document>();
}

void
DocumentStore::write(uint64_t syncToken, DocumentIdT lid, const document::Document& doc) {
    nbostream stream(12345);
    doc.serialize(stream);
    write(syncToken, lid, stream);
}

void
DocumentStore::write(uint64_t syncToken, DocumentIdT lid, const vespalib::nbostream & stream) {
    if (useCache()) {
        switch (updateStrategy()) {
            case Config::UpdateStrategy::INVALIDATE:
                _backingStore.write(syncToken, lid, stream.peek(), stream.size());
                _cache->invalidate(lid);
                break;
            case Config::UpdateStrategy::UPDATE:
                if (_cache->hasKey(lid)) {
                    Value value(syncToken);
                    vespalib::DataBuffer buf(stream.size());
                    buf.writeBytes(stream.peek(), stream.size());
                    value.set(std::move(buf), stream.size(), _store->getCompression());
                    _cache->write(lid, std::move(value));
                } else {
                    _backingStore.write(syncToken, lid, stream.peek(), stream.size());
                }
                break;
        }
        _visitCache->invalidate(lid); // The cost and complexity of this updating this is not worth it.
    } else {
        _backingStore.write(syncToken, lid, stream.peek(), stream.size());
    }
}

void
DocumentStore::remove(uint64_t syncToken, DocumentIdT lid)
{
    _backingStore.remove(syncToken, lid);
    if (useCache()) {
        _cache->invalidate(lid);
        _visitCache->invalidate(lid);
    }
}

void
DocumentStore::compactBloat(uint64_t syncToken)
{
    (void) syncToken;
    // Most implementations does not offer compact.
}

void
DocumentStore::compactSpread(uint64_t syncToken)
{
    (void) syncToken;
    // Most implementations does not offer compact.
}

void
DocumentStore::flush(uint64_t syncToken)
{
    _backingStore.flush(syncToken);
}

uint64_t
DocumentStore::initFlush(uint64_t syncToken)
{
    return _backingStore.initFlush(syncToken);
}

uint64_t
DocumentStore::lastSyncToken() const
{
    return _backingStore.lastSyncToken();
}

uint64_t
DocumentStore::tentativeLastSyncToken() const
{
    return _backingStore.tentativeLastSyncToken();
}

vespalib::system_time
DocumentStore::getLastFlushTime() const
{
    return _backingStore.getLastFlushTime();
}

template <class Visitor>
class DocumentStore::WrapVisitor : public IDataStoreVisitor
{
    Visitor                 &_visitor;
    const DocumentTypeRepo  &_repo;
    const CompressionConfig  _compression;
    IDocumentStore          &_ds;
    uint64_t                 _syncToken;
    
public:
    void visit(uint32_t lid, const void *buffer, size_t sz) override;

    WrapVisitor(Visitor &visitor, const DocumentTypeRepo &repo, CompressionConfig compresion,
                IDocumentStore &ds, uint64_t syncToken);
    
    void rewrite(uint32_t lid, const document::Document &doc);
    void rewrite(uint32_t lid);
    void visitRemove(uint32_t lid);
};


class DocumentStore::WrapVisitorProgress : public IDataStoreVisitorProgress
{
    IDocumentStoreVisitorProgress &_visitorProgress;
public:
    void updateProgress(double progress) override {
        _visitorProgress.updateProgress(progress);
    }
    
    explicit WrapVisitorProgress(IDocumentStoreVisitorProgress &visitProgress)
        : _visitorProgress(visitProgress)
    {
    }
};

template <>
void
DocumentStore::WrapVisitor<IDocumentStoreReadVisitor>::
rewrite(uint32_t , const document::Document &)
{
}

template <>
void
DocumentStore::WrapVisitor<IDocumentStoreReadVisitor>::rewrite(uint32_t )
{
}

template <>
void
DocumentStore::WrapVisitor<IDocumentStoreReadVisitor>::visitRemove(uint32_t lid)
{
    _visitor.visit(lid);
}

template <>
void
DocumentStore::WrapVisitor<IDocumentStoreRewriteVisitor>::
rewrite(uint32_t lid, const document::Document &doc)
{
    _ds.write(_syncToken, lid, doc);
}

template <>
void
DocumentStore::WrapVisitor<IDocumentStoreRewriteVisitor>::rewrite(uint32_t lid)
{
    _ds.remove(_syncToken, lid);
}

template <>
void
DocumentStore::WrapVisitor<IDocumentStoreRewriteVisitor>::visitRemove(uint32_t )
{
}

template <class Visitor>
void
DocumentStore::WrapVisitor<Visitor>::visit(uint32_t lid, const void *buffer, size_t sz)
{
    Value value;
    vespalib::DataBuffer buf(4_Ki);
    buf.clear();
    buf.writeBytes(buffer, sz);
    ssize_t len = sz;
    if (len > 0) {
        value.set(std::move(buf), len);
    }
    if (! value.empty()) {
        auto doc = std::make_shared<document::Document>(_repo, value.decompressed().first);
        _visitor.visit(lid, doc);
        rewrite(lid, *doc);
    } else {
        visitRemove(lid);
        rewrite(lid);
    }
}

template <class Visitor>
DocumentStore::WrapVisitor<Visitor>::
WrapVisitor(Visitor &visitor, const DocumentTypeRepo &repo, CompressionConfig compression,
            IDocumentStore &ds, uint64_t syncToken)
    : _visitor(visitor),
      _repo(repo),
      _compression(compression),
      _ds(ds),
      _syncToken(syncToken)
{
}

void
DocumentStore::accept(IDocumentStoreReadVisitor &visitor, IDocumentStoreVisitorProgress &visitorProgress,
                      const DocumentTypeRepo &repo)
{
    WrapVisitor<IDocumentStoreReadVisitor> wrap(visitor, repo, _store->getCompression(), *this,
                                                _backingStore.tentativeLastSyncToken());
    WrapVisitorProgress wrapVisitorProgress(visitorProgress);
    _backingStore.accept(wrap, wrapVisitorProgress, false);
}

void
DocumentStore::accept(IDocumentStoreRewriteVisitor &visitor, IDocumentStoreVisitorProgress &visitorProgress,
                      const DocumentTypeRepo &repo)
{
    WrapVisitor<IDocumentStoreRewriteVisitor> wrap(visitor, repo, _store->getCompression(), *this,
                                                   _backingStore.tentativeLastSyncToken());
    WrapVisitorProgress wrapVisitorProgress(visitorProgress);
    _backingStore.accept(wrap, wrapVisitorProgress, true);
}

double
DocumentStore::getVisitCost() const
{
    return _backingStore.getVisitCost();
}

DataStoreStorageStats
DocumentStore::getStorageStats() const
{
    return _backingStore.getStorageStats();
}

vespalib::MemoryUsage
DocumentStore::getMemoryUsage() const
{
    vespalib::MemoryUsage usage = _backingStore.getMemoryUsage();
    usage.merge(_cache->getStaticMemoryUsage());
    usage.merge(_visitCache->getStaticMemoryUsage());
    return usage;
}

std::vector<DataStoreFileChunkStats>
DocumentStore::getFileChunkStats() const
{
    return _backingStore.getFileChunkStats();
}

CacheStats
DocumentStore::getCacheStats() const {
    CacheStats visitStats = _visitCache->getCacheStats();
    CacheStats singleStats = _cache->get_stats();
    singleStats.add_extra_misses(_uncached_lookups.load(std::memory_order_relaxed));
    singleStats += visitStats;
    return singleStats;
}

void
DocumentStore::compactLidSpace(uint32_t wantedDocLidLimit)
{
    _backingStore.compactLidSpace(wantedDocLidLimit);
}

bool
DocumentStore::canShrinkLidSpace() const
{
    return _backingStore.canShrinkLidSpace();
}

size_t
DocumentStore::getEstimatedShrinkLidSpaceGain() const
{
    return _backingStore.getEstimatedShrinkLidSpaceGain();
}

void
DocumentStore::shrinkLidSpace()
{
    _backingStore.shrinkLidSpace();
}

}