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

#include <vespa/searchlib/attribute/i_document_weight_attribute.h>
#include <vespa/searchlib/attribute/attributevector.h>
#include <vespa/searchlib/attribute/multinumericattribute.h>
#include <vespa/searchlib/attribute/multinumericpostattribute.hpp>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/vespalib/testkit/test_kit.h>

namespace search::test {

class DocumentWeightAttributeHelper
{
private:
    AttributeVector::SP _attr;
    IntegerAttribute *_int_attr;
    const IDocumentWeightAttribute *_dwa;

    AttributeVector::SP make_attr();

public:
    DocumentWeightAttributeHelper()
        : _attr(make_attr()),
          _int_attr(dynamic_cast<IntegerAttribute *>(_attr.get())),
          _dwa(_attr->asDocumentWeightAttribute())
    {
        ASSERT_TRUE(_int_attr != nullptr);
        ASSERT_TRUE(_dwa != nullptr);
    }
    ~DocumentWeightAttributeHelper();

    void add_docs(size_t limit) {
        AttributeVector::DocId docid;
        for (size_t i = 0; i < limit; ++i) {
            _attr->addDoc(docid);
        }
        _attr->commit();
        ASSERT_EQUAL((limit - 1), docid);
    }

    void set_doc(uint32_t docid, int64_t key, int32_t weight) {
        _int_attr->clearDoc(docid);
        _int_attr->append(docid, key, weight);
        _int_attr->commit();
    }

    const IDocumentWeightAttribute &dwa() const { return *_dwa; }
};

}