aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/postinglistattribute.cpp
blob: d3f9c3f5d8282bb525e74158eb8f3925d035b709 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "postinglistattribute.h"
#include "loadednumericvalue.h"
#include "enumcomparator.h"
#include "enum_store_loaders.h"
#include <vespa/vespalib/util/array.hpp>

namespace search {

using attribute::LoadedNumericValue;

template <typename P>
PostingListAttributeBase<P>::
PostingListAttributeBase(AttributeVector &attr,
                         IEnumStore &enumStore)
    : attribute::IPostingListAttributeBase(),
      _posting_store(enumStore.get_dictionary(), attr.getStatus(),
                     attr.getConfig()),
      _attr(attr),
      _dictionary(enumStore.get_dictionary())
{ }

template <typename P>
PostingListAttributeBase<P>::~PostingListAttributeBase() = default;

template <typename P>
void
PostingListAttributeBase<P>::clearAllPostings()
{
    _posting_store.clearBuilder();
    _attr.incGeneration(); // Force freeze
    auto clearer = [this](EntryRef posting_idx)
                   {
                       _posting_store.clear(posting_idx);
                   };
    _dictionary.clear_all_posting_lists(clearer);
    _attr.incGeneration(); // Force freeze
}


template <typename P>
void
PostingListAttributeBase<P>::handle_load_posting_lists_and_update_enum_store(enumstore::EnumeratedPostingsLoader& loader)
{
    clearAllPostings();
    uint32_t docIdLimit = _attr.getNumDocs();
    EntryRef newIndex;
    PostingChange<P> postings;
    const auto& loaded_enums = loader.get_loaded_enums();
    if (loaded_enums.empty()) {
        loader.build_empty_dictionary();
        return;
    }
    uint32_t preve = 0;
    uint32_t refCount = 0;

    vespalib::ConstArrayRef<EnumIndex> enum_indexes(loader.get_enum_indexes());
    assert(!enum_indexes.empty());
    auto posting_indexes = loader.initialize_empty_posting_indexes();
    uint32_t posting_enum = preve;
    for (const auto& elem : loaded_enums) {
        if (preve != elem.getEnum()) {
            assert(preve < elem.getEnum());
            assert(elem.getEnum() < enum_indexes.size());
            loader.set_ref_count(enum_indexes[preve], refCount);
            refCount = 0;
            preve = elem.getEnum();
            if (loader.is_folded_change(enum_indexes[posting_enum], enum_indexes[preve])) {
                postings.removeDups();
                newIndex = EntryRef();
                _posting_store.apply(newIndex,
                                   postings._additions.data(),
                                   postings._additions.data() +
                                   postings._additions.size(),
                                   postings._removals.data(),
                                   postings._removals.data() +
                                   postings._removals.size());
                posting_indexes[posting_enum] = newIndex;
                postings.clear();
                posting_enum = elem.getEnum();
            }
        }
        assert(refCount < std::numeric_limits<uint32_t>::max());
        ++refCount;
        assert(elem.getDocId() < docIdLimit);
        (void) docIdLimit;
        postings.add(elem.getDocId(), elem.getWeight());
    }
    assert(refCount != 0);
    loader.set_ref_count(enum_indexes[preve], refCount);
    postings.removeDups();
    newIndex = EntryRef();
    _posting_store.apply(newIndex,
                       postings._additions.data(),
                       postings._additions.data() + postings._additions.size(),
                       postings._removals.data(),
                       postings._removals.data() + postings._removals.size());
    posting_indexes[posting_enum] = newIndex;
    loader.build_dictionary();
    loader.free_unused_values();
}

template <typename P>
void
PostingListAttributeBase<P>::updatePostings(PostingMap &changePost,
                                            const vespalib::datastore::EntryComparator &cmp)
{
    for (auto& elem : changePost) {
        EnumIndex idx = elem.first.getEnumIdx();
        auto& change = elem.second;
        change.removeDups();
        auto updater= [this, &change](EntryRef posting_idx) -> EntryRef
                      {
                          _posting_store.apply(posting_idx,
                                             change._additions.data(),
                                             change._additions.data() + change._additions.size(),
                                             change._removals.data(),
                                             change._removals.data() + change._removals.size());
                          return posting_idx;
                      };
        _dictionary.update_posting_list(idx, cmp, updater);
    }
}

template <typename P>
bool
PostingListAttributeBase<P>::forwardedOnAddDoc(DocId doc,
                                               size_t wantSize,
                                               size_t wantCapacity)
{
    if (doc >= wantSize) {
        wantSize = doc + 1;
    }
    if (doc >= wantCapacity) {
        wantCapacity = doc + 1;
    }
    return _posting_store.resizeBitVectors(wantSize, wantCapacity);
}

template <typename P>
void
PostingListAttributeBase<P>::
clearPostings(attribute::IAttributeVector::EnumHandle eidx,
              uint32_t fromLid,
              uint32_t toLid,
              const vespalib::datastore::EntryComparator &cmp)
{
    PostingChange<P> postings;

    for (uint32_t lid = fromLid; lid < toLid; ++lid) {
        postings.remove(lid);
    }

    EntryRef er(eidx);
    auto updater = [this, &postings](EntryRef posting_idx) -> EntryRef
                   {
                       _posting_store.apply(posting_idx,
                                          postings._additions.data(),
                                          postings._additions.data() + postings._additions.size(),
                                          postings._removals.data(),
                                          postings._removals.data() + postings._removals.size());
                       return posting_idx;
                   };
    _dictionary.update_posting_list(er, cmp, updater);
}

template <typename P>
void
PostingListAttributeBase<P>::forwardedShrinkLidSpace(uint32_t newSize)
{
    (void) _posting_store.resizeBitVectors(newSize, newSize);
}

template <typename P>
attribute::PostingStoreMemoryUsage
PostingListAttributeBase<P>::getMemoryUsage() const
{
    return _posting_store.getMemoryUsage();
}

template <typename P>
bool
PostingListAttributeBase<P>::consider_compact_worst_btree_nodes(const CompactionStrategy& compaction_strategy)
{
    return _posting_store.consider_compact_worst_btree_nodes(compaction_strategy);
}

template <typename P>
bool
PostingListAttributeBase<P>::consider_compact_worst_buffers(const CompactionStrategy& compaction_strategy)
{
    return _posting_store.consider_compact_worst_buffers(compaction_strategy);
}

template <typename P, typename LoadedVector, typename LoadedValueType,
          typename EnumStoreType>
PostingListAttributeSubBase<P, LoadedVector, LoadedValueType, EnumStoreType>::
PostingListAttributeSubBase(AttributeVector &attr,
                            EnumStore &enumStore)
    : Parent(attr, enumStore),
      _es(enumStore)
{
}

template <typename P, typename LoadedVector, typename LoadedValueType,
          typename EnumStoreType>
PostingListAttributeSubBase<P, LoadedVector, LoadedValueType, EnumStoreType>::
~PostingListAttributeSubBase() = default;

template <typename P, typename LoadedVector, typename LoadedValueType,
          typename EnumStoreType>
void
PostingListAttributeSubBase<P, LoadedVector, LoadedValueType, EnumStoreType>::
handle_load_posting_lists(LoadedVector& loaded)
{
    if constexpr (!std::is_same_v<LoadedVector, NoLoadedVector>) {
        clearAllPostings();
        EntryRef newIndex;
        PostingChange<P> postings;
        uint32_t docIdLimit = _attr.getNumDocs();
        _posting_store.resizeBitVectors(docIdLimit, docIdLimit);
        if ( ! loaded.empty() ) {
            vespalib::Array<typename LoadedVector::Type> similarValues;
            auto value = loaded.read();
            LoadedValueType prev = value.getValue();
            for (size_t i(0), m(loaded.size()); i < m; i++, loaded.next()) {
                value = loaded.read();
                if (ComparatorType::equal_helper(prev, value.getValue())) {
                    // for single value attributes loaded[numDocs] is used
                    // for default value but we don't want to add an
                    // invalid docId to the posting list.
                    if (value._docId < docIdLimit) {
                        postings.add(value._docId, value.getWeight());
                        similarValues.push_back(value);
                    }
                } else {
                    postings.removeDups();
                    newIndex = EntryRef();
                    _posting_store.apply(newIndex,
                                       postings._additions.data(),
                                       postings._additions.data() +
                                       postings._additions.size(),
                                       postings._removals.data(),
                                       postings._removals.data() +
                                       postings._removals.size());
                    postings.clear();
                    if (value._docId < docIdLimit) {
                        postings.add(value._docId, value.getWeight());
                    }
                    similarValues[0]._pidx = newIndex;
                    for (size_t j(0), k(similarValues.size()); j < k; j++) {
                        loaded.write(similarValues[j]);
                    }
                    similarValues.clear();
                    similarValues.push_back(value);
                    prev = value.getValue();
                }
            }
            postings.removeDups();
            newIndex = EntryRef();
            _posting_store.apply(newIndex,
                               postings._additions.data(),
                               postings._additions.data() +
                               postings._additions.size(),
                               postings._removals.data(),
                               postings._removals.data() + postings._removals.size());
            similarValues[0]._pidx = newIndex;
            for (size_t i(0), m(similarValues.size()); i < m; i++) {
                loaded.write(similarValues[i]);
            }
        }
    }
}


template <typename P, typename LoadedVector, typename LoadedValueType,
          typename EnumStoreType>
void
PostingListAttributeSubBase<P, LoadedVector, LoadedValueType, EnumStoreType>::
updatePostings(PostingMap &changePost)
{
    updatePostings(changePost, _es.get_folded_comparator());
}


template <typename P, typename LoadedVector, typename LoadedValueType,
          typename EnumStoreType>
void
PostingListAttributeSubBase<P, LoadedVector, LoadedValueType, EnumStoreType>::
clearPostings(attribute::IAttributeVector::EnumHandle eidx, uint32_t fromLid, uint32_t toLid)
{
    clearPostings(eidx, fromLid, toLid, _es.get_folded_comparator());
}


template class PostingListAttributeBase<AttributePosting>;
template class PostingListAttributeBase<AttributeWeightPosting>;

using LoadedInt8Vector = SequentialReadModifyWriteInterface<LoadedNumericValue<int8_t> >;

using LoadedInt16Vector = SequentialReadModifyWriteInterface<LoadedNumericValue<int16_t> >;

using LoadedInt32Vector = SequentialReadModifyWriteInterface<LoadedNumericValue<int32_t> >;

using LoadedInt64Vector = SequentialReadModifyWriteInterface<LoadedNumericValue<int64_t> >;

using LoadedFloatVector = SequentialReadModifyWriteInterface<LoadedNumericValue<float> >;

using LoadedDoubleVector = SequentialReadModifyWriteInterface<LoadedNumericValue<double> >;
                                                       

template class
PostingListAttributeSubBase<AttributePosting,
                            LoadedInt8Vector,
                            int8_t,
                            EnumStoreT<int8_t>>;

template class
PostingListAttributeSubBase<AttributePosting,
                            LoadedInt16Vector,
                            int16_t,
                            EnumStoreT<int16_t>>;

template class
PostingListAttributeSubBase<AttributePosting,
                            LoadedInt32Vector,
                            int32_t,
                            EnumStoreT<int32_t>>;

template class
PostingListAttributeSubBase<AttributePosting,
                            LoadedInt64Vector,
                            int64_t,
                            EnumStoreT<int64_t>>;

template class
PostingListAttributeSubBase<AttributePosting,
                            LoadedFloatVector,
                            float,
                            EnumStoreT<float>>;

template class
PostingListAttributeSubBase<AttributePosting,
                            LoadedDoubleVector,
                            double,
                            EnumStoreT<double>>;

template class
PostingListAttributeSubBase<AttributePosting,
                            NoLoadedVector,
                            const char*,
                            EnumStoreT<const char*>>;

template class
PostingListAttributeSubBase<AttributeWeightPosting,
                            LoadedInt8Vector,
                            int8_t,
                            EnumStoreT<int8_t>>;

template class
PostingListAttributeSubBase<AttributeWeightPosting,
                            LoadedInt16Vector,
                            int16_t,
                            EnumStoreT<int16_t>>;

template class
PostingListAttributeSubBase<AttributeWeightPosting,
                            LoadedInt32Vector,
                            int32_t,
                            EnumStoreT<int32_t>>;

template class
PostingListAttributeSubBase<AttributeWeightPosting,
                            LoadedInt64Vector,
                            int64_t,
                            EnumStoreT<int64_t>>;

template class
PostingListAttributeSubBase<AttributeWeightPosting,
                            LoadedFloatVector,
                            float,
                            EnumStoreT<float>>;

template class
PostingListAttributeSubBase<AttributeWeightPosting,
                            LoadedDoubleVector,
                            double,
                            EnumStoreT<double>>;

template class
PostingListAttributeSubBase<AttributeWeightPosting,
                            NoLoadedVector,
                            const char*,
                            EnumStoreT<const char*>>;


}