aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/predicate/simple_index.h
blob: 3290aaf929ef4f1a653148b5576a05f6b25eef8d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "common.h"
#include <vespa/vespalib/btree/btreestore.h>
#include <vespa/vespalib/data/databuffer.h>
#include <vespa/vespalib/util/rcuvector.h>
#include <optional>

namespace search::predicate {

template <typename Key = uint64_t, typename DocId = uint32_t>
struct SimpleIndexDeserializeObserver {
    virtual ~SimpleIndexDeserializeObserver() {}
    virtual void notifyInsert(Key key, DocId docId, uint32_t k) = 0;
};

template <typename Posting>
struct PostingSerializer {
    virtual ~PostingSerializer() {}
    virtual void serialize(const Posting &posting, vespalib::DataBuffer &buffer) const = 0;
};

template <typename Posting>
struct PostingDeserializer {
    virtual ~PostingDeserializer() {}
    virtual Posting deserialize(vespalib::DataBuffer &buffer) = 0;
};

struct SimpleIndexConfig {
    static constexpr double DEFAULT_UPPER_DOCID_FREQ_THRESHOLD = 0.40;
    static constexpr double DEFAULT_LOWER_DOCID_FREQ_THRESHOLD = 0.8 * DEFAULT_UPPER_DOCID_FREQ_THRESHOLD;
    static constexpr size_t DEFAULT_UPPER_VECTOR_SIZE_THRESHOLD = 10000;
    static constexpr size_t DEFAULT_LOWER_VECTOR_SIZE_THRESHOLD = static_cast<size_t>(0.8 * DEFAULT_UPPER_VECTOR_SIZE_THRESHOLD);
    static constexpr size_t DEFAULT_VECTOR_PRUNE_FREQUENCY = 20000;
    static constexpr double DEFAULT_FOREACH_VECTOR_THRESHOLD = 0.25;

    // Create vector posting list if doc frequency is above
    double upper_docid_freq_threshold = DEFAULT_UPPER_DOCID_FREQ_THRESHOLD;
    // Remove vector posting list if doc frequency is below
    double lower_docid_freq_threshold = DEFAULT_LOWER_DOCID_FREQ_THRESHOLD;
    // Threshold to create vector posting list
    size_t upper_vector_size_threshold = DEFAULT_UPPER_VECTOR_SIZE_THRESHOLD;
    // Threshold to remove vector posting list
    size_t lower_vector_size_threshold = DEFAULT_LOWER_VECTOR_SIZE_THRESHOLD;
    // How often to prune vector when add is called
    size_t vector_prune_frequency = DEFAULT_VECTOR_PRUNE_FREQUENCY;
    // Use vector posting list in foreach_frozen if doc frequency is above
    double foreach_vector_threshold = DEFAULT_FOREACH_VECTOR_THRESHOLD;
    // Grow strategy for the posting vectors
    vespalib::GrowStrategy grow_strategy = vespalib::GrowStrategy();

    SimpleIndexConfig() {}
    SimpleIndexConfig(double upper_docid_freq_threshold_,
                      double lower_docid_freq_threshold_,
                      size_t upper_vector_size_threshold_,
                      size_t lower_vector_size_threshold_,
                      size_t vector_prune_frequency_,
                      double foreach_vector_threshold_,
                      vespalib::GrowStrategy grow_strategy_)
            : upper_docid_freq_threshold(upper_docid_freq_threshold_),
              lower_docid_freq_threshold(lower_docid_freq_threshold_),
              upper_vector_size_threshold(upper_vector_size_threshold_),
              lower_vector_size_threshold(lower_vector_size_threshold_),
              vector_prune_frequency(vector_prune_frequency_),
              foreach_vector_threshold(foreach_vector_threshold_),
              grow_strategy(grow_strategy_) {}
    SimpleIndexConfig(double upper_docid_freq_threshold_, vespalib::GrowStrategy grow_strategy_)
            : upper_docid_freq_threshold(upper_docid_freq_threshold_),
              lower_docid_freq_threshold(upper_docid_freq_threshold_ * 0.80),
              grow_strategy(grow_strategy_) {}
};

template <typename Posting, typename Key, typename DocId>
class PostingVectorIterator {
    using PostingVector = vespalib::RcuVectorBase<Posting>;

    const Posting * const _vector;
    const size_t _size;
    size_t _pos;
    Posting _data;

public:
    // Handle both move and copy construction
    PostingVectorIterator(PostingVectorIterator&&) = default;
    PostingVectorIterator& operator=(PostingVectorIterator&&) = default;
    PostingVectorIterator(const PostingVectorIterator&) = default;
    PostingVectorIterator& operator=(const PostingVectorIterator&) = default;

    explicit PostingVectorIterator(const PostingVector & vector, size_t size)
        : _vector(&vector.acquire_elem_ref(0)),
          _size(size)
    {
        assert(_size <= vector.get_size()); // Data race: not writer
        linearSeek(1);
    }

    bool valid() const { return _pos < _size; }
    DocId getKey() const { return _pos; }
    Posting getData() const { return _data; }
    void linearSeek(DocId doc_id) {
        while (doc_id < _size) {
            const Posting &p = _vector[doc_id];
            if (p.valid()) {
                _pos = doc_id;
                _data = p;
                return;
            }
            ++doc_id;
        }
        _pos = _size;
    }
    PostingVectorIterator & operator++() {
        linearSeek(_pos + 1);
        return *this;
    }
};

/**
 * SimpleIndex holds a dictionary of Keys and posting lists of DocIds
 * with Posting information.
 *
 * Serialization / deserialization assumes that Key fits in 64 bits
 * and DocId fits in 32 bits.
 */
template <typename Posting,
          typename Key = uint64_t, typename DocId = uint32_t>
class SimpleIndex {
public:
    using Dictionary = vespalib::btree::BTree<Key, vespalib::datastore::EntryRef, vespalib::btree::NoAggregated>;
    using DictionaryIterator = typename Dictionary::ConstIterator;
    using BTreeStore = vespalib::btree::BTreeStore<
            DocId, Posting, vespalib::btree::NoAggregated, std::less<DocId>, vespalib::btree::BTreeDefaultTraits>;
    using BTreeIterator = typename BTreeStore::ConstIterator;
    using PostingVector = vespalib::RcuVectorBase<Posting>;
    using VectorStore = vespalib::btree::BTree<Key, std::shared_ptr<PostingVector>, vespalib::btree::NoAggregated>;
    using VectorIterator = PostingVectorIterator<Posting, Key, DocId>;

private:
    using GenerationHolder = vespalib::GenerationHolder;
    using generation_t = vespalib::GenerationHandler::generation_t;
    template <typename T>
    using optional = std::optional<T>;

    Dictionary                _dictionary;
    BTreeStore                _btree_posting_lists;
    VectorStore               _vector_posting_lists;
    GenerationHolder         &_generation_holder;
    uint32_t                  _insert_remove_counter = 0;
    const SimpleIndexConfig   _config;
    const DocIdLimitProvider &_limit_provider;

    void insertIntoPosting(vespalib::datastore::EntryRef &ref, Key key, DocId doc_id, const Posting &posting);
    void insertIntoVectorPosting(vespalib::datastore::EntryRef ref, Key key, DocId doc_id, const Posting &posting);
    void removeFromVectorPostingList(vespalib::datastore::EntryRef ref, Key key, DocId doc_id);
    void pruneBelowThresholdVectors();
    void createVectorIfOverThreshold(vespalib::datastore::EntryRef ref, Key key);
    bool removeVectorIfBelowThreshold(vespalib::datastore::EntryRef ref, typename VectorStore::Iterator &it);

    void logVector(const char *action, Key key, size_t document_count,
                   double ratio, size_t vector_length) const;
    double getDocumentRatio(size_t document_count, uint32_t doc_id_limit) const;
    size_t getDocumentCount(vespalib::datastore::EntryRef ref) const;
    bool shouldCreateVectorPosting(size_t size, double ratio) const;
    bool shouldRemoveVectorPosting(size_t size, double ratio) const;
    size_t getVectorPostingSize(const PostingVector &vector) const {
        return std::min(vector.get_size() /* Data race: not writer */,
                        static_cast<size_t>(_limit_provider.getCommittedDocIdLimit()));
    }

public:
    SimpleIndex(GenerationHolder &generation_holder,
                const DocIdLimitProvider &provider, const SimpleIndexConfig &config)
        : _generation_holder(generation_holder), _config(config), _limit_provider(provider) {}
    ~SimpleIndex();

    void serialize(vespalib::DataBuffer &buffer,
                   const PostingSerializer<Posting> &serializer) const;
    void deserialize(vespalib::DataBuffer &buffer,
                     PostingDeserializer<Posting> &deserializer,
                     SimpleIndexDeserializeObserver<Key, DocId> &observer, uint32_t version);

    void addPosting(Key key, DocId doc_id, const Posting &posting);
    std::pair<Posting, bool> removeFromPostingList(Key key, DocId doc_id);
    // Call promoteOverThresholdVectors() after deserializing a SimpleIndex
    // (and after doc id limits values are determined) to promote posting lists to vectors.
    void promoteOverThresholdVectors();
    void commit();
    void reclaim_memory(generation_t oldest_used_gen);
    void assign_generation(generation_t current_gen);
    vespalib::MemoryUsage getMemoryUsage() const;
    template <typename FunctionType>
    void foreach_frozen_key(vespalib::datastore::EntryRef ref, Key key, FunctionType func) const;

    DictionaryIterator lookup(Key key) const {
        return _dictionary.getFrozenView().find(key);
    }

    size_t getPostingListSize(vespalib::datastore::EntryRef ref) const {
        return _btree_posting_lists.frozenSize(ref);
    }

    BTreeIterator getBTreePostingList(vespalib::datastore::EntryRef ref) const {
        return _btree_posting_lists.beginFrozen(ref);
    }

    optional<VectorIterator> getVectorPostingList(Key key) const {
        auto it = _vector_posting_lists.getFrozenView().find(key);
        if (it.valid()) {
            auto &vector = *it.getData();
            size_t size = getVectorPostingSize(vector);
            return optional<VectorIterator>(VectorIterator(vector, size));
        }
        return optional<VectorIterator>();

    }
};

template<typename Posting, typename Key, typename DocId>
template<typename FunctionType>
void
SimpleIndex<Posting, Key, DocId>::foreach_frozen_key(vespalib::datastore::EntryRef ref, Key key, FunctionType func) const {
    auto it = _vector_posting_lists.getFrozenView().find(key);
    double ratio = getDocumentRatio(getDocumentCount(ref), _limit_provider.getDocIdLimit());
    if (it.valid() && ratio > _config.foreach_vector_threshold) {
        auto &vector = *it.getData();
        size_t size = getVectorPostingSize(vector);
        for (DocId doc_id = 1; doc_id < size; ++doc_id) {
            if (vector[doc_id].valid()) {
                func(doc_id);
            }
        }
    } else {
        _btree_posting_lists.foreach_frozen_key(ref, func);
    }
}

}