/** * @author Frane Jakelić updated by Ivan Pusic * @brief Function that is called in a separate thread that is responsible for acquiring locks, releasing them and finding the associated block addresses * @todo Check multithreading, check if it's working correctly * @param commandArray array filled with commands that need to be secured using transactions * @param lengthOfArray length of commandArray * @param transactionId associated with the transaction * @return ABORT or COMMIT based on the success of the function. */ int AK_execute_commands(command * commandArray, int lengthOfArray) { int i = 0, status = 0; AK_memoryAddresses addresses; AK_PRO; AK_memoryAddresses_link address = (AK_memoryAddresses_link) AK_malloc(sizeof(struct memoryAddresses)); for (i = 0; i < lengthOfArray; i++) { if (!AK_get_memory_blocks(commandArray[i].tblName, &addresses)) { printf("Error reading block Addresses. Aborting\n"); AK_EPI; return ABORT; }; address = &addresses; while (address->nextElement != NULL) { switch (commandArray[i].id_command) { case UPDATE: status = AK_acquire_lock(address->adresa, EXCLUSIVE_LOCK, pthread_self()); break; case DELETE: status = AK_acquire_lock(address->adresa, EXCLUSIVE_LOCK, pthread_self()); break; case INSERT: status = AK_acquire_lock(address->adresa, EXCLUSIVE_LOCK, pthread_self()); break; case SELECT: status = AK_acquire_lock(address->adresa, SHARED_LOCK, pthread_self()); break; default: break; } if (status == NOT_OK) { printf("Error acquiring lock. Aborting\n"); AK_release_locks(&addresses, pthread_self()); AK_EPI; return ABORT; } address = address->nextElement; } } if(AK_command(commandArray, lengthOfArray) == EXIT_ERROR){ AK_EPI; return ABORT; } AK_release_locks(&addresses, pthread_self()); AK_EPI; return COMMIT; }
/** * @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; }