wisc_db
page.h
1 
9 #pragma once
10 
11 #include <cstddef>
12 #include <stdint.h>
13 #include <memory>
14 #include <string>
15 #include <cstring>
16 #include "include/types.h"
17 
18 namespace wiscdb {
19 
26 struct PageHeader {
31  std::uint16_t free_space_begin;
32 
37  std::uint16_t free_space_end;
38 
44  std::uint16_t num_slots;
45 
49  std::uint16_t num_free_slots;
50 
55 
60 
67  bool operator==(const PageHeader& rhs) const {
68  return num_slots == rhs.num_slots &&
69  num_free_slots == rhs.num_free_slots &&
70  current_page_number == rhs.current_page_number &&
71  next_page_number == rhs.next_page_number;
72  }
73 };
74 
78 struct PageSlot {
83  bool used;
84 
88  std::uint16_t slot_offset;
89 
93  std::uint16_t slot_length;
94 };
95 
96 class PageIterator;
97 
110 class Page {
111 
112  //Public data members; This section contains constants that can be
113  // referenced externally
114  public:
119  //static const std::size_t SIZE = 8192;
120  // SIZE for testing
121  static const std::size_t SIZE =
122  sizeof(PageHeader) + 10*sizeof(PageSlot) + 10*strlen("Hello!") + 5;
123 
127  static const std::size_t DATA_SIZE = SIZE - sizeof(PageHeader);
128 
132  static const PageId INVALID_NUMBER = 0;
133 
137  static const SlotId INVALID_SLOT = 0;
138 
139  //private data members that define the central structures of a page
140 
145 
146  private:
147 
153  char data[DATA_SIZE];
154 
155  //Public methods that define the Page interface
156  public:
160  Page();
161 
169  RecordId insertRecord(const char* record_data);
170 
181  std::string getRecord(const RecordId& record_id) const;
182 
195  void updateRecord(const RecordId& record_id, const char* record_data);
196 
206  void deleteRecord(const RecordId& record_id);
207 
214  bool hasSpaceForRecord(const char* record_data) const;
215 
221  std::uint16_t getFreeSpace() const;
222 
228  PageId page_number() const;
229 
235  PageId next_page_number() const;
236 
242  PageIterator begin();
243 
250  PageIterator end();
251 
255  void print();
256 
257  //Private helper methods
258  private:
262  void reset();
263 
269  void setPageNumber(const PageId new_page_number);
270 
276  void setNextPageNumber(const PageId new_next_page_number);
277 
288  void deleteRecord(const RecordId& record_id,
289  const bool allow_slot_compaction);
290 
299  PageSlot* getSlot(const SlotId slot_number);
300 
309  const PageSlot& getSlot(const SlotId slot_number) const;
310 
325  SlotId getAvailableSlot();
326 
340  void insertRecordInSlot(const SlotId slot_number,
341  const char* record_data);
342  //private:
351  void validateRecordId(const RecordId& record_id) const;
352 
358  bool isUsed() const;
359 
360  friend class File;
361  friend class PageIterator;
362  friend class PageTest;
363  friend class BufferTest;
364 };
365 
366 /*Assumptions built into the DB that need to be verified
367  * 1. The header information on each page can't be bigger than the page itself
368  * 2. There must be some room for data
369  */
370 static_assert(Page::SIZE > sizeof(PageHeader),
371  "Page size must be large enough to hold header and data.");
372 static_assert(Page::DATA_SIZE > 0,
373  "Page must have some space to hold data.");
374 
375 }
Slot metadata that tracks where a record is in the data space.
Definition: page.h:78
Class which represents a fixed-size database page containing records.
Definition: page.h:110
Definition: buffer.h:14
std::uint16_t SlotId
Identifier for a slot in a page.
Definition: types.h:20
PageId current_page_number
Definition: page.h:54
std::uint16_t slot_length
Definition: page.h:93
static const std::size_t SIZE
Definition: page.h:121
std::uint16_t slot_offset
Definition: page.h:88
Identifier for a record in a page.
Definition: types.h:30
std::uint32_t PageId
Identifier for a page in a file.
Definition: types.h:15
bool operator==(const PageHeader &rhs) const
Definition: page.h:67
PageId next_page_number
Definition: page.h:59
PageHeader header
Definition: page.h:144
std::uint16_t free_space_end
Definition: page.h:37
std::uint16_t num_free_slots
Definition: page.h:49
Iterator for iterating over the records in a page.
Definition: page_iterator.h:23
std::uint16_t free_space_begin
Definition: page.h:31
Represents a file in the filesystem containing pages.
Definition: file.h:71
static const std::size_t DATA_SIZE
Definition: page.h:127
std::uint16_t num_slots
Definition: page.h:44
Header metadata in a page.
Definition: page.h:26
bool used
Definition: page.h:83