BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends Pages
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 badgerdb {
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 }
SlotId getNextUsedSlot(const SlotId start) const
SlotId num_slots
Definition: page.h:43
std::string getRecord(const RecordId &record_id) const
Definition: page.cpp:43
std::string operator*() const
Iterator for iterating over the records in a page.
Definition: page_iterator.h:23
Class which represents a fixed-size database page containing records.
Definition: page.h:107
SlotId slot_number
Definition: types.h:39
PageId page_number() const
Definition: page.h:193
PageIterator(Page *page, const RecordId &record_id)
Definition: page_iterator.h:53
PageIterator(Page *page)
Definition: page_iterator.h:39
Slot metadata that tracks where a record is in the data space.
Definition: page.h:77
Identifier for a record in a page.
Definition: types.h:30
static const SlotId INVALID_SLOT
Definition: page.h:128
static const PageId INVALID_NUMBER
Definition: page.h:123
std::uint16_t SlotId
Identifier for a slot in a page.
Definition: types.h:20
bool operator==(const PageIterator &rhs) const
Definition: page_iterator.h:84
PageIterator & operator++()
Definition: page_iterator.h:61