smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
record_file.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_RECORD_FILE_H_
2#define SMOLLERGRAPH_RECORD_FILE_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <cstring>
7#include <string>
8#include <type_traits>
9
10#include "absl/status/status.h"
12#include "smollergraph/pager.h"
13
14namespace smollergraph {
15
16// A file of fixed-size records, packed into pages after a header page, which
17// holds how many records there are. A record's id says which page it's on and
18// where, so finding one needs no search. Its pages go through a buffer pool,
19// which writes the changed ones to the file when it needs their frames, or
20// when it's flushed or closed.
21template <typename Record>
23 static_assert(std::is_trivially_copyable_v<Record>,
24 "records are copied to and from pages as bytes");
25
26 public:
27 static constexpr uint32_t kPerPage = kPageSize / sizeof(Record);
28 // The most pages the pool keeps in memory, 64 MiB of them, which holds the
29 // million relationships we benchmark.
30 static constexpr size_t kPoolPages = 16384;
31
32 // Opening another file writes back the changes to the one that was open.
33 absl::Status Open(const std::string& path) {
34 pool_.Clear();
35 if (absl::Status status = pager_.Open(path); !status.ok()) {
36 return status;
37 }
38 LoadCount();
39 return absl::OkStatus();
40 }
41 absl::Status OpenTemporary() {
42 pool_.Clear();
43 if (absl::Status status = pager_.OpenTemporary(); !status.ok()) {
44 return status;
45 }
46 LoadCount();
47 return absl::OkStatus();
48 }
49 // Writes the changed pages to the file.
50 void Flush() { pool_.Flush(); }
51
52 uint32_t count() const { return count_; }
53 uint64_t page_reads() const { return pager_.reads(); }
54 uint64_t page_writes() const { return pager_.writes(); }
55
56 // Page 0 is the header, so the records start on page 1.
57 static PageId PageOf(uint32_t id) { return 1 + id / kPerPage; }
58 static size_t OffsetOf(uint32_t id) {
59 return (id % kPerPage) * sizeof(Record);
60 }
61
62 // Reads record `id`, which has to be below count().
63 Record Read(uint32_t id) const {
64 PageHandle page = pool_.Fetch(PageOf(id));
65 Record record;
66 std::memcpy(&record, page.read().data() + OffsetOf(id), sizeof(Record));
67 return record;
68 }
69
70 void Write(uint32_t id, const Record& record) {
71 PageHandle page = pool_.Fetch(PageOf(id));
72 std::memcpy(page.write().data() + OffsetOf(id), &record, sizeof(Record));
73 }
74
75 // Adds a record after the last one, and returns its id.
76 uint32_t Append(const Record& record) {
77 uint32_t id = count_;
78 Write(id, record);
79 count_ = id + 1;
80 PageHandle header = pool_.Fetch(0);
81 std::memcpy(header.write().data(), &count_, sizeof(count_));
82 return id;
83 }
84
85 private:
86 void LoadCount() {
87 count_ = 0;
88 if (pager_.page_count() == 0) return;
89 PageHandle header = pool_.Fetch(0);
90 std::memcpy(&count_, header.read().data(), sizeof(count_));
91 }
92
93 // The pool is declared after the pager, so it's destroyed first, and
94 // writes its changed pages while the file is still open. It's mutable, as
95 // Read is const and still brings pages into the pool.
96 Pager pager_;
97 mutable BufferPool pool_{pager_, kPoolPages};
98 uint32_t count_ = 0;
99};
100
101} // namespace smollergraph
102
103#endif // SMOLLERGRAPH_RECORD_FILE_H_
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.
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.
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
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
A file of fixed-size records, packed into pages after a header page, which holds how many records the...
Definition record_file.h:22
static constexpr size_t kPoolPages
The most pages the pool keeps in memory, 64 MiB of them, which holds the million relationships we ben...
Definition record_file.h:30
absl::Status Open(const std::string &path)
Opening another file writes back the changes to the one that was open.
Definition record_file.h:33
absl::Status OpenTemporary()
Definition record_file.h:41
static PageId PageOf(uint32_t id)
Page 0 is the header, so the records start on page 1.
Definition record_file.h:57
static constexpr uint32_t kPerPage
Definition record_file.h:27
void Write(uint32_t id, const Record &record)
Definition record_file.h:70
uint64_t page_reads() const
Definition record_file.h:53
uint32_t Append(const Record &record)
Adds a record after the last one, and returns its id.
Definition record_file.h:76
uint64_t page_writes() const
Definition record_file.h:54
uint32_t count() const
Definition record_file.h:52
static size_t OffsetOf(uint32_t id)
Definition record_file.h:58
Record Read(uint32_t id) const
Reads record id, which has to be below count().
Definition record_file.h:63
void Flush()
Writes the changed pages to the file.
Definition record_file.h:50
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