unsigned long IndexManager::getIndexID(Attribute attr) { if (isIndexExist(attr)) return column_attribute_to_id[attr]; cout << "[ERROR: IndexManager.cpp->getIndexID()]: The index for column " << attr.attrName << " is not exist!\n"; return -1; }
bool Index::createIndex() { if (isIndexExist()){ //索引名已存在 throw Table_Index_Error("index", index_name); } Table T(table_name); if (!T.isTableExist()){ //表不存在 throw Table_Index_Error("table", table_name); } T.getTableInfo(); if (!T.isAttriExist(attr_name)){ //属性不存在 throw Table_Index_Error("attribute", attr_name); } if (!T.isAttriUnique(attr_name)){ //属性不是unique属性 throw Multip_Error(attr_name); } //满足创建索引的条件 //向indexlist.txt中追加索引名 ofstream outfile; outfile.open("indexlist.txt", ios::app); if (!outfile) throw File_openfail("indexlist.txt"); outfile << index_name << endl; outfile.close(); //新建记录该索引信息的文件 string filename; filename = index_name + "_indexinfo.txt"; outfile.open(filename.c_str(), ios::app); if (!outfile) throw File_openfail(filename.c_str()); outfile << index_name << " "; outfile << table_name << " "; outfile << attr_name << endl; outfile.close(); //新建记录该表所有索引信息的文件 filename = table_name + "_tableindexinfo.txt"; outfile.open(filename.c_str(), ios::app); if (!outfile) throw File_openfail(filename.c_str()); outfile << index_name << " "; outfile << attr_name << endl; outfile.close(); //新建储存该索引的B+树的文件 filename = index_name; outfile.open(filename.c_str(), ios::app); if (!outfile) throw File_openfail(filename.c_str()); outfile.close(); return true; }
//删除索引 bool Index::dropIndex() { if (!isIndexExist()){ //索引不存在,无法删除 cerr << "The index doesn't exist!" << endl; return false; } //索引存在,则删除或更新相关文件 //删除indexlist.txt中索引名 ifstream infile; infile.open("indexlist.txt", ios::in); //以只读模式打开文件,读取其中信息 if (!infile) throw File_openfail("indexlist.txt"); string indexname; string newIndexList = ""; while (!infile.eof()){ getline(infile, indexname); //每行只储存一个索引信息 if (indexname != "" && indexname != index_name){ //过滤掉要删除的索引,保留其余的索引信息,存在newIndexList中 newIndexList = newIndexList + indexname + '\n'; } } infile.close(); ofstream outfile; outfile.open("indexlist.txt", ios::out); //打开indexlist.txt并清空 if (!outfile) throw File_openfail("indexlist.txt"); outfile << newIndexList; //将保留的索引信息存入文件中 outfile.close(); string filename; string tbname; string idname; //删除记录索引信息的文件 filename = index_name + "_indexinfo.txt"; infile.open(filename.c_str(), ios::in); //读取索引所在的表名和字段信息 { infile >> indexname >>tbname >>idname; } string thisIndexinfo = indexname + " " + idname; infile.close(); remove(filename.c_str()); //移除文件 //删除_tableindexinfo.txt中索引信息 filename = tbname + "_tableindexinfo.txt"; infile.open(filename.c_str(), ios::in); //以只读模式打开文件,读取其中信息 if (!infile) throw File_openfail(filename.c_str()); string newIndexinfo = ""; string indexinfo; while (!infile.eof()){ getline(infile, indexinfo); //每行只储存一个索引信息 if (indexinfo != "" && indexinfo != thisIndexinfo){ //过滤掉要删除的索引信息,保留其余的索引信息,存在newIndexList中 newIndexinfo = newIndexinfo + indexinfo + '\n'; } } infile.close(); outfile.open(filename.c_str(), ios::out); //打开_tableindexlist.txt并清空 if (!outfile) throw File_openfail(filename.c_str()); outfile << newIndexinfo; //将保留的该表的索引信息存入文件中 outfile.close(); //删除储存该索引B+树的文件 filename = index_name; remove(filename.c_str()); //移除文件 return true; }