diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ae78cc..8e672b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_CXX_STANDARD 14) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -pedantic-errors") if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) diff --git a/src/defs.h b/src/defs.h index e0362c3..5b675a6 100644 --- a/src/defs.h +++ b/src/defs.h @@ -20,8 +20,10 @@ struct Rid { int page_no; int slot_no; - friend bool operator==(const Rid &x, const Rid &y) { return x.page_no == y.page_no && x.slot_no == y.slot_no; } + Rid() = default; + Rid(int page_no_, int slot_no_) : page_no(page_no_), slot_no(slot_no_) {} + friend bool operator==(const Rid &x, const Rid &y) { return x.page_no == y.page_no && x.slot_no == y.slot_no; } friend bool operator!=(const Rid &x, const Rid &y) { return !(x == y); } }; diff --git a/src/interp.h b/src/interp.h index 0edab6e..b45ca77 100644 --- a/src/interp.h +++ b/src/interp.h @@ -41,9 +41,8 @@ class Interp { std::vector col_defs; for (auto &field : x->fields) { if (auto sv_col_def = std::dynamic_pointer_cast(field)) { - ColDef col_def = {.name = sv_col_def->col_name, - .type = interp_sv_type(sv_col_def->type_len->type), - .len = sv_col_def->type_len->len}; + ColDef col_def(sv_col_def->col_name, interp_sv_type(sv_col_def->type_len->type), + sv_col_def->type_len->len); col_defs.push_back(col_def); } else { throw InternalError("Unexpected field type"); @@ -69,8 +68,7 @@ class Interp { std::vector conds = interp_where_clause(x->conds); std::vector set_clauses; for (auto &sv_set_clause : x->set_clauses) { - SetClause set_clause = {.lhs = {.tab_name = "", .col_name = sv_set_clause->col_name}, - .rhs = interp_sv_value(sv_set_clause->val)}; + SetClause set_clause(TabCol("", sv_set_clause->col_name), interp_sv_value(sv_set_clause->val)); set_clauses.push_back(set_clause); } QlManager::update_set(x->tab_name, set_clauses, conds); @@ -78,7 +76,7 @@ class Interp { std::vector conds = interp_where_clause(x->conds); std::vector sel_cols; for (auto &sv_sel_col : x->cols) { - TabCol sel_col = {.tab_name = sv_sel_col->tab_name, .col_name = sv_sel_col->col_name}; + TabCol sel_col(sv_sel_col->tab_name, sv_sel_col->col_name); sel_cols.push_back(sel_col); } QlManager::select_from(sel_cols, x->tabs, conds); @@ -120,14 +118,14 @@ class Interp { std::vector conds; for (auto &expr : sv_conds) { Condition cond; - cond.lhs_col = {.tab_name = expr->lhs->tab_name, .col_name = expr->lhs->col_name}; + cond.lhs_col = TabCol(expr->lhs->tab_name, expr->lhs->col_name); cond.op = interp_sv_comp_op(expr->op); if (auto rhs_val = std::dynamic_pointer_cast(expr->rhs)) { cond.is_rhs_val = true; cond.rhs_val = interp_sv_value(rhs_val); } else if (auto rhs_col = std::dynamic_pointer_cast(expr->rhs)) { cond.is_rhs_val = false; - cond.rhs_col = {.tab_name = rhs_col->tab_name, .col_name = rhs_col->col_name}; + cond.rhs_col = TabCol(rhs_col->tab_name, rhs_col->col_name); } conds.push_back(cond); } diff --git a/src/ix/ix_defs.h b/src/ix/ix_defs.h index 4883875..acf19ff 100644 --- a/src/ix/ix_defs.h +++ b/src/ix/ix_defs.h @@ -14,6 +14,13 @@ struct IxFileHdr { int rid_offset; // offset of rid array (children array) int first_leaf; int last_leaf; + + IxFileHdr() = default; + IxFileHdr(int first_free_, int num_pages_, int root_page_, ColType col_type_, int col_len_, int btree_order_, + int key_offset_, int rid_offset_, int first_leaf_, int last_leaf_) + : first_free(first_free_), num_pages(num_pages_), root_page(root_page_), col_type(col_type_), col_len(col_len_), + btree_order(btree_order_), key_offset(key_offset_), rid_offset(rid_offset_), first_leaf(first_leaf_), + last_leaf(last_leaf_) {} }; struct IxPageHdr { @@ -24,14 +31,21 @@ struct IxPageHdr { bool is_leaf; int prev_leaf; // previous leaf node, effective only when is_leaf is true int next_leaf; // next leaf node, effective only when is_leaf is true + + IxPageHdr() = default; + IxPageHdr(int next_free_, int parent_, int num_key_, int num_child_, bool is_leaf_, int prev_leaf_, int next_leaf_) + : next_free(next_free_), parent(parent_), num_key(num_key_), num_child(num_child_), is_leaf(is_leaf_), + prev_leaf(prev_leaf_), next_leaf(next_leaf_) {} }; struct Iid { int page_no; int slot_no; - friend bool operator==(const Iid &x, const Iid &y) { return x.page_no == y.page_no && x.slot_no == y.slot_no; } + Iid() = default; + Iid(int page_no_, int slot_no_) : page_no(page_no_), slot_no(slot_no_) {} + friend bool operator==(const Iid &x, const Iid &y) { return x.page_no == y.page_no && x.slot_no == y.slot_no; } friend bool operator!=(const Iid &x, const Iid &y) { return !(x == y); } }; diff --git a/src/ix/ix_index_handle.cpp b/src/ix/ix_index_handle.cpp index be01a9d..89f74e3 100644 --- a/src/ix/ix_index_handle.cpp +++ b/src/ix/ix_index_handle.cpp @@ -130,7 +130,7 @@ IxIndexHandle::IxIndexHandle(int fd_) { void IxIndexHandle::insert_entry(const uint8_t *key, const Rid &rid) { Iid iid = upper_bound(key); IxNodeHandle node = fetch_node(iid.page_no); - PfPager::mark_dirty(node.page); + node.page->mark_dirty(); // We need to insert at iid.slot_no node.insert_key(iid.slot_no, key); node.insert_rid(iid.slot_no, rid); @@ -145,17 +145,9 @@ void IxIndexHandle::insert_entry(const uint8_t *key, const Rid &rid) { if (node.hdr->parent == IX_NO_PAGE) { // If current page is root node, allocate new root IxNodeHandle root = create_node(); - *root.hdr = { - .next_free = IX_NO_PAGE, - .parent = IX_NO_PAGE, - .num_key = 0, - .num_child = 0, - .is_leaf = false, - .prev_leaf = IX_NO_PAGE, - .next_leaf = IX_NO_PAGE, - }; + *root.hdr = IxPageHdr(IX_NO_PAGE, IX_NO_PAGE, 0, 0, false, IX_NO_PAGE, IX_NO_PAGE); // Insert current node's key & rid - Rid curr_rid = {.page_no = node.page->id.page_no, .slot_no = -1}; + Rid curr_rid(node.page->id.page_no, -1); root.insert_rid(0, curr_rid); root.insert_key(0, node.get_key(node.hdr->num_key - 1)); // update current node's parent @@ -165,22 +157,18 @@ void IxIndexHandle::insert_entry(const uint8_t *key, const Rid &rid) { } // Allocate brother node IxNodeHandle bro = create_node(); - *bro.hdr = { - .next_free = IX_NO_PAGE, - .parent = node.hdr->parent, // They have the same parent - .num_key = 0, - .num_child = 0, - .is_leaf = node.hdr->is_leaf, // Brother node is leaf only if current node is leaf. - .prev_leaf = IX_NO_PAGE, - .next_leaf = IX_NO_PAGE, - }; + *bro.hdr = IxPageHdr(IX_NO_PAGE, + node.hdr->parent, // They have the same parent + 0, 0, + node.hdr->is_leaf, // Brother node is leaf only if current node is leaf. + IX_NO_PAGE, IX_NO_PAGE); if (bro.hdr->is_leaf) { // maintain brother node's leaf pointer bro.hdr->next_leaf = node.hdr->next_leaf; bro.hdr->prev_leaf = node.page->id.page_no; // Let original next node's prev = brother node IxNodeHandle next = fetch_node(node.hdr->next_leaf); - PfPager::mark_dirty(next.page); + next.page->mark_dirty(); next.hdr->prev_leaf = bro.page->id.page_no; // curr's next = brother node node.hdr->next_leaf = bro.page->id.page_no; @@ -201,12 +189,12 @@ void IxIndexHandle::insert_entry(const uint8_t *key, const Rid &rid) { uint8_t *popup_key = node.get_key(split_idx - 1); // Load parent node IxNodeHandle parent = fetch_node(node.hdr->parent); - PfPager::mark_dirty(parent.page); + parent.page->mark_dirty(); // Find the rank of current node in its parent int child_idx = parent.find_child(node); // Insert popup key into parent parent.insert_key(child_idx, popup_key); - Rid bro_rid = {.page_no = bro.page->id.page_no, .slot_no = -1}; + Rid bro_rid(bro.page->id.page_no, -1); parent.insert_rid(child_idx + 1, bro_rid); // Update global last_leaf if needed if (hdr.last_leaf == node.page->id.page_no) { @@ -229,7 +217,7 @@ void IxIndexHandle::delete_entry(const uint8_t *key, const Rid &rid) { continue; } // Found the entry with the given rid, delete it - PfPager::mark_dirty(node.page); + node.page->mark_dirty(); node.erase_key(scan.iid().slot_no); node.erase_rid(scan.iid().slot_no); // Update its parent's key to the node's new last key @@ -243,7 +231,7 @@ void IxIndexHandle::delete_entry(const uint8_t *key, const Rid &rid) { int new_root_page = node.get_rid(0)->page_no; // Load new root and set its parent to NO_PAGE IxNodeHandle new_root = fetch_node(new_root_page); - PfPager::mark_dirty(new_root.page); + new_root.page->mark_dirty(); new_root.hdr->parent = IX_NO_PAGE; // Update global root hdr.root_page = new_root_page; @@ -254,7 +242,7 @@ void IxIndexHandle::delete_entry(const uint8_t *key, const Rid &rid) { } // Load parent node IxNodeHandle parent = fetch_node(node.hdr->parent); - PfPager::mark_dirty(parent.page); + parent.page->mark_dirty(); // Find the rank of this child in its parent int child_idx = parent.find_child(node); if (0 < child_idx) { @@ -262,7 +250,7 @@ void IxIndexHandle::delete_entry(const uint8_t *key, const Rid &rid) { IxNodeHandle bro = fetch_node(parent.get_rid(child_idx - 1)->page_no); if (bro.hdr->num_child > (hdr.btree_order + 1) / 2) { // If left brother is rich, borrow one node from it - PfPager::mark_dirty(bro.page); + bro.page->mark_dirty(); node.insert_key(0, bro.get_key(bro.hdr->num_key - 1)); node.insert_rid(0, *bro.get_rid(bro.hdr->num_child - 1)); bro.erase_key(bro.hdr->num_key - 1); @@ -280,7 +268,7 @@ void IxIndexHandle::delete_entry(const uint8_t *key, const Rid &rid) { IxNodeHandle bro = fetch_node(parent.get_rid(child_idx + 1)->page_no); if (bro.hdr->num_child > (hdr.btree_order + 1) / 2) { // If right brother is rich, borrow one node from it - PfPager::mark_dirty(bro.page); + bro.page->mark_dirty(); node.insert_key(node.hdr->num_key, bro.get_key(0)); node.insert_rid(node.hdr->num_child, *bro.get_rid(0)); bro.erase_key(0); @@ -297,7 +285,7 @@ void IxIndexHandle::delete_entry(const uint8_t *key, const Rid &rid) { if (0 < child_idx) { // merge with left brother, transfer all children of current node to left brother IxNodeHandle bro = fetch_node(parent.get_rid(child_idx - 1)->page_no); - PfPager::mark_dirty(bro.page); + bro.page->mark_dirty(); bro.insert_keys(bro.hdr->num_key, node.get_key(0), node.hdr->num_key); bro.insert_rids(bro.hdr->num_child, node.get_rid(0), node.hdr->num_child); // Maintain left brother's children @@ -321,7 +309,7 @@ void IxIndexHandle::delete_entry(const uint8_t *key, const Rid &rid) { assert(child_idx + 1 < parent.hdr->num_child); // merge with right brother, transfer all children of right brother to current node IxNodeHandle bro = fetch_node(parent.get_rid(child_idx + 1)->page_no); - PfPager::mark_dirty(bro.page); + bro.page->mark_dirty(); // Transfer all right brother's valid rid to current node node.insert_rids(node.hdr->num_child, bro.get_rid(0), bro.hdr->num_child); node.insert_keys(node.hdr->num_key, bro.get_key(0), bro.hdr->num_key); @@ -372,7 +360,7 @@ Iid IxIndexHandle::lower_bound(const uint8_t *key) const { } // Now we come to a leaf node, we do a sequential search int key_idx = node.lower_bound(key); - Iid iid{.page_no = node.page->id.page_no, .slot_no = key_idx}; + Iid iid(node.page->id.page_no, key_idx); return iid; } @@ -389,18 +377,18 @@ Iid IxIndexHandle::upper_bound(const uint8_t *key) const { } // Now we come to a leaf node, we do a sequential search int key_idx = node.upper_bound(key); - Iid iid = {.page_no = node.page->id.page_no, .slot_no = key_idx}; + Iid iid(node.page->id.page_no, key_idx); return iid; } Iid IxIndexHandle::leaf_end() const { IxNodeHandle node = fetch_node(hdr.last_leaf); - Iid iid = {.page_no = hdr.last_leaf, .slot_no = node.hdr->num_key}; + Iid iid(hdr.last_leaf, node.hdr->num_key); return iid; } Iid IxIndexHandle::leaf_begin() const { - Iid iid = {.page_no = hdr.first_leaf, .slot_no = 0}; + Iid iid(hdr.first_leaf, 0); return iid; } @@ -416,7 +404,7 @@ IxNodeHandle IxIndexHandle::create_node() { node = IxNodeHandle(&hdr, page); hdr.first_free = node.hdr->next_free; } - PfPager::mark_dirty(page); + page->mark_dirty(); return node; } @@ -438,7 +426,7 @@ void IxIndexHandle::maintain_parent(const IxNodeHandle &node) { if (memcmp(parent_key, child_max_key, hdr.col_len) == 0) { break; } - PfPager::mark_dirty(parent.page); + parent.page->mark_dirty(); memcpy(parent_key, child_max_key, hdr.col_len); curr = parent; } @@ -447,11 +435,11 @@ void IxIndexHandle::maintain_parent(const IxNodeHandle &node) { void IxIndexHandle::erase_leaf(IxNodeHandle &leaf) { assert(leaf.hdr->is_leaf); IxNodeHandle prev = fetch_node(leaf.hdr->prev_leaf); - PfPager::mark_dirty(prev.page); + prev.page->mark_dirty(); prev.hdr->next_leaf = leaf.hdr->next_leaf; IxNodeHandle next = fetch_node(leaf.hdr->next_leaf); - PfPager::mark_dirty(next.page); + next.page->mark_dirty(); next.hdr->prev_leaf = leaf.hdr->prev_leaf; } @@ -465,7 +453,7 @@ void IxIndexHandle::maintain_child(IxNodeHandle &node, int child_idx) { // Current node is inner node, load its child and set its parent to current node int child_page_no = node.get_rid(child_idx)->page_no; IxNodeHandle child = fetch_node(child_page_no); - PfPager::mark_dirty(child.page); + child.page->mark_dirty(); child.hdr->parent = node.page->id.page_no; } } diff --git a/src/ix/ix_manager.cpp b/src/ix/ix_manager.cpp index f0651b5..1bfa6af 100644 --- a/src/ix/ix_manager.cpp +++ b/src/ix/ix_manager.cpp @@ -25,46 +25,20 @@ void IxManager::create_index(const std::string &filename, int index_no, ColType int key_offset = sizeof(IxPageHdr); int rid_offset = key_offset + (btree_order + 1) * col_len; - IxFileHdr fhdr = { - .first_free = IX_NO_PAGE, - .num_pages = IX_INIT_NUM_PAGES, - .root_page = IX_INIT_ROOT_PAGE, - .col_type = col_type, - .col_len = col_len, - .btree_order = btree_order, - .key_offset = key_offset, - .rid_offset = rid_offset, - .first_leaf = IX_INIT_ROOT_PAGE, - .last_leaf = IX_INIT_ROOT_PAGE, - }; + IxFileHdr fhdr(IX_NO_PAGE, IX_INIT_NUM_PAGES, IX_INIT_ROOT_PAGE, col_type, col_len, btree_order, key_offset, + rid_offset, IX_INIT_ROOT_PAGE, IX_INIT_ROOT_PAGE); static uint8_t page_buf[PAGE_SIZE]; PfPager::write_page(fd, IX_FILE_HDR_PAGE, (const uint8_t *)&fhdr, sizeof(fhdr)); // Create leaf list header page and write to file { auto phdr = (IxPageHdr *)page_buf; - *phdr = { - .next_free = IX_NO_PAGE, - .parent = IX_NO_PAGE, - .num_key = 0, - .num_child = 0, - .is_leaf = true, - .prev_leaf = IX_INIT_ROOT_PAGE, - .next_leaf = IX_INIT_ROOT_PAGE, - }; + *phdr = IxPageHdr(IX_NO_PAGE, IX_NO_PAGE, 0, 0, true, IX_INIT_ROOT_PAGE, IX_INIT_ROOT_PAGE); PfPager::write_page(fd, IX_LEAF_HEADER_PAGE, page_buf, PAGE_SIZE); } // Create root node and write to file { auto phdr = (IxPageHdr *)page_buf; - *phdr = { - .next_free = IX_NO_PAGE, - .parent = IX_NO_PAGE, - .num_key = 0, - .num_child = 0, - .is_leaf = true, - .prev_leaf = IX_LEAF_HEADER_PAGE, - .next_leaf = IX_LEAF_HEADER_PAGE, - }; + *phdr = IxPageHdr(IX_NO_PAGE, IX_NO_PAGE, 0, 0, true, IX_LEAF_HEADER_PAGE, IX_LEAF_HEADER_PAGE); // Must write PAGE_SIZE here in case of future fetch_node() PfPager::write_page(fd, IX_INIT_ROOT_PAGE, page_buf, PAGE_SIZE); } diff --git a/src/ix/ix_test.cpp b/src/ix/ix_test.cpp index 1c6d2f7..3f522b8 100644 --- a/src/ix/ix_test.cpp +++ b/src/ix/ix_test.cpp @@ -96,7 +96,7 @@ class IxTest : public ::testing::Test { std::multimap mock; for (int i = 0; i < round; i++) { int rand_key = rand() % round; - Rid rand_val = {.page_no = rand(), .slot_no = rand()}; + Rid rand_val(rand(), rand()); ih->insert_entry((const uint8_t *)&rand_key, rand_val); mock.insert(std::make_pair(rand_key, rand_val)); if (round % 500 == 0) { @@ -144,7 +144,7 @@ class IxTest : public ::testing::Test { if (mock.empty() || dice < insert_prob) { // Insert int rand_key = rand() % round; - Rid rand_val = {.page_no = rand(), .slot_no = rand()}; + Rid rand_val(rand(), rand()); ih->insert_entry((const uint8_t *)&rand_key, rand_val); mock.insert(std::make_pair(rand_key, rand_val)); add_cnt++; diff --git a/src/pf/pf_defs.h b/src/pf/pf_defs.h index 7baa7d3..b8331b6 100644 --- a/src/pf/pf_defs.h +++ b/src/pf/pf_defs.h @@ -4,22 +4,35 @@ #include #include -constexpr int PAGE_SIZE = 4096; -constexpr int NUM_CACHE_PAGES = 65536; +static constexpr int PAGE_SIZE = 4096; +static constexpr int NUM_CACHE_PAGES = 65536; struct PageId { int fd; int page_no; + PageId() = default; + PageId(int fd_, int page_no_) : fd(fd_), page_no(page_no_) {} + friend bool operator==(const PageId &x, const PageId &y) { return x.fd == y.fd && x.page_no == y.page_no; } + friend bool operator!=(const PageId &x, const PageId &y) { return !(x == y); } + + friend std::ostream &operator<<(std::ostream &os, const PageId &self) { + return os << "PageId(fd=" << self.fd << ", page_no=" << self.page_no << ")"; + } }; -struct PageIdHash { - size_t operator()(const PageId &x) const { return (x.fd << 16) | x.page_no; } +namespace std { +template <> +struct hash { + size_t operator()(const PageId &pid) const noexcept { return (pid.fd << 16) | pid.page_no; } }; +} // namespace std struct Page { PageId id; uint8_t *buf; bool is_dirty; + + void mark_dirty() { is_dirty = true; } }; diff --git a/src/pf/pf_manager.cpp b/src/pf/pf_manager.cpp index aea942e..8ae679a 100644 --- a/src/pf/pf_manager.cpp +++ b/src/pf/pf_manager.cpp @@ -56,13 +56,14 @@ int PfManager::open_file(const std::string &path) { } void PfManager::close_file(int fd) { - if (!_fd2path.count(fd)) { + auto pos = _fd2path.find(fd); + if (pos == _fd2path.end()) { throw FileNotOpenError(fd); } pager.flush_file(fd); - std::string filename = _fd2path[fd]; + const std::string &filename = pos->second; _path2fd.erase(filename); - _fd2path.erase(fd); + _fd2path.erase(pos); if (close(fd) != 0) { throw UnixError(); } diff --git a/src/pf/pf_manager.h b/src/pf/pf_manager.h index 2fe862d..104152d 100644 --- a/src/pf/pf_manager.h +++ b/src/pf/pf_manager.h @@ -9,9 +9,6 @@ #include class PfManager { - static std::unordered_map _path2fd; - static std::unordered_map _fd2path; - public: static PfPager pager; @@ -24,4 +21,8 @@ class PfManager { static int open_file(const std::string &path); static void close_file(int fd); + + private: + static std::unordered_map _path2fd; + static std::unordered_map _fd2path; }; diff --git a/src/pf/pf_pager.cpp b/src/pf/pf_pager.cpp index 382a7fa..b9134a7 100644 --- a/src/pf/pf_pager.cpp +++ b/src/pf/pf_pager.cpp @@ -32,11 +32,13 @@ void PfPager::write_page(int fd, int page_no, const uint8_t *buf, int num_bytes) } Page *PfPager::create_page(int fd, int page_no) { - Page *page = get_page(fd, page_no, false); - mark_dirty(page); + Page *page = get_page(fd, page_no); + page->mark_dirty(); return page; } +Page *PfPager::fetch_page(int fd, int page_no) { return get_page(fd, page_no); } + void PfPager::flush_file(int fd) { auto it_page = _busy_pages.begin(); while (it_page != _busy_pages.end()) { @@ -55,56 +57,51 @@ void PfPager::force_page(Page *page) { } } -Page *PfPager::get_page(int fd, int page_no, bool exists) { +template +Page *PfPager::get_page(int fd, int page_no) { Page *page; - PageId page_id = {.fd = fd, .page_no = page_no}; - auto it_page = _hashmap.find(page_id); - if (it_page == _hashmap.end()) { + PageId page_id(fd, page_no); + auto map_it = _busy_map.find(page_id); + if (map_it == _busy_map.end()) { // Page is not in memory (i.e. on disk). Allocate new cache page for it. if (_free_pages.empty()) { // Cache is full. Need to flush a page to disk. assert(!_busy_pages.empty()); - page = _busy_pages.back(); - force_page(page); - _busy_pages.pop_back(); - _hashmap.erase(page->id); + force_page(_busy_pages.back()); + _busy_map.erase(_busy_pages.back()->id); + _busy_pages.splice(_busy_pages.begin(), _busy_pages, --_busy_pages.end()); } else { - // Allocate from free pages. - page = *_free_pages.begin(); - _free_pages.pop_front(); + // Cache is not full. Allocate from free pages. + _busy_pages.splice(_busy_pages.begin(), _free_pages, _free_pages.begin()); } - page->id = {.fd = fd, .page_no = page_no}; + _busy_map[page_id] = _busy_pages.begin(); + page = _busy_pages.front(); + page->id = page_id; page->is_dirty = false; - // Push to the list front - _busy_pages.push_front(page); - _hashmap[page->id] = _busy_pages.begin(); - if (exists) { + if (EXISTS) { read_page(fd, page_no, page->buf, PAGE_SIZE); } } else { // Page is in memory - page = *it_page->second; + page = *map_it->second; access(page); } return page; } void PfPager::access(Page *page) { - assert(page_in_cache(page->id)); - auto it = _hashmap[page->id]; - _busy_pages.erase(it); - _busy_pages.push_front(page); - _hashmap[page->id] = _busy_pages.begin(); + assert(in_cache(page->id)); + _busy_pages.splice(_busy_pages.begin(), _busy_pages, _busy_map[page->id]); } void PfPager::flush_page(Page *page) { - assert(page_in_cache(page->id)); - auto it = _hashmap[page->id]; + assert(in_cache(page->id)); + auto map_it = _busy_map.find(page->id); + auto busy_it = map_it->second; force_page(page); - _busy_pages.erase(it); - _free_pages.push_front(page); - _hashmap.erase(page->id); - assert(!page_in_cache(page->id)); + _free_pages.splice(_free_pages.begin(), _busy_pages, busy_it); + _busy_map.erase(map_it); + assert(!in_cache(page->id)); } void PfPager::flush_all() { @@ -113,5 +110,5 @@ void PfPager::flush_all() { } _free_pages.insert(_free_pages.end(), _busy_pages.begin(), _busy_pages.end()); _busy_pages.clear(); - _hashmap.clear(); + _busy_map.clear(); } diff --git a/src/pf/pf_pager.h b/src/pf/pf_pager.h index 00c1079..2d2165a 100644 --- a/src/pf/pf_pager.h +++ b/src/pf/pf_pager.h @@ -6,44 +6,43 @@ #include class PfPager { - friend class PfTest; - public: PfPager(); - ~PfPager(); - static void read_page(int fd, int page_no, uint8_t *buf, int num_bytes); + PfPager &operator=(const PfPager &other) = delete; + static void read_page(int fd, int page_no, uint8_t *buf, int num_bytes); static void write_page(int fd, int page_no, const uint8_t *buf, int num_bytes); Page *create_page(int fd, int page_no); - Page *fetch_page(int fd, int page_no) { return get_page(fd, page_no, true); } - - static void mark_dirty(Page *page) { page->is_dirty = true; } + Page *fetch_page(int fd, int page_no); void flush_file(int fd); + void flush_page(Page *page); + void flush_all(); - private: - bool page_in_cache(const PageId &page_id) const { return _hashmap.count(page_id) > 0; } + bool in_cache(const PageId &page_id) const { return _busy_map.find(page_id) != _busy_map.end(); } + + const std::list &busy_list() const { return _busy_pages; } + const std::unordered_map::iterator> &busy_map() const { return _busy_map; } + const std::list &free_list() const { return _free_pages; } + private: static void force_page(Page *page); // Get the page from memory corresponding to the disk page. // If the page is not in memory, allocate a page and read the disk. - Page *get_page(int fd, int page_no, bool exists); + template + Page *get_page(int fd, int page_no); void access(Page *page); - void flush_page(Page *page); - - void flush_all(); - private: uint8_t _cache[NUM_CACHE_PAGES * PAGE_SIZE]; Page _pages[NUM_CACHE_PAGES]; - std::unordered_map::iterator, PageIdHash> _hashmap; + std::unordered_map::iterator> _busy_map; std::list _busy_pages; std::list _free_pages; }; diff --git a/src/pf/pf_test.cpp b/src/pf/pf_test.cpp index e48bc69..c2fc574 100644 --- a/src/pf/pf_test.cpp +++ b/src/pf/pf_test.cpp @@ -1,44 +1,131 @@ #include "pf/pf.h" #include -static constexpr int MAX_FILES = 32; -static constexpr int MAX_PAGES = 128; - -std::unordered_map mock; // fd -> buffer +TEST(PfManagerTest, basic) { + std::string path1 = "a.txt"; + if (PfManager::is_file(path1)) { + PfManager::destroy_file(path1); + } + std::string path2 = "b.txt"; + if (PfManager::is_file(path2)) { + PfManager::destroy_file(path2); + } -uint8_t *mock_get_page(int fd, int page_no) { return &mock[fd][page_no * PAGE_SIZE]; } + // create file + PfManager::create_file(path1); + EXPECT_THROW(PfManager::create_file(path1), FileExistsError); + + // is file + EXPECT_TRUE(PfManager::is_file(path1)); + EXPECT_FALSE(PfManager::is_file(path2)); + + // open file + int fd = PfManager::open_file(path1); + EXPECT_THROW(PfManager::open_file(path2), FileNotFoundError); + EXPECT_THROW(PfManager::open_file(path1), FileNotClosedError); + + // close file + EXPECT_THROW(PfManager::close_file(fd + 1), FileNotOpenError); + PfManager::close_file(fd); + EXPECT_THROW(PfManager::close_file(fd), FileNotOpenError); + + // destroy file + EXPECT_THROW(PfManager::destroy_file(path2), FileNotFoundError); + fd = PfManager::open_file(path1); + EXPECT_THROW(PfManager::destroy_file(path1), FileNotClosedError); + PfManager::close_file(fd); + PfManager::destroy_file(path1); + EXPECT_THROW(PfManager::destroy_file(path1), FileNotFoundError); +} -void check_disk(int fd, int page_no) { - static uint8_t buf[PAGE_SIZE]; - PfPager::read_page(fd, page_no, buf, PAGE_SIZE); - uint8_t *mock_buf = mock_get_page(fd, page_no); - EXPECT_EQ(memcmp(buf, mock_buf, PAGE_SIZE), 0); +static void check_pages(const std::list &busy_page_ids) { + EXPECT_EQ(PfManager::pager.free_list().size(), NUM_CACHE_PAGES - busy_page_ids.size()); + EXPECT_EQ(PfManager::pager.busy_list().size(), busy_page_ids.size()); + auto busy_it = PfManager::pager.busy_list().begin(); + for (const auto &pid : busy_page_ids) { + EXPECT_EQ(pid, (*busy_it)->id); + EXPECT_TRUE(PfManager::pager.in_cache(pid)); + EXPECT_EQ(PfManager::pager.busy_map().at(pid), busy_it); + busy_it++; + } } -void check_disk_all() { - for (auto &file : mock) { - int fd = file.first; - for (int page_no = 0; page_no < MAX_PAGES; page_no++) { - check_disk(fd, page_no); +TEST(PfPagerTest, lru) { + srand((unsigned)time(nullptr)); + + std::vector paths{"0.txt", "1.txt", "2.txt", "3.txt"}; + std::vector fds; + for (const auto &path : paths) { + if (PfManager::is_file(path)) { + PfManager::destroy_file(path); } + PfManager::create_file(path); + int fd = PfManager::open_file(path); + fds.emplace_back(fd); } -} -void check_cache(int fd, int page_no) { - Page *page = PfManager::pager.fetch_page(fd, page_no); - uint8_t *mock_buf = mock_get_page(fd, page_no); - EXPECT_EQ(memcmp(page->buf, mock_buf, PAGE_SIZE), 0); -} + // create pages + std::list busy_page_ids; + for (int i = 0; i < NUM_CACHE_PAGES; i++) { + if (rand() % 100 == 0) { + check_pages(busy_page_ids); + } + for (int fd : fds) { + PfManager::pager.create_page(fd, i); + busy_page_ids.push_front(PageId(fd, i)); + if (busy_page_ids.size() > NUM_CACHE_PAGES) { + busy_page_ids.pop_back(); + } + } + } + check_pages(busy_page_ids); -void check_cache_all() { - for (auto &file : mock) { - int fd = file.first; - for (int page_no = 0; page_no < MAX_PAGES; page_no++) { - check_cache(fd, page_no); + // access pages + auto busy_it = busy_page_ids.begin(); + while (busy_it != busy_page_ids.end()) { + if (rand() % 100 == 0) { + check_pages(busy_page_ids); } + PfManager::pager.fetch_page(busy_it->fd, busy_it->page_no); + auto curr_it = busy_it++; + busy_page_ids.push_front(*curr_it); + busy_page_ids.erase(curr_it); + } + check_pages(busy_page_ids); + + for (int fd : fds) { + PfManager::close_file(fd); + } + for (const auto &path : paths) { + PfManager::destroy_file(path); } } +static constexpr int MAX_FILES = 16; +static constexpr int MAX_PAGES = 128; + +class MockPager { + public: + uint8_t *get_page(int fd, int page_no) { return &data[fd][page_no * PAGE_SIZE]; } + + void check_disk(int fd, int page_no) { + EXPECT_TRUE(!PfManager::pager.in_cache({fd, page_no})); + uint8_t buf[PAGE_SIZE]; + PfPager::read_page(fd, page_no, buf, PAGE_SIZE); + uint8_t *mock_buf = get_page(fd, page_no); + EXPECT_EQ(memcmp(buf, mock_buf, PAGE_SIZE), 0); + } + + void check_cache(int fd, int page_no) { + EXPECT_TRUE(PfManager::pager.in_cache({fd, page_no})); + Page *page = PfManager::pager.fetch_page(fd, page_no); + uint8_t *mock_buf = get_page(fd, page_no); + EXPECT_EQ(memcmp(page->buf, mock_buf, PAGE_SIZE), 0); + } + + std::unordered_map> data; // fd -> buffer +}; + void rand_buf(int size, uint8_t *buf) { for (int i = 0; i < size; i++) { int rand_ch = rand() & 0xff; @@ -46,129 +133,92 @@ void rand_buf(int size, uint8_t *buf) { } } -int rand_fd() { - EXPECT_EQ(mock.size(), MAX_FILES); - int fd_idx = rand() % MAX_FILES; - auto it = mock.begin(); - for (int i = 0; i < fd_idx; i++) { - it++; - } - return it->first; -} +TEST(PfPagerTest, readwrite) { + MockPager mock; -class PfTest : public ::testing::Test { - public: - void test_basic() { - srand((unsigned)time(nullptr)); - - // test files - std::vector filenames(MAX_FILES); - std::unordered_map fd2name; - for (size_t i = 0; i < filenames.size(); i++) { - auto &filename = filenames[i]; - filename = std::to_string(i) + ".txt"; - if (PfManager::is_file(filename)) { - PfManager::destroy_file(filename); - } - // open without create - EXPECT_THROW(PfManager::open_file(filename), FileNotFoundError); + // create files + std::vector filenames; + std::vector fds; + for (int i = 0; i < MAX_FILES; i++) { + std::string filename = std::to_string(i) + ".txt"; + if (PfManager::is_file(filename)) { + PfManager::destroy_file(filename); + } + PfManager::create_file(filename); + int fd = PfManager::open_file(filename); + filenames.emplace_back(std::move(filename)); + fds.emplace_back(fd); + mock.data[fd] = {}; + } - PfManager::create_file(filename); - EXPECT_TRUE(PfManager::is_file(filename)); - EXPECT_THROW(PfManager::create_file(filename), FileExistsError); + std::unordered_map is_created; + is_created.reserve(MAX_FILES * MAX_PAGES); + for (int fd : fds) { + for (int i = 0; i < MAX_PAGES; i++) { + is_created[PageId(fd, i)] = false; + } + } - // open file - int fd = PfManager::open_file(filename); - mock[fd] = new uint8_t[PAGE_SIZE * MAX_PAGES]; - fd2name[fd] = filename; + // random access + for (int i = 0; i < 100000; i++) { + int file_idx = rand() % MAX_FILES; + int fd = fds[file_idx]; + int page_no = rand() % MAX_PAGES; + PageId page_id(fd, page_no); + uint8_t *mock_buf = mock.get_page(fd, page_no); + + if (!is_created[page_id]) { + // create page + Page *page = PfManager::pager.create_page(fd, page_no); + rand_buf(PAGE_SIZE, page->buf); + memcpy(mock_buf, page->buf, PAGE_SIZE); } - // init & test alloc_page - int num_pages = 0; - uint8_t init_buf[PAGE_SIZE]; - for (auto &fh : mock) { - int fd = fh.first; - for (int page_no = 0; page_no < MAX_PAGES; page_no++) { - rand_buf(PAGE_SIZE, init_buf); - Page *page = PfManager::pager.create_page(fd, page_no); - memcpy(page->buf, init_buf, PAGE_SIZE); - uint8_t *mock_buf = mock_get_page(fd, page_no); - memcpy(mock_buf, init_buf, PAGE_SIZE); - num_pages++; - } + + // get page + Page *page = PfManager::pager.fetch_page(fd, page_no); + EXPECT_EQ(memcmp(page->buf, mock_buf, PAGE_SIZE), 0); + // check equal in cache + mock.check_cache(fd, page_no); + + // modify + rand_buf(PAGE_SIZE, page->buf); + memcpy(mock_buf, page->buf, PAGE_SIZE); + page->mark_dirty(); + + // flush + if (rand() % 20 == 0) { + PfManager::pager.flush_page(page); + mock.check_disk(fd, page_no); } - check_cache_all(); - // Test LRU cache - EXPECT_FALSE(PfManager::pager._busy_pages.empty()); - auto prev = PfManager::pager._busy_pages; - for (int r = 0; r < 50; r++) { - // Randomly access a page - int idx = rand() % PfManager::pager._busy_pages.size(); - auto it = prev.begin(); - for (int i = 0; i < idx; i++) { - it++; - } - Page *page = *it; - prev.erase(it); - prev.push_front(page); - // Fetch this page with PF - PfManager::pager.fetch_page(page->id.fd, page->id.page_no); - // Check this page is front of the list - EXPECT_EQ(prev, PfManager::pager._busy_pages); - EXPECT_EQ(PfManager::pager._hashmap[page->id], PfManager::pager._busy_pages.begin()); + // flush the entire file + if (rand() % 200 == 0) { + PfManager::pager.flush_file(fd); } - // Flush and test disk - PfManager::pager.flush_all(); - check_disk_all(); - // test get_page - for (int r = 0; r < 10000; r++) { - int fd = rand_fd(); - int page_no = rand() % MAX_PAGES; - // get page - Page *page = PfManager::pager.fetch_page(fd, page_no); - uint8_t *mock_buf = mock_get_page(fd, page_no); - EXPECT_EQ(memcmp(page->buf, mock_buf, PAGE_SIZE), 0); - - // modify - rand_buf(PAGE_SIZE, init_buf); - memcpy(page->buf, init_buf, PAGE_SIZE); - memcpy(mock_buf, init_buf, PAGE_SIZE); - PfPager::mark_dirty(page); - - // flush - if (rand() % 10 == 0) { - PfManager::pager.flush_page(page); - check_disk(fd, page_no); - } - // flush entire file - if (rand() % 100 == 0) { - PfManager::pager.flush_file(fd); - } - // re-open file - if (rand() % 100 == 0) { - PfManager::close_file(fd); - auto filename = fd2name[fd]; - uint8_t *buf = mock[fd]; - fd2name.erase(fd); - mock.erase(fd); - int new_fd = PfManager::open_file(filename); - mock[new_fd] = buf; - fd2name[new_fd] = filename; + // re-open file + if (rand() % 200 == 0) { + PfManager::close_file(fd); + int new_fd = PfManager::open_file(filenames[file_idx]); + if (new_fd != fd) { + fds[file_idx] = new_fd; + mock.data[new_fd] = mock.data[fd]; + mock.data.erase(fd); } - // check equal in cache - check_cache(fd, page_no); } - check_cache_all(); - PfManager::pager.flush_all(); - check_disk_all(); - // close and destroy files - for (auto &entry : fd2name) { - int fd = entry.first; - auto &filename = entry.second; - PfManager::close_file(fd); - PfManager::destroy_file(filename); - EXPECT_THROW(PfManager::destroy_file(filename), FileNotFoundError); + } + + // flush and check disk + PfManager::pager.flush_all(); + for (int fd : fds) { + for (int page_no = 0; page_no < MAX_PAGES; page_no++) { + mock.check_disk(fd, page_no); } } -}; -TEST_F(PfTest, basic) { test_basic(); } + // close and destroy files + for (int fd : fds) { + PfManager::close_file(fd); + } + for (const auto &path : filenames) { + PfManager::destroy_file(path); + } +} diff --git a/src/ql/ql_manager.cpp b/src/ql/ql_manager.cpp index dbabd33..abd936f 100644 --- a/src/ql/ql_manager.cpp +++ b/src/ql/ql_manager.cpp @@ -212,7 +212,7 @@ void QlManager::select_from(std::vector sel_cols, const std::vector::const_iterator QlNode::get_col(const std::vector std::map QlNode::rec2dict(const std::vector &cols, const RmRecord *rec) { std::map rec_dict; for (auto &col : cols) { - TabCol key = {.tab_name = col.tab_name, .col_name = col.name}; + TabCol key(col.tab_name, col.name); Value val; uint8_t *val_buf = rec->data + col.offset; if (col.type == TYPE_INT) { diff --git a/src/rm/rm_file_handle.cpp b/src/rm/rm_file_handle.cpp index 45dceb9..283070b 100644 --- a/src/rm/rm_file_handle.cpp +++ b/src/rm/rm_file_handle.cpp @@ -26,7 +26,7 @@ Rid RmFileHandle::insert_record(uint8_t *buf) { // update bitmap Bitmap::set(ph.bitmap, slot_no); // update page header - PfPager::mark_dirty(ph.page); + ph.page->mark_dirty(); ph.hdr->num_records++; if (ph.hdr->num_records == hdr.num_records_per_page) { // page is full @@ -35,7 +35,7 @@ Rid RmFileHandle::insert_record(uint8_t *buf) { // copy record data into slot uint8_t *slot = ph.get_slot(slot_no); memcpy(slot, buf, hdr.record_size); - Rid rid{.page_no = ph.page->id.page_no, .slot_no = slot_no}; + Rid rid(ph.page->id.page_no, slot_no); return rid; } @@ -44,7 +44,7 @@ void RmFileHandle::delete_record(const Rid &rid) { if (!Bitmap::test(ph.bitmap, rid.slot_no)) { throw RecordNotFoundError(rid.page_no, rid.slot_no); } - PfPager::mark_dirty(ph.page); + ph.page->mark_dirty(); if (ph.hdr->num_records == hdr.num_records_per_page) { // originally full, now available for new record release_page(ph); @@ -58,7 +58,7 @@ void RmFileHandle::update_record(const Rid &rid, uint8_t *buf) { if (!Bitmap::test(ph.bitmap, rid.slot_no)) { throw RecordNotFoundError(rid.page_no, rid.slot_no); } - PfPager::mark_dirty(ph.page); + ph.page->mark_dirty(); uint8_t *slot = ph.get_slot(rid.slot_no); memcpy(slot, buf, hdr.record_size); } diff --git a/src/rm/rm_scan.cpp b/src/rm/rm_scan.cpp index 1f1c53c..c92c9e2 100644 --- a/src/rm/rm_scan.cpp +++ b/src/rm/rm_scan.cpp @@ -3,7 +3,7 @@ #include RmScan::RmScan(const RmFileHandle *fh) : _fh(fh) { - _rid = {.page_no = RM_FIRST_RECORD_PAGE, .slot_no = -1}; + _rid = Rid(RM_FIRST_RECORD_PAGE, -1); next(); } diff --git a/src/rm/rm_test.cpp b/src/rm/rm_test.cpp index 43d23e9..c4318c6 100644 --- a/src/rm/rm_test.cpp +++ b/src/rm/rm_test.cpp @@ -25,7 +25,7 @@ void check_equal(const RmFileHandle *fh, const std::unordered_maphdr.num_pages - 1), .slot_no = rand() % fh->hdr.num_records_per_page}; + Rid rid(1 + rand() % (fh->hdr.num_pages - 1), rand() % fh->hdr.num_records_per_page); bool mock_exist = mock.count(rid) > 0; bool rm_exist = fh->is_record(rid); EXPECT_EQ(rm_exist, mock_exist); diff --git a/src/sm/sm_manager.cpp b/src/sm/sm_manager.cpp index 5293969..ef69490 100644 --- a/src/sm/sm_manager.cpp +++ b/src/sm/sm_manager.cpp @@ -134,12 +134,7 @@ void SmManager::create_table(const std::string &tab_name, const std::vector col_defs = {{.name = "a", .type = TYPE_INT, .len = 4}, - {.name = "b", .type = TYPE_FLOAT, .len = 4}, - {.name = "c", .type = TYPE_STRING, .len = 256}}; + std::vector col_defs = {ColDef("a", TYPE_INT, 4), ColDef("b", TYPE_FLOAT, 4), + ColDef("c", TYPE_STRING, 256)}; // Create table 1 SmManager::create_table(tab1, col_defs); // Cannot re-create table