Skip to content

Commit 3531cf1

Browse files
xzhsehskyzh
andauthored
feat(b_plus_tree_printer): add file batch operations (#553)
* feat: support batch operations in b_plus_tree_printer * fix bugs in switch and add comment for the method * add space for comment * update comment for BatchOpsFromFile() * fix: check-format bugs --------- Co-authored-by: Alex Chi Z <[email protected]>
1 parent 32b059b commit 3531cf1

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/include/storage/index/b_plus_tree.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ class BPlusTree {
116116
// read data from file and remove one by one
117117
void RemoveFromFile(const std::string &file_name, Transaction *txn = nullptr);
118118

119+
/**
120+
* @brief Read batch operations from input file, below is a sample file format
121+
* insert some keys and delete 8, 9 from the tree with one step.
122+
* { i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i30 d8 d9 } // batch.txt
123+
* B+ Tree(4 max leaf, 4 max internal) after processing:
124+
* (5)
125+
* (3) (7)
126+
* (1,2) (3,4) (5,6) (7,10,30) // The output tree example
127+
*/
128+
void BatchOpsFromFile(const std::string &file_name, Transaction *txn = nullptr);
129+
119130
private:
120131
/* Debug Routines for FREE!! */
121132
void ToGraph(page_id_t page_id, const BPlusTreePage *page, std::ofstream &out);

src/storage/index/b_plus_tree.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,33 @@ void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *t
145145
}
146146
}
147147

148+
/*
149+
* This method is used for test only
150+
* Read data from file and insert/remove one by one
151+
*/
152+
INDEX_TEMPLATE_ARGUMENTS
153+
void BPLUSTREE_TYPE::BatchOpsFromFile(const std::string &file_name, Transaction *txn) {
154+
int64_t key;
155+
char instruction;
156+
std::ifstream input(file_name);
157+
while (input) {
158+
input >> instruction >> key;
159+
RID rid(key);
160+
KeyType index_key;
161+
index_key.SetFromInteger(key);
162+
switch (instruction) {
163+
case 'i':
164+
Insert(index_key, rid, txn);
165+
break;
166+
case 'd':
167+
Remove(index_key, txn);
168+
break;
169+
default:
170+
break;
171+
}
172+
}
173+
}
174+
148175
INDEX_TEMPLATE_ARGUMENTS
149176
void BPLUSTREE_TYPE::Print(BufferPoolManager *bpm) {
150177
auto root_page_id = GetRootPageId();

tools/b_plus_tree_printer/b_plus_tree_printer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ auto UsageMessage() -> std::string {
3535
"\ti <k> -- Insert <k> (int64_t) as both key and value).\n"
3636
"\tf <filename> -- insert multiple keys from reading file.\n"
3737
"\tc <filename> -- delete multiple keys from reading file.\n"
38+
"\tx <filename> -- insert or delete multiple keys from reading file.\n"
3839
"\td <k> -- Delete key <k> and its associated value.\n"
3940
"\tg <filename>.dot -- Output the tree in graph format to a dot file\n"
4041
"\tp -- Print the B+ tree.\n"
@@ -92,6 +93,10 @@ auto main(int argc, char **argv) -> int {
9293
std::cin >> filename;
9394
tree.RemoveFromFile(filename, transaction);
9495
break;
96+
case 'x':
97+
std::cin >> filename;
98+
tree.BatchOpsFromFile(filename, transaction);
99+
break;
95100
case 'd':
96101
std::cin >> key;
97102
index_key.SetFromInteger(key);

0 commit comments

Comments
 (0)