smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
buffer_pool.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_BUFFER_POOL_H_
2#define SMOLLERGRAPH_BUFFER_POOL_H_
3
4#include <cstddef>
5#include <mutex>
6#include <unordered_map>
7#include <vector>
8
10
11namespace smollergraph {
12
13class BufferPool;
14
15// A page pinned in a buffer pool. The pool doesn't evict a pinned page, and
16// the handle unpins it when it goes away.
18 public:
19 PageHandle(BufferPool& pool, size_t frame) : pool_(pool), frame_(frame) {}
21 PageHandle(const PageHandle&) = delete;
22 PageHandle& operator=(const PageHandle&) = delete;
23
24 // The page, to read.
25 const Page& read() const;
26 // The page, to change, which marks it to be written back to the file.
27 Page& write();
28
29 private:
30 BufferPool& pool_;
31 size_t frame_;
32};
33
34// Keeps some of a file's pages in memory, in frames, so reading or changing a
35// page there needs no system call. The file is read when a page comes into a
36// frame, and written when a changed page leaves one, or when it's flushed.
38 public:
39 // A pool of up to `frames` pages of the file `pager` has open.
40 BufferPool(Pager& pager, size_t frames);
41 // Writes the changed pages back.
43 BufferPool(const BufferPool&) = delete;
44 BufferPool& operator=(const BufferPool&) = delete;
45
46 // Pins page `id`, reading it from the file when it isn't in a frame. A page
47 // past the end of the file starts out as zeros.
49 // Writes the changed pages back to the file, and keeps them.
50 void Flush();
51 // Writes the changed pages back, and forgets them all, before the pager
52 // opens another file.
53 void Clear();
54
55 private:
56 friend class PageHandle;
57
58 // A frame holds a page while it's in the pool. `referenced` is set each time
59 // the page is fetched, and the clock clears it on its way round.
60 struct Frame {
61 Page page;
62 PageId id = 0;
63 int pins = 0;
64 bool dirty = false;
65 bool referenced = false;
66 };
67
68 // Picks a frame to reuse, by the clock: a page that hasn't been fetched since
69 // the hand last passed it.
70 size_t Victim();
71 void Unpin(size_t frame);
72
73 // sg serve fetches pages on several threads at once, so a latch, a mutex
74 // held for a moment, keeps them from changing the frames and the table
75 // together. A pinned page is safe to read without it.
76 std::mutex latch_;
77 Pager& pager_;
78 size_t capacity_;
79 std::vector<Frame> frames_;
80 // Which frame holds each page in the pool.
81 std::unordered_map<PageId, size_t> table_;
82 size_t hand_ = 0;
83};
84
85} // namespace smollergraph
86
87#endif // SMOLLERGRAPH_BUFFER_POOL_H_
Keeps some of a file's pages in memory, in frames, so reading or changing a page there needs no syste...
Definition buffer_pool.h:37
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(const BufferPool &)=delete
~BufferPool()
Writes the changed pages back.
BufferPool & operator=(const BufferPool &)=delete
A page pinned in a buffer pool.
Definition buffer_pool.h:17
PageHandle & operator=(const PageHandle &)=delete
Page & write()
The page, to change, which marks it to be written back to the file.
const Page & read() const
The page, to read.
PageHandle(const PageHandle &)=delete
PageHandle(BufferPool &pool, size_t frame)
Definition buffer_pool.h:19
A file of pages, each read and written whole, by its number.
Definition pager.h:23
std::array< std::byte, kPageSize > Page
Definition pager.h:20
uint32_t PageId
Definition pager.h:19