Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
connortsui20 committed Sep 10, 2024
1 parent 7d609c1 commit 4bbeb91
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 170 deletions.
8 changes: 0 additions & 8 deletions src/include/buffer/buffer_pool_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "common/config.h"
#include "recovery/log_manager.h"
#include "storage/disk/disk_scheduler.h"
#include "storage/disk/write_back_cache.h"
#include "storage/page/page.h"
#include "storage/page/page_guard.h"

Expand Down Expand Up @@ -155,13 +154,6 @@ class BufferPoolManager {
/** @brief A pointer to the disk scheduler. */
std::unique_ptr<DiskScheduler> disk_scheduler_;

/**
* @brief A write-back cache.
*
* Note: You may want to use this cache to optimize the write requests for the leaderboard task.
*/
WriteBackCache write_back_cache_ __attribute__((__unused__));

/**
* @brief A pointer to the log manager.
*
Expand Down
35 changes: 31 additions & 4 deletions src/include/storage/disk/disk_manager_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
// Copyright (c) 2015-2020, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "storage/disk/disk_manager.h"

#include <array>
#include <cassert>
#include <chrono> // NOLINT
#include <cstring>
#include <fstream>
Expand All @@ -27,10 +31,12 @@
#include "common/exception.h"
#include "common/logger.h"
#include "fmt/core.h"
#include "storage/disk/disk_manager.h"

namespace bustub {

/** @brief The default size of the database file. */
static const size_t DEFAULT_DB_IO_SIZE = 16;

/**
* DiskManagerMemory replicates the utility of DiskManager on memory. It is primarily used for
* data structure performance testing.
Expand Down Expand Up @@ -72,12 +78,31 @@ class DiskManagerMemory : public DiskManager {
*/
class DiskManagerUnlimitedMemory : public DiskManager {
public:
DiskManagerUnlimitedMemory() { std::fill(recent_access_.begin(), recent_access_.end(), -1); }
DiskManagerUnlimitedMemory() {
std::scoped_lock l(mutex_);
while (data_.size() < pages_ + 1) {
data_.push_back(std::make_shared<ProtectedPage>());
}
std::fill(recent_access_.begin(), recent_access_.end(), -1);
}

/**
* This function should increase the disk space, but since this is memory we just resize the vector.
*/
void IncreaseDiskSpace(size_t pages) override { data_.resize(pages + 1, std::make_shared<ProtectedPage>()); }
void IncreaseDiskSpace(size_t pages) override {
std::scoped_lock l(mutex_);

if (pages < pages_) {
return;
}

while (data_.size() < pages + 1) {
data_.push_back(std::make_shared<ProtectedPage>());
}
assert(data_.size() == pages + 1);

pages_ = pages;
}

/**
* Write a page to the database file.
Expand Down Expand Up @@ -124,7 +149,7 @@ class DiskManagerUnlimitedMemory : public DiskManager {
return;
}
if (data_[page_id] == nullptr) {
fmt::println(stderr, "page {} not exist", page_id);
fmt::println(stderr, "page {} not exist", page_id, pages_);
std::terminate();
return;
}
Expand Down Expand Up @@ -186,6 +211,8 @@ class DiskManagerUnlimitedMemory : public DiskManager {
std::mutex mutex_;
std::optional<std::thread::id> thread_id_;
std::vector<std::shared_ptr<ProtectedPage>> data_;

size_t pages_{DEFAULT_DB_IO_SIZE};
};

} // namespace bustub
97 changes: 0 additions & 97 deletions src/include/storage/disk/write_back_cache.h

This file was deleted.

10 changes: 4 additions & 6 deletions src/storage/disk/disk_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ static const size_t DEFAULT_DB_IO_SIZE = 16;
* Constructor: open/create a single database file & log file
* @input db_file: database file name
*/
DiskManager::DiskManager(const std::filesystem::path &db_file) : file_name_(db_file) {
DiskManager::DiskManager(const std::filesystem::path &db_file)
: file_name_(db_file), pages_(0), page_capacity_(DEFAULT_DB_IO_SIZE) {
log_name_ = file_name_.filename().stem().string() + ".log";

log_io_.open(log_name_, std::ios::binary | std::ios::in | std::ios::app | std::ios::out);
Expand All @@ -60,9 +61,6 @@ DiskManager::DiskManager(const std::filesystem::path &db_file) : file_name_(db_f
}

// Initialize the database file.
pages_ = 0;
page_capacity_ = DEFAULT_DB_IO_SIZE;

std::filesystem::resize_file(db_file, (page_capacity_ + 1) * BUSTUB_PAGE_SIZE);
assert(static_cast<size_t>(GetFileSize(file_name_)) >= page_capacity_ * BUSTUB_PAGE_SIZE);

Expand All @@ -84,12 +82,12 @@ void DiskManager::ShutDown() {
* @brief Increases the size of the file to fit the specified number of pages.
*/
void DiskManager::IncreaseDiskSpace(size_t pages) {
std::scoped_lock scoped_db_io_latch(db_io_latch_);

if (pages < pages_) {
return;
}

std::scoped_lock scoped_db_io_latch(db_io_latch_);

pages_ = pages;
while (page_capacity_ < pages_) {
page_capacity_ *= 2;
Expand Down
55 changes: 0 additions & 55 deletions test/storage/write_back_cache_test.cpp

This file was deleted.

0 comments on commit 4bbeb91

Please sign in to comment.