BadgerDB
|
00001 #include <sys/types.h> 00002 #include <sys/stat.h> 00003 #include <cstdio> 00004 #include <cstring> 00005 #include <string> 00006 #include <unistd.h> 00007 #include "catalog.h" 00008 #include "utility.h" 00009 #include "filescan.h" 00010 #include "query.h" 00011 #include "sort.h" 00012 #include "exceptions/end_of_file_exception.h" 00013 #include "parser/SqlParser.h" 00014 #include "parser/SqlInterp.h" 00015 00016 #define NUMBUFS 100 00017 #define TESTREL "testrel" 00018 #define TESTREL2 "testrel2" 00019 00020 00021 extern FILE *yyin; 00022 extern void parse(); 00023 00024 using namespace badgerdb; 00025 00026 badgerdb::BufMgr *bufMgr; // pointer to the buffer manager 00027 badgerdb::RelCatalog *relCat; // pointer to the relation catalogs 00028 badgerdb::AttrCatalog *attrCat; // pointer to the attribute catalogs 00029 00030 00031 /* 00032 * Add tuples describing relcat and attrcat to relation catalog and attribute catalog 00033 */ 00034 void addToRelCat(std::string relationNameStr, int attrCnt, int indexCnt){ 00035 RelDesc relationDesc; 00036 relationDesc.relName[relationNameStr.size()]=0; 00037 memcpy(relationDesc.relName,relationNameStr.c_str(),relationNameStr.size()); 00038 relationDesc.attrCnt = attrCnt; 00039 relationDesc.indexCnt = indexCnt; 00040 relCat->addInfo(relationDesc); 00041 } 00042 void addToAttrCat(std::string relationNameStr,std::string attrNameStr, int attrOffset, int attrType, int attrLen, int indexed){ 00043 00044 AttrDesc attrDesc; 00045 attrDesc.relName[relationNameStr.size()]=0; 00046 memcpy(attrDesc.relName,relationNameStr.c_str(),relationNameStr.size()); 00047 attrDesc.attrName[attrNameStr.size()]=0; 00048 memcpy(attrDesc.attrName,attrNameStr.c_str(),attrNameStr.size()); 00049 attrDesc.attrOffset = attrOffset; 00050 attrDesc.attrType = attrType; 00051 attrDesc.attrLen = attrLen; 00052 attrDesc.indexed = indexed; 00053 attrCat->addInfo(attrDesc); 00054 00055 } 00056 00057 void bootstrapCatalogs() 00058 { 00059 00060 std::string relation; 00061 RelDesc outRelationDesc; 00062 RelDesc outAttrDesc; 00063 00064 //BOOTSTRAP RELCAT 00065 addToRelCat("relcat", 3, 0); 00066 addToRelCat("attrcat", 6, 0); 00067 00068 //BOOTSTRAP ATTRCAT 00069 addToAttrCat("relcat", "relname",0,STRING,(MAXNAMESIZE * sizeof(char)),0); 00070 addToAttrCat("relcat", "attrcnt",(MAXNAMESIZE * sizeof(char)),INTEGER,sizeof(int),0); 00071 addToAttrCat("relcat", "indexcnt",(MAXNAMESIZE * sizeof(char)) + sizeof(int) ,INTEGER,sizeof(int),0); 00072 addToAttrCat("attrcat", "relname",0,STRING,(MAXNAMESIZE * sizeof(char)),0); 00073 addToAttrCat("attrcat", "attrname",(MAXNAMESIZE * sizeof(char)),STRING,(MAXNAMESIZE * sizeof(char)),0); 00074 addToAttrCat("attrcat", "attroffset",2*(MAXNAMESIZE * sizeof(char)),INTEGER,sizeof(int),0); 00075 addToAttrCat("attrcat", "attrtype",2*(MAXNAMESIZE * sizeof(char)) +sizeof(int) ,INTEGER,sizeof(int),0); 00076 addToAttrCat("attrcat", "attrlen",2*(MAXNAMESIZE * sizeof(char))+ 2*sizeof(int) ,INTEGER,sizeof(int),0); 00077 addToAttrCat("attrcat", "indexed",2*(MAXNAMESIZE * sizeof(char)) + 3*sizeof(int),INTEGER,sizeof(int),0); 00078 00079 if(DEBUG) 00080 Helpers::printAttrCat("attrcat"); 00081 00082 attrCat->flushFile(); 00083 relCat->flushFile(); 00084 } 00085 00086 00087 //Driver program for creating the database 00088 int main(int argc, char *argv[]) 00089 { 00090 //bootstrapCatalogs(); 00091 if( argc < 2 ) 00092 { 00093 std::cerr << "Usage: " << argv[0] << " <dbname>" << " <queryfile>" << std::endl; 00094 std::cerr << "<queryfile> is optional" << std::endl; 00095 return 1; 00096 } 00097 00098 bool newDB = true; 00099 00100 //Create database subdirectory and chdir there 00101 if(mkdir(argv[1], S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP) < 0) 00102 { 00103 std::cerr << "Using database from last run." << std::endl; 00104 newDB = false; 00105 } 00106 00107 if(chdir(argv[1]) < 0) 00108 { 00109 perror("chdir"); 00110 exit(1); 00111 } 00112 00113 bufMgr = new BufMgr(NUMBUFS); 00114 relCat = new RelCatalog(bufMgr); 00115 attrCat = new AttrCatalog(bufMgr); 00116 00117 if(newDB) 00118 { 00119 bootstrapCatalogs(); 00120 } 00121 00122 if(argc > 2) 00123 { 00124 if(!(yyin = fopen (argv[2], "r"))) 00125 { 00126 cerr << "Error in opening file: " << argv[2] << endl; 00127 exit (-1); 00128 } 00129 } 00130 00131 //Launch the command prompt 00132 parse(); 00133 00134 //If quit command not used. 00135 delete relCat; 00136 delete attrCat; 00137 delete bufMgr; 00138 00139 return 0; 00140 }