wisc_db
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 wiscdb {
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 }
Class which represents a fixed-size database page containing records.
Definition: page.h:110
Definition: buffer.h:14
FileIterator(File *file, PageId page_number)
Definition: file_iterator.h:53
Page readPage(const PageId page_number) const
FileIterator & operator++()
Definition: file_iterator.h:61
PageId first_used_page
Definition: file.h:31
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
PageId next_page_number
Definition: page.h:59
Header metadata for files on disk which contain pages.
Definition: file.h:22
Iterator for iterating over the pages in a file.
Definition: file_iterator.h:23
FileIterator(File *file)
Definition: file_iterator.h:39
Represents a file in the filesystem containing pages.
Definition: file.h:71
Page operator*() const
const std::string & filename() const
Definition: file.h:180
Header metadata in a page.
Definition: page.h:26