SwatDB
Public Member Functions | Protected Member Functions | Private Attributes | Friends | List of all members
Catalog Class Reference

#include <catalog.h>

Public Member Functions

 Catalog ()
 creates an empty Catalog (should only be one in the system)
 
 Catalog (std::string db_metadata_file)
 create a catalog from existing DB state stored in meta data file More...
 
 ~Catalog ()
 Destroys the Catalog object. More...
 
FileId addEntry (std::string name, Schema *schema, File *file, CatType type, std::string file_name)
 Adds an entry to the catalog with a defined schema. May be called to create an entry for an index or relation as the SwatDB instance runs, or may be called as a SwatDB instance is booted from a saved state. More...
 
void deleteEntry (FileId file_id)
 Deletes an entry from the database. More...
 
std::string getFileName (FileId file_id)
 Returns the filename associated with the given FileId. More...
 
FileId getFileId (std::string name)
 Returns the FileId associated with the File identified by the given relation/index name. More...
 
FilegetFile (FileId file_id)
 Returns the File object associated with the given FileId. More...
 
SchemagetSchema (FileId file_id)
 Returns the schema of the requested file. More...
 
CatType getType (FileId file_id)
 Returns the type of the requested file. More...
 
std::vector< FileIdgetFileIds ()
 Gets the set of valid FileIDs in the system. More...
 

Protected Member Functions

void _setFile (FileId file_id, File *file_ptr)
 Set the File * field of a Catalog entry. More...
 
void _saveDBStateToFile (std::string db_metadata_filename)
 Valled by SwatDB on shutdown to save DB meta data state to a file. More...
 

Private Attributes

std::vector< CatalogEntryentries
 
FileId next_fid
 
std::mutex cat_mux
 

Friends

class SwatDB
 
class FileManager
 

Detailed Description

SwatDB Catalog Class: defines the interface to the part of SwatDB that keeps track of information about Relations and Indices in the system. High-level layers may add index and relation entries, and examine their schema for query processing. Low level layers may access some internal information about a Relation or Index through methods provided by the Catalog class. A Relation and Index is uniquely identified in the system by its FileId value.

The Catalog class is the only one that needs to know the format of a saved DB metadata file. It is the class that reads and parses the DB metadata as part of initing the SwatDB state to an existing DB, and also the one that saves Catalog state to a DB metadata file on shutdown. The main SwatDB class controls invoking the Catalog class constructor to init the catalog from a saved DB metadata file or to init an empty Catalog on start-up. It also is the only class that can invoke the Catalog's _saveDBStateToFile method, which it may do on shutdown of the DB. Currently, the metadata file format is the following (NOTE: TODO missing is representation of entries schema in metadata): num_entries 1st entry's Relation or Index name (string) 1st entry's File or Relation Type (int (its CatType value)) 1st entry's Disk file name (string) 2nd entry's Relation of Index name (string) ... FileIds are unique for a full execution of SwatDB regardless of if relations and indices are created or deleted at runtime. They do not persist across two separate boots of SwatDB.

Constructor & Destructor Documentation

◆ Catalog()

Catalog::Catalog ( std::string  db_metadata_file)

create a catalog from existing DB state stored in meta data file

Parameters
db_metadata_filefile containing metadata information about the DB with which to initialize the Catalog

◆ ~Catalog()

Catalog::~Catalog ( )

Destroys the Catalog object.

NOTE: The destructor does not determine whether the state of the SwatDB should be saved or not in the current implementation.

Member Function Documentation

◆ _saveDBStateToFile()

void Catalog::_saveDBStateToFile ( std::string  db_metadata_filename)
protected

Valled by SwatDB on shutdown to save DB meta data state to a file.

Parameters

◆ _setFile()

void Catalog::_setFile ( FileId  file_id,
File file_ptr 
)
protected

Set the File * field of a Catalog entry.

This is called by the FileManager constructor when SwatDB is booted and inited from exising db state.

Precondition
file_id is a valid FileId and its Catalog entry has a nullptr value for its file field.
Postcondition
The file field of the Catalog entry for file_id is set to file_ptr
Parameters
file_idA valid FileId.
file_ptrA pointer to a valid File object
Exceptions
InvalidFileIdCatif the FileId is not valid.
FileAlreadyExistCatif the file entry already has a valid File *

◆ addEntry()

FileId Catalog::addEntry ( std::string  name,
Schema schema,
File file,
CatType  type,
std::string  file_name 
)

Adds an entry to the catalog with a defined schema. May be called to create an entry for an index or relation as the SwatDB instance runs, or may be called as a SwatDB instance is booted from a saved state.

Precondition
An entry with matching name, type and file_name does not already exist in the system.
Postcondition
A new file or index has been added to the system, the passed File object's file_id field is set by this method since a file's id is determined by adding an entry for it to the Catalog.
Parameters
nameThe name of the relation.
schemaThe schema object assciated with the relation (may be null).
fileA pointer to the File object associated with the relation/index (may be null).
typeThe type of entry (index or relation).
file_nameThe name of the file into which the DiskManager stores the relation.
Returns
FileID of the added entry.
Exceptions
FileAlreadyExistCatif the file_name already exists in the database.
RelationAlreadyExistCatif a relation named name already exists in the databse.
IndexAlreadyExistsCatif an index named name already exists in the database.

◆ deleteEntry()

void Catalog::deleteEntry ( FileId  file_id)

Deletes an entry from the database.

Precondition
A valid FileId is provided as input.
Postcondition
The relation or index identified by the given FileId is removed from the database, along with all other stored data associated with it.
Parameters
file_idThe FileID of the file/index to remove from the database.
Exceptions
InvalidFileIdCatif the FileId is not valid.

◆ getFile()

File* Catalog::getFile ( FileId  file_id)

Returns the File object associated with the given FileId.

Precondition
The given FileId is valid.
Postcondition
The File object associated with the given FileId in the system is returned.
Parameters
file_idA FileId.
Returns
File object associated with the given FileId.
Exceptions
InvalidFileIdCatif the given FileId is not valid.

◆ getFileId()

FileId Catalog::getFileId ( std::string  name)

Returns the FileId associated with the File identified by the given relation/index name.

Precondition
A File identified by the given name exists in the SwatDB system.
Postcondition
FileId of the File identified by the given relation/index name is is returned.
Parameters
file_idA FileId.
Returns
FileId of the File identified by the given relation/index name.
Exceptions
InvalidNameCatif the given name is not valid.

◆ getFileIds()

std::vector<FileId> Catalog::getFileIds ( )

Gets the set of valid FileIDs in the system.

Returns
std::vector<FileId> vector of FileIDs in the system. The vector is empty if there are no files in the database.

◆ getFileName()

std::string Catalog::getFileName ( FileId  file_id)

Returns the filename associated with the given FileId.

Precondition
The given FileId is valid.
Postcondition
The filename associated with the given FileId in the system is returned.
Parameters
file_idA FileId.
Returns
std::string of the file name associated with the given FileId.
Exceptions
InvalidFileIdCatif the given FileId is not valid.

◆ getSchema()

Schema* Catalog::getSchema ( FileId  file_id)

Returns the schema of the requested file.

Precondition
A valid FileId is provided as input.
Postcondition
The schema pointer of the requested file is returned. (note: this points to the same Schema object as in the catalog—it is not a copy).
Parameters
file_idA valid FileId.
Returns
Schema* associated with the file identified by the given FileId.
Exceptions
InvalidFileIdCatif the FileId is not valid.

◆ getType()

CatType Catalog::getType ( FileId  file_id)

Returns the type of the requested file.

Precondition
A valid FileId is provided as input.
Postcondition
The type of the file with this FileId is returned.
Parameters
file_idA valid FileId.
Returns
CatType value of file identified by the given FileId.
Exceptions
InvalidFileIdCatif the FileId is not valid.

Member Data Documentation

◆ cat_mux

std::mutex Catalog::cat_mux
private

Mutex object to control access to the table of catalog entries.

◆ entries

std::vector<CatalogEntry> Catalog::entries
private

The set of entries, one for each relation and index in the database.

◆ next_fid

FileId Catalog::next_fid
private

FileIds are monotonically increasing as SwatDB adds entries. Deleting an entry sets its valid member to false. The FileId of invalid files remains in this vector.


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