smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
buffer_pool.cc
Go to the documentation of this file.
2
3#include <cstdio>
4#include <cstdlib>
5
6namespace smollergraph {
7
8PageHandle::~PageHandle() { pool_.Unpin(frame_); }
9
10const Page& PageHandle::read() const { return pool_.frames_[frame_].page; }
11
13 std::lock_guard<std::mutex> lock(pool_.latch_);
14 BufferPool::Frame& frame = pool_.frames_[frame_];
15 frame.dirty = true;
16 return frame.page;
17}
18
19BufferPool::BufferPool(Pager& pager, size_t frames)
20 : pager_(pager), capacity_(frames) {
21 // Frames are made as pages come in, so a pool that doesn't fill up stays
22 // small.
23 frames_.reserve(capacity_);
24}
25
27
29 // The latch is held while a page is read or written, too, which keeps this
30 // simple, and makes other threads wait for the disk.
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];
35 ++frame.pins;
36 frame.referenced = true;
37 return PageHandle(*this, found->second);
38 }
39
40 size_t at;
41 if (frames_.size() < capacity_) {
42 at = frames_.size();
43 frames_.emplace_back();
44 } else {
45 at = Victim();
46 Frame& old = frames_[at];
47 if (old.dirty) pager_.Write(old.id, old.page);
48 table_.erase(old.id);
49 }
50 Frame& frame = frames_[at];
51 frame.id = id;
52 frame.pins = 1;
53 frame.dirty = false;
54 frame.referenced = true;
55 if (id < pager_.page_count()) {
56 pager_.Read(id, frame.page);
57 } else {
58 frame.page.fill(std::byte{0});
59 }
60 table_[id] = at;
61 return PageHandle(*this, at);
62}
63
64size_t BufferPool::Victim() {
65 // The first time round, the hand clears the referenced pages it passes, so
66 // the second time round it finds one, unless all of them are pinned.
67 for (size_t step = 0; step < 2 * frames_.size(); ++step) {
68 size_t at = hand_;
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;
74 continue;
75 }
76 return at;
77 }
78 std::fprintf(stderr,
79 "smollergraph: the buffer pool's pages are all pinned\n");
80 std::abort();
81}
82
83void BufferPool::Unpin(size_t frame) {
84 std::lock_guard<std::mutex> lock(latch_);
85 --frames_[frame].pins;
86}
87
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);
93 frame.dirty = false;
94 }
95}
96
98 Flush();
99 std::lock_guard<std::mutex> lock(latch_);
100 frames_.clear();
101 table_.clear();
102 hand_ = 0;
103}
104
105} // namespace smollergraph
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.
Definition buffer_pool.h:17
Page & write()
The page, to change, which marks it to be written back to the file.
const Page & read() const
The page, to read.
A file of pages, each read and written whole, by its number.
Definition pager.h:23
void Read(PageId id, Page &page) const
Reads page id, which has to be below page_count().
Definition pager.cc:59
void Write(PageId id, const Page &page)
Writes page id. Writing the page at page_count() adds it to the file.
Definition pager.cc:68
PageId page_count() const
Definition pager.h:35
std::array< std::byte, kPageSize > Page
Definition pager.h:20
uint32_t PageId
Definition pager.h:19