Class which represents a file in the filesystem containing database pages. More...
#include <file.h>
Public Member Functions | |
File (const std::string &name, const bool create_new) | |
virtual | ~File () |
virtual Page | allocatePage (PageId &new_page_number)=0 |
virtual Page | readPage (const PageId page_number) const =0 |
virtual void | writePage (const PageId page_number, const Page &new_page)=0 |
virtual void | deletePage (const PageId page_number)=0 |
const std::string & | filename () const |
PageId | getFirstPageNo () |
Static Public Member Functions | |
static void | remove (const std::string &filename) |
static bool | isOpen (const std::string &filename) |
static bool | exists (const std::string &filename) |
Protected Types | |
typedef std::map< std::string, std::shared_ptr< std::fstream > > | StreamMap |
typedef std::map< std::string, int > | CountMap |
Protected Member Functions | |
void | openIfNeeded (const bool create_new) |
void | close () |
FileHeader | readHeader () const |
void | writeHeader (const FileHeader &header) |
Static Protected Member Functions | |
static std::streampos | pagePosition (const PageId page_number) |
Protected Attributes | |
std::string | filename_ |
std::shared_ptr< std::fstream > | stream_ |
Static Protected Attributes | |
static StreamMap | open_streams_ |
static CountMap | open_counts_ |
Friends | |
class | FileIterator |
Class which represents a file in the filesystem containing database pages.
The File class wraps a stream to an underlying file on disk. Files contain fixed-sized pages, and they never deallocate space (though they do reuse deleted pages if possible). If multiple File objects refer to the same underlying file, they will share the stream in memory. If a file that has already been opened (possibly by another query), then the File class detects this (by looking in the open_streams_ map) and just returns a file object with the already created stream for the file without actually opening the UNIX file again.
badgerdb::File::File | ( | const std::string & | name, |
const bool | create_new | ||
) |
Constructs a file object representing a file on the filesystem.
name | Name of file. |
create_new | Whether to create a new file. |
FileExistsException | If the underlying file exists and create_new is true. |
FileNotFoundException | If the underlying file doesn't exist and create_new is false. |
Definition at line 67 of file file.cpp.
: filename_(name) { openIfNeeded(create_new); if (create_new) { // File starts with 1 page (the header). FileHeader header = {1 /* num_pages */, 0 /* first_used_page */, 0 /* num_free_pages */, 0 /* first_free_page */}; writeHeader(header); } }
badgerdb::File::~File | ( | ) | [virtual] |
virtual Page badgerdb::File::allocatePage | ( | PageId & | new_page_number | ) | [pure virtual] |
Allocates a new page in the file.
Implemented in badgerdb::BlobFile, and badgerdb::PageFile.
void badgerdb::File::close | ( | ) | [protected] |
Closes the underlying file stream in <stream_>. This method only closes the file if no other File objects exist that access the same file.
Definition at line 105 of file file.cpp.
{ if(open_counts_[filename_] > 0) --open_counts_[filename_]; stream_.reset(); assert(open_counts_[filename_] >= 0); if (open_counts_[filename_] == 0) { open_streams_.erase(filename_); open_counts_.erase(filename_); } }
virtual void badgerdb::File::deletePage | ( | const PageId | page_number | ) | [pure virtual] |
Deletes a page from the file.
page_number | Number of page to delete. |
Implemented in badgerdb::BlobFile, and badgerdb::PageFile.
bool badgerdb::File::exists | ( | const std::string & | filename | ) | [static] |
const std::string& badgerdb::File::filename | ( | ) | const [inline] |
Returns pageid of first page in the file.
Definition at line 62 of file file.cpp.
{ const FileHeader& header = readHeader(); return header.first_used_page; }
bool badgerdb::File::isOpen | ( | const std::string & | filename | ) | [static] |
Returns true if the file exists and is open.
filename | Name of the file. |
Definition at line 39 of file file.cpp.
{ if (!exists(filename)) { return false; } return open_counts_.find(filename) != open_counts_.end(); }
void badgerdb::File::openIfNeeded | ( | const bool | create_new | ) | [protected] |
Opens the underlying file named in filename_. This method only opens the file if no other File objects exist that access the same filesystem file; otherwise, it reuses the existing stream.
create_new | Whether to create a new file. |
FileExistsException | If the underlying file exists and create_new is true. |
FileNotFoundException | If the underlying file doesn't exist and create_new is false. |
Definition at line 78 of file file.cpp.
{ if (open_counts_.find(filename_) != open_counts_.end()) { //exists an entry already ++open_counts_[filename_]; stream_ = open_streams_[filename_]; } else { std::ios_base::openmode mode = std::fstream::in | std::fstream::out | std::fstream::binary; const bool already_exists = exists(filename_); if (create_new) { // Error if we try to overwrite an existing file. if (already_exists) { throw FileExistsException(filename_); } // New files have to be truncated on open. mode = mode | std::fstream::trunc; } else { // Error if we try to open a file that doesn't exist. if (!already_exists) { throw FileNotFoundException(filename_); } } stream_.reset(new std::fstream(filename_, mode)); open_streams_[filename_] = stream_; open_counts_[filename_] = 1; } }
static std::streampos badgerdb::File::pagePosition | ( | const PageId | page_number | ) | [inline, static, protected] |
Returns the position of the page with the given number in the file (as an offset from the beginning of the file).
page_number | Number of page. |
Definition at line 175 of file file.h.
{ return sizeof(FileHeader) + ((page_number - 1) * Page::SIZE); }
FileHeader badgerdb::File::readHeader | ( | ) | const [protected] |
virtual Page badgerdb::File::readPage | ( | const PageId | page_number | ) | const [pure virtual] |
Reads an existing page from the file.
page_number | Number of page to read. |
InvalidPageException | If the page doesn't exist in the file or is not currently used. |
Implemented in badgerdb::BlobFile, and badgerdb::PageFile.
void badgerdb::File::remove | ( | const std::string & | filename | ) | [static] |
Deletes an existing file.
filename | Name of the file. |
FileNotFoundException | If the file doesn't exist. |
FileOpenException | If the file is currently open. |
void badgerdb::File::writeHeader | ( | const FileHeader & | header | ) | [protected] |
virtual void badgerdb::File::writePage | ( | const PageId | page_number, |
const Page & | new_page | ||
) | [pure virtual] |
Writes a page into the file at the given page number. No bounds checking is performed.
page_number | Number of page whose contents to replace. |
new_page | Page to write. |
Implemented in badgerdb::BlobFile, and badgerdb::PageFile.
std::string badgerdb::File::filename_ [protected] |
File::CountMap badgerdb::File::open_counts_ [static, protected] |
File::StreamMap badgerdb::File::open_streams_ [static, protected] |
std::shared_ptr<std::fstream> badgerdb::File::stream_ [protected] |