aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/attribute/multi_value_read_view/multi_value_read_view_test.cpp
blob: de832c688c24a75e620dd08afe247118600f32bd (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/document/base/documentid.h>
#include <vespa/searchcommon/attribute/i_multi_value_attribute.h>
#include <vespa/searchcommon/attribute/multi_value_traits.h>
#include <vespa/searchcommon/attribute/config.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/attribute/attribute_read_guard.h>
#include <vespa/searchlib/attribute/extendableattributes.h>
#include <vespa/searchlib/attribute/floatbase.h>
#include <vespa/searchlib/attribute/imported_attribute_vector.h>
#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h>
#include <vespa/searchlib/attribute/integerbase.h>
#include <vespa/searchlib/attribute/reference_attribute.h>
#include <vespa/searchlib/attribute/stringbase.h>
#include <vespa/searchlib/test/mock_gid_to_lid_mapping.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/stash.h>

using document::GlobalId;
using document::DocumentId;

namespace search::attribute {

using test::MockGidToLidMapperFactory;

namespace {

GlobalId toGid(vespalib::stringref docId) {
    return DocumentId(docId).getGlobalId();
}

vespalib::string doc1("id:test:music::1");
vespalib::string doc2("id:test:music::2");

struct MyGidToLidMapperFactory : public MockGidToLidMapperFactory
{
    MyGidToLidMapperFactory()
        : MockGidToLidMapperFactory()
    {
        _map.insert({toGid(doc1), 1});
        _map.insert({toGid(doc2), 2});
    }
};

struct MockReadGuard : public IDocumentMetaStoreContext::IReadGuard {
    virtual const search::IDocumentMetaStore &get() const override {
        search::IDocumentMetaStore *nullStore = nullptr;
        return static_cast<search::IDocumentMetaStore &>(*nullStore);
    }
};

struct MockDocumentMetaStoreContext : public IDocumentMetaStoreContext
{
    std::shared_ptr<IReadGuard> getReadGuard() const override;
};

std::shared_ptr<IDocumentMetaStoreContext::IReadGuard>
MockDocumentMetaStoreContext::getReadGuard() const
{
    return std::make_shared<MockReadGuard>();
}


std::shared_ptr<ReferenceAttribute>
create_reference_attribute(const vespalib::string &name, const std::shared_ptr<IGidToLidMapperFactory> gid_to_lid_mapper_factory)
{
    auto attr = std::make_shared<ReferenceAttribute>(name, Config(BasicType::REFERENCE));
    attr->addReservedDoc();
    while (attr->getNumDocs() < 20u) {
        uint32_t doc_id = 0;
        attr->addDoc(doc_id);
        EXPECT_NE(0u, doc_id);
    }
    attr->update(4, toGid(doc1));
    attr->update(11, toGid(doc2));
    attr->setGidToLidMapperFactory(gid_to_lid_mapper_factory);
    attr->populateTargetLids({});
    return attr;
}

}

class TestParam {
    BasicType _basic_type;

public:
    TestParam(BasicType basic_type_in)
        : _basic_type(basic_type_in)
    {
    }

    BasicType basic_type() const noexcept { return _basic_type; }
};

std::ostream& operator<<(std::ostream& os, const TestParam& param)
{
    os << param.basic_type().asString();
    return os;
}

class MultiValueReadViewTest : public ::testing::TestWithParam<TestParam>
{
protected:
    std::shared_ptr<IGidToLidMapperFactory> _gid_to_lid_mapper_factory;
    std::shared_ptr<ReferenceAttribute> _reference_attribute;
    MultiValueReadViewTest()
        : ::testing::TestWithParam<TestParam>(),
          _gid_to_lid_mapper_factory(std::make_shared<MyGidToLidMapperFactory>()),
          _reference_attribute(create_reference_attribute("ref", _gid_to_lid_mapper_factory))
    {
    }
    ~MultiValueReadViewTest() override = default;

    template <typename AttributeBaseType, typename BaseType>
    void populate_helper(AttributeVector& attr, const std::vector<BaseType>& values);
    void populate(AttributeVector& attr);
    template <typename MultiValueType>
    void check_values_helper(const IAttributeVector &attr, const std::vector<multivalue::ValueType_t<MultiValueType>>& exp_values);
    template <typename BasicType>
    void check_integer_values(const IAttributeVector &attr);
    template <typename BasicType>
    void check_floating_point_values(const IAttributeVector &attr);
    void check_string_values(const IAttributeVector &attr);
    void check_values(const IAttributeVector& attr);
    std::shared_ptr<AttributeVector> make_attribute(CollectionType collection_type, bool fast_search);
    std::shared_ptr<ReadableAttributeVector> make_imported_attribute(std::shared_ptr<AttributeVector> target);
    std::shared_ptr<AttributeVector> make_extendable_attribute(CollectionType collection_type);
    void test_normal_attribute_vector(CollectionType collection_type, bool fast_search);
    void test_imported_attribute_vector(CollectionType collection_type, bool fast_search);
    void test_extendable_attribute_vector(CollectionType collection_type);
};

template <typename AttributeBaseType, typename BaseType>
void
MultiValueReadViewTest::populate_helper(AttributeVector& attr, const std::vector<BaseType>& values)
{
    auto extend_interface = attr.getExtendInterface();
    if (extend_interface == nullptr) {
        attr.addReservedDoc();
    } else {
        uint32_t doc_id = 0;
        EXPECT_TRUE(attr.addDoc(doc_id));
        EXPECT_EQ(0u, doc_id);
    }
    uint32_t doc_id = 0;
    attr.addDoc(doc_id);
    EXPECT_EQ(1u, doc_id);
    if (extend_interface == nullptr) {
        attr.clearDoc(doc_id);
    }
    attr.addDoc(doc_id);
    EXPECT_EQ(2u, doc_id);
    if (extend_interface == nullptr) {
        attr.clearDoc(doc_id);
        auto& spec_attr = dynamic_cast<AttributeBaseType&>(attr);
        EXPECT_TRUE(spec_attr.append(doc_id, values[0], 2));
        EXPECT_TRUE(spec_attr.append(doc_id, values[1], 7));
        attr.commit();
    } else {
        EXPECT_TRUE(extend_interface->add(values[0], 2));
        EXPECT_TRUE(extend_interface->add(values[1], 7));
    }
}

void
MultiValueReadViewTest::populate(AttributeVector& attr)
{
    switch (attr.getConfig().basicType().type()) {
    case BasicType::Type::INT8:
    case BasicType::Type::INT16:
    case BasicType::Type::INT32:
    case BasicType::Type::INT64:
        populate_helper<IntegerAttribute, int64_t>(attr, {42, 44});
        break;
    case BasicType::Type::FLOAT:
    case BasicType::Type::DOUBLE:
        populate_helper<FloatingPointAttribute, double>(attr, {42.0, 44.0});
        break;
    case BasicType::Type::STRING:
        populate_helper<StringAttribute, const char*>(attr, {"42", "44"});
        break;
    default:
        FAIL() << "Cannot populate attribute vector";
    }
}

namespace {

template <typename BasicType>
struct CompareValues
{
    bool operator()(const BasicType &lhs, const BasicType &rhs) const { return lhs < rhs; }
    bool operator()(const multivalue::WeightedValue<BasicType>& lhs, const multivalue::WeightedValue<BasicType>& rhs) const { return lhs.value() < rhs.value(); }
    bool equal(const BasicType &lhs, const BasicType &rhs) const { return lhs == rhs; }
    bool equal(const multivalue::WeightedValue<BasicType>& lhs, const multivalue::WeightedValue<BasicType>& rhs) const { return lhs.value() == rhs.value(); }
};

template <>
struct CompareValues<const char *>
{
    bool operator()(const char *lhs, const char *rhs) const { return strcmp(lhs, rhs) < 0; }
    bool operator()(const multivalue::WeightedValue<const char *>& lhs, const multivalue::WeightedValue<const char *>& rhs) const { return strcmp(lhs.value(), rhs.value()) < 0; }
    bool equal(const char *lhs, const char *rhs) const { return strcmp(lhs, rhs) == 0; }
    bool equal(const multivalue::WeightedValue<const char *>& lhs, const multivalue::WeightedValue<const char *>& rhs) const { return strcmp(lhs.value(), rhs.value()) == 0; }
};

}

template <typename MultiValueType>
void
MultiValueReadViewTest::check_values_helper(const IAttributeVector &attr, const std::vector<multivalue::ValueType_t<MultiValueType>>& exp_values)
{
    vespalib::Stash stash;
    auto mv_attr = attr.as_multi_value_attribute();
    EXPECT_NE(nullptr, mv_attr);
    auto read_view = mv_attr->make_read_view(IMultiValueAttribute::MultiValueTag<MultiValueType>(), stash);
    EXPECT_NE(nullptr, read_view);
    bool is_imported = attr.isImported();
    auto values = read_view->get_values(is_imported ? 4 : 1);
    EXPECT_TRUE(values.empty());
    values = read_view->get_values(is_imported ? 11 : 2);
    std::vector<MultiValueType> values_copy(values.begin(), values.end());
    bool was_array = true;
    CompareValues<multivalue::ValueType_t<MultiValueType>> compare_values;
    if (attr.getCollectionType() == CollectionType::Type::WSET) {
        std::sort(values_copy.begin(), values_copy.end(), compare_values);
        was_array = false;
    }
    EXPECT_EQ(2u, values_copy.size());
    if constexpr (multivalue::is_WeightedValue_v<MultiValueType>) {
        EXPECT_TRUE(compare_values.equal(exp_values[0], values_copy[0].value()));
        EXPECT_EQ(was_array ? 1 : 2, values_copy[0].weight());
        EXPECT_TRUE(compare_values.equal(exp_values[1], values_copy[1].value()));
        EXPECT_EQ(was_array ? 1 : 7, values_copy[1].weight());
    } else {
        EXPECT_TRUE(compare_values.equal(exp_values[0], values_copy[0]));
        EXPECT_TRUE(compare_values.equal(exp_values[1], values_copy[1]));
    }
}

template <typename BasicType>
void
MultiValueReadViewTest::check_integer_values(const IAttributeVector &attr)
{
    std::vector<BasicType> exp_values{42, 44};
    check_values_helper<BasicType>(attr, exp_values);
    check_values_helper<multivalue::WeightedValue<BasicType>>(attr, exp_values);
}

template <typename BasicType>
void
MultiValueReadViewTest::check_floating_point_values(const IAttributeVector &attr)
{
    std::vector<BasicType> exp_values{42.0, 44.0};
    check_values_helper<BasicType>(attr, exp_values);
    check_values_helper<multivalue::WeightedValue<BasicType>>(attr, exp_values);
}

void
MultiValueReadViewTest::check_string_values(const IAttributeVector &attr)
{
    std::vector<const char *> exp_values{"42", "44"};
    check_values_helper<const char *>(attr, exp_values);
    check_values_helper<multivalue::WeightedValue<const char *>>(attr, exp_values);
}

void
MultiValueReadViewTest::check_values(const IAttributeVector& attr)
{
    switch (attr.getBasicType()) {
    case BasicType::Type::INT8:
        check_integer_values<int8_t>(attr);
        break;
    case BasicType::Type::INT16:
        check_integer_values<int16_t>(attr);
        break;
    case BasicType::Type::INT32:
        check_integer_values<int32_t>(attr);
        break;
    case BasicType::Type::INT64:
        check_integer_values<int64_t>(attr);
        break;
    case BasicType::Type::FLOAT:
        check_floating_point_values<float>(attr);
        break;
    case BasicType::Type::DOUBLE:
        check_floating_point_values<double>(attr);
        break;
    case BasicType::Type::STRING:
        check_string_values(attr);
        break;
    default:
        FAIL() << "Cannot check values in attribute vector";
    }
}

std::shared_ptr<AttributeVector>
MultiValueReadViewTest::make_attribute(CollectionType collection_type, bool fast_search)
{
    auto param = GetParam();
    Config config(param.basic_type(), collection_type);
    config.setFastSearch(fast_search);
    auto attr = AttributeFactory::createAttribute("attr", config);
    return attr;
}

std::shared_ptr<ReadableAttributeVector>
MultiValueReadViewTest::make_imported_attribute(std::shared_ptr<AttributeVector> target)
{
    return ImportedAttributeVectorFactory::create("imported",
                                                  _reference_attribute,
                                                  std::make_shared<MockDocumentMetaStoreContext>(),
                                                  target,
                                                  std::make_shared<MockDocumentMetaStoreContext>(),
                                                  false);
}

std::shared_ptr<AttributeVector>
MultiValueReadViewTest::make_extendable_attribute(CollectionType collection_type)
{
    vespalib::string name("attr");
    // Match strategy in streaming visitor
    switch (collection_type.type()) {
    case CollectionType::Type::ARRAY:
        switch (GetParam().basic_type().type()) {
        case BasicType::Type::INT8:
        case BasicType::Type::INT16:
        case BasicType::Type::INT32:
        case BasicType::Type::INT64:
            return std::make_shared<MultiIntegerExtAttribute>(name);
        case BasicType::Type::FLOAT:
        case BasicType::Type::DOUBLE:
            return std::make_shared<MultiFloatExtAttribute>(name);
        case BasicType::Type::STRING:
            return std::make_shared<MultiStringExtAttribute>(name);
        default:
            ;
        }
        break;
    case CollectionType::Type::WSET:
        switch (GetParam().basic_type().type()) {
        case BasicType::Type::INT8:
        case BasicType::Type::INT16:
        case BasicType::Type::INT32:
        case BasicType::Type::INT64:
            return std::make_shared<WeightedSetIntegerExtAttribute>(name);
        case BasicType::Type::FLOAT:
        case BasicType::Type::DOUBLE:
            return std::make_shared<WeightedSetFloatExtAttribute>(name);
        case BasicType::Type::STRING:
            return std::make_shared<WeightedSetStringExtAttribute>(name);
        default:
            ;
        }
        break;
    default:
        ;
    }
    return {};
}

void
MultiValueReadViewTest::test_normal_attribute_vector(CollectionType collection_type, bool fast_search)
{
    auto attr = make_attribute(collection_type, fast_search);
    populate(*attr);
    check_values(*attr);
}

void
MultiValueReadViewTest::test_imported_attribute_vector(CollectionType collection_type, bool fast_search)
{
    auto attr = make_attribute(collection_type, fast_search);
    populate(*attr);
    auto imported_attr = make_imported_attribute(attr);
    auto guard = imported_attr->makeReadGuard(false);
    check_values(**guard);
}

void
MultiValueReadViewTest::test_extendable_attribute_vector(CollectionType collection_type)
{
    auto attr = make_extendable_attribute(collection_type);
    if (attr == nullptr) {
        FAIL() << "Cannot create extend attribute";
    }
    populate(*attr);
    check_values(*attr);
}

TEST_P(MultiValueReadViewTest, test_array)
{
    test_normal_attribute_vector(CollectionType::Type::ARRAY, false);
};

TEST_P(MultiValueReadViewTest, test_enumerated_array)
{
    test_normal_attribute_vector(CollectionType::Type::ARRAY, true);
};

TEST_P(MultiValueReadViewTest, test_weighted_set)
{
    test_normal_attribute_vector(CollectionType::Type::WSET, false);
};

TEST_P(MultiValueReadViewTest, test_enumerated_weighted_set)
{
    test_normal_attribute_vector(CollectionType::Type::WSET, true);
};

TEST_P(MultiValueReadViewTest, test_imported_array)
{
    test_imported_attribute_vector(CollectionType::Type::ARRAY, false);
};

TEST_P(MultiValueReadViewTest, test_imported_enumerated_array)
{
    test_imported_attribute_vector(CollectionType::Type::ARRAY, true);
};

TEST_P(MultiValueReadViewTest, test_importe_weighted_set)
{
    test_imported_attribute_vector(CollectionType::Type::WSET, false);
};

TEST_P(MultiValueReadViewTest, test_imported_enumerated_weighted_set)
{
    test_imported_attribute_vector(CollectionType::Type::WSET, true);
};

TEST_P(MultiValueReadViewTest, test_extendable_array)
{
    test_extendable_attribute_vector(CollectionType::Type::ARRAY);
}

TEST_P(MultiValueReadViewTest, test_extendable_weighted_set)
{
    test_extendable_attribute_vector(CollectionType::Type::WSET);
}

auto test_values = ::testing::Values(TestParam(BasicType::Type::INT8),
                                     TestParam(BasicType::Type::INT16),
                                     TestParam(BasicType::Type::INT32),
                                     TestParam(BasicType::Type::INT64),
                                     TestParam(BasicType::Type::FLOAT),
                                     TestParam(BasicType::Type::DOUBLE),
                                     TestParam(BasicType::Type::STRING));

INSTANTIATE_TEST_SUITE_P(ReadView, MultiValueReadViewTest, test_values,testing::PrintToStringParamName());

}

GTEST_MAIN_RUN_ALL_TESTS()