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

#include "lidstatevector.h"
#include <cassert>

namespace proton {

using vespalib::GenerationHolder;

LidStateVector::LidStateVector(unsigned int newSize, unsigned int newCapacity,
                               GenerationHolder &generationHolder,
                               bool trackLowest, bool trackHighest)
    : _bv(newSize, newCapacity, generationHolder),
      _lowest(trackLowest ? newSize : 0u),
      _highest(0),
      _trackLowest(trackLowest),
      _trackHighest(trackHighest)
{
}

LidStateVector::~LidStateVector() = default;

void
LidStateVector::resizeVector(uint32_t newSize, uint32_t newCapacity)
{
    uint32_t lowest = getLowest();
    uint32_t highest = getHighest();
    assert(!_trackLowest || lowest <= _bv.writer().size());
    assert(!_trackHighest || _bv.writer().size() == 0 || highest < _bv.writer().size());
    bool nolowest(lowest == _bv.writer().size());
    if (_bv.writer().size() > newSize) {
        _bv.shrink(newSize);
    }
    if (_bv.writer().capacity() < newCapacity) {
        _bv.reserve(newCapacity);
    }
    if (_bv.writer().size() < newSize) {
        _bv.extend(newSize);
    }
    if (_trackLowest) {
        if (nolowest || lowest > _bv.writer().size()) {
            lowest = _bv.writer().size();
            _lowest.store(lowest, std::memory_order_relaxed);
        }
    }
    if (_trackHighest) {
        if (highest >= _bv.writer().size()) {
            highest = _bv.writer().size() > 0 ? _bv.writer().getPrevTrueBit(_bv.writer().size() - 1) : 0;
            _highest.store(highest, std::memory_order_relaxed);
        }
    }
}

void
LidStateVector::updateLowest(uint32_t lowest)
{
    lowest = _bv.writer().getNextTrueBit(lowest);
    assert(lowest <= _bv.writer().size());
    _lowest.store(lowest, std::memory_order_relaxed);
}

void
LidStateVector::updateHighest(uint32_t highest)
{
    highest = _bv.writer().getPrevTrueBit(highest);
    assert(_bv.writer().size() == 0 || highest < _bv.writer().size());
    _highest.store(highest, std::memory_order_relaxed);
}

void
LidStateVector::setBit(unsigned int idx)
{
    assert(idx < _bv.writer().size());
    if (_trackLowest && idx < getLowest()) {
        _lowest.store(idx, std::memory_order_relaxed);
    }
    if (_trackHighest && idx > getHighest()) {
        _highest.store(idx, std::memory_order_relaxed);
    }
    assert(!_bv.writer().testBit(idx));
    _bv.writer().setBitAndMaintainCount(idx);
}

template <bool do_set>
uint32_t
LidStateVector::assert_is_not_set_then_set_bits_helper(const std::vector<uint32_t>& idxs)
{
    uint32_t size = _bv.writer().size();
    uint32_t high = 0;
    uint32_t low = size;
    for (auto idx : idxs) {
        assert(idx < size);
        if (idx > high) {
            high = idx;
        }
        assert(!_bv.writer().testBit(idx));
        if (do_set) {
            if (idx < low) {
                low = idx;
            }
            _bv.writer().setBitAndMaintainCount(idx);
        }
    }
    if (do_set) {
        if (_trackLowest && low < getLowest()) {
            _lowest.store(low, std::memory_order_relaxed);
        }
        if (_trackHighest && high > getHighest()) {
            _highest.store(high, std::memory_order_relaxed);
        }
    }
    return high;
}

uint32_t
LidStateVector::assert_not_set_bits(const std::vector<uint32_t>& idxs)
{
    return assert_is_not_set_then_set_bits_helper<false>(idxs);
}

uint32_t 
LidStateVector::set_bits(const std::vector<uint32_t>& idxs)
{
    return assert_is_not_set_then_set_bits_helper<true>(idxs);
}


void
LidStateVector::clearBit(unsigned int idx)
{
    assert(idx < _bv.writer().size());
    assert(_bv.writer().testBit(idx));
    _bv.writer().clearBitAndMaintainCount(idx);
    maybeUpdateLowest();
    maybeUpdateHighest();
}

template <bool do_assert>
void
LidStateVector::assert_is_set_then_clear_bits_helper(const std::vector<uint32_t>& idxs)
{
    for (auto idx : idxs) {
        if (do_assert) {
            assert(_bv.writer().testBit(idx));
        }
        _bv.writer().clearBitAndMaintainCount(idx);
    }
    maybeUpdateLowest();
    maybeUpdateHighest();
}

void 
LidStateVector::consider_clear_bits(const std::vector<uint32_t>& idxs)
{
    assert_is_set_then_clear_bits_helper<false>(idxs);
}

void 
LidStateVector::clear_bits(const std::vector<uint32_t>& idxs)
{
    assert_is_set_then_clear_bits_helper<true>(idxs);
}

}  // namespace proton