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

#include "reference_mappings.h"
#include "reference.h"
#include <vespa/vespalib/datastore/datastore.hpp>
#include <vespa/vespalib/btree/btreestore.hpp>
#include <vespa/vespalib/btree/btreenode.hpp>
#include <vespa/vespalib/util/rcuvector.hpp>

namespace search::attribute {

ReferenceMappings::ReferenceMappings(GenerationHolder& genHolder,
                                     const std::atomic<uint32_t>& committedDocIdLimit,
                                     const vespalib::alloc::Alloc& initial_alloc)
    : _reverseMappingIndices(vespalib::GrowStrategy(16, 1.0, 0, 0), genHolder, initial_alloc),
      _targetLidLimit(0),
      _reverseMapping(),
      _targetLids(vespalib::GrowStrategy(16, 1.0, 0, 0), genHolder, initial_alloc),
      _committedDocIdLimit(committedDocIdLimit)
{
}

ReferenceMappings::~ReferenceMappings() = default;

void
ReferenceMappings::clearMapping(const Reference &entry)
{
    EntryRef revMapIdx = entry.revMapIdx();
    if (revMapIdx.valid()) {
        _reverseMapping.clear(revMapIdx);
    }
}

void
ReferenceMappings::syncForwardMapping(const Reference &entry)
{
    uint32_t targetLid = entry.lid();
    EntryRef revMapIdx = entry.revMapIdx();
    auto &targetLids = _targetLids;
    _reverseMapping.foreach_unfrozen_key(revMapIdx,
                                         [&targetLids, targetLid](uint32_t lid)
                                         { targetLids[lid].store_release(targetLid); });
}

void
ReferenceMappings::syncReverseMappingIndices(const Reference &entry)
{
    uint32_t targetLid = entry.lid();
    if (targetLid != 0u) {
        _reverseMappingIndices.ensure_size(targetLid + 1);
        _reverseMappingIndices[targetLid].store_release(entry.revMapIdx());
        if (targetLid >= _targetLidLimit.load(std::memory_order_relaxed)) {
            _targetLidLimit.store(targetLid + 1, std::memory_order_release);
        }
    }
}

void
ReferenceMappings::removeReverseMapping(const Reference &entry, uint32_t lid)
{
    EntryRef revMapIdx = entry.revMapIdx();
    _reverseMapping.apply(revMapIdx, nullptr, nullptr, &lid, &lid + 1);
    std::atomic_thread_fence(std::memory_order_release);
    entry.setRevMapIdx(revMapIdx);
    syncReverseMappingIndices(entry);
    _targetLids[lid].store_release(0); // forward mapping
}

void
ReferenceMappings::addReverseMapping(const Reference &entry, uint32_t lid)
{
    EntryRef revMapIdx = entry.revMapIdx();
    ReverseMapping::KeyDataType add(lid, vespalib::btree::BTreeNoLeafData());
    _reverseMapping.apply(revMapIdx, &add, &add + 1, nullptr, nullptr);
    std::atomic_thread_fence(std::memory_order_release);
    entry.setRevMapIdx(revMapIdx);
    syncReverseMappingIndices(entry);
    _targetLids[lid].store_release(entry.lid()); // forward mapping
}

void
ReferenceMappings::buildReverseMapping(const Reference &entry, const std::vector<ReverseMapping::KeyDataType> &adds)
{
    EntryRef revMapIdx = entry.revMapIdx();
    assert(!revMapIdx.valid());
    _reverseMapping.apply(revMapIdx, &adds[0], &adds[adds.size()], nullptr, nullptr);
    entry.setRevMapIdx(revMapIdx);
}

void
ReferenceMappings::notifyReferencedPut(const Reference &entry, uint32_t targetLid)
{
    uint32_t oldTargetLid = entry.lid();
    if (oldTargetLid != targetLid) {
        if (oldTargetLid != 0u && oldTargetLid < _reverseMappingIndices.size()) {
            _reverseMappingIndices[oldTargetLid].store_release(EntryRef());
        }
        entry.setLid(targetLid);
    }
    syncReverseMappingIndices(entry);
    syncForwardMapping(entry);
}

void
ReferenceMappings::notifyReferencedRemove(const Reference &entry)
{
    uint32_t oldTargetLid = entry.lid();
    if (oldTargetLid != 0) {
        if (oldTargetLid < _reverseMappingIndices.size()) {
            _reverseMappingIndices[oldTargetLid].store_release(EntryRef());
        }
        entry.setLid(0);
    }
    syncReverseMappingIndices(entry);
    syncForwardMapping(entry);
}

void
ReferenceMappings::onAddDocs(uint32_t docIdLimit)
{
    _targetLids.reserve(docIdLimit);
}

void
ReferenceMappings::addDoc()
{
    _targetLids.push_back(AtomicTargetLid(0));
}

void
ReferenceMappings::onLoad(uint32_t docIdLimit)
{
    _targetLids.clear();
    _targetLids.unsafe_reserve(docIdLimit);
    _targetLids.ensure_size(docIdLimit);
}

void
ReferenceMappings::shrink(uint32_t docIdLimit)
{
    _targetLids.shrink(docIdLimit);
}

vespalib::MemoryUsage
ReferenceMappings::getMemoryUsage()
{
    vespalib::MemoryUsage usage = _reverseMapping.getMemoryUsage();
    usage.merge(_reverseMappingIndices.getMemoryUsage());
    usage.merge(_targetLids.getMemoryUsage());
    return usage;
}

}