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

#pragma once

#include "i_docid_posting_store.h"
#include "numeric_direct_posting_store_adapter.h"
#include "postinglistattribute.h"
#include "postinglistsearchcontext.h"
#include "singlenumericenumattribute.h"

namespace search {

/**
 * Implementation of single value numeric attribute that in addition to enum store
 * uses an underlying posting list to provide faster search.
 *
 * B: EnumAttribute<BaseClass>
 */
template <typename B>
class SingleValueNumericPostingAttribute
    : public SingleValueNumericEnumAttribute<B>,
      protected PostingListAttributeSubBase<AttributePosting,
                                            typename B::LoadedVector,
                                            typename B::LoadedValueType,
                                            typename B::EnumStore>
{
public:
    using T = typename SingleValueNumericEnumAttribute<B>::T;
    using Dictionary = EnumPostingTree;
    using EnumStore = typename SingleValueNumericEnumAttribute<B>::EnumStore;
    using EnumStoreBatchUpdater = typename EnumStore::BatchUpdater;

private:
    friend class PostingListAttributeTest;
    template <typename, typename, typename>
    friend class attribute::PostingSearchContext; // getEnumStore()

    using LoadedVector = typename B::LoadedVector;
    using PostingParent = PostingListAttributeSubBase<AttributePosting,
                                                      LoadedVector,
                                                      typename B::LoadedValueType,
                                                      typename B::EnumStore>;

    using Change = typename B::BaseClass::Change;
    using ComparatorType = typename EnumStore::ComparatorType;
    using DocId = typename B::BaseClass::DocId;
    using EnumIndex = typename SingleValueEnumAttributeBase::EnumIndex;
    using PostingMap = typename PostingParent::PostingMap;
    using PostingStore = typename PostingParent::PostingStore;
    using QueryTermSimpleUP = AttributeVector::QueryTermSimpleUP;
    using SelfType = SingleValueNumericPostingAttribute<B>;
    using ValueModifier = typename B::BaseClass::ValueModifier;
    using generation_t = typename SingleValueNumericEnumAttribute<B>::generation_t;

    using PostingParent::_posting_store;
    using PostingParent::clearAllPostings;
    using PostingParent::handle_load_posting_lists;
    using PostingParent::handle_load_posting_lists_and_update_enum_store;
    using PostingParent::forwardedOnAddDoc;

    using DirectPostingStoreAdapterType = attribute::NumericDirectPostingStoreAdapter<IDocidPostingStore,
                                                                                      PostingStore, EnumStore>;
    DirectPostingStoreAdapterType _posting_store_adapter;

    void freezeEnumDictionary() override;
    void mergeMemoryStats(vespalib::MemoryUsage & total) override;
    void applyUpdateValueChange(const Change & c, EnumStore & enumStore,
                                std::map<DocId, EnumIndex> & currEnumIndices);
    void makePostingChange(const vespalib::datastore::EntryComparator &cmp,
                           const std::map<DocId, EnumIndex> &currEnumIndices,
                           PostingMap &changePost);

    void applyValueChanges(EnumStoreBatchUpdater& updater) override;

public:
    SingleValueNumericPostingAttribute(const vespalib::string & name, const AttributeVector::Config & cfg);
    ~SingleValueNumericPostingAttribute();

    void reclaim_memory(generation_t oldest_used_gen) override;
    void before_inc_generation(generation_t current_gen) override;

    std::unique_ptr<attribute::SearchContext>
    getSearch(QueryTermSimpleUP term, const attribute::SearchContextParams & params) const override;

    const IDocidPostingStore* as_docid_posting_store() const override;

    bool onAddDoc(DocId doc) override {
        return forwardedOnAddDoc(doc, this->_enumIndices.size(), this->_enumIndices.capacity());
    }
    void onAddDocs(DocId docIdLimit) override {
        forwardedOnAddDoc(docIdLimit, this->_enumIndices.size(), this->_enumIndices.capacity());
    }

    void load_posting_lists(LoadedVector& loaded) override { handle_load_posting_lists(loaded); }
    attribute::IPostingListAttributeBase *getIPostingListAttributeBase() override { return this; }
    const attribute::IPostingListAttributeBase *getIPostingListAttributeBase() const override { return this; }
    void load_posting_lists_and_update_enum_store(enumstore::EnumeratedPostingsLoader& loader) override {
        handle_load_posting_lists_and_update_enum_store(loader);
    }
};

} // namespace search