aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/btree/btreeroot.h
blob: cd1d98725dc8e4b7233b576d2dc8cebf3ade188b (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "btreeiterator.h"
#include "btreenode.h"
#include "btreenodeallocator.h"
#include "btreerootbase.h"
#include "noaggrcalc.h"
#include "minmaxaggrcalc.h"

namespace vespalib::btree {

template <typename, typename, typename, size_t, size_t>
class BTreeNodeAllocator;
template <typename, typename, typename, size_t, size_t, class>
class BTreeBuilder;
template <typename, typename, typename, size_t, size_t, class>
class BTreeAggregator;

template <typename KeyT,
          typename DataT,
          typename AggrT = NoAggregated,
          typename CompareT = std::less<KeyT>,
          typename TraitsT = BTreeDefaultTraits>
class BTreeRootT : public BTreeRootBase<KeyT, DataT, AggrT,
                                       TraitsT::INTERNAL_SLOTS,
                                       TraitsT::LEAF_SLOTS>
{
public:
    using ParentType = BTreeRootBase<KeyT, DataT, AggrT,
                                     TraitsT::INTERNAL_SLOTS, TraitsT::LEAF_SLOTS>;
    using NodeAllocatorType = typename ParentType::NodeAllocatorType;
    using KeyDataType = BTreeKeyData<KeyT, DataT>;
    using InternalNodeType = typename ParentType::InternalNodeType;
    using LeafNodeType = typename ParentType::LeafNodeType;
    using LeafNodeTempType = BTreeLeafNodeTemp<KeyT, DataT, AggrT, TraitsT::LEAF_SLOTS>;
    using Iterator = BTreeIterator<KeyT, DataT, AggrT, CompareT, TraitsT>;
    using ConstIterator = BTreeConstIterator<KeyT, DataT, AggrT, CompareT, TraitsT>;
    using KeyType = typename ParentType::KeyType;
    using DataType = typename ParentType::DataType;
protected:
    using BTreeRootBaseType = typename ParentType::BTreeRootBaseType;
    using BTreeRootTType = BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>;
    using InternalNodeTypeRefPair = typename InternalNodeType::RefPair;
    using LeafNodeTypeRefPair = typename LeafNodeType::RefPair;
    using ParentType::_root;
    using ParentType::getFrozenRoot;
    using ParentType::getFrozenRootRelaxed;
    using ParentType::isFrozen;

    vespalib::string toString(BTreeNode::Ref node, const NodeAllocatorType &allocator) const;
public:
    /**
     * Read view of the frozen version of the tree.
     * Should be used by reader threads.
     **/
    class FrozenView {
    private:
        BTreeNode::Ref _frozenRoot;
        const NodeAllocatorType *const _allocator;
    public:
        using Iterator = ConstIterator;
        FrozenView() : _frozenRoot(BTreeNode::Ref()),_allocator(nullptr) {}
        FrozenView(BTreeNode::Ref frozenRoot, const NodeAllocatorType & allocator)
            : _frozenRoot(frozenRoot),
              _allocator(&allocator)
        {}
        ConstIterator find(const KeyType& key, CompareT comp = CompareT()) const;
        ConstIterator lowerBound(const KeyType &key, CompareT comp = CompareT()) const;
        ConstIterator upperBound(const KeyType &key, CompareT comp = CompareT()) const;
        ConstIterator begin() const {
            return ConstIterator(_frozenRoot, *_allocator);
        }
        void begin(std::vector<ConstIterator> &where) const {
            where.emplace_back(_frozenRoot, *_allocator);
        }

        BTreeNode::Ref getRoot() const { return _frozenRoot; }
        size_t size() const {
            if (NodeAllocatorType::isValidRef(_frozenRoot)) {
                return _allocator->validLeaves(_frozenRoot);
            }
            return 0u;
        }
        const NodeAllocatorType &getAllocator() const { return *_allocator; }

        const AggrT &getAggregated() const {
            return _allocator->getAggregated(_frozenRoot);
        }

        bool empty() const { return !_frozenRoot.valid(); }

        template <typename FunctionType>
        void foreach_key(FunctionType func) const {
            _allocator->getNodeStore().foreach_key(_frozenRoot, func);
        }

        template <typename FunctionType>
        void foreach(FunctionType func) const {
            _allocator->getNodeStore().foreach(_frozenRoot, func);
        }
    };

private:

    static Iterator findHelper(BTreeNode::Ref root, const KeyType & key,
                               const NodeAllocatorType & allocator, CompareT comp = CompareT());

    static Iterator lowerBoundHelper(BTreeNode::Ref root, const KeyType & key,
                                     const NodeAllocatorType & allocator, CompareT comp = CompareT());

    static Iterator upperBoundHelper(BTreeNode::Ref root, const KeyType & key,
                                     const NodeAllocatorType & allocator, CompareT comp = CompareT());

public:
    BTreeRootT();
    ~BTreeRootT();

    void clear(NodeAllocatorType &allocator);

    Iterator find(const KeyType & key, const NodeAllocatorType &allocator, CompareT comp = CompareT()) const;

    Iterator lowerBound(const KeyType & key, const NodeAllocatorType & allocator, CompareT comp = CompareT()) const;
    Iterator upperBound(const KeyType & key, const NodeAllocatorType & allocator, CompareT comp = CompareT()) const;

    Iterator begin(const NodeAllocatorType &allocator) const {
        return Iterator(_root, allocator);
    }

    FrozenView getFrozenView(const NodeAllocatorType & allocator) const {
        return FrozenView(getFrozenRoot(), allocator);
    }

    size_t size(const NodeAllocatorType &allocator) const;
    size_t frozenSize(const NodeAllocatorType &allocator) const;
    vespalib::string toString(const NodeAllocatorType &allocator) const;
    size_t bitSize(const NodeAllocatorType &allocator) const;
    size_t bitSize(BTreeNode::Ref node, const NodeAllocatorType &allocator) const;
    void thaw(Iterator &itr);
};


template <typename KeyT,
          typename DataT,
          typename AggrT = NoAggregated,
          typename CompareT = std::less<KeyT>,
          typename TraitsT = BTreeDefaultTraits,
          class AggrCalcT = NoAggrCalc>
class BTreeRoot : public BTreeRootT<KeyT, DataT, AggrT,
                                    CompareT, TraitsT>
{
public:
    using ParentType = BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>;
    using Parent2Type = typename ParentType::ParentType;
    using NodeAllocatorType = typename ParentType::NodeAllocatorType;
    using KeyType = typename ParentType::KeyType;
    using DataType = typename ParentType::DataType;
    using LeafNodeType = typename ParentType::LeafNodeType;
    using InternalNodeType = typename ParentType::InternalNodeType;
    using LeafNodeTypeRefPair = typename ParentType::LeafNodeTypeRefPair;
    using InternalNodeTypeRefPair = typename ParentType::InternalNodeTypeRefPair;
    using Iterator = typename ParentType::Iterator;
    using Builder = BTreeBuilder<KeyT, DataT, AggrT,
                                 TraitsT::INTERNAL_SLOTS, TraitsT::LEAF_SLOTS,
                                 AggrCalcT>;
    using Aggregator = BTreeAggregator<KeyT, DataT, AggrT,
                                       TraitsT::INTERNAL_SLOTS,
                                       TraitsT::LEAF_SLOTS,
                                       AggrCalcT>;
    using AggrCalcType = AggrCalcT;
    using Parent2Type::_root;
    using Parent2Type::getFrozenRoot;
    using Parent2Type::getFrozenRootRelaxed;
    using Parent2Type::isFrozen;

protected:
    bool isValid(BTreeNode::Ref node, bool ignoreMinSlots, uint32_t level,
                 const NodeAllocatorType &allocator, CompareT comp, AggrCalcT aggrCalc) const;

public:
    /**
     * Create a tree from a tree builder.  This is a destructive
     * assignment, old content of tree is destroyed and tree
     * builder is emptied when tree grabs ownership of nodes.
     */
    void
    assign(Builder &rhs, NodeAllocatorType &allocator);

    bool
    insert(const KeyType & key, const DataType & data,
           NodeAllocatorType &allocator, CompareT comp = CompareT(),
           const AggrCalcT &aggrCalc = AggrCalcT());

    void
    insert(Iterator &itr,
           const KeyType &key, const DataType &data,
           const AggrCalcT &aggrCalc = AggrCalcT());

    bool
    remove(const KeyType & key,
           NodeAllocatorType &allocator, CompareT comp = CompareT(),
           const AggrCalcT &aggrCalc = AggrCalcT());

    void
    remove(Iterator &itr,
           const AggrCalcT &aggrCalc = AggrCalcT());

    bool isValid(const NodeAllocatorType &allocator, CompareT comp = CompareT()) const;

    bool isValidFrozen(const NodeAllocatorType &allocator, CompareT comp = CompareT()) const;

    void move_nodes(NodeAllocatorType &allocator);
};



extern template class BTreeRootT<uint32_t, uint32_t, NoAggregated>;
extern template class BTreeRootT<uint32_t, BTreeNoLeafData, NoAggregated>;
extern template class BTreeRootT<uint32_t, int32_t, MinMaxAggregated>;
extern template class BTreeRoot<uint32_t, uint32_t, NoAggregated>;
extern template class BTreeRoot<uint32_t, BTreeNoLeafData, NoAggregated>;
extern template class BTreeRoot<uint32_t, int32_t, MinMaxAggregated,
                                std::less<uint32_t>, BTreeDefaultTraits, MinMaxAggrCalc>;

}