10 #include "exceptions/insufficient_space_exception.h"
11 #include "exceptions/invalid_record_exception.h"
12 #include "exceptions/invalid_slot_exception.h"
13 #include "exceptions/slot_in_use_exception.h"
14 #include "page_iterator.h"
23 void Page::initialize() {
38 const SlotId slot_number = getAvailableSlot();
39 insertRecordInSlot(slot_number, record_data);
44 validateRecordId(record_id);
50 const std::string& record_data) {
51 validateRecordId(record_id);
53 const std::size_t free_space_after_delete =
55 if (record_data.length() > free_space_after_delete) {
57 page_number(), record_data.length(), free_space_after_delete);
63 insertRecordInSlot(record_id.
slot_number, record_data);
71 const bool allow_slot_compaction) {
72 validateRecordId(record_id);
78 std::size_t move_bytes = 0;
93 const std::string& data_to_move = data_.substr(move_offset, move_bytes);
94 data_.replace(move_offset + slot->
item_length, move_bytes, data_to_move);
107 int num_slots_to_delete = 1;
110 const PageSlot* other_slot = getSlot(header_.
num_slots - i);
111 if (!other_slot->used) {
112 ++num_slots_to_delete;
119 header_.
num_slots -= num_slots_to_delete;
126 std::size_t record_size = record_data.length();
135 &data_[(slot_number - 1) *
sizeof(
PageSlot)]);
138 const PageSlot& Page::getSlot(
const SlotId slot_number)
const {
139 return *
reinterpret_cast<const PageSlot*
>(
140 &data_[(slot_number - 1) *
sizeof(PageSlot)]);
143 SlotId Page::getAvailableSlot() {
148 const PageSlot* slot = getSlot(i);
164 return static_cast<SlotId>(slot_number);
167 void Page::insertRecordInSlot(
const SlotId slot_number,
168 const std::string& record_data) {
171 throw InvalidSlotException(
page_number(), slot_number);
173 PageSlot* slot = getSlot(slot_number);
175 throw SlotInUseException(
page_number(), slot_number);
177 const int record_length = record_data.length();
179 slot->item_length = record_length;
183 data_.replace(slot->item_offset, slot->item_length, record_data);
186 void Page::validateRecordId(
const RecordId& record_id)
const {
188 throw InvalidRecordException(record_id,
page_number());
190 const PageSlot& slot = getSlot(record_id.slot_number);
192 throw InvalidRecordException(record_id,
page_number());
std::uint16_t item_offset
void updateRecord(const RecordId &record_id, const std::string &record_data)
std::string getRecord(const RecordId &record_id) const
Iterator for iterating over the records in a page.
static const std::size_t DATA_SIZE
std::uint16_t item_length
PageId page_number() const
Slot metadata that tracks where a record is in the data space.
Identifier for a record in a page.
static const SlotId INVALID_SLOT
std::uint16_t getFreeSpace() const
static const PageId INVALID_NUMBER
An exception that is thrown when a record is attempted to be inserted into a page that doesn't have s...
std::uint16_t SlotId
Identifier for a slot in a page.
bool hasSpaceForRecord(const std::string &record_data) const
void deleteRecord(const RecordId &record_id)
RecordId insertRecord(const std::string &record_data)