aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/btree/btree_key_data.h
blob: 3afcec63ae21faa5e8bfc9bcdd0722e94f3d4dd6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <cstdint>

namespace vespalib::btree {

/**
 * Empty class to use as DataT template parameter for BTree classes to
 * indicate that leaf nodes have no data (similar to std::set having less
 * information than std::map).
 */
class BTreeNoLeafData
{
public:
    static BTreeNoLeafData _instance;
};


template <typename KeyT, typename DataT>
class BTreeKeyData
{
public:
    using KeyType = KeyT;
    using DataType = DataT;

    KeyT _key;
    DataT _data;

    BTreeKeyData() noexcept
        : _key(),
          _data()
    {}

    BTreeKeyData(const KeyT &key, const DataT &data) noexcept
        : _key(key),
          _data(data)
    {}

    void setData(const DataT &data) { _data = data; }
    const DataT &getData() const { return _data; }

    /**
     * This operator only works when using direct keys.  References to
     * externally stored keys will not be properly sorted.
     */
    bool operator<(const BTreeKeyData &rhs) const {
        return _key < rhs._key;
    }
};


template <typename KeyT>
class BTreeKeyData<KeyT, BTreeNoLeafData>
{
public:
    using KeyType = KeyT;
    using DataType = BTreeNoLeafData;

    KeyT _key;

    BTreeKeyData() noexcept : _key() {}

    BTreeKeyData(const KeyT &key, const BTreeNoLeafData &) noexcept
        : _key(key)
    {
    }

    void setData(const BTreeNoLeafData &) { }
    const BTreeNoLeafData &getData() const { return BTreeNoLeafData::_instance; }

    /**
     * This operator only works when using direct keys.  References to
     * externally stored keys will not be properly sorted.
     */
    bool operator<(const BTreeKeyData &rhs) const {
        return _key < rhs._key;
    }
};

extern template class BTreeKeyData<uint32_t, uint32_t>;
extern template class BTreeKeyData<uint32_t, int32_t>;

} // namespace vespalib::btree