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

#pragma once

#include "atomic_utils.h"
#include "attributevector.h"
#include "multi_value_mapping.h"
#include <vespa/searchcommon/attribute/i_multi_value_attribute.h>
#include <vespa/searchcommon/attribute/multi_value_traits.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,
                            public attribute::IMultiValueAttribute
{
protected:
    using DocId = typename B::DocId;
    using Change = typename B::Change;
    using ChangeVector = typename B::ChangeVector;

    using MultiValueType = M;
    using MultiValueMapping = attribute::MultiValueMapping<MultiValueType>;
    using ValueType = multivalue::ValueType_t<MultiValueType>;
    using ValueVector = std::vector<MultiValueType>;
    using MultiValueArrayRef = vespalib::ConstArrayRef<MultiValueType>;
    using DocumentValues = std::vector<std::pair<DocId, ValueVector> >;
    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;

    const IMultiValueAttribute* as_multi_value_attribute() const override;

    // Implements attribute::IMultiValueAttribute
    const attribute::IArrayReadView<ValueType>* make_read_view(attribute::IMultiValueAttribute::ArrayTag<ValueType>, vespalib::Stash& stash) const override;
    const attribute::IWeightedSetReadView<ValueType>* make_read_view(attribute::IMultiValueAttribute::WeightedSetTag<ValueType>, vespalib::Stash& stash) const override;
};

} // namespace search