summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/docsummary/attribute_combiner/attribute_combiner_test.cpp
blob: 97fafd0a446843fcfe492581d71f3e1ea36ea869 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchcommon/common/undefinedvalues.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/attribute/attributemanager.h>
#include <vespa/searchlib/attribute/attributevector.h>
#include <vespa/searchlib/attribute/attributevector.hpp>
#include <vespa/searchlib/attribute/floatbase.h>
#include <vespa/searchlib/attribute/integerbase.h>
#include <vespa/searchlib/attribute/stringbase.h>
#include <vespa/searchlib/util/slime_output_raw_buf_adapter.h>
#include <vespa/searchsummary/docsummary/docsumstate.h>
#include <vespa/searchsummary/docsummary/docsum_field_writer_state.h>
#include <vespa/searchsummary/docsummary/attribute_combiner_dfw.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/testkit/testapp.h>

#include <vespa/log/log.h>
LOG_SETUP("attribute_combiner_test");

using search::AttributeFactory;
using search::AttributeManager;
using search::AttributeVector;
using search::IntegerAttribute;
using search::FloatingPointAttribute;
using search::StringAttribute;
using search::attribute::BasicType;
using search::attribute::CollectionType;
using search::attribute::Config;
using search::attribute::IAttributeVector;
using search::attribute::getUndefined;
using search::docsummary::AttributeCombinerDFW;
using search::docsummary::GetDocsumsState;
using search::docsummary::GetDocsumsStateCallback;
using search::docsummary::IDocsumEnvironment;
using search::docsummary::IDocsumFieldWriter;

namespace {

vespalib::string
toCompactJsonString(const vespalib::Slime &slime)
{
    vespalib::SimpleBuffer buf;
    vespalib::slime::JsonFormat::encode(slime, buf, true);
    return buf.get().make_string();
}

struct FieldBlock {
    vespalib::string input;
    vespalib::Slime slime;
    search::RawBuf binary;
    vespalib::string json;

    explicit FieldBlock(const vespalib::string &jsonInput)
        : input(jsonInput), slime(), binary(1024), json()
    {
        size_t used = vespalib::slime::JsonFormat::decode(jsonInput, slime);
        EXPECT_TRUE(used > 0);
        json = toCompactJsonString(slime);
        search::SlimeOutputRawBufAdapter adapter(binary);
        vespalib::slime::BinaryFormat::encode(slime, adapter);
    }
    const char *data() const { return binary.GetDrainPos(); }
    size_t dataLen() const { return binary.GetUsedLen(); }
};

struct AttributeManagerFixture
{
    AttributeManager mgr;

    AttributeManagerFixture();

    ~AttributeManagerFixture();

    template <typename AttributeType, typename ValueType>
    void
    buildAttribute(const vespalib::string &name,
                   BasicType type,
                   std::vector<std::vector<ValueType>> values);

    void
    buildStringAttribute(const vespalib::string &name,
                         std::vector<std::vector<vespalib::string>> values);
    void
    buildFloatAttribute(const vespalib::string &name,
                        std::vector<std::vector<double>> values);

    void
    buildIntegerAttribute(const vespalib::string &name,
                          BasicType type,
                          std::vector<std::vector<IAttributeVector::largeint_t>> values);
};

AttributeManagerFixture::AttributeManagerFixture()
    : mgr()
{
    buildStringAttribute("array.name", {{"n1.1", "n1.2"}, {"n2"}, {"n3.1", "n3.2"}, {"", "n4.2"}});
    buildIntegerAttribute("array.val", BasicType::Type::INT8, {{ 10, 11}, {20, 21 }, {30}, { getUndefined<int8_t>(), 41}});
    buildFloatAttribute("array.fval", {{ 110.0}, { 120.0, 121.0 }, { 130.0, 131.0}, { getUndefined<double>(), 141.0 }});
}

AttributeManagerFixture::~AttributeManagerFixture() = default;

template <typename AttributeType, typename ValueType>
void
AttributeManagerFixture::buildAttribute(const vespalib::string &name,
                                        BasicType type,
                                        std::vector<std::vector<ValueType>> values)
{
    Config cfg(type, CollectionType::Type::ARRAY);
    auto attrBase = AttributeFactory::createAttribute(name, cfg);
    EXPECT_TRUE(attrBase);
    auto attr = std::dynamic_pointer_cast<AttributeType>(attrBase);
    EXPECT_TRUE(attr);
    attr->addReservedDoc();
    for (const auto &docValues : values) {
        uint32_t docId = 0;
        EXPECT_TRUE(attr->addDoc(docId));
        EXPECT_NOT_EQUAL(0u, docId);
        for (const auto &value : docValues) {
            attr->append(docId, value, 1);
        }
        attr->commit();
    }
    EXPECT_TRUE(mgr.add(attr));
}

void
AttributeManagerFixture::buildStringAttribute(const vespalib::string &name,
                                              std::vector<std::vector<vespalib::string>> values)
{
    buildAttribute<StringAttribute, vespalib::string>(name, BasicType::Type::STRING, std::move(values));
}

void
AttributeManagerFixture::buildFloatAttribute(const vespalib::string &name,
                                             std::vector<std::vector<double>> values)
{
    buildAttribute<FloatingPointAttribute, double>(name, BasicType::Type::DOUBLE, std::move(values));
}

void
AttributeManagerFixture::buildIntegerAttribute(const vespalib::string &name,
                                               BasicType type,
                                               std::vector<std::vector<IAttributeVector::largeint_t>> values)
{
    buildAttribute<IntegerAttribute, IAttributeVector::largeint_t>(name, type, std::move(values));
}


class DummyStateCallback : public GetDocsumsStateCallback
{
public:
    void FillSummaryFeatures(GetDocsumsState *, IDocsumEnvironment *) override { }
    void FillRankFeatures(GetDocsumsState *, IDocsumEnvironment *) override { }
    void ParseLocation(GetDocsumsState *) override { }
    ~DummyStateCallback() override { }
};


struct Fixture
{
    AttributeManagerFixture             attrs;
    std::unique_ptr<IDocsumFieldWriter> writer;
    DummyStateCallback                  stateCallback;
    GetDocsumsState                     state;

    Fixture();
    ~Fixture();
    void assertWritten(const vespalib::string &exp, uint32_t docId);
};

Fixture::Fixture()
    : attrs(),
      writer(AttributeCombinerDFW::create("array", attrs.mgr)),
      stateCallback(),
      state(stateCallback)
{
    EXPECT_TRUE(writer->setFieldWriterStateIndex(0));
    state._attrCtx = attrs.mgr.createContext();
    state._fieldWriterStates.resize(1);
}

Fixture::~Fixture()
{
}

void
Fixture::assertWritten(const vespalib::string &expectedJson, uint32_t docId)
{
    vespalib::Slime target;
    vespalib::slime::SlimeInserter inserter(target);
    writer->insertField(docId, nullptr, &state, search::docsummary::RES_JSONSTRING, inserter);
    search::RawBuf binary(1024);
    vespalib::string json = toCompactJsonString(target);
    search::SlimeOutputRawBufAdapter adapter(binary);
    vespalib::slime::BinaryFormat::encode(target, adapter);
    FieldBlock block(expectedJson);
    if (!EXPECT_EQUAL(block.dataLen(), binary.GetUsedLen()) ||
        !EXPECT_EQUAL(0, memcmp(block.data(), binary.GetDrainPos(), block.dataLen()))) {
        LOG(error, "Expected '%s'", expectedJson.c_str());
        LOG(error, "Expected normalized '%s'", block.json.c_str());
        LOG(error, "Got '%s'", json.c_str());
    }
}

TEST_F("require that attributes combiner dfw generates correct slime output for array of struct", Fixture())
{
    f.assertWritten("[ { fval: 110.0, name: \"n1.1\", val: 10}, { name: \"n1.2\", val: 11}]", 1);
    f.assertWritten("[ { fval: 120.0, name: \"n2\", val: 20}, { fval: 121.0, val: 21 }]", 2);
    f.assertWritten("[ { fval: 130.0, name: \"n3.1\", val: 30}, { fval: 131.0, name: \"n3.2\"} ]", 3);
    f.assertWritten("[ { }, { fval: 141.0, name: \"n4.2\", val:  41} ]", 4);
}

}

TEST_MAIN() { TEST_RUN_ALL(); }