aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/diskindex/docidmapper.h
blob: 7c6f53720f27b006d8da2769194a028efa73b159 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/util/array.h>
#include <vespa/vespalib/stllike/string.h>
#include <cassert>

namespace search { class BitVector; }

namespace search::diskindex {

using SelectorArray = vespalib::Array<uint8_t>;

class DocIdMapping
{
public:
    const SelectorArray *_selector; // External ownership
    uint32_t _docIdLimit;
    uint8_t _selectorId;

    DocIdMapping();
    void clear();
    void setup(uint32_t docIdLimit);
    void setup(uint32_t docIdLimit, const SelectorArray *selector, uint8_t selectorId);
    bool readDocIdLimit(const vespalib::string &dir);
};


class DocIdMapper
{
public:
    const uint8_t *_selector;
    uint32_t _docIdLimit; // Limit on legal input values
    uint32_t _selectorLimit; // Limit on output
    uint8_t  _selectorId;

    DocIdMapper()
        : _selector(nullptr),
          _docIdLimit(0u),
          _selectorLimit(0),
          _selectorId(0u)
    { }

    void setup(const DocIdMapping &mapping) {
        _selector = (mapping._selector != nullptr) ? mapping._selector->data() : nullptr;
        _docIdLimit = mapping._docIdLimit;
        _selectorLimit = (mapping._selector != nullptr) ? mapping._selector->size() : 0u;
        _selectorId = mapping._selectorId;
    }

    static uint32_t noDocId() {
        return static_cast<uint32_t>(-1);
    }

    uint32_t mapDocId(uint32_t docId) const {
        assert(docId < _docIdLimit);
        if (_selector != nullptr && (docId >= _selectorLimit || _selector[docId] != _selectorId)) {
            docId = noDocId();
        }
        return docId;
    }
};

}