aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/attrvector.hpp
blob: fdc94df2e405f65dad6df3bc0683a441f7ab2f16 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "attrvector.h"
#include "load_utils.h"
#include "numeric_sort_blob_writer.h"
#include "string_sort_blob_writer.h"
#include <vespa/vespalib/util/hdr_abort.h>
#include <vespa/searchlib/util/filekit.h>

namespace search {

template <typename B>
NumericDirectAttribute<B>::
NumericDirectAttribute(const vespalib::string & baseFileName, const Config & c)
    : B(baseFileName, c),
      _data(),
      _idx()
{
}

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

template <typename B>
bool NumericDirectAttribute<B>::findEnum(typename B::BaseType key, EnumHandle & e) const
{
    if (_data.empty()) {
        e = 0;
        return false;
    }
    int delta;
    const int eMax = B::getEnumMax();
    for (delta = 1; delta <= eMax; delta <<= 1) { }
    delta >>= 1;
    int pos = delta - 1;
    typename B::BaseType value = key;

    while (delta != 0) {
        delta >>= 1;
        if (pos >= eMax) {
            pos -= delta;
        } else {
            value = _data[pos];
            if (value == key) {
                e = pos;
                return true;
            } else if (value < key) {
                pos += delta;
            } else {
                pos -= delta;
            }
        }
    }
    e = ((key > value) && (pos < eMax)) ? pos + 1 : pos;
    return false;
}

template <typename B>
void NumericDirectAttribute<B>::onCommit()
{
    B::_changes.clear();
    HDR_ABORT("should not be reached");
}

template <typename B>
bool NumericDirectAttribute<B>::addDoc(DocId & )
{
    return false;
}

}

template <typename F, typename B>
NumericDirectAttrVector<F, B>::
NumericDirectAttrVector(const vespalib::string & baseFileName, const AttributeVector::Config & c)
    : search::NumericDirectAttribute<B>(baseFileName, c)
{
    if (F::IsMultiValue()) {
        this->_idx.push_back(0);
    }
}

template <typename F, typename B>
NumericDirectAttrVector<F, B>::
NumericDirectAttrVector(const vespalib::string & baseFileName)
    : search::NumericDirectAttribute<B>(baseFileName, AttributeVector::Config(AttributeVector::BasicType::fromType(BaseType()), F::IsMultiValue() ? search::attribute::CollectionType::ARRAY : search::attribute::CollectionType::SINGLE))
{
    if (F::IsMultiValue()) {
        this->_idx.push_back(0);
    }
}

template <typename F, typename B>
template <bool asc>
long
NumericDirectAttrVector<F, B>::on_serialize_for_sort(DocId doc, void* serTo, long available) const
{
    search::attribute::NumericSortBlobWriter<BaseType, asc> writer;
    vespalib::ConstArrayRef<BaseType> values(this->_data.data() + this->_idx[doc], this->_idx[doc + 1] - this->_idx[doc]);
    for (auto& v : values) {
        writer.candidate(v);
    }
    return writer.write(serTo, available);
}

template <typename F, typename B>
long
NumericDirectAttrVector<F, B>::onSerializeForAscendingSort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc) const
{
    if (!F::IsMultiValue()) {
        return search::NumericDirectAttribute<B>::onSerializeForAscendingSort(doc, serTo, available, bc);
    }
    return on_serialize_for_sort<true>(doc, serTo, available);
}

template <typename F, typename B>
long
NumericDirectAttrVector<F, B>::onSerializeForDescendingSort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc) const
{
    if (!F::IsMultiValue()) {
        return search::NumericDirectAttribute<B>::onSerializeForDescendingSort(doc, serTo, available, bc);
    }
    return on_serialize_for_sort<false>(doc, serTo, available);
}

template <typename F>
StringDirectAttrVector<F>::
StringDirectAttrVector(const vespalib::string & baseFileName, const Config & c) :
    search::StringDirectAttribute(baseFileName, c)
{
    if (F::IsMultiValue()) {
        _idx.push_back(0);
    }
    setEnum(true);
}

template <typename F>
StringDirectAttrVector<F>::
StringDirectAttrVector(const vespalib::string & baseFileName) :
    search::StringDirectAttribute(baseFileName, Config(BasicType::STRING, F::IsMultiValue() ? search::attribute::CollectionType::ARRAY : search::attribute::CollectionType::SINGLE))
{
    if (F::IsMultiValue()) {
        _idx.push_back(0);
    }
    setEnum(true);
}

template <typename F>
long
StringDirectAttrVector<F>::on_serialize_for_sort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc, bool asc) const
{
    search::attribute::StringSortBlobWriter writer(serTo, available, bc, asc);
    vespalib::ConstArrayRef<uint32_t> offsets(this->_offsets.data() + this->_idx[doc], this->_idx[doc + 1] - this->_idx[doc]);
    for (auto& offset : offsets) {
        if (!writer.candidate(&this->_buffer[offset])) {
            return -1;
        }
    }
    return writer.write();
}

template <typename F>
long
StringDirectAttrVector<F>::onSerializeForAscendingSort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc) const
{
    if (!F::IsMultiValue()) {
        return search::StringDirectAttribute::onSerializeForAscendingSort(doc, serTo, available, bc);
    }
    return on_serialize_for_sort(doc, serTo, available, bc, true);
}

template <typename F>
long
StringDirectAttrVector<F>::onSerializeForDescendingSort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc) const
{
    if (!F::IsMultiValue()) {
        return search::StringDirectAttribute::onSerializeForDescendingSort(doc, serTo, available, bc);
    }
    return on_serialize_for_sort(doc, serTo, available, bc, false);
}