wisc_db
file.h
1 
6 #pragma once
7 
8 #include <fstream>
9 #include <string>
10 #include <map>
11 #include <memory>
12 
13 #include "page.h"
14 
15 namespace wiscdb {
16 
17 class FileIterator;
18 
22 struct FileHeader {
27 
32 
37 
42 
49  bool operator==(const FileHeader& rhs) const {
50  return num_pages == rhs.num_pages &&
51  num_free_pages == rhs.num_free_pages &&
52  first_used_page == rhs.first_used_page &&
53  first_free_page == rhs.first_free_page;
54  }
55 };
56 
71 class File {
72  public:
79  static File create(const std::string& filename);
80 
94  static File open(const std::string& filename);
95 
103  static void remove(const std::string& filename);
104 
110  static bool isOpen(const std::string& filename);
111 
112 
118  static bool exists(const std::string& filename);
119 
126  File(const File& other);
127 
134  File& operator=(const File& rhs);
135 
140  ~File();
141 
147  Page allocatePage();
148 
157  Page readPage(const PageId page_number) const;
158 
166  void writePage(const Page& new_page);
167 
173  void deletePage(const PageId page_number);
174 
180  const std::string& filename() const { return filename_; }
181 
187  FileIterator begin();
188 
195  FileIterator end();
196 
202  void close();
203 
204  private:
212  static std::streampos pagePosition(const PageId page_number) {
213  return sizeof(FileHeader) + ((page_number - 1) * Page::SIZE);
214  }
215 
230  File(const std::string& name, const bool create_new);
231 
243  void openIfNeeded(const bool create_new);
244 
258  Page readPage(const PageId page_number, const bool allow_free) const;
259 
268  void writePage(const PageId page_number, const Page& new_page);
269 
279  void writePage(const PageId page_number, const PageHeader& header,
280  const Page& new_page);
281 
287  FileHeader readHeader() const;
288 
294  void writeHeader(const FileHeader& header);
295 
303  PageHeader readPageHeader(const PageId page_number) const;
304 
305  typedef std::map<std::string,
306  std::shared_ptr<std::fstream> > StreamMap;
307  typedef std::map<std::string, int> CountMap;
308 
312  static StreamMap open_streams_;
313 
317  static CountMap open_counts_;
318 
322  std::string filename_;
323 
327  std::shared_ptr<std::fstream> stream_;
328 
329  friend class FileIterator;
330  friend class FileTest;
331 };
332 
333 }
Class which represents a fixed-size database page containing records.
Definition: page.h:110
Definition: buffer.h:14
PageId num_pages
Definition: file.h:26
bool operator==(const FileHeader &rhs) const
Definition: file.h:49
PageId first_used_page
Definition: file.h:31
static const std::size_t SIZE
Definition: page.h:121
PageId first_free_page
Definition: file.h:41
std::uint32_t PageId
Identifier for a page in a file.
Definition: types.h:15
PageId num_free_pages
Definition: file.h:36
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
Represents a file in the filesystem containing pages.
Definition: file.h:71
const std::string & filename() const
Definition: file.h:180
Header metadata in a page.
Definition: page.h:26