wisc_db
page_iterator.h
1 
8 #pragma once
9 
10 #include <cassert>
11 #include "file.h"
12 #include "page.h"
13 #include "types.h"
14 
15 namespace wiscdb {
16 
23 class PageIterator {
24  public:
29  : page_(NULL) {
30  current_record_ = {Page::INVALID_NUMBER, Page::INVALID_SLOT};
31  }
32 
40  : page_(page) {
41  assert(page_ != NULL);
42  const SlotId used_slot = getNextUsedSlot(Page::INVALID_SLOT /* start */);
43  current_record_ = {page_->page_number(), used_slot};
44  }
45 
53  PageIterator(Page* page, const RecordId& record_id)
54  : page_(page),
55  current_record_(record_id) {
56  }
57 
62  assert(page_ != NULL);
63  const SlotId used_slot = getNextUsedSlot(current_record_.slot_number);
64  current_record_ = {page_->page_number(), used_slot};
65 
66  return *this;
67  }
68 
69  inline PageIterator operator++(int) {
70  PageIterator tmp = *this; // copy ourselves
71 
72  assert(page_ != NULL);
73  const SlotId used_slot = getNextUsedSlot(current_record_.slot_number);
74  current_record_ = {page_->page_number(), used_slot};
75 
76  return tmp;
77  }
84  inline bool operator==(const PageIterator& rhs) const {
85  return page_->page_number() == rhs.page_->page_number() &&
86  current_record_ == rhs.current_record_;
87  }
88 
89  inline bool operator!=(const PageIterator& rhs) const {
90  return (page_->page_number() != rhs.page_->page_number()) ||
91  (current_record_ != rhs.current_record_);
92  }
93 
100  inline std::string operator*() const {
101  return page_->getRecord(current_record_);
102  }
103 
111  SlotId getNextUsedSlot(const SlotId start) const {
112  SlotId slot_number = Page::INVALID_SLOT;
113  for (SlotId i = start + 1; i <= page_->header.num_slots; ++i) {
114  const PageSlot* slot = page_->getSlot(i);
115  if (slot->used) {
116  slot_number = i;
117  break;
118  }
119  }
120  return slot_number;
121  }
122 
123  private:
127  Page* page_;
128 
132  RecordId current_record_;
133 
134 };
135 
136 }
Slot metadata that tracks where a record is in the data space.
Definition: page.h:78
std::string getRecord(const RecordId &record_id) const
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
PageIterator(Page *page)
Definition: page_iterator.h:39
bool operator==(const PageIterator &rhs) const
Definition: page_iterator.h:84
PageId page_number() const
static const SlotId INVALID_SLOT
Definition: page.h:137
Identifier for a record in a page.
Definition: types.h:30
static const PageId INVALID_NUMBER
Definition: page.h:132
std::string operator*() const
PageHeader header
Definition: page.h:144
PageIterator & operator++()
Definition: page_iterator.h:61
PageIterator(Page *page, const RecordId &record_id)
Definition: page_iterator.h:53
SlotId slot_number
Definition: types.h:39
Iterator for iterating over the records in a page.
Definition: page_iterator.h:23
SlotId getNextUsedSlot(const SlotId start) const
std::uint16_t num_slots
Definition: page.h:44
bool used
Definition: page.h:83