summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/predicate/predicate_index.cpp
blob: cf7d7dafa05e22d5a1c6b37814e2cd661cb37c18 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "predicate_index.h"
#include "predicate_hash.h"
#include <vespa/vespalib/datastore/buffer_type.hpp>
#include <vespa/vespalib/btree/btree.hpp>
#include <vespa/vespalib/btree/btreeroot.hpp>
#include <vespa/vespalib/btree/btreeiterator.hpp>
#include <vespa/vespalib/btree/btreestore.hpp>
#include <vespa/vespalib/btree/btreenodeallocator.hpp>


using vespalib::datastore::EntryRef;
using vespalib::DataBuffer;

namespace search::predicate {

template <>
void
PredicateIndex::addPosting<Interval>(uint64_t feature, uint32_t doc_id, EntryRef ref) {
    _interval_index.addPosting(feature, doc_id, ref);
}
template <>
void
PredicateIndex::addPosting<IntervalWithBounds>(uint64_t feature, uint32_t doc_id, EntryRef ref) {
    _bounds_index.addPosting(feature, doc_id, ref);
}

template <typename IntervalT>
void
PredicateIndex::indexDocumentFeatures(uint32_t doc_id, const PredicateIndex::FeatureMap<IntervalT> &interval_map) {
    for (const auto &map_entry : interval_map) {
        uint64_t feature = map_entry.first;
        const auto &interval_list = map_entry.second;
        EntryRef ref = _interval_store.insert(interval_list);
        assert(ref.valid());
        addPosting<IntervalT>(feature, doc_id, ref);
        _cache.set(feature, doc_id, true);
    }
}

namespace {
constexpr double THRESHOLD_USE_BIT_VECTOR_CACHE = 0.1;

// PostingSerializer that writes intervals from interval store based
// on the EntryRef that is to be serialized.
template <typename IntervalT>
class IntervalSerializer : public PostingSerializer<EntryRef> {
    const PredicateIntervalStore &_store;
public:
    IntervalSerializer(const PredicateIntervalStore &store) : _store(store) {}
    void serialize(const EntryRef &ref, DataBuffer &buffer) const override {
        uint32_t size;
        IntervalT single_buf;
        const IntervalT *interval = _store.get(ref, size, &single_buf);
        buffer.writeInt16(size);
        for (uint32_t i = 0; i < size; ++i) {
            interval[i].serialize(buffer);
        }
    }
};

// PostingDeserializer that writes intervals to interval store and
// returns an EntryRef to be stored in the PredicateIndex.
template <typename IntervalT>
class IntervalDeserializer : public PostingDeserializer<EntryRef> {
    PredicateIntervalStore &_store;
public:
    IntervalDeserializer(PredicateIntervalStore &store) : _store(store) {}
    EntryRef deserialize(DataBuffer &buffer) override {
        std::vector<IntervalT> intervals;
        size_t size = buffer.readInt16();
        for (uint32_t i = 0; i < size; ++i) {
            intervals.push_back(IntervalT::deserialize(buffer));
        }
        return _store.insert(intervals);
    }
};

}  // namespace

PredicateIndex::PredicateIndex(GenerationHolder &genHolder,
                               const DocIdLimitProvider &limit_provider,
                               const SimpleIndexConfig &simple_index_config, uint32_t arity)
    : _arity(arity),
      _limit_provider(limit_provider),
      _interval_index(genHolder, limit_provider, simple_index_config),
      _bounds_index(genHolder, limit_provider, simple_index_config),
      _interval_store(),
      _zero_constraint_docs(),
      _features_store(arity),
      _cache(genHolder)
{
}

PredicateIndex::PredicateIndex(GenerationHolder &genHolder,
                               const DocIdLimitProvider &limit_provider,
                               const SimpleIndexConfig &simple_index_config, DataBuffer &buffer,
                               SimpleIndexDeserializeObserver<> & observer, uint32_t version)
    : _arity(0),
      _limit_provider(limit_provider),
      _interval_index(genHolder, limit_provider, simple_index_config),
      _bounds_index(genHolder, limit_provider, simple_index_config),
      _interval_store(),
      _zero_constraint_docs(),
      _features_store(buffer),
      _cache(genHolder)
{
    _arity = buffer.readInt16();
    uint32_t zero_constraint_doc_count = buffer.readInt32();
    typename BTreeSet::Builder builder(_zero_constraint_docs.getAllocator());
    for (size_t i = 0; i < zero_constraint_doc_count; ++i) {
        uint32_t raw_id = buffer.readInt32();
        uint32_t doc_id = version == 0 ? raw_id >> 6 : raw_id;
        builder.insert(doc_id, vespalib::btree::BTreeNoLeafData::_instance);
        observer.notifyInsert(0, doc_id, 0);
    }
    _zero_constraint_docs.assign(builder);
    IntervalDeserializer<Interval> interval_deserializer(_interval_store);
    _interval_index.deserialize(buffer, interval_deserializer, observer, version);
    IntervalDeserializer<IntervalWithBounds> bounds_deserializer(_interval_store);
    _bounds_index.deserialize(buffer, bounds_deserializer, observer, version);
    commit();
}

PredicateIndex::~PredicateIndex() = default;

void
PredicateIndex::serialize(DataBuffer &buffer, SerializeStats& stats) const {
    _features_store.serialize(buffer);
    stats._features_len = buffer.getDataLen();
    auto old_len = buffer.getDataLen();
    buffer.writeInt16(_arity);
    buffer.writeInt32(_zero_constraint_docs.size());
    for (auto it = _zero_constraint_docs.begin(); it.valid(); ++it) {
        buffer.writeInt32(it.getKey());
    }
    stats._zeroes_len = buffer.getDataLen() - old_len;
    IntervalSerializer<Interval> interval_serializer(_interval_store);
    _interval_index.serialize(buffer, interval_serializer, stats._interval);
    IntervalSerializer<IntervalWithBounds> bounds_serializer(_interval_store);
    _bounds_index.serialize(buffer, bounds_serializer, stats._interval_with_bounds);
}

void
PredicateIndex::onDeserializationCompleted() {
    _interval_index.promoteOverThresholdVectors();
    _bounds_index.promoteOverThresholdVectors();
}

void
PredicateIndex::indexDocument(uint32_t doc_id, const PredicateTreeAnnotations &annotations) {
    indexDocumentFeatures(doc_id, annotations.interval_map);
    indexDocumentFeatures(doc_id, annotations.bounds_map);
    _features_store.insert(annotations, doc_id);
}

void
PredicateIndex::indexEmptyDocument(uint32_t doc_id)
{
    _zero_constraint_docs.insert(doc_id, vespalib::btree::BTreeNoLeafData::_instance);
}

namespace {
void
removeFromIndex(uint64_t feature, uint32_t doc_id, SimpleIndex<EntryRef> &index,
                PredicateIntervalStore &interval_store)
{
    auto result = index.removeFromPostingList(feature, doc_id);
    if (result.second) { // Posting was removed
        auto ref = result.first;
        assert(ref.valid());
        interval_store.remove(ref);
    }
}

class DocIdIterator : public PopulateInterface::Iterator {
public:
    using BTreeIterator = SimpleIndex<EntryRef>::BTreeIterator;

    DocIdIterator(BTreeIterator it) : _it(it) { }
    int32_t getNext() override {
        if (_it.valid()) {
            uint32_t docId = _it.getKey();
            ++_it;
            return docId;
        }
        return -1;
    }
private:
    BTreeIterator _it;
};

}  // namespace

void
PredicateIndex::removeDocument(uint32_t doc_id) {
    _zero_constraint_docs.remove(doc_id);

    auto features = _features_store.get(doc_id);
    if (!features.empty()) {
        for (auto feature : features) {
            removeFromIndex(feature, doc_id, _interval_index, _interval_store);
            removeFromIndex(feature, doc_id, _bounds_index, _interval_store);
        }
        _cache.removeIndex(doc_id);
    }
    _features_store.remove(doc_id);
}

void
PredicateIndex::commit() {
    _interval_index.commit();
    _bounds_index.commit();
    _zero_constraint_docs.getAllocator().freeze();
    _features_store.commit();
}

void
PredicateIndex::reclaim_memory(generation_t oldest_used_gen) {
    _interval_index.reclaim_memory(oldest_used_gen);
    _bounds_index.reclaim_memory(oldest_used_gen);
    _interval_store.reclaim_memory(oldest_used_gen);
    _zero_constraint_docs.getAllocator().reclaim_memory(oldest_used_gen);
    _features_store.reclaim_memory(oldest_used_gen);
}

void
PredicateIndex::assign_generation(generation_t current_gen) {
    _interval_index.assign_generation(current_gen);
    _bounds_index.assign_generation(current_gen);
    _interval_store.assign_generation(current_gen);
    _zero_constraint_docs.getAllocator().assign_generation(current_gen);
    _features_store.assign_generation(current_gen);
}

vespalib::MemoryUsage
PredicateIndex::getMemoryUsage() const {
    // TODO Include bit vector cache memory usage
    vespalib::MemoryUsage combined;
    combined.merge(_interval_index.getMemoryUsage());
    combined.merge(_bounds_index.getMemoryUsage());
    combined.merge(_zero_constraint_docs.getMemoryUsage());
    combined.merge(_interval_store.getMemoryUsage());
    combined.merge(_features_store.getMemoryUsage());
    return combined;
}

PopulateInterface::Iterator::UP
PredicateIndex::lookup(uint64_t key) const
{
    auto dictIterator = _interval_index.lookup(key);
    if (dictIterator.valid()) {
        auto it = _interval_index.getBTreePostingList(dictIterator.getData());
        if (it.valid()) {
            return std::make_unique<DocIdIterator>(it);
        }
    }
    return PopulateInterface::Iterator::UP();
}

void
PredicateIndex::populateIfNeeded(size_t doc_id_limit)
{
    if ( _cache.needPopulation()) {
        _cache.populate(doc_id_limit, *this);
    }
}

BitVectorCache::KeySet
PredicateIndex::lookupCachedSet(const BitVectorCache::KeyAndCountSet & keys) const
{
    // Don't count documents using bit vector if combined length is less than threshold
    uint64_t total_length = 0;
    auto cached_keys = _cache.lookupCachedSet(keys);
    for (const auto &p : keys) {
        if (cached_keys.find(p.first) != cached_keys.end()) {
            total_length += p.second;
        }
    }
    double fill_ratio = total_length / static_cast<double>(_limit_provider.getDocIdLimit());
    if (fill_ratio < THRESHOLD_USE_BIT_VECTOR_CACHE) {
        cached_keys.clear();
    }
    return cached_keys;
}

void
PredicateIndex::computeCountVector(BitVectorCache::KeySet & keys, BitVectorCache::CountVector & v) const
{
    _cache.computeCountVector(keys, v);
}


void
PredicateIndex::adjustDocIdLimit(uint32_t docId)
{
    _cache.adjustDocIdLimit(docId);
}

}