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

#pragma once

#include "enum_store_types.h"
#include <vespa/vespalib/util/arrayref.h>
#include <cassert>
#include <limits>

namespace search::attribute {

/**
 * Temporary representation of enumerated attribute loaded from enumerated
 * save file.
 */

class LoadedEnumAttribute
{
private:
    uint32_t _enum;
    uint32_t _docId;
    int32_t  _weight;

public:
    class EnumRadix
    {
    public:
        uint64_t
        operator()(const LoadedEnumAttribute &v)
        {
            return (static_cast<uint64_t>(v._enum) << 32) | v.getDocId();
        }
    };

    class EnumCompare
    {
    public:
        bool
        operator()(const LoadedEnumAttribute &x,
                   const LoadedEnumAttribute &y) const
        {
            if (x.getEnum() != y.getEnum())
                return x.getEnum() < y.getEnum();
            return x.getDocId() < y.getDocId();
        }
    };

    LoadedEnumAttribute()
        : _enum(0),
          _docId(0),
          _weight(1)
    {
    }

    LoadedEnumAttribute(uint32_t e,
                        uint32_t docId,
                        int32_t weight)
        : _enum(e),
          _docId(docId),
          _weight(weight)
    {
    }

    uint32_t getEnum() const  { return _enum; }
    uint32_t getDocId() const { return _docId; }
    int32_t getWeight() const { return _weight; }
};

using LoadedEnumAttributeVector = std::vector<LoadedEnumAttribute, vespalib::allocator_large<LoadedEnumAttribute>>;


/**
 * Helper class used to populate temporary vector representing loaded
 * enumerated attribute with posting lists loaded from enumerated save
 * file.
 */

class SaveLoadedEnum
{
private:
    LoadedEnumAttributeVector &_loaded;

public:
    SaveLoadedEnum(LoadedEnumAttributeVector &loaded)
        : _loaded(loaded)
    {
    }

    void
    save(uint32_t e, uint32_t docId, int32_t weight)
    {
        _loaded.push_back(LoadedEnumAttribute(e, docId, weight));
    }
};

/**
 * Helper class used when loading non-enumerated attribute from
 * enumerated save file.
 */

class NoSaveLoadedEnum
{
public:
    static void
    save(uint32_t e, uint32_t docId, int32_t weight)
    {
        (void) e;
        (void) docId;
        (void) weight;
    }
};

/**
 * Helper class used to populate temporary vector representing loaded
 * enumerated attribute without posting lists loaded from enumerated
 * save file.
 */

class SaveEnumHist
{
    vespalib::ArrayRef<uint32_t> _hist;

public:
    SaveEnumHist(enumstore::EnumVector &enumHist)
        : _hist(enumHist)
    {
    }

    void
    save(uint32_t e, uint32_t docId, int32_t weight)
    {
        (void) docId;
        (void) weight;
        assert(e < _hist.size());
        assert(_hist[e] < std::numeric_limits<uint32_t>::max());
        ++_hist[e];
    }
};

void sortLoadedByEnum(LoadedEnumAttributeVector &loaded);

}