Class for executing commands issued via the DavisDB shell. More...
#include <SystemManager.h>
Public Member Functions | |
ReturnCode | createDb (const char *dbName) |
Creates a new database. | |
ReturnCode | dropDb (const char *dbName) |
Drops an existing database. | |
ReturnCode | openDb (const char *dbName) |
Opens a database. | |
ReturnCode | closeDb () |
Closes the open database. | |
ReturnCode | createTable (const char *relName, int attrCount, AttributeInfo *attributes) |
Creates a new table. | |
ReturnCode | dropTable (const char *relName) |
Drops an existing table. | |
ReturnCode | createIndex (const char *relName, const char *attrName) |
Creates an index on a table. | |
ReturnCode | dropIndex (const char *relName, const char *attrName) |
Drops an index on a table. | |
ReturnCode | load (const char *relName, const char *fileName) |
Bulk loads the contents of a file into a table. | |
ReturnCode | info () |
Displays metadata about the database. | |
ReturnCode | info (const char *relName) |
Display metadata about a table. | |
ReturnCode | print (const char *relName) |
Print the contents of a table. | |
bool | isOpen () |
Returns true iff a database is currently open. | |
Static Public Member Functions | |
static SystemManager * | getInstance () |
Gets the singleton instance. |
Class for executing commands issued via the DavisDB shell.
Class for executing commands issued via the DavisDB shell. When the parser recognizes a DDL or system utility command, it calls one of the public methods of the SystemManager class listed below. As with the other DavisDB components, this is a singleton class; a reference to the singleton class instance is obtained by calling SystemManager::getInstance.
ReturnCode SystemManager::closeDb | ( | ) |
Closes the open database.
Closes the database currently open by closing any open files and changing out of the database subdirectory. Returns RC_OK on success, and other RC error codes otherwise.
ReturnCode SystemManager::createDb | ( | const char * | dbName | ) |
Creates a new database.
dbName | The name of the database to be created |
Creates a new database. The method should create a subdirectory dbName for the new database, and create and initialize the system catalogs inside that subdirectory. Returns RC_OK on success, fails with RC_FILE_EXISTS if the subdirectory already exists, and other RC error codes otherwise.
ReturnCode SystemManager::createIndex | ( | const char * | relName, | |
const char * | attrName | |||
) |
Creates an index on a table.
relName | The name of the table to be indexed | |
attrName | The name of the attribute to be indexed |
Creates a B+ tree index on the specified table and attribute, updating the system catalogs appropriately. Returns RC_OK on success, RC_FILE_NOT_FOUND if no such table exists, RC_FILE_EXISTS if the specified index already exists, and other RC error codes otherwise.
ReturnCode SystemManager::createTable | ( | const char * | relName, | |
int | attrCount, | |||
AttributeInfo * | attributes | |||
) |
Creates a new table.
relName | The name of the table to be created | |
attrCount | The number of attributes for the table | |
attributes | An array of AttributeInfo pointers describing the attributes. |
Creates a new table in the current database, updating the system catalogs appropriately. Returns RC_OK on success, RC_FILE_EXISTS if a table by that name already exists, and other RC error codes otherwise.
ReturnCode SystemManager::dropDb | ( | const char * | dbName | ) |
Drops an existing database.
dbName | The name of the database to be deleted |
Deletes an existing database by removing the subdirectory dbName and all its contents (by using, e.g., a call to system("rm -rf <dbName>")). Returns RC_OK on success, RC_FILE_NOT_FOUND if no such subdirectory exists, RC_FILE_OPEN if a database is currently open, and other RC error codes otherwise.
ReturnCode SystemManager::dropIndex | ( | const char * | relName, | |
const char * | attrName | |||
) |
Drops an index on a table.
relName | The name of the table for the index | |
attrName | The name of the attribute for the index |
Deletes the B+ tree index on the specified table and attribute, updating the system catalogs appropriately. Returns RC_OK on success, RC_FILE_NOT_FOUND if no such table or index exists, and other RC error codes otherwise.
ReturnCode SystemManager::dropTable | ( | const char * | relName | ) |
Drops an existing table.
relName | The name of the table to be deleted |
Deletes a table from the current database, along with any indices on the table, and updates the system catalogs appropriately. Returns RC_OK on success, RC_FILE_NOT_FOUND if no such table exists, and other RC error codes otherwise.
ReturnCode SystemManager::info | ( | const char * | relName | ) |
Display metadata about a table.
relName | The name of the table |
Displays (i.e., outputs to stdout) metadata about a table in the database. This information should include the name, type, length, and offset of each attribute in the specified relation, together with any other information you feel may be useful. Returns RC_OK on success, RC_FILE_NOT_FOUND if the specified table does not exist, and other RC error codes otherwise.
One reasonable implementation of this method is to simply invoke SystemManager::print for the system "attributes" catalog, printing only those rows relevant to relName.
ReturnCode SystemManager::info | ( | ) |
Displays metadata about the database.
Displays (i.e., outputs to stdout) metadata about the database. This information should include the names of all relations in the database, as well as any other information you would like to include. Returns RC_OK on success and other RC error codes on failure.
One reasonable implementation of this method is to simply invoke SystemManager::print for the system "relations" catalog.
bool SystemManager::isOpen | ( | ) |
Returns true iff a database is currently open.
Returns true iff a database is currently open. SystemManager should support multiple databases, but only one is allowed to be open at any given time. SystemParser uses this method to determine whether a given command may be issued (e.g., it will only call SystemManager::closeDatabase if the database is currently open).
ReturnCode SystemManager::load | ( | const char * | relName, | |
const char * | fileName | |||
) |
Bulk loads the contents of a file into a table.
relName | The name of the target table | |
fileName | The name of the source file |
Bulk loads the contents of the specified text file into the specified table, while updating any indices on the attributes of the table. The file should contain data in comma-separated format, with one line per tuple. Integers and floats in the file may be assumed to use the standard C library string encodings of those data types, hence can be parsed using strtol and strtof, respectively. (See the man pages for those functions for a description of the formats.) Strings in the file may be assumed to not contain any commas.
A typical implementation of SystemManager::load will first open the file (using fopen or ifstream::open), then read the contents of the file one line at a time, breaking each line into a comma-delimited attribute values using, e.g., strtok, parsing the attribute values according to their types, serializing the sequence of parsed attribute values into a record, then inserting the record into the target table (and the relevant keys into any indices on that table).
Character strings in the text file can be of any length up to the length specified for the corresponding attribute, including zero length (no characters for that field in the text file). If a character string is too long, you may silently truncate it, or you may generate a nonzero return code and stop loading, whichever behavior you prefer.
ReturnCode SystemManager::openDb | ( | const char * | dbName | ) |
Opens a database.
dbName | The name of the database to be opened |
Opens a database by changing into the directory dbName and performing any appropriate initialization, such as opening the system catalogs. Returns RC_OK on success, RC_FILE_NOT_FOUND if subdirectory dbName does not exist, and other RC error codes otherwise.
ReturnCode SystemManager::print | ( | const char * | relName | ) |
Print the contents of a table.
relName | The name of the table |
Prints the contents of a table to stdout. For uniformity, this must be done using the provided SystemPrinter class, using a call to SystemPrinter::printHeader (to print header information such as the name of each attribute), then a call to SystemPrinter::printRecord for each row in the table, and finally a call to SystemPrinter::printFooter (to output footer information including the total number of rows in the table). Returns RC_OK on success, RC_FILE_NOT_FOUND if the specified table does not exist, and other RC error codes otherwise.