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

#pragma once

#include "singlenumericenumattribute.h"
#include "load_utils.h"
#include "loadednumericvalue.h"
#include "primitivereader.h"
#include "singleenumattribute.hpp"
#include "single_numeric_enum_search_context.h"
#include <vespa/searchlib/query/query_term_simple.h>
#include <vespa/searchlib/util/fileutil.hpp>

namespace search {

template <typename B>
void
SingleValueNumericEnumAttribute<B>::considerUpdateAttributeChange(DocId doc, const Change & c)
{
    _currDocValues[doc] = c._data.get();
}

template <typename B>
void
SingleValueNumericEnumAttribute<B>::considerArithmeticAttributeChange(const Change & c, EnumStoreBatchUpdater & inserter)
{
    T oldValue;
    auto iter = _currDocValues.find(c._doc);
    if (iter != _currDocValues.end()) {
        oldValue = iter->second;
    } else {
        oldValue = get(c._doc);
    }

    T newValue = this->template applyArithmetic<T, typename Change::DataType>(oldValue, c._data.getArithOperand(), c._type);

    EnumIndex idx;
    if (!this->_enumStore.find_index(newValue, idx)) {
        c.set_entry_ref(inserter.insert(newValue).ref());
    } else {
        c.set_entry_ref(idx.ref());
    }

    _currDocValues[c._doc] = newValue;
}

template <typename B>
void
SingleValueNumericEnumAttribute<B>::applyArithmeticValueChange(const Change& c, EnumStoreBatchUpdater& updater)
{
    EnumIndex oldIdx = this->_enumIndices[c._doc].load_relaxed();
    EnumIndex newIdx;
    T newValue = this->template applyArithmetic<T, typename Change::DataType>(get(c._doc), c._data.getArithOperand(), c._type);
    this->_enumStore.find_index(newValue, newIdx);

    this->updateEnumRefCounts(c._doc, newIdx, oldIdx, updater);
}

template <typename B>
SingleValueNumericEnumAttribute<B>::
SingleValueNumericEnumAttribute(const vespalib::string & baseFileName,
                                const AttributeVector::Config & c)
    : SingleValueEnumAttribute<B>(baseFileName, c),
      _currDocValues()
{
}

template <typename B>
SingleValueNumericEnumAttribute<B>::~SingleValueNumericEnumAttribute() = default;

template <typename B>
void
SingleValueNumericEnumAttribute<B>::onCommit()
{
    SingleValueEnumAttribute<B>::onCommit();
    _currDocValues.clear();
}

template <typename B>
bool
SingleValueNumericEnumAttribute<B>::onLoadEnumerated(ReaderBase &attrReader)
{
    auto udatBuffer = attribute::LoadUtils::loadUDAT(*this);

    uint64_t numValues = attrReader.getEnumCount();
    uint32_t numDocs = numValues;

    this->setNumDocs(numDocs);
    this->setCommittedDocIdLimit(numDocs);
    if (this->hasPostings()) {
        auto loader = this->getEnumStore().make_enumerated_postings_loader();
        loader.load_unique_values(udatBuffer->buffer(), udatBuffer->size());
        loader.build_enum_value_remapping();
        this->load_enumerated_data(attrReader, loader, numValues);
        if (numDocs > 0) {
            this->onAddDoc(numDocs - 1);
        }
        this->load_posting_lists_and_update_enum_store(loader);
    } else {
        auto loader = this->getEnumStore().make_enumerated_loader();
        loader.load_unique_values(udatBuffer->buffer(), udatBuffer->size());
        loader.build_enum_value_remapping();
        this->load_enumerated_data(attrReader, loader);
    }
    return true;
}


template <typename B>
bool
SingleValueNumericEnumAttribute<B>::onLoad(vespalib::Executor *)
{
    PrimitiveReader<T> attrReader(*this);
    bool ok(attrReader.getHasLoadData());
    
    if (!ok) {
        return false;
    }

    this->_enumStore.clear_default_value_ref();
    this->commit();
    this->incGeneration();

    this->setCreateSerialNum(attrReader.getCreateSerialNum());

    if (attrReader.getEnumerated()) {
        return onLoadEnumerated(attrReader);
    }

    const uint32_t numDocs(attrReader.getDataCount());
    SequentialReadModifyWriteVector<LoadedNumericValueT> loaded(numDocs);

    this->setNumDocs(numDocs);
    this->setCommittedDocIdLimit(numDocs);
    if (numDocs > 0) {
        this->onAddDoc(numDocs - 1);
    }

    for (uint32_t docIdx = 0; docIdx < numDocs; ++docIdx) {
        loaded[docIdx]._docId = docIdx;
        loaded[docIdx]._idx = 0;
        loaded[docIdx].setValue(attrReader.getNextData());
    }

    attribute::sortLoadedByValue(loaded);
    this->load_posting_lists(loaded);
    loaded.rewind();
    this->load_enum_store(loaded);
    attribute::sortLoadedByDocId(loaded);
    loaded.rewind();
    this->fillValues(loaded);
    
    return true;
}


template <typename B>
std::unique_ptr<attribute::SearchContext>
SingleValueNumericEnumAttribute<B>::getSearch(QueryTermSimple::UP qTerm,
                                              const attribute::SearchContextParams & params) const
{
    (void) params;
    auto docid_limit = this->getCommittedDocIdLimit();
    return std::make_unique<attribute::SingleNumericEnumSearchContext<T>>(std::move(qTerm), *this, this->_enumIndices.make_read_view(docid_limit), this->_enumStore);
}

}