aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/datastore/unique_store_enumerator.h
blob: a6e7c2c974e8ba704450216f6502c64ac49c8833 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "i_unique_store_dictionary.h"
#include "i_unique_store_dictionary_read_snapshot.h"
#include <vespa/vespalib/stllike/allocator.h>
#include <cassert>

namespace vespalib::datastore {

class DataStoreBase;

/**
 * Enumerator for related UniqueStore class.
 *
 * Contains utility methods for traversing all unique values (as
 * EntryRef value) and mapping from EntryRef value to enum value.
 */
template <typename RefT>
class UniqueStoreEnumerator {
public:
    using RefType = RefT;

private:
    using UInt32Vector = std::vector<uint32_t, vespalib::allocator_large<uint32_t>>;
    using EnumValues = std::vector<UInt32Vector>;
    std::unique_ptr<IUniqueStoreDictionaryReadSnapshot> _dict_snapshot;
    const DataStoreBase &_store;
    EnumValues _enumValues;
    uint32_t _next_enum_val;

    void allocate_enum_values(DataStoreBase &store);
public:
    UniqueStoreEnumerator(const IUniqueStoreDictionary &dict, DataStoreBase &store, bool sort_unique_values);
    ~UniqueStoreEnumerator();
    void enumerateValue(EntryRef ref);
    void enumerateValues();
    void clear();

    template <typename Function>
    void
    foreach_key(Function &&func) const {
        _dict_snapshot->foreach_key(func);
    }

    uint32_t mapEntryRefToEnumValue(EntryRef ref) const {
        if (ref.valid()) {
            RefType iRef(ref);
            assert(iRef.offset() < _enumValues[iRef.bufferId()].size());
            uint32_t enumValue = _enumValues[iRef.bufferId()][iRef.offset()];
            assert(enumValue != 0);
            return enumValue;
        } else {
            return 0u;
        }
    }

    uint32_t map_entry_ref_to_enum_value_or_zero(EntryRef ref) const {
        if (ref.valid()) {
            RefType iRef(ref);
            if (iRef.offset() < _enumValues[iRef.bufferId()].size()) {
                return  _enumValues[iRef.bufferId()][iRef.offset()];
            } else {
                return 0u;
            }
        } else {
            return 0u;
        }
    }
};

}