Exemple #1
0
int main(int argc, char ** argv) {
    std::cout << "Hello :-)" << std::endl;

    BufferManager* bm = new BufferManager(20);
    SchemaManager* sm = new SchemaManager(*bm);

    sm->dropSchema("test");

    int size = sizeof(struct Schema) + 1*sizeof(struct Schema::Attribute);
    struct Schema * schema = reinterpret_cast<struct Schema *>(malloc(size));
    schema->attr_count = 1;
    schema->primary_key_index[0] = 0;
    schema->primary_key_index[1] = 5;
    schema->primary_key_index[2] = 5;
    schema->primary_key_index[3] = 5;
    schema->primary_key_index[4] = 5;
    strncpy(schema->name, "test", 11);
    schema->attributes[0].type = Schema::Attribute::Type::Integer;
    strncpy(schema->attributes[0].name, "id", 3);
    schema->attributes[0].notNull = true;
 
    sm->addSchema(*schema);

    int size2 = sizeof(struct Schema) + 2*sizeof(struct Schema::Attribute);
    struct Schema * schema2 = reinterpret_cast<struct Schema *>(malloc(size2));
    schema2->attr_count = 2;
    schema2->primary_key_index[0] = 0;
    schema2->primary_key_index[1] = 5;
    schema2->primary_key_index[2] = 5;
    schema2->primary_key_index[3] = 5;
    schema2->primary_key_index[4] = 5;
    strncpy(schema2->name, "test-table", 11);
    schema2->attributes[0].type = Schema::Attribute::Type::Integer;
    strncpy(schema2->attributes[0].name, "id", 3);
    schema2->attributes[0].notNull = true;
    schema2->attributes[1].type = Schema::Attribute::Type::Varchar;
    schema2->attributes[1].length = 20;
    strncpy(schema2->attributes[1].name, "name", 5);
    schema2->attributes[1].notNull = true;
    
    sm->addSchema(*schema2);

    sm->incrementPagesCount("test-table");
    sm->incrementPagesCount("test");

    const struct Schema * schema3 = sm->getSchema("test");
    std::cout << schema3->name << ": " << schema3->page_count << std::endl;

    const struct Schema * schema4 = sm->getSchema("test-table");
    std::cout << schema4->name << ": " << schema4->page_count << std::endl;
    
    const struct Schema * nullSchema = sm->getSchema("nonexistent");
    std::cout << (nullSchema == NULL) << std::endl;

    delete sm;
    delete bm;

    return 0;

}
void preProcess(const vector<string> &tables, vector<string> &words, SchemaManager &schema_manager){
	for (int i = 0; i < words.size(); i++){
		bool is_column = false;
		// has no "."
		for (int j = 0; j < tables.size(); j++){
			if (words[i].find('.') == string::npos){
				if (schema_manager.getSchema(tables[j]).fieldNameExists(words[i])){
					words[i] = tables[j] + "." + words[i];
					is_column = true;
					break;
				}
			}
			// term or value
			if (!is_column){
				string legal_word;
				// removing tailing and head spaces for our custom test case
				string::iterator it = words[i].begin();
				while(it != words[i].end() && *it == ' ') words[i].erase(it++);
				reverse(words[i].begin(), words[i].end());
				it = words[i].begin();
				while(it != words[i].end() && *it == ' ') words[i].erase(it++);
				reverse(words[i].begin(), words[i].end());
				for (int k = 0; k < words[i].size(); k++){
					if (words[i][k] != '"'){
						legal_word.push_back(words[i][k]);
					}
				}
				words[i] = legal_word;
			}
		}
	}
}
Exemple #3
0
string crossJoin(vector<string> attributes, string tableName1, string tableName2, string whereCondition, bool multi) {

	string small,big;
	bool proj = false;
	if(schemaManager.getRelation(tableName1)->getNumOfBlocks()<=schemaManager.getRelation(tableName2)->getNumOfBlocks()) {
		small = tableName1;
		big = tableName2;
	}
	else {
		small=tableName2;
		big=tableName1;
	}
	Schema schema1 = schemaManager.getSchema(small);
        Schema schema2 = schemaManager.getSchema(big);
        vector<string> fieldNames;
        vector<enum FIELD_TYPE> fieldTypes;
	if(!multi) {
        	for(int i=0;i<schema1.getNumOfFields();i++) {
                	fieldNames.push_back(small+"."+schema1.getFieldName(i));
                	fieldTypes.push_back(schema1.getFieldType(i));
        	}
	}
	else {
		for(int i=0;i<schema1.getNumOfFields();i++) {
                        fieldNames.push_back(schema1.getFieldName(i));
                        fieldTypes.push_back(schema1.getFieldType(i));
                }
	}
        for(int i=0;i<schema2.getNumOfFields();i++) {
                fieldNames.push_back(big+"."+schema2.getFieldName(i));
                fieldTypes.push_back(schema2.getFieldType(i));
        }
        Schema schema(fieldNames,fieldTypes);
        Relation *relation = schemaManager.createRelation(big+"_join",schema);
	Relation *relation1 = schemaManager.getRelation(small);
	Relation *relation2 = schemaManager.getRelation(big);
	int size1 = relation1->getNumOfBlocks(), size2 = relation2->getNumOfBlocks();
	if(!((attributes.size()==1 && attributes[0]=="*") || multi)) {
		vector<string> fieldNames1;
		vector<enum FIELD_TYPE> fieldTypes1;
		for(int i=0;i<attributes.size();i++) {
			int temp = schema.getFieldOffset(attributes[i]);
			fieldNames1.push_back(schema.getFieldName(temp));
			fieldTypes1.push_back(schema.getFieldType(attributes[i]));
		}
		Schema schema1(fieldNames1, fieldTypes1);
		proj = true;
		Relation *relationp = schemaManager.createRelation(big+"_joinp", schema1);
	}
	if(size1<=10) {
		relation1->getBlocks(0,0,size1);
		vector<Tuple> tuples = mainMemory.getTuples(0,size1);
		for(int x=0;x<tuples.size();x++) {
			for(int i=0;i<size2;i++) {
                                 relation2->getBlock(i,1);
                                 Block *block = mainMemory.getBlock(1);
                                 for(int j=0;j<block->getNumTuples();j++) {
                                         Tuple tuple2 = block->getTuple(j);
                                         join(tuples[x], tuple2, small, big, whereCondition, multi, attributes);
                                 }
                         }

		}
	}	
	else {
	for(int x=0;x<size1;x++) {
		relation1->getBlock(x,0);
		Block *block0 = mainMemory.getBlock(0);
		for(int y=0;y<block0->getNumTuples();y++) {
			Tuple tuple1 = block0->getTuple(y);	
			for(int i=0;i<size2;i++) {
				relation2->getBlock(i,1);
				Block *block = mainMemory.getBlock(1);
				for(int j=0;j<block->getNumTuples();j++) {
					Tuple tuple2 = block->getTuple(j);
					join(tuple1, tuple2, small, big, whereCondition, multi, attributes);
				}
			}
		}
	}
	}
	string rt = big+"_join";
	if(proj) {
		schemaManager.deleteRelation(rt);
		rt = big+"_joinp";
	}
	return rt;
}