aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/multivalueattribute.h
blob: 01cef03e687852ea475ec33af7264d38ac2bc69d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "multi_value_mapping.h"
#include "attributevector.h"
#include "atomic_utils.h"

namespace search {

/*
 * Implementation of multi value attribute using an underlying multi value mapping
 *
 * B: Base class
 * M: MultiValueType
 */
template <typename B, typename M>
class MultiValueAttribute : public B
{
protected:
    typedef typename B::DocId                             DocId;
    typedef typename B::Change                            Change;
    typedef typename B::ChangeVector                      ChangeVector;

    using MultiValueType = M;
    using MultiValueMapping = attribute::MultiValueMapping<MultiValueType>;
    typedef typename MultiValueType::ValueType            ValueType;
    typedef std::vector<MultiValueType>                   ValueVector;
    using MultiValueArrayRef = vespalib::ConstArrayRef<MultiValueType>;
    typedef typename ValueVector::iterator                ValueVectorIterator;
    typedef std::vector<std::pair<DocId, ValueVector> >   DocumentValues;
    using NonAtomicValueType = attribute::atomic_utils::NonAtomicValue_t<ValueType>;

    MultiValueMapping _mvMapping;

    MultiValueMapping &       getMultiValueMapping()       { return _mvMapping; }
    const MultiValueMapping & getMultiValueMapping() const { return _mvMapping; }

    /*
     * Iterate through the change vector and calculate new values for documents with changes
     */
    void applyAttributeChanges(DocumentValues & docValues);

    virtual bool extractChangeData(const Change & c, NonAtomicValueType & data) = 0;

    /**
     * Called when a new document has been added.
     * Can be overridden by subclasses that need to resize structures as a result of this.
     * Should return true if underlying structures were resized.
     **/
    bool onAddDoc(DocId doc) override { (void) doc; return false; }

    void populate_address_space_usage(AddressSpaceUsage& usage) const override;

public:
    MultiValueAttribute(const vespalib::string & baseFileName, const AttributeVector::Config & cfg);
    ~MultiValueAttribute() override;

    bool addDoc(DocId & doc) override;
    uint32_t getValueCount(DocId doc) const override;
    const attribute::MultiValueMappingBase *getMultiValueBase() const override {
        return &getMultiValueMapping();
    }

private:
    int32_t getWeight(DocId doc, uint32_t idx) const override;

    uint64_t getTotalValueCount() const override;

    void apply_attribute_changes_to_array(DocumentValues& docValues);
    void apply_attribute_changes_to_wset(DocumentValues& docValues);

public:
    void clearDocs(DocId lidLow, DocId lidLimit, bool in_shrink_lid_space) override;
    void onShrinkLidSpace() override ;
    void onAddDocs(DocId lidLimit) override;
};

} // namespace search