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.
wiscdb::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. |
virtual wiscdb::File::~File | ( | ) | [virtual] |
Destructor that automatically closes the underlying file if no other File objects are using it.
virtual Page wiscdb::File::allocatePage | ( | PageId & | new_page_number | ) | [pure virtual] |
Allocates a new page in the file.
Implemented in wiscdb::RawFile, and wiscdb::PageFile.
void wiscdb::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.
virtual void wiscdb::File::deletePage | ( | const PageId | page_number | ) | [pure virtual] |
Deletes a page from the file.
page_number | Number of page to delete. |
Implemented in wiscdb::RawFile, and wiscdb::PageFile.
static bool wiscdb::File::exists | ( | const std::string & | filename | ) | [static] |
Returns true if the file exists and is open.
filename | Name of the file. |
const std::string& wiscdb::File::filename | ( | ) | const [inline] |
Returns pageid of first page in the file.
static bool wiscdb::File::isOpen | ( | const std::string & | filename | ) | [static] |
Returns true if the file exists and is open.
filename | Name of the file. |
void wiscdb::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. |
static std::streampos wiscdb::File::pagePosition | ( | const PageId | page_number | ) | [inline, static, protected] |
FileHeader wiscdb::File::readHeader | ( | ) | const [protected] |
Reads the header for this file from disk.
virtual Page wiscdb::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 wiscdb::RawFile, and wiscdb::PageFile.
static void wiscdb::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 wiscdb::File::writeHeader | ( | const FileHeader & | header | ) | [protected] |
Writes the given header to the disk as the header for this file.
header | File header to write. |
virtual void wiscdb::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 wiscdb::RawFile, and wiscdb::PageFile.
std::string wiscdb::File::filename_ [protected] |
CountMap wiscdb::File::open_counts_ [static, protected] |
StreamMap wiscdb::File::open_streams_ [static, protected] |
std::shared_ptr<std::fstream> wiscdb::File::stream_ [protected] |