aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/datastore/unique_store.hpp
blob: 928dbea89ed09c6c3b76fb87404840ec3e4401b7 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "unique_store.h"
#include "datastore.hpp"
#include <vespa/searchlib/btree/btree.hpp>
#include <vespa/searchlib/btree/btreebuilder.hpp>
#include <vespa/searchlib/btree/btreeroot.hpp>
#include <vespa/searchlib/btree/btreenodeallocator.hpp>
#include <vespa/searchlib/btree/btreeiterator.hpp>
#include <vespa/searchlib/btree/btreenode.hpp>
#include <vespa/searchlib/util/bufferwriter.h>
#include "unique_store_builder.hpp"
#include "unique_store_saver.hpp"
#include <atomic>

namespace search::datastore {

constexpr size_t NUMCLUSTERS_FOR_NEW_UNIQUESTORE_BUFFER = 1024u;
constexpr float ALLOC_GROW_FACTOR = 0.2;

template <typename EntryT, typename RefT>
UniqueStore<EntryT, RefT>::UniqueStore()
    : _store(),
      _typeHandler(1, 2u, RefT::offsetSize(), NUMCLUSTERS_FOR_NEW_UNIQUESTORE_BUFFER, ALLOC_GROW_FACTOR),
      _typeId(0),
      _dict()
{
    _typeId = _store.addType(&_typeHandler);
    assert(_typeId == 0u);
    _store.initActiveBuffers();
}

template <typename EntryT, typename RefT>
UniqueStore<EntryT, RefT>::~UniqueStore()
{
    _store.clearHoldLists();
    _store.dropBuffers();
}

template <typename EntryT, typename RefT>
typename UniqueStore<EntryT, RefT>::AddResult
UniqueStore<EntryT, RefT>::add(const EntryType &value)
{
    Compare comp(_store, value);
    auto itr = _dict.lowerBound(RefType(), comp);
    if (itr.valid() && !comp(EntryRef(), itr.getKey())) {
        uint32_t refCount = itr.getData();
        assert(refCount != std::numeric_limits<uint32_t>::max());
        itr.writeData(refCount + 1);
        RefType iRef(itr.getKey());
        return AddResult(itr.getKey(), false);

    } else {
        EntryRef newRef = _store.template allocator<EntryType>(_typeId).alloc(value).ref;
        _dict.insert(itr, newRef, 1u);
        return AddResult(newRef, true);
    }
}

template <typename EntryT, typename RefT>
EntryRef
UniqueStore<EntryT, RefT>::find(const EntryType &value)
{
    Compare comp(_store, value);
    auto itr = _dict.lowerBound(RefType(), comp);
    if (itr.valid() && !comp(EntryRef(), itr.getKey())) {
        return itr.getKey();
    } else {
        return EntryRef();
    }
}

template <typename EntryT, typename RefT>
EntryRef
UniqueStore<EntryT, RefT>::move(EntryRef ref)
{
    return _store.template allocator<EntryType>(_typeId).alloc(get(ref)).ref;
}

template <typename EntryT, typename RefT>
void
UniqueStore<EntryT, RefT>::remove(EntryRef ref)
{
    assert(ref.valid());
    EntryType unused{};
    Compare comp(_store, unused);
    auto itr = _dict.lowerBound(ref, comp);
    if (itr.valid() && itr.getKey() == ref) {
        uint32_t refCount = itr.getData();
        if (refCount > 1) {
            itr.writeData(refCount - 1);
        } else {
            _dict.remove(itr);
            _store.holdElem(ref, 1);
        }
    }
}

namespace uniquestore {

template <typename EntryT, typename RefT>
class CompactionContext : public ICompactionContext {
private:
    using UniqueStoreType = UniqueStore<EntryT, RefT>;
    using Dictionary = typename UniqueStoreType::Dictionary;
    DataStoreBase &_dataStore;
    Dictionary &_dict;
    UniqueStoreType &_store;
    std::vector<uint32_t> _bufferIdsToCompact;
    std::vector<std::vector<EntryRef>> _mapping;

    bool compactingBuffer(uint32_t bufferId) {
        return std::find(_bufferIdsToCompact.begin(), _bufferIdsToCompact.end(),
                         bufferId) != _bufferIdsToCompact.end();
    }

    void allocMapping() {
        _mapping.resize(RefT::numBuffers());
        for (const auto bufferId : _bufferIdsToCompact) {
            BufferState &state = _dataStore.getBufferState(bufferId);
            _mapping[bufferId].resize(state.size());
        }
    }

    void fillMapping() {
        auto itr = _dict.begin();
        while (itr.valid()) {
            RefT iRef(itr.getKey());
            assert(iRef.valid());
            if (compactingBuffer(iRef.bufferId())) {
                assert(iRef.offset() < _mapping[iRef.bufferId()].size());
                EntryRef &mappedRef = _mapping[iRef.bufferId()][iRef.offset()];
                assert(!mappedRef.valid());
                EntryRef newRef = _store.move(itr.getKey());
                mappedRef = newRef;
                _dict.thaw(itr);
                itr.writeKey(newRef);
            }
            ++itr;
        }
    }

public:
    CompactionContext(DataStoreBase &dataStore,
                      Dictionary &dict,
                      UniqueStoreType &store,
                      std::vector<uint32_t> bufferIdsToCompact)
        : _dataStore(dataStore),
          _dict(dict),
          _store(store),
          _bufferIdsToCompact(std::move(bufferIdsToCompact)),
          _mapping()
    {
    }
    virtual ~CompactionContext() {
        _dataStore.finishCompact(_bufferIdsToCompact);
    }
    virtual void compact(vespalib::ArrayRef<EntryRef> refs) override {
        if (!_bufferIdsToCompact.empty()) {
            if (_mapping.empty()) {
                allocMapping();
                fillMapping();
            }
            for (auto &ref : refs) {
                if (ref.valid()) {
                    RefT internalRef(ref);
                    if (compactingBuffer(internalRef.bufferId())) {
                        assert(internalRef.offset() < _mapping[internalRef.bufferId()].size());
                        EntryRef newRef = _mapping[internalRef.bufferId()][internalRef.offset()];
                        assert(newRef.valid());
                        ref = newRef;
                    }
                }
            }
        }
    }
};

}

template <typename EntryT, typename RefT>
ICompactionContext::UP
UniqueStore<EntryT, RefT>::compactWorst()
{
    std::vector<uint32_t> bufferIdsToCompact = _store.startCompactWorstBuffers(true, true);
    return std::make_unique<uniquestore::CompactionContext<EntryT, RefT>>
        (_store, _dict, *this, std::move(bufferIdsToCompact));
}

template <typename EntryT, typename RefT>
MemoryUsage
UniqueStore<EntryT, RefT>::getMemoryUsage() const
{
    MemoryUsage usage = _store.getMemoryUsage();
    usage.merge(_dict.getMemoryUsage());
    return usage;
}

template <typename EntryT, typename RefT>
const BufferState &
UniqueStore<EntryT, RefT>::bufferState(EntryRef ref) const
{
    RefT internalRef(ref);
    return _store.getBufferState(internalRef.bufferId());
}


template <typename EntryT, typename RefT>
void
UniqueStore<EntryT, RefT>::transferHoldLists(generation_t generation)
{
    _dict.getAllocator().transferHoldLists(generation);
    _store.transferHoldLists(generation);
}

template <typename EntryT, typename RefT>
void
UniqueStore<EntryT, RefT>::trimHoldLists(generation_t firstUsed)
{
    _dict.getAllocator().trimHoldLists(firstUsed);
    _store.trimHoldLists(firstUsed);
}

template <typename EntryT, typename RefT>
void
UniqueStore<EntryT, RefT>::freeze()
{
    _dict.getAllocator().freeze();
}

template <typename EntryT, typename RefT>
typename UniqueStore<EntryT, RefT>::Builder
UniqueStore<EntryT, RefT>::getBuilder(uint32_t uniqueValuesHint)
{
    return Builder(_store, _typeId, _dict, uniqueValuesHint);
}

template <typename EntryT, typename RefT>
typename UniqueStore<EntryT, RefT>::Saver
UniqueStore<EntryT, RefT>::getSaver() const
{
    return Saver(_dict, _store);
}

template <typename EntryT, typename RefT>
uint32_t
UniqueStore<EntryT, RefT>::getNumUniques() const
{
    return _dict.getFrozenView().size();
}

}