WiscDB
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends
wiscdb::File Class Reference

Class which represents a file in the filesystem containing database pages. More...

#include <file.h>

Inheritance diagram for wiscdb::File:
wiscdb::PageFile wiscdb::RawFile

List of all members.

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

Detailed Description

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.

Warning:
This class is not threadsafe.

Definition at line 75 of file file.h.


Constructor & Destructor Documentation

wiscdb::File::File ( const std::string &  name,
const bool  create_new 
)

Constructs a file object representing a file on the filesystem.

Parameters:
nameName of file.
create_newWhether to create a new file.
Exceptions:
FileExistsExceptionIf the underlying file exists and create_new is true.
FileNotFoundExceptionIf 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.


Member Function Documentation

virtual Page wiscdb::File::allocatePage ( PageId new_page_number) [pure virtual]

Allocates a new page in the file.

Returns:
The new page.

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.

Parameters:
page_numberNumber 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.

Parameters:
filenameName of the file.
const std::string& wiscdb::File::filename ( ) const [inline]

Returns the name of the file this object represents.

Returns:
Name of file.

Definition at line 158 of file file.h.

Returns pageid of first page in the file.

Returns:
Iterator at first page of file.
static bool wiscdb::File::isOpen ( const std::string &  filename) [static]

Returns true if the file exists and is open.

Parameters:
filenameName 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.

Parameters:
create_newWhether to create a new file.
Exceptions:
FileExistsExceptionIf the underlying file exists and create_new is true.
FileNotFoundExceptionIf the underlying file doesn't exist and create_new is false.
static std::streampos wiscdb::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).

Parameters:
page_numberNumber of page.
Returns:
Position of page in file.

Definition at line 175 of file file.h.

FileHeader wiscdb::File::readHeader ( ) const [protected]

Reads the header for this file from disk.

Returns:
The file header.
virtual Page wiscdb::File::readPage ( const PageId  page_number) const [pure virtual]

Reads an existing page from the file.

Parameters:
page_numberNumber of page to read.
Returns:
The page.
Exceptions:
InvalidPageExceptionIf 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.

Parameters:
filenameName of the file.
Exceptions:
FileNotFoundExceptionIf the file doesn't exist.
FileOpenExceptionIf 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.

Parameters:
headerFile 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.

Parameters:
page_numberNumber of page whose contents to replace.
new_pagePage to write.

Implemented in wiscdb::RawFile, and wiscdb::PageFile.


Member Data Documentation

std::string wiscdb::File::filename_ [protected]

Name of the file this object represents.

Definition at line 229 of file file.h.

CountMap wiscdb::File::open_counts_ [static, protected]

Counts for opened files.

Definition at line 224 of file file.h.

StreamMap wiscdb::File::open_streams_ [static, protected]

Streams for opened files.

Definition at line 219 of file file.h.

std::shared_ptr<std::fstream> wiscdb::File::stream_ [protected]

Stream for underlying filesystem object.

Definition at line 234 of file file.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends