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

#pragma once

#include "btreerootbase.h"
#include <cassert>

namespace vespalib::btree {

template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS>
BTreeRootBase<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>::BTreeRootBase()
    : _root(BTreeNode::Ref()),
      _frozenRoot(BTreeNode::Ref().ref())
{
}


template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS>
BTreeRootBase<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>::
BTreeRootBase(const BTreeRootBase &rhs)
    : _root(rhs._root),
      _frozenRoot(rhs._frozenRoot.load())
{
}


template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS>
BTreeRootBase<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>::~BTreeRootBase()
{
    assert(!_root.valid());
#if 0
    assert(!_frozenRoot.valid());
#endif
}


template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS>
BTreeRootBase<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS> &
BTreeRootBase<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>::
operator=(const BTreeRootBase &rhs)
{
    _root = rhs._root;
    _frozenRoot.store(rhs._frozenRoot.load(), std::memory_order_release);
    return *this;
}


template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS>
void
BTreeRootBase<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>::
freeze(NodeAllocatorType &allocator)
{
    if (NodeAllocatorType::isValidRef(_root)) {
        if (allocator.isLeafRef(_root))
            assert(allocator.mapLeafRef(_root)->getFrozen());
        else
            assert(allocator.mapInternalRef(_root)->getFrozen());
    }
    _frozenRoot.store(_root.ref(), std::memory_order_release);
}


template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS>
void
BTreeRootBase<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>::
recursiveDelete(BTreeNode::Ref node, NodeAllocatorType &allocator)
{
    assert(allocator.isValidRef(node));
    if (!allocator.isLeafRef(node)) {
        InternalNodeType * inode = allocator.mapInternalRef(node);
        for (size_t i = 0; i < inode->validSlots(); ++i) {
            recursiveDelete(inode->getChild(i), allocator);
        }
        allocator.holdNode(node, inode);
    } else {
        allocator.holdNode(node, allocator.mapLeafRef(node));
    }
}

}