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

#pragma once

#include "unique_store_allocator.h"
#include <vespa/vespalib/stllike/allocator.h>

namespace vespalib::datastore {

class IUniqueStoreDictionary;

/**
 * Builder for related UniqueStore class.
 *
 * Contains utility method for adding new unique values and mapping
 * from enum value to EntryRef value.  New unique values must be added
 * in sorted order.
 */
template <typename Allocator>
class UniqueStoreBuilder {
    using EntryType = typename Allocator::EntryType;

    Allocator& _allocator;
    IUniqueStoreDictionary& _dict;
    std::vector<EntryRef, allocator_large<EntryRef>> _refs;
    std::vector<uint32_t, allocator_large<uint32_t>> _refCounts;

public:
    UniqueStoreBuilder(Allocator& allocator, IUniqueStoreDictionary& dict, uint32_t uniqueValuesHint);
    ~UniqueStoreBuilder();
    void setupRefCounts();
    void makeDictionary();
    void add(const EntryType& value) {
        EntryRef newRef = _allocator.allocate(value);
        _refs.push_back(newRef);
    }
    EntryRef mapEnumValueToEntryRef(uint32_t enumValue) {
        assert(enumValue < _refs.size());
        assert(_refCounts[enumValue] < std::numeric_limits<uint32_t>::max());
        ++_refCounts[enumValue];
        return _refs[enumValue];
    }
};

}