Exemple #1
0
void deleteFromTable(string tableName, string whereCondition) {

	if(whereCondition.empty()){
		if(!schemaManager.relationExists(tableName)) {
			cout<<"Illegal Table Name"<<endl;
			return;
		}
		Relation *relation = schemaManager.getRelation(tableName);
		while(relation->getNumOfBlocks())
		relation->deleteBlocks(relation->getNumOfBlocks()-1);
	}
	else {
		Relation *relation = schemaManager.getRelation(tableName);
		for(int i=0;i<relation->getNumOfBlocks();i++) {
			relation->getBlock(i,0);
			Block *block = mainMemory.getBlock(0);
			vector<Tuple> tuples = block->getTuples();
			for(int j=0;j<tuples.size();j++) {
				if(!tuples[j].isNull() && whereConditionEvaluator(whereCondition, tuples[j])) {
					block->nullTuple(j);
				}
			}	
			relation->setBlock(i,0);
		}
	}
}
Exemple #2
0
void insertTuple(string tableName, Tuple tuple) {
	Relation *relation = schemaManager.getRelation(tableName);
	if(relation->getNumOfBlocks()>0){
                relation->getBlock(relation->getNumOfBlocks()-1,9);
                Block *block = mainMemory.getBlock(9);
                if(block->isFull()){
                        block->clear();
                        block->appendTuple(tuple);
                        relation->setBlock(relation->getNumOfBlocks(),9);
                }
                else {
                        block->appendTuple(tuple);
                        relation->setBlock(relation->getNumOfBlocks()-1,9);
                }
        }
        else {
                Block *block = mainMemory.getBlock(9);
                block->clear();
                block->setTuple(0,tuple);
                relation->setBlock(0,9);
        }
}
Exemple #3
0
string distinct(string tableName) {

	Relation *relation = schemaManager.getRelation(tableName);
	Schema schema = relation->getSchema();
	int size = relation->getNumOfBlocks();
	vector<Tuple> tuples;
	bool flag = true;
	//one-pass
	if(size<=10) {
		relation->getBlocks(0,0,size);
		tuples = mainMemory.getTuples(0,size);
		tuples = getDistinctTuples(tuples);
		Relation *relation1 = schemaManager.createRelation(tableName+"_distinct",schema); 
		insertTuples(tableName+"_distinct",tuples);
	}
	//two pass
	else {
		int index = 0, loadSize=10;
		while(size>0) {
			relation->getBlocks(index,0,loadSize);
			for(int i=0;i<loadSize;i++) {
				Block *block = mainMemory.getBlock(i);
				for(int j=0;j<block->getNumTuples();j++) {
					tuples.push_back(block->getTuple(j));
				}
			}	
			tuples = getDistinctTuples(tuples);
			//partition(tuples, 0, tuples.size()-1);
			if(flag) {	
				Relation *relation2= schemaManager.createRelation(tableName+"_dis", schema);
				flag = false;
			}
			insertTuples(tableName+"_dis", tuples);
			Relation *relation2 = schemaManager.getRelation(tableName+"_dis");
			tuples.clear();
			index = index+10;
			size = size-10;
			if(size<10)
			loadSize = size;
		}
		if(size<=100) {
			Relation *relation2 = schemaManager.createRelation(tableName+"_distinct", schema);
			relation = schemaManager.getRelation(tableName+"_dis");
			int buckets = relation->getNumOfBlocks()/10;
			vector<Tuple> tuples;
			for(int i=0;i<10;i++) {
				for(int j=0;j<buckets;j++) {
					if(j*10+i > relation->getNumOfBlocks()) break;
					relation->getBlock(i+10*j,j);
					Block *block = mainMemory.getBlock(j);
					for(int k=0;k<block->getNumTuples();k++) {
						tuples.push_back(block->getTuple(k));
					}
				}
			}
			tuples = getDistinctTuples(tuples);
			insertTuples(tableName+"_distinct", tuples);
			tuples.clear();
			schemaManager.deleteRelation(tableName+"_dis");
		}
		else 
		cerr<<"Table size exceeds the limit size(mainMemory)^2"<<endl;
	}
	return tableName+"_distinct";
}
Exemple #4
0
string projection(vector<string> attributes, string tableName, string whereCondition) {
	
	Relation *relation = schemaManager.getRelation(tableName);
	Schema tableSchema = relation->getSchema();
	vector<string> fieldNames;
	vector<enum FIELD_TYPE> fieldTypes;
	vector<string>::iterator it;
	int flag=-1;
	bool print=true;
	for(it=attributes.begin();it!=attributes.end();it++) {
		for(int i=0;i<tableSchema.getNumOfFields();i++) {
			string temp = *it;
			if(tableSchema.getFieldName(i)==temp || tableName+"."+tableSchema.getFieldName(i) == temp) 
			flag=i;
		}
		if(flag!=-1) {
			fieldNames.push_back(tableSchema.getFieldName(flag));
			fieldTypes.push_back(tableSchema.getFieldType(flag));
			flag = -1;
		}	
	}
	if(attributes.size()==1 && attributes[0] == "*") {
		if(whereCondition.empty()) return tableName;
		fieldNames = tableSchema.getFieldNames();
		fieldTypes = tableSchema.getFieldTypes();
	} 
	Schema dupSchema(fieldNames,fieldTypes);
	Relation *relationDup = schemaManager.createRelation(tableName.append("_dup"), dupSchema);	
	Tuple tuple = relationDup->createTuple();
	vector<Tuple>::iterator it1;
	Block *block = mainMemory.getBlock(9);
	block->clear();
	int index=0;
	for(int i=0;i<relation->getNumOfBlocks();i++) {
		relation->getBlock(i,0);
		vector<Tuple> t = mainMemory.getBlock(0)->getTuples();
		for(it1=t.begin();it1!=t.end();it1++) {
			if(!it1->isNull()){
			for(int j=0;j<fieldNames.size();j++) {
				if(fieldTypes[j]==INT)
				tuple.setField(fieldNames[j],it1->getField(fieldNames[j]).integer);
				else
				tuple.setField(fieldNames[j],*(it1->getField(fieldNames[j]).str));
			}
			bool ttp = whereConditionEvaluator(whereCondition, *it1);
			if(ttp) {
				if(!block->isFull())	
				block->appendTuple(tuple);
				else {
					relationDup->setBlock(index,9);
					index++;
					block->clear();
					block->appendTuple(tuple);
				}
			}
			}
		}
	}
	if(index!=relationDup->getNumOfBlocks()-1)
		relationDup->setBlock(index, 9);
	return tableName;
}