SwatDB
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
FileManager Class Reference

#include <filemgr.h>

Collaboration diagram for FileManager:
Collaboration graph
[legend]

Public Member Functions

 FileManager (Catalog *cat, BufferManager *buf_mgr, bool load=false)
 Creates SwatDB FileManager, the interface to the File layer.
 
 ~FileManager ()
 Destructor.
 
FileId createRelation (std::string name, Schema *schema, CatType type, std::string file_name, bool save_on_exit=true)
 Creates a new Relation in the system. Adds an entry for the new file to the Catalog and creates the underlying storage for it with the DiskManager.
 
FileId createIndex (std::string name, std::vector< std::string > field_list, std::string index_file_name, FileId rel_fid, bool save_on_exit=false)
 Creates a new Index in the system. Adds an entry for the new file to the Catalog and creates the underlying storage for it with the DiskManager.
 
void removeRelation (std::string name)
 Deletes relation from the system and removes its underlying storage from the database.
 
void removeFile (FileId file_id)
 Deletes a File or Index from the system and removes its underlying storage from the database.
 
FilegetRelation (std::string name)
 Retuns File object identified by the relation name.
 
FilegetFile (FileId file_id)
 Retuns File object identified by the given FileId.
 
void printFile (FileId file_id, std::uint64_t max_to_print=50)
 prints a file's contents to stdout (default, only prints up to the first 50 records)
 
std::uint64_t getNumRecords (FileId file_id)
 returns the number of records in a relation file
 
void fileToText (FileId file_id, std::string file_name)
 saves contents of a file to a text file.
 

Protected Member Functions

void _removeAllFiles ()
 Removes all Files in the system and their underlying storage from the database.
 
void _saveOrRemoveFiles ()
 Flushes and closes all underlying files in the system, saving db state.
 

Private Member Functions

void _loadAllFiles ()
 Loads all Files listed in the Catalog.
 
void _loadFile (FileId fid)
 Loads an existing relation into the system, whose contents are already stored in a file on disk.
 

Private Attributes

Catalogcatalog
 
BufferManagerbuf_mgr
 

Friends

class SwatDB
 

Detailed Description

SwatDB FileManager Class. The interface to the file layer of the system: manages Files and Indexes.

Constructor & Destructor Documentation

◆ FileManager()

FileManager::FileManager ( Catalog cat,
BufferManager buf_mgr,
bool  load = false 
)

Creates SwatDB FileManager, the interface to the File layer.

NOTE: load paramameter has a defaulf vlaue of false, which means that the constructor can be called just passing argument values for the first 2 parameter when a false value for load is desired.

Precondition
Input parameter objects (cat and buf_mgr) are valid.
Postcondition
If load is true, FileManager is constructed for SwatDB tat is loaded from existing files on disk. File objects are created for every relation and index file in the system and corresponding Catalog entries are updated with them. If load is false, FileManager is constructed for a new empty SwatDB.
Parameters
catPointer to the SwatDB Catalog.
buf_mgrPointer to the SwatDB BufferManager.
loadbool indicating whether Files shoud be loaded from the existing SwatDB.

◆ ~FileManager()

FileManager::~FileManager ( )

Destructor.

SwatDB controls how all the database state is saved/removed at shut down and deletes the BufferManager and Catalog objects in its destructor

Member Function Documentation

◆ _loadAllFiles()

void FileManager::_loadAllFiles ( )
private

Loads all Files listed in the Catalog.

This is a helper function to constructor when initializing from a saved db state. Calls _loadFile below for each file in the catalog.

Precondition
None.
Postcondition
New File object is created for each entry in catalog and File* is is added tot he catalog entry. New header Page is not created for each File as they are existing relation and index files, being loaded from the saved db state.

◆ _loadFile()

void FileManager::_loadFile ( FileId  fid)
private

Loads an existing relation into the system, whose contents are already stored in a file on disk.

This is a helper function to the constructor when initializing from an existing db. At the point it is called the Catalog, BufMgr, and DiskMgr have already been created.

A new file object of the approriate type is created, its header page contents are already on disk, so this will not create and initialize a header page for the File object like createRelation does.

This method is called by SwatDB constructor when initializing the system from saved db metadata.

The constructor sequence is weird, but its ordering if very important, so at this point this is called, there is already a catalog entry for this file, and the diskmanager layer has already opened its underlying disk file.

Precondition
A File object with the requested relation name is not already in the system. An entry for this file is, however, already in the catalog, but missing its File * associated with this relation or index. Its underlying file is open by the disk manager
Postcondition
A File object of the specified type is added to the system, and its entry in the Catalog is updated to point to this newly created File object.
Parameters
fidThe FileId of the Relation or Index.
Exceptions
FileAlreadyExistCatif the catalog already contains a File * for this entry.
InvalidFileTypeFileMgrif the passed CatType is invalid or not supported.

◆ _removeAllFiles()

void FileManager::_removeAllFiles ( )
protected

Removes all Files in the system and their underlying storage from the database.

NOTE: This method is called on shutdown of the SwatDB, removing the db state.

Precondition
None. @post Underlying files that store the db state of the SwatDB are removed.

◆ _saveOrRemoveFiles()

void FileManager::_saveOrRemoveFiles ( )
protected

Flushes and closes all underlying files in the system, saving db state.

NOTE: This method is called on shutdown of the SwatDB, saving the db state.

Precondition
None.
Postcondition
Header page of each File is flushed to disk, but the underlying Unix files are not removed, effectively saving the current db state to disk. Files that are have save_on_exit set to false are not saved, and removed.

◆ createIndex()

FileId FileManager::createIndex ( std::string  name,
std::vector< std::string >  field_list,
std::string  index_file_name,
FileId  rel_fid,
bool  save_on_exit = false 
)

Creates a new Index in the system. Adds an entry for the new file to the Catalog and creates the underlying storage for it with the DiskManager.

Precondition
A file with the requested index name and type and underlying file name is not already defined in the system.
Postcondition
A new file or index of the specified type is added to the system. A new file or index object is created, its entry is added to the SwatDB catalog, and a Unix file to store its data is created on disk by the DiskManager.
Parameters
nameThe Index name in the system.
field_listA vector associated with the field list the index is built on
index_file_nameThe name of the disk file in which the new file object will be stored.
rel_fidFileId corresponding to the relation file on which the index is being built
save_on_exitbool whether to save file when program is exited.
Returns
FileId of the newly created file or index, or INVALID_FILE_ID if an error occurs.
Exceptions
FileAlreadyExistCatif the file_name already exists in the database. in the database.
IndexAlreadyExistsCatif an index named name already exists in the database.
InvalidFileTypeFileMgrif the passed CatType is invalid or not supported.

◆ createRelation()

FileId FileManager::createRelation ( std::string  name,
Schema schema,
CatType  type,
std::string  file_name,
bool  save_on_exit = true 
)

Creates a new Relation in the system. Adds an entry for the new file to the Catalog and creates the underlying storage for it with the DiskManager.

Precondition
A file with the requested relation or index name and type and underlying file name is not already defined in the system.
Postcondition
A new file or index of the specified type is added to the system. A new file or index object is created, its entry is added to the SwatDB catalog, and a Unix file to store its data is created on disk by the DiskManager.
Parameters
nameThe Relation name in the system.
schemaA pointer to a Schema object associated with the file. (for RelationFile types).
typeThe specific type of file or index to create.
file_nameThe name of the disk file in which the new file object will be stored.
save_on_exitbool whether to save file when program is exited.
Returns
FileId of the newly created file or index, or INVALID_FILE_ID if an error occurs.
Exceptions
FileAlreadyExistCatif the file_name already exists in the database.
RelationAlreadyExistCatif a relation named name already exists in the database.
IndexAlreadyExistsCatif an index named name already exists in the database.
InvalidFileTypeFileMgrif the passed CatType is invalid or not supported.

◆ getFile()

File * FileManager::getFile ( FileId  file_id)

Retuns File object identified by the given FileId.

Precondition
The File, identified by the FileId already exists.
Postcondition
File* is returned.
Parameters
file_idFileId of the file to be returned.
Exceptions
FileNotFoundFileMgrif the file identified by the FileId does not exist.

◆ getNumRecords()

std::uint64_t FileManager::getNumRecords ( FileId  file_id)

returns the number of records in a relation file

Parameters
file_idFileId of the file to get num records.
Exceptions
InvalidFileTypeFileMgrif the passed file_id is not for a RelationFile type
FileNotFoundFileMgrif the file identified by the FileId does not exist.
Returns
the number of records in the relation

◆ getRelation()

File * FileManager::getRelation ( std::string  name)

Retuns File object identified by the relation name.

Precondition
The realtion of the given name already exists.
Postcondition
File* is returned.
Parameters
nameRelation ame of the relation file to be returned.
Exceptions
FileNotFoundFileMgrif the file identified by the name does not exist.

◆ printFile()

void FileManager::printFile ( FileId  file_id,
std::uint64_t  max_to_print = 50 
)

prints a file's contents to stdout (default, only prints up to the first 50 records)

Parameters
file_idFileId of the file to print
max_to_printoptional 2nd arg to specify the maximum number of records to print out
Exceptions
InvalidFileTypeFileMgrif the passed file_id is not for a RelationFile type
FileNotFoundFileMgrif the file identified by the FileId does not exist.

◆ removeFile()

void FileManager::removeFile ( FileId  file_id)

Deletes a File or Index from the system and removes its underlying storage from the database.

Precondition
A valid FileId is provided as input.
Postcondition
The file identified by the given FileId is removed from the database.
Parameters
file_idFileId of the file to be deleted from the database.
Exceptions
FileNotFoundFileMgrif the given FileId does not exist in the system.

◆ removeRelation()

void FileManager::removeRelation ( std::string  name)

Deletes relation from the system and removes its underlying storage from the database.

Precondition
A valid relation name is provided as input.
Postcondition
The relation of the given name is removed from the database.
Parameters
nameName of the relation to be deleted from the database.
Exceptions
FileNotFoundFileMgrif the file identified by the FileId does not exist.

Member Data Documentation

◆ buf_mgr

BufferManager* FileManager::buf_mgr
private

Pointer to this SwatDB's BufferManager object.

◆ catalog

Catalog* FileManager::catalog
private

Pointer to this SwatDB's Catalog object.


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