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

#pragma once

#include "btreetraits.h"
#include "btreenode.h"
#include "btreenodeallocator.h"
#include <atomic>

namespace vespalib::btree {

template <typename KeyT,
          typename DataT,
          typename AggrT,
          size_t INTERNAL_SLOTS,
          size_t LEAF_SLOTS>
class BTreeRootBase
{
protected:
    using KeyType = KeyT;
    using DataType = DataT;
    using AggregatedType = AggrT;
    using BTreeRootBaseType = BTreeRootBase<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>;
    using InternalNodeType = BTreeInternalNode<KeyT, AggrT, INTERNAL_SLOTS>;
    using LeafNodeType = BTreeLeafNode<KeyT, DataT, AggrT, LEAF_SLOTS>;
    using NodeAllocatorType = BTreeNodeAllocator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS>;

    BTreeNode::Ref  _root;
    std::atomic<uint32_t> _frozenRoot;

    static_assert(sizeof(_root) == sizeof(_frozenRoot),
                  "BTree root reference size mismatch");

    BTreeRootBase();
    BTreeRootBase(const BTreeRootBase &rhs);
    BTreeRootBase &operator=(const BTreeRootBase &rhs);
    ~BTreeRootBase();

public:
    void freeze(NodeAllocatorType &allocator);

    bool isFrozen() const {
        return (_root.ref() == _frozenRoot.load(std::memory_order_relaxed));
    }

    void setRoot(BTreeNode::Ref newRoot, NodeAllocatorType &allocator) {
        bool oldFrozen = isFrozen();
        _root = newRoot;
        if (oldFrozen && !isFrozen())
            allocator.needFreeze(this);
    }

    void prepare_hold() {
        // entry for _root is owned by new copy of BTreeRootBase.
        _root = BTreeNode::Ref();
    }

    void setRoots(BTreeNode::Ref newRoot) {
        _root = newRoot;
        _frozenRoot = newRoot.ref();
    }

    BTreeNode::Ref getRoot() const {
        return _root;
    }

    BTreeNode::Ref getFrozenRoot() const {
        return BTreeNode::Ref(_frozenRoot.load(std::memory_order_acquire));
    }

    BTreeNode::Ref getFrozenRootRelaxed() const {
        return BTreeNode::Ref(_frozenRoot.load(std::memory_order_relaxed));
    }

    const AggrT &getAggregated(const NodeAllocatorType &allocator) const {
        return allocator.getAggregated(_root);
    }

    void recycle() {
        _root = BTreeNode::Ref();
        _frozenRoot = BTreeNode::Ref().ref();
    }

protected:
    void recursiveDelete(BTreeNode::Ref node, NodeAllocatorType &allocator);
};

extern template class BTreeRootBase<uint32_t, uint32_t, NoAggregated,
                                    BTreeDefaultTraits::INTERNAL_SLOTS,
                                    BTreeDefaultTraits::LEAF_SLOTS>;
extern template class BTreeRootBase<uint32_t, BTreeNoLeafData, NoAggregated,
                                    BTreeDefaultTraits::INTERNAL_SLOTS,
                                    BTreeDefaultTraits::LEAF_SLOTS>;
extern template class BTreeRootBase<uint32_t, int32_t, MinMaxAggregated,
                                    BTreeDefaultTraits::INTERNAL_SLOTS,
                                    BTreeDefaultTraits::LEAF_SLOTS>;

}