BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends
Public Member Functions | Static Public Attributes | Friends
badgerdb::Page Class Reference

Class which represents a fixed-size database page containing records. More...

#include <page.h>

List of all members.

Public Member Functions

 Page ()
RecordId insertRecord (const std::string &record_data)
std::string getRecord (const RecordId &record_id) const
void updateRecord (const RecordId &record_id, const std::string &record_data)
void deleteRecord (const RecordId &record_id)
bool hasSpaceForRecord (const std::string &record_data) const
std::uint16_t getFreeSpace () const
PageId page_number () const
PageId next_page_number () const
PageIterator begin ()
PageIterator end ()

Static Public Attributes

static const std::size_t SIZE = 8192
static const std::size_t DATA_SIZE = SIZE - sizeof(PageHeader)
static const PageId INVALID_NUMBER = 0
static const SlotId INVALID_SLOT = 0

Friends

class File
class PageIterator
class PageTest
class BufferTest

Detailed Description

Class which represents a fixed-size database page containing records.

A page is a fixed-size unit of data storage. Each page holds zero or more records, which consist of arbitrary binary data. Records are placed into slots and identified by a RecordId. Although a record's actual contents may be moved on the page, accessing a record by its slot is consistent.

Warning:
This class is not threadsafe.

Definition at line 107 of file page.h.


Constructor & Destructor Documentation

Constructs a new, uninitialized page.

Definition at line 19 of file page.cpp.

           {
  initialize();
}

Member Function Documentation

Returns an iterator at the first record in the page.

Returns:
Iterator at first record of page.

Definition at line 196 of file page.cpp.

                         {
  return PageIterator(this);
}
void badgerdb::Page::deleteRecord ( const RecordId record_id)

Deletes the record with the given ID. Page is compacted upon delete to ensure that data of all records is contiguous. Slot array is compacted if the slot deleted is at the end of the slot array.

Parameters:
record_idID of the record to delete.

Definition at line 66 of file page.cpp.

                                                 {
  deleteRecord(record_id, true /* allow_slot_compaction */);
}

Returns an iterator representing the record after the last record in the page. This iterator should not be dereferenced.

Returns:
Iterator representing record after the last record in the page.

Definition at line 200 of file page.cpp.

                       {
  const RecordId& end_record_id = {page_number(), Page::INVALID_SLOT};
  return PageIterator(this, end_record_id);
}
std::uint16_t badgerdb::Page::getFreeSpace ( ) const [inline]

Returns this page's free space in bytes.

Returns:
Free space in bytes.

Definition at line 185 of file page.h.

                                   { return header_.free_space_upper_bound -
                                              header_.free_space_lower_bound; }
std::string badgerdb::Page::getRecord ( const RecordId record_id) const

Returns the record with the given ID. Returned data is a copy of what is stored on the page; use updateRecord to change it.

See also:
updateRecord
Parameters:
record_idID of the record to return.
Returns:
The record.

Definition at line 43 of file page.cpp.

                                                         {
  validateRecordId(record_id);
  const PageSlot& slot = getSlot(record_id.slot_number);
  return data_.substr(slot.item_offset, slot.item_length);
}
bool badgerdb::Page::hasSpaceForRecord ( const std::string &  record_data) const

Returns true if the page has enough free space to hold the given data.

Parameters:
record_dataBytes that compose the record.
Returns:
Whether the page can hold the data.

Definition at line 125 of file page.cpp.

                                                               {
  std::size_t record_size = record_data.length();
  if (header_.num_free_slots == 0) {
    record_size += sizeof(PageSlot);
  }
  return record_size <= getFreeSpace();
}
RecordId badgerdb::Page::insertRecord ( const std::string &  record_data)

Inserts a new record into the page.

Parameters:
record_dataBytes that compose the record.
Returns:
ID of the newly inserted record.

Definition at line 33 of file page.cpp.

                                                        {
  if (!hasSpaceForRecord(record_data)) {
    throw InsufficientSpaceException(
        page_number(), record_data.length(), getFreeSpace());
  }
  const SlotId slot_number = getAvailableSlot();
  insertRecordInSlot(slot_number, record_data);
  return {page_number(), slot_number};
}

Returns the number of the next used page this page in its file.

Returns:
Page number of next used page in file.

Definition at line 200 of file page.h.

{ return header_.next_page_number; }
PageId badgerdb::Page::page_number ( ) const [inline]

Returns this page's number in its file.

Returns:
Page number.

Definition at line 193 of file page.h.

{ return header_.current_page_number; }
void badgerdb::Page::updateRecord ( const RecordId record_id,
const std::string &  record_data 
)

Updates the record with the given ID, replacing its data with a new version. This is equivalent to deleting the old record and inserting a new one, with the exception that the record ID will not change.

Parameters:
record_idID of record to update.
record_dataUpdated bytes that compose the record.

Definition at line 49 of file page.cpp.

                                                      {
  validateRecordId(record_id);
  const PageSlot* slot = getSlot(record_id.slot_number);
  const std::size_t free_space_after_delete =
      getFreeSpace() + slot->item_length;
  if (record_data.length() > free_space_after_delete) {
    throw InsufficientSpaceException(
        page_number(), record_data.length(), free_space_after_delete);
  }
  // We have to disallow slot compaction here because we're going to place the
  // record data in the same slot, and compaction might delete the slot if we
  // permit it.
  deleteRecord(record_id, false /* allow_slot_compaction */);
  insertRecordInSlot(record_id.slot_number, record_data);
}

Member Data Documentation

const std::size_t badgerdb::Page::DATA_SIZE = SIZE - sizeof(PageHeader) [static]

Size of page free space area in bytes.

Definition at line 118 of file page.h.

Number of page indicating that it's invalid.

Definition at line 123 of file page.h.

Number of slot indicating that it's invalid.

Definition at line 128 of file page.h.

const std::size_t badgerdb::Page::SIZE = 8192 [static]

Page size in bytes. If this is changed, database files created with a different page size value will be unreadable by the resulting binaries.

Definition at line 113 of file page.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Friends