-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathb_plus_tree_iterator.cpp
147 lines (136 loc) · 4.32 KB
/
b_plus_tree_iterator.cpp
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
/**
* Copyright 2023 KUMAZAKI Hiroki
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "b_plus_tree_iterator.hpp"
#include <stdexcept>
#include <string_view>
#include "index/b_plus_tree.hpp"
#include "page/index_key.hpp"
#include "page/page_manager.hpp"
#include "page/page_ref.hpp"
#include "transaction/transaction.hpp"
namespace tinylamb {
BPlusTreeIterator::BPlusTreeIterator(BPlusTree* tree, Transaction* txn,
std::string_view begin,
std::string_view end, bool ascending)
: tree_(tree), txn_(txn), begin_(begin), end_(end) {
if (!end.empty() && !begin.empty() && end < begin) {
std::runtime_error("invalid begin & end");
}
if (ascending) {
if (begin.empty()) {
PageRef leaf = tree->LeftmostPage(*txn_);
pid_ = leaf->PageID();
idx_ = 0;
valid_ = true;
} else {
PageRef leaf = tree_->FindLeaf(*txn_, begin, false);
pid_ = leaf->PageID();
idx_ = leaf->body.leaf_page.Find(begin);
valid_ = 0 <= idx_ && idx_ < leaf->body.leaf_page.row_count_;
}
} else {
if (end.empty()) {
PageRef leaf = tree->RightmostPage(*txn_);
pid_ = leaf->PageID();
idx_ = leaf->body.leaf_page.row_count_ - 1;
valid_ = true;
} else {
PageRef leaf = tree_->FindLeaf(*txn_, end, false);
pid_ = leaf->PageID();
idx_ = leaf->body.leaf_page.Find(end);
valid_ = 0 <= idx_ && idx_ < leaf->body.leaf_page.row_count_;
}
}
}
std::string BPlusTreeIterator::Key() const {
return std::string(
txn_->PageManager()->GetPage(pid_)->body.leaf_page.GetKey(idx_));
}
std::string BPlusTreeIterator::Value() const {
return std::string(
txn_->PageManager()->GetPage(pid_)->body.leaf_page.GetValue(idx_));
}
BPlusTreeIterator& BPlusTreeIterator::operator++() {
PageRef ref = txn_->PageManager()->GetPage(pid_);
LeafPage* const lp = &ref->body.leaf_page;
idx_++;
if (lp->row_count_ <= idx_) {
if (pid_ == 0) {
valid_ = false;
return *this;
}
if (auto foster = ref->GetFoster(*txn_)) {
const FosterPair& foster_pair = foster.Value();
pid_ = foster_pair.child_pid;
PageRef next_ref = txn_->PageManager()->GetPage(pid_);
ref.PageUnlock();
idx_ = 0;
if (next_ref->body.leaf_page.row_count_ == 0 ||
(!end_.empty() && end_ < next_ref->body.leaf_page.GetKey(idx_))) {
valid_ = false;
}
return *this;
}
IndexKey high_fence = ref->GetHighFence(*txn_);
if (high_fence.IsPlusInfinity()) {
valid_ = false;
return *this;
}
PageRef next_ref =
tree_->FindLeaf(*txn_, high_fence.GetKey().Value(), false);
ref.PageUnlock();
idx_ = 0;
pid_ = next_ref->PageID();
if (next_ref->body.leaf_page.row_count_ == 0 ||
(!end_.empty() && end_ < next_ref->body.leaf_page.GetKey(idx_))) {
valid_ = false;
}
return *this;
}
if (!end_.empty() && end_ < lp->GetKey(idx_)) {
valid_ = false;
}
return *this;
}
BPlusTreeIterator& BPlusTreeIterator::operator--() {
PageRef ref = txn_->PageManager()->GetPage(pid_);
LeafPage* const lp = &ref->body.leaf_page;
if (0 == idx_) {
if (pid_ == 0) {
valid_ = false;
}
IndexKey low_fence = ref->GetLowFence(*txn_);
if (low_fence.IsMinusInfinity()) {
valid_ = false;
return *this;
}
PageRef prev_ref = tree_->FindLeaf(*txn_, low_fence.GetKey().Value(), true);
idx_ = prev_ref->body.leaf_page.row_count_ - 1;
pid_ = prev_ref->PageID();
if (prev_ref->body.leaf_page.row_count_ == 0 ||
(!begin_.empty() && prev_ref->body.leaf_page.GetKey(idx_) < begin_)) {
valid_ = false;
}
ref.PageUnlock();
return *this;
}
--idx_;
if (!begin_.empty() && lp->GetKey(idx_) < begin_) {
valid_ = false;
}
return *this;
}
} // namespace tinylamb