smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
pager.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_PAGER_H_
2#define SMOLLERGRAPH_PAGER_H_
3
4#include <array>
5#include <atomic>
6#include <cstddef>
7#include <cstdint>
8#include <string>
9#include <string_view>
10
11#include "absl/status/status.h"
12
13namespace smollergraph {
14
15// What the database reads and writes at a time: 4 KiB, the size of a block on
16// most file systems and disks.
17inline constexpr size_t kPageSize = 4096;
18
19using PageId = uint32_t;
20using Page = std::array<std::byte, kPageSize>;
21
22// A file of pages, each read and written whole, by its number.
23class Pager {
24 public:
25 Pager() = default;
26 ~Pager();
27 Pager(const Pager&) = delete;
28 Pager& operator=(const Pager&) = delete;
29
30 // Opens the file at `path`, creating it when it isn't there.
31 absl::Status Open(const std::string& path);
32 // Opens a file with no name, which is deleted once it's closed.
33 absl::Status OpenTemporary();
34
35 PageId page_count() const { return page_count_; }
36 // Reads page `id`, which has to be below page_count().
37 void Read(PageId id, Page& page) const;
38 // Writes page `id`. Writing the page at page_count() adds it to the file.
39 void Write(PageId id, const Page& page);
40
41 // How many pages this pager has read and written since it opened.
42 uint64_t reads() const { return reads_; }
43 uint64_t writes() const { return writes_; }
44
45 private:
46 absl::Status Adopt(int fd, std::string_view name);
47
48 int fd_ = -1;
49 PageId page_count_ = 0;
50 // Atomic, as sg serve reads on several threads at once, and mutable, as
51 // Read is const and still counts.
52 mutable std::atomic<uint64_t> reads_ = 0;
53 std::atomic<uint64_t> writes_ = 0;
54};
55
56} // namespace smollergraph
57
58#endif // SMOLLERGRAPH_PAGER_H_
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
uint64_t writes() const
Definition pager.h:43
absl::Status Open(const std::string &path)
Opens the file at path, creating it when it isn't there.
Definition pager.cc:32
absl::Status OpenTemporary()
Opens a file with no name, which is deleted once it's closed.
Definition pager.cc:36
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
uint64_t reads() const
How many pages this pager has read and written since it opened.
Definition pager.h:42
Pager(const Pager &)=delete
Pager & operator=(const Pager &)=delete
std::array< std::byte, kPageSize > Page
Definition pager.h:20
uint32_t PageId
Definition pager.h:19
constexpr size_t kPageSize
What the database reads and writes at a time: 4 KiB, the size of a block on most file systems and dis...
Definition pager.h:17
Path path
Definition plan.cc:115