BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
badgerdb::BlobFile Class Reference
Inheritance diagram for badgerdb::BlobFile:
badgerdb::File

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_
 

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.

344 : File(name, create_new) {
345 }
File(const std::string &name, const bool create_new)
Definition: file.cpp:67
badgerdb::BlobFile::BlobFile ( const BlobFile other)

Copy constructor.

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

Definition at line 350 of file file.cpp.

351 : File(other.filename_, false /* create_new */)
352 {
353 }
File(const std::string &name, const bool create_new)
Definition: file.cpp:67
badgerdb::BlobFile::~BlobFile ( )

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

Definition at line 347 of file file.cpp.

347  {
348 }

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.

364  {
365  FileHeader header = readHeader();
366  Page new_page;
367 
368  new_page_number = header.num_pages;
369 
370  if (header.first_used_page == Page::INVALID_NUMBER) {
371  header.first_used_page = header.num_pages;
372  }
373 
374  ++header.num_pages;
375  //Fix set the 'new_page's page number to new_page_number before writing it to the disk
376  new_page.set_page_number(new_page_number);
377  writePage(new_page_number, new_page);
378  writeHeader(header);
379 
380  return new_page;
381 }
void writePage(const PageId page_number, const Page &new_page)
Definition: file.cpp:390
void writeHeader(const FileHeader &header)
Definition: file.cpp:125
static const PageId INVALID_NUMBER
Definition: page.h:124
FileHeader readHeader() const
Definition: file.cpp:118
void badgerdb::File::close ( )
protectedinherited

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.

105  {
106  if(open_counts_[filename_] > 0)
108 
109  stream_.reset();
110  assert(open_counts_[filename_] >= 0);
111 
112  if (open_counts_[filename_] == 0) {
113  open_streams_.erase(filename_);
114  open_counts_.erase(filename_);
115  }
116 }
static CountMap open_counts_
Definition: file.h:224
std::string filename_
Definition: file.h:229
std::shared_ptr< std::fstream > stream_
Definition: file.h:234
static StreamMap open_streams_
Definition: file.h:219
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.

335  {
336  return BlobFile(filename, true /* create_new */);
337 }
const std::string & filename() const
Definition: file.h:158
BlobFile(const std::string &name, const bool create_new)
Definition: file.cpp:343
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 397 of file file.cpp.

397  {
398  throw InvalidPageException(page_number, filename_);
399 }
std::string filename_
Definition: file.h:229
bool badgerdb::File::exists ( const std::string &  filename)
staticinherited

Returns true if the file exists and is open.

Parameters
filenameName of the file.

Definition at line 46 of file file.cpp.

46  {
47  std::fstream file(filename);
48  if(file)
49  {
50  file.close();
51  return true;
52  }
53 
54  return false;
55 }
const std::string & filename() const
Definition: file.h:158
const std::string& badgerdb::File::filename ( ) const
inlineinherited

Returns the name of the file this object represents.

Returns
Name of file.

Definition at line 158 of file file.h.

158 { return filename_; }
std::string filename_
Definition: file.h:229
PageId badgerdb::File::getFirstPageNo ( )
inherited

Returns pageid of first page in the file.

Returns
Iterator at first page of file.

Definition at line 62 of file file.cpp.

62  {
63  const FileHeader& header = readHeader();
64  return header.first_used_page;
65 }
FileHeader readHeader() const
Definition: file.cpp:118
bool badgerdb::File::isOpen ( const std::string &  filename)
staticinherited

Returns true if the file exists and is open.

Parameters
filenameName of the file.

Definition at line 39 of file file.cpp.

39  {
40  if (!exists(filename)) {
41  return false;
42  }
43  return open_counts_.find(filename) != open_counts_.end();
44 }
static CountMap open_counts_
Definition: file.h:224
const std::string & filename() const
Definition: file.h:158
static bool exists(const std::string &filename)
Definition: file.cpp:46
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.

339  {
340  return BlobFile(filename, false /* create_new */);
341 }
const std::string & filename() const
Definition: file.h:158
BlobFile(const std::string &name, const bool create_new)
Definition: file.cpp:343
void badgerdb::File::openIfNeeded ( const bool  create_new)
protectedinherited

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.

78  {
79  if (open_counts_.find(filename_) != open_counts_.end()) { //exists an entry already
82  } else {
83  std::ios_base::openmode mode =
84  std::fstream::in | std::fstream::out | std::fstream::binary;
85  const bool already_exists = exists(filename_);
86  if (create_new) {
87  // Error if we try to overwrite an existing file.
88  if (already_exists) {
89  throw FileExistsException(filename_);
90  }
91  // New files have to be truncated on open.
92  mode = mode | std::fstream::trunc;
93  } else {
94  // Error if we try to open a file that doesn't exist.
95  if (!already_exists) {
96  throw FileNotFoundException(filename_);
97  }
98  }
99  stream_.reset(new std::fstream(filename_, mode));
101  open_counts_[filename_] = 1;
102  }
103 }
static CountMap open_counts_
Definition: file.h:224
std::string filename_
Definition: file.h:229
std::shared_ptr< std::fstream > stream_
Definition: file.h:234
static StreamMap open_streams_
Definition: file.h:219
static bool exists(const std::string &filename)
Definition: file.cpp:46
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.

355  {
356  // This accounts for self-assignment and assignment of a File object for the
357  // same file.
358  close(); //close my file and associate me with the new one
359  filename_ = rhs.filename_;
360  openIfNeeded(false /* create_new */);
361  return *this;
362 }
std::string filename_
Definition: file.h:229
void openIfNeeded(const bool create_new)
Definition: file.cpp:78
void close()
Definition: file.cpp:105
static std::streampos badgerdb::File::pagePosition ( const PageId  page_number)
inlinestaticprotectedinherited

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.

175  {
176  return sizeof(FileHeader) + ((page_number - 1) * Page::SIZE);
177  }
static const std::size_t SIZE
Definition: page.h:114
FileHeader badgerdb::File::readHeader ( ) const
protectedinherited

Reads the header for this file from disk.

Returns
The file header.

Definition at line 118 of file file.cpp.

118  {
119  FileHeader header;
120  stream_->seekg(0 /* pos */, std::ios::beg);
121  stream_->read(reinterpret_cast<char*>(&header), sizeof(FileHeader));
122  return header;
123 }
std::shared_ptr< std::fstream > stream_
Definition: file.h:234
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 383 of file file.cpp.

383  {
384  Page page;
385  stream_->seekg(pagePosition(page_number), std::ios::beg);
386  stream_->read(reinterpret_cast<char*>(&page), Page::SIZE);
387  return page;
388 }
static std::streampos pagePosition(const PageId page_number)
Definition: file.h:175
std::shared_ptr< std::fstream > stream_
Definition: file.h:234
static const std::size_t SIZE
Definition: page.h:114
void badgerdb::File::remove ( const std::string &  filename)
staticinherited

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.

29  {
30  if (!exists(filename)) {
31  throw FileNotFoundException(filename);
32  }
33  if (isOpen(filename)) {
34  throw FileOpenException(filename);
35  }
36  std::remove(filename.c_str());
37 }
static bool isOpen(const std::string &filename)
Definition: file.cpp:39
const std::string & filename() const
Definition: file.h:158
static bool exists(const std::string &filename)
Definition: file.cpp:46
void badgerdb::File::writeHeader ( const FileHeader header)
protectedinherited

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.

125  {
126  stream_->seekp(0 /* pos */, std::ios::beg);
127  stream_->write(reinterpret_cast<const char*>(&header), sizeof(FileHeader));
128  stream_->flush();
129 }
std::shared_ptr< std::fstream > stream_
Definition: file.h:234
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 390 of file file.cpp.

390  {
391  stream_->seekp(pagePosition(new_page_number), std::ios::beg);
392  stream_->write(reinterpret_cast<const char*>(&new_page), Page::SIZE);
393  stream_->flush();
394 }
static std::streampos pagePosition(const PageId page_number)
Definition: file.h:175
std::shared_ptr< std::fstream > stream_
Definition: file.h:234
static const std::size_t SIZE
Definition: page.h:114

Member Data Documentation

std::string badgerdb::File::filename_
protectedinherited

Name of the file this object represents.

Definition at line 229 of file file.h.

File::CountMap badgerdb::File::open_counts_
staticprotectedinherited

Counts for opened files.

Definition at line 224 of file file.h.

File::StreamMap badgerdb::File::open_streams_
staticprotectedinherited

Streams for opened files.

Definition at line 219 of file file.h.

std::shared_ptr<std::fstream> badgerdb::File::stream_
protectedinherited

Stream for underlying filesystem object.

Definition at line 234 of file file.h.


The documentation for this class was generated from the following files: