BadgerDB
 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
badgerdb::BlobFile Class Reference
Inheritance diagram for badgerdb::BlobFile:
badgerdb::File

List of all members.

Public Member Functions

 BlobFile (const std::string &name, const bool create_new)
 BlobFile (const BlobFile &other)
BlobFileoperator= (const BlobFile &rhs)
 ~BlobFile ()
Page allocatePage (PageId &new_page_number)
Page readPage (const PageId page_number) const
void writePage (const PageId page_number, const Page &new_page)
void deletePage (const PageId page_number)
const std::string & filename () const
PageId getFirstPageNo ()

Static Public Member Functions

static BlobFile create (const std::string &filename)
static BlobFile open (const std::string &filename)
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

Definition at line 385 of file file.h.


Constructor & Destructor Documentation

badgerdb::BlobFile::BlobFile ( const std::string &  name,
const bool  create_new 
)

Constructs a file object representing a file on the filesystem.

See also:
File::create()
File::open()
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.

Definition at line 343 of file file.cpp.

: File(name, create_new) {
}

Copy constructor.

Parameters:
otherFile object to copy.
Returns:
A copy of the File object.

Definition at line 350 of file file.cpp.

: File(other.filename_, false /* create_new */)
{
}

Destructor that automatically closes the underlying file if no other File objects are using it.

Definition at line 347 of file file.cpp.

                    {
}

Member Function Documentation

Page badgerdb::BlobFile::allocatePage ( PageId new_page_number) [virtual]

Allocates a new page in the file.

Returns:
The new page.

Implements badgerdb::File.

Definition at line 364 of file file.cpp.

                                                   {
  FileHeader header = readHeader();
  Page new_page;

  new_page_number = header.num_pages;

  if (header.first_used_page == Page::INVALID_NUMBER) {
    header.first_used_page = header.num_pages;
  }

  ++header.num_pages;

  writePage(new_page_number, new_page);
  writeHeader(header);

  return new_page;
}
void badgerdb::File::close ( ) [protected, inherited]

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.

BlobFile badgerdb::BlobFile::create ( const std::string &  filename) [static]

Creates a new BlobFile.

Parameters:
filenameName of the file.
Exceptions:
FileExistsExceptionIf the requested file already exists.

Definition at line 335 of file file.cpp.

                                                   {
  return BlobFile(filename, true /* create_new */);
}
void badgerdb::BlobFile::deletePage ( const PageId  page_number) [virtual]

Deletes a page from the file.

Parameters:
page_numberNumber of page to delete.

Implements badgerdb::File.

Definition at line 396 of file file.cpp.

                                                  {
  throw InvalidPageException(page_number, filename_);
}
bool badgerdb::File::exists ( const std::string &  filename) [static, inherited]

Returns true if the file exists and is open.

Parameters:
filenameName of the file.

Definition at line 46 of file file.cpp.

                                           {
  std::fstream file(filename);
  if(file)
  {
    file.close();
    return true;
  }

  return false;
}
const std::string& badgerdb::File::filename ( ) const [inline, inherited]

Returns the name of the file this object represents.

Returns:
Name of file.

Definition at line 158 of file file.h.

{ return filename_; }

Returns pageid of first page in the file.

Returns:
Iterator at first page of 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, inherited]

Returns true if the file exists and is open.

Parameters:
filenameName of the file.

Definition at line 39 of file file.cpp.

                                           {
  if (!exists(filename)) {
    return false;
  }
  return open_counts_.find(filename) != open_counts_.end();
}
BlobFile badgerdb::BlobFile::open ( const std::string &  filename) [static]

Opens the file named fileName and returns the corresponding File object. It first checks if the file is already open. If so, then the new File object created uses the same input-output stream to read to or write fom that already open file. Reference count (open_counts_ static variable inside the File object) is incremented whenever an already open file is opened again. Otherwise the UNIX file is actually opened. The fileName and the stream associated with this File object are inserted into the open_streams_ map.

Parameters:
filenameName of the file.
Exceptions:
FileNotFoundExceptionIf the requested file doesn't exist.

Definition at line 339 of file file.cpp.

                                                 {
  return BlobFile(filename, false /* create_new */);
}
void badgerdb::File::openIfNeeded ( const bool  create_new) [protected, inherited]

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.

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;
  }
}
BlobFile & badgerdb::BlobFile::operator= ( const BlobFile rhs)

Assignment operator.

Parameters:
rhsFile object to assign.
Returns:
Newly assigned file object.

Definition at line 355 of file file.cpp.

                                                 {
  // This accounts for self-assignment and assignment of a File object for the
  // same file.
  close();  //close my file and associate me with the new one
  filename_ = rhs.filename_;
  openIfNeeded(false /* create_new */);
  return *this;
}
static std::streampos badgerdb::File::pagePosition ( const PageId  page_number) [inline, static, protected, inherited]

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.

                                                             {
    return sizeof(FileHeader) + ((page_number - 1) * Page::SIZE);
  }
FileHeader badgerdb::File::readHeader ( ) const [protected, inherited]

Reads the header for this file from disk.

Returns:
The file header.

Definition at line 118 of file file.cpp.

                                  {
  FileHeader header;
  stream_->seekg(0 /* pos */, std::ios::beg);
  stream_->read(reinterpret_cast<char*>(&header), sizeof(FileHeader));
  return header;
}
Page badgerdb::BlobFile::readPage ( const PageId  page_number) const [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.

Implements badgerdb::File.

Definition at line 382 of file file.cpp.

                                                      {
  Page page;
  stream_->seekg(pagePosition(page_number), std::ios::beg);
  stream_->read(reinterpret_cast<char*>(&page), Page::SIZE);
  return page;
}
void badgerdb::File::remove ( const std::string &  filename) [static, inherited]

Deletes an existing file.

Parameters:
filenameName of the file.
Exceptions:
FileNotFoundExceptionIf the file doesn't exist.
FileOpenExceptionIf the file is currently open.

Definition at line 29 of file file.cpp.

                                           {
  if (!exists(filename)) {
    throw FileNotFoundException(filename);
  }
  if (isOpen(filename)) {
    throw FileOpenException(filename);
  }
  std::remove(filename.c_str());
}
void badgerdb::File::writeHeader ( const FileHeader header) [protected, inherited]

Writes the given header to the disk as the header for this file.

Parameters:
headerFile header to write.

Definition at line 125 of file file.cpp.

                                               {
  stream_->seekp(0 /* pos */, std::ios::beg);
  stream_->write(reinterpret_cast<const char*>(&header), sizeof(FileHeader));
  stream_->flush();
}
void badgerdb::BlobFile::writePage ( const PageId  page_number,
const Page new_page 
) [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.

Implements badgerdb::File.

Definition at line 389 of file file.cpp.

                                                                           {
  stream_->seekp(pagePosition(new_page_number), std::ios::beg);
  stream_->write(reinterpret_cast<const char*>(&new_page), Page::SIZE);
  stream_->flush();
}

Member Data Documentation

std::string badgerdb::File::filename_ [protected, inherited]

Name of the file this object represents.

Definition at line 229 of file file.h.

File::CountMap badgerdb::File::open_counts_ [static, protected, inherited]

Counts for opened files.

Definition at line 224 of file file.h.

File::StreamMap badgerdb::File::open_streams_ [static, protected, inherited]

Streams for opened files.

Definition at line 219 of file file.h.

std::shared_ptr<std::fstream> badgerdb::File::stream_ [protected, inherited]

Stream for underlying filesystem object.

Definition at line 234 of file file.h.


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