├── .gitignore ├── .travis.yml ├── BTree.hpp ├── CMakeLists.txt ├── LICENSE ├── README.md ├── generate.sh └── src ├── BTree.hpp ├── BTree_Index_test.cpp ├── BTree_Iterator_test.cpp ├── BTree_Leaf_test.cpp ├── BTree_Storage_test.cpp ├── BTree_test.cpp ├── Container.hpp ├── Container_test.cpp ├── Iterator.hpp ├── LRU.hpp ├── LRU_test.cpp ├── Persistence.hpp ├── Persistence_test.cpp ├── bench1.hpp ├── bench2.hpp ├── benchmark.hpp ├── main.cpp ├── test_main.cpp └── utility.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeCache.txt 2 | CMakeFiles 3 | CMakeScripts 4 | Testing 5 | Makefile 6 | cmake_install.cmake 7 | install_manifest.txt 8 | compile_commands.json 9 | CTestTestfile.cmake 10 | 11 | cmake-build-*/ 12 | Debug/ 13 | Release/ 14 | Bin/ 15 | 16 | .idea/ 17 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | dist: xenial 3 | compiler: 4 | - gcc 5 | cache: 6 | directories: 7 | - /home/travis/vcpkg/ 8 | before_script: 9 | - export CC=gcc-9 10 | - export CXX=g++-9 11 | - pushd . && cd ~ 12 | - if [ ! -f "vcpkg/bootstrap-vcpkg.sh" ] ; then git clone https://github.com/Microsoft/vcpkg.git ; fi 13 | - cd vcpkg && ./bootstrap-vcpkg.sh && ./vcpkg install catch2 14 | script: 15 | - popd 16 | - cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake 17 | - cd build && cmake --build . 18 | - ./BPlusTreeTest 19 | after_success: 20 | - if [ "$BUILD_TYPE" == 'RELEASE' ] ; then ./BPlusTree; fi 21 | env: 22 | - BUILD_TYPE=DEBUG 23 | - BUILD_TYPE=RELEASE 24 | addons: 25 | apt: 26 | sources: 27 | - ubuntu-toolchain-r-test 28 | packages: 29 | - g++-9 -------------------------------------------------------------------------------- /BTree.hpp: -------------------------------------------------------------------------------- 1 | 2 | // THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT 3 | 4 | #include "utility.hpp" 5 | #include 6 | #include 7 | #include "exception.hpp" 8 | #include 9 | #include 10 | 11 | #define ONLINE_JUDGE 12 | 13 | namespace sjtu { 14 | // 15 | // Created by Alex Chi on 2019-05-30. 16 | // 17 | 18 | #ifndef BPLUSTREE_LRU_HPP 19 | #define BPLUSTREE_LRU_HPP 20 | 21 | #include 22 | 23 | template 24 | class LRU { 25 | struct Node { 26 | Idx idx; 27 | Node *prev, *next; 28 | 29 | Node(Idx idx) : idx(idx), prev(nullptr), next(nullptr) {} 30 | } *head, *tail; 31 | 32 | void push(Node* ptr) { 33 | if (size == 0) { head = tail = ptr; } 34 | else { 35 | ptr->next = head; 36 | head->prev = ptr; 37 | head = ptr; 38 | } 39 | ++size; 40 | } 41 | 42 | void remove(Node* ptr) { 43 | if (ptr->prev) ptr->prev->next = ptr->next; else { 44 | head = ptr->next; 45 | } 46 | if (ptr->next) ptr->next->prev = ptr->prev; else { 47 | tail = ptr->prev; 48 | } 49 | ptr->next = ptr->prev = nullptr; 50 | --size; 51 | } 52 | 53 | public: 54 | unsigned size; 55 | 56 | Node** nodes; 57 | 58 | LRU() : head(nullptr), tail(nullptr), size(0) { 59 | nodes = new Node*[Cap]; 60 | memset(nodes, 0, sizeof(Node*) * Cap); 61 | } 62 | 63 | ~LRU() { 64 | delete[] nodes; 65 | } 66 | 67 | void put(Idx idx) { 68 | assert(idx < Cap); 69 | assert(nodes[idx] == nullptr); 70 | Node *ptr = new Node(idx); 71 | push(ptr); 72 | nodes[idx] = ptr; 73 | } 74 | 75 | void get(Idx idx) { 76 | assert(idx < Cap); 77 | assert(nodes[idx]); 78 | remove(nodes[idx]); 79 | push(nodes[idx]); 80 | } 81 | 82 | Idx expire() { 83 | return tail->idx; 84 | } 85 | 86 | void remove(Idx idx) { 87 | assert(idx < Cap); 88 | assert(nodes[idx]); 89 | remove(nodes[idx]); 90 | delete nodes[idx]; 91 | nodes[idx] = nullptr; 92 | } 93 | 94 | void debug() { 95 | unsigned last_idx; 96 | for (auto ptr = head; ptr != nullptr; ptr = ptr->next) { 97 | if (ptr->idx == last_idx) { 98 | std::clog << "Cycle detected!" << std::endl; 99 | return; 100 | } 101 | std::clog << ptr->idx << " "; 102 | last_idx = ptr->idx; 103 | } 104 | std::clog << std::endl; 105 | } 106 | }; 107 | 108 | #endif //BPLUSTREE_LRU_HPP 109 | // 110 | // Created by Alex Chi on 2019-05-29. 111 | // 112 | 113 | #ifndef BPLUSTREE_PERSISTENCE_HPP 114 | #define BPLUSTREE_PERSISTENCE_HPP 115 | 116 | #include 117 | #include 118 | #include 119 | #ifndef ONLINE_JUDGE 120 | #include "LRU.hpp" 121 | #endif 122 | 123 | class Serializable { 124 | public: 125 | virtual unsigned storage_size() const = 0; 126 | 127 | virtual void serialize(std::ostream &out) const = 0; 128 | 129 | virtual void deserialize(std::istream &in) = 0; 130 | 131 | static constexpr bool is_serializable() { return true; } 132 | }; 133 | 134 | template 135 | struct Persistence { 136 | const char *path; 137 | std::fstream f; 138 | 139 | static const unsigned VERSION = 6; 140 | 141 | struct PersistenceIndex { 142 | unsigned root_idx; 143 | unsigned magic_key; 144 | unsigned version; 145 | unsigned size; 146 | size_t tail_pos; 147 | size_t page_offset[MAX_PAGES]; 148 | size_t page_size[MAX_PAGES]; 149 | unsigned char is_leaf[MAX_PAGES]; 150 | 151 | static unsigned constexpr MAGIC_KEY() { 152 | return sizeof(Index) * 233 153 | + sizeof(Leaf) * 23333 154 | + MAX_PAGES * 23; 155 | } 156 | 157 | PersistenceIndex() : root_idx(0), magic_key(MAGIC_KEY()), 158 | version(VERSION), 159 | tail_pos(sizeof(PersistenceIndex)), 160 | size(0) { 161 | memset(page_offset, 0, sizeof(page_offset)); 162 | memset(is_leaf, 0, sizeof(is_leaf)); 163 | } 164 | } *persistence_index; 165 | 166 | Block **pages; 167 | bool *dirty; 168 | unsigned lst_empty_slot; 169 | 170 | struct Stat { 171 | long long create; 172 | long long destroy; 173 | long long access_cache_hit; 174 | long long access_cache_miss; 175 | long long dirty_write; 176 | long long swap_out; 177 | 178 | void stat() { 179 | printf(" access hit/total %lld/%lld %.5f%%\n", 180 | access_cache_hit, 181 | access_cache_miss + access_cache_hit, 182 | double(access_cache_hit) / (access_cache_miss + access_cache_hit) * 100); 183 | printf(" create/destroy %lld %lld\n", create, destroy); 184 | printf(" swap_in/out/dirty %lld %lld %lld %.5f%%\n", 185 | access_cache_miss, swap_out, dirty_write, 186 | double(dirty_write) / (swap_out) * 100); 187 | } 188 | 189 | Stat() : create(0), destroy(0), 190 | access_cache_hit(1), access_cache_miss(0), dirty_write(0), swap_out(0) {} 191 | } stat; 192 | 193 | using BLRU = LRU; 194 | BLRU lru; 195 | 196 | Persistence(const char *path = nullptr) : path(path), lst_empty_slot(16) { 197 | assert(Index::is_serializable()); 198 | assert(Leaf::is_serializable()); 199 | persistence_index = new PersistenceIndex; 200 | pages = new Block *[MAX_PAGES]; 201 | dirty = new bool[MAX_PAGES]; 202 | memset(pages, 0, sizeof(Block *) * MAX_PAGES); 203 | memset(dirty, 0, sizeof(bool) * MAX_PAGES); 204 | if (path) { 205 | f.open(path, std::ios::in | std::ios::out | std::ios::ate | std::ios::binary); 206 | if (f) 207 | restore(); 208 | else 209 | f.open(path, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary); 210 | } 211 | } 212 | 213 | ~Persistence() { 214 | save(); 215 | f.close(); 216 | delete[] dirty; 217 | delete[] pages; 218 | delete persistence_index; 219 | } 220 | 221 | void restore() { 222 | if (!path) return; 223 | f.seekg(0, f.beg); 224 | if (!f.read(reinterpret_cast(persistence_index), sizeof(PersistenceIndex))) { 225 | std::clog << "[Warning] failed to restore from " << path << " " << f.gcount() << std::endl; 226 | f.clear(); 227 | } 228 | assert(persistence_index->version == VERSION); 229 | assert(persistence_index->magic_key == PersistenceIndex::MAGIC_KEY()); 230 | } 231 | 232 | void offload_page(unsigned page_id) { 233 | if (!path) return; 234 | Block *page = pages[page_id]; 235 | 236 | if (dirty[page_id]) { 237 | auto offset = persistence_index->page_offset[page_id]; 238 | f.seekp(offset, f.beg); 239 | page->serialize(f); 240 | ++stat.dirty_write; 241 | } 242 | 243 | delete pages[page_id]; 244 | pages[page_id] = nullptr; 245 | 246 | lru.remove(page_id); 247 | } 248 | 249 | bool is_loaded(unsigned page_id) { return pages[page_id] != nullptr; } 250 | 251 | Block *load_page(unsigned page_id) { 252 | if (pages[page_id]) { 253 | ++stat.access_cache_hit; 254 | lru.get(page_id); 255 | return pages[page_id]; 256 | } 257 | if (!path) return nullptr; 258 | ++stat.access_cache_miss; 259 | Block *page; 260 | if (!persistence_index->page_offset[page_id]) return nullptr; 261 | if (persistence_index->is_leaf[page_id] == 1) 262 | page = new Leaf; 263 | else if (persistence_index->is_leaf[page_id] == 0) 264 | page = new Index; 265 | else 266 | assert(false); 267 | f.seekg(persistence_index->page_offset[page_id], f.beg); 268 | page->deserialize(f); 269 | pages[page_id] = page; 270 | page->storage = this; 271 | page->idx = page_id; 272 | lru.put(page_id); 273 | return page; 274 | } 275 | 276 | void save() { 277 | if (!path) return; 278 | f.seekp(0, f.beg); 279 | f.write(reinterpret_cast(persistence_index), sizeof(PersistenceIndex)); 280 | for (unsigned i = 0; i < MAX_PAGES; i++) { 281 | if (pages[i]) offload_page(i); 282 | } 283 | } 284 | 285 | const Block *read(unsigned page_id) { 286 | return load_page(page_id); 287 | } 288 | 289 | Block *get(unsigned page_id) { 290 | dirty[page_id] = true; 291 | return load_page(page_id); 292 | } 293 | 294 | size_t align_to_4k(size_t offset) { 295 | return (offset + 0xfff) & (~0xfff); 296 | } 297 | 298 | unsigned append_page(size_t &offset, size_t size) { 299 | offset = align_to_4k(persistence_index->tail_pos); 300 | persistence_index->tail_pos = offset + size; 301 | return lst_empty_slot++; 302 | } 303 | 304 | unsigned request_page(size_t &offset, size_t size) { 305 | for(;;lst_empty_slot++) { 306 | if (persistence_index->page_offset[lst_empty_slot] == 0) return append_page(offset, size); 307 | if (persistence_index->is_leaf[lst_empty_slot] == 2 && persistence_index->page_size[lst_empty_slot] >= size) { 308 | offset = persistence_index->page_offset[lst_empty_slot]; 309 | return lst_empty_slot++; 310 | } 311 | } 312 | } 313 | 314 | void create_page(Block *block) { 315 | size_t offset; 316 | unsigned page_id = request_page(offset, block->storage_size()); 317 | block->idx = page_id; 318 | pages[page_id] = block; 319 | persistence_index->is_leaf[page_id] = block->is_leaf() ? 1 : 0; 320 | persistence_index->page_offset[page_id] = offset; 321 | persistence_index->page_size[page_id] = block->storage_size(); 322 | lru.put(page_id); 323 | dirty[page_id] = true; 324 | } 325 | 326 | void swap_out_pages() { 327 | while (lru.size > MAX_IN_MEMORY) { 328 | unsigned idx = lru.expire(); 329 | offload_page(idx); 330 | ++stat.swap_out; 331 | } 332 | } 333 | 334 | void record(Block *block) { 335 | create_page(block); 336 | block->storage = this; 337 | ++stat.create; 338 | } 339 | 340 | void deregister(Block *block) { 341 | lru.remove(block->idx); 342 | pages[block->idx] = nullptr; 343 | dirty[block->idx] = false; 344 | lst_empty_slot = std::min(lst_empty_slot, block->idx); 345 | persistence_index->is_leaf[block->idx] = 2; 346 | block->idx = 0; 347 | ++stat.destroy; 348 | } 349 | }; 350 | 351 | #endif //BPLUSTREE_PERSISTENCE_HPP 352 | // 353 | // Created by Alex Chi on 2019-05-24. 354 | // 355 | 356 | #ifndef BPLUSTREE_CONTAINER_HPP 357 | #define BPLUSTREE_CONTAINER_HPP 358 | 359 | #include 360 | #include 361 | #include 362 | #ifndef ONLINE_JUDGE 363 | #include "Persistence.hpp" 364 | #endif 365 | 366 | #ifdef ONLINE_JUDGE 367 | #define ALLOCATOR_DISABLE_MEMORY_ALIGN 368 | #endif 369 | 370 | template 371 | struct Allocator { 372 | U *allocate(unsigned size) { 373 | #ifdef ALLOCATOR_DISABLE_MEMORY_ALIGN 374 | return (U *) ::operator new(sizeof(U) * size); 375 | #else 376 | return (U *) ::operator new(sizeof(U) * size, (std::align_val_t) (4 * 1024)); 377 | #endif 378 | } 379 | 380 | void deallocate(U *x) { ::operator delete(x); } 381 | 382 | void construct(U *x, const U &d) { new(x) U(d); } 383 | 384 | void destruct(U *x) { x->~U(); } 385 | }; 386 | 387 | template 388 | class Vector : Serializable { 389 | Allocator a; 390 | public: 391 | T *x; 392 | unsigned size; 393 | 394 | static constexpr unsigned capacity() { return Cap; } 395 | 396 | Vector() : size(0) { 397 | x = a.allocate(capacity()); 398 | } 399 | 400 | virtual ~Vector() { 401 | for (int i = 0; i < size; i++) a.destruct(&x[i]); 402 | a.deallocate(x); 403 | } 404 | 405 | Vector(const Vector &) = delete; 406 | 407 | T &operator[](unsigned i) { 408 | assert(i < size); 409 | return x[i]; 410 | } 411 | 412 | const T &operator[](unsigned i) const { 413 | assert(i < size); 414 | return x[i]; 415 | } 416 | 417 | void append(const T &d) { 418 | assert(size < Cap); 419 | a.construct(&x[size++], d); 420 | } 421 | 422 | T pop() { 423 | assert(size > 0); 424 | T d = x[size - 1]; 425 | a.destruct(&x[--size]); 426 | return d; 427 | } 428 | 429 | void insert(unsigned pos, const T &d) { 430 | assert(pos >= 0 && pos <= size); 431 | assert(size < capacity()); 432 | memmove(x + pos + 1, x + pos, (size - pos) * sizeof(T)); 433 | a.construct(&x[pos], d); 434 | ++size; 435 | } 436 | 437 | void remove_range(unsigned pos, unsigned length = 1) { 438 | assert(size >= length); 439 | assert(pos < size); 440 | for (int i = pos; i < pos + length; i++) a.destruct(&x[i]); 441 | memmove(x + pos, x + pos + length, (size - length - pos) * sizeof(T)); 442 | size -= length; 443 | } 444 | 445 | T remove(unsigned pos) { 446 | assert(size > 0); 447 | assert(pos < size); 448 | T element = x[pos]; 449 | a.destruct(&x[pos]); 450 | memmove(x + pos, x + pos + 1, (size - 1 - pos) * sizeof(T)); 451 | size -= 1; 452 | return element; 453 | } 454 | 455 | void move_from(Vector &that, unsigned offset, unsigned length) { 456 | assert(size == 0); 457 | move_insert_from(that, offset, length, 0); 458 | } 459 | 460 | void move_insert_from(Vector &that, unsigned offset, unsigned length, unsigned at) { 461 | assert(at <= size); 462 | assert(offset + length <= that.size); 463 | assert(size + length <= capacity()); 464 | memmove(x + at + length, x + at, (size - at) * sizeof(T)); 465 | memcpy(x + at, that.x + offset, length * sizeof(T)); 466 | memmove(that.x + offset, that.x + offset + length, (that.size - length - offset) * sizeof(T)); 467 | that.size -= length; 468 | size += length; 469 | } 470 | 471 | unsigned storage_size() const { return Storage_Size(); }; 472 | 473 | /* 474 | * Storage Mapping 475 | * | 8 size | Cap() T x | 476 | */ 477 | void serialize(std::ostream &out) const { 478 | out.write(reinterpret_cast(&size), sizeof(size)); 479 | out.write(reinterpret_cast(x), sizeof(T) * capacity()); 480 | }; 481 | 482 | void deserialize(std::istream &in) { 483 | in.read(reinterpret_cast(&size), sizeof(size)); 484 | in.read(reinterpret_cast(x), sizeof(T) * capacity()); 485 | // WARNING: only applicable to primitive types because no data were constructed!!! 486 | }; 487 | 488 | static constexpr unsigned Storage_Size() { 489 | return sizeof(T) * capacity() + sizeof(unsigned); 490 | } 491 | }; 492 | 493 | template 494 | class Set : public Vector { 495 | public: 496 | unsigned bin_lower_bound(const T &d) const { 497 | // https://academy.realm.io/posts/how-we-beat-cpp-stl-binary-search/ 498 | unsigned low = 0, size = this->size; 499 | while (size > 0) { 500 | unsigned half = size / 2; 501 | unsigned other_half = size - half; 502 | unsigned probe = low + half; 503 | unsigned other_low = low + other_half; 504 | size = half; 505 | low = this->x[probe] < d ? other_low : low; 506 | } 507 | return low; 508 | } 509 | 510 | unsigned bin_upper_bound(const T &d) const { 511 | // https://academy.realm.io/posts/how-we-beat-cpp-stl-binary-search/ 512 | unsigned low = 0, size = this->size; 513 | while (size > 0) { 514 | unsigned half = size / 2; 515 | unsigned other_half = size - half; 516 | unsigned probe = low + half; 517 | unsigned other_low = low + other_half; 518 | size = half; 519 | low = this->x[probe] < d || this->x[probe] == d ? other_low : low; 520 | } 521 | return low; 522 | } 523 | 524 | unsigned linear_upper_bound(const T &d) const { 525 | for (unsigned i = 0; i < this->size; i++) { 526 | if (this->x[i] > d) return i; 527 | } 528 | return this->size; 529 | } 530 | 531 | unsigned linear_lower_bound(const T &d) const { 532 | for (unsigned i = 0; i < this->size; i++) { 533 | if (this->x[i] >= d) return i; 534 | } 535 | return this->size; 536 | } 537 | 538 | unsigned upper_bound(const T &d) const { return bin_upper_bound(d); } 539 | 540 | unsigned lower_bound(const T &d) const { return bin_lower_bound(d); } 541 | 542 | unsigned insert(const T &d) { 543 | unsigned pos = upper_bound(d); 544 | Vector::insert(pos, d); 545 | return pos; 546 | } 547 | }; 548 | 549 | #endif //BPLUSTREE_CONTAINER_HPP 550 | // 551 | // Created by Alex Chi on 2019-06-08. 552 | // 553 | 554 | #ifndef BPLUSTREE_ITERATOR_HPP 555 | #define BPLUSTREE_ITERATOR_HPP 556 | 557 | template 558 | class Iterator { 559 | mutable BTree *tree; 560 | using BlockIdx = typename BTree::BlockIdx; 561 | BlockIdx leaf_idx; 562 | int pos; 563 | public: 564 | Iterator(BTree *tree, BlockIdx leaf_idx, int pos) : tree(tree), leaf_idx(leaf_idx), pos(pos) {} 565 | 566 | Iterator(const Iterator &that) : tree(that.tree), leaf_idx(that.leaf_idx), pos(that.pos) {} 567 | 568 | Iterator &operator++() { 569 | ++pos; 570 | if (pos == leaf()->keys.size && leaf()->next) { 571 | pos = 0; 572 | leaf_idx = leaf()->next; 573 | } 574 | expire(); 575 | return *this; 576 | } 577 | 578 | void expire() const { 579 | tree->storage->swap_out_pages(); 580 | } 581 | 582 | const Leaf *leaf() const { return reinterpret_cast(tree->storage->read(leaf_idx)); } 583 | 584 | Leaf *leaf_mut() { return Block::into_leaf(tree->storage->get(leaf_idx)); } 585 | 586 | Iterator &operator--() { 587 | --pos; 588 | if (pos < 0 && leaf()->prev) { 589 | leaf_idx = leaf()->prev; 590 | pos = leaf()->keys.size - 1; 591 | } 592 | expire(); 593 | return *this; 594 | } 595 | 596 | Iterator operator++(int) { 597 | auto _ = Iterator(*this); 598 | ++(*this); 599 | return _; 600 | } 601 | 602 | Iterator operator--(int) { 603 | auto _ = Iterator(*this); 604 | --(*this); 605 | return _; 606 | } 607 | 608 | V operator*() { 609 | return getValue(); 610 | } 611 | 612 | friend bool operator==(const Iterator &a, const Iterator &b) { 613 | return a.tree == b.tree && a.leaf_idx == b.leaf_idx && a.pos == b.pos; 614 | } 615 | 616 | friend bool operator!=(const Iterator &a, const Iterator &b) { 617 | return !(a == b); 618 | } 619 | 620 | V getValue() { 621 | expire(); 622 | return leaf_mut()->data[pos]; 623 | } 624 | 625 | void modify(const V &v) { 626 | expire(); 627 | leaf_mut()->data[pos] = v; 628 | } 629 | }; 630 | 631 | #endif //BPLUSTREE_ITERATOR_HPP 632 | // 633 | // Created by Alex Chi on 2019-05-23. 634 | //ac 635 | 636 | #ifndef BPLUSTREE_BTREE_HPP 637 | #define BPLUSTREE_BTREE_HPP 638 | 639 | #include 640 | #include 641 | #include 642 | 643 | #include "utility.hpp" 644 | 645 | #ifndef ONLINE_JUDGE 646 | 647 | #include "Container.hpp" 648 | #include "Persistence.hpp" 649 | #include "Iterator.hpp" 650 | 651 | #endif 652 | 653 | template 654 | constexpr unsigned Default_Ord() { 655 | return std::max((int) ((4 * 1024 - sizeof(unsigned) * 3) / (sizeof(K) + sizeof(unsigned))), 4); 656 | } 657 | 658 | template 659 | constexpr unsigned Default_Max_Page_In_Memory() { 660 | // maximum is about 6GB in memory 661 | return 2 * 1024 * 1024 / Ord / sizeof(K) * 1024; 662 | } 663 | 664 | // if unit test is enabled 665 | #ifdef REQUIRE 666 | 667 | constexpr unsigned Default_Max_Pages() { 668 | return 1048576; 669 | } 670 | 671 | #else 672 | constexpr unsigned Default_Max_Pages() { 673 | return 16777216; 674 | } 675 | #endif 676 | 677 | template(), 679 | unsigned Max_Page_In_Memory = Default_Max_Page_In_Memory(), 680 | unsigned Max_Page = Default_Max_Pages()> 681 | class BTree { 682 | public: 683 | static constexpr unsigned MaxPageInMemory() { return Max_Page_In_Memory; } 684 | 685 | static constexpr unsigned MaxPage() { return Max_Page; } 686 | 687 | using BlockIdx = unsigned; 688 | 689 | static constexpr unsigned Order() { return Ord; } 690 | 691 | static constexpr unsigned HalfOrder() { return Order() >> 1; } 692 | 693 | /* 694 | * data Block k v = Index { idx :: Int, 695 | * keys :: [k], 696 | * children :: [Block] } | 697 | * Leaf { idx :: Int, 698 | * prev :: Block, 699 | * next :: Block, 700 | * keys :: [k], 701 | * data :: [v] } 702 | */ 703 | 704 | class Leaf; 705 | 706 | class Index; 707 | 708 | class Block; 709 | 710 | using LeafPos = pair; 711 | using BPersistence = Persistence; 712 | 713 | struct Block : public Serializable { 714 | BlockIdx idx; 715 | Set keys; 716 | BPersistence *storage; 717 | 718 | Block() : storage(nullptr) {} 719 | 720 | virtual ~Block() {} 721 | 722 | virtual bool is_leaf() const = 0; 723 | 724 | virtual Block *split(K &split_key) = 0; 725 | 726 | virtual bool insert(const K &k, const V &v) = 0; 727 | 728 | virtual bool remove(const K &k) = 0; 729 | 730 | virtual const V *query(const K &k) const = 0; 731 | 732 | virtual LeafPos find(const K &k) const = 0; 733 | 734 | bool should_split() const { return keys.size == Order(); } 735 | 736 | bool should_merge() const { return keys.size * 2 < Order(); } 737 | 738 | bool may_borrow() const { return keys.size * 2 > Order(); } 739 | 740 | virtual K borrow_from_left(Block *left, const K &split_key) = 0; 741 | 742 | virtual K borrow_from_right(Block *right, const K &split_key) = 0; 743 | 744 | virtual void merge_with_left(Block *left, const K &split_key) = 0; 745 | 746 | virtual void merge_with_right(Block *right, const K &split_key) = 0; 747 | 748 | inline static Leaf *into_leaf(Block *b) { 749 | assert(b->is_leaf()); 750 | Leaf *l = reinterpret_cast(b); 751 | assert(l); 752 | return l; 753 | } 754 | 755 | inline static Index *into_index(Block *b) { 756 | assert(!b->is_leaf()); 757 | Index *i = reinterpret_cast(b); 758 | assert(i); 759 | return i; 760 | } 761 | }; 762 | 763 | struct Index : public Block { 764 | Index() : Block() {} 765 | 766 | Vector children; 767 | 768 | constexpr bool is_leaf() const override { return false; } 769 | 770 | Index *split(K &k) override { 771 | Index *that = new Index; 772 | this->storage->record(that); 773 | that->keys.move_from(this->keys, HalfOrder(), HalfOrder()); 774 | that->children.move_from(this->children, HalfOrder(), HalfOrder() + 1); 775 | k = this->keys.pop(); 776 | return that; 777 | } 778 | 779 | void insert_block(const K &k, BlockIdx v) { 780 | unsigned pos = this->keys.insert(k); 781 | this->children.insert(pos + 1, v); 782 | } 783 | 784 | const V *query(const K &k) const override { 785 | // {left: key < index_key} {right: key >= index_key} 786 | unsigned pos = this->keys.upper_bound(k); 787 | const Block *block = this->storage->read(children[pos]); 788 | return block->query(k); 789 | } 790 | 791 | LeafPos find(const K &k) const override { 792 | unsigned pos = this->keys.upper_bound(k); 793 | const Block *block = this->storage->read(children[pos]); 794 | return block->find(k); 795 | } 796 | 797 | bool insert(const K &k, const V &v) override { 798 | // {left: key < index_key} {right: key >= index_key} 799 | unsigned pos = this->keys.upper_bound(k); 800 | Block *block = this->storage->get(children[pos]); 801 | bool result = block->insert(k, v); 802 | if (!result) return false; 803 | if (block->should_split()) { 804 | K k; 805 | Block *that = block->split(k); 806 | insert_block(k, that->idx); 807 | } 808 | return true; 809 | }; 810 | 811 | bool remove(const K &k) override { 812 | unsigned pos = this->keys.upper_bound(k); 813 | Block *block = this->storage->get(children[pos]); 814 | bool result = block->remove(k); 815 | if (!result) return false; 816 | if (block->should_merge()) { 817 | if (pos != 0) { 818 | K split_key = this->keys[pos - 1]; 819 | Block *left = this->storage->get(children[pos - 1]); 820 | if (left->may_borrow()) { 821 | this->keys[pos - 1] = block->borrow_from_left(left, split_key); 822 | return true; 823 | } 824 | } 825 | if (pos != this->children.size - 1) { 826 | K split_key = this->keys[pos]; 827 | Block *right = this->storage->get(children[pos + 1]); 828 | if (right->may_borrow()) { 829 | this->keys[pos] = block->borrow_from_right(right, split_key); 830 | return true; 831 | } 832 | } 833 | if (pos != 0) { 834 | K split_key = this->keys[pos - 1]; 835 | Block *left = this->storage->get(children.remove(pos - 1)); 836 | block->merge_with_left(left, split_key); 837 | delete left; 838 | this->keys.remove(pos - 1); 839 | return true; 840 | } 841 | if (pos != this->children.size - 1) { 842 | K split_key = this->keys[pos]; 843 | Block *right = this->storage->get(children.remove(pos + 1)); 844 | block->merge_with_right(right, split_key); 845 | delete right; 846 | this->keys.remove(pos); 847 | return true; 848 | } 849 | assert(false); 850 | } 851 | return true; 852 | } 853 | 854 | static constexpr unsigned Storage_Size() { 855 | return Set::Storage_Size() + Vector::Storage_Size(); 856 | } 857 | 858 | K borrow_from_left(Block *_left, const K &split_key) override { 859 | Index *left = this->into_index(_left); 860 | // TODO: we should verify that split_key is always the minimum 861 | this->keys.insert(split_key); 862 | // TODO: wish I were writing in Rust... therefore there'll be no copy overhead 863 | K new_split_key = left->keys.pop(); 864 | this->children.move_insert_from(left->children, left->children.size - 1, 1, 0); 865 | return new_split_key; 866 | }; 867 | 868 | K borrow_from_right(Block *_right, const K &split_key) override { 869 | Index *right = this->into_index(_right); 870 | this->keys.insert(split_key); 871 | K new_split_key = right->keys.remove(0); 872 | this->children.move_insert_from(right->children, 0, 1, this->children.size); 873 | return new_split_key; 874 | }; 875 | 876 | void merge_with_left(Block *_left, const K &split_key) override { 877 | Index *left = this->into_index(_left); 878 | this->keys.insert(split_key); 879 | this->keys.move_insert_from(left->keys, 0, left->keys.size, 0); 880 | this->children.move_insert_from(left->children, 0, left->children.size, 0); 881 | this->storage->deregister(left); 882 | }; 883 | 884 | void merge_with_right(Block *_right, const K &split_key) override { 885 | Index *right = this->into_index(_right); 886 | this->keys.insert(split_key); 887 | this->keys.move_insert_from(right->keys, 0, right->keys.size, this->keys.size); 888 | this->children.move_insert_from(right->children, 0, right->children.size, this->children.size); 889 | this->storage->deregister(right); 890 | }; 891 | 892 | unsigned storage_size() const override { return Storage_Size(); } 893 | 894 | /* 895 | * Storage Mapping 896 | * | 8 size | Order() K keys | 897 | * | 8 size | Order()+1 BlockIdx children | 898 | */ 899 | void serialize(std::ostream &out) const override { 900 | this->keys.serialize(out); 901 | this->children.serialize(out); 902 | }; 903 | 904 | void deserialize(std::istream &in) override { 905 | this->keys.deserialize(in); 906 | this->children.deserialize(in); 907 | }; 908 | }; 909 | 910 | struct Leaf : public Block { 911 | BlockIdx prev, next; 912 | Vector data; 913 | 914 | Leaf() : Block(), prev(0), next(0) {} 915 | 916 | constexpr bool is_leaf() const override { return true; } 917 | 918 | const V *query(const K &k) const override { 919 | LeafPos pos = this->find(k); 920 | if (pos.first == 0) return nullptr; 921 | return &this->data[pos.second]; 922 | } 923 | 924 | LeafPos find(const K &k) const override { 925 | unsigned pos = this->keys.lower_bound(k); 926 | if (pos >= this->keys.size || this->keys[pos] != k) 927 | return LeafPos(0, 0); 928 | else 929 | return LeafPos(this->idx, pos); 930 | } 931 | 932 | bool insert(const K &k, const V &v) override { 933 | unsigned pos = this->keys.lower_bound(k); 934 | if (pos < this->keys.size && this->keys[pos] == k) return false; 935 | this->keys.insert(k); 936 | this->data.insert(pos, v); 937 | return true; 938 | } 939 | 940 | bool remove(const K &k) override { 941 | unsigned pos = this->keys.lower_bound(k); 942 | if (pos >= this->keys.size || this->keys[pos] != k) 943 | return false; 944 | this->keys.remove(pos); 945 | this->data.remove(pos); 946 | return true; 947 | } 948 | 949 | /* split :: Leaf -> (k, Leaf, Leaf) 950 | * split (Leaf a) = [k, prev, next] 951 | * this = prev, return = next 952 | */ 953 | Leaf *split(K &k) override { 954 | assert(this->should_split()); 955 | Leaf *that = new Leaf; 956 | this->storage->record(that); 957 | if (this->next) { 958 | Leaf* rr = this->into_leaf(this->storage->get(this->next)); 959 | rr->prev = that->idx; 960 | that->next = rr->idx; 961 | } 962 | this->next = that->idx; 963 | that->prev = this->idx; 964 | that->keys.move_from(this->keys, HalfOrder(), HalfOrder()); 965 | that->data.move_from(this->data, HalfOrder(), HalfOrder()); 966 | k = that->keys[0]; 967 | return that; 968 | } 969 | 970 | K borrow_from_left(Block *_left, const K &) override { 971 | Leaf *left = this->into_leaf(_left); 972 | assert(this->prev == left->idx); 973 | assert(left->next == this->idx); 974 | this->keys.move_insert_from(left->keys, left->keys.size - 1, 1, 0); 975 | this->data.move_insert_from(left->data, left->data.size - 1, 1, 0); 976 | return this->keys[0]; 977 | }; 978 | 979 | K borrow_from_right(Block *_right, const K &) override { 980 | Leaf *right = this->into_leaf(_right); 981 | assert(this->next == right->idx); 982 | assert(right->prev == this->idx); 983 | this->keys.move_insert_from(right->keys, 0, 1, this->keys.size); 984 | this->data.move_insert_from(right->data, 0, 1, this->data.size); 985 | return right->keys[0]; 986 | }; 987 | 988 | void merge_with_left(Block *_left, const K &) override { 989 | Leaf *left = this->into_leaf(_left); 990 | assert(this->prev == left->idx); 991 | assert(left->next == this->idx); 992 | this->keys.move_insert_from(left->keys, 0, left->keys.size, 0); 993 | this->data.move_insert_from(left->data, 0, left->data.size, 0); 994 | this->prev = left->prev; 995 | if (this->prev) { 996 | Leaf* ll = this->into_leaf(this->storage->get(this->prev)); 997 | ll->next = this->idx; 998 | } 999 | this->storage->deregister(left); 1000 | }; 1001 | 1002 | void merge_with_right(Block *_right, const K &) override { 1003 | Leaf *right = this->into_leaf(_right); 1004 | assert(this->next == right->idx); 1005 | assert(right->prev == this->idx); 1006 | this->keys.move_insert_from(right->keys, 0, right->keys.size, this->keys.size); 1007 | this->data.move_insert_from(right->data, 0, right->data.size, this->data.size); 1008 | this->next = right->next; 1009 | if (this->next) { 1010 | Leaf* rr = this->into_leaf(this->storage->get(this->next)); 1011 | rr->prev = this->idx; 1012 | } 1013 | this->storage->deregister(right); 1014 | }; 1015 | 1016 | static constexpr unsigned Storage_Size() { 1017 | return Set::Storage_Size() + Vector::Storage_Size() + sizeof(BlockIdx) * 2; 1018 | } 1019 | 1020 | unsigned storage_size() const override { return Storage_Size(); } 1021 | 1022 | /* 1023 | * Storage Mapping 1024 | * | 8 BlockIdx prev | 8 BlockIdx next | 1025 | * | 8 size | Order() K keys | 1026 | * | 8 size | Order() V data | 1027 | */ 1028 | 1029 | void serialize(std::ostream &out) const override { 1030 | out.write(reinterpret_cast(&prev), sizeof(prev)); 1031 | out.write(reinterpret_cast(&next), sizeof(next)); 1032 | this->keys.serialize(out); 1033 | this->data.serialize(out); 1034 | }; 1035 | 1036 | void deserialize(std::istream &in) override { 1037 | in.read(reinterpret_cast(&prev), sizeof(prev)); 1038 | in.read(reinterpret_cast(&next), sizeof(next)); 1039 | this->keys.deserialize(in); 1040 | this->data.deserialize(in); 1041 | }; 1042 | }; 1043 | 1044 | using iterator = Iterator; 1045 | using const_iterator = Iterator; 1046 | 1047 | BTree(const BTree &) = delete; 1048 | 1049 | BTree &operator=(const BTree &) = delete; 1050 | 1051 | Leaf *get_leaf_begin() const { 1052 | Block *blk = root(); 1053 | while (!blk->is_leaf()) blk = storage->get(Block::into_index(blk)->children[0]); 1054 | return Block::into_leaf(blk); 1055 | } 1056 | 1057 | Leaf *get_leaf_end() const { 1058 | Block *blk = root(); 1059 | while (!blk->is_leaf()) { 1060 | Index *idx = Block::into_index(blk); 1061 | blk = storage->get(idx->children[idx->children.size - 1]); 1062 | } 1063 | return Block::into_leaf(blk); 1064 | } 1065 | 1066 | iterator begin() { 1067 | auto leaf = get_leaf_begin(); 1068 | return iterator(this, leaf->idx, 0); 1069 | } 1070 | 1071 | iterator end() { 1072 | auto leaf = get_leaf_end(); 1073 | return iterator(this, leaf->idx, leaf->keys.size); 1074 | } 1075 | 1076 | const_iterator cbegin() const { 1077 | auto leaf = get_leaf_begin(); 1078 | return const_iterator(this, leaf->idx, 0); 1079 | } 1080 | 1081 | const_iterator cend() const { 1082 | auto leaf = get_leaf_end(); 1083 | return const_iterator(this, leaf->idx, leaf->keys.size); 1084 | } 1085 | 1086 | mutable BPersistence *storage; 1087 | const char *path; 1088 | 1089 | unsigned &root_idx() const { 1090 | return storage->persistence_index->root_idx; 1091 | } 1092 | 1093 | Block *root() const { 1094 | return storage->get(root_idx()); 1095 | } 1096 | 1097 | BTree(const char *path) : path(path) { 1098 | storage = new BPersistence(path); 1099 | } 1100 | 1101 | #ifdef ONLINE_JUDGE 1102 | BTree() : BTree("persist.db") {} 1103 | #else 1104 | 1105 | BTree() : BTree(nullptr) {} 1106 | 1107 | #endif 1108 | 1109 | ~BTree() { 1110 | delete storage; 1111 | } 1112 | 1113 | V at(const K &k) { 1114 | return *query(k); 1115 | } 1116 | 1117 | const V* query(const K& k) { 1118 | if (!root_idx()) return nullptr; 1119 | auto v = storage->read(root_idx())->query(k); 1120 | storage->swap_out_pages(); 1121 | return v; 1122 | } 1123 | 1124 | iterator find(const K &k) { 1125 | if (!root_idx()) return end(); 1126 | auto v = storage->read(root_idx())->find(k); 1127 | storage->swap_out_pages(); 1128 | if (v.first == 0) return end(); 1129 | return iterator(this, v.first, v.second); 1130 | } 1131 | 1132 | Leaf *create_leaf() { 1133 | Leaf *block = new Leaf; 1134 | storage->record(block); 1135 | return block; 1136 | } 1137 | 1138 | Index *create_index() { 1139 | Index *block = new Index; 1140 | storage->record(block); 1141 | return block; 1142 | } 1143 | 1144 | OperationResult insert(const K &k, const V &v) { 1145 | if (!root_idx()) root_idx() = create_leaf()->idx; 1146 | auto root = storage->get(root_idx()); 1147 | bool result = storage->get(root_idx())->insert(k, v); 1148 | if (!result) return OperationResult::Duplicated; 1149 | if (root->should_split()) { 1150 | K k; 1151 | Block *next = root->split(k); 1152 | Block *prev = root; 1153 | Index *idx = create_index(); 1154 | idx->children.append(prev->idx); 1155 | idx->children.append(next->idx); 1156 | idx->keys.append(k); 1157 | root_idx() = idx->idx; 1158 | } 1159 | ++storage->persistence_index->size; 1160 | storage->swap_out_pages(); 1161 | return OperationResult::Success; 1162 | } 1163 | 1164 | bool remove(const K &k) { 1165 | if (!root_idx()) return false; 1166 | auto root = storage->get(root_idx()); 1167 | bool result = storage->get(root_idx())->remove(k); 1168 | if (!result) return false; 1169 | if (root->keys.size == 0) { 1170 | if (!root->is_leaf()) { 1171 | Index *prev_root = Block::into_index(root); 1172 | root_idx() = prev_root->children[0]; 1173 | storage->deregister(prev_root); 1174 | delete prev_root; 1175 | } 1176 | } 1177 | --storage->persistence_index->size; 1178 | storage->swap_out_pages(); 1179 | return true; 1180 | } 1181 | 1182 | unsigned size() const { return storage->persistence_index->size; } 1183 | 1184 | unsigned count(const K &k) { return query(k) ? 1 : 0; } 1185 | 1186 | void debug(Block *block) { 1187 | std::cerr << "Block ID: " << block->idx << " "; 1188 | if (block->is_leaf()) std::cerr << "(Leaf)" << std::endl; 1189 | else std::cerr << "(Index)" << std::endl; 1190 | if (block->is_leaf()) { 1191 | Leaf *leaf = Block::into_leaf(block); 1192 | for (int i = 0; i < leaf->keys.size; i++) { 1193 | std::cerr << leaf->keys[i] << "=" << leaf->data[i] << " "; 1194 | } 1195 | std::cerr << std::endl; 1196 | } else { 1197 | Index *index = Block::into_index(block); 1198 | for (int i = 0; i < index->keys.size; i++) { 1199 | std::cerr << index->keys[i] << " "; 1200 | } 1201 | std::cerr << std::endl; 1202 | for (int i = 0; i < index->children.size; i++) { 1203 | std::cerr << index->children[i] << " "; 1204 | } 1205 | std::cerr << std::endl; 1206 | for (int i = 0; i < index->children.size; i++) { 1207 | debug(storage->get(index->children[i])); 1208 | } 1209 | } 1210 | } 1211 | 1212 | OperationResult erase(const K &k) { 1213 | if (remove(k)) return OperationResult::Success; 1214 | else return OperationResult::Fail; 1215 | } 1216 | }; 1217 | 1218 | #endif //BPLUSTREE_BTREE_HPP 1219 | } // namespace sjtu 1220 | 1221 | // THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT 1222 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | project(BPlusTree) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | find_package(Catch2 CONFIG REQUIRED) 7 | 8 | add_executable(BPlusTree src/main.cpp src/BTree.hpp src/Container.hpp src/Persistence.hpp src/LRU.hpp src/bench1.hpp src/bench2.hpp src/benchmark.hpp) 9 | add_executable(BPlusTreeTest src/test_main.cpp src/BTree.hpp src/BTree_test.cpp src/Container.hpp src/Container_test.cpp src/BTree_Leaf_test.cpp src/BTree_Index_test.cpp src/BTree_Storage_test.cpp src/Persistence_test.cpp src/LRU_test.cpp src/Iterator.hpp src/BTree_Iterator_test.cpp) 10 | target_link_libraries(BPlusTreeTest PRIVATE Catch2::Catch2) 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alex Chi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BPlusTree 2 | 3 | [![Build Status](https://travis-ci.com/SkyZH/bplustree.svg?token=szB6fz2m5vb2KyfAiZ3B&branch=master)](https://travis-ci.com/SkyZH/bplustree) 4 | 5 | A simple B+ tree implemented in cpp. Fully unit-tested. 6 | 7 | ## Implementations 8 | 9 | `BTree.cpp` is automatically generated with source file from `src/` folder. 10 | For original implementation, you should refer to `src/`. 11 | 12 | Container: some basic containers implementation including `Vector` and `Set`. 13 | 14 | LRU: least recently used cache, eliminating memory usage when processing huge chunks. 15 | 16 | Persistence: manage so-called 'pages', which store BTree data. 17 | 18 | Iterator: B+Tree iterators 19 | 20 | Partially ported to upstream https://github.com/peterzheng98/CS158-DS_Project 21 | 22 | ## Limitations 23 | 24 | Number of pages in memory must be larger than those required for a single search, because page swapping is done after one operation. 25 | 26 | ## Todo 27 | 28 | - [x] Reduce overhead in serialize and deserialize by passing istream as argument 29 | - [x] 4k align 30 | - [x] Reduce page offload overhead by introducing read-only page request 31 | - [ ] Use HashMap to store pages in memory to store larger dataset. 32 | - [ ] Implement copy constructor and assign 33 | - [x] Implement iterator and const iterator 34 | - [x] Reuse deleted pages 35 | - [ ] Port to upstream: size, empty, iterator, return value of query and insert. 36 | -------------------------------------------------------------------------------- /generate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cat < BTree.hpp 4 | 5 | // THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT 6 | 7 | #include "utility.hpp" 8 | #include 9 | #include 10 | #include "exception.hpp" 11 | #include 12 | #include 13 | 14 | #define ONLINE_JUDGE 15 | 16 | namespace sjtu { 17 | EOF 18 | 19 | cat src/LRU.hpp >> BTree.hpp 20 | cat src/Persistence.hpp >> BTree.hpp 21 | cat src/Container.hpp >> BTree.hpp 22 | cat src/Iterator.hpp >> BTree.hpp 23 | cat src/BTree.hpp >> BTree.hpp 24 | 25 | cat <> BTree.hpp 26 | } // namespace sjtu 27 | 28 | // THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT 29 | EOF 30 | -------------------------------------------------------------------------------- /src/BTree.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-23. 3 | //ac 4 | 5 | #ifndef BPLUSTREE_BTREE_HPP 6 | #define BPLUSTREE_BTREE_HPP 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "utility.hpp" 13 | 14 | #ifndef ONLINE_JUDGE 15 | 16 | #include "Container.hpp" 17 | #include "Persistence.hpp" 18 | #include "Iterator.hpp" 19 | 20 | #endif 21 | 22 | template 23 | constexpr unsigned Default_Ord() { 24 | return std::max((int) ((4 * 1024 - sizeof(unsigned) * 3) / (sizeof(K) + sizeof(unsigned))), 4); 25 | } 26 | 27 | template 28 | constexpr unsigned Default_Max_Page_In_Memory() { 29 | // maximum is about 6GB in memory 30 | return 2 * 1024 * 1024 / Ord / sizeof(K) * 1024; 31 | } 32 | 33 | // if unit test is enabled 34 | #ifdef REQUIRE 35 | 36 | constexpr unsigned Default_Max_Pages() { 37 | return 1048576; 38 | } 39 | 40 | #else 41 | constexpr unsigned Default_Max_Pages() { 42 | return 16777216; 43 | } 44 | #endif 45 | 46 | template(), 48 | unsigned Max_Page_In_Memory = Default_Max_Page_In_Memory(), 49 | unsigned Max_Page = Default_Max_Pages()> 50 | class BTree { 51 | public: 52 | static constexpr unsigned MaxPageInMemory() { return Max_Page_In_Memory; } 53 | 54 | static constexpr unsigned MaxPage() { return Max_Page; } 55 | 56 | using BlockIdx = unsigned; 57 | 58 | static constexpr unsigned Order() { return Ord; } 59 | 60 | static constexpr unsigned HalfOrder() { return Order() >> 1; } 61 | 62 | /* 63 | * data Block k v = Index { idx :: Int, 64 | * keys :: [k], 65 | * children :: [Block] } | 66 | * Leaf { idx :: Int, 67 | * prev :: Block, 68 | * next :: Block, 69 | * keys :: [k], 70 | * data :: [v] } 71 | */ 72 | 73 | class Leaf; 74 | 75 | class Index; 76 | 77 | class Block; 78 | 79 | using LeafPos = pair; 80 | using BPersistence = Persistence; 81 | 82 | struct Block : public Serializable { 83 | BlockIdx idx; 84 | Set keys; 85 | BPersistence *storage; 86 | 87 | Block() : storage(nullptr) {} 88 | 89 | virtual ~Block() {} 90 | 91 | virtual bool is_leaf() const = 0; 92 | 93 | virtual Block *split(K &split_key) = 0; 94 | 95 | virtual bool insert(const K &k, const V &v) = 0; 96 | 97 | virtual bool remove(const K &k) = 0; 98 | 99 | virtual const V *query(const K &k) const = 0; 100 | 101 | virtual LeafPos find(const K &k) const = 0; 102 | 103 | bool should_split() const { return keys.size == Order(); } 104 | 105 | bool should_merge() const { return keys.size * 2 < Order(); } 106 | 107 | bool may_borrow() const { return keys.size * 2 > Order(); } 108 | 109 | virtual K borrow_from_left(Block *left, const K &split_key) = 0; 110 | 111 | virtual K borrow_from_right(Block *right, const K &split_key) = 0; 112 | 113 | virtual void merge_with_left(Block *left, const K &split_key) = 0; 114 | 115 | virtual void merge_with_right(Block *right, const K &split_key) = 0; 116 | 117 | inline static Leaf *into_leaf(Block *b) { 118 | assert(b->is_leaf()); 119 | Leaf *l = reinterpret_cast(b); 120 | assert(l); 121 | return l; 122 | } 123 | 124 | inline static Index *into_index(Block *b) { 125 | assert(!b->is_leaf()); 126 | Index *i = reinterpret_cast(b); 127 | assert(i); 128 | return i; 129 | } 130 | }; 131 | 132 | struct Index : public Block { 133 | Index() : Block() {} 134 | 135 | Vector children; 136 | 137 | constexpr bool is_leaf() const override { return false; } 138 | 139 | Index *split(K &k) override { 140 | Index *that = new Index; 141 | this->storage->record(that); 142 | that->keys.move_from(this->keys, HalfOrder(), HalfOrder()); 143 | that->children.move_from(this->children, HalfOrder(), HalfOrder() + 1); 144 | k = this->keys.pop(); 145 | return that; 146 | } 147 | 148 | void insert_block(const K &k, BlockIdx v) { 149 | unsigned pos = this->keys.insert(k); 150 | this->children.insert(pos + 1, v); 151 | } 152 | 153 | const V *query(const K &k) const override { 154 | // {left: key < index_key} {right: key >= index_key} 155 | unsigned pos = this->keys.upper_bound(k); 156 | const Block *block = this->storage->read(children[pos]); 157 | return block->query(k); 158 | } 159 | 160 | LeafPos find(const K &k) const override { 161 | unsigned pos = this->keys.upper_bound(k); 162 | const Block *block = this->storage->read(children[pos]); 163 | return block->find(k); 164 | } 165 | 166 | bool insert(const K &k, const V &v) override { 167 | // {left: key < index_key} {right: key >= index_key} 168 | unsigned pos = this->keys.upper_bound(k); 169 | Block *block = this->storage->get(children[pos]); 170 | bool result = block->insert(k, v); 171 | if (!result) return false; 172 | if (block->should_split()) { 173 | K k; 174 | Block *that = block->split(k); 175 | insert_block(k, that->idx); 176 | } 177 | return true; 178 | }; 179 | 180 | bool remove(const K &k) override { 181 | unsigned pos = this->keys.upper_bound(k); 182 | Block *block = this->storage->get(children[pos]); 183 | bool result = block->remove(k); 184 | if (!result) return false; 185 | if (block->should_merge()) { 186 | if (pos != 0) { 187 | K split_key = this->keys[pos - 1]; 188 | Block *left = this->storage->get(children[pos - 1]); 189 | if (left->may_borrow()) { 190 | this->keys[pos - 1] = block->borrow_from_left(left, split_key); 191 | return true; 192 | } 193 | } 194 | if (pos != this->children.size - 1) { 195 | K split_key = this->keys[pos]; 196 | Block *right = this->storage->get(children[pos + 1]); 197 | if (right->may_borrow()) { 198 | this->keys[pos] = block->borrow_from_right(right, split_key); 199 | return true; 200 | } 201 | } 202 | if (pos != 0) { 203 | K split_key = this->keys[pos - 1]; 204 | Block *left = this->storage->get(children.remove(pos - 1)); 205 | block->merge_with_left(left, split_key); 206 | delete left; 207 | this->keys.remove(pos - 1); 208 | return true; 209 | } 210 | if (pos != this->children.size - 1) { 211 | K split_key = this->keys[pos]; 212 | Block *right = this->storage->get(children.remove(pos + 1)); 213 | block->merge_with_right(right, split_key); 214 | delete right; 215 | this->keys.remove(pos); 216 | return true; 217 | } 218 | assert(false); 219 | } 220 | return true; 221 | } 222 | 223 | static constexpr unsigned Storage_Size() { 224 | return Set::Storage_Size() + Vector::Storage_Size(); 225 | } 226 | 227 | K borrow_from_left(Block *_left, const K &split_key) override { 228 | Index *left = this->into_index(_left); 229 | // TODO: we should verify that split_key is always the minimum 230 | this->keys.insert(split_key); 231 | // TODO: wish I were writing in Rust... therefore there'll be no copy overhead 232 | K new_split_key = left->keys.pop(); 233 | this->children.move_insert_from(left->children, left->children.size - 1, 1, 0); 234 | return new_split_key; 235 | }; 236 | 237 | K borrow_from_right(Block *_right, const K &split_key) override { 238 | Index *right = this->into_index(_right); 239 | this->keys.insert(split_key); 240 | K new_split_key = right->keys.remove(0); 241 | this->children.move_insert_from(right->children, 0, 1, this->children.size); 242 | return new_split_key; 243 | }; 244 | 245 | void merge_with_left(Block *_left, const K &split_key) override { 246 | Index *left = this->into_index(_left); 247 | this->keys.insert(split_key); 248 | this->keys.move_insert_from(left->keys, 0, left->keys.size, 0); 249 | this->children.move_insert_from(left->children, 0, left->children.size, 0); 250 | this->storage->deregister(left); 251 | }; 252 | 253 | void merge_with_right(Block *_right, const K &split_key) override { 254 | Index *right = this->into_index(_right); 255 | this->keys.insert(split_key); 256 | this->keys.move_insert_from(right->keys, 0, right->keys.size, this->keys.size); 257 | this->children.move_insert_from(right->children, 0, right->children.size, this->children.size); 258 | this->storage->deregister(right); 259 | }; 260 | 261 | unsigned storage_size() const override { return Storage_Size(); } 262 | 263 | /* 264 | * Storage Mapping 265 | * | 8 size | Order() K keys | 266 | * | 8 size | Order()+1 BlockIdx children | 267 | */ 268 | void serialize(std::ostream &out) const override { 269 | this->keys.serialize(out); 270 | this->children.serialize(out); 271 | }; 272 | 273 | void deserialize(std::istream &in) override { 274 | this->keys.deserialize(in); 275 | this->children.deserialize(in); 276 | }; 277 | }; 278 | 279 | struct Leaf : public Block { 280 | BlockIdx prev, next; 281 | Vector data; 282 | 283 | Leaf() : Block(), prev(0), next(0) {} 284 | 285 | constexpr bool is_leaf() const override { return true; } 286 | 287 | const V *query(const K &k) const override { 288 | LeafPos pos = this->find(k); 289 | if (pos.first == 0) return nullptr; 290 | return &this->data[pos.second]; 291 | } 292 | 293 | LeafPos find(const K &k) const override { 294 | unsigned pos = this->keys.lower_bound(k); 295 | if (pos >= this->keys.size || this->keys[pos] != k) 296 | return LeafPos(0, 0); 297 | else 298 | return LeafPos(this->idx, pos); 299 | } 300 | 301 | bool insert(const K &k, const V &v) override { 302 | unsigned pos = this->keys.lower_bound(k); 303 | if (pos < this->keys.size && this->keys[pos] == k) return false; 304 | this->keys.insert(k); 305 | this->data.insert(pos, v); 306 | return true; 307 | } 308 | 309 | bool remove(const K &k) override { 310 | unsigned pos = this->keys.lower_bound(k); 311 | if (pos >= this->keys.size || this->keys[pos] != k) 312 | return false; 313 | this->keys.remove(pos); 314 | this->data.remove(pos); 315 | return true; 316 | } 317 | 318 | /* split :: Leaf -> (k, Leaf, Leaf) 319 | * split (Leaf a) = [k, prev, next] 320 | * this = prev, return = next 321 | */ 322 | Leaf *split(K &k) override { 323 | assert(this->should_split()); 324 | Leaf *that = new Leaf; 325 | this->storage->record(that); 326 | if (this->next) { 327 | Leaf* rr = this->into_leaf(this->storage->get(this->next)); 328 | rr->prev = that->idx; 329 | that->next = rr->idx; 330 | } 331 | this->next = that->idx; 332 | that->prev = this->idx; 333 | that->keys.move_from(this->keys, HalfOrder(), HalfOrder()); 334 | that->data.move_from(this->data, HalfOrder(), HalfOrder()); 335 | k = that->keys[0]; 336 | return that; 337 | } 338 | 339 | K borrow_from_left(Block *_left, const K &) override { 340 | Leaf *left = this->into_leaf(_left); 341 | assert(this->prev == left->idx); 342 | assert(left->next == this->idx); 343 | this->keys.move_insert_from(left->keys, left->keys.size - 1, 1, 0); 344 | this->data.move_insert_from(left->data, left->data.size - 1, 1, 0); 345 | return this->keys[0]; 346 | }; 347 | 348 | K borrow_from_right(Block *_right, const K &) override { 349 | Leaf *right = this->into_leaf(_right); 350 | assert(this->next == right->idx); 351 | assert(right->prev == this->idx); 352 | this->keys.move_insert_from(right->keys, 0, 1, this->keys.size); 353 | this->data.move_insert_from(right->data, 0, 1, this->data.size); 354 | return right->keys[0]; 355 | }; 356 | 357 | void merge_with_left(Block *_left, const K &) override { 358 | Leaf *left = this->into_leaf(_left); 359 | assert(this->prev == left->idx); 360 | assert(left->next == this->idx); 361 | this->keys.move_insert_from(left->keys, 0, left->keys.size, 0); 362 | this->data.move_insert_from(left->data, 0, left->data.size, 0); 363 | this->prev = left->prev; 364 | if (this->prev) { 365 | Leaf* ll = this->into_leaf(this->storage->get(this->prev)); 366 | ll->next = this->idx; 367 | } 368 | this->storage->deregister(left); 369 | }; 370 | 371 | void merge_with_right(Block *_right, const K &) override { 372 | Leaf *right = this->into_leaf(_right); 373 | assert(this->next == right->idx); 374 | assert(right->prev == this->idx); 375 | this->keys.move_insert_from(right->keys, 0, right->keys.size, this->keys.size); 376 | this->data.move_insert_from(right->data, 0, right->data.size, this->data.size); 377 | this->next = right->next; 378 | if (this->next) { 379 | Leaf* rr = this->into_leaf(this->storage->get(this->next)); 380 | rr->prev = this->idx; 381 | } 382 | this->storage->deregister(right); 383 | }; 384 | 385 | static constexpr unsigned Storage_Size() { 386 | return Set::Storage_Size() + Vector::Storage_Size() + sizeof(BlockIdx) * 2; 387 | } 388 | 389 | unsigned storage_size() const override { return Storage_Size(); } 390 | 391 | /* 392 | * Storage Mapping 393 | * | 8 BlockIdx prev | 8 BlockIdx next | 394 | * | 8 size | Order() K keys | 395 | * | 8 size | Order() V data | 396 | */ 397 | 398 | void serialize(std::ostream &out) const override { 399 | out.write(reinterpret_cast(&prev), sizeof(prev)); 400 | out.write(reinterpret_cast(&next), sizeof(next)); 401 | this->keys.serialize(out); 402 | this->data.serialize(out); 403 | }; 404 | 405 | void deserialize(std::istream &in) override { 406 | in.read(reinterpret_cast(&prev), sizeof(prev)); 407 | in.read(reinterpret_cast(&next), sizeof(next)); 408 | this->keys.deserialize(in); 409 | this->data.deserialize(in); 410 | }; 411 | }; 412 | 413 | using iterator = Iterator; 414 | using const_iterator = Iterator; 415 | 416 | BTree(const BTree &) = delete; 417 | 418 | BTree &operator=(const BTree &) = delete; 419 | 420 | Leaf *get_leaf_begin() const { 421 | Block *blk = root(); 422 | while (!blk->is_leaf()) blk = storage->get(Block::into_index(blk)->children[0]); 423 | return Block::into_leaf(blk); 424 | } 425 | 426 | Leaf *get_leaf_end() const { 427 | Block *blk = root(); 428 | while (!blk->is_leaf()) { 429 | Index *idx = Block::into_index(blk); 430 | blk = storage->get(idx->children[idx->children.size - 1]); 431 | } 432 | return Block::into_leaf(blk); 433 | } 434 | 435 | iterator begin() { 436 | auto leaf = get_leaf_begin(); 437 | return iterator(this, leaf->idx, 0); 438 | } 439 | 440 | iterator end() { 441 | auto leaf = get_leaf_end(); 442 | return iterator(this, leaf->idx, leaf->keys.size); 443 | } 444 | 445 | const_iterator cbegin() const { 446 | auto leaf = get_leaf_begin(); 447 | return const_iterator(this, leaf->idx, 0); 448 | } 449 | 450 | const_iterator cend() const { 451 | auto leaf = get_leaf_end(); 452 | return const_iterator(this, leaf->idx, leaf->keys.size); 453 | } 454 | 455 | mutable BPersistence *storage; 456 | const char *path; 457 | 458 | unsigned &root_idx() const { 459 | return storage->persistence_index->root_idx; 460 | } 461 | 462 | Block *root() const { 463 | return storage->get(root_idx()); 464 | } 465 | 466 | BTree(const char *path) : path(path) { 467 | storage = new BPersistence(path); 468 | } 469 | 470 | #ifdef ONLINE_JUDGE 471 | BTree() : BTree("persist.db") {} 472 | #else 473 | 474 | BTree() : BTree(nullptr) {} 475 | 476 | #endif 477 | 478 | ~BTree() { 479 | delete storage; 480 | } 481 | 482 | V at(const K &k) { 483 | return *query(k); 484 | } 485 | 486 | const V* query(const K& k) { 487 | if (!root_idx()) return nullptr; 488 | auto v = storage->read(root_idx())->query(k); 489 | storage->swap_out_pages(); 490 | return v; 491 | } 492 | 493 | iterator find(const K &k) { 494 | if (!root_idx()) return end(); 495 | auto v = storage->read(root_idx())->find(k); 496 | storage->swap_out_pages(); 497 | if (v.first == 0) return end(); 498 | return iterator(this, v.first, v.second); 499 | } 500 | 501 | Leaf *create_leaf() { 502 | Leaf *block = new Leaf; 503 | storage->record(block); 504 | return block; 505 | } 506 | 507 | Index *create_index() { 508 | Index *block = new Index; 509 | storage->record(block); 510 | return block; 511 | } 512 | 513 | OperationResult insert(const K &k, const V &v) { 514 | if (!root_idx()) root_idx() = create_leaf()->idx; 515 | auto root = storage->get(root_idx()); 516 | bool result = storage->get(root_idx())->insert(k, v); 517 | if (!result) return OperationResult::Duplicated; 518 | if (root->should_split()) { 519 | K k; 520 | Block *next = root->split(k); 521 | Block *prev = root; 522 | Index *idx = create_index(); 523 | idx->children.append(prev->idx); 524 | idx->children.append(next->idx); 525 | idx->keys.append(k); 526 | root_idx() = idx->idx; 527 | } 528 | ++storage->persistence_index->size; 529 | storage->swap_out_pages(); 530 | return OperationResult::Success; 531 | } 532 | 533 | bool remove(const K &k) { 534 | if (!root_idx()) return false; 535 | auto root = storage->get(root_idx()); 536 | bool result = storage->get(root_idx())->remove(k); 537 | if (!result) return false; 538 | if (root->keys.size == 0) { 539 | if (!root->is_leaf()) { 540 | Index *prev_root = Block::into_index(root); 541 | root_idx() = prev_root->children[0]; 542 | storage->deregister(prev_root); 543 | delete prev_root; 544 | } 545 | } 546 | --storage->persistence_index->size; 547 | storage->swap_out_pages(); 548 | return true; 549 | } 550 | 551 | unsigned size() const { return storage->persistence_index->size; } 552 | 553 | unsigned count(const K &k) { return query(k) ? 1 : 0; } 554 | 555 | void debug(Block *block) { 556 | std::cerr << "Block ID: " << block->idx << " "; 557 | if (block->is_leaf()) std::cerr << "(Leaf)" << std::endl; 558 | else std::cerr << "(Index)" << std::endl; 559 | if (block->is_leaf()) { 560 | Leaf *leaf = Block::into_leaf(block); 561 | for (int i = 0; i < leaf->keys.size; i++) { 562 | std::cerr << leaf->keys[i] << "=" << leaf->data[i] << " "; 563 | } 564 | std::cerr << std::endl; 565 | } else { 566 | Index *index = Block::into_index(block); 567 | for (int i = 0; i < index->keys.size; i++) { 568 | std::cerr << index->keys[i] << " "; 569 | } 570 | std::cerr << std::endl; 571 | for (int i = 0; i < index->children.size; i++) { 572 | std::cerr << index->children[i] << " "; 573 | } 574 | std::cerr << std::endl; 575 | for (int i = 0; i < index->children.size; i++) { 576 | debug(storage->get(index->children[i])); 577 | } 578 | } 579 | } 580 | 581 | OperationResult erase(const K &k) { 582 | if (remove(k)) return OperationResult::Success; 583 | else return OperationResult::Fail; 584 | } 585 | }; 586 | 587 | #endif //BPLUSTREE_BTREE_HPP 588 | -------------------------------------------------------------------------------- /src/BTree_Index_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-25. 3 | // 4 | 5 | #include 6 | #include "BTree.hpp" 7 | 8 | using Map = BTree; 9 | 10 | TEST_CASE("Index", "[BTree]") { 11 | Map::BPersistence *storage = new Map::BPersistence; 12 | SECTION("should split") { 13 | Map::Index idx; 14 | storage->record(&idx); 15 | idx.children.append(0); 16 | idx.insert_block(1, 1); 17 | idx.insert_block(3, 3); 18 | idx.insert_block(2, 2); 19 | idx.insert_block(4, 4); 20 | int k; 21 | Map::Index *that = idx.split(k); 22 | REQUIRE (k == 2); 23 | REQUIRE (idx.keys.size == 1); 24 | REQUIRE (idx.keys[0] == 1); 25 | REQUIRE (that->keys.size == 2); 26 | REQUIRE (that->keys[0] == 3); 27 | REQUIRE (that->keys[1] == 4); 28 | 29 | REQUIRE (idx.children[0] == 0); 30 | REQUIRE (idx.children[1] == 1); 31 | REQUIRE (that->children[0] == 2); 32 | REQUIRE (that->children[1] == 3); 33 | REQUIRE (that->children[2] == 4); 34 | } 35 | 36 | SECTION("should borrow from left") { 37 | Map::Index idx1; 38 | storage->record(&idx1); 39 | idx1.children.append(0); 40 | idx1.insert_block(1, 1); 41 | idx1.insert_block(2, 2); 42 | idx1.insert_block(3, 3); 43 | 44 | Map::Index idx2; 45 | storage->record(&idx2); 46 | idx2.children.append(4); 47 | idx2.insert_block(5, 5); 48 | idx2.insert_block(6, 6); 49 | idx2.insert_block(7, 7); 50 | 51 | REQUIRE (idx2.borrow_from_left(&idx1, 4) == 3); 52 | REQUIRE (idx1.keys.size == 2); 53 | REQUIRE (idx1.children.size == 3); 54 | REQUIRE (idx2.keys.size == 4); 55 | REQUIRE (idx2.children.size == 5); 56 | int idx1_keys_should_be[] = {1, 2}; 57 | for (int i = 0; i < idx1.keys.size; i++) REQUIRE(idx1.keys[i] == idx1_keys_should_be[i]); 58 | int idx1_children_should_be[] = {0, 1, 2}; 59 | for (int i = 0; i < idx1.children.size; i++) REQUIRE(idx1.children[i] == idx1_children_should_be[i]); 60 | int idx2_keys_should_be[] = {4, 5, 6, 7}; 61 | for (int i = 0; i < idx2.keys.size; i++) REQUIRE(idx2.keys[i] == idx2_keys_should_be[i]); 62 | int idx2_children_should_be[] = {3, 4, 5, 6, 7}; 63 | for (int i = 0; i < idx2.children.size; i++) REQUIRE(idx2.children[i] == idx2_children_should_be[i]); 64 | } 65 | 66 | SECTION("should borrow from right") { 67 | Map::Index idx1; 68 | storage->record(&idx1); 69 | idx1.children.append(0); 70 | idx1.insert_block(1, 1); 71 | idx1.insert_block(2, 2); 72 | idx1.insert_block(3, 3); 73 | 74 | Map::Index idx2; 75 | storage->record(&idx2); 76 | idx2.children.append(4); 77 | idx2.insert_block(5, 5); 78 | idx2.insert_block(6, 6); 79 | idx2.insert_block(7, 7); 80 | 81 | REQUIRE (idx1.borrow_from_right(&idx2, 4) == 5); 82 | REQUIRE (idx1.keys.size == 4); 83 | REQUIRE (idx1.children.size == 5); 84 | REQUIRE (idx2.keys.size == 2); 85 | REQUIRE (idx2.children.size == 3); 86 | int idx1_keys_should_be[] = {1, 2, 3, 4}; 87 | for (int i = 0; i < idx1.keys.size; i++) REQUIRE(idx1.keys[i] == idx1_keys_should_be[i]); 88 | int idx1_children_should_be[] = {0, 1, 2, 3, 4}; 89 | for (int i = 0; i < idx1.children.size; i++) REQUIRE(idx1.children[i] == idx1_children_should_be[i]); 90 | int idx2_keys_should_be[] = {6, 7}; 91 | for (int i = 0; i < idx2.keys.size; i++) REQUIRE(idx2.keys[i] == idx2_keys_should_be[i]); 92 | int idx2_children_should_be[] = {5, 6, 7}; 93 | for (int i = 0; i < idx2.children.size; i++) REQUIRE(idx2.children[i] == idx2_children_should_be[i]); 94 | } 95 | 96 | SECTION("should merge with left") { 97 | Map::Index idx1; 98 | storage->record(&idx1); 99 | idx1.children.append(0); 100 | idx1.insert_block(1, 1); 101 | idx1.insert_block(2, 2); 102 | 103 | Map::Index idx2; 104 | storage->record(&idx2); 105 | idx2.children.append(3); 106 | idx2.insert_block(4, 4); 107 | 108 | idx2.merge_with_left(&idx1, 3); 109 | REQUIRE (idx1.keys.size == 0); 110 | REQUIRE (idx1.children.size == 0); 111 | REQUIRE (idx2.keys.size == 4); 112 | REQUIRE (idx2.children.size == 5); 113 | int idx2_keys_should_be[] = {1, 2, 3, 4}; 114 | for (int i = 0; i < idx2.keys.size; i++) REQUIRE(idx2.keys[i] == idx2_keys_should_be[i]); 115 | int idx2_children_should_be[] = {0, 1, 2, 3, 4}; 116 | for (int i = 0; i < idx2.children.size; i++) REQUIRE(idx2.children[i] == idx2_children_should_be[i]); 117 | } 118 | 119 | SECTION("should merge with right") { 120 | Map::Index idx1; 121 | storage->record(&idx1); 122 | idx1.children.append(0); 123 | idx1.insert_block(1, 1); 124 | idx1.insert_block(2, 2); 125 | 126 | Map::Index idx2; 127 | storage->record(&idx2); 128 | idx2.children.append(3); 129 | idx2.insert_block(4, 4); 130 | 131 | idx1.merge_with_right(&idx2, 3); 132 | REQUIRE (idx1.keys.size == 4); 133 | REQUIRE (idx1.children.size == 5); 134 | REQUIRE (idx2.keys.size == 0); 135 | REQUIRE (idx2.children.size == 0); 136 | int idx1_keys_should_be[] = {1, 2, 3, 4}; 137 | for (int i = 0; i < idx1.keys.size; i++) REQUIRE(idx1.keys[i] == idx1_keys_should_be[i]); 138 | int idx1_children_should_be[] = {0, 1, 2, 3, 4}; 139 | for (int i = 0; i < idx1.children.size; i++) REQUIRE(idx1.children[i] == idx1_children_should_be[i]); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/BTree_Iterator_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-06-08. 3 | // 4 | 5 | #include 6 | #include "BTree.hpp" 7 | #include 8 | 9 | using Map = BTree; 10 | 11 | TEST_CASE("Iterator", "[Iterator]") { 12 | SECTION("should get data") { 13 | BTree m; 14 | const int test_size = 100000; 15 | int test_data[test_size]; 16 | for (int i = 0; i < test_size; i++) { 17 | m.insert(i, i); 18 | } 19 | auto iter = m.begin(); 20 | for (int i = 0; i < test_size; i++) { 21 | REQUIRE (*iter == i); 22 | ++iter; 23 | } 24 | } 25 | 26 | SECTION("should get data after removal") { 27 | BTree m; 28 | const int test_size = 100000; 29 | const int remove_size = 50000; 30 | int test_data[test_size]; 31 | for (int i = 0; i < test_size; i++) { 32 | m.insert(i, i); 33 | } 34 | for (int i = 0; i < remove_size; i++) { 35 | m.remove(i); 36 | } 37 | auto iter = m.begin(); 38 | for (int i = remove_size; i < test_size; i++) { 39 | REQUIRE (*iter == i); 40 | ++iter; 41 | } 42 | } 43 | 44 | SECTION("should equal") { 45 | const int test_size = 100; 46 | Map m; 47 | for (int i = 0; i < test_size; i++) { 48 | m.insert(i, i); 49 | } 50 | REQUIRE (m.begin() == m.begin()); 51 | REQUIRE (m.end() == m.end()); 52 | REQUIRE (m.cbegin() == m.cbegin()); 53 | REQUIRE (m.cend() == m.cend()); 54 | } 55 | 56 | SECTION("should access data sequentially") { 57 | const int test_size = 1000; 58 | Map m; 59 | for (int i = 0; i < test_size; i++) { 60 | m.insert(i, i); 61 | } 62 | int i = 0; 63 | auto iter1 = m.begin(); 64 | auto iter2 = m.begin(); 65 | auto iter3 = m.cbegin(); 66 | auto iter4 = m.cbegin(); 67 | 68 | for (; iter1 != m.end(); 69 | ++iter1, iter2++, ++iter3, iter4++, ++i) { 70 | REQUIRE (*iter1 == i); 71 | REQUIRE (*iter2 == i); 72 | REQUIRE (*iter3 == i); 73 | REQUIRE (*iter4 == i); 74 | REQUIRE (iter1 == iter2); 75 | REQUIRE (iter3 == iter4); 76 | } 77 | 78 | --iter1; 79 | --iter2; 80 | --iter3; 81 | --iter4; 82 | --i; 83 | for (; iter1 != m.begin(); 84 | --iter1, iter2--, --iter3, iter4--, --i) { 85 | REQUIRE (*iter1 == i); 86 | REQUIRE (*iter2 == i); 87 | REQUIRE (*iter3 == i); 88 | REQUIRE (*iter4 == i); 89 | REQUIRE (iter1 == iter2); 90 | REQUIRE (iter3 == iter4); 91 | } 92 | } 93 | 94 | SECTION("should access data sequentially with offload") { 95 | const int test_size = 10000; 96 | remove("iterator.test"); 97 | { 98 | Map m("iterator.test"); 99 | for (int i = 0; i < test_size; i++) { 100 | m.insert(i, i); 101 | } 102 | int i = 0; 103 | auto iter1 = m.begin(); 104 | auto iter2 = m.begin(); 105 | auto iter3 = m.cbegin(); 106 | auto iter4 = m.cbegin(); 107 | 108 | for (; iter1 != m.end(); 109 | ++iter1, iter2++, ++iter3, iter4++, ++i) { 110 | REQUIRE (*iter1 == i); 111 | REQUIRE (*iter2 == i); 112 | REQUIRE (*iter3 == i); 113 | REQUIRE (*iter4 == i); 114 | REQUIRE (iter1 == iter2); 115 | REQUIRE (iter3 == iter4); 116 | } 117 | 118 | --iter1; 119 | --iter2; 120 | --iter3; 121 | --iter4; 122 | --i; 123 | for (; iter1 != m.begin(); 124 | --iter1, iter2--, --iter3, iter4--, --i) { 125 | REQUIRE (*iter1 == i); 126 | REQUIRE (*iter2 == i); 127 | REQUIRE (*iter3 == i); 128 | REQUIRE (*iter4 == i); 129 | REQUIRE (iter1 == iter2); 130 | REQUIRE (iter3 == iter4); 131 | } 132 | CHECK(m.storage->stat.swap_out > 10); 133 | for (int i = 0; i < test_size; i++) { 134 | REQUIRE (*m.query(i) == i); 135 | } 136 | } 137 | 138 | { 139 | Map m("iterator.test"); 140 | int i = 0; 141 | auto iter1 = m.begin(); 142 | auto iter2 = m.begin(); 143 | auto iter3 = m.cbegin(); 144 | auto iter4 = m.cbegin(); 145 | 146 | for (; iter1 != m.end(); 147 | ++iter1, iter2++, ++iter3, iter4++, ++i) { 148 | REQUIRE (*iter1 == i); 149 | REQUIRE (*iter2 == i); 150 | REQUIRE (*iter3 == i); 151 | REQUIRE (*iter4 == i); 152 | REQUIRE (iter1 == iter2); 153 | REQUIRE (iter3 == iter4); 154 | } 155 | 156 | --iter1; 157 | --iter2; 158 | --iter3; 159 | --iter4; 160 | --i; 161 | for (; iter1 != m.begin(); 162 | --iter1, iter2--, --iter3, iter4--, --i) { 163 | REQUIRE (*iter1 == i); 164 | REQUIRE (*iter2 == i); 165 | REQUIRE (*iter3 == i); 166 | REQUIRE (*iter4 == i); 167 | REQUIRE (iter1 == iter2); 168 | REQUIRE (iter3 == iter4); 169 | } 170 | CHECK(m.storage->stat.swap_out > 10); 171 | for (int i = 0; i < test_size; i++) { 172 | REQUIRE (*m.query(i) == i); 173 | REQUIRE (*m.find(i) == i); 174 | } 175 | } 176 | 177 | remove("iterator.test"); 178 | } 179 | 180 | SECTION("should access data after remove with offload") { 181 | remove("iterator.db"); 182 | const int test_size = 100000; 183 | const int remove_size = 50000; 184 | int test_data[test_size]; 185 | for (int i = 0; i < test_size; i++) test_data[i] = i; 186 | for (int i = test_size - 1; i > 0; i--) { 187 | int j = rand() % (i + 1); 188 | std::swap(test_data[i], test_data[j]); 189 | } 190 | { 191 | BTree m("iterator.db"); 192 | for (int i = 0; i < test_size; i++) { 193 | m.insert(i, i); 194 | } 195 | } 196 | 197 | { 198 | BTree m("iterator.db"); 199 | for (int i = 0; i < remove_size; i++) { 200 | m.remove(test_data[i]); 201 | } 202 | } 203 | 204 | { 205 | BTree m("iterator.db"); 206 | auto iter = m.begin(); 207 | for (int i = remove_size; i < test_size; i++) { 208 | REQUIRE (*m.query(test_data[i]) == test_data[i]); 209 | ++iter; 210 | } 211 | } 212 | remove("iterator.db"); 213 | } 214 | } 215 | 216 | -------------------------------------------------------------------------------- /src/BTree_Leaf_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-25. 3 | // 4 | 5 | #include 6 | #include "BTree.hpp" 7 | 8 | using Map = BTree; 9 | 10 | TEST_CASE("Leaf", "[Leaf]") { 11 | Map::BPersistence *storage = new Map::BPersistence; 12 | SECTION("should insert and query") { 13 | Map::Leaf leaf; 14 | storage->record(&leaf); 15 | leaf.insert(3, 3); 16 | leaf.insert(2, 2); 17 | leaf.insert(1, 1); 18 | leaf.insert(4, 4); 19 | REQUIRE(*leaf.query(1) == 1); 20 | REQUIRE(*leaf.query(2) == 2); 21 | REQUIRE(*leaf.query(3) == 3); 22 | REQUIRE(*leaf.query(4) == 4); 23 | REQUIRE(leaf.query(5) == nullptr); 24 | } 25 | 26 | SECTION("should split") { 27 | Map::Leaf leaf; 28 | storage->record(&leaf); 29 | leaf.insert(3, 3); 30 | leaf.insert(2, 2); 31 | leaf.insert(1, 1); 32 | leaf.insert(4, 4); 33 | REQUIRE(leaf.should_split()); 34 | int k; 35 | Map::Leaf *that = leaf.split(k); 36 | REQUIRE(k == 3); 37 | REQUIRE(*leaf.query(1) == 1); 38 | REQUIRE(*leaf.query(2) == 2); 39 | REQUIRE(*that->query(3) == 3); 40 | REQUIRE(*that->query(4) == 4); 41 | REQUIRE(leaf.query(3) == nullptr); 42 | REQUIRE(leaf.query(4) == nullptr); 43 | REQUIRE(that->query(1) == nullptr); 44 | REQUIRE(that->query(2) == nullptr); 45 | REQUIRE(leaf.next == that->idx); 46 | REQUIRE(that->prev == leaf.idx); 47 | } 48 | 49 | SECTION("should borrow from left") { 50 | Map::Leaf leaf1, leaf2; 51 | storage->record(&leaf1); 52 | storage->record(&leaf2); 53 | leaf1.insert(1, 1); 54 | leaf1.insert(2, 2); 55 | leaf2.insert(3, 3); 56 | leaf2.insert(4, 4); 57 | leaf1.next = leaf2.idx; 58 | leaf2.prev = leaf1.idx; 59 | REQUIRE(leaf2.borrow_from_left(&leaf1, 0) == 2); 60 | REQUIRE(leaf1.keys.size == 1); 61 | REQUIRE(leaf1.data.size == 1); 62 | REQUIRE(leaf2.keys.size == 3); 63 | REQUIRE(leaf2.data.size == 3); 64 | REQUIRE(*leaf1.query(1) == 1); 65 | REQUIRE(*leaf2.query(2) == 2); 66 | REQUIRE(leaf2.keys[0] == 2); 67 | REQUIRE(leaf2.keys[1] == 3); 68 | REQUIRE(leaf2.keys[2] == 4); 69 | REQUIRE(*leaf2.query(3) == 3); 70 | REQUIRE(*leaf2.query(4) == 4); 71 | } 72 | 73 | SECTION("should borrow from right") { 74 | Map::Leaf leaf1, leaf2; 75 | storage->record(&leaf1); 76 | storage->record(&leaf2); 77 | leaf1.insert(1, 1); 78 | leaf1.insert(2, 2); 79 | leaf2.insert(3, 3); 80 | leaf2.insert(4, 4); 81 | leaf1.next = leaf2.idx; 82 | leaf2.prev = leaf1.idx; 83 | REQUIRE(leaf1.borrow_from_right(&leaf2, 0) == 4); 84 | REQUIRE(leaf1.keys.size == 3); 85 | REQUIRE(leaf2.keys.size == 1); 86 | REQUIRE(leaf1.data.size == 3); 87 | REQUIRE(leaf2.data.size == 1); 88 | REQUIRE(*leaf1.query(1) == 1); 89 | REQUIRE(*leaf1.query(2) == 2); 90 | REQUIRE(*leaf1.query(3) == 3); 91 | REQUIRE(*leaf2.query(4) == 4); 92 | } 93 | 94 | SECTION("should merge with left") { 95 | Map::Leaf leaf1, leaf2; 96 | storage->record(&leaf1); 97 | storage->record(&leaf2); 98 | leaf1.insert(1, 1); 99 | leaf1.insert(2, 2); 100 | leaf2.insert(3, 3); 101 | leaf2.insert(4, 4); 102 | leaf1.next = leaf2.idx; 103 | leaf2.prev = leaf1.idx; 104 | leaf2.merge_with_left(&leaf1, 0); 105 | REQUIRE(leaf1.keys.size == 0); 106 | REQUIRE(leaf1.data.size == 0); 107 | REQUIRE(leaf2.keys.size == 4); 108 | REQUIRE(leaf2.data.size == 4); 109 | REQUIRE(*leaf2.query(1) == 1); 110 | REQUIRE(*leaf2.query(2) == 2); 111 | REQUIRE(*leaf2.query(3) == 3); 112 | REQUIRE(*leaf2.query(4) == 4); 113 | } 114 | 115 | SECTION("should merge with right") { 116 | Map::Leaf leaf1, leaf2; 117 | storage->record(&leaf1); 118 | storage->record(&leaf2); 119 | leaf1.insert(1, 1); 120 | leaf1.insert(2, 2); 121 | leaf2.insert(3, 3); 122 | leaf2.insert(4, 4); 123 | leaf1.next = leaf2.idx; 124 | leaf2.prev = leaf1.idx; 125 | leaf1.merge_with_right(&leaf2, 0); 126 | REQUIRE(leaf1.keys.size == 4); 127 | REQUIRE(leaf1.data.size == 4); 128 | REQUIRE(leaf2.keys.size == 0); 129 | REQUIRE(leaf2.data.size == 0); 130 | REQUIRE(*leaf1.query(1) == 1); 131 | REQUIRE(*leaf1.query(2) == 2); 132 | REQUIRE(*leaf1.query(3) == 3); 133 | REQUIRE(*leaf1.query(4) == 4); 134 | } 135 | 136 | SECTION("should remove") { 137 | Map::Leaf leaf; 138 | storage->record(&leaf); 139 | leaf.insert(1, 1); 140 | leaf.insert(2, 2); 141 | leaf.insert(3, 3); 142 | leaf.insert(4, 4); 143 | leaf.remove(1); 144 | REQUIRE(leaf.keys.size == 3); 145 | REQUIRE(leaf.data.size == 3); 146 | REQUIRE(leaf.query(1) == nullptr); 147 | REQUIRE(!leaf.remove(1)); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/BTree_Storage_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-25. 3 | // 4 | 5 | #include 6 | #include "BTree.hpp" 7 | #include 8 | #include 9 | 10 | using Map = BTree; 11 | using BigMap = BTree; 12 | using BigLimitedMap = BTree; 13 | 14 | TEST_CASE("Storage", "[Storage]") { 15 | SECTION("should persist data") { 16 | remove("persist.db"); 17 | const int test_size = 16; 18 | { 19 | Map m("persist.db"); 20 | for (int i = 0; i < test_size; i++) { 21 | m.insert(i, i); 22 | } 23 | } 24 | { 25 | Map m("persist.db"); 26 | for (int i = 0; i < test_size; i++) { 27 | REQUIRE (m.query(i)); 28 | REQUIRE (*m.query(i) == i); 29 | m.remove(i); 30 | } 31 | } 32 | { 33 | Map m("persist.db"); 34 | for (int i = 0; i < test_size; i++) { 35 | REQUIRE (m.query(i) == nullptr); 36 | } 37 | } 38 | remove("persist.db"); 39 | } 40 | 41 | SECTION("should persist size") { 42 | remove("persist.db"); 43 | const int test_size = 16; 44 | { 45 | Map m("persist.db"); 46 | for (int i = 0; i < test_size; i++) { 47 | m.insert(i, i); 48 | } 49 | } 50 | { 51 | Map m("persist.db"); 52 | REQUIRE (m.size() == test_size); 53 | } 54 | remove("persist.db"); 55 | } 56 | 57 | SECTION("should persist data when root is offloaded") { 58 | remove("persist.db"); 59 | const int test_size = 16; 60 | { 61 | Map m("persist.db"); 62 | for (int i = 0; i < test_size; i++) { 63 | if (m.storage->is_loaded((m.root_idx()))) m.storage->offload_page(m.root_idx()); 64 | m.insert(i, i); 65 | } 66 | } 67 | { 68 | Map m("persist.db"); 69 | for (int i = 0; i < test_size; i++) { 70 | if (m.storage->is_loaded((m.root_idx()))) m.storage->offload_page(m.root_idx()); 71 | REQUIRE (m.query(i)); 72 | REQUIRE (*m.query(i) == i); 73 | m.remove(i); 74 | } 75 | } 76 | { 77 | Map m("persist.db"); 78 | for (int i = 0; i < test_size; i++) { 79 | if (m.storage->is_loaded((m.root_idx()))) m.storage->offload_page(m.root_idx()); 80 | REQUIRE (m.query(i) == nullptr); 81 | } 82 | } 83 | remove("persist.db"); 84 | } 85 | 86 | SECTION("should persist even more data") { 87 | const int test_size = 100000; 88 | remove("persist_long_long.db"); 89 | { 90 | BigMap m("persist_long_long.db"); 91 | for (int i = 0; i < test_size; i++) { 92 | m.insert(i, i); 93 | } 94 | } 95 | { 96 | BigMap m("persist_long_long.db"); 97 | for (int i = 0; i < test_size; i++) { 98 | REQUIRE (*m.query(i) == i); 99 | m.remove(i); 100 | } 101 | } 102 | { 103 | BigMap m("persist_long_long.db"); 104 | for (int i = 0; i < test_size; i++) { 105 | REQUIRE (m.query(i) == nullptr); 106 | } 107 | } 108 | remove("persist_long_long.db"); 109 | } 110 | 111 | SECTION("should persist even more data when memory is small") { 112 | const int test_size = 100000; 113 | remove("persist_long_long.db"); 114 | { 115 | BigLimitedMap m("persist_long_long.db"); 116 | for (int i = 0; i < test_size; i++) { 117 | m.insert(i, i); 118 | } 119 | } 120 | { 121 | BigLimitedMap m("persist_long_long.db"); 122 | for (int i = 0; i < test_size; i++) { 123 | REQUIRE (*m.query(i) == i); 124 | m.remove(i); 125 | } 126 | } 127 | { 128 | BigLimitedMap m("persist_long_long.db"); 129 | for (int i = 0; i < test_size; i++) { 130 | m.insert(i, i); 131 | } 132 | } 133 | { 134 | BigLimitedMap m("persist_long_long.db"); 135 | for (int i = 0; i < test_size; i++) { 136 | REQUIRE (*m.query(i) == i); 137 | m.remove(i); 138 | } 139 | } 140 | { 141 | BigLimitedMap m("persist_long_long.db"); 142 | for (int i = 0; i < test_size; i++) { 143 | REQUIRE (m.query(i) == nullptr); 144 | } 145 | } 146 | remove("persist_long_long.db"); 147 | } 148 | 149 | 150 | SECTION("should use empty slot") { 151 | const int test_size = 100000; 152 | remove("persist_long_long.db"); 153 | { 154 | BigLimitedMap m("persist_long_long.db"); 155 | for (int i = 0; i < test_size; i++) { 156 | m.insert(i, i); 157 | } 158 | unsigned long lst_page_use = 0; 159 | for (int i = 0; i < m.MaxPage(); i++) { 160 | if (m.storage->pages[i] != nullptr) 161 | lst_page_use = std::max(lst_page_use, m.storage->persistence_index->page_offset[i]); 162 | } 163 | for (int i = 0; i < test_size; i++) m.remove(i); 164 | for (int i = 0; i < test_size; i++) m.insert(i, i); 165 | for (int i = 0; i < test_size; i++) m.remove(i); 166 | for (int i = 0; i < test_size; i++) m.insert(i, i); 167 | unsigned long _lst_page_use = 0; 168 | for (int i = 0; i < m.MaxPage(); i++) { 169 | if (m.storage->pages[i] != nullptr) 170 | _lst_page_use = std::max(_lst_page_use, m.storage->persistence_index->page_offset[i]); 171 | } 172 | REQUIRE(_lst_page_use - lst_page_use <= 1000); 173 | } 174 | remove("persist_long_long.db"); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/BTree_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-23. 3 | // 4 | 5 | #include 6 | #include 7 | #include "BTree.hpp" 8 | 9 | using Map = BTree; 10 | 11 | TEST_CASE("BTree", "[BTree]") { 12 | SECTION("should use unit test settings") { 13 | BTree t; 14 | REQUIRE (t.MaxPage() == 1048576); 15 | } 16 | 17 | SECTION("should insert and query") { 18 | Map m; 19 | m.insert(3, 3); 20 | m.insert(2, 2); 21 | m.insert(1, 1); 22 | REQUIRE (*m.query(1) == 1); 23 | REQUIRE (*m.query(2) == 2); 24 | REQUIRE (*m.query(3) == 3); 25 | } 26 | 27 | SECTION("should handle duplicated key") { 28 | Map m; 29 | m.insert(3, 3); 30 | REQUIRE (m.insert(3, 3) == OperationResult::Duplicated); 31 | } 32 | 33 | SECTION("should insert and split") { 34 | Map m; 35 | m.insert(3, 3); 36 | m.insert(2, 2); 37 | m.insert(1, 1); 38 | REQUIRE (m.root()->is_leaf()); 39 | m.insert(4, 4); 40 | REQUIRE (*m.query(1) == 1); 41 | REQUIRE (*m.query(2) == 2); 42 | REQUIRE (*m.query(3) == 3); 43 | REQUIRE (*m.query(4) == 4); 44 | REQUIRE (!m.root()->is_leaf()); 45 | } 46 | 47 | SECTION("should insert and split to 3 levels") { 48 | Map m; 49 | for (int i = 20; i > 0; i--) { 50 | m.insert(i, i); 51 | } 52 | for (int i = 1; i <= 20; i++) { 53 | REQUIRE(m.query(i)); 54 | REQUIRE(*m.query(i) == i); 55 | } 56 | } 57 | 58 | SECTION("should handle more insert and query") { 59 | BTree m; 60 | const int test_size = 100000; 61 | int test_data[test_size]; 62 | for (int i = 0; i < test_size; i++) { 63 | test_data[i] = i; 64 | } 65 | for (int i = test_size - 1; i >= 0; i--) { 66 | int j = rand() % (i + 1); 67 | std::swap(test_data[i], test_data[j]); 68 | } 69 | for (int i = 0; i < test_size; i++) { 70 | m.insert(test_data[i], test_data[i]); 71 | } 72 | for (int i = 0; i < test_size; i++) { 73 | REQUIRE(m.query(i)); 74 | REQUIRE(i == *m.query(i)); 75 | } 76 | } 77 | 78 | SECTION("should remove and query") { 79 | BTree m; 80 | const int test_size = 16; 81 | int test_data[test_size]; 82 | for (int i = 0; i < test_size; i++) { 83 | test_data[i] = i; 84 | } 85 | for (int i = test_size - 1; i >= 0; i--) { 86 | int j = rand() % (i + 1); 87 | std::swap(test_data[i], test_data[j]); 88 | } 89 | for (int i = 0; i < test_size; i++) { 90 | m.insert(i, i); 91 | } 92 | for (int i = 0; i < test_size; i++) { 93 | REQUIRE (m.remove(test_data[i])); 94 | for (int j = 0; j < test_size; j++) { 95 | int idx = test_data[j]; 96 | const int *query_result = m.query(idx); 97 | if (j <= i) REQUIRE (query_result == nullptr); 98 | else { 99 | REQUIRE (query_result); 100 | REQUIRE (*query_result == idx); 101 | } 102 | } 103 | } 104 | } 105 | 106 | SECTION("should handle more remove and query") { 107 | BTree m; 108 | const int test_size = 100000; 109 | int test_data[test_size]; 110 | for (int i = 0; i < test_size; i++) { 111 | test_data[i] = i; 112 | } 113 | for (int i = test_size - 1; i >= 0; i--) { 114 | int j = rand() % (i + 1); 115 | std::swap(test_data[i], test_data[j]); 116 | } 117 | for (int i = 0; i < test_size; i++) { 118 | m.insert(i, i); 119 | } 120 | for (int i = 0; i < test_size; i++) { 121 | m.remove(test_data[i]); 122 | if (i % 10000 == 0) { 123 | for (int j = 0; j < test_size; j++) { 124 | const int *query_result = m.query(test_data[j]); 125 | if (j <= i) REQUIRE(query_result == nullptr); 126 | else { 127 | REQUIRE(query_result); 128 | REQUIRE(*query_result == test_data[j]); 129 | } 130 | } 131 | } 132 | } 133 | } 134 | 135 | SECTION("should handle more insert and query") { 136 | BTree m; 137 | const int test_size = 1000000; 138 | int *test_data = new int[test_size]; 139 | for (int i = 0; i < test_size; i++) { 140 | test_data[i] = i; 141 | } 142 | for (int i = test_size - 1; i >= 0; i--) { 143 | int j = rand() % (i + 1); 144 | std::swap(test_data[i], test_data[j]); 145 | } 146 | for (int i = 0; i < test_size; i++) { 147 | m.insert(test_data[i], test_data[i]); 148 | } 149 | for (int i = 0; i < test_size; i++) { 150 | REQUIRE(m.query(i)); 151 | REQUIRE(i == *m.query(i)); 152 | } 153 | delete[] test_data; 154 | } 155 | 156 | SECTION("should handle even more remove and query") { 157 | BTree m; 158 | const int test_size = 1000000; 159 | int *test_data = new int[test_size]; 160 | for (int i = 0; i < test_size; i++) { 161 | test_data[i] = i; 162 | } 163 | for (int i = test_size - 1; i >= 0; i--) { 164 | int j = rand() % (i + 1); 165 | std::swap(test_data[i], test_data[j]); 166 | } 167 | for (int i = 0; i < test_size; i++) { 168 | m.insert(i, i); 169 | } 170 | for (int i = 0; i < test_size; i++) { 171 | m.remove(test_data[i]); 172 | } 173 | for (int i = 0; i < test_size; i++) { 174 | REQUIRE (m.query(i) == nullptr); 175 | } 176 | delete[] test_data; 177 | } 178 | 179 | SECTION("should handle further more insert, remove and query") { 180 | BTree m; 181 | const int test_size = 10000; 182 | int *test_data = new int[test_size]; 183 | for (int i = 0; i < test_size; i++) { 184 | test_data[i] = i; 185 | } 186 | for (int i = test_size - 1; i >= 0; i--) { 187 | int j = rand() % (i + 1); 188 | std::swap(test_data[i], test_data[j]); 189 | } 190 | const int threshold = 5000; 191 | for (int i = 0; i < threshold; i++) { 192 | m.insert(test_data[i], test_data[i]); 193 | } 194 | for (int j = 0; j < 5; j++) { 195 | for (int i = threshold; i < test_size; i++) { 196 | m.insert(test_data[i], test_data[i]); 197 | } 198 | for (int i = threshold; i < test_size; i++) { 199 | m.remove(test_data[i]); 200 | } 201 | } 202 | for (int i = 0; i < test_size; i++) { 203 | if (i >= threshold) REQUIRE (m.query(test_data[i]) == nullptr); 204 | else 205 | REQUIRE (*m.query(test_data[i]) == test_data[i]); 206 | } 207 | delete[] test_data; 208 | } 209 | 210 | SECTION("should align key to 4K") { 211 | using BTreeInt = BTree; 212 | using BTreeLong = BTree; 213 | using BTreeChar = BTree; 214 | CHECK (BTreeInt::Index::Storage_Size() <= 4 * 1024); 215 | CHECK (BTreeLong::Index::Storage_Size() <= 4 * 1024); 216 | CHECK (BTreeChar::Index::Storage_Size() <= 4 * 1024); 217 | } 218 | } 219 | 220 | TEST_CASE("Serialize", "[BTree]") { 221 | SECTION("leaf should be serialized") { 222 | std::stringstream s; 223 | { 224 | Map::Leaf leaf; 225 | leaf.insert(3, 3); 226 | leaf.insert(2, 2); 227 | leaf.insert(1, 1); 228 | leaf.insert(4, 4); 229 | leaf.serialize(s); 230 | } 231 | { 232 | Map::Leaf leaf; 233 | leaf.deserialize(s); 234 | REQUIRE(*leaf.query(1) == 1); 235 | REQUIRE(*leaf.query(2) == 2); 236 | REQUIRE(*leaf.query(3) == 3); 237 | REQUIRE(*leaf.query(4) == 4); 238 | REQUIRE(leaf.query(5) == nullptr); 239 | } 240 | } 241 | 242 | SECTION("index should be serialized") { 243 | std::stringstream s; 244 | { 245 | Map::Index idx; 246 | idx.children.append(0); 247 | idx.insert_block(1, 1); 248 | idx.insert_block(3, 3); 249 | idx.insert_block(2, 2); 250 | idx.insert_block(4, 4); 251 | idx.serialize(s); 252 | } 253 | { 254 | Map::Index idx; 255 | idx.deserialize(s); 256 | REQUIRE (idx.keys.size == 4); 257 | REQUIRE (idx.keys[0] == 1); 258 | REQUIRE (idx.keys[1] == 2); 259 | REQUIRE (idx.keys[2] == 3); 260 | REQUIRE (idx.keys[3] == 4); 261 | REQUIRE (idx.children.size == 5); 262 | REQUIRE (idx.children[0] == 0); 263 | REQUIRE (idx.children[1] == 1); 264 | REQUIRE (idx.children[2] == 2); 265 | REQUIRE (idx.children[3] == 3); 266 | REQUIRE (idx.children[4] == 4); 267 | } 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /src/Container.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-24. 3 | // 4 | 5 | #ifndef BPLUSTREE_CONTAINER_HPP 6 | #define BPLUSTREE_CONTAINER_HPP 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #ifndef ONLINE_JUDGE 13 | 14 | #include "Persistence.hpp" 15 | 16 | #endif 17 | 18 | #ifdef ONLINE_JUDGE 19 | #define ALLOCATOR_DISABLE_MEMORY_ALIGN 20 | #endif 21 | 22 | template 23 | struct Allocator { 24 | U *allocate(unsigned size) { 25 | #ifdef ALLOCATOR_DISABLE_MEMORY_ALIGN 26 | return (U *) ::operator new(sizeof(U) * size); 27 | #else 28 | return (U *) ::operator new(sizeof(U) * size, (std::align_val_t) (4 * 1024)); 29 | #endif 30 | } 31 | 32 | void deallocate(U *x) { ::operator delete(x); } 33 | 34 | void construct(U *x, const U &d) { new(x) U(d); } 35 | 36 | void destruct(U *x) { x->~U(); } 37 | }; 38 | 39 | template 40 | class Vector : Serializable { 41 | Allocator a; 42 | public: 43 | T *x; 44 | unsigned size; 45 | 46 | static constexpr unsigned capacity() { return Cap; } 47 | 48 | Vector() : size(0) { 49 | x = a.allocate(capacity()); 50 | } 51 | 52 | virtual ~Vector() { 53 | for (int i = 0; i < size; i++) a.destruct(&x[i]); 54 | a.deallocate(x); 55 | } 56 | 57 | Vector(const Vector &) = delete; 58 | 59 | T &operator[](unsigned i) { 60 | assert(i < size); 61 | return x[i]; 62 | } 63 | 64 | const T &operator[](unsigned i) const { 65 | assert(i < size); 66 | return x[i]; 67 | } 68 | 69 | void append(const T &d) { 70 | assert(size < Cap); 71 | a.construct(&x[size++], d); 72 | } 73 | 74 | T pop() { 75 | assert(size > 0); 76 | T d = x[size - 1]; 77 | a.destruct(&x[--size]); 78 | return d; 79 | } 80 | 81 | void insert(unsigned pos, const T &d) { 82 | assert(pos >= 0 && pos <= size); 83 | assert(size < capacity()); 84 | memmove(x + pos + 1, x + pos, (size - pos) * sizeof(T)); 85 | a.construct(&x[pos], d); 86 | ++size; 87 | } 88 | 89 | void remove_range(unsigned pos, unsigned length = 1) { 90 | assert(size >= length); 91 | assert(pos < size); 92 | for (int i = pos; i < pos + length; i++) a.destruct(&x[i]); 93 | memmove(x + pos, x + pos + length, (size - length - pos) * sizeof(T)); 94 | size -= length; 95 | } 96 | 97 | T remove(unsigned pos) { 98 | assert(size > 0); 99 | assert(pos < size); 100 | T element = x[pos]; 101 | a.destruct(&x[pos]); 102 | memmove(x + pos, x + pos + 1, (size - 1 - pos) * sizeof(T)); 103 | size -= 1; 104 | return element; 105 | } 106 | 107 | void move_from(Vector &that, unsigned offset, unsigned length) { 108 | assert(size == 0); 109 | move_insert_from(that, offset, length, 0); 110 | } 111 | 112 | void move_insert_from(Vector &that, unsigned offset, unsigned length, unsigned at) { 113 | assert(at <= size); 114 | assert(offset + length <= that.size); 115 | assert(size + length <= capacity()); 116 | memmove(x + at + length, x + at, (size - at) * sizeof(T)); 117 | memcpy(x + at, that.x + offset, length * sizeof(T)); 118 | memmove(that.x + offset, that.x + offset + length, (that.size - length - offset) * sizeof(T)); 119 | that.size -= length; 120 | size += length; 121 | } 122 | 123 | unsigned storage_size() const { return Storage_Size(); }; 124 | 125 | /* 126 | * Storage Mapping 127 | * | 8 size | Cap() T x | 128 | */ 129 | void serialize(std::ostream &out) const { 130 | out.write(reinterpret_cast(&size), sizeof(size)); 131 | out.write(reinterpret_cast(x), sizeof(T) * capacity()); 132 | }; 133 | 134 | void deserialize(std::istream &in) { 135 | in.read(reinterpret_cast(&size), sizeof(size)); 136 | in.read(reinterpret_cast(x), sizeof(T) * capacity()); 137 | // WARNING: only applicable to primitive types because no data were constructed!!! 138 | }; 139 | 140 | static constexpr unsigned Storage_Size() { 141 | return sizeof(T) * capacity() + sizeof(unsigned); 142 | } 143 | }; 144 | 145 | template 146 | class Set : public Vector { 147 | public: 148 | unsigned bin_lower_bound(const T &d) const { 149 | // https://academy.realm.io/posts/how-we-beat-cpp-stl-binary-search/ 150 | unsigned low = 0, size = this->size; 151 | while (size > 0) { 152 | unsigned half = size / 2; 153 | unsigned other_half = size - half; 154 | unsigned probe = low + half; 155 | unsigned other_low = low + other_half; 156 | size = half; 157 | low = this->x[probe] < d ? other_low : low; 158 | } 159 | return low; 160 | } 161 | 162 | unsigned bin_upper_bound(const T &d) const { 163 | // https://academy.realm.io/posts/how-we-beat-cpp-stl-binary-search/ 164 | unsigned low = 0, size = this->size; 165 | while (size > 0) { 166 | unsigned half = size / 2; 167 | unsigned other_half = size - half; 168 | unsigned probe = low + half; 169 | unsigned other_low = low + other_half; 170 | size = half; 171 | low = this->x[probe] < d || this->x[probe] == d ? other_low : low; 172 | } 173 | return low; 174 | } 175 | 176 | unsigned linear_upper_bound(const T &d) const { 177 | for (unsigned i = 0; i < this->size; i++) { 178 | if (this->x[i] > d) return i; 179 | } 180 | return this->size; 181 | } 182 | 183 | unsigned linear_lower_bound(const T &d) const { 184 | for (unsigned i = 0; i < this->size; i++) { 185 | if (this->x[i] >= d) return i; 186 | } 187 | return this->size; 188 | } 189 | 190 | unsigned upper_bound(const T &d) const { 191 | if (Cap >= 16) return bin_upper_bound(d); else return linear_upper_bound(d); 192 | } 193 | unsigned lower_bound(const T &d) const { 194 | if (Cap >= 16) return bin_lower_bound(d); else return linear_lower_bound(d); 195 | } 196 | 197 | unsigned insert(const T &d) { 198 | unsigned pos = upper_bound(d); 199 | Vector::insert(pos, d); 200 | return pos; 201 | } 202 | }; 203 | 204 | #endif //BPLUSTREE_CONTAINER_HPP 205 | -------------------------------------------------------------------------------- /src/Container_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-24. 3 | // 4 | 5 | #include 6 | #include "Container.hpp" 7 | #include 8 | 9 | struct TestClass { 10 | static int construct_cnt; 11 | static int destruct_cnt; 12 | int x; 13 | 14 | TestClass(int x) : x(x) { ++construct_cnt; } 15 | 16 | TestClass(const TestClass &that) : x(that.x) { ++construct_cnt; } 17 | 18 | friend bool operator==(const TestClass &x, int y) { return x.x == y; } 19 | 20 | ~TestClass() { ++destruct_cnt; } 21 | }; 22 | 23 | int TestClass::construct_cnt = 0; 24 | int TestClass::destruct_cnt = 0; 25 | 26 | TEST_CASE("Vector", "[Vector]") { 27 | 28 | SECTION("should save data") { 29 | Vector v; 30 | v.append(1); 31 | v.append(2); 32 | v.append(3); 33 | v.append(4); 34 | v.append(5); 35 | v.append(6); 36 | v.append(7); 37 | v.append(8); 38 | REQUIRE (v[0] == 1); 39 | REQUIRE (v[1] == 2); 40 | REQUIRE (v[2] == 3); 41 | REQUIRE (v[3] == 4); 42 | REQUIRE (v[4] == 5); 43 | REQUIRE (v[5] == 6); 44 | REQUIRE (v[6] == 7); 45 | REQUIRE (v[7] == 8); 46 | } 47 | 48 | SECTION("should save data without default constructor", "[Vector]") { 49 | TestClass::construct_cnt = 0; 50 | TestClass::destruct_cnt = 0; 51 | { 52 | Vector v; 53 | v.append(TestClass(1)); 54 | v.append(TestClass(2)); 55 | v.append(TestClass(3)); 56 | v.append(TestClass(4)); 57 | v.append(TestClass(5)); 58 | v.append(TestClass(6)); 59 | v.append(TestClass(7)); 60 | v.append(TestClass(8)); 61 | REQUIRE (v[0] == 1); 62 | REQUIRE (v[1] == 2); 63 | REQUIRE (v[2] == 3); 64 | REQUIRE (v[3] == 4); 65 | REQUIRE (v[4] == 5); 66 | REQUIRE (v[5] == 6); 67 | REQUIRE (v[6] == 7); 68 | REQUIRE (v[7] == 8); 69 | REQUIRE (v.size == 8); 70 | } 71 | REQUIRE (TestClass::construct_cnt == 16); 72 | REQUIRE (TestClass::destruct_cnt == 16); 73 | } 74 | 75 | SECTION("should insert", "[Vector]") { 76 | Vector v; 77 | v.append(2); 78 | v.append(4); 79 | v.append(6); 80 | v.insert(0, 1); 81 | v.insert(2, 3); 82 | v.insert(5, 7); 83 | REQUIRE (v[0] == 1); 84 | REQUIRE (v[1] == 2); 85 | REQUIRE (v[2] == 3); 86 | REQUIRE (v[3] == 4); 87 | REQUIRE (v[4] == 6); 88 | REQUIRE (v[5] == 7); 89 | } 90 | 91 | SECTION("should pop", "[Vector]") { 92 | Vector v; 93 | v.append(2); 94 | v.append(4); 95 | v.append(6); 96 | REQUIRE (v.pop() == 6); 97 | REQUIRE (v[0] == 2); 98 | REQUIRE (v[1] == 4); 99 | } 100 | 101 | SECTION("should move from", "[Vector]") { 102 | Vector v; 103 | Vector b; 104 | v.append(1); 105 | v.append(2); 106 | v.append(3); 107 | v.append(4); 108 | v.append(5); 109 | v.append(6); 110 | v.append(7); 111 | b.move_from(v, 1, 3); 112 | REQUIRE (v[0] == 1); 113 | REQUIRE (v[1] == 5); 114 | REQUIRE (v[2] == 6); 115 | REQUIRE (v[3] == 7); 116 | REQUIRE (v.size == 4); 117 | REQUIRE (b[0] == 2); 118 | REQUIRE (b[1] == 3); 119 | REQUIRE (b[2] == 4); 120 | REQUIRE (b.size == 3); 121 | } 122 | 123 | SECTION("should move insert", "[Vector]") { 124 | Vector v; 125 | Vector b; 126 | v.append(1); 127 | v.append(2); 128 | v.append(3); 129 | v.append(4); 130 | v.append(5); 131 | v.append(6); 132 | v.append(7); 133 | b.append(-5); 134 | b.append(-4); 135 | b.append(-3); 136 | b.append(-2); 137 | b.append(-1); 138 | b.append(2333); 139 | b.move_insert_from(v, 1, 3, 2); 140 | REQUIRE (v[0] == 1); 141 | REQUIRE (v[1] == 5); 142 | REQUIRE (v[2] == 6); 143 | REQUIRE (v[3] == 7); 144 | REQUIRE (v.size == 4); 145 | REQUIRE (b[0] == -5); 146 | REQUIRE (b[1] == -4); 147 | REQUIRE (b[2] == 2); 148 | REQUIRE (b[3] == 3); 149 | REQUIRE (b[4] == 4); 150 | REQUIRE (b[5] == -3); 151 | REQUIRE (b[6] == -2); 152 | REQUIRE (b[7] == -1); 153 | REQUIRE (b[8] == 2333); 154 | REQUIRE (b.size == 9); 155 | } 156 | 157 | SECTION("should remove", "[Vector]") { 158 | Vector v; 159 | v.append(1); 160 | v.append(2); 161 | v.append(3); 162 | v.append(4); 163 | v.append(5); 164 | v.append(6); 165 | v.append(7); 166 | v.append(8); 167 | REQUIRE(v.remove(0) == 1); 168 | REQUIRE(v.remove(2) == 4); 169 | REQUIRE(v.remove(5) == 8); 170 | REQUIRE (v[0] == 2); 171 | REQUIRE (v[1] == 3); 172 | REQUIRE (v[2] == 5); 173 | REQUIRE (v[3] == 6); 174 | REQUIRE (v[4] == 7); 175 | REQUIRE (v.size == 5); 176 | v.remove_range(1, 3); 177 | REQUIRE (v[0] == 2); 178 | REQUIRE (v[1] == 7); 179 | REQUIRE (v.size == 2); 180 | } 181 | 182 | SECTION("should remove data without default constructor", "[Vector]") { 183 | TestClass::construct_cnt = 0; 184 | TestClass::destruct_cnt = 0; 185 | { 186 | Vector v; 187 | v.append(TestClass(1)); 188 | v.append(TestClass(2)); 189 | v.append(TestClass(3)); 190 | v.append(TestClass(4)); 191 | v.append(TestClass(5)); 192 | v.append(TestClass(6)); 193 | v.append(TestClass(7)); 194 | v.append(TestClass(8)); 195 | v.remove_range(0, 8); 196 | } 197 | REQUIRE (TestClass::construct_cnt == 16); 198 | REQUIRE (TestClass::destruct_cnt == 16); 199 | } 200 | 201 | SECTION("should be serialized") { 202 | std::stringstream s; 203 | const int test_num = 2333; 204 | using V = Vector; 205 | { 206 | V data; 207 | for (int i = 0; i < test_num; i++) { 208 | data.append(i); 209 | } 210 | data.serialize(s); 211 | } 212 | { 213 | V data; 214 | data.deserialize(s); 215 | REQUIRE(data.size == test_num); 216 | for (int i = 0; i < test_num; i++) { 217 | REQUIRE (data[i] == i); 218 | } 219 | } 220 | } 221 | 222 | SECTION("should have correct storage size") { 223 | using V = Vector; 224 | REQUIRE (V::Storage_Size() == 256 * sizeof(int) + sizeof(unsigned)); 225 | } 226 | } 227 | 228 | TEST_CASE("Set", "[Set]") { 229 | 230 | SECTION("should insert") { 231 | Set s; 232 | s.insert(3); 233 | s.insert(5); 234 | s.insert(4); 235 | s.insert(4); 236 | REQUIRE (s.insert(1) == 0); 237 | REQUIRE (s.insert(5) == 5); 238 | s.insert(1); 239 | REQUIRE (s[0] == 1); 240 | REQUIRE (s[1] == 1); 241 | REQUIRE (s[2] == 3); 242 | REQUIRE (s[3] == 4); 243 | REQUIRE (s[4] == 4); 244 | REQUIRE (s[5] == 5); 245 | REQUIRE (s[6] == 5); 246 | } 247 | 248 | SECTION("should calc bound") { 249 | Set s; 250 | s.insert(1); 251 | s.insert(1); 252 | s.insert(3); 253 | s.insert(3); 254 | s.insert(7); 255 | s.insert(7); 256 | REQUIRE (s.lower_bound(0) == 0); 257 | REQUIRE (s.lower_bound(1) == 0); 258 | REQUIRE (s.lower_bound(2) == 2); 259 | REQUIRE (s.lower_bound(3) == 2); 260 | REQUIRE (s.lower_bound(4) == 4); 261 | REQUIRE (s.lower_bound(5) == 4); 262 | REQUIRE (s.lower_bound(6) == 4); 263 | REQUIRE (s.lower_bound(7) == 4); 264 | REQUIRE (s.lower_bound(8) == 6); 265 | REQUIRE (s.upper_bound(0) == 0); 266 | REQUIRE (s.upper_bound(1) == 2); 267 | REQUIRE (s.upper_bound(2) == 2); 268 | REQUIRE (s.upper_bound(3) == 4); 269 | REQUIRE (s.upper_bound(4) == 4); 270 | REQUIRE (s.upper_bound(5) == 4); 271 | REQUIRE (s.upper_bound(6) == 4); 272 | REQUIRE (s.upper_bound(7) == 6); 273 | REQUIRE (s.upper_bound(8) == 6); 274 | } 275 | } 276 | 277 | TEST_CASE("Allocator", "[Allocator][!mayfail]") { 278 | SECTION("should align to 4K") { 279 | { 280 | Allocator alloc; 281 | auto addr = alloc.allocate(233); 282 | REQUIRE(reinterpret_cast(addr) % (4 * 1024) == 0); 283 | alloc.destruct(addr); 284 | } 285 | { 286 | Allocator alloc; 287 | auto addr = alloc.allocate(233); 288 | REQUIRE(reinterpret_cast(addr) % (4 * 1024) == 0); 289 | alloc.destruct(addr); 290 | } 291 | { 292 | Allocator alloc; 293 | auto addr = alloc.allocate(233); 294 | REQUIRE(reinterpret_cast(addr) % (4 * 1024) == 0); 295 | alloc.destruct(addr); 296 | } 297 | { 298 | Allocator alloc; 299 | auto addr = alloc.allocate(233); 300 | REQUIRE(reinterpret_cast(addr) % (4 * 1024) == 0); 301 | alloc.destruct(addr); 302 | } 303 | } 304 | } -------------------------------------------------------------------------------- /src/Iterator.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-06-08. 3 | // 4 | 5 | #ifndef BPLUSTREE_ITERATOR_HPP 6 | #define BPLUSTREE_ITERATOR_HPP 7 | 8 | template 9 | class Iterator { 10 | mutable BTree *tree; 11 | using BlockIdx = typename BTree::BlockIdx; 12 | BlockIdx leaf_idx; 13 | int pos; 14 | public: 15 | Iterator(BTree *tree, BlockIdx leaf_idx, int pos) : tree(tree), leaf_idx(leaf_idx), pos(pos) {} 16 | 17 | Iterator(const Iterator &that) : tree(that.tree), leaf_idx(that.leaf_idx), pos(that.pos) {} 18 | 19 | Iterator &operator++() { 20 | ++pos; 21 | if (pos == leaf()->keys.size && leaf()->next) { 22 | pos = 0; 23 | leaf_idx = leaf()->next; 24 | } 25 | expire(); 26 | return *this; 27 | } 28 | 29 | void expire() const { 30 | tree->storage->swap_out_pages(); 31 | } 32 | 33 | const Leaf *leaf() const { return reinterpret_cast(tree->storage->read(leaf_idx)); } 34 | 35 | Leaf *leaf_mut() { return Block::into_leaf(tree->storage->get(leaf_idx)); } 36 | 37 | Iterator &operator--() { 38 | --pos; 39 | if (pos < 0 && leaf()->prev) { 40 | leaf_idx = leaf()->prev; 41 | pos = leaf()->keys.size - 1; 42 | } 43 | expire(); 44 | return *this; 45 | } 46 | 47 | Iterator operator++(int) { 48 | auto _ = Iterator(*this); 49 | ++(*this); 50 | return _; 51 | } 52 | 53 | Iterator operator--(int) { 54 | auto _ = Iterator(*this); 55 | --(*this); 56 | return _; 57 | } 58 | 59 | V operator*() { 60 | return getValue(); 61 | } 62 | 63 | friend bool operator==(const Iterator &a, const Iterator &b) { 64 | return a.tree == b.tree && a.leaf_idx == b.leaf_idx && a.pos == b.pos; 65 | } 66 | 67 | friend bool operator!=(const Iterator &a, const Iterator &b) { 68 | return !(a == b); 69 | } 70 | 71 | V getValue() { 72 | expire(); 73 | return leaf_mut()->data[pos]; 74 | } 75 | 76 | void modify(const V &v) { 77 | expire(); 78 | leaf_mut()->data[pos] = v; 79 | } 80 | }; 81 | 82 | #endif //BPLUSTREE_ITERATOR_HPP 83 | -------------------------------------------------------------------------------- /src/LRU.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-30. 3 | // 4 | 5 | #ifndef BPLUSTREE_LRU_HPP 6 | #define BPLUSTREE_LRU_HPP 7 | 8 | #include 9 | 10 | template 11 | class LRU { 12 | struct Node { 13 | Idx idx; 14 | Node *prev, *next; 15 | 16 | Node(Idx idx) : idx(idx), prev(nullptr), next(nullptr) {} 17 | } *head, *tail; 18 | 19 | void push(Node* ptr) { 20 | if (size == 0) { head = tail = ptr; } 21 | else { 22 | ptr->next = head; 23 | head->prev = ptr; 24 | head = ptr; 25 | } 26 | ++size; 27 | } 28 | 29 | void remove(Node* ptr) { 30 | if (ptr->prev) ptr->prev->next = ptr->next; else { 31 | head = ptr->next; 32 | } 33 | if (ptr->next) ptr->next->prev = ptr->prev; else { 34 | tail = ptr->prev; 35 | } 36 | ptr->next = ptr->prev = nullptr; 37 | --size; 38 | } 39 | 40 | public: 41 | unsigned size; 42 | 43 | Node** nodes; 44 | 45 | LRU() : head(nullptr), tail(nullptr), size(0) { 46 | nodes = new Node*[Cap]; 47 | memset(nodes, 0, sizeof(Node*) * Cap); 48 | } 49 | 50 | ~LRU() { 51 | delete[] nodes; 52 | } 53 | 54 | void put(Idx idx) { 55 | assert(idx < Cap); 56 | assert(nodes[idx] == nullptr); 57 | Node *ptr = new Node(idx); 58 | push(ptr); 59 | nodes[idx] = ptr; 60 | } 61 | 62 | void get(Idx idx) { 63 | assert(idx < Cap); 64 | assert(nodes[idx]); 65 | remove(nodes[idx]); 66 | push(nodes[idx]); 67 | } 68 | 69 | Idx expire() { 70 | return tail->idx; 71 | } 72 | 73 | void remove(Idx idx) { 74 | assert(idx < Cap); 75 | assert(nodes[idx]); 76 | remove(nodes[idx]); 77 | delete nodes[idx]; 78 | nodes[idx] = nullptr; 79 | } 80 | 81 | void debug() { 82 | unsigned last_idx; 83 | for (auto ptr = head; ptr != nullptr; ptr = ptr->next) { 84 | if (ptr->idx == last_idx) { 85 | std::clog << "Cycle detected!" << std::endl; 86 | return; 87 | } 88 | std::clog << ptr->idx << " "; 89 | last_idx = ptr->idx; 90 | } 91 | std::clog << std::endl; 92 | } 93 | }; 94 | 95 | #endif //BPLUSTREE_LRU_HPP 96 | -------------------------------------------------------------------------------- /src/LRU_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-30. 3 | // 4 | 5 | #include 6 | #include 7 | #include "Persistence.hpp" 8 | #include "LRU.hpp" 9 | 10 | TEST_CASE("LRU", "[Persistence]") { 11 | SECTION("should expire correctly") { 12 | LRU<64> lru; 13 | lru.put(0); 14 | lru.put(1); 15 | lru.put(2); 16 | lru.put(3); 17 | REQUIRE(lru.expire() == 0); 18 | lru.remove(lru.expire()); 19 | lru.get(2); 20 | REQUIRE(lru.expire() == 1); 21 | lru.remove(lru.expire()); 22 | lru.get(3); 23 | REQUIRE(lru.expire() == 2); 24 | lru.remove(lru.expire()); 25 | REQUIRE(lru.expire() == 3); 26 | lru.remove(lru.expire()); 27 | REQUIRE(lru.size == 0); 28 | lru.put(0); 29 | REQUIRE(lru.expire() == 0); 30 | } 31 | } -------------------------------------------------------------------------------- /src/Persistence.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-29. 3 | // 4 | 5 | #ifndef BPLUSTREE_PERSISTENCE_HPP 6 | #define BPLUSTREE_PERSISTENCE_HPP 7 | 8 | #include 9 | #include 10 | #include 11 | #ifndef ONLINE_JUDGE 12 | #include "LRU.hpp" 13 | #endif 14 | 15 | class Serializable { 16 | public: 17 | virtual unsigned storage_size() const = 0; 18 | 19 | virtual void serialize(std::ostream &out) const = 0; 20 | 21 | virtual void deserialize(std::istream &in) = 0; 22 | 23 | static constexpr bool is_serializable() { return true; } 24 | }; 25 | 26 | template 27 | struct Persistence { 28 | const char *path; 29 | std::fstream f; 30 | 31 | static const unsigned VERSION = 6; 32 | 33 | struct PersistenceIndex { 34 | unsigned root_idx; 35 | unsigned magic_key; 36 | unsigned version; 37 | unsigned size; 38 | size_t tail_pos; 39 | size_t page_offset[MAX_PAGES]; 40 | size_t page_size[MAX_PAGES]; 41 | unsigned char is_leaf[MAX_PAGES]; 42 | 43 | static unsigned constexpr MAGIC_KEY() { 44 | return sizeof(Index) * 233 45 | + sizeof(Leaf) * 23333 46 | + MAX_PAGES * 23; 47 | } 48 | 49 | PersistenceIndex() : root_idx(0), magic_key(MAGIC_KEY()), 50 | version(VERSION), 51 | tail_pos(sizeof(PersistenceIndex)), 52 | size(0) { 53 | memset(page_offset, 0, sizeof(page_offset)); 54 | memset(is_leaf, 0, sizeof(is_leaf)); 55 | } 56 | } *persistence_index; 57 | 58 | Block **pages; 59 | bool *dirty; 60 | unsigned lst_empty_slot; 61 | 62 | struct Stat { 63 | long long create; 64 | long long destroy; 65 | long long access_cache_hit; 66 | long long access_cache_miss; 67 | long long dirty_write; 68 | long long swap_out; 69 | 70 | void stat() { 71 | printf(" access hit/total %lld/%lld %.5f%%\n", 72 | access_cache_hit, 73 | access_cache_miss + access_cache_hit, 74 | double(access_cache_hit) / (access_cache_miss + access_cache_hit) * 100); 75 | printf(" create/destroy %lld %lld\n", create, destroy); 76 | printf(" swap_in/out/dirty %lld %lld %lld %.5f%%\n", 77 | access_cache_miss, swap_out, dirty_write, 78 | double(dirty_write) / (swap_out) * 100); 79 | } 80 | 81 | Stat() : create(0), destroy(0), 82 | access_cache_hit(1), access_cache_miss(0), dirty_write(0), swap_out(0) {} 83 | } stat; 84 | 85 | using BLRU = LRU; 86 | BLRU lru; 87 | 88 | Persistence(const char *path = nullptr) : path(path), lst_empty_slot(16) { 89 | assert(Index::is_serializable()); 90 | assert(Leaf::is_serializable()); 91 | persistence_index = new PersistenceIndex; 92 | pages = new Block *[MAX_PAGES]; 93 | dirty = new bool[MAX_PAGES]; 94 | memset(pages, 0, sizeof(Block *) * MAX_PAGES); 95 | memset(dirty, 0, sizeof(bool) * MAX_PAGES); 96 | if (path) { 97 | f.open(path, std::ios::in | std::ios::out | std::ios::ate | std::ios::binary); 98 | if (f) 99 | restore(); 100 | else 101 | f.open(path, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary); 102 | } 103 | } 104 | 105 | ~Persistence() { 106 | save(); 107 | f.close(); 108 | delete[] dirty; 109 | delete[] pages; 110 | delete persistence_index; 111 | } 112 | 113 | void restore() { 114 | if (!path) return; 115 | f.seekg(0, f.beg); 116 | if (!f.read(reinterpret_cast(persistence_index), sizeof(PersistenceIndex))) { 117 | std::clog << "[Warning] failed to restore from " << path << " " << f.gcount() << std::endl; 118 | f.clear(); 119 | } 120 | assert(persistence_index->version == VERSION); 121 | assert(persistence_index->magic_key == PersistenceIndex::MAGIC_KEY()); 122 | } 123 | 124 | void offload_page(unsigned page_id) { 125 | if (!path) return; 126 | Block *page = pages[page_id]; 127 | 128 | if (dirty[page_id]) { 129 | auto offset = persistence_index->page_offset[page_id]; 130 | f.seekp(offset, f.beg); 131 | page->serialize(f); 132 | ++stat.dirty_write; 133 | } 134 | 135 | delete pages[page_id]; 136 | pages[page_id] = nullptr; 137 | 138 | lru.remove(page_id); 139 | } 140 | 141 | bool is_loaded(unsigned page_id) { return pages[page_id] != nullptr; } 142 | 143 | Block *load_page(unsigned page_id) { 144 | if (pages[page_id]) { 145 | ++stat.access_cache_hit; 146 | lru.get(page_id); 147 | return pages[page_id]; 148 | } 149 | if (!path) return nullptr; 150 | ++stat.access_cache_miss; 151 | Block *page; 152 | if (!persistence_index->page_offset[page_id]) return nullptr; 153 | if (persistence_index->is_leaf[page_id] == 1) 154 | page = new Leaf; 155 | else if (persistence_index->is_leaf[page_id] == 0) 156 | page = new Index; 157 | else 158 | assert(false); 159 | f.seekg(persistence_index->page_offset[page_id], f.beg); 160 | page->deserialize(f); 161 | pages[page_id] = page; 162 | page->storage = this; 163 | page->idx = page_id; 164 | lru.put(page_id); 165 | return page; 166 | } 167 | 168 | void save() { 169 | if (!path) return; 170 | f.seekp(0, f.beg); 171 | f.write(reinterpret_cast(persistence_index), sizeof(PersistenceIndex)); 172 | for (unsigned i = 0; i < MAX_PAGES; i++) { 173 | if (pages[i]) offload_page(i); 174 | } 175 | } 176 | 177 | const Block *read(unsigned page_id) { 178 | return load_page(page_id); 179 | } 180 | 181 | Block *get(unsigned page_id) { 182 | dirty[page_id] = true; 183 | return load_page(page_id); 184 | } 185 | 186 | size_t align_to_4k(size_t offset) { 187 | return (offset + 0xfff) & (~0xfff); 188 | } 189 | 190 | unsigned append_page(size_t &offset, size_t size) { 191 | offset = align_to_4k(persistence_index->tail_pos); 192 | persistence_index->tail_pos = offset + size; 193 | return lst_empty_slot++; 194 | } 195 | 196 | unsigned request_page(size_t &offset, size_t size) { 197 | for(;;lst_empty_slot++) { 198 | if (persistence_index->page_offset[lst_empty_slot] == 0) return append_page(offset, size); 199 | if (persistence_index->is_leaf[lst_empty_slot] == 2 && persistence_index->page_size[lst_empty_slot] >= size) { 200 | offset = persistence_index->page_offset[lst_empty_slot]; 201 | return lst_empty_slot++; 202 | } 203 | } 204 | } 205 | 206 | void create_page(Block *block) { 207 | size_t offset; 208 | unsigned page_id = request_page(offset, block->storage_size()); 209 | block->idx = page_id; 210 | pages[page_id] = block; 211 | persistence_index->is_leaf[page_id] = block->is_leaf() ? 1 : 0; 212 | persistence_index->page_offset[page_id] = offset; 213 | persistence_index->page_size[page_id] = block->storage_size(); 214 | lru.put(page_id); 215 | dirty[page_id] = true; 216 | } 217 | 218 | void swap_out_pages() { 219 | while (lru.size > MAX_IN_MEMORY) { 220 | unsigned idx = lru.expire(); 221 | offload_page(idx); 222 | ++stat.swap_out; 223 | } 224 | } 225 | 226 | void record(Block *block) { 227 | create_page(block); 228 | block->storage = this; 229 | ++stat.create; 230 | } 231 | 232 | void deregister(Block *block) { 233 | lru.remove(block->idx); 234 | pages[block->idx] = nullptr; 235 | dirty[block->idx] = false; 236 | lst_empty_slot = std::min(lst_empty_slot, block->idx); 237 | persistence_index->is_leaf[block->idx] = 2; 238 | block->idx = 0; 239 | ++stat.destroy; 240 | } 241 | }; 242 | 243 | #endif //BPLUSTREE_PERSISTENCE_HPP 244 | -------------------------------------------------------------------------------- /src/Persistence_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-29. 3 | // 4 | 5 | #include 6 | #include 7 | #include "Persistence.hpp" 8 | #include "Container.hpp" 9 | 10 | class MockBlock; 11 | 12 | class MockIndex; 13 | 14 | class MockLeaf; 15 | 16 | using MPersistence = Persistence; 17 | 18 | struct MockBlock : public Serializable { 19 | unsigned idx; 20 | MPersistence *storage; 21 | 22 | MockBlock() : storage(nullptr) {} 23 | 24 | virtual ~MockBlock() {} 25 | 26 | virtual bool is_leaf() const = 0; 27 | }; 28 | 29 | struct MockLeaf : public MockBlock { 30 | Vector data; 31 | 32 | virtual bool is_leaf() const override { return true; } 33 | 34 | unsigned storage_size() const override { return data.storage_size(); } 35 | 36 | void serialize(std::ostream &out) const override { this->data.serialize(out); }; 37 | 38 | void deserialize(std::istream &in) override { this->data.deserialize(in); } 39 | }; 40 | 41 | struct MockIndex : public MockLeaf { 42 | 43 | Vector data; 44 | 45 | virtual bool is_leaf() const override { return false; } 46 | 47 | unsigned storage_size() const override { return data.storage_size(); } 48 | 49 | void serialize(std::ostream &out) const override { this->data.serialize(out); }; 50 | 51 | void deserialize(std::istream &in) override { this->data.deserialize(in); } 52 | 53 | }; 54 | 55 | MockLeaf *make_leaf() { 56 | MockLeaf *leaf = new MockLeaf; 57 | for (int i = 0; i < leaf->data.capacity(); i++) leaf->data.append(i); 58 | return leaf; 59 | } 60 | 61 | TEST_CASE("Persistence", "[Persistence]") { 62 | SECTION("should occupy storage") { 63 | MPersistence persistence; 64 | MockLeaf *leaf1 = make_leaf(); 65 | persistence.record(leaf1); 66 | REQUIRE (leaf1->storage == &persistence); 67 | unsigned idx = leaf1->idx; 68 | persistence.offload_page(idx); 69 | leaf1 = dynamic_cast(persistence.get(idx)); 70 | REQUIRE (leaf1->storage == &persistence); 71 | } 72 | 73 | SECTION("should occupy different page id") { 74 | MPersistence persistence; 75 | MockLeaf leaf1; 76 | MockLeaf leaf2; 77 | persistence.record(&leaf1); 78 | persistence.record(&leaf2); 79 | REQUIRE(leaf1.idx != leaf2.idx); 80 | persistence.deregister(&leaf1); 81 | persistence.deregister(&leaf2); 82 | } 83 | 84 | SECTION("should offload pages") { 85 | MPersistence persistence; 86 | MockLeaf *leaf1 = new MockLeaf; 87 | MockLeaf *leaf2 = new MockLeaf; 88 | persistence.record(leaf1); 89 | persistence.record(leaf2); 90 | } 91 | 92 | SECTION("should get correct pages") { 93 | MPersistence persistence; 94 | MockLeaf *leaf1 = new MockLeaf; 95 | MockLeaf *leaf2 = new MockLeaf; 96 | persistence.record(leaf1); 97 | persistence.record(leaf2); 98 | REQUIRE(persistence.get(leaf1->idx) == leaf1); 99 | REQUIRE(persistence.get(leaf2->idx) == leaf2); 100 | } 101 | 102 | SECTION("should return null on not found pages") { 103 | MPersistence persistence; 104 | REQUIRE(persistence.get(5) == nullptr); 105 | } 106 | 107 | SECTION("should persist value") { 108 | remove("p1.test"); 109 | unsigned leaf_idx, index_idx; 110 | { 111 | MockLeaf *leaf = new MockLeaf; 112 | MockIndex *index = new MockIndex; 113 | for (int i = 0; i < leaf->data.capacity(); i++) leaf->data.append(i); 114 | for (int i = 0; i < index->data.capacity(); i++) index->data.append(i); 115 | MPersistence persistence("p1.test"); 116 | persistence.record(leaf); 117 | persistence.record(index); 118 | leaf_idx = leaf->idx; 119 | index_idx = index->idx; 120 | } 121 | { 122 | MPersistence persistence("p1.test"); 123 | MockLeaf *leaf = dynamic_cast(persistence.get(leaf_idx)); 124 | REQUIRE(leaf); 125 | MockIndex *index = dynamic_cast(persistence.get(index_idx)); 126 | REQUIRE(index); 127 | REQUIRE(leaf->idx == leaf_idx); 128 | REQUIRE(index->idx == index_idx); 129 | for (int i = 0; i < leaf->data.capacity(); i++) REQUIRE(leaf->data[i] == i); 130 | for (int i = 0; i < index->data.capacity(); i++) REQUIRE(index->data[i] == i); 131 | } 132 | } 133 | 134 | SECTION("should correctly offload pages") { 135 | { 136 | MockLeaf *leaf = new MockLeaf; 137 | MockIndex *index = new MockIndex; 138 | for (int i = 0; i < leaf->data.capacity(); i++) leaf->data.append(i); 139 | for (int i = 0; i < index->data.capacity(); i++) index->data.append(i); 140 | MPersistence persistence("p1.test"); 141 | persistence.record(leaf); 142 | persistence.record(index); 143 | unsigned leaf_idx = leaf->idx; 144 | unsigned index_idx = index->idx; 145 | persistence.offload_page(leaf_idx); 146 | persistence.offload_page(index_idx); 147 | leaf = dynamic_cast(persistence.get(leaf_idx)); 148 | index = dynamic_cast(persistence.get(index_idx)); 149 | for (int i = 0; i < leaf->data.capacity(); i++) REQUIRE(leaf->data[i] == i); 150 | for (int i = 0; i < index->data.capacity(); i++) REQUIRE(index->data[i] == i); 151 | } 152 | } 153 | 154 | SECTION("should swap out pages") { 155 | remove("p_swap.test"); 156 | MPersistence persistence("p_swap.test"); 157 | persistence.record(make_leaf()); 158 | persistence.record(make_leaf()); 159 | persistence.record(make_leaf()); 160 | persistence.record(make_leaf()); 161 | persistence.record(make_leaf()); 162 | persistence.record(make_leaf()); 163 | persistence.record(make_leaf()); 164 | persistence.record(make_leaf()); 165 | persistence.swap_out_pages(); 166 | REQUIRE(persistence.stat.swap_out == 4); 167 | remove("p_swap.test"); 168 | } 169 | 170 | SECTION("should align to 4k") { 171 | MPersistence persistence("p_swap.test"); 172 | for (int i = 0; i < 20000; i++) { 173 | ssize_t aligned = persistence.align_to_4k(i); 174 | REQUIRE(aligned % (4 * 1024) == 0); 175 | REQUIRE(aligned >= i); 176 | } 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /src/bench1.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-06-08. 3 | // 4 | 5 | #include "benchmark.hpp" 6 | 7 | namespace bench1 { 8 | const int test_size = 3 * 1e7; 9 | int test_data[test_size]; 10 | 11 | void generate_test_data() { 12 | for (int i = 0; i < test_size; i++) test_data[i] = i; 13 | for (int i = test_size - 1; i > 0; i--) { 14 | int j = rand() % (i + 1); 15 | std::swap(test_data[i], test_data[j]); 16 | } 17 | } 18 | 19 | void test_insert() { 20 | m = new BigTable("data.db"); 21 | update_clock(); 22 | for (int i = 0; i < test_size; i++) { 23 | m->insert(test_data[i], test_data[i]); 24 | progress(i, test_size, m); 25 | } 26 | print_clock("Insertion"); 27 | m->storage->stat.stat(); 28 | delete m; 29 | print_clock("Save"); 30 | } 31 | 32 | void test_remove() { 33 | m = new BigTable("data.db"); 34 | print_clock("Read"); 35 | for (int i = 0; i < test_size; i++) { 36 | m->query(test_data[i]); 37 | progress(i, test_size, m); 38 | } 39 | print_clock("Find"); 40 | m->storage->stat.stat(); 41 | for (int i = 0; i < test_size; i++) { 42 | m->remove(test_data[i]); 43 | progress(i, test_size, m); 44 | } 45 | print_clock("Remove"); 46 | m->storage->stat.stat(); 47 | delete m; 48 | } 49 | 50 | void bench_1() { 51 | remove("data.db"); 52 | generate_test_data(); 53 | update_clock(); 54 | std::sort(test_data, test_data + test_size); 55 | print_clock("Sort"); 56 | generate_test_data(); 57 | std::cout << "Generation complete" << std::endl; 58 | test_insert(); 59 | test_remove(); 60 | } 61 | } -------------------------------------------------------------------------------- /src/bench2.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-06-08. 3 | // 4 | 5 | #include "benchmark.hpp" 6 | 7 | namespace bench2 { 8 | const int test_size = 5 * 1e8; 9 | 10 | void test_insert() { 11 | m = new BigTable("data.db"); 12 | update_clock(); 13 | for (int i = 0; i < test_size; i++) { 14 | m->insert(i, i); 15 | progress(i, test_size, m); 16 | } 17 | print_clock("Insertion"); 18 | m->storage->stat.stat(); 19 | delete m; 20 | print_clock("Save"); 21 | } 22 | 23 | void test_remove() { 24 | m = new BigTable("data.db"); 25 | print_clock("Read"); 26 | for (int i = 0; i < test_size; i++) { 27 | assert(*m->query(i) == i); 28 | progress(i, test_size, m); 29 | } 30 | print_clock("Find"); 31 | m->storage->stat.stat(); 32 | for (int i = 0; i < test_size; i++) { 33 | m->remove(i); 34 | progress(i, test_size, m); 35 | } 36 | print_clock("Remove"); 37 | m->storage->stat.stat(); 38 | delete m; 39 | } 40 | 41 | void bench_2() { 42 | update_clock(); 43 | test_insert(); 44 | test_remove(); 45 | test_insert(); 46 | test_remove(); 47 | } 48 | } -------------------------------------------------------------------------------- /src/benchmark.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-06-08. 3 | // 4 | 5 | #ifndef BPLUSTREE_BENCHMARK_HPP 6 | #define BPLUSTREE_BENCHMARK_HPP 7 | 8 | #include 9 | #include "BTree.hpp" 10 | #include 11 | #include 12 | #include 13 | 14 | using BigTable = BTree; 15 | BigTable *m; 16 | 17 | 18 | double update_clock() { 19 | static clock_t start = std::clock(); 20 | double duration; 21 | duration = (std::clock() - start) / (double) CLOCKS_PER_SEC; 22 | start = std::clock(); 23 | return duration; 24 | } 25 | 26 | void print_clock(const char *message) { 27 | std::cout << "[" << message << "] " << update_clock() << "s" << std::endl; 28 | } 29 | 30 | 31 | inline void progress(long long i, long long n, BigTable *t) { 32 | if (n <= 10000000) return; 33 | if ((i + 1) % (n / 100) == 0) { 34 | std::cout << " [" << i << "/" << n << "] " << i * 100 / n << "%" << std::endl; 35 | t->storage->stat.stat(); 36 | } 37 | } 38 | 39 | #endif //BPLUSTREE_BENCHMARK_HPP 40 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "BTree.hpp" 3 | #include 4 | #include 5 | #include 6 | 7 | #include "bench1.hpp" 8 | #include "bench2.hpp" 9 | 10 | int main() { 11 | remove("data.db"); 12 | bench1::bench_1(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /src/test_main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Alex Chi on 2019-05-23. 3 | // 4 | 5 | #define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file 6 | #include 7 | #include 8 | -------------------------------------------------------------------------------- /src/utility.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by 郑文鑫 on 2019-03-09. 3 | // 4 | 5 | #ifndef BPLUSTREE_UTILITY_H 6 | #define BPLUSTREE_UTILITY_H 7 | 8 | #include 9 | 10 | enum OperationResult { 11 | Success, Duplicated, Fail 12 | }; 13 | 14 | template 15 | class pair { 16 | public: 17 | T1 first; 18 | T2 second; 19 | 20 | constexpr pair() : first(), second() {} 21 | 22 | pair(const pair &other) = default; 23 | 24 | pair(pair &&other) = default; 25 | 26 | pair(const T1 &x, const T2 &y) : first(x), second(y) {} 27 | 28 | template 29 | pair(U1 &&x, U2 &&y) : first(x), second(y) {} 30 | 31 | template 32 | pair(const pair &other) : first(other.first), second(other.second) {} 33 | 34 | template 35 | pair(pair &&other) : first(other.first), second(other.second) {} 36 | }; 37 | 38 | 39 | 40 | #endif //BPLUSTREE_UTILITY_H 41 | --------------------------------------------------------------------------------