void xyzsql_process_insert(insert_stmt *s ) { if ( s == NULL ) s = dynamic_cast<insert_stmt *>(stmt_queue.front().second); auto t = catm.exist_relation(s->table_name)->cols; if (verify_validation(s->values, t) == false) throw invalid_argument("Uncapatable values"); auto table_info = catm.exist_relation(s->table_name); Record r(*(s->values), table_info->cols); for(auto x : *(r.table_info)) { if(x->flag & (table_column::unique_attr | table_column::primary_attr)) { string filename; indexIterator cursor; int asdf = IndexManager.selectNode(cursor, s->table_name + "/index_" + x->name + ".db", condition::EQUALTO, r.get_value(x).to_str(x->data_type)); if (asdf == 0) throw invalid_argument("Unique Key already exists."); } } int blockNum, offset; RecordManager.insertRecord(s->table_name, r, blockNum, offset); table_info->inc_size(); for(auto x : *(r.table_info)) { if(x->flag & (table_column::unique_attr | table_column::primary_attr)) { IndexManager.insertNode(s->table_name + "/index_" + x->name + ".db", r.get_value(x->name).to_str(x->data_type) , blockNum, offset); } } }
void calc_algric_tree(algbric_node *root) { if (root->flag == true) return; string table_name; int blockNum, offset; auto new_col_list = new vector<table_column *>, old_col_list = new_col_list, old_col_list2 = new_col_list; // auto old_col_list = catm.exist_relation((root->left->table))->cols; switch ( root->op ) { case algbric_node::DIRECT : root->flag = true; return; case algbric_node::PROJECTION : { if (!root->left->flag) calc_algric_tree(root->left); old_col_list = catm.exist_relation((root->left->table))->cols; for( auto x : *(root->projection_list) ) { auto att = catm.exist_relation(x->relation_name)->get_column(x->attribute_name); new_col_list->push_back(new table_column((root->left->op == algbric_node::DIRECT ? x->attribute_name.c_str() : x->full_name.c_str()), att->data_type, att->str_len, 0 )); } table_name = create_temp_table(new_col_list); root->table = table_name; auto cursor = RecordManager.getCursor(root->left->table, catm.calc_record_size(root->left->table)); while (cursor->next()) { Record r = cursor->getRecord(); vector<record_value> result; for(auto i = new_col_list->begin(); i != new_col_list->end(); i++) { for(auto j = old_col_list->begin(); j != old_col_list->end(); j++ ) { if ( (*i)->name == (*j)->name ) { result.push_back(r.values[j-old_col_list->begin()]); } } } RecordManager.insertRecord(table_name, Record(result, new_col_list), blockNum, offset); } if (root->left->op != algbric_node::DIRECT) catm.drop_table(root->left->table); root->flag = true; return; } case algbric_node::SELECTION : { string left_name = root->left->table; old_col_list = catm.exist_relation(left_name)->cols; for( auto x : *(old_col_list) ) { new_col_list->push_back( new table_column(x->name.c_str(), x->data_type, x->str_len, x->flag )); } table_name = create_temp_table(new_col_list); root->table = table_name; condition *p = NULL, *eq = NULL; for(auto x : (root->conditions)) { if ( catm.is_indexed(x->left_attr) ) { p = x; if (x->op == condition::EQUALTO) eq = x; } } if ( eq != NULL ) p = eq; if ( p != NULL ) { cout << "Index used: " << p->left_attr->full_name << endl; int record_size = catm.calc_record_size(root->left->table); auto t = p->left_attr; indexIterator cursor; int asdf = IndexManager.selectNode(cursor, t->relation_name + "/index_" + t->attribute_name + ".db", p->op, (p->v).to_str(catm.get_data_type(t))); if ( asdf == 0 ) { int b = 0, c = 0; while (cursor.next(b, c) == 0) { Record a = RecordManager.getRecord(t->relation_name, b, c, record_size); if (calc_conditions(&(root->conditions), a)) RecordManager.insertRecord(table_name, a, blockNum, offset); } } } else { string t = root->left->table; int record_size = catm.calc_record_size(t); indexIterator cursor; int asdf = IndexManager.getStarter(cursor, t + "/index_" + catm.get_primary(t) + ".db"); if ( asdf == 0 ) { int b = 0, c = 0; while (cursor.next(b, c) == 0) { Record a = RecordManager.getRecord(t, b, c, record_size); if (calc_conditions(&(root->conditions), a)) RecordManager.insertRecord(table_name, a, blockNum, offset); } } } root->flag = true; return; } case algbric_node::JOIN : { if (!root->left->flag) calc_algric_tree(root->left); if (!root->right->flag) calc_algric_tree(root->right); if ( catm.get_size(root->right->table) < catm.get_size(root->left->table) ) { auto tmp = root->left; root->left = root->right; root->right = tmp; } old_col_list = catm.exist_relation((root->left->table))->cols; old_col_list2= catm.exist_relation((root->right->table))->cols; for( auto x : *old_col_list ) { new_col_list->push_back(new table_column(x->name.c_str(), x->data_type, x->str_len, 0 )); } for( auto x : *old_col_list2 ) { new_col_list->push_back(new table_column(x->name.c_str(), x->data_type, x->str_len, 0 )); } table_name = create_temp_table(new_col_list); root->table = table_name; auto outter_table = catm.exist_relation(root->left->table), inner_table = catm.exist_relation(root->right->table); int outter_size = catm.calc_record_size(root->left->table), inner_size = catm.calc_record_size(root->right->table); outter_table->get_size(); condition * p = NULL; for ( auto x : root->conditions ) { if ( outter_table->get_column(x->left_attr->full_name) != NULL && inner_table->get_column(x->right_attr->full_name) != NULL) { } else if ( inner_table->get_column(x->left_attr->full_name) != NULL && outter_table->get_column(x->right_attr->full_name) != NULL) { auto tmp = x->left_attr; x->left_attr = x->right_attr; x->right_attr = tmp; } assert( outter_table->get_column(x->left_attr->full_name) != NULL && inner_table->get_column(x->right_attr->full_name) != NULL); if ( inner_table->is_indexed(x->right_attr->full_name) ) { p = x; } } auto cursor1 = RecordManager.getCursor(root->left->table, outter_size); cout << "Index used: " << p->right_attr->full_name << endl; while (cursor1->next()) { Record r1 = cursor1->getRecord(); if ( p ) { // nested-index join indexIterator a; int asdf = IndexManager.getStarter(a, root->right->table + "/index_" + p->right_attr->full_name + ".db"); if (asdf == 0) { int b = 0, c = 0; while (a.next(b, c) == 0) { Record r2 = RecordManager.getRecord(root->right->table, b, c, inner_size); if ( calc_conditions(&(root->conditions), r1, r2) ) { vector<record_value> result(r1.values); result.insert(result.end(), r2.values.begin(), r2.values.end()); RecordManager.insertRecord(table_name, Record(result, new_col_list), blockNum, offset); } } } } else { // nested-loop join auto cursor2 = RecordManager.getCursor(root->right->table, inner_size); while (cursor2->next()) { Record r2 = cursor2->getRecord(); if ( calc_conditions(&(root->conditions), r1, r2) ) { vector<record_value> result(r1.values); result.insert(result.end(), r2.values.begin(), r2.values.end()); RecordManager.insertRecord(table_name, Record(result, new_col_list), blockNum, offset); } } delete cursor2; } } delete cursor1; if (root->right->op != algbric_node::DIRECT) catm.drop_table(root->right->table); if (root->left->op != algbric_node::DIRECT) catm.drop_table(root->left->table); root->flag = true; return; } } }
void xyzsql_process_delete() { auto s = dynamic_cast<delete_stmt *>(stmt_queue.front().second); if (s->condition_list->empty()) { // delete all system(("rm " + s->table_name + "/*.db").c_str()); RecordManager.createMaster(s->table_name); auto table_info = catm.exist_relation(s->table_name); auto cols = table_info->cols; table_info->set_size(0); for(auto x : *cols) if(x->flag & (table_column::unique_attr | table_column::primary_attr)) IndexManager.createIndex(s->table_name + "/index_" + x->name + ".db", data_type_to_str(x->data_type), x->str_len, 0, {}, {}, {}); } else { auto table_info = catm.exist_relation(s->table_name); int record_size = catm.calc_record_size(s->table_name); BufferManager.newTrashCan(); // unique condition *p = NULL, *eq = NULL; for(auto x : *(s->condition_list)) { if ( catm.is_indexed(x->left_attr) ) { p = x; if (x->op == condition::EQUALTO) eq = x; } } if ( eq != NULL || p != NULL ) { cout << "Index used: " << p->left_attr->full_name << endl; auto t = eq == NULL ? p->left_attr : eq->left_attr; if (eq != NULL) p = eq; indexIterator a; int asdf = IndexManager.selectNode(a, base_addr + "/" + t->relation_name + "/index_" + t->attribute_name + ".db", p->op, (p->v).to_str(catm.get_data_type(t))); if ( asdf == 0 ) { int b = 0, c = 0; while (a.next(b, c) == 0) { Record a = RecordManager.getRecord(t->relation_name, b, c, record_size); if (calc_conditions(s->condition_list, a)) BufferManager.appendTrashCan(b, c); } } } else { indexIterator a; int asdf = IndexManager.getStarter(a, s->table_name + "/index_" + catm.get_primary(s->table_name) + ".db"); if (asdf == 0) { int b = 0, c = 0; while (a.next(b, c) == 0) { Record a = RecordManager.getRecord(s->table_name, b, c, record_size); if (calc_conditions(s->condition_list, a)) BufferManager.appendTrashCan(b, c); } } } BufferManager.beginFetchTrash(); int blockNum, offset; while(BufferManager.fetchTrash(blockNum, offset)) { auto r = RecordManager.getRecord(s->table_name, blockNum, offset, record_size); for(auto x : *(r.table_info)) { if(x->flag & (table_column::unique_attr | table_column::primary_attr)) { indexIterator cursor; IndexManager.deleteNode(s->table_name + "/index_" + x->name + ".db", r.get_value(x).to_str(x->data_type)); } } RecordManager.deleteRecord(s->table_name, blockNum, offset, record_size); table_info->dec_size(); } } cout << "records deleted." << endl; }