BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends Pages
file_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 FileIterator {
24  public:
29  : file_(NULL),
30  current_page_number_(Page::INVALID_NUMBER) {
31  }
32 
40  : file_(file) {
41  assert(file_ != NULL);
42  const FileHeader& header = file_->readHeader();
43  current_page_number_ = header.first_used_page;
44  }
45 
53  FileIterator(File* file, PageId page_number)
54  : file_(file),
55  current_page_number_(page_number) {
56  }
57 
62  assert(file_ != NULL);
63  const PageHeader& header = file_->readPageHeader(current_page_number_);
64  current_page_number_ = header.next_page_number;
65 
66  return *this;
67  }
68 
69  //postfix
70  inline FileIterator operator++(int)
71  {
72  FileIterator tmp = *this; // copy ourselves
73 
74  assert(file_ != NULL);
75  const PageHeader& header = file_->readPageHeader(current_page_number_);
76  current_page_number_ = header.next_page_number;
77 
78  return tmp;
79  }
80 
87  inline bool operator==(const FileIterator& rhs) const {
88  return file_->filename() == rhs.file_->filename() &&
89  current_page_number_ == rhs.current_page_number_;
90  }
91 
92  inline bool operator!=(const FileIterator& rhs) const {
93  return (file_->filename() != rhs.file_->filename()) ||
94  (current_page_number_ != rhs.current_page_number_);
95  }
96 
103  inline Page operator*() const
104  { return file_->readPage(current_page_number_); }
105 
106  private:
110  File* file_;
111 
115  PageId current_page_number_;
116 };
117 
118 }
FileIterator & operator++()
Definition: file_iterator.h:61
Page operator*() const
const std::string & filename() const
Definition: file.h:179
Header metadata for files on disk which contain pages.
Definition: file.h:24
PageId next_page_number
Definition: page.h:58
Class which represents a file in the filesystem containing database pages.
Definition: file.h:73
bool operator==(const FileIterator &rhs) const
Definition: file_iterator.h:87
std::uint32_t PageId
Identifier for a page in a file.
Definition: types.h:15
Class which represents a fixed-size database page containing records.
Definition: page.h:107
Iterator for iterating over the pages in a file.
Definition: file_iterator.h:23
FileIterator(File *file, PageId page_number)
Definition: file_iterator.h:53
Page readPage(const PageId page_number) const
Definition: file.cpp:149
PageId first_used_page
Definition: file.h:33
FileIterator(File *file)
Definition: file_iterator.h:39
Header metadata in a page.
Definition: page.h:25