|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "paimon/core/mergetree/compact/loser_tree.h" |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <cassert> |
| 23 | + |
| 24 | +namespace paimon { |
| 25 | +LoserTree::LoserTree(std::vector<std::unique_ptr<KeyValueRecordReader>>&& readers, |
| 26 | + const CompareFunc& first_comparator, const CompareFunc& second_comparator) |
| 27 | + : size_(readers.size()), |
| 28 | + initialized_(false), |
| 29 | + readers_holder_(std::move(readers)), |
| 30 | + tree_(size_), |
| 31 | + first_comparator_(first_comparator), |
| 32 | + second_comparator_(second_comparator) { |
| 33 | + leaves_.reserve(size_); |
| 34 | + for (const auto& reader : readers_holder_) { |
| 35 | + leaves_.emplace_back(reader.get()); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +Status LoserTree::InitializeIfNeeded() { |
| 40 | + if (!initialized_) { |
| 41 | + std::fill(tree_.begin(), tree_.end(), -1); |
| 42 | + for (int32_t i = size_ - 1; i >= 0; i--) { |
| 43 | + PAIMON_RETURN_NOT_OK(leaves_[i].AdvanceIfAvailable()); |
| 44 | + Adjust(i); |
| 45 | + } |
| 46 | + initialized_ = true; |
| 47 | + } |
| 48 | + return Status::OK(); |
| 49 | +} |
| 50 | + |
| 51 | +Status LoserTree::AdjustForNextLoop() { |
| 52 | + LeafIterator* winner = &leaves_[tree_[0]]; |
| 53 | + while (winner->state == State::WINNER_POPPED) { |
| 54 | + PAIMON_RETURN_NOT_OK(winner->AdvanceIfAvailable()); |
| 55 | + Adjust(tree_[0]); |
| 56 | + winner = &leaves_[tree_[0]]; |
| 57 | + } |
| 58 | + return Status::OK(); |
| 59 | +} |
| 60 | + |
| 61 | +std::optional<KeyValue> LoserTree::PopWinner() { |
| 62 | + LeafIterator* winner = &leaves_[tree_[0]]; |
| 63 | + if (winner->state == State::WINNER_POPPED) { |
| 64 | + // if the winner has already been popped, it means that all the same key has been |
| 65 | + // processed. |
| 66 | + return std::nullopt; |
| 67 | + } |
| 68 | + std::optional<KeyValue> result = std::move(winner->Pop()); |
| 69 | + Adjust(tree_[0]); |
| 70 | + return result; |
| 71 | +} |
| 72 | + |
| 73 | +const std::optional<KeyValue>& LoserTree::PeekWinner() const { |
| 74 | + static const std::optional<KeyValue> empty_kv = std::nullopt; |
| 75 | + return leaves_[tree_[0]].state != State::WINNER_POPPED ? leaves_[tree_[0]].Peek() : empty_kv; |
| 76 | +} |
| 77 | + |
| 78 | +void LoserTree::Adjust(int32_t winner) { |
| 79 | + for (int32_t parent = (winner + size_) / 2; parent > 0 && winner >= 0; parent /= 2) { |
| 80 | + LeafIterator* winner_node = &leaves_[winner]; |
| 81 | + LeafIterator* parent_node = nullptr; |
| 82 | + |
| 83 | + if (tree_[parent] == -1) { |
| 84 | + // initialize the tree. |
| 85 | + winner_node->state = State::LOSER_WITH_NEW_KEY; |
| 86 | + } else { |
| 87 | + parent_node = &leaves_[tree_[parent]]; |
| 88 | + switch (winner_node->state) { |
| 89 | + case State::WINNER_WITH_NEW_KEY: { |
| 90 | + AdjustWithNewWinnerKey(parent, parent_node, winner_node); |
| 91 | + break; |
| 92 | + } |
| 93 | + case State::WINNER_WITH_SAME_KEY: { |
| 94 | + AdjustWithSameWinnerKey(parent, parent_node, winner_node); |
| 95 | + break; |
| 96 | + } |
| 97 | + case State::WINNER_POPPED: { |
| 98 | + if (winner_node->first_same_key_index < 0) { |
| 99 | + // fast path, which means that the same key is not yet processed in the |
| 100 | + // current tree. |
| 101 | + parent = -1; |
| 102 | + } else { |
| 103 | + // fast path. Directly exchange positions with the same key that has not |
| 104 | + // yet been processed, no need to compare level by level. |
| 105 | + parent = winner_node->first_same_key_index; |
| 106 | + parent_node = &leaves_[tree_[parent]]; |
| 107 | + winner_node->state = State::LOSER_POPPED; |
| 108 | + parent_node->state = State::WINNER_WITH_SAME_KEY; |
| 109 | + } |
| 110 | + break; |
| 111 | + } |
| 112 | + default: |
| 113 | + assert(false); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // if the winner loses, exchange nodes. |
| 118 | + if (!IsWinner(winner_node->state)) { |
| 119 | + std::swap(winner, tree_[parent]); |
| 120 | + } |
| 121 | + } |
| 122 | + tree_[0] = winner; |
| 123 | +} |
| 124 | + |
| 125 | +void LoserTree::AdjustWithSameWinnerKey(int32_t index, LeafIterator* parent_node, |
| 126 | + LeafIterator* winner_node) { |
| 127 | + switch (parent_node->state) { |
| 128 | + case State::LOSER_WITH_SAME_KEY: { |
| 129 | + // the key of the previous loser is the same as the key of the current winner, |
| 130 | + // only the sequence needs to be compared. |
| 131 | + const auto& parent_key = parent_node->Peek(); |
| 132 | + const auto& child_key = winner_node->Peek(); |
| 133 | + int32_t second_result = second_comparator_(parent_key, child_key); |
| 134 | + if (second_result > 0) { |
| 135 | + parent_node->state = State::WINNER_WITH_SAME_KEY; |
| 136 | + winner_node->state = State::LOSER_WITH_SAME_KEY; |
| 137 | + parent_node->SetFirstSameKeyIndex(index); |
| 138 | + } else { |
| 139 | + winner_node->SetFirstSameKeyIndex(index); |
| 140 | + } |
| 141 | + return; |
| 142 | + } |
| 143 | + case State::LOSER_WITH_NEW_KEY: |
| 144 | + case State::LOSER_POPPED: |
| 145 | + return; |
| 146 | + default: |
| 147 | + assert(false); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +void LoserTree::AdjustWithNewWinnerKey(int32_t index, LeafIterator* parent_node, |
| 152 | + LeafIterator* winner_node) { |
| 153 | + switch (parent_node->state) { |
| 154 | + case State::LOSER_WITH_NEW_KEY: { |
| 155 | + // when the new winner is also a new key, it needs to be compared. |
| 156 | + const auto& parent_key = parent_node->Peek(); |
| 157 | + const auto& child_key = winner_node->Peek(); |
| 158 | + int32_t first_result = first_comparator_(parent_key, child_key); |
| 159 | + if (first_result == 0) { |
| 160 | + // if the compared keys are the same, we need to update the state of the node |
| 161 | + // and record the index of the same key for the winner. |
| 162 | + int32_t second_result = second_comparator_(parent_key, child_key); |
| 163 | + if (second_result < 0) { |
| 164 | + parent_node->state = State::LOSER_WITH_SAME_KEY; |
| 165 | + winner_node->SetFirstSameKeyIndex(index); |
| 166 | + } else { |
| 167 | + winner_node->state = State::LOSER_WITH_SAME_KEY; |
| 168 | + parent_node->state = State::WINNER_WITH_NEW_KEY; |
| 169 | + parent_node->SetFirstSameKeyIndex(index); |
| 170 | + } |
| 171 | + } else if (first_result > 0) { |
| 172 | + // the two keys are completely different and just need to update the state. |
| 173 | + parent_node->state = State::WINNER_WITH_NEW_KEY; |
| 174 | + winner_node->state = State::LOSER_WITH_NEW_KEY; |
| 175 | + } |
| 176 | + return; |
| 177 | + } |
| 178 | + case State::LOSER_WITH_SAME_KEY: { |
| 179 | + // A node in the WINNER_WITH_NEW_KEY state cannot encounter a node in the |
| 180 | + // LOSER_WITH_SAME_KEY state. |
| 181 | + assert(false); |
| 182 | + break; |
| 183 | + } |
| 184 | + case State::LOSER_POPPED: { |
| 185 | + // this case will only happen during adjustForNextLoop. |
| 186 | + parent_node->state = State::WINNER_POPPED; |
| 187 | + parent_node->first_same_key_index = -1; |
| 188 | + winner_node->state = State::LOSER_WITH_NEW_KEY; |
| 189 | + return; |
| 190 | + } |
| 191 | + default: |
| 192 | + assert(false); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +} // namespace paimon |
0 commit comments