1#ifndef SMOLLERGRAPH_RECORD_FILE_H_
2#define SMOLLERGRAPH_RECORD_FILE_H_
10#include "absl/status/status.h"
21template <
typename Record>
23 static_assert(std::is_trivially_copyable_v<Record>,
24 "records are copied to and from pages as bytes");
35 if (absl::Status status = pager_.
Open(
path); !status.ok()) {
39 return absl::OkStatus();
43 if (absl::Status status = pager_.
OpenTemporary(); !status.ok()) {
47 return absl::OkStatus();
52 uint32_t
count()
const {
return count_; }
59 return (
id %
kPerPage) *
sizeof(Record);
63 Record
Read(uint32_t
id)
const {
66 std::memcpy(&record, page.
read().data() +
OffsetOf(
id),
sizeof(Record));
70 void Write(uint32_t
id,
const Record& record) {
72 std::memcpy(page.
write().data() +
OffsetOf(
id), &record,
sizeof(Record));
76 uint32_t
Append(
const Record& record) {
81 std::memcpy(header.
write().data(), &count_,
sizeof(count_));
90 std::memcpy(&count_, header.
read().data(),
sizeof(count_));
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.
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 fixed-size records, packed into pages after a header page, which holds how many records the...
static constexpr size_t kPoolPages
The most pages the pool keeps in memory, 64 MiB of them, which holds the million relationships we ben...
absl::Status Open(const std::string &path)
Opening another file writes back the changes to the one that was open.
absl::Status OpenTemporary()
static PageId PageOf(uint32_t id)
Page 0 is the header, so the records start on page 1.
static constexpr uint32_t kPerPage
void Write(uint32_t id, const Record &record)
uint64_t page_reads() const
uint32_t Append(const Record &record)
Adds a record after the last one, and returns its id.
uint64_t page_writes() const
static size_t OffsetOf(uint32_t id)
Record Read(uint32_t id) const
Reads record id, which has to be below count().
void Flush()
Writes the changed pages to the file.
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...