コード例 #1
0
ファイル: Table.cpp プロジェクト: MaTTze/even-tinierDB
//---------------------------------------------------------------------------
static void readIndex(istream& in,map<Register,unsigned>& index,Attribute::Type type,unsigned cardinality)
   // Read an index
{
   unsigned count;
   in.read(reinterpret_cast<char*>(&count),sizeof(count));
   if (count!=cardinality) {
      cerr << "index corruption encountered, reload the database" << endl;
      throw;
   }
   index.clear();

   switch (type) {
      case Attribute::Type::Int:
         for (unsigned index2=0;index2<count;++index2) {
            int val; unsigned pos;
            in.read(reinterpret_cast<char*>(&val),sizeof(val)); in.read(reinterpret_cast<char*>(&pos),sizeof(pos));
            Register r; r.setInt(val);
            index[r]=pos;
         }
         break;
      case Attribute::Type::Double:
         for (unsigned index2=0;index2<count;++index2) {
            double val; unsigned pos;
            in.read(reinterpret_cast<char*>(&val),sizeof(val)); in.read(reinterpret_cast<char*>(&pos),sizeof(pos));
            Register r; r.setDouble(val);
            index[r]=pos;
         }
         break;
      case Attribute::Type::Bool:
         for (unsigned index2=0;index2<count;++index2) {
            bool val; unsigned pos;
            in.read(reinterpret_cast<char*>(&val),sizeof(val)); in.read(reinterpret_cast<char*>(&pos),sizeof(pos));
            Register r; r.setBool(val);
            index[r]=pos;
         }
         break;
      case Attribute::Type::String:
         for (unsigned index2=0;index2<count;++index2) {
            unsigned valLen,pos; string val;
            in.read(reinterpret_cast<char*>(&valLen),sizeof(valLen));
            val.resize(valLen);
            in.read(const_cast<char*>(val.data()),valLen);
            in.read(reinterpret_cast<char*>(&pos),sizeof(pos));
            Register r; r.setString(val);
            index[r]=pos;
         }
         break;
   }
}
コード例 #2
0
//executes query canonically 
void Executor::execute(query q){
	 
	Database db; 
	db.open("data/uni"); 
	
	if(q.from.size() >= 1){
		
		unordered_map<string, unordered_map<string, const Register*>> registers; 
		unique_ptr<Operator> crossproduct(nullptr); 
		
		//Make crossproducts
		int counter = 0; 
		for(auto it = q.from.begin(); it != q.from.end(); ++it) {
			unique_ptr<Tablescan> tablescan(new Tablescan(db.getTable(it->first))); 
			//Used for constants push down
	
			//Get names of all attributes an populate tables
			vector<string> names = db.getTable(it->first).getAttributeNames();
			populateRegisterTable(&registers, *tablescan, &names, it->second); 		
			unique_ptr<Operator> selection(move(tablescan)); 
			
			//Find pushdowns and execute them
			for(auto it2 = q.where.begin(); it2 != q.where.end(); it2++){ 
				
				if((*it2).r_attr.first == "" && (*it2).l_attr.first == it->second){ //See if bindings are the same  
					Register* registertmp = new Register(); //tmpp
					
					int attrIndex = db.getTable(it->first).findAttribute((*it2).l_attr.second);  
					
					if(attrIndex < 0){
						throw "NO EXISTENT ATTRIBUTE";
					}
					const Attribute& attr = db.getTable(it->first).getAttribute(attrIndex); 
					Attribute::Type attr_type = attr.getType(); 
					string name = attr.getName(); 
					
					//Check type for attribute
					if(attr_type == Attribute::Type::Int)
					{
						int tmp; 
						size_t end; 
						tmp = stoi((*it2).r_attr.second, &end); 
						registertmp->setInt(tmp);
					}
					else if(attr_type == Attribute::Type::Double)
					{
						int tmp; 
						size_t end; 
						tmp = stod((*it2).r_attr.second, &end); 
						registertmp->setInt(tmp);
					}
					else if(attr_type == Attribute::Type::Bool)
					{
						//TODO
					}
					else
					{
						registertmp->setString((*it2).r_attr.second);
					}
					
					unique_ptr<Operator> seltmp(new Selection(move(selection), registers[it->second][(*it2).l_attr.second] ,registertmp )); //tmp
					selection.swap(seltmp); 
				}
			}
			if(crossproduct == nullptr){ 
				crossproduct.swap(selection); 
			}
			else{
				unique_ptr<Operator> dummy(new CrossProduct(move(selection), move(crossproduct)));
				crossproduct.swap(dummy);   
			}
		}
	
		//Find join conditions and execute them
		for(auto it2 = q.where.begin(); it2 != q.where.end(); it2++){
			if((*it2).r_attr.first != ""){ //See if bindings are the 
				string rightTable; 
				string leftTable; 
				for(auto it3 = q.from.begin(); it3 != q.from.end(); ++it3 ){
					if(it3->second == (*it2).r_attr.first) {
						rightTable = it3->second; 
					}
					else if(it3->second == (*it2).l_attr.first) {
						leftTable = it3->second; 
					}
				}
				unique_ptr<Operator> seltmp(new Selection(move(crossproduct), registers[leftTable][(*it2).l_attr.second] ,registers[rightTable][(*it2).r_attr.second] )); //tmp
				crossproduct.swap(seltmp); 
			}	
		}
		
		
		//Print the names of the students
		Printer out(move(crossproduct));
		out.open();
		while (out.next());
		out.close();
	}	
}