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 | Friends | List of all members
badgerdb::File Class Referenceabstract

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

#include <file.h>

Inheritance diagram for badgerdb::File:
badgerdb::BlobFile badgerdb::PageFile

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

badgerdb::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.

Definition at line 67 of file file.cpp.

67  : filename_(name) {
68  openIfNeeded(create_new);
69 
70  if (create_new) {
71  // File starts with 1 page (the header).
72  FileHeader header = {1 /* num_pages */, 0 /* first_used_page */,
73  0 /* num_free_pages */, 0 /* first_free_page */};
74  writeHeader(header);
75  }
76 }
std::string filename_
Definition: file.h:229
void writeHeader(const FileHeader &header)
Definition: file.cpp:125
void openIfNeeded(const bool create_new)
Definition: file.cpp:78
badgerdb::File::~File ( )
virtual

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

Definition at line 57 of file file.cpp.

57  {
58  close();
59 }
void close()
Definition: file.cpp:105

Member Function Documentation

virtual Page badgerdb::File::allocatePage ( PageId new_page_number)
pure virtual

Allocates a new page in the file.

Returns
The new page.

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.

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
virtual void badgerdb::File::deletePage ( const PageId  page_number)
pure virtual

Deletes a page from the file.

Parameters
page_numberNumber of page to delete.

Implemented in badgerdb::BlobFile, and badgerdb::PageFile.

bool badgerdb::File::exists ( const std::string &  filename)
static

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
inline

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 ( )

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)
static

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
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.

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
static std::streampos badgerdb::File::pagePosition ( const PageId  page_number)
inlinestaticprotected

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
protected

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
virtual Page badgerdb::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 badgerdb::BlobFile, and badgerdb::PageFile.

void badgerdb::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.

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)
protected

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
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.

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

Implemented in badgerdb::BlobFile, and badgerdb::PageFile.

Member Data Documentation

std::string badgerdb::File::filename_
protected

Name of the file this object represents.

Definition at line 229 of file file.h.

File::CountMap badgerdb::File::open_counts_
staticprotected

Counts for opened files.

Definition at line 224 of file file.h.

File::StreamMap badgerdb::File::open_streams_
staticprotected

Streams for opened files.

Definition at line 219 of file file.h.

std::shared_ptr<std::fstream> badgerdb::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 files: