BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends Pages
file.h
1 
8 #pragma once
9 
10 #include <fstream>
11 #include <string>
12 #include <map>
13 #include <memory>
14 
15 #include "page.h"
16 
17 namespace badgerdb {
18 
19 class FileIterator;
20 
24 struct FileHeader {
29 
34 
39 
44 
51  bool operator==(const FileHeader& rhs) const {
52  return num_pages == rhs.num_pages &&
56  }
57 };
58 
73 class File {
74  public:
81  static File create(const std::string& filename);
82 
93  static File open(const std::string& filename);
94 
102  static void remove(const std::string& filename);
103 
109  static bool isOpen(const std::string& filename);
110 
111 
117  static bool exists(const std::string& filename);
118 
125  File(const File& other);
126 
133  File& operator=(const File& rhs);
134 
139  ~File();
140 
146  Page allocatePage();
147 
156  Page readPage(const PageId page_number) const;
157 
165  void writePage(const Page& new_page);
166 
172  void deletePage(const PageId page_number);
173 
179  const std::string& filename() const { return filename_; }
180 
187 
194  FileIterator end();
195 
196  private:
204  static std::streampos pagePosition(const PageId page_number) {
205  return sizeof(FileHeader) + ((page_number - 1) * Page::SIZE);
206  }
207 
222  File(const std::string& name, const bool create_new);
223 
235  void openIfNeeded(const bool create_new);
236 
242  void close();
243 
257  Page readPage(const PageId page_number, const bool allow_free) const;
258 
267  void writePage(const PageId page_number, const Page& new_page);
268 
278  void writePage(const PageId page_number, const PageHeader& header,
279  const Page& new_page);
280 
286  FileHeader readHeader() const;
287 
293  void writeHeader(const FileHeader& header);
294 
302  PageHeader readPageHeader(const PageId page_number) const;
303 
304  typedef std::map<std::string,
305  std::shared_ptr<std::fstream> > StreamMap;
306  typedef std::map<std::string, int> CountMap;
307 
311  static StreamMap open_streams_;
312 
316  static CountMap open_counts_;
317 
321  std::string filename_;
322 
326  std::shared_ptr<std::fstream> stream_;
327 
328  friend class FileIterator;
329  friend class FileTest;
330 };
331 
332 }
static bool isOpen(const std::string &filename)
Definition: file.cpp:47
PageId first_free_page
Definition: file.h:43
FileIterator begin()
Definition: file.cpp:214
FileIterator end()
Definition: file.cpp:219
const std::string & filename() const
Definition: file.h:179
Header metadata for files on disk which contain pages.
Definition: file.h:24
static File open(const std::string &filename)
Definition: file.cpp:33
Class which represents a file in the filesystem containing database pages.
Definition: file.h:73
static File create(const std::string &filename)
Definition: file.cpp:29
std::uint32_t PageId
Identifier for a page in a file.
Definition: types.h:15
PageId num_pages
Definition: file.h:28
static const std::size_t SIZE
Definition: page.h:113
Class which represents a fixed-size database page containing records.
Definition: page.h:107
bool operator==(const FileHeader &rhs) const
Definition: file.h:51
void writePage(const Page &new_page)
Definition: file.cpp:169
Iterator for iterating over the pages in a file.
Definition: file_iterator.h:23
File(const File &other)
Definition: file.cpp:65
Page readPage(const PageId page_number) const
Definition: file.cpp:149
PageId num_free_pages
Definition: file.h:38
PageId first_used_page
Definition: file.h:33
Page allocatePage()
Definition: file.cpp:84
File & operator=(const File &rhs)
Definition: file.cpp:71
static bool exists(const std::string &filename)
Definition: file.cpp:54
void deletePage(const PageId page_number)
Definition: file.cpp:184