aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/test/attribute_builder.cpp
blob: ce8c86367b177cfa829dbd9df88e82cae715e381 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "attribute_builder.h"
#include <vespa/eval/eval/simple_value.h>
#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/attribute/attributevector.h>
#include <vespa/searchlib/attribute/floatbase.h>
#include <vespa/searchlib/attribute/integerbase.h>
#include <vespa/searchlib/attribute/single_raw_attribute.h>
#include <vespa/searchlib/attribute/stringbase.h>
#include <vespa/searchlib/tensor/tensor_attribute.h>
#include <cassert>

using vespalib::eval::SimpleValue;
using vespalib::eval::TensorSpec;

namespace search::attribute::test {

AttributeBuilder::AttributeBuilder(const vespalib::string& name, const Config& cfg)
    : _attr_ptr(AttributeFactory::createAttribute(name, cfg)),
      _attr(*_attr_ptr)
{
    _attr.addReservedDoc();
}

namespace {

void
add_docs(AttributeVector& attr, size_t num_docs)
{
    attr.addDocs(num_docs);
}

template <typename AttrType, typename ValueType>
void
fill_helper(AttributeVector& attr, std::initializer_list<ValueType> values)
{
    add_docs(attr, values.size());
    auto& real = dynamic_cast<AttrType&>(attr);
    uint32_t docid = 1;
    for (auto value : values) {
        real.update(docid++, value);
    }
    attr.commit(true);
}

template <typename AttrType, typename ValueType>
void
fill_array_helper(AttributeVector& attr, std::initializer_list<std::initializer_list<ValueType>> values)
{
    assert(attr.hasMultiValue());
    add_docs(attr, values.size());
    auto& real = dynamic_cast<AttrType&>(attr);
    uint32_t docid = 1;
    for (auto value : values) {
        for (auto elem : value) {
            real.append(docid, elem, 1);
        }
        ++docid;
    }
    attr.commit(true);
}

template <typename AttrType, typename ValueType>
void
fill_wset_helper(AttributeVector& attr, std::initializer_list<std::initializer_list<std::pair<ValueType, int32_t>>> values)
{
    assert(attr.hasMultiValue());
    add_docs(attr, values.size());
    auto& real = dynamic_cast<AttrType&>(attr);
    uint32_t docid = 1;
    for (auto value : values) {
        for (auto elem : value) {
            real.append(docid, elem.first, elem.second);
        }
        ++docid;
    }
    attr.commit(true);
}

}

AttributeBuilder&
AttributeBuilder::docs(size_t num_docs)
{
    add_docs(_attr, num_docs);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill(std::initializer_list<int32_t> values)
{
    fill_helper<IntegerAttribute, int32_t>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill(std::initializer_list<int64_t> values)
{
    fill_helper<IntegerAttribute, int64_t>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill_array(std::initializer_list<IntList> values)
{
    fill_array_helper<IntegerAttribute, int32_t>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill_wset(std::initializer_list<WeightedIntList> values)
{
    fill_wset_helper<IntegerAttribute, int32_t>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill(std::initializer_list<double> values)
{
    fill_helper<FloatingPointAttribute, double>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill_array(std::initializer_list<DoubleList> values)
{
    fill_array_helper<FloatingPointAttribute, double>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill_wset(std::initializer_list<WeightedDoubleList> values)
{
    fill_wset_helper<FloatingPointAttribute, double>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill(std::initializer_list<vespalib::string> values)
{
    fill_helper<StringAttribute, vespalib::string>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill_array(std::initializer_list<StringList> values)
{
    fill_array_helper<StringAttribute, vespalib::string>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill_wset(std::initializer_list<WeightedStringList> values)
{
    fill_wset_helper<StringAttribute, vespalib::string>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill(std::initializer_list<vespalib::ConstArrayRef<char>> values)
{
    fill_helper<SingleRawAttribute, vespalib::ConstArrayRef<char>>(_attr, values);
    return *this;
}

AttributeBuilder&
AttributeBuilder::fill_tensor(const std::vector<vespalib::string>& values)
{
    add_docs(_attr, values.size());
    auto& real = dynamic_cast<search::tensor::TensorAttribute&>(_attr);
    vespalib::string tensor_type = real.getConfig().tensorType().to_spec();
    uint32_t docid = 1;
    for (const auto& value : values) {
        if (!value.empty()) {
            auto spec = TensorSpec::from_expr(tensor_type + ":" + value);
            auto tensor = SimpleValue::from_spec(spec);
            real.setTensor(docid, *tensor);
        }
        ++docid;
    }
    _attr.commit(true);
    return *this;
}

}