aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/fixedsourceselector.cpp
blob: 749ef7d45e8fbffc90a1d99a06f13227850f14dc (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "fixedsourceselector.h"
#include <vespa/searchcommon/attribute/config.h>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.attribute.fixed_source_selector");

namespace search {

namespace {

attribute::Config getConfig() { return attribute::Config(attribute::BasicType::INT8); }

uint32_t
capSelector(queryeval::sourceselector::Iterator::SourceStore &store, queryeval::Source defaultSource)
{
    uint32_t committedDocIdLimit = store.getCommittedDocIdLimit();
    uint32_t cappedSources = 0;
    for (uint32_t docId = 0; docId < committedDocIdLimit; ++docId) {
        queryeval::Source source = store.getFast(docId);
        if (source > defaultSource) {
            ++cappedSources;
            store.set(docId, defaultSource);
        }
    }
    return cappedSources;
}

}

FixedSourceSelector::Iterator::Iterator(const FixedSourceSelector & sourceSelector) :
    IIterator(sourceSelector._source),
    _attributeGuard(sourceSelector._realSource)
{ }

FixedSourceSelector::FixedSourceSelector(queryeval::Source defaultSource,
                                         const vespalib::string & attrBaseFileName,
                                         uint32_t initialNumDocs) :
    SourceSelector(defaultSource, std::make_shared<SourceStore>(attrBaseFileName, getConfig())),
    _source(static_cast<SourceStore &>(*_realSource))
{
    if (initialNumDocs != std::numeric_limits<uint32_t>::max()) {
        reserve(initialNumDocs);
        _source.commit();
    }
}

FixedSourceSelector::~FixedSourceSelector() = default;

FixedSourceSelector::UP
FixedSourceSelector::cloneAndSubtract(const vespalib::string & attrBaseFileName, uint32_t diff)
{
    queryeval::Source newDefault = getNewSource(getDefaultSource(), diff);
    auto selector = std::make_unique< FixedSourceSelector>(newDefault, attrBaseFileName, _source.getNumDocs()-1);
    for (uint32_t docId = 0; docId < _source.getNumDocs(); ++docId) {
        queryeval::Source src = _source.get(docId);
        src = getNewSource(src, diff);
        assert(src < SOURCE_LIMIT);
        selector->_source.set(docId, src);
    }
    selector->_source.commit();
    selector->setBaseId(getBaseId() + diff);
    selector->_source.setCommittedDocIdLimit(_source.getCommittedDocIdLimit());
    return selector;
}

FixedSourceSelector::UP
FixedSourceSelector::load(const vespalib::string & baseFileName, uint32_t currentId)
{
    LoadInfo::UP info = extractLoadInfo(baseFileName);
    info->load();
    uint32_t defaultSource = currentId - info->header()._baseId;
    assert(defaultSource < SOURCE_LIMIT);
    if (defaultSource != info->header()._defaultSource) {
        LOG(info, "Default source mismatch: header says %u, should be %u selector %s",
            (uint32_t) info->header()._defaultSource, defaultSource,
            baseFileName.c_str());
    }
    auto selector = std::make_unique<FixedSourceSelector>(defaultSource,
                                                          info->header()._baseFileName,
                                                          std::numeric_limits<uint32_t>::max());
    selector->setBaseId(info->header()._baseId);
    selector->_source.load();
    uint32_t cappedSources = capSelector(selector->_source, selector->getDefaultSource());
    if (cappedSources > 0) {
        LOG(warning, "%u sources capped in source selector %s", cappedSources, baseFileName.c_str());
    }
    return selector;
}

void FixedSourceSelector::reserve(uint32_t numDocs)
{
    const uint32_t maxDoc(_source.getNumDocs());
    const uint32_t newMaxDocIdPlussOne(numDocs + 1);
    if (newMaxDocIdPlussOne > maxDoc) {
        uint32_t newDocId(0);
        for (_source.addDoc(newDocId); newDocId < numDocs; _source.addDoc(newDocId));
    }
    for (uint32_t i = _source.getCommittedDocIdLimit(); i < newMaxDocIdPlussOne; ++i) {
        _source.set(i, getDefaultSource());
    }
}

void
FixedSourceSelector::setSource(uint32_t docId, queryeval::Source source)
{
    assert(source < SOURCE_LIMIT);
    /**
     * Due to matchingloop advancing 1 past end, we need to initialize data that
     * far too.
     **/
    reserve(docId+1);
    _source.update(docId, source);
    _source.updateUncommittedDocIdLimit(docId + 1);
    _source.commit();
}

void
FixedSourceSelector::compactLidSpace(uint32_t lidLimit)
{
    if (lidLimit < _source.getCommittedDocIdLimit()) {
        _source.compactLidSpace(lidLimit + 1);
    }
}

} // namespace search