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

#pragma once

#include "enumattribute.h"
#include "address_space_components.h"
#include "enumstore.hpp"
#include <vespa/vespalib/util/hdr_abort.h>
#include <vespa/searchcommon/attribute/config.h>

namespace search {

template <typename B>
EnumAttribute<B>::
EnumAttribute(const vespalib::string &baseFileName,
              const AttributeVector::Config &cfg)
    : B(baseFileName, cfg),
      _enumStore(cfg.fastSearch(), cfg.get_dictionary_config(), this->get_memory_allocator(), this->_defaultValue._data.raw())
{
    this->setEnum(true);
}

template <typename B>
EnumAttribute<B>::~EnumAttribute() = default;

template <typename B>
void EnumAttribute<B>::load_enum_store(LoadedVector& loaded)
{
    if constexpr(!std::is_same_v<LoadedVector, NoLoadedVector>) {
        auto loader = _enumStore.make_non_enumerated_loader();
        if (!loaded.empty()) {
            auto value = loaded.read();
            typename B::LoadedValueType prev = value.getValue();
            uint32_t prevRefCount(0);
            EnumIndex index = loader.insert(value.getValue(), value._pidx.ref());
            for (size_t i(0), m(loaded.size()); i < m; ++i, loaded.next()) {
                value = loaded.read();
                if (!EnumStore::ComparatorType::equal_helper(prev, value.getValue())) {
                    loader.set_ref_count_for_last_value(prevRefCount);
                    index = loader.insert(value.getValue(), value._pidx.ref());
                    prev = value.getValue();
                    prevRefCount = 1;
                } else {
                    assert(prevRefCount < std::numeric_limits<uint32_t>::max());
                    prevRefCount++;
                }
                value.setEidx(index);
                loaded.write(value);
            }
            loader.set_ref_count_for_last_value(prevRefCount);
        }
        loader.build_dictionary();
        _enumStore.setup_default_value_ref();
    }
}

template <typename B>
uint64_t
EnumAttribute<B>::getUniqueValueCount() const
{
    return _enumStore.get_num_uniques();
}

template <typename B>
void
EnumAttribute<B>::insertNewUniqueValues(EnumStoreBatchUpdater& updater)
{
    // find and insert new unique strings
    for (const auto & data : this->_changes.getInsertOrder()) {
        considerAttributeChange(data, updater);
    }
}

template <typename B>
vespalib::MemoryUsage
EnumAttribute<B>::getEnumStoreValuesMemoryUsage() const
{
    return _enumStore.get_dynamic_values_memory_usage();
}

template <typename B>
void
EnumAttribute<B>::populate_address_space_usage(AddressSpaceUsage& usage) const
{
    B::populate_address_space_usage(usage);
    usage.set(AddressSpaceComponents::enum_store, _enumStore.get_values_address_space_usage());
}

template <typename B>
void
EnumAttribute<B>::cache_change_data_entry_ref(const Change& c) const
{
    EnumIndex new_idx;
    _enumStore.find_index(c._data.raw(), new_idx);
    c.set_entry_ref(new_idx.ref());
}

} // namespace search