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

#pragma once

#include "singlestringpostattribute.h"
#include "single_string_enum_search_context.h"
#include <vespa/searchcommon/attribute/config.h>
#include <vespa/searchlib/query/query_term_ucs4.h>

namespace search {

template <typename B>
SingleValueStringPostingAttributeT<B>::SingleValueStringPostingAttributeT(const vespalib::string & name,
                                                                          const AttributeVector::Config & c) :
    SingleValueStringAttributeT<B>(name, c),
    PostingParent(*this, this->getEnumStore())
{
}

template <typename B>
SingleValueStringPostingAttributeT<B>::SingleValueStringPostingAttributeT(const vespalib::string & name)
    : SingleValueStringPostingAttributeT<B>(name, AttributeVector::Config(AttributeVector::BasicType::STRING))
{
}

template <typename B>
SingleValueStringPostingAttributeT<B>::~SingleValueStringPostingAttributeT()
{
    this->disableFreeLists();
    this->disable_entry_hold_list();
    clearAllPostings();
}

template <typename B>
void
SingleValueStringPostingAttributeT<B>::freezeEnumDictionary()
{
    this->getEnumStore().freeze_dictionary();
}

template <typename B>
void
SingleValueStringPostingAttributeT<B>::mergeMemoryStats(vespalib::MemoryUsage & total)
{
    auto& compaction_strategy = this->getConfig().getCompactionStrategy();
    total.merge(this->_postingList.update_stat(compaction_strategy));
}

template <typename B>
void
SingleValueStringPostingAttributeT<B>::applyUpdateValueChange(const Change & c,
                                                              EnumStore & enumStore,
                                                              std::map<DocId, EnumIndex> &currEnumIndices)
{
    EnumIndex newIdx;
    if (c.has_entry_ref()) {
        newIdx = EnumIndex(vespalib::datastore::EntryRef(c.get_entry_ref()));
    } else {
        enumStore.find_index(c._data.raw(), newIdx);
    }

    currEnumIndices[c._doc] = newIdx;
}

template <typename B>
void
SingleValueStringPostingAttributeT<B>::
makePostingChange(const vespalib::datastore::EntryComparator &cmpa,
                  IEnumStoreDictionary& dictionary,
                  const std::map<DocId, EnumIndex> &currEnumIndices,
                  PostingMap &changePost)
{
    for (const auto& elem : currEnumIndices) {
        uint32_t docId = elem.first;
        EnumIndex oldIdx = this->_enumIndices[docId].load_relaxed();
        EnumIndex newIdx = elem.second;

        // add new posting
        auto remapped_new_idx = dictionary.remap_index(newIdx);
        changePost[EnumPostingPair(remapped_new_idx, &cmpa)].add(docId, 1);

        // remove old posting
        if ( oldIdx.valid()) {
            auto remapped_old_idx = dictionary.remap_index(oldIdx);
            changePost[EnumPostingPair(remapped_old_idx, &cmpa)].remove(docId);
        }
    }
}

template <typename B>
void
SingleValueStringPostingAttributeT<B>::applyValueChanges(EnumStoreBatchUpdater& updater)
{
    EnumStore & enumStore = this->getEnumStore();
    IEnumStoreDictionary& dictionary = enumStore.get_dictionary();
    PostingMap changePost;

    // used to make sure several arithmetic operations on the same document in a single commit works
    std::map<DocId, EnumIndex> currEnumIndices;

    for (const auto& change : this->_changes.getInsertOrder()) {
        auto enumIter = currEnumIndices.find(change._doc);
        EnumIndex oldIdx;
        if (enumIter != currEnumIndices.end()) {
            oldIdx = enumIter->second;
        } else {
            oldIdx = this->_enumIndices[change._doc].load_relaxed();
        }
        if (change._type == ChangeBase::UPDATE) {
            applyUpdateValueChange(change, enumStore, currEnumIndices);
        } else if (change._type == ChangeBase::CLEARDOC) {
            currEnumIndices[change._doc] = enumStore.get_default_value_ref().load_relaxed();
        }
    }

    makePostingChange(enumStore.get_folded_comparator(), dictionary, currEnumIndices, changePost);

    this->updatePostings(changePost);

    SingleValueStringAttributeT<B>::applyValueChanges(updater);
}

template <typename B>
void
SingleValueStringPostingAttributeT<B>::reclaim_memory(generation_t oldest_used_gen)
{
    SingleValueStringAttributeT<B>::reclaim_memory(oldest_used_gen);
    _postingList.reclaim_memory(oldest_used_gen);
}

template <typename B>
void
SingleValueStringPostingAttributeT<B>::before_inc_generation(generation_t current_gen)
{
    _postingList.freeze();
    SingleValueStringAttributeT<B>::before_inc_generation(current_gen);
    _postingList.assign_generation(current_gen);
}

template <typename B>
std::unique_ptr<attribute::SearchContext>
SingleValueStringPostingAttributeT<B>::getSearch(QueryTermSimpleUP qTerm,
                                                 const attribute::SearchContextParams & params) const
{
    using BaseSC = attribute::SingleStringEnumSearchContext;
    using SC = attribute::StringPostingSearchContext<BaseSC, SelfType, vespalib::btree::BTreeNoLeafData>;
    bool cased = this->get_match_is_cased();
    auto docid_limit = this->getCommittedDocIdLimit();
    BaseSC base_sc(std::move(qTerm), cased, params.fuzzy_matching_algorithm(), *this, this->_enumIndices.make_read_view(docid_limit), this->_enumStore);
    return std::make_unique<SC>(std::move(base_sc),
                                params.useBitVector(),
                                *this);
}

} // namespace search