BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
Public Member Functions | Static Public Attributes | Friends | List of all members
badgerdb::Page Class Reference

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

#include <page.h>

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 PageFile
 
class BlobFile
 
class PageIterator
 

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 108 of file page.h.

Constructor & Destructor Documentation

badgerdb::Page::Page ( )

Constructs a new, uninitialized page.

Definition at line 21 of file page.cpp.

21  {
22  initialize();
23 }

Member Function Documentation

PageIterator badgerdb::Page::begin ( )

Returns an iterator at the first record in the page.

Returns
Iterator at first record of page.

Definition at line 211 of file page.cpp.

211  {
212  return PageIterator(this);
213 }
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 71 of file page.cpp.

71  {
72  deleteRecord(record_id, true /* allow_slot_compaction */);
73 }
void deleteRecord(const RecordId &record_id)
Definition: page.cpp:71
PageIterator badgerdb::Page::end ( )

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 215 of file page.cpp.

215  {
216  const RecordId& end_record_id = {page_number(), Page::INVALID_SLOT};
217  return PageIterator(this, end_record_id);
218 }
PageId page_number() const
Definition: page.h:194
static const SlotId INVALID_SLOT
Definition: page.h:129
std::uint16_t badgerdb::Page::getFreeSpace ( ) const
inline

Returns this page's free space in bytes.

Returns
Free space in bytes.

Definition at line 186 of file page.h.

186  { return header_.free_space_upper_bound -
187  header_.free_space_lower_bound; }
std::uint16_t free_space_upper_bound
Definition: page.h:37
std::uint16_t free_space_lower_bound
Definition: page.h:31
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 46 of file page.cpp.

46  {
47  validateRecordId(record_id);
48  const PageSlot& slot = getSlot(record_id.slot_number);
49  std::string retStr = std::string(data_, DATA_SIZE).substr(slot.item_offset, slot.item_length);
50 
51  return retStr;
52 }
static const std::size_t DATA_SIZE
Definition: page.h:119
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 138 of file page.cpp.

138  {
139  std::size_t record_size = record_data.length();
140  if (header_.num_free_slots == 0) {
141  record_size += sizeof(PageSlot);
142  }
143  return record_size <= getFreeSpace();
144 }
SlotId num_free_slots
Definition: page.h:49
std::uint16_t getFreeSpace() const
Definition: page.h:186
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 36 of file page.cpp.

36  {
37  if (!hasSpaceForRecord(record_data)) {
38  throw InsufficientSpaceException(
39  page_number(), record_data.length(), getFreeSpace());
40  }
41  const SlotId slot_number = getAvailableSlot();
42  insertRecordInSlot(slot_number, record_data);
43  return {page_number(), slot_number};
44 }
PageId page_number() const
Definition: page.h:194
std::uint16_t getFreeSpace() const
Definition: page.h:186
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:138
PageId badgerdb::Page::next_page_number ( ) const
inline

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 201 of file page.h.

201 { return header_.next_page_number; }
PageId next_page_number
Definition: page.h:59
PageId badgerdb::Page::page_number ( ) const
inline

Returns this page's number in its file.

Returns
Page number.

Definition at line 194 of file page.h.

194 { return header_.current_page_number; }
PageId current_page_number
Definition: page.h:54
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 54 of file page.cpp.

55  {
56  validateRecordId(record_id);
57  const PageSlot* slot = getSlot(record_id.slot_number);
58  const std::size_t free_space_after_delete =
59  getFreeSpace() + slot->item_length;
60  if (record_data.length() > free_space_after_delete) {
61  throw InsufficientSpaceException(
62  page_number(), record_data.length(), free_space_after_delete);
63  }
64  // We have to disallow slot compaction here because we're going to place the
65  // record data in the same slot, and compaction might delete the slot if we
66  // permit it.
67  deleteRecord(record_id, false /* allow_slot_compaction */);
68  insertRecordInSlot(record_id.slot_number, record_data);
69 }
PageId page_number() const
Definition: page.h:194
std::uint16_t getFreeSpace() const
Definition: page.h:186
void deleteRecord(const RecordId &record_id)
Definition: page.cpp:71

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 119 of file page.h.

const PageId badgerdb::Page::INVALID_NUMBER = 0
static

Number of page indicating that it's invalid.

Definition at line 124 of file page.h.

const SlotId badgerdb::Page::INVALID_SLOT = 0
static

Number of slot indicating that it's invalid.

Definition at line 129 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 114 of file page.h.


The documentation for this class was generated from the following files: