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

#pragma once

#include "btreeaggregator.h"

namespace vespalib::btree {

template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS, class AggrCalcT>
AggrT
BTreeAggregator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS, AggrCalcT>::aggregate(const LeafNodeType &node, AggrCalcT aggrCalc)
{
    AggrT a;
    for (uint32_t i = 0, ie = node.validSlots(); i < ie; ++i) {
        if constexpr (AggrCalcT::aggregate_over_values()) {
            aggrCalc.add(a, aggrCalc.getVal(node.getData(i)));
        } else {
            aggrCalc.add(a, aggrCalc.getVal(node.getKey(i)));
        }
    }
    return a;
}

template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS, class AggrCalcT>
AggrT
BTreeAggregator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS, AggrCalcT>::aggregate(const InternalNodeType &node, const NodeAllocatorType &allocator, AggrCalcT aggrCalc)
{
    AggrT a;
    for (uint32_t i = 0, ie = node.validSlots(); i < ie; ++i) {
        const BTreeNode::Ref childRef = node.getChild(i);
        const AggrT &ca(allocator.getAggregated(childRef));
        aggrCalc.add(a, ca);
    }
    return a;
}

template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS, class AggrCalcT>
void
BTreeAggregator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS, AggrCalcT>::
recalc(LeafNodeType &node, const AggrCalcT &aggrCalc)
{
    node.getAggregated() = aggregate(node, aggrCalc);
}

template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS, class AggrCalcT>
void
BTreeAggregator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS, AggrCalcT>::
recalc(InternalNodeType &node,
       const NodeAllocatorType &allocator,
       const AggrCalcT &aggrCalc)
{
    node.getAggregated() = aggregate(node, allocator, aggrCalc);
}


template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS, class AggrCalcT>
typename BTreeAggregator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS,
                         AggrCalcT>::AggregatedType
BTreeAggregator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS, AggrCalcT>::
recalc(LeafNodeType &node,
       LeafNodeType &splitNode,
       const AggrCalcT &aggrCalc)
{
    AggrT a;
    recalc(node, aggrCalc);
    recalc(splitNode, aggrCalc);
    a = node.getAggregated();
    aggrCalc.add(a, splitNode.getAggregated());
    return a;
}
 
          
template <typename KeyT, typename DataT, typename AggrT,
          size_t INTERNAL_SLOTS, size_t LEAF_SLOTS, class AggrCalcT>
typename BTreeAggregator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS,
                         AggrCalcT>::AggregatedType
BTreeAggregator<KeyT, DataT, AggrT, INTERNAL_SLOTS, LEAF_SLOTS, AggrCalcT>::
    recalc(InternalNodeType &node,
           InternalNodeType &splitNode,
           const NodeAllocatorType &allocator,
           const AggrCalcT &aggrCalc)
{
    AggrT a;
    recalc(node, allocator, aggrCalc);
    recalc(splitNode, allocator, aggrCalc);
    a = node.getAggregated();
    aggrCalc.add(a, splitNode.getAggregated());
    return a;
}

}