Class for general utility functions. More...
#include <utility.h>
Static Public Member Functions | |
| static void | Load (const std::string &relation, const std::string &fileName) |
| static void | Print (const std::string &relation) |
| static void | UT_computeWidth (int attrCnt, AttrDesc attrs[], int *&attrWidth) |
| static void | Quit () |
| void badgerdb::Utilities::Load | ( | const std::string & | relation, |
| const std::string & | fileName | ||
| ) | [static] |
A bulkload utility
Definition at line 156 of file utility.cpp.
{
RelDesc rd;
AttrDesc *attrs;
int attrCnt;
if(relation.empty() || fileName.empty() || relation == std::string(RELCATNAME) || relation == std::string(ATTRCATNAME))
throw BadParamException();
// open Unix data file
int fd;
if((fd = open(fileName.c_str(), O_RDONLY, 0)) < 0)
throw FileOpenException(fileName);
// get relation data
relCat->getInfo(relation, rd);
// get attribute data
attrCat->getRelInfo(rd.relName, attrCnt, attrs);
// open data file
FileScan fscan(std::string(rd.relName), bufMgr);
int records = 0;
Index **index = new Index * [attrCnt];
//compute width of tuple and open index files, if any
int width = 0;
int i;
for(i = 0; i < attrCnt; i++)
{
width += attrs[i].attrLen;
index[i] = NULL;
if (attrs[i].indexed)
{
index[i] = new Index(std::string(rd.relName), attrs[i].attrOffset, attrs[i].attrLen, (Datatype)attrs[i].attrType, NONUNIQUE, bufMgr);
}
}
// Now start reading "width" bytes from the unix file (fd). The bytes that you
// read are in the same format as the record used by Minirel. You now need to
// insert this record into the heapfile (hfile) corresponding to this relation
// and also insert entries for this record in "each" index. You can insert
// entries into the index using the index[i]->insesrtEntry(...) command. Note,
// you will need to provide to this method, the key of the entry being inserted.
// Use the record and the index information from the catalog to locate the
// key that you need to pass to insertEntry.
// Solution Starts
// create a record for constructing the tuple
char *record = new char [width];
int nbytes;
std::string rec;
while((nbytes = read(fd, record, width)) == width)
{
RecordId rid;
rec.assign(record, width);
fscan.insertRecord(rec, rid);
for(i = 0; i < attrCnt; i++)
{
if (index[i])
{
index[i]->insertEntry(record + attrs[i].attrOffset, rid);
}
}
records++;
}
std::cout << "Number of records inserted: " << records << std::endl;
if(close(fd) < 0)
throw FileOpenException(fileName);
for(i = 0; i < attrCnt; i++)
{
if(index[i])
delete index[i];
}
delete []index;
delete [] record;
delete [] attrs;
}
| void badgerdb::Utilities::Print | ( | const std::string & | relation | ) | [static] |
Print a relation.
Definition at line 98 of file utility.cpp.
{
RelDesc rd;
AttrDesc *attrs;
int attrCnt;
if(relation.empty())
std::string relation = RELCATNAME;
relCat->getInfo(relation, rd);
// get attribute data
attrCat->getRelInfo(rd.relName, attrCnt, attrs);
// compute width of output columns
int *attrWidth;
UT_computeWidth(attrCnt, attrs, attrWidth);
// open data file
FileScan filescan(rd.relName, bufMgr);
for(int i = 0; i < attrCnt; i++)
{
printf("%-*.*s%c ", attrWidth[i], attrWidth[i], attrs[i].attrName, (attrs[i].indexed ? '*' : ' '));
}
printf("\n");
for(int i = 0; i < attrCnt; i++) {
for(int j = 0; j < attrWidth[i]; j++)
putchar('-');
printf(" ");
}
printf("\n");
RecordId rid;
int records = 0;
while(1)
{
try
{
filescan.scanNext(rid);
std::string recordStr = filescan.getRecord();
UT_printRec(attrCnt, attrs, attrWidth, recordStr);
records++;
}
catch(EndOfFileException &e)
{
break;
}
}
std::cout << std::endl << "Number of records: " << records << std::endl;
delete [] attrWidth;
delete [] attrs;
}
| void badgerdb::Utilities::Quit | ( | ) | [static] |
Quit the DBMS and perform any necessary cleanup.
Definition at line 251 of file utility.cpp.
{
// close relcat and attrcat
delete ::relCat;
delete ::attrCat;
// delete bufMgr to flush out all dirty pages
delete ::bufMgr;
exit(1);
}
1.7.6.1