aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/searchsummary/docsummary/attributedfw.cpp
blob: 6dd952f87ef87d2376f728f705080c8def152b45 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "attributedfw.h"
#include "docsumstate.h"
#include "docsumwriter.h"
#include <vespa/eval/eval/value.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/searchcommon/attribute/iattributecontext.h>
#include <vespa/searchlib/attribute/iattributemanager.h>
#include <vespa/searchlib/attribute/integerbase.h>
#include <vespa/searchlib/attribute/stringbase.h>
#include <vespa/searchlib/common/matching_elements.h>
#include <vespa/searchlib/common/matching_elements_fields.h>
#include <vespa/searchlib/tensor/i_tensor_attribute.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/issue.h>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.docsummary.attributedfw");

using namespace search;
using search::attribute::BasicType;
using search::attribute::IAttributeContext;
using search::attribute::IAttributeVector;
using vespalib::Memory;
using vespalib::slime::Cursor;
using vespalib::slime::Inserter;
using vespalib::slime::Symbol;
using vespalib::eval::Value;
using vespalib::Issue;

namespace search::docsummary {

AttrDFW::AttrDFW(const vespalib::string & attrName) :
    _attrName(attrName)
{
}

const attribute::IAttributeVector &
AttrDFW::get_attribute(const GetDocsumsState& s) const {
    return *s.getAttribute(getIndex());
}

namespace {

class SingleAttrDFW : public AttrDFW
{
public:
    explicit SingleAttrDFW(const vespalib::string & attrName) :
        AttrDFW(attrName)
    { }
    void insertField(uint32_t docid, GetDocsumsState *state, ResType type, Inserter &target) override;
    bool isDefaultValue(uint32_t docid, const GetDocsumsState * state) const override;
};

bool SingleAttrDFW::isDefaultValue(uint32_t docid, const GetDocsumsState * state) const
{
    return get_attribute(*state).isUndefined(docid);
}

void
SingleAttrDFW::insertField(uint32_t docid, GetDocsumsState * state, ResType type, Inserter &target)
{
    const auto& v = get_attribute(*state);
    switch (type) {
    case RES_INT: {
        uint32_t val = v.getInt(docid);
        target.insertLong(val);
        break;
    }
    case RES_SHORT: {
        uint16_t val = v.getInt(docid);
        target.insertLong(val);
        break;
    }
    case RES_BYTE: {
        uint8_t val = v.getInt(docid);
        target.insertLong(val);
        break;
    }
    case RES_BOOL: {
        uint8_t val = v.getInt(docid);
        target.insertBool(val != 0);
        break;
    }
    case RES_FLOAT: {
        float val = v.getFloat(docid);
        target.insertDouble(val);
        break;
    }
    case RES_DOUBLE: {
        double val = v.getFloat(docid);
        target.insertDouble(val);
        break;
    }
    case RES_INT64: {
        uint64_t val = v.getInt(docid);
        target.insertLong(val);
        break;
    }
    case RES_TENSOR: {
        BasicType::Type t = v.getBasicType();
        switch (t) {
        case BasicType::TENSOR: {
            const tensor::ITensorAttribute *tv = v.asTensorAttribute();
            assert(tv != nullptr);
            const auto tensor = tv->getTensor(docid);
            if (tensor) {
                vespalib::nbostream str;
                encode_value(*tensor, str);
                target.insertData(vespalib::Memory(str.peek(), str.size()));
            }
        }
        default:
            ;
        }
    }
        break;
    case RES_JSONSTRING:
    case RES_XMLSTRING:
    case RES_FEATUREDATA:
    case RES_LONG_STRING:
    case RES_STRING: {
        const char *s = v.getString(docid, nullptr, 0); // no need to pass in a buffer, this attribute has a string storage.
        target.insertString(vespalib::Memory(s));
        break;
    }
    case RES_LONG_DATA:
    case RES_DATA: {
        const char *s = v.getString(docid, nullptr, 0); // no need to pass in a buffer, this attribute has a string storage.
        target.insertData(vespalib::Memory(s));
        break;
    }
    default:
        // unknown type, will be missing, should not happen
        return;
    }
}


//-----------------------------------------------------------------------------

template <typename DataType>
class MultiAttrDFW : public AttrDFW {
private:
    bool _is_weighted_set;
    bool _filter_elements;
    std::shared_ptr<MatchingElementsFields> _matching_elems_fields;

public:
    explicit MultiAttrDFW(const vespalib::string& attr_name, bool is_weighted_set,
                          bool filter_elements, std::shared_ptr<MatchingElementsFields> matching_elems_fields)
        : AttrDFW(attr_name),
          _is_weighted_set(is_weighted_set),
          _filter_elements(filter_elements),
          _matching_elems_fields(std::move(matching_elems_fields))
    {
        if (filter_elements && _matching_elems_fields) {
            _matching_elems_fields->add_field(attr_name);
        }
    }
    void insertField(uint32_t docid, GetDocsumsState* state, ResType type, Inserter& target) override;
};

void
set(const vespalib::string & value, Symbol itemSymbol, Cursor & cursor)
{
    cursor.setString(itemSymbol, value);
}

void
append(const IAttributeVector::WeightedString & element, Cursor& arr)
{
    arr.addString(element.getValue());
}

void
set(int64_t value, Symbol itemSymbol, Cursor & cursor)
{
    cursor.setLong(itemSymbol, value);
}

void
append(const IAttributeVector::WeightedInt & element, Cursor& arr)
{
    arr.addLong(element.getValue());
}

void
set(double value, Symbol itemSymbol, Cursor & cursor)
{
    cursor.setDouble(itemSymbol, value);
}

void
append(const IAttributeVector::WeightedFloat & element, Cursor& arr)
{
    arr.addDouble(element.getValue());
}

Memory ITEM("item");
Memory WEIGHT("weight");

template <typename DataType>
void
MultiAttrDFW<DataType>::insertField(uint32_t docid, GetDocsumsState* state, ResType, Inserter& target)
{
    const auto& attr = get_attribute(*state);
    uint32_t entries = attr.getValueCount(docid);
    if (entries == 0) {
        return;  // Don't insert empty fields
    }

    std::vector<DataType> elements(entries);
    entries = std::min(entries, attr.get(docid, elements.data(), entries));
    Cursor &arr = target.insertArray(entries);

    if (_filter_elements) {
        const auto& matching_elems = state->get_matching_elements(*_matching_elems_fields)
                .get_matching_elements(docid, getAttributeName());
        if (!matching_elems.empty() && matching_elems.back() < entries) {
            if (_is_weighted_set) {
                Symbol itemSymbol = arr.resolve(ITEM);
                Symbol weightSymbol = arr.resolve(WEIGHT);
                for (uint32_t id_to_keep : matching_elems) {
                    const DataType & element = elements[id_to_keep];
                    Cursor& elemC = arr.addObject();
                    set(element.getValue(), itemSymbol, elemC);
                    elemC.setLong(weightSymbol, element.getWeight());
                }
            } else {
                for (uint32_t id_to_keep : matching_elems) {
                    append(elements[id_to_keep], arr);
                }
            }
        }
    } else {
        if (_is_weighted_set) {
            Symbol itemSymbol = arr.resolve(ITEM);
            Symbol weightSymbol = arr.resolve(WEIGHT);
            for (const auto & element : elements) {
                Cursor& elemC = arr.addObject();
                set(element.getValue(), itemSymbol, elemC);
                elemC.setLong(weightSymbol, element.getWeight());
            }
        } else {
            for (const auto & element : elements) {
                append(element, arr);
            }
        }
    }
}

std::unique_ptr<IDocsumFieldWriter>
create_multi_writer(const IAttributeVector& attr,
                    bool filter_elements,
                    std::shared_ptr<MatchingElementsFields> matching_elems_fields)
{
    auto type = attr.getBasicType();
    bool is_weighted_set = attr.hasWeightedSetType();
    switch (type) {
    case BasicType::NONE:
    case BasicType::STRING: {
        return std::make_unique<MultiAttrDFW<IAttributeVector::WeightedString>>(attr.getName(), is_weighted_set,
                                                                                filter_elements, std::move(matching_elems_fields));
    }
    case BasicType::BOOL:
    case BasicType::UINT2:
    case BasicType::UINT4:
    case BasicType::INT8:
    case BasicType::INT16:
    case BasicType::INT32:
    case BasicType::INT64: {
        return std::make_unique<MultiAttrDFW<IAttributeVector::WeightedInt>>(attr.getName(), is_weighted_set,
                                                                             filter_elements, std::move(matching_elems_fields));
    }
    case BasicType::FLOAT:
    case BasicType::DOUBLE: {
        return std::make_unique<MultiAttrDFW<IAttributeVector::WeightedFloat>>(attr.getName(), is_weighted_set,
                                                                               filter_elements, std::move(matching_elems_fields));
    }
    default:
        // should not happen
        LOG(error, "Bad value for attribute type: %u", type);
        LOG_ASSERT(false);
    }
}

}

std::unique_ptr<IDocsumFieldWriter>
AttributeDFWFactory::create(IAttributeManager& attr_mgr,
                            const vespalib::string& attr_name,
                            bool filter_elements,
                            std::shared_ptr<MatchingElementsFields> matching_elems_fields)
{
    auto ctx = attr_mgr.createContext();
    const auto* attr = ctx->getAttribute(attr_name);
    if (attr == nullptr) {
        Issue::report("No valid attribute vector found: '%s'", attr_name.c_str());
        return std::unique_ptr<IDocsumFieldWriter>();
    }
    if (attr->hasMultiValue()) {
        return create_multi_writer(*attr, filter_elements, std::move(matching_elems_fields));
    } else {
        return std::make_unique<SingleAttrDFW>(attr->getName());
    }
}

}