aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/multivalueattributesaverutils.h
blob: 1a8d1eaa23ad093c4c81bdc90f4dad0dfeaf5689 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "iattributesavetarget.h"
#include <vespa/searchlib/util/bufferwriter.h>
#include <vespa/vespalib/util/arrayref.h>

namespace search::multivalueattributesaver {

/*
 * Class to write to count files for multivalue attributes (.idx suffix).
 */
class CountWriter
{
    std::unique_ptr<search::BufferWriter> _countWriter;
    uint64_t _cnt;

public:
    CountWriter(IAttributeSaveTarget &saveTarget);
    ~CountWriter();

    void writeCount(uint32_t count);
};

/*
 * Class to write to weight files (or not) for multivalue attributes.
 */
template <bool hasWeight>
class WeightWriter;

/*
 * Class to write to weight files for multivalue attributes (.weight suffix).
 */
template <>
class WeightWriter<true>
{
    std::unique_ptr<search::BufferWriter> _weightWriter;

public:
    WeightWriter(IAttributeSaveTarget &saveTarget)
        : _weightWriter(saveTarget.weightWriter().allocBufferWriter())
    {}

    ~WeightWriter() {
        _weightWriter->flush();
    }

    template <typename MultiValueT>
    void
    writeWeights(vespalib::ConstArrayRef<MultiValueT> values) {
        for (const MultiValueT &valueRef : values) {
            int32_t weight = valueRef.weight();
            _weightWriter->write(&weight, sizeof(int32_t));
        }
    }
};

/*
 * Class to not write to weight files for multivalue attributes.
 */
template <>
class WeightWriter<false>
{
public:
    WeightWriter(IAttributeSaveTarget &) {}

    ~WeightWriter() {}

    template <typename MultiValueT>
    void writeWeights(vespalib::ConstArrayRef<MultiValueT>) {}
};

}