aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/btree/btreeroot.hpp
blob: 5acd83e944b2f9cd82cb21938f4b95ba851905da (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "btreeroot.h"
#include "btreebuilder.h"
#include "btreerootbase.hpp"
#include "btreeinserter.hpp"
#include "btreeremover.hpp"
#include "btreeaggregator.hpp"
#include <vespa/vespalib/stllike/asciistream.h>

namespace vespalib::btree {

//----------------------- BTreeRoot ------------------------------------------//

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
vespalib::string
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
toString(BTreeNode::Ref node,
         const NodeAllocatorType &allocator) const
{
    if (allocator.isLeafRef(node)) {
        vespalib::asciistream ss;
        ss << "{" << allocator.toString(node) << "}";
        return ss.str();
    } else {
        const InternalNodeType * inode = allocator.mapInternalRef(node);
        vespalib::asciistream ss;
        ss << "{" << allocator.toString(inode) << ",children(" << inode->validSlots() << ")[";
        for (size_t i = 0; i < inode->validSlots(); ++i) {
            if (i > 0) ss << ",";
            ss << "c[" << i << "]" << toString(inode->getChild(i), allocator);
        }
        ss << "]}";
        return ss.str();
    }
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
bool
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
isValid(BTreeNode::Ref node,
        bool ignoreMinSlots, uint32_t level, const NodeAllocatorType &allocator,
        CompareT comp, AggrCalcT aggrCalc) const
{
    if (allocator.isLeafRef(node)) {
        if (level != 0) {
            return false;
        }
        const LeafNodeType * lnode = allocator.mapLeafRef(node);
        if (level != lnode->getLevel()) {
            return false;
        }
        if (lnode->validSlots() > LeafNodeType::maxSlots())
            return false;
        if (lnode->validSlots() < LeafNodeType::minSlots() && !ignoreMinSlots)
            return false;
        for (size_t i = 1; i < lnode->validSlots(); ++i) {
            if (!comp(lnode->getKey(i - 1), lnode->getKey(i))) {
                return false;
            }
        }
        if constexpr (AggrCalcT::hasAggregated()) {
            AggrT aggregated = Aggregator::aggregate(*lnode, aggrCalc);
            if (aggregated != lnode->getAggregated()) {
                return false;
            }
        }
    } else {
        if (level == 0) {
            return false;
        }
        const InternalNodeType * inode = allocator.mapInternalRef(node);
        if (level != inode->getLevel()) {
            return false;
        }
        if (inode->validSlots() > InternalNodeType::maxSlots())
            return false;
        if (inode->validSlots() < InternalNodeType::minSlots() &&
            !ignoreMinSlots)
            return false;
        size_t lChildren = 0;
        size_t iChildren = 0;
        uint32_t validLeaves = 0;
        for (size_t i = 0; i < inode->validSlots(); ++i) {
            if (i > 0 && !comp(inode->getKey(i - 1), inode->getKey(i))) {
                return false;
            }
            const BTreeNode::Ref childRef = inode->getChild(i);
            if (!allocator.isValidRef(childRef))
                return false;
            validLeaves += allocator.validLeaves(childRef);
            if (allocator.isLeafRef(childRef))
                lChildren++;
            else
                iChildren++;
            if (comp(inode->getKey(i), allocator.getLastKey(childRef))) {
                return false;
            }
            if (comp(allocator.getLastKey(childRef), inode->getKey(i))) {
                return false;
            }
            if (!isValid(childRef, false, level - 1, allocator, comp, aggrCalc)) {
                return false;
            }
        }
        if (validLeaves != inode->validLeaves()) {
            return false;
        }
        if (lChildren < inode->validSlots() && iChildren < inode->validSlots()) {
            return false;
        }
        if constexpr (AggrCalcT::hasAggregated()) {
            AggrT aggregated = Aggregator::aggregate(*inode, allocator, aggrCalc);
            if (aggregated != inode->getAggregated()) {
                return false;
            }
        }
    }
    return true;
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::Iterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
findHelper(BTreeNode::Ref root, const KeyType & key,
           const NodeAllocatorType & allocator, CompareT comp)
{
    Iterator itr(BTreeNode::Ref(), allocator);
    itr.lower_bound(root, key, comp);
    if (itr.valid() && comp(key, itr.getKey())) {
        itr.setupEnd();
    }
    return itr;
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::Iterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
lowerBoundHelper(BTreeNode::Ref root, const KeyType & key, const NodeAllocatorType & allocator, CompareT comp)
{
    Iterator itr(BTreeNode::Ref(), allocator);
    itr.lower_bound(root, key, comp);
    return itr;
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::Iterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
upperBoundHelper(BTreeNode::Ref root, const KeyType & key,
                 const NodeAllocatorType & allocator, CompareT comp)
{
    Iterator itr(root, allocator);
    if (itr.valid() && !comp(key, itr.getKey())) {
        itr.seekPast(key, comp);
    }
    return itr;
}


//----------------------- BTreeRoot::FrozenView ----------------------------------//

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::ConstIterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
FrozenView::find(const KeyType & key, CompareT comp) const
{
    ConstIterator itr(BTreeNode::Ref(), *_allocator);
    itr.lower_bound(_frozenRoot, key, comp);
    if (itr.valid() && comp(key, itr.getKey())) {
        itr.setupEnd();
    }
    return itr;
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::ConstIterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
FrozenView::lowerBound(const KeyType & key, CompareT comp) const
{
    ConstIterator itr(BTreeNode::Ref(), *_allocator);
    itr.lower_bound(_frozenRoot, key, comp);
    return itr;
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::ConstIterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
FrozenView::upperBound(const KeyType & key, CompareT comp) const
{
    ConstIterator itr(_frozenRoot, *_allocator);
    if (itr.valid() && !comp(key, itr.getKey())) {
        itr.seekPast(key, comp);
    }
    return itr;
}

//----------------------- BTreeRoot ----------------------------------------------//

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::BTreeRootT() = default;

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::~BTreeRootT() = default;

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
void
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
clear(NodeAllocatorType &allocator)
{
    if (NodeAllocatorType::isValidRef(_root)) {
        this->recursiveDelete(_root, allocator);
        _root = BTreeNode::Ref();
        if (NodeAllocatorType::isValidRef(getFrozenRootRelaxed()))
            allocator.needFreeze(this);
    }
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::Iterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
find(const KeyType & key, const NodeAllocatorType & allocator, CompareT comp) const
{
    return findHelper(_root, key, allocator, comp);
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::Iterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
lowerBound(const KeyType & key, const NodeAllocatorType & allocator, CompareT comp) const
{
    return lowerBoundHelper(_root, key, allocator, comp);
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
typename BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::Iterator
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
upperBound(const KeyType & key, const NodeAllocatorType & allocator, CompareT comp) const
{
    return upperBoundHelper(_root, key, allocator, comp);
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
size_t
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
size(const NodeAllocatorType &allocator) const
{
    if (NodeAllocatorType::isValidRef(_root)) {
        return allocator.validLeaves(_root);
    }
    return 0u;
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
size_t
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
frozenSize(const NodeAllocatorType &allocator) const
{
    BTreeNode::Ref frozenRoot = getFrozenRoot();
    if (NodeAllocatorType::isValidRef(frozenRoot)) {
        return allocator.validLeaves(frozenRoot);
    }
    return 0u;
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
vespalib::string
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
toString(const NodeAllocatorType &allocator) const
{
    vespalib::asciistream ss;
    if (NodeAllocatorType::isValidRef(_root)) {
        ss << "root(" << toString(_root, allocator) << ")";
    }
    return ss.str();
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
bool
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
isValid(const NodeAllocatorType &allocator,
        CompareT comp) const
{
    if (NodeAllocatorType::isValidRef(_root)) {
        uint32_t level  = allocator.getLevel(_root);
        return isValid(_root, true, level, allocator, comp, AggrCalcT());
    }
    return true;
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
bool
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
isValidFrozen(const NodeAllocatorType &allocator, CompareT comp) const
{
    BTreeNode::Ref frozenRoot = getFrozenRoot();
    if (NodeAllocatorType::isValidRef(frozenRoot)) {
        uint32_t level  = allocator.getLevel(frozenRoot);
        return isValid(frozenRoot, true, level, allocator, comp, AggrCalcT());
    }
    return true;
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT>
size_t
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
bitSize(const NodeAllocatorType &allocator) const
{
    size_t ret = sizeof(BTreeRootT) * 8;
    if (NodeAllocatorType::isValidRef(_root))
        ret += bitSize(_root, allocator);
    return ret;
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT>
size_t
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
bitSize(BTreeNode::Ref node, const NodeAllocatorType &allocator) const
{
    if (allocator.isLeafRef(node)) {
        return sizeof(LeafNodeType) * 8;
    } else {
        size_t ret = sizeof(InternalNodeType) * 8;
        const InternalNodeType * inode = allocator.mapInternalRef(node);
        size_t slots = inode->validSlots();
        for (size_t i = 0; i < slots; ++i) {
            ret += bitSize(inode->getChild(i), allocator);
        }
        return ret;
    }
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT, typename TraitsT>
void
BTreeRootT<KeyT, DataT, AggrT, CompareT, TraitsT>::
thaw(Iterator &itr)
{
    bool oldFrozen = isFrozen();
    _root = itr.thaw(_root);
    if (oldFrozen && !isFrozen())
        itr.getAllocator().needFreeze(this);
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
void
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
assign(Builder &rhs,
       NodeAllocatorType &allocator)
{
    this->clear(allocator);

    bool oldFrozen = isFrozen();
    _root = rhs.handover();
    if (oldFrozen && !isFrozen())
        allocator.needFreeze(this);
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
bool
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
insert(const KeyType & key, const DataType & data,
       NodeAllocatorType &allocator, CompareT comp,
       const AggrCalcT &aggrCalc)
{
    Iterator itr(BTreeNode::Ref(), allocator);
    itr.lower_bound(_root, key, comp);
    if (itr.valid() && !comp(key, itr.getKey()))
        return false; // Element already exists
    insert(itr, key, data, aggrCalc);
    return true;
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
void
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
insert(Iterator &itr,
       const KeyType &key, const DataType &data,
       const AggrCalcT &aggrCalc)
{
    using Inserter = BTreeInserter<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>;
    bool oldFrozen = isFrozen();
    Inserter::insert(_root, itr, key, data,aggrCalc);
    if (oldFrozen && !isFrozen())
        itr.getAllocator().needFreeze(this);
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
bool
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
remove(const KeyType & key,
       NodeAllocatorType &allocator, CompareT comp,
       const AggrCalcT &aggrCalc)
{
    Iterator itr(BTreeNode::Ref(), allocator);
    itr.lower_bound(_root, key, comp);
    if (!itr.valid() || comp(key, itr.getKey()))
        return false;
    remove(itr, aggrCalc);
    return true;
}


template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
void
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
remove(Iterator &itr, const AggrCalcT &aggrCalc)
{
    using Remover = BTreeRemover<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>;
    bool oldFrozen = isFrozen();
    Remover::remove(_root, itr, aggrCalc);
    if (oldFrozen && !isFrozen())
        itr.getAllocator().needFreeze(this);
}

template <typename KeyT, typename DataT, typename AggrT, typename CompareT,
          typename TraitsT, class AggrCalcT>
void
BTreeRoot<KeyT, DataT, AggrT, CompareT, TraitsT, AggrCalcT>::
move_nodes(NodeAllocatorType &allocator)
{
    Iterator itr = this->begin(allocator);
    this->setRoot(itr.moveFirstLeafNode(this->getRoot()), allocator);
    while (itr.valid()) {
        itr.moveNextLeafNode();
    }
}

}