Info API::CreateIndex(SqlCommandCreateIndex* command){ IndexManager index_manager; CatalogManager catalog_manager; RecordManager record_manager; std::string index_name = command->index_name(); std::string attribute_name = command->column_name(); std::string table_name = command->table_name(); if (catalog_manager.HasIndex(index_name)){ std::string error_info; error_info = "Index \"" + index_name + "\" already exists."; return Info(error_info); } if (!catalog_manager.HasTable(table_name)){ std::string error_info; error_info = "table \"" + table_name + "\" not exists."; return Info(error_info); } TableInfo table = catalog_manager.GetTableInfo(table_name); if(!table.HasAttribute(attribute_name)){ std::string error_info; error_info = "attribute \"" + attribute_name + "\" not exists."; return Info(error_info); } if (!table.attribute(attribute_name).is_unique() && !table.attribute(attribute_name).is_primary_key()){ std::string error_info; error_info = "attribute \"" + attribute_name + "\" is not an unique attribute."; return Info(error_info); } else{ IndexInfo index_info(index_name, table_name, attribute_name); //create index in index manager if(index_manager.CreateIndex(table.attribute(attribute_name).type(),table.attribute(attribute_name).length(), index_info)){ auto all_records = record_manager.SelectAllRecords(table); for (auto record : all_records){ if(!index_manager.AddRecord(table,index_info,record.first.at(table.attribute_index(index_info.attribute_name())),record.second)){ return Info("Update index failed."); } } if (!catalog_manager.RegisterIndex(index_info)) { return Info("Register Index failed."); } //update table info in catalog manager table.add_index(attribute_name, index_name); catalog_manager.WriteTableInfo(table); return Info(); } else{ return Info("Create index failed."); } } }