17 #include "exceptions/file_exists_exception.h"
18 #include "exceptions/file_not_found_exception.h"
19 #include "exceptions/file_open_exception.h"
20 #include "exceptions/invalid_page_exception.h"
21 #include "file_iterator.h"
36 std::remove(filename.c_str());
47 std::fstream file(filename);
67 File::File(
const std::string& name,
const bool create_new) : filename_(name) {
83 std::ios_base::openmode mode =
84 std::fstream::in | std::fstream::out | std::fstream::binary;
92 mode = mode | std::fstream::trunc;
95 if (!already_exists) {
120 stream_->seekg(0 , std::ios::beg);
126 stream_->seekp(0 , std::ios::beg);
144 :
File(name, create_new)
152 :
File(other.filename_, false )
189 next_page_number = (*iter).next_page_number();
192 existing_page = *iter;
196 existing_page.set_next_page_number(new_page.
page_number());
197 new_page.set_next_page_number(next_page_number);
205 new_page.set_page_number(header.
num_pages);
218 existing_page = *iter;
222 assert(existing_page.isUsed());
223 existing_page.set_next_page_number(new_page.
page_number());
227 writePage(new_page_number, new_page.header_, new_page);
245 return readPage(page_number,
false );
253 if (!allow_free && !page.isUsed()) {
261 PageHeader header = readPageHeader(new_page_number);
271 header = new_page.header_;
273 writePage(new_page_number, header, new_page);
288 previous_page = *iter;
296 existing_page.initialize();
300 if (previous_page.isUsed()) {
303 writePage(page_number, existing_page.header_, existing_page);
317 const Page& new_page) {
320 stream_->write(reinterpret_cast<const char*>(&new_page.data_[0]),
325 PageHeader PageFile::readPageHeader(
PageId page_number)
const {
328 stream_->read(reinterpret_cast<char*>(&header),
sizeof(PageHeader));
344 :
File(name, create_new) {
351 :
File(other.filename_, false )
376 new_page.set_page_number(new_page_number);
static std::streampos pagePosition(const PageId page_number)
static bool isOpen(const std::string &filename)
static CountMap open_counts_
void deletePage(const PageId page_number)
std::shared_ptr< std::fstream > stream_
PageId next_page_number() const
File(const std::string &name, const bool create_new)
Page allocatePage(PageId &new_page_number)
void writePage(const PageId page_number, const Page &new_page)
void deletePage(const PageId page_number)
static PageFile open(const std::string &filename)
BlobFile(const std::string &name, const bool create_new)
An exception that is thrown when a file creation is requested for a filename that already exists...
Class which represents a file in the filesystem containing database pages.
static const std::size_t DATA_SIZE
void writeHeader(const FileHeader &header)
Page readPage(const PageId page_number) const
std::uint32_t PageId
Identifier for a page in a file.
static const std::size_t SIZE
An exception that is thrown when a file operation is requested for a filename that doesn't exist...
An exception that is thrown when an attempt is made to access an invalid page in a file...
Class which represents a fixed-size database page containing records.
void writePage(const PageId page_number, const Page &new_page)
An exception that is thrown when a file deletion is requested for a filename that's currently open...
void openIfNeeded(const bool create_new)
Page readPage(const PageId page_number) const
BlobFile & operator=(const BlobFile &rhs)
PageId page_number() const
Iterator for iterating over the pages in a file.
static void remove(const std::string &filename)
PageFile(const std::string &name, const bool create_new)
static PageFile create(const std::string &filename)
static BlobFile open(const std::string &filename)
Page allocatePage(PageId &new_page_number)
static BlobFile create(const std::string &filename)
static const PageId INVALID_NUMBER
PageFile & operator=(const PageFile &rhs)
static StreamMap open_streams_
FileHeader readHeader() const
static bool exists(const std::string &filename)