/** * @author Krunoslav Bilić * @brief Function for selection operator testing * */ void AK_op_selection_test2() { AK_PRO; printf("\n********** SELECTION TEST 2**********\n"); struct list_node *expr = (struct list_node *) AK_malloc(sizeof(struct list_node)); Ak_Init_L3(&expr); char *srcTable = "student"; char *destTable = "selection_test2"; char num = 23; strcpy(expr->table,destTable); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "year", sizeof ("year"), expr); Ak_InsertAtEnd_L3(TYPE_INT, &num, sizeof (int), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, ">", sizeof (">"), expr); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "firstname", sizeof ("firstname"), expr); Ak_InsertAtEnd_L3(TYPE_VARCHAR, "Mislav", sizeof ("Mislav"), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "=", sizeof ("="), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "OR", sizeof ("OR"), expr); printf("\nQUERY: SELECT * FROM student WHERE year > 2023 OR firstname = 'Mislav';\n\n"); AK_selection(srcTable, destTable, expr); //AK_print_table("selection_test"); Ak_DeleteAll_L3(&expr); AK_free(expr); AK_EPI; }
/** * @author Matija Šestak. * @brief Function that fetches a value in some row and column * @param row zero-based row index * @param column zero-based column index * @param *tblName table name * @return value in the list */ struct list_node *AK_get_tuple(int row, int column, char *tblName) { AK_PRO; int num_rows = AK_get_num_records(tblName); int num_attr = AK_num_attr(tblName); if (row >= num_rows || column >= num_attr){ AK_EPI; return NULL; } table_addresses *addresses = (table_addresses*) AK_get_table_addresses(tblName); struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root); int i, j, k, counter; char data[ MAX_VARCHAR_LENGTH ]; i = 0; counter = -1; while (addresses->address_from[ i ] != 0) { for (j = addresses->address_from[ i ]; j < addresses->address_to[ i ]; j++) { AK_mem_block *temp = (AK_mem_block*) AK_get_block(j); if (temp->block->last_tuple_dict_id == 0) break; for (k = 0; k < DATA_BLOCK_SIZE; k += num_attr) { if (temp->block->tuple_dict[k].size > 0) counter++; if (counter == row) { struct list_node *next; int type = temp->block->tuple_dict[ k + column ].type; int size = temp->block->tuple_dict[ k + column ].size; int address = temp->block->tuple_dict[ k + column ].address; memcpy(data, &(temp->block->data[address]), size); data[ size ] = '\0'; Ak_InsertAtEnd_L3(type, data, size, row_root); AK_free(addresses); next = Ak_First_L2(row_root); //store next AK_free(row_root); //now we can free base AK_EPI; //returns next in row_root leaving base of the list allocated, so we made some corrections //return (struct list_node *) Ak_First_L2(row_root); return next; //returns next } } } i++; } AK_free(addresses); Ak_DeleteAll_L3(&row_root); AK_free(row_root); AK_EPI; return NULL; }
/** * @author Matija Šestak, updated by Dino Laktašić,Nikola Miljancic * @brief Function for selection operator testing * */ void AK_op_selection_test() { AK_PRO; printf("\n********** SELECTION TEST **********\n"); struct list_node *expr = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&expr); char *srcTable = "student"; char *destTable = "selection_test"; int num = 2010; strcpy(expr->table,destTable); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "year", sizeof ("year"), expr); Ak_InsertAtEnd_L3(TYPE_INT, &num, sizeof (int), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, ">", sizeof (">"), expr); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "firstname", sizeof ("firstname"), expr); Ak_InsertAtEnd_L3(TYPE_VARCHAR, "Robert", sizeof ("Robert"), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "=", sizeof ("="), expr); //Ak_InsertAtEnd_L3(TYPE_OPERATOR, "OR", sizeof ("OR"), expr); Ak_InsertAtEnd_L3( TYPE_OPERATOR, "AND", sizeof("AND"), expr ); printf("\nQUERY: SELECT * FROM student WHERE year > 2010 AND firstname = 'Robert';\n\n"); AK_selection(srcTable, destTable, expr); Ak_DeleteAll_L3(&expr); AK_free(expr); struct list_node *expr1 = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&expr1); char *srcTable1 = "student"; char *destTable1 = "selection_test1"; float weight = 83.750; strcpy(expr1->table,destTable1); Ak_InsertAtEnd_L3( TYPE_ATTRIBS, "weight", sizeof("weight"), expr1 ); Ak_InsertAtEnd_L3( TYPE_FLOAT, &weight, sizeof(float), expr1 ); Ak_InsertAtEnd_L3( TYPE_OPERATOR, ">", sizeof(">"), expr1 ); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "firstname", sizeof ("firstname"), expr1); Ak_InsertAtEnd_L3(TYPE_VARCHAR, "Dino", sizeof ("Robert"), expr1); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "=", sizeof ("="), expr1); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "OR", sizeof ("OR"), expr1); printf("\nQUERY: SELECT * FROM student WHERE weight > 83.750 OR firstname = 'Dino';\n\n"); AK_selection(srcTable1, destTable1, expr1); printf("\n Test is successful :) \n"); Ak_DeleteAll_L3(&expr1); AK_free(expr1); AK_EPI; }
/** * @author Jurica Hlevnjak, updated by Tomislav Ilisevic * @brief Help function for checking if the element(view, function, sequence, user ...) exist in system catalog table * @param tblName name of table, index view, function, trigger, sequence, user, group or constraint * @param sys_table name of system catalog table * @return if element exist in system catalog returns 1, if not returns 0 */ int AK_if_exist(char *tblName, char *sys_table) { AK_PRO; int num_rows = AK_get_num_records(sys_table); int a; struct list_node *el; for (a = 0; a < num_rows; a++) { el = AK_get_tuple(a, 1, sys_table); if (!strcmp(tblName, el->data)) { Ak_DeleteAll_L3(&el); AK_free(el); return 1; // exist } Ak_DeleteAll_L3(&el); AK_free(el); } AK_EPI; return 0; // not exist }
/** * @author Jurica Hlevnjak * @brief Function that examines whether there is a table with the name "tblName" in the system catalog (AK_relation) * @param tblName table name * @return returns 1 if table exist or returns 0 if table does not exist */ int AK_table_exist(char *tblName) { AK_PRO; char *sys_table = "AK_relation"; int num_rows = AK_get_num_records(sys_table); int a; int exist = 0; struct list_node *el; for (a = 0; a < num_rows; a++) { el = AK_get_tuple(a, 1, sys_table); if (!strcmp(tblName, el->data)) { exist = 1; Ak_DeleteAll_L3(&el); AK_free(el); break; } Ak_DeleteAll_L3(&el); AK_free(el); } AK_EPI; return exist; }
/** * @author Markus Schatten, Matija Šestak. * @brief Function that fetches all values in some row and put on the list * @param num zero-based row index * @param * tblName table name * @return row values list */ struct list_node *AK_get_row(int num, char * tblName) { AK_PRO; table_addresses *addresses = (table_addresses*) AK_get_table_addresses(tblName); struct list_node *row_root = (struct list_node *)AK_calloc(1, sizeof (struct list_node)); Ak_Init_L3(&row_root); int num_attr = AK_num_attr(tblName); int i, j, k, l, counter; i = 0; char data[MAX_VARCHAR_LENGTH]; counter = -1; while (addresses->address_from[i] != 0) { for (j = addresses->address_from[i]; j < addresses->address_to[i]; j++) { AK_mem_block *temp = (AK_mem_block*) AK_get_block(j); if (temp->block->last_tuple_dict_id == 0) break; for (k = 0; k < DATA_BLOCK_SIZE; k += num_attr) { if (temp->block->tuple_dict[k].size > 0) counter++; if (counter == num) { for (l = 0; l < num_attr; l++) { int type = temp->block->tuple_dict[k + l].type; int size = temp->block->tuple_dict[k + l].size; int address = temp->block->tuple_dict[k + l].address; memcpy(data, &(temp->block->data[address]), size); data[size] = '\0'; Ak_InsertAtEnd_L3(type, data, size, row_root); } AK_free(addresses); AK_EPI; return row_root; } } } i++; } AK_free(addresses); Ak_DeleteAll_L3(&row_root); AK_free(row_root); AK_EPI; return NULL; }
/** * @author Luka Rajcevic * @brief Function for selection operator on one table + @param src_table - name of the source table + @param dest_table - table in which selection will be stored * @param sel_query - array of operators, operands and attributes (postfix query) * @param _num - number of attributes * @param _type - array of attribute types (eg. TYPE_INT, TYPE_VARCHAR, etc.) * @return EXIT_SUCCESS if ok, EXIT_ERROR otherwise * */ int selection_test(char* src_table, char* dest_table, char** sel_query, int _num, int* _type){ AK_PRO; printf("==================== SELECTION_TEST =====================\n"); struct list_node *expr = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&expr); // TYPE_OPERAND 10 // TYPE_OPERATOR 11 // TYPE_ATTRIBS 12 strcpy(expr->table,dest_table); int i; for (i = 0; i < _num; i++){ if (_type[i] == TYPE_INT){ int val = atoi(sel_query[i]); Ak_InsertAtEnd_L3(_type[i], (char*) &val, sizeof(int), expr); } if (_type[i] == TYPE_FLOAT){ float val = atof(sel_query[i]); Ak_InsertAtEnd_L3(_type[i], (char *) &val, sizeof(float), expr); } if (_type[i] == TYPE_OPERATOR || _type[i] == TYPE_ATTRIBS || _type[i] == TYPE_VARCHAR){ Ak_InsertAtEnd_L3(_type[i], sel_query[i], strlen(sel_query[i]), expr); } } if (AK_selection(src_table, dest_table, expr) == EXIT_SUCCESS){ Ak_DeleteAll_L3(&expr); AK_free(expr); AK_EPI; return 1; } AK_EPI; return 0; }
/** * @author Luka Rajcevic * @brief Function for inserting test data into the table (needed for python testing) * @param tbl_name - name of the table for which the header will be created * @param attr_name - array of attribute names * @param attr_value - values of attributes to be inserted * @param _num - number of attributes * @param _type - array of attribute types (eg. TYPE_INT, TYPE_VARCHAR, etc.) * @return EXIT_SUCCESS if ok, EXIT_ERROR otherwise */ int insert_data_test(char* tbl_name, char** attr_name, char** attr_value, int _num, int* _type){ int i, ret; AK_PRO; struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root); Ak_DeleteAll_L3(&row_root); for (i = 0; i < _num; i++){ if (_type[i] == TYPE_VARCHAR) Ak_Insert_New_Element(_type[i], attr_value[i], tbl_name, attr_name[i], row_root); if (_type[i] == TYPE_INT){ int val = atoi(attr_value[i]); Ak_Insert_New_Element(_type[i], &val, tbl_name, attr_name[i], row_root); } if (_type[i] == TYPE_FLOAT){ float val = atof(attr_value[i]); Ak_Insert_New_Element(_type[i], &val , tbl_name, attr_name[i], row_root); } } ret = Ak_insert_row(row_root); AK_EPI; return ret; }
/** * @author Miroslav Policki * @brief Function for testing file search * @return No return value */ void Ak_filesearch_test() { int i; double f; AK_mem_block *mem_block, tmp; AK_header hBroj_int[4], *hTmp; struct list_node *row_root; AK_PRO; // create table and fill it for testing purposes hTmp = (AK_header *) AK_create_header("Number int", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); hBroj_int[0] = *hTmp; hTmp = (AK_header *) AK_create_header("Number float", TYPE_FLOAT, FREE_INT, FREE_CHAR, FREE_CHAR); hBroj_int[1] = *hTmp; hTmp = (AK_header *) AK_create_header("Varchar column", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); hBroj_int[2] = *hTmp; memset(&hBroj_int[3], 0, sizeof (AK_header)); if (EXIT_ERROR == AK_initialize_new_segment("filesearch test table", SEGMENT_TYPE_TABLE, hBroj_int)) { printf("filesearch_test: ERROR. Unable to create \"filesearch test table \"\n"); AK_EPI; exit(EXIT_ERROR); } row_root = AK_malloc(sizeof (struct list_node)); if (row_root == NULL) { printf("filesearch_test: ERROR. Cannot allocate row_root.\n"); AK_EPI; exit(EXIT_ERROR); } for (i = -10, f = -i; i < 10; i++, f = -i) { Ak_Init_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &i, "filesearch test table", "Number int", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &f, "filesearch test table", "Number float", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "test text", "filesearch test table", "Varchar column", row_root); Ak_insert_row(row_root); Ak_DeleteAll_L3(&row_root); } AK_free(row_root); // filesearch usage example starts here { char *szTmp; search_params sp[3]; search_result sr; int iLower, iUpper; double dTmp; sp[0].szAttribute = "Varchar column"; sp[0].iSearchType = SEARCH_ALL; sp[1].szAttribute = "Number int"; sp[1].iSearchType = SEARCH_RANGE; iLower = -5; iUpper = 5; sp[1].pData_lower = &iLower; sp[1].pData_upper = &iUpper; sp[2].szAttribute = "Number float"; sp[2].iSearchType = SEARCH_PARTICULAR; dTmp = 2; sp[2].pData_lower = &dTmp; Ak_dbg_messg(LOW, FILE_MAN, "Calling AK_search_unsorted"); sr = AK_search_unsorted("filesearch test table", sp, 3); for (i = 0; i < sr.iNum_tuple_addresses; i++) { //mem_block = AK_get_block (sr.aiBlocks[i]); // caching should be used when available mem_block = &tmp; mem_block->block = (AK_block *) AK_read_block(sr.aiBlocks[i]); printf("Found:%d\n", *((int *) (mem_block->block->data + mem_block->block->tuple_dict[sr.aiTuple_addresses[i] + 0].address))); printf("Found:%f\n", *((double *) (mem_block->block->data + mem_block->block->tuple_dict[sr.aiTuple_addresses[i] + 1].address))); szTmp = AK_malloc(mem_block->block->tuple_dict[sr.aiTuple_addresses[i] + 2].size + 1); memcpy(szTmp, mem_block->block->data + mem_block->block->tuple_dict[sr.aiTuple_addresses[i] + 2].address, mem_block->block->tuple_dict[sr.aiTuple_addresses[i] + 2].size); szTmp[mem_block->block->tuple_dict[sr.aiTuple_addresses[i] + 2].size] = '\0'; printf("Found:%s\n", szTmp); AK_free(szTmp); } AK_deallocate_search_result(sr); } AK_EPI; }
TestResult AK_test_Transaction() { AK_PRO; printf("***Test Transaction***\n"); pthread_mutex_lock(&endTransationTestLockMutex); pthread_mutex_lock(&newTransactionLockMutex); AK_init_observable_transaction(); // NOTE: This is the way on which we can broadcast notice to all observers // observable_transaction->observable->AK_notify_observers(observable_transaction->observable); memset(LockTable, 0, NUMBER_OF_KEYS * sizeof (struct transaction_list_head)); /**************** INSERT AND UPDATE COMMAND TEST ******************/ char *tblName = "student"; struct list_node *row_root_insert = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root_insert); Ak_DeleteAll_L3(&row_root_insert); int mbr, year; float weight; mbr = 38262; year = 2012; weight = 82.00; Ak_DeleteAll_L3(&row_root_insert); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root_insert); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivan", tblName, "firstname", row_root_insert); Ak_Insert_New_Element(TYPE_VARCHAR, "Pusic", tblName, "lastname", row_root_insert); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root_insert); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root_insert); struct list_node *row_root_update = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root_update); Ak_DeleteAll_L3(&row_root_update); Ak_Update_Existing_Element(TYPE_INT, &mbr, tblName, "mbr", row_root_update); Ak_Insert_New_Element(TYPE_VARCHAR, "pppppppppp", tblName, "lastname", row_root_update); command* commands_ins_up = AK_malloc(sizeof (command) * 2); commands_ins_up[0].tblName = "student"; commands_ins_up[0].id_command = INSERT; commands_ins_up[0].parameters = row_root_insert; commands_ins_up[1].tblName = "student"; commands_ins_up[1].id_command = UPDATE; commands_ins_up[1].parameters = row_root_update; int id_prof; id_prof = 35893; struct list_node *row_root_p_update = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root_p_update); Ak_DeleteAll_L3(&row_root_p_update); Ak_Update_Existing_Element(TYPE_INT, &id_prof, "professor", "id_prof", row_root_p_update); Ak_Insert_New_Element(TYPE_VARCHAR, "FOI", "professor", "firstname", row_root_p_update); /**************** DELETE COMMAND TEST ******************/ command* commands_delete = AK_malloc(sizeof (command) * 1); commands_delete[0].tblName = "professor"; commands_delete[0].id_command = DELETE; commands_delete[0].parameters = row_root_p_update; struct list_node *expr = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&expr); int num = 2010; Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "year", sizeof ("year"), expr); Ak_InsertAtEnd_L3(TYPE_INT, (char*)&num, sizeof (int), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "<", sizeof ("<"), expr); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "firstname", sizeof ("firstname"), expr); Ak_InsertAtEnd_L3(TYPE_VARCHAR, "Robert", sizeof ("Robert"), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "=", sizeof ("="), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "OR", sizeof ("OR"), expr); /**************** SELECT COMMAND TEST ******************/ command* commands_select = AK_malloc(sizeof(command) * 1); commands_select[0].tblName = "student"; commands_select[0].id_command = SELECT; commands_select[0].parameters = expr; /**************** EXECUTE TRANSACTIONS ******************/ AK_transaction_manager(commands_ins_up, 2); AK_transaction_manager(commands_ins_up, 2); AK_transaction_manager(commands_delete, 1); AK_transaction_manager(commands_select, 1); pthread_mutex_lock(&endTransationTestLockMutex); AK_free(expr); AK_free(commands_delete); AK_free(commands_select); AK_free(commands_ins_up); AK_free(row_root_insert); AK_free(row_root_update); AK_free(row_root_p_update); AK_free(observable_transaction); pthread_mutex_unlock(&endTransationTestLockMutex); printf("***End test Transaction***\n"); AK_EPI; return TEST_result(0,0); }
/** * @author Dino Laktašić. * @brief Main function for generating RA expresion according to selection equivalence rules * @param *list_rel_eq RA expresion as the struct list_node * @return optimised RA expresion as the struct list_node */ struct list_node *AK_rel_eq_selection(struct list_node *list_rel_eq) { int step; //, exit_cond[5] = {0}; AK_PRO; //Initialize temporary linked list struct list_node *temp = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&temp); // struct list_node *list_split_sel; struct list_node *tmp, *temp_elem, *temp_elem_prev, *temp_elem_next; struct list_node *list_elem_next, *list_elem = (struct list_node *) Ak_First_L2(list_rel_eq); //Iterate through all the elements of RA linked list while (list_elem != NULL) { switch (list_elem->type) { case TYPE_OPERATOR: Ak_dbg_messg(LOW, REL_EQ, "\nOPERATOR '%c' SELECTED\n", list_elem->data[0]); Ak_dbg_messg(LOW, REL_EQ, "----------------------\n"); temp_elem = (struct list_node *) Ak_End_L2(temp); temp_elem_prev = (struct list_node *) Ak_Previous_L2(temp_elem, temp); list_elem_next = (struct list_node *) Ak_Next_L2(list_elem); switch (list_elem->data[0]) { //Commutativity of Selection and Projection. case RO_PROJECTION: step = -1; if (temp_elem != NULL) { while (temp_elem != NULL) { if (temp_elem->type == TYPE_OPERAND || temp_elem->type == TYPE_CONDITION) { if (temp_elem->type == TYPE_CONDITION) { temp_elem_prev = (struct list_node *) Ak_Previous_L2(temp_elem, temp); if ((AK_rel_eq_can_commute(list_elem_next, temp_elem) == EXIT_FAILURE) && (temp_elem_prev->data[0] == RO_SELECTION) && (temp_elem_prev->type == TYPE_OPERATOR)) { Ak_InsertAtEnd_L3(list_elem->type, list_elem->data, list_elem->size, temp); Ak_InsertAtEnd_L3(list_elem_next->type, list_elem_next->data, list_elem_next->size, temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted with condition (%s) in temp list\n", list_elem->data, list_elem_next->data); step++; break; } else if ((AK_rel_eq_can_commute(list_elem_next, temp_elem) == EXIT_SUCCESS) && (temp_elem_prev->data[0] == RO_SELECTION) && (temp_elem_prev->type == TYPE_OPERATOR)) { Ak_InsertBefore_L2(list_elem->type, list_elem->data, list_elem->size, &temp_elem_prev, &temp); Ak_InsertBefore_L2(list_elem_next->type, list_elem_next->data, list_elem_next->size, &temp_elem_prev, &temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted with condition (%s) in temp list\n", list_elem->data, list_elem_next->data); step++; break; } } } temp_elem = (struct list_node *) Ak_Previous_L2(temp_elem, temp); } } if (temp_elem == NULL || step != 0) { Ak_InsertAtEnd_L3(list_elem->type, list_elem->data, list_elem->size, temp); Ak_InsertAtEnd_L3(list_elem_next->type, list_elem_next->data, list_elem_next->size, temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted with condition (%s) in temp list\n", list_elem->data, list_elem_next->data); } list_elem = list_elem->next; break; //Cascade of Selection and Commutativity of Selection case RO_SELECTION: //Join cascade selection conditions to one if (temp_elem != NULL && temp_elem_prev != NULL && temp_elem->type == TYPE_CONDITION && temp_elem_prev->data[0] == RO_SELECTION && temp_elem_prev->type == TYPE_OPERATOR) { temp_elem->size = temp_elem->size + list_elem_next->size + strlen(" AND") + 1; //edit to (" AND ") //strcat(temp_elem->data, " AND "); //uncomment for infix use strcat(temp_elem->data, " "); //remove for infix strcat(temp_elem->data, list_elem_next->data); strcat(temp_elem->data, " AND"); //comment if using infix format memcpy(temp_elem->data, temp_elem->data, temp_elem->size); Ak_dbg_messg(MIDDLE, REL_EQ, "::selection cascade - condition changed to (%s) in temp list\n", temp_elem->data); } else { Ak_InsertAtEnd_L3(list_elem->type, list_elem->data, list_elem->size, temp); Ak_InsertAtEnd_L3(list_elem_next->type, list_elem_next->data, list_elem_next->size, temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted with attributes (%s) in temp list\n", list_elem->data, list_elem_next->data); } /*//Divide selection condition (slower than upper solution but can be useful in certain cases) list_split_sel = AK_rel_eq_split_condition(list_elem_next->data); struct list_node *list_elem_split = (struct list_node *)FirstL(list_split_sel); if (temp_elem != NULL) { tmp = temp_elem; while (list_elem_split != NULL) { step = 0; while (temp_elem != NULL) { if (temp_elem->type == TYPE_CONDITION || (temp_elem->data[0] == RO_SELECTION && temp_elem->type == TYPE_OPERATOR)) { if (temp_elem->type == TYPE_CONDITION && strcmp(list_elem_next->data, temp_elem->data) == 0) { step = 1; } } else if (!step){ InsertAtEndL(list_elem->type , list_elem->data, list_elem->size, temp); InsertAtEndL(list_elem_next->type, list_elem_split->data, list_elem_split->size, temp); break; } temp_elem = (struct list_node *)PreviousL(temp_elem, temp); } list_elem_split = list_elem_split->next; temp_elem = tmp; } } else { while (list_elem_split != NULL) { InsertAtEndL(list_elem->type , list_elem->data, list_elem->size, temp); InsertAtEndL(list_elem_next->type, list_elem_split->data, list_elem_split->size, temp); list_elem_split = list_elem_split->next; } } DeleteAllL(list_split_sel);*/ list_elem = list_elem->next; break; //Commutativity of Selection and set operations (Union, Intersection, and Set difference) case RO_UNION: case RO_INTERSECT: case RO_EXCEPT: step = -1; while (temp_elem != NULL) { if (temp_elem->type == TYPE_OPERAND || temp_elem->type == TYPE_CONDITION) { step++; temp_elem_prev = (struct list_node *) Ak_Previous_L2(temp_elem, temp); if (temp_elem_prev->data[0] == RO_SELECTION && temp_elem_prev->type == TYPE_OPERATOR) { if (step > 1) { tmp = temp_elem; while (tmp->type != TYPE_OPERAND) { tmp = tmp->next; } Ak_InsertAfter_L2(temp_elem->type, temp_elem->data, temp_elem->size, &tmp, &temp); Ak_InsertAfter_L2(temp_elem_prev->type, temp_elem_prev->data, temp_elem_prev->size, &tmp, &temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted with attributes (%s) in temp list\n", temp_elem_prev->data, temp_elem->data); } break; } } else { break; } temp_elem = (struct list_node *) Ak_Previous_L2(temp_elem, temp); } Ak_InsertAtEnd_L3(list_elem->type, list_elem->data, list_elem->size, temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted in temp list\n", list_elem->data); break; case RO_NAT_JOIN: Ak_InsertAtEnd_L3(list_elem->type, list_elem->data, list_elem->size, temp); Ak_InsertAtEnd_L3(list_elem_next->type, list_elem_next->data, list_elem_next->size, temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted in temp list\n", list_elem->data); list_elem = list_elem->next; break; //Commutativity of Selection and Theta join (or Cartesian product) case RO_THETA_JOIN: step = -1; while (temp_elem != NULL) { if (temp_elem->type == TYPE_OPERAND || temp_elem->type == TYPE_CONDITION) { step++; temp_elem_prev = (struct list_node *) Ak_Previous_L2(temp_elem, temp); if (temp_elem_prev->data[0] == RO_SELECTION && temp_elem_prev->type == TYPE_OPERATOR) { if (step > 1) { tmp = temp_elem; temp_elem_next = temp_elem->next; char *data1, *data2; char *cond_attr1, *cond_attr2; char op_selected[2]; memcpy(op_selected, temp_elem_prev->data, 2); data1 = AK_rel_eq_commute_with_theta_join(temp_elem->data, temp_elem_next->data); cond_attr1 = AK_rel_eq_cond_attributes(data1); data2 = AK_rel_eq_commute_with_theta_join(temp_elem->data, (temp_elem_next->next)->data); cond_attr2 = AK_rel_eq_cond_attributes(data2); //Debug lines - can be removed later //printf("CONDITION DATA : data: (%s),(%s), cond: (%s),(%s)\n", data1, data2, cond_attr1, cond_attr2); //printf("SHARE ATTRIBUTE: (%i)\n", AK_rel_eq_share_attributes(cond_attr1, cond_attr2)); if (AK_rel_eq_share_attributes(cond_attr1, cond_attr2)) { if (cond_attr1 != NULL) { //memset(temp_elem->data, '\0', MAX_VARCHAR_LENGHT); temp_elem->size = strlen(data1) + 1; memcpy(temp_elem->data, data1, temp_elem->size); memset(temp_elem->data + temp_elem->size, '\0', MAX_VARCHAR_LENGTH - temp_elem->size); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted with attributes (%s) in temp list\n", temp_elem_prev->data, temp_elem->data); } else { struct list_node *temp_elem_prevprev = (struct list_node *) Ak_Previous_L2(temp_elem_prev, temp); temp_elem_prevprev->next = temp_elem; AK_free(temp_elem_prev); struct list_node *temp_elem_prev = temp_elem_prevprev; temp_elem_prev->next = temp_elem_next; AK_free(temp_elem); struct list_node *temp_elem = temp_elem_next; temp_elem_next = temp_elem->next; tmp = temp_elem; } while (tmp->type != TYPE_OPERAND) { tmp = tmp->next; } if (cond_attr2 != NULL) { memset(data2 + strlen(data2), '\0', 1); Ak_InsertAfter_L2(temp_elem->type, data2, strlen(data2) + 1, &tmp, &temp); Ak_InsertAfter_L2(TYPE_OPERATOR, op_selected, 2, &tmp, &temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted with attributes (%s) in temp list\n", op_selected, data2); } } AK_free(data1); AK_free(data2); AK_free(cond_attr1); AK_free(cond_attr2); break; } } } else { break; } temp_elem = (struct list_node *) Ak_Previous_L2(temp_elem, temp); } Ak_InsertAtEnd_L3(list_elem->type, list_elem->data, list_elem->size, temp); Ak_InsertAtEnd_L3(list_elem_next->type, list_elem_next->data, list_elem_next->size, temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted with condition (%s) in temp list\n", list_elem->data, list_elem_next->data); list_elem = list_elem->next; break; case RO_RENAME: Ak_InsertAtEnd_L3(list_elem->type, list_elem->data, list_elem->size, temp); Ak_dbg_messg(MIDDLE, REL_EQ, "::operator %s inserted in temp list\n", list_elem->data); break; default: Ak_dbg_messg(LOW, REL_EQ, "Invalid operator: %s", list_elem->data); break; } break; //additional type definition included to distinguish beetween table name and attribute/s case TYPE_ATTRIBS: //printf("::attribute '%s' inserted in the temp list\n", list_elem->data); break; //additional type definition included to distinguish beetween attribute/s and condition case TYPE_CONDITION: //printf("::condition '%s' inserted in the temp list\n", list_elem->data); break; case TYPE_OPERAND: Ak_dbg_messg(MIDDLE, REL_EQ, "::table_name (%s) inserted in the temp list\n", list_elem->data); Ak_InsertAtEnd_L3(TYPE_OPERAND, list_elem->data, list_elem->size, temp); break; default: Ak_dbg_messg(LOW, REL_EQ, "Invalid type: %s", list_elem->data); break; } list_elem = list_elem->next; } //====================================> IMPROVMENTS <======================================= //Recursive RA optimization (need to implement exit condition in place of each operator, ...) //If there is no new changes on the list return generated struct list_nodes //int iter_cond; //for (iter_cond = 0; iter_cond < sizeof(exit_cond); iter_cond++) { // if (exit_cond[iter_cond] == 0) { //// Edit function to return collection of the struct list_nodes //// Generate next RA expr. (new plan) //// temp += remain from the list_rel_eq // AK_rel_eq_selection(temp); // } //} Ak_DeleteAll_L3(&list_rel_eq); AK_EPI; return temp; }
/** * @author Matija Šestak, modified for indexes by Lovro Predovan * @brief Function for printing index table * @param *tblName table name * @return No return value */ void AK_print_index_table(char *indexTblName) { AK_PRO; table_addresses *addresses = (table_addresses*) AK_get_index_addresses(indexTblName); char *index_table = "AK_index"; if ((addresses->address_from[0] == 0) || (AK_index_table_exist(indexTblName) == 0)) { printf("Table %s does not exist!\n", indexTblName); AK_EPI; } else { AK_header *head = AK_get_index_header(indexTblName); int i, j, k, l; int num_attr = AK_num_index_attr(indexTblName); int num_rows = AK_get_index_num_records(indexTblName); int len[num_attr]; //max length for each attribute in row int length = 0; //length of spacer clock_t t; //printf("BROJ ATTR %i ; BROJ REDOVA %i U TABLICI %s",num_attr,num_rows,indexTblName); //store lengths of header attributes for (i = 0; i < num_attr; i++) { len[i] = strlen((head + i)->att_name); } //for each header attribute iterate through all table rows and check if //there is longer element than previously longest and store it in array for (i = 0; i < num_attr; i++) { for (j = 0; j < num_rows; j++) { struct list_node *el = AK_get_index_tuple(j, i, indexTblName); switch (el->type) { case TYPE_INT: length = AK_chars_num_from_number(*((int *) (el)->data), 10); if (len[i] < length) { len[i] = length; } break; case TYPE_FLOAT: length = AK_chars_num_from_number(*((float *) (el)->data), 10); if (len[i] < length) { len[i] = length; } break; case TYPE_VARCHAR: default: if (len[i] < el->size) { len[i] = el->size; } break; } } } //num_attr is number of char | + space in printf //set offset to change the box size length = 0; for (i = 0; i < num_attr; length += len[i++]); length += num_attr * TBL_BOX_OFFSET + 2 * num_attr + 1; //start measuring time t = clock(); if (num_attr <= 0 || num_rows <= 0) { printf("Table is empty.\n"); AK_EPI; } else { AK_print_row_spacer(len, length); printf("\n|"); //HEADER for (i = 0; i < num_attr; i++) { //PRINTING HEADER ELEMENTS CENTERED k = (len[i] - (int) strlen((head + i)->att_name) + TBL_BOX_OFFSET + 1); if (k % 2 == 0) { k /= 2; printf("%-*s%-*s|", k, " ", k + (int) strlen((head + i)->att_name), (head + i)->att_name); } else { k /= 2; printf("%-*s%-*s|", k, " ", k + (int) strlen((head + i)->att_name) + 1, (head + i)->att_name); } } printf("\n"); AK_print_row_spacer(len, length); //END HEADER struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root); int type, size, address,last_addres_value; i = 0; if(addresses->address_to[i] != 0){ last_addres_value = addresses->address_to[i]; } while (addresses->address_from[i] != 0) { for (j = addresses->address_from[i]; j < addresses->address_to[i]; j++) { AK_mem_block *temp = (AK_mem_block*) AK_get_block(j); //THIS IS KINDA HACK, THIS PROBABLY SHOULD BE CHANGED SO MAIN INDEX TABLE ARE NOT INCLUDED if ((temp->block->last_tuple_dict_id == 0) && (last_addres_value == addresses->address_to[i])){ printf("\nIndex: %s\n", indexTblName); //PRINTING OUT TIME STATS t = clock() - t; if ((((double) t) / CLOCKS_PER_SEC) < 0.1) { printf("%i rows found, duration: %f μs\n", num_rows, ((double) t) / CLOCKS_PER_SEC * 1000); } else { printf("%i rows found, duration: %f s\n", num_rows, ((double) t) / CLOCKS_PER_SEC); } AK_free(row_root); AK_free(addresses); AK_EPI; //break; return 1; } //PRINTING VALUES IN INDEX TABLE for (k = 0; k < DATA_BLOCK_SIZE; k += num_attr) { if (temp->block->tuple_dict[k].size > 0) { for (l = 0; l < num_attr; l++) { type = temp->block->tuple_dict[k + l].type; size = temp->block->tuple_dict[k + l].size; address = temp->block->tuple_dict[k + l].address; Ak_InsertAtEnd_L3(type, &temp->block->data[address], size, row_root); } AK_print_row(len, row_root); AK_print_row_spacer(len, length); Ak_DeleteAll_L3(&row_root); } } } i++; } AK_free(row_root); } } AK_free(addresses); AK_EPI; }
//int AK_selection(char *srcTable, char *dstTable, AK_list *expr) { int AK_selection(char *srcTable, char *dstTable, struct list_node *expr) { AK_PRO; AK_header *t_header = (AK_header *) AK_get_header(srcTable); int num_attr = AK_num_attr(srcTable); int startAddress = AK_initialize_new_segment(dstTable, SEGMENT_TYPE_TABLE, t_header); if (startAddress == EXIT_ERROR) { AK_EPI; return EXIT_ERROR; } Ak_dbg_messg(LOW, REL_OP, "\nTABLE %s CREATED from %s!\n", dstTable, srcTable); table_addresses *src_addr = (table_addresses*) AK_get_table_addresses(srcTable); /* AK_list_elem row_root = (AK_list_elem) AK_malloc(sizeof (AK_list)); Ak_Init_L3(&row_root); */ struct list_node * row_root = (struct list_node *) AK_malloc(sizeof(struct list_node)); Ak_Init_L3(&row_root); int i, j, k, l, type, size, address; char data[MAX_VARCHAR_LENGTH]; for (i = 0; src_addr->address_from[i] != 0; i++) { for (j = src_addr->address_from[i]; j < src_addr->address_to[i]; j++) { AK_mem_block *temp = (AK_mem_block *) AK_get_block(j); if (temp->block->last_tuple_dict_id == 0) break; for (k = 0; k < DATA_BLOCK_SIZE; k += num_attr) { if (temp->block->tuple_dict[k].type == FREE_INT) break; for (l = 0; l < num_attr; l++) { type = temp->block->tuple_dict[k + l].type; size = temp->block->tuple_dict[k + l].size; address = temp->block->tuple_dict[k + l].address; memcpy(data, &(temp->block->data[address]), size); data[size] = '\0'; Ak_Insert_New_Element(type, data, dstTable, t_header[l].att_name, row_root); } if (AK_check_if_row_satisfies_expression(row_root, expr)) Ak_insert_row(row_root); Ak_DeleteAll_L3(&row_root); } } } AK_free(src_addr); AK_free(t_header); AK_free(row_root); AK_print_table(dstTable); Ak_dbg_messg(LOW, REL_OP, "SELECTION_TEST_SUCCESS\n\n"); AK_EPI; return EXIT_SUCCESS; }
/** * @author Dino Laktašić * @brief Function for creating test tables * @return No return value */ void AK_create_test_tables() { int mbr, year, id_prof, id_department; float weight; //---------------------------------------> CREATE TABLE 'STUDENT' <--------------------------------------- //create header AK_header t_header[ MAX_ATTRIBUTES ]; AK_header* temp; AK_PRO; temp = (AK_header*) AK_create_header("mbr", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("firstname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header + 1, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("lastname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header + 2, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("year", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header + 3, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("weight", TYPE_FLOAT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header + 4, temp, sizeof ( AK_header)); AK_free(temp); memset(t_header + 5, 0, MAX_ATTRIBUTES - 5); //create table char *tblName = "student"; int startAddress = AK_initialize_new_segment(tblName, SEGMENT_TYPE_TABLE, t_header); if (startAddress != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName); struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root); mbr = 35890; year = 1999; weight = 80.00; //insert rows in table student mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Dino", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Laktasic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Netko", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Netkic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Mislav", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Cakaric", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivan", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Horvat", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivo", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Marko", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Markovic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivan", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivanovic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Josip", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Josipovic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivan", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ankovic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Marina", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Marovic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Mario", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Maric", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Matija", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Matkovic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivana", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Ivic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "John", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Smith", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "William", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Brown", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "David", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Jones", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Robert", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "White", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "James", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Jones", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Jack", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Moore", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Joseph", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Harris", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Richard", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Thomas", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Daniel", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Jackson", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Martin", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Clark", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Joe", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Davis", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Paul", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Lee", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); mbr++; year++; weight += 0.75; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Steve", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Parker", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); Ak_insert_row(row_root); AK_print_table(tblName); Ak_DeleteAll_L3(&row_root); //------------------------------------------------------------------------------------------------------- //--------------------------------------> CREATE TABLE 'PROFESSOR' <------------------------------------- //create header AK_header t_header2[ MAX_ATTRIBUTES ]; temp = (AK_header*) AK_create_header("id_prof", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("firstname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 1, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("lastname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 2, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("tel", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 3, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("email", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 4, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("web_page", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 5, temp, sizeof ( AK_header)); AK_free(temp); memset(t_header2 + 6, 0, MAX_ATTRIBUTES - 6); //create table tblName = "professor"; startAddress = AK_initialize_new_segment(tblName, SEGMENT_TYPE_TABLE, t_header2); if (startAddress != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName); //row_root = (element) AK_malloc(sizeof (list)); Ak_Init_L3(&row_root); id_prof = 35890; id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Miroslav", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Baca", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390873", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/baca.miroslav/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Igor", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Balaban", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "000000000", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/balaban.igor/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Antun", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Brumnic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/brumnic.antun/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Mirko", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Cubrilo", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/cubrilo.mirko/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Dragutin", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Kermek", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/kermek.dragutin/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Tonimir", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Kisasondi", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/kisasondi.tonimir/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Alen", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Lovrencic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390866", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/lovrencic.alen/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Markus", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Schatten", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390892", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/schatten.markus/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Neven", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Vrcek", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390892", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/vrcek.neven/", tblName, "web_page", row_root); Ak_insert_row(row_root); AK_print_table(tblName); //------------------------------------------------------------------------------------------------------- //--------------------------------------> CREATE TABLE 'PROFESSOR2' <------------------------------------- //create header temp = (AK_header*) AK_create_header("id_prof", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("firstname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 1, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("lastname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 2, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("tel", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 3, temp, sizeof ( AK_header)); AK_free(temp); memset(t_header2 + 4, 0, MAX_ATTRIBUTES - 4); //create table tblName = "professor2"; startAddress = AK_initialize_new_segment(tblName, SEGMENT_TYPE_TABLE, t_header2); if (startAddress != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName); //row_root = (element) AK_malloc(sizeof (list)); Ak_DeleteAll_L3(&row_root); Ak_Init_L3(&row_root); id_prof = 35890; id_prof++; //Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Miroslav", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Baca", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390873", tblName, "tel", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Igor", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Balaban", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "000000000", tblName, "tel", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Antun", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Brumnic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Mirko", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Cubrilo", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Dragutin", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Kermek", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Tonimir", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Kisasondi", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Alen", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Lovrencic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390866", tblName, "tel", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Markus", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Schatten", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390892", tblName, "tel", row_root); Ak_insert_row(row_root); id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Neven", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Vrcek", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390892", tblName, "tel", row_root); Ak_insert_row(row_root); AK_print_table(tblName); Ak_DeleteAll_L3(&row_root); //------------------------------------------------------------------------------------------------------- //--------------------------------------> CREATE TABLE 'ASSISTANT' <------------------------------------- //create table, same header as professor for intersect test AK_header t_header3[ MAX_ATTRIBUTES ]; temp = (AK_header*) AK_create_header("id_prof", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header3, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("firstname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header3 + 1, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("lastname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header3 + 2, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("tel", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header3 + 3, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("email", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header3 + 4, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("web_page", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header3 + 5, temp, sizeof ( AK_header)); AK_free(temp); memset(t_header3 + 6, 0, MAX_ATTRIBUTES - 6); tblName = "assistant"; startAddress = AK_initialize_new_segment(tblName, SEGMENT_TYPE_TABLE, t_header3); if (startAddress != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName); //row_root = (element) AK_malloc(sizeof (list)); Ak_Init_L3(&row_root); id_prof = 35892; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Igor", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Balaban", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "000000000", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/balaban.igor/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof = 35896; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Tonimir", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Kisasondi", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042213777", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/kisasondi.tonimir/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof = 35898; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Markus", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Schatten", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390892", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/schatten.markus/", tblName, "web_page", row_root); Ak_insert_row(row_root); id_prof = 35899; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Miran", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Zlatović", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, "042390858", tblName, "tel", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "*****@*****.**", tblName, "email", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "www.foi.hr/nastavnici/zlatovic.miran/index.html", tblName, "web_page", row_root); Ak_insert_row(row_root); AK_print_table(tblName); //------------------------------------------------------------------------------------------------------- //--------------------------------------> CREATE TABLE 'EMPLOYEE' <-------------------------------------- //create header AK_header t_header4[ MAX_ATTRIBUTES ]; temp = (AK_header*) AK_create_header("id_prof", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header4, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("id_department", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header4 + 1, temp, sizeof ( AK_header)); AK_free(temp); memset(t_header4 + 2, 0, MAX_ATTRIBUTES - 2); //create table tblName = "employee"; startAddress = AK_initialize_new_segment(tblName, SEGMENT_TYPE_TABLE, t_header4); if (startAddress != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName); //row_root = (element) AK_malloc(sizeof (list)); Ak_DeleteAll_L3(&row_root); Ak_Init_L3(&row_root); id_prof = 35890; id_department = 1; id_prof++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_insert_row(row_root); id_prof++; id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_insert_row(row_root); id_prof++; id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_insert_row(row_root); id_prof++; id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_insert_row(row_root); id_prof++; id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_insert_row(row_root); id_prof++; id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_insert_row(row_root); id_prof++; id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_insert_row(row_root); //don't need id_prof++ here id_prof++; //id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_prof, tblName, "id_prof", row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_insert_row(row_root); AK_print_table(tblName); //------------------------------------------------------------------------------------------------------- //-------------------------------------> CREATE TABLE 'DEPARTMENT' <------------------------------------- //create header AK_header t_header5[ MAX_ATTRIBUTES ]; temp = (AK_header*) AK_create_header("id_department", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header5, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("dep_name", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header5 + 1, temp, sizeof ( AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("manager", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header5 + 2, temp, sizeof ( AK_header)); AK_free(temp); memset(t_header5 + 3, 0, MAX_ATTRIBUTES - 3); //create table tblName = "department"; startAddress = AK_initialize_new_segment(tblName, SEGMENT_TYPE_TABLE, t_header5); if (startAddress != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName); /* not needed - row_root is still allocated AK_free(row_root); row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); */ Ak_DeleteAll_L3(&row_root); Ak_Init_L3(&row_root); id_department = 1; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Department of Economics", tblName, "dep_name", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Redep", tblName, "manager", row_root); Ak_insert_row(row_root); //id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Department of Organization", tblName, "dep_name", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Zugaj", tblName, "manager", row_root); Ak_insert_row(row_root); id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Department of Quantitative Methods", tblName, "dep_name", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Kero", tblName, "manager", row_root); Ak_insert_row(row_root); /* too long id_department++; DeleteAllElements(row_root); InsertNewElement(TYPE_INT, &id_department, tblName, "id_department", row_root); InsertNewElement(TYPE_VARCHAR, "Department of theoretical and applied foundations of Information Science", tblName, "dep_name", row_root); InsertNewElement(TYPE_VARCHAR, "", tblName, "manager", row_root); insert_row(row_root);*/ //id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Department of Information Technology and Computing", tblName, "dep_name", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Hutinski", tblName, "manager", row_root); Ak_insert_row(row_root); id_department++; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &id_department, tblName, "id_department", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Department of Information Systems Development", tblName, "dep_name", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Vrcek", tblName, "manager", row_root); Ak_insert_row(row_root); /* too long id_department++; DeleteAllElements(row_root); InsertNewElement(TYPE_INT, &id_department, tblName, "id_department", row_root); InsertNewElement(TYPE_VARCHAR, "Department of foreign languages and general education discipline", tblName, "dep_name", row_root); InsertNewElement(TYPE_VARCHAR, "", tblName, "manager", row_root); insert_row(row_root);*/ AK_print_table(tblName); //------------------------------------------------------------------------------------------------------- //---------------------------------------> CREATE TABLE 'COURSE' <--------------------------------------- //create header AK_header t_header6[MAX_ATTRIBUTES]; temp = (AK_header*) AK_create_header("id_course", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header6, temp, sizeof (AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("name", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header6 + 1, temp, sizeof (AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("category", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header6 + 2, temp, sizeof (AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("lecturer", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header6 + 3, temp, sizeof (AK_header)); AK_free(temp); temp = (AK_header*) AK_create_header("active", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header6 + 4, temp, sizeof (AK_header)); AK_free(temp); memset(t_header6 + 5, '\0', MAX_ATTRIBUTES - 5); //create table tblName = "course"; startAddress = AK_initialize_new_segment(tblName, SEGMENT_TYPE_TABLE, t_header6); if (startAddress != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName); Ak_DeleteAll_L3(&row_root); AK_free(row_root); AK_EPI; }
/** * @author Dino Laktašić and Mislav Čakarić (replaced old print table function by new one) * @brief Function for printing table * @param *tblName table name * @return No return value */ void AK_print_table(char *tblName) { AK_PRO; table_addresses *addresses = (table_addresses*) AK_get_table_addresses(tblName); // || (AK_table_exist(tblName) == 0) if ((addresses->address_from[0] == 0) || (AK_table_exist(tblName) == 0)) { printf("Table %s does not exist!\n", tblName); AK_free(addresses); } else { AK_header *head = AK_get_header(tblName); int i, j, k, l; int num_attr = AK_num_attr(tblName); int num_rows = AK_get_num_records(tblName); int len[num_attr]; //max length for each attribute in row int length = 0; //length of spacer clock_t t; //store lengths of header attributes for (i = 0; i < num_attr; i++) { len[i] = strlen((head + i)->att_name); } //for each header attribute iterate through all table rows and check if //there is longer element than previously longest and store it in array for (i = 0; i < num_attr; i++) { for (j = 0; j < num_rows; j++) { struct list_node *el = AK_get_tuple(j, i, tblName); switch (el->type) { case TYPE_INT: length = AK_chars_num_from_number(*((int *) (el)->data), 10); if (len[i] < length) { len[i] = length; } break; case TYPE_FLOAT: length = AK_chars_num_from_number(*((float *) (el)->data), 10); if (len[i] < length) { len[i] = length; } break; case TYPE_VARCHAR: default: if (len[i] < el->size) { len[i] = el->size; } break; } //we don't need this linked list anymore (starting from tupple first to the end //see comment above in function AK_get_tuple - Elvis Popovic Ak_DeleteAll_L3(&el); AK_free(el); } } //num_attr is number of char | + space in printf //set offset to change the box size length = 0; for (i = 0; i < num_attr; length += len[i++]); length += num_attr * TBL_BOX_OFFSET + 2 * num_attr + 1; //start measuring time t = clock(); printf("Table: %s\n", tblName); if (num_attr <= 0 || num_rows <= 0) { printf("Table is empty.\n"); } else { //print table header AK_print_row_spacer(len, length); printf("\n|"); for (i = 0; i < num_attr; i++) { //print attributes center aligned inside box k = (len[i] - (int) strlen((head + i)->att_name) + TBL_BOX_OFFSET + 1); if (k % 2 == 0) { k /= 2; printf("%-*s%-*s|", k, " ", k + (int) strlen((head + i)->att_name), (head + i)->att_name); } else { k /= 2; printf("%-*s%-*s|", k, " ", k + (int) strlen((head + i)->att_name) + 1, (head + i)->att_name); } //print attributes left aligned inside box //printf(" %-*s|", len[i] + MAX_TABLE_BOX_OFFSET, (head + i)->att_name); } AK_free(head); printf("\n"); AK_print_row_spacer(len, length); struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root); i = 0; int type, size, address; while (addresses->address_from[i] != 0) { for (j = addresses->address_from[i]; j < addresses->address_to[i]; j++) { AK_mem_block *temp = (AK_mem_block*) AK_get_block(j); if (temp->block->last_tuple_dict_id == 0) break; for (k = 0; k < DATA_BLOCK_SIZE; k += num_attr) { if (temp->block->tuple_dict[k].size > 0 /*&& k / num_attr < num_rows*/) { for (l = 0; l < num_attr; l++) { type = temp->block->tuple_dict[k + l].type; size = temp->block->tuple_dict[k + l].size; address = temp->block->tuple_dict[k + l].address; Ak_InsertAtEnd_L3(type, &temp->block->data[address], size, row_root); } AK_print_row(len, row_root); AK_print_row_spacer(len, length); Ak_DeleteAll_L3(&row_root); } } } i++; } printf("\n"); t = clock() - t; if ((((double) t) / CLOCKS_PER_SEC) < 0.1) { printf("%i rows found, duration: %f μs\n", num_rows, ((double) t) / CLOCKS_PER_SEC * 1000); } else { printf("%i rows found, duration: %f s\n", num_rows, ((double) t) / CLOCKS_PER_SEC); } AK_free(row_root); } AK_free(addresses); } AK_EPI; }
/** * @author Dino Laktašić and Mislav Čakarić (replaced old print table function by new one) * update by Luka Rajcevic * @brief Function that prints a table * @param *tblName table name * @return No return value * update by Anto Tomaš (corrected the Ak_DeleteAll_L3 function) */ void AK_print_table_to_file(char *tblName) { char* FILEPATH = "table_test.txt"; FILE *fp; AK_PRO; fp = fopen(FILEPATH, "a"); table_addresses *addresses = (table_addresses*) AK_get_table_addresses(tblName); if (addresses->address_from[0] == 0) { fprintf(fp, "Table %s does not exist!\n", tblName); } else { AK_header *head = AK_get_header(tblName); int i, j, k, l; int num_attr = AK_num_attr(tblName); int num_rows = AK_get_num_records(tblName); int len[num_attr]; //max length for each attribute in row int length = 0; //length of spacer //store lengths of header attributes for (i = 0; i < num_attr; i++) { len[i] = strlen((head + i)->att_name); } //for each header attribute iterate through all table rows and check if //there is longer element than previously longest and store it in array for (i = 0; i < num_attr; i++) { for (j = 0; j < num_rows; j++) { struct list_node *el = AK_get_tuple(j, i, tblName); switch (el->type) { case TYPE_INT: length = AK_chars_num_from_number(*((int *) (el)->data), 10); if (len[i] < length) { len[i] = length; } break; case TYPE_FLOAT: length = AK_chars_num_from_number(*((float *) (el)->data), 10); if (len[i] < length) { len[i] = length; } break; case TYPE_VARCHAR: default: if (len[i] < el->size) { len[i] = el->size; } break; } } } //num_attr is number of char | + space in printf //set offset to change the box size length = 0; for (i = 0; i < num_attr; length += len[i++]); length += num_attr * TBL_BOX_OFFSET + 2 * num_attr + 1; fprintf(fp, "Table: %s\n", tblName); if (num_attr <= 0 || num_rows <= 0) { fprintf(fp, "Table is empty.\n"); } else { //print table header fclose(fp); AK_print_row_spacer_to_file(len, length); fp = fopen(FILEPATH, "a"); fprintf(fp, "\n|"); for (i = 0; i < num_attr; i++) { //print attributes center aligned inside box k = (len[i] - (int) strlen((head + i)->att_name) + TBL_BOX_OFFSET + 1); if (k % 2 == 0) { k /= 2; fprintf(fp, "%-*s%-*s|", k, " ", k + (int) strlen((head + i)->att_name), (head + i)->att_name); } else { k /= 2; fprintf(fp, "%-*s%-*s|", k, " ", k + (int) strlen((head + i)->att_name) + 1, (head + i)->att_name); } //print attributes left aligned inside box //printf(" %-*s|", len[i] + MAX_TABLE_BOX_OFFSET, (head + i)->att_name); } fprintf(fp, "\n"); fclose(fp); AK_print_row_spacer_to_file(len, length); struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root); i = 0; int type, size, address; while (addresses->address_from[i] != 0) { for (j = addresses->address_from[i]; j < addresses->address_to[i]; j++) { AK_mem_block *temp = (AK_mem_block*) AK_get_block(j); if (temp->block->last_tuple_dict_id == 0) break; for (k = 0; k < DATA_BLOCK_SIZE; k += num_attr) { if (temp->block->tuple_dict[k].size > 0) { for (l = 0; l < num_attr; l++) { type = temp->block->tuple_dict[k + l].type; size = temp->block->tuple_dict[k + l].size; address = temp->block->tuple_dict[k + l].address; Ak_InsertAtEnd_L3(type, &(temp->block->data[address]), size, row_root); } AK_print_row_to_file(len, row_root); AK_print_row_spacer_to_file(len, length); Ak_DeleteAll_L3(&row_root); } } } i++; } fp = fopen(FILEPATH, "a"); fprintf(fp, "\n"); } } fclose(fp); AK_EPI; }
/** * @author Dino Laktašić. * @brief Function for testing rel_eq_selection * @return No return value */ void AK_rel_eq_selection_test() { AK_PRO; printf("rel_eq_selection.c: Present!\n"); printf("\n********** REL_EQ_SELECTION TEST by Dino Laktašić **********\n"); //create header AK_header t_header[MAX_ATTRIBUTES]; AK_header *temp; temp = (AK_header*) AK_create_header("id", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header, temp, sizeof (AK_header)); temp = (AK_header*) AK_create_header("firstname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header + 1, temp, sizeof (AK_header)); temp = (AK_header*) AK_create_header("job", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header + 2, temp, sizeof (AK_header)); temp = (AK_header*) AK_create_header("year", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header + 3, temp, sizeof (AK_header)); temp = (AK_header*) AK_create_header("tezina", TYPE_FLOAT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header + 4, temp, sizeof (AK_header)); memset(t_header + 5, '\0', MAX_ATTRIBUTES - 5); //create table char *tblName = "profesor"; int startAddress = AK_initialize_new_segment(tblName, SEGMENT_TYPE_TABLE, t_header); if (startAddress != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName); printf("rel_eq_selection_test: After segment initialization: %d\n", AK_num_attr(tblName)); //create header AK_header t_header2[MAX_ATTRIBUTES]; AK_header *temp2; temp2 = (AK_header*) AK_create_header("mbr", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2, temp2, sizeof (AK_header)); temp2 = (AK_header*) AK_create_header("firstname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 1, temp2, sizeof (AK_header)); temp2 = (AK_header*) AK_create_header("lastname", TYPE_VARCHAR, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 2, temp2, sizeof (AK_header)); temp2 = (AK_header*) AK_create_header("year", TYPE_INT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 3, temp2, sizeof (AK_header)); temp2 = (AK_header*) AK_create_header("weight", TYPE_FLOAT, FREE_INT, FREE_CHAR, FREE_CHAR); memcpy(t_header2 + 4, temp2, sizeof (AK_header)); memset(t_header2 + 5, '\0', MAX_ATTRIBUTES - 5); //create table char *tblName2 = "student"; int startAddress2 = AK_initialize_new_segment(tblName2, SEGMENT_TYPE_TABLE, t_header2); if (startAddress2 != EXIT_ERROR) printf("\nTABLE %s CREATED!\n", tblName2); printf("rel_eq_selection_test: After segment initialization: %d\n", AK_num_attr(tblName2)); //----------------------------------------------------------------------------------------- //Init list and insert elements (Query parser output) struct list_node *expr = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&expr); //Commutativity of Selection and Projection Ak_InsertAtEnd_L3(TYPE_OPERATOR, "s", sizeof ("s"), expr); Ak_InsertAtEnd_L3(TYPE_CONDITION, "`L1` 100 >", sizeof ("`L1` 100 >"), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "p", sizeof ("p"), expr); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "L1;L2;L3;L4", sizeof ("L1;L2;L3;L4"), expr); //projection attribute Ak_InsertAtEnd_L3(TYPE_OPERATOR, "p", sizeof ("p"), expr); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "L1;L4;L3;L2;L5", sizeof ("L1;L4;L3;L2;L5"), expr); //Cascade of Selection and Commutativity of Selection Ak_InsertAtEnd_L3(TYPE_OPERATOR, "s", sizeof ("s"), expr); Ak_InsertAtEnd_L3(TYPE_CONDITION, "`L1` 100 >", sizeof ("`L1` 100 >"), expr); // //Commutativity of Selection and set operations (Union, Intersection, and Set difference) Ak_InsertAtEnd_L3(TYPE_OPERATOR, "s", sizeof ("s"), expr); Ak_InsertAtEnd_L3(TYPE_CONDITION, "`L2` 100 > `L3` 50 < OR", sizeof ("`L2` 100 > `L3` 50 < OR"), expr); Ak_InsertAtEnd_L3(TYPE_OPERAND, "R", sizeof ("R"), expr); Ak_InsertAtEnd_L3(TYPE_OPERAND, "S", sizeof ("S"), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "u", sizeof ("u"), expr); //u, i, e //Commutativity of Selection and Theta join (or Cartesian product) Ak_InsertAtEnd_L3(TYPE_OPERATOR, "s", sizeof ("s"), expr); Ak_InsertAtEnd_L3(TYPE_CONDITION, "`job` 'teacher' = `mbr` 50 < AND", sizeof ("`job` 'teacher' = `mbr` 50 < AND"), expr); Ak_InsertAtEnd_L3(TYPE_OPERAND, "student", sizeof ("student"), expr); Ak_InsertAtEnd_L3(TYPE_OPERAND, "profesor", sizeof ("profesor"), expr); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "t", sizeof ("t"), expr); Ak_InsertAtEnd_L3(TYPE_CONDITION, "`mbr` 50 = `job` 'teacher' = AND", sizeof ("`mbr` 50 = `job` 'teacher' = AND"), expr); //theta join attribute //printf("\nRA expr. before rel_eq optimization:\n"); //AK_print_rel_eq_projection(expr); AK_print_rel_eq_selection(AK_rel_eq_selection(expr)); if (DEBUG_ALL) { printf("\n------------------> TEST_SELECTION_FUNCTIONS <------------------\n\n"); char *test_cond1, *test_cond2; char *test_table; char *cond_attr1, *cond_attr2; test_table = "profesor"; test_cond1 = "`mbr` 100 > `firstname` 50 < AND `id` 'A' > OR"; test_cond2 = "`id` 100 > `firstname` 50 < AND `job` 'teacher' = AND"; cond_attr1 = AK_rel_eq_cond_attributes(test_cond1); cond_attr2 = AK_rel_eq_cond_attributes(test_cond2); printf("IS_SET_SUBSET_OF_LARGER_SET_TEST: (%i)\n\n", AK_rel_eq_is_attr_subset(cond_attr1, cond_attr2)); printf("GET_ALL_TABLE_ATTRIBUTES_TEST : (%s)\n\n", AK_rel_eq_get_atrributes_char(test_table)); printf("GET_CONDITION_ATTRIBUTES_TEST : (%s)\n\n", AK_rel_eq_cond_attributes(test_cond1)); printf("COMMUTE_WITH_THETA_JOIN_TEST : (%s)\n\n", AK_rel_eq_commute_with_theta_join(test_cond1, test_table)); printf("CONDITION_SHARE_ATTRIBUTES_TEST : (%i)\n", AK_rel_eq_share_attributes(cond_attr1, cond_attr2)); /**/ } else { printf("...\n"); } Ak_DeleteAll_L3(&expr); //dealocate variables ;) AK_EPI; }
/** * @author Unknown, updated by Tomislav Ilisevic * @brief Function for testing commands * */ void AK_test_command(){ AK_PRO; printf("***Test Command***\n"); int brojkomandi = 4; command *komande = AK_malloc(sizeof(command)*(brojkomandi+1)); // Test za komandu INSERT struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); Ak_Init_L3(&row_root); char *tblName = "student"; int mbr, year; float weight; mbr = 35917; year = 2012; weight = 82.00; Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element(TYPE_INT, &mbr, tblName, "mbr", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Mario", tblName, "firstname", row_root); Ak_Insert_New_Element(TYPE_VARCHAR, "Kolmacic", tblName, "lastname", row_root); Ak_Insert_New_Element(TYPE_INT, &year, tblName, "year", row_root); Ak_Insert_New_Element(TYPE_FLOAT, &weight, tblName, "weight", row_root); komande[0].id_command = INSERT; komande[0].tblName = "student"; komande[0].parameters = row_root; // Test za komandu UPDATE mbr = 35900; row_root = (struct list_node *) AK_malloc(sizeof (struct list_node )); Ak_Init_L3(&row_root); Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element_For_Update(TYPE_INT, &mbr, tblName, "mbr", row_root, 1); Ak_Insert_New_Element_For_Update(TYPE_VARCHAR, "FOI", tblName, "firstname", row_root, 0); komande[1].id_command = UPDATE; komande[1].tblName = "student"; komande[1].parameters = row_root; // Test za komandu DELETE int id_prof; id_prof = 35893; row_root = (struct list_node *) AK_malloc(sizeof (struct list_node )); Ak_Init_L3(&row_root); Ak_DeleteAll_L3(&row_root); Ak_Insert_New_Element_For_Update(TYPE_INT, &id_prof, tblName, "id_prof", row_root, 1); Ak_Insert_New_Element_For_Update(TYPE_VARCHAR, "FOI", tblName, "firstname", row_root, 0); komande[2].id_command = DELETE; komande[2].tblName = "professor"; komande[2].parameters = row_root; // Test za komandu SELECT row_root = (struct list_node *) AK_malloc(sizeof (struct list_node )); Ak_Init_L3(&row_root); Ak_DeleteAll_L3(&row_root); // printf("\nQUERY: SELECT * FROM student WHERE firstname = 'Robert';\n\n"); Ak_InsertAtEnd_L3(TYPE_ATTRIBS, "firstname", sizeof ("firstname"), row_root); Ak_InsertAtEnd_L3(TYPE_VARCHAR, "Robert", sizeof ("Robert"), row_root); Ak_InsertAtEnd_L3(TYPE_OPERATOR, "=", sizeof ("="), row_root); komande[3].id_command = SELECT; komande[3].tblName = "student"; komande[3].parameters = row_root; // Izvrsi komande AK_command(komande, brojkomandi); Ak_DeleteAll_L3(&row_root); AK_EPI; }
/** * @author Dino Laktašić * @brief Function that produces a difference of the two tables. Table addresses are get through names of tables. * Specially start addresses are taken from them. They are used to allocate blocks for them. It is checked whether the tables have same table schemas. If not, it returns EXIT_ERROR. New segment for result of difference operation is initialized. Function compares every block in extent of the first table with every block in extent of second table. If there is a difference between their rows, they are put in dstTable. * @param srcTable1 name of the first table * @param srcTable2 name of the second table * @param dstTable name of the new table * @return if success returns EXIT_SUCCESS, else returns EXIT_ERROR */ int AK_difference(char *srcTable1, char *srcTable2, char *dstTable) { AK_PRO; table_addresses *src_addr1 = (table_addresses*) AK_get_table_addresses(srcTable1); table_addresses *src_addr2 = (table_addresses*) AK_get_table_addresses(srcTable2); int startAddress1 = src_addr1->address_from[0]; int startAddress2 = src_addr2->address_from[0]; if ((startAddress1 != 0) && (startAddress2 != 0)) { register int i, j, k, l, m, n, o; i = j = k = l = 0; AK_mem_block *tbl1_temp_block = (AK_mem_block *) AK_get_block(startAddress1); AK_mem_block *tbl2_temp_block = (AK_mem_block *) AK_get_block(startAddress2); int num_att = AK_check_tables_scheme(tbl1_temp_block, tbl2_temp_block, "Difference"); if (num_att == EXIT_ERROR) { AK_EPI; return EXIT_ERROR; } int address, type, size; int different, num_rows, temp_int,summ; different = num_rows = temp_int = summ = 0; float temp_float = 0; char data1[MAX_VARCHAR_LENGTH]; char data2[MAX_VARCHAR_LENGTH]; //initialize new segment AK_header *header = (AK_header *) AK_malloc(num_att * sizeof (AK_header)); memcpy(header, tbl1_temp_block->block->header, num_att * sizeof (AK_header)); AK_initialize_new_segment(dstTable, SEGMENT_TYPE_TABLE, header); AK_free(header); struct list_node * row_root = (struct list_node *) AK_malloc(sizeof(struct list_node)); for (i = 0; src_addr1->address_from[i] != 0; i++) { startAddress1 = src_addr1->address_from[i]; //BLOCK: for each block in table1 extent for (j = startAddress1; j < src_addr1->address_to[i]; j++) { tbl1_temp_block = (AK_mem_block *) AK_get_block(j); //read block from first table //if there is data in the block if (tbl1_temp_block->block->AK_free_space != 0) { //TABLE2: for each extent in table2 for (k = 0; k < (src_addr2->address_from[k] != 0); k++) { startAddress2 = src_addr2->address_from[k]; if (startAddress2 != 0) { //BLOCK: for each block in table2 extent for (l = startAddress2; l < src_addr2->address_to[k]; l++) { tbl2_temp_block = (AK_mem_block *) AK_get_block(l); //if there is data in the block if (tbl2_temp_block->block->AK_free_space != 0) { //TUPLE_DICTS: for each tuple_dict in the block for (m = 0; m < DATA_BLOCK_SIZE; m += num_att) { if (tbl1_temp_block->block->tuple_dict[m + 1].type == FREE_INT) break; //TUPLE_DICTS: for each tuple_dict in the block for (n = 0; n < DATA_BLOCK_SIZE; n += num_att) { if (tbl2_temp_block->block->tuple_dict[n + 1].type == FREE_INT) break; //for each element in row for (o = 0; o < num_att; o++) { address = tbl1_temp_block->block->tuple_dict[m + o].address; size = tbl1_temp_block->block->tuple_dict[m + o].size; type = tbl1_temp_block->block->tuple_dict[m + o].type; switch (type) { case TYPE_INT: memcpy(&temp_int, &(tbl1_temp_block->block->data[address]), size); sprintf(data1, "%d", temp_int); break; case TYPE_FLOAT: memcpy(&temp_float, &(tbl1_temp_block->block->data[address]), size); sprintf(data1, "%f", temp_float); break; case TYPE_VARCHAR: default: memset(data1, '\0', MAX_VARCHAR_LENGTH); memcpy(data1, &(tbl1_temp_block->block->data[address]), size); } address = tbl2_temp_block->block->tuple_dict[n + o].address; size = tbl2_temp_block->block->tuple_dict[n + o].size; type = tbl2_temp_block->block->tuple_dict[n + o].type; switch (type) { case TYPE_INT: memcpy(&temp_int, &(tbl2_temp_block->block->data[address]), size); sprintf(data2, "%d", temp_int); break; case TYPE_FLOAT: memcpy(&temp_float, &(tbl2_temp_block->block->data[address]), size); sprintf(data2, "%f", temp_float); break; case TYPE_VARCHAR: default: memset(data2, '\0', MAX_VARCHAR_LENGTH); memcpy(data2, &(tbl2_temp_block->block->data[address]), size); } //if they are the same if(strcmp(data1,data2)==0){ different++; } if(different==(num_att-1)) summ=1; } //if same rows are found don't keep searching if(summ==1)break; } //if there is a difference between tuple_dicts if (summ == 0) { Ak_DeleteAll_L3(&row_root); for (o = 0; o < num_att; o++) { address = tbl1_temp_block->block->tuple_dict[m + o].address; size = tbl1_temp_block->block->tuple_dict[m + o].size; type = tbl1_temp_block->block->tuple_dict[m + o].type; memset(data1, '\0', MAX_VARCHAR_LENGTH); memcpy(data1, tbl1_temp_block->block->data + address, size); Ak_Insert_New_Element(type, data1, dstTable, tbl1_temp_block->block->header[o].att_name, row_root); } Ak_insert_row(row_root); } num_rows = different = summ = 0; } } } } else break; } } } } AK_free(src_addr1); AK_free(src_addr2); Ak_DeleteAll_L3(&row_root); AK_free(row_root); Ak_dbg_messg(LOW, REL_OP, "DIFFERENCE_TEST_SUCCESS\n\n"); AK_EPI; return EXIT_SUCCESS; } else { Ak_dbg_messg(LOW, REL_OP, "\nAK_difference: Table/s doesn't exist!"); AK_free(src_addr1); AK_free(src_addr2); AK_EPI; return EXIT_ERROR; } AK_EPI; }
/** * @author Dino Laktašić * @brief Function to make union of the two tables. Union is implemented for working with multiple sets of data, i.e. duplicate * tuples can be written in same table (union) * @param srcTable1 name of the first table * @param srcTable2 name of the second table * @param dstTable name of the new table * @return if success returns EXIT_SUCCESS, else returns EXIT_ERROR */ int AK_union(char *srcTable1, char *srcTable2, char *dstTable) { AK_PRO; table_addresses *src_addr1 = (table_addresses*) AK_get_table_addresses(srcTable1); table_addresses *src_addr2 = (table_addresses*) AK_get_table_addresses(srcTable2); int startAddress1 = src_addr1->address_from[0]; int startAddress2 = src_addr2->address_from[0]; if ((startAddress1 != 0) && (startAddress2 != 0)) { register int i, j, k; i = j = k = 0; AK_mem_block *tbl1_temp_block = (AK_mem_block *) AK_get_block(startAddress1); AK_mem_block *tbl2_temp_block = (AK_mem_block *) AK_get_block(startAddress2); int num_att = AK_check_tables_scheme(tbl1_temp_block, tbl2_temp_block, "Union"); int address, type, size; char data[MAX_VARCHAR_LENGTH]; //initialize new segment AK_header *header = (AK_header *) AK_malloc(num_att * sizeof (AK_header)); memcpy(header, tbl1_temp_block->block->header, num_att * sizeof (AK_header)); AK_initialize_new_segment(dstTable, SEGMENT_TYPE_TABLE, header); AK_free(header); //AK_list *row_root = (AK_list *) AK_malloc(sizeof (AK_list)); struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node)); //writing first block or table to new segment for (i = 0; src_addr1->address_from[i] != 0; i++) { startAddress1 = src_addr1->address_from[i]; //BLOCK: for each block in table1 extent for (j = startAddress1; j < src_addr1->address_to[i]; j++) { tbl1_temp_block = (AK_mem_block *) AK_get_block(j); //read block from first table //if there is data in the block if (tbl1_temp_block->block->AK_free_space != 0) { for (k = 0; k < DATA_BLOCK_SIZE; k++) { if (tbl1_temp_block->block->tuple_dict[k].type == FREE_INT) break; address = tbl1_temp_block->block->tuple_dict[k].address; size = tbl1_temp_block->block->tuple_dict[k].size; type = tbl1_temp_block->block->tuple_dict[k].type; memset(data, '\0', MAX_VARCHAR_LENGTH); memcpy(data, tbl1_temp_block->block->data + address, size); Ak_Insert_New_Element_For_Update(type, data, dstTable, tbl1_temp_block->block->header[k % num_att].att_name, row_root, 0); if ((k + 1) % num_att == 0 && k != 0) { Ak_insert_row(row_root); Ak_DeleteAll_L3(&row_root); } } } } } //writing first block or table to new segment for (i = 0; src_addr2->address_from[i] != 0; i++) { startAddress2 = src_addr2->address_from[i]; //BLOCK: for each block in table2 extent for (j = startAddress2; j < src_addr2->address_to[i]; j++) { tbl2_temp_block = (AK_mem_block *) AK_get_block(j); //read block from second table //if there is data in the block if (tbl2_temp_block->block->AK_free_space != 0) { for (k = 0; k < DATA_BLOCK_SIZE; k++) { if (tbl2_temp_block->block->tuple_dict[k].type == FREE_INT) break; address = tbl2_temp_block->block->tuple_dict[k].address; size = tbl2_temp_block->block->tuple_dict[k].size; type = tbl2_temp_block->block->tuple_dict[k].type; memset(data, '\0', MAX_VARCHAR_LENGTH); memcpy(data, tbl2_temp_block->block->data + address, size); Ak_Insert_New_Element_For_Update(type, data, dstTable, tbl2_temp_block->block->header[k % num_att].att_name, row_root, 0); if ((k + 1) % num_att == 0) { Ak_insert_row(row_root); Ak_DeleteAll_L3(&row_root); } } } } } AK_free(src_addr1); AK_free(src_addr2); Ak_dbg_messg(LOW, REL_OP, "UNION_TEST_SUCCESS\n\n"); AK_EPI; return EXIT_SUCCESS; } else { Ak_dbg_messg(LOW, REL_OP, "\nAK_union: Table/s doesn't exist!"); AK_free(src_addr1); AK_free(src_addr2); AK_EPI; return EXIT_ERROR; } AK_EPI; }
/** * @author unknown, Jurica Hlevnjak - fix bugs and reorganize code in this function * @brief Help function for the drop command. Delete memory blocks and addresses of table * and removes table or index from system table. * @param tblName name of table or index * @param sys_table name of system catalog table */ void AK_drop_help_function(char *tblName, char *sys_table) { table_addresses *addresses; AK_PRO; addresses = (table_addresses*) AK_get_table_addresses(tblName); AK_mem_block *mem_block; int from = 0, to = 0, i = 0, j = 0, c; for (j = 0; j < MAX_EXTENTS_IN_SEGMENT; j++) { if (addresses->address_from != 0) { from = addresses->address_from[j]; to = addresses->address_to[j]; if (from == 0 || to == 0) break; for (i = from; i <= to; i++) { mem_block = (AK_mem_block *) AK_get_block(i); mem_block->block->type = BLOCK_TYPE_FREE; for (c = 0; c < DATA_BLOCK_SIZE; c++) { mem_block->block->tuple_dict[c].type = FREE_INT; mem_block->block->data[c] = FREE_CHAR; } } } else break; } int data_adr = 0; int data_size = 0; //int data_type = 0; int address_sys; char name_sys[MAX_ATT_NAME]; AK_mem_block *mem_block2 = (AK_mem_block *) AK_get_block(0); for (i = 0; i < DATA_BLOCK_SIZE; i++) { memset(name_sys, 0, MAX_ATT_NAME); if (mem_block2->block->tuple_dict[i].address == FREE_INT) { break; } data_adr = mem_block2->block->tuple_dict[i].address; data_size = mem_block2->block->tuple_dict[i].size; memcpy(name_sys, mem_block2->block->data + data_adr, data_size); i++; data_adr = mem_block2->block->tuple_dict[i].address; data_size = mem_block2->block->tuple_dict[i].size; memcpy(&address_sys, mem_block2->block->data + data_adr, data_size); if (strcmp(name_sys, sys_table) == 0) { break; } } mem_block2 = (AK_mem_block *) AK_get_block(address_sys); table_addresses *addresses2; addresses2 = (table_addresses*) AK_get_table_addresses(tblName); for (i = 0; i < MAX_EXTENTS_IN_SEGMENT; i++) { addresses2->address_from[i] = 0; addresses2->address_to[i] = 0; } char name[MAX_VARCHAR_LENGTH]; for (i = 0; i < DATA_BLOCK_SIZE; i++) { if (mem_block2->block->tuple_dict[i].type == FREE_INT) break; i++; memcpy(name, &(mem_block2->block->data[mem_block2->block->tuple_dict[i].address]), mem_block2->block->tuple_dict[i].size); name[ mem_block2->block->tuple_dict[i].size] = '\0'; if (strcmp(name, tblName) == 0) { i++; mem_block2->block->data[mem_block2->block->tuple_dict[i].address] = 0; i++; mem_block2->block->data[mem_block2->block->tuple_dict[i].address] = 0; } } struct list_node *row_root = (struct list_node *) AK_malloc(sizeof (struct list_node )); Ak_Init_L3(&row_root); Ak_DeleteAll_L3(&row_root); Ak_Update_Existing_Element(TYPE_VARCHAR, tblName, sys_table, "name", row_root); Ak_delete_row(row_root); AK_free(addresses); AK_free(addresses2); AK_EPI; }