13 std::lock_guard<std::mutex> lock(pool_.latch_);
14 BufferPool::Frame& frame = pool_.frames_[frame_];
20 : pager_(pager), capacity_(frames) {
23 frames_.reserve(capacity_);
31 std::lock_guard<std::mutex> lock(latch_);
32 auto found = table_.find(
id);
33 if (found != table_.end()) {
34 Frame& frame = frames_[found->second];
36 frame.referenced =
true;
41 if (frames_.size() < capacity_) {
43 frames_.emplace_back();
46 Frame& old = frames_[at];
47 if (old.dirty) pager_.
Write(old.id, old.page);
50 Frame& frame = frames_[at];
54 frame.referenced =
true;
56 pager_.
Read(
id, frame.page);
58 frame.page.fill(std::byte{0});
64size_t BufferPool::Victim() {
67 for (
size_t step = 0; step < 2 * frames_.size(); ++step) {
69 hand_ = (hand_ + 1) % frames_.size();
70 Frame& frame = frames_[at];
71 if (frame.pins > 0)
continue;
72 if (frame.referenced) {
73 frame.referenced =
false;
79 "smollergraph: the buffer pool's pages are all pinned\n");
83void BufferPool::Unpin(
size_t frame) {
84 std::lock_guard<std::mutex> lock(latch_);
85 --frames_[frame].pins;
89 std::lock_guard<std::mutex> lock(latch_);
90 for (Frame& frame : frames_) {
91 if (!frame.dirty)
continue;
92 pager_.
Write(frame.id, frame.page);
99 std::lock_guard<std::mutex> lock(latch_);
void Flush()
Writes the changed pages back to the file, and keeps them.
PageHandle Fetch(PageId id)
Pins page id, reading it from the file when it isn't in a frame.
void Clear()
Writes the changed pages back, and forgets them all, before the pager opens another file.
~BufferPool()
Writes the changed pages back.
BufferPool(Pager &pager, size_t frames)
A pool of up to frames pages of the file pager has open.
A page pinned in a buffer pool.
Page & write()
The page, to change, which marks it to be written back to the file.
const Page & read() const
The page, to read.
std::array< std::byte, kPageSize > Page