BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends Pages
page.h
1 
8 #pragma once
9 
10 #include <cstddef>
11 #include <stdint.h>
12 #include <memory>
13 #include <string>
14 
15 #include "types.h"
16 
17 namespace badgerdb {
18 
25 struct PageHeader {
30  std::uint16_t free_space_lower_bound;
31 
36  std::uint16_t free_space_upper_bound;
37 
44 
49 
54 
59 
66  bool operator==(const PageHeader& rhs) const {
67  return num_slots == rhs.num_slots &&
71  }
72 };
73 
77 struct PageSlot {
82  bool used;
83 
87  std::uint16_t item_offset;
88 
92  std::uint16_t item_length;
93 };
94 
95 class PageIterator;
96 
107 class Page {
108  public:
113  static const std::size_t SIZE = 8192;
114 
118  static const std::size_t DATA_SIZE = SIZE - sizeof(PageHeader);
119 
123  static const PageId INVALID_NUMBER = 0;
124 
128  static const SlotId INVALID_SLOT = 0;
129 
133  Page();
134 
141  RecordId insertRecord(const std::string& record_data);
142 
151  std::string getRecord(const RecordId& record_id) const;
152 
161  void updateRecord(const RecordId& record_id, const std::string& record_data);
162 
170  void deleteRecord(const RecordId& record_id);
171 
178  bool hasSpaceForRecord(const std::string& record_data) const;
179 
185  std::uint16_t getFreeSpace() const { return header_.free_space_upper_bound -
186  header_.free_space_lower_bound; }
187 
193  PageId page_number() const { return header_.current_page_number; }
194 
200  PageId next_page_number() const { return header_.next_page_number; }
201 
208 
215  PageIterator end();
216 
217  private:
221  void initialize();
222 
228  void set_page_number(const PageId new_page_number) {
229  header_.current_page_number = new_page_number;
230  }
231 
237  void set_next_page_number(const PageId new_next_page_number) {
238  header_.next_page_number = new_next_page_number;
239  }
240 
251  void deleteRecord(const RecordId& record_id,
252  const bool allow_slot_compaction);
253 
262  PageSlot* getSlot(const SlotId slot_number);
263 
272  const PageSlot& getSlot(const SlotId slot_number) const;
273 
288  SlotId getAvailableSlot();
289 
303  void insertRecordInSlot(const SlotId slot_number,
304  const std::string& record_data);
305 
314  void validateRecordId(const RecordId& record_id) const;
315 
321  bool isUsed() const { return page_number() != INVALID_NUMBER; }
322 
326  PageHeader header_;
327 
333  std::string data_;
334 
335  friend class File;
336  friend class PageIterator;
337  friend class PageTest;
338  friend class BufferTest;
339 };
340 
341 static_assert(Page::SIZE > sizeof(PageHeader),
342  "Page size must be large enough to hold header and data.");
343 static_assert(Page::DATA_SIZE > 0,
344  "Page must have some space to hold data.");
345 
346 }
std::uint16_t item_offset
Definition: page.h:87
bool operator==(const PageHeader &rhs) const
Definition: page.h:66
PageId next_page_number() const
Definition: page.h:200
SlotId num_free_slots
Definition: page.h:48
SlotId num_slots
Definition: page.h:43
void updateRecord(const RecordId &record_id, const std::string &record_data)
Definition: page.cpp:49
std::string getRecord(const RecordId &record_id) const
Definition: page.cpp:43
PageId next_page_number
Definition: page.h:58
std::uint16_t free_space_upper_bound
Definition: page.h:36
Iterator for iterating over the records in a page.
Definition: page_iterator.h:23
static const std::size_t DATA_SIZE
Definition: page.h:118
std::uint32_t PageId
Identifier for a page in a file.
Definition: types.h:15
static const std::size_t SIZE
Definition: page.h:113
std::uint16_t free_space_lower_bound
Definition: page.h:30
Class which represents a fixed-size database page containing records.
Definition: page.h:107
std::uint16_t item_length
Definition: page.h:92
PageId page_number() const
Definition: page.h:193
PageIterator begin()
Definition: page.cpp:196
PageIterator end()
Definition: page.cpp:200
Slot metadata that tracks where a record is in the data space.
Definition: page.h:77
Identifier for a record in a page.
Definition: types.h:30
static const SlotId INVALID_SLOT
Definition: page.h:128
std::uint16_t getFreeSpace() const
Definition: page.h:185
static const PageId INVALID_NUMBER
Definition: page.h:123
std::uint16_t SlotId
Identifier for a slot in a page.
Definition: types.h:20
bool hasSpaceForRecord(const std::string &record_data) const
Definition: page.cpp:125
PageId current_page_number
Definition: page.h:53
Header metadata in a page.
Definition: page.h:25
void deleteRecord(const RecordId &record_id)
Definition: page.cpp:66
RecordId insertRecord(const std::string &record_data)
Definition: page.cpp:33