|
CS4432 (DBII) LAB #1 for D term 2001 |
OUT: Friday, March 16, 2001.
DUE: 2pm, Tuesday, March 27, 2001.
POINTS: max of 100pts.
Your first programming lab is to implement a mini database program called MyDB using C++ or C (other languages are not permitted). Your program will be able to load a database from a text file and perform searches in the database for attributes that match an equality condition. In the next programming lab, you will add some more advanced queries/searches to your database program.
Your program will store the database records in a data file, which represents your MyDB database. All records in a given database has the same format, but they may be of variable size. The only data format for attributes that you need to support is strings.
With the standard library from C/C++ (esp. String Library), your program will manage data files that contain pairs of a key and associated data record. To support multiple attributes for one given data record, you can pack them into one single record with a fixed format but possibly variable length. For simplicity, you can put the attribute that you are using as the key also in the data record inself.
For example, if you have a Movie database with schema:
Movie( Key char(6), Name char(60), Year char(4), Ranking char(3), Score char(3)),
a possible way of storing the data is to use Key as the record key. Then the whole record would correspond to a string of 81 bytes as the data record, namely, 6+60+4+3+3=76 bytes for the five data attributes plus 5 additional bytes to store the actual length of each data attribute. You should consider other ways of storing the records, describe their pros and cons, and then justify the particular choice you are making for this layout in your documentation.
The database engine interface should be specified in the header file MyDB.h, which you will later ``#include'' in the main program. The MyDB.h should export the following functions that are further described below:
int CreateDB( char* dbName, char* datafile );
int PrintDB( char* dbName );
int RecordScan( char* dbName, int attrNumber, char* value );
int KeyScan( char* dbName, char* value);
int InsertRecord (char* dbName, char* value);
int DeleteRecord (char* dbName, char* keyValue);
int ModifyRecord (char* dbName, char* keyValue, int attrNumber, char* newValue);
void error( int rc );
All functions that return an int, return a ``0'' when the operation was successful or error codes (defined by you) in any other case.
int CreateDB( char* dbName, char* datafile );
This function should create a database with name dbName and populate it with the data from datafile. The file datafile holds the data to be loaded in the database in ASCII format. The first line of the file defines the maximum length of each attribute. After that, every tuple to be inserted is on a separate line in the file, with attribute values separated by ampersands (&). The first attribute value is the key of the relation. You may assume that the attribute values will not contain ampersands. Character strings in the file that are longer than the length specified in the first line should be truncated once your load them into your database.
In the Movie database described above, the data file will have the format:
6&60&4&3&3
97141&Star Wars&1977&1&8.9
92383&Shawshank Redemption, The&1994&2&8.9
107712&Usual Suspects, The&1995&3&8.7
...
int PrintDB( char* dbName )
This function should print all keys and records of the database dbName in a human readable form.
int RecordScan( char* dbName, int attrNumber, char* value );
This function should print all keys and records of the database dbName for which the attribute in the attrNumber position (starting with the first attribute key being at position zero) has the same value as the null-terminated string pointed by value. The function should return an error if there are less than attrNumber attributes. This function should work on all attributes of the relation, including the one that you are using as the key of your MyDB database.
int KeyScan( char* dbName, char* value );
This function should print the record of the database dbName that has a key with the same value as the string pointed by value.
int InsertRecord (char* dbName, char* value);
This function should insert a record with values as given in the input string pointed to by value in the database dbName. Before inserting the new record you must check key integrity by calling KeyScan function to make sure that no record with the same key (as the key of the new record to be inserted) already exists in the database file. If the insertion operation would violate the key integrity do not insert the record and return an error message.
int DeleteRecord (char* dbName, char* keyValue);
This function should delete the record with the same key value as the string pointed by value in the database dbName. Notice that you need to maintain a mechanism to handle the gap caused by the deleted record in the database file (you can use a special symbol or you can move the rest of the file to remove the gap).
int ModifyRecord (char* dbName, char* keyValue , int attrNumber, char* newValue);
This function should change the value of the attribute in the attrNumber position to the new value newValue for the record with same key value as the string pointed by keyValue. In case of modifying the value of the key attribute of a record you must check first key integrity by calling KeyScan function to make sure that no record with the same key (as the new key value) already exists in the database file. If the modification (to key) operation would violate the key integrity do not modify the record and return an error message.
void error( int rc );
This function should print the error message associated with the error code rc and terminate the execution of the program
Below we give an application that uses your database engine to create a database from the Movie data file defined in the previous section. We assume here that the file is called movies.txt. This program prints all records in the database, prints all records with score of 8.9, and prints the record of the movie with `107712'' as key. Then the program inserts a new record with value "59734&Deep Blue See&2000&2&8.8", deletes record with `107712'' as key, modifies the rank of record with `97141'' as key to be "8.8", and at the end prints all records in the database after all previous operations.
#include "MyDB.h"
main() {
int rc;
if (rc=CreateDB( "mydb", "movies.txt" )) { error(rc); }
if (rc=PrintDB("mydb")) { error(rc); }
if (rc=RecordScan( "mydb", 4, "8.9" )) { error(rc); }
if (rc=KeyScan( "mydb", "107712" )) { error(rc); }
if (rc= InsertRecord ("mydb", "59734&Deep Blue See&2000&2&8.8"){ error(rc); }
if (rc= DeleteRecord ("mydb", "107712") { error(rc); }
if (rc=ModifyRecord ("mydb", "97141", 4, "8.8") { error(rc); }
if (rc=PrintDB("mydb")) { error(rc); }
}
Each student is expected to submit a description of your design in a plain text file called MyDB.doc, and to include comments in the code. For the format of this document, please refer to CS Department Standard Document. We point out in particular that this means that you have to provide example testing cases you have worked with to evaluate your program, including testing runs that illustrate that your tests were successful. A discussion of what features work and do not work must be included.
The grade of your program will be based on three aspects:
Submission of the assignment will take place electronically via myWPI. Instructions for the submission process will be made available at myWPI. Your complete work including your MyDB.doc documentation file must be submitted in one package.