void *test_metadb(void *arg) { int tid = pthread_self(); //Pthread_detach(tid); oph_metadb_db_row *db_table = (oph_metadb_db_row *)arg; pmesg(LOG_DEBUG,__FILE__,__LINE__,"Executing thread %d\n", tid); //Create DB record oph_metadb_db_row *test_row = NULL; oph_iostore_resource_id db_id; db_id.id_length = strlen("trial"); db_id.id = (void *)strndup("trial", strlen("trial")); if(oph_metadb_setup_db_struct ("trial", "Memory", 0, &db_id, 0, &test_row)) { printf("Unable to create DB record\n"); return(NULL); } //Add db record if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); oph_metadb_cleanup_db_struct (test_row); return(NULL); } if(oph_metadb_add_db (&db_table, test_row)){ pthread_mutex_unlock( &metadb_mutex ); printf("Unable to add data to MetaDB\n"); oph_metadb_cleanup_db_struct (test_row); return(NULL); } if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); oph_metadb_cleanup_db_struct (test_row); return(NULL); } oph_metadb_db_row *test_row1 = NULL; //Find DB record if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); oph_metadb_cleanup_db_struct (test_row); return(NULL); } if(oph_metadb_find_db (db_table, test_row->db_name, test_row->device, &test_row1) || test_row1 == NULL){ pthread_mutex_unlock( &metadb_mutex ); printf("Unable to add data to MetaDB\n"); oph_metadb_cleanup_db_struct (test_row); return(NULL); } if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); oph_metadb_cleanup_db_struct (test_row); return(NULL); } //Create frag record oph_metadb_frag_row *test_frag_row = NULL; oph_iostore_resource_id frag_id; frag_id.id_length = strlen("trial1"); frag_id.id = (void *)strndup("trial1", strlen("trial1")); if(oph_metadb_setup_frag_struct ("Fragment", "Memory", 0, &db_id, &frag_id, 100, &test_frag_row)) { printf("Unable to create Frag record\n"); return(NULL); } //Add frag record if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); oph_metadb_cleanup_db_struct (test_row); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } if(oph_metadb_add_frag (test_row1, test_frag_row)){ pthread_mutex_unlock( &metadb_mutex ); printf("Unable to add data to MetaDB\n"); oph_metadb_cleanup_db_struct (test_row); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); oph_metadb_cleanup_db_struct (test_row); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } //Update db record test_row->frag_number++; if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); oph_metadb_cleanup_db_struct (test_row); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } if(oph_metadb_update_db (db_table, test_row)){ pthread_mutex_unlock( &metadb_mutex ); printf("Unable to add data to MetaDB\n"); oph_metadb_cleanup_db_struct (test_row); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); oph_metadb_cleanup_db_struct (test_row); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } oph_metadb_cleanup_db_struct (test_row); test_row = NULL; //Display DB record if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } printf("%s %llu %llu\n", test_row1->db_name, test_row1->file_offset, test_row1->frag_number); if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } //Update Frag record test_frag_row->frag_size = 555; if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } if(oph_metadb_update_frag (test_row1, test_frag_row)){ pthread_mutex_unlock( &metadb_mutex ); printf("Unable to add data to MetaDB\n"); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } oph_metadb_frag_row *test_frag_row1 = NULL; //Find frag record if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } if(oph_metadb_find_frag (test_row1, test_frag_row->frag_name, &test_frag_row1)){ pthread_mutex_unlock( &metadb_mutex ); printf("Unable to find frag in MetaDB\n"); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); oph_metadb_cleanup_frag_struct (test_frag_row); return(NULL); } oph_metadb_cleanup_frag_struct (test_frag_row); test_frag_row = NULL; //Display frag record if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); return(NULL); } printf("%s %llu %llu\n", test_frag_row1->frag_name, test_frag_row1->file_offset, test_frag_row1->frag_size); if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); return(NULL); } //Remove frag record if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); return(NULL); } if(oph_metadb_remove_frag (test_row1, test_frag_row1->frag_name)){ pthread_mutex_unlock( &metadb_mutex ); printf("Unable to remove data from MetaDB\n"); return(NULL); } if(pthread_mutex_unlock(&metadb_mutex) != 0){ printf("Unable to unlock mutex\n"); return(NULL); } //Remove DB record if(pthread_mutex_lock(&metadb_mutex) != 0){ printf("Unable to lock mutex\n"); return(NULL); } if(oph_metadb_remove_db (&db_table, test_row1->db_name, test_row1->device)){ pthread_mutex_unlock( &metadb_mutex ); printf("Unable to remove data from MetaDB\n"); return(NULL); } pthread_mutex_unlock( &metadb_mutex ); return(NULL); }
int oph_metadb_update_frag (oph_metadb_db_row *db, oph_metadb_frag_row *frag) { if(!db || !frag->frag_name){ pmesg(LOG_ERROR,__FILE__,__LINE__, OPH_METADB_LOG_NULL_INPUT_PARAM); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_NULL_INPUT_PARAM); return OPH_METADB_NULL_ERR; } if(frag->db_ptr || frag->next_frag || frag->file_offset || frag->db_id.id || frag->frag_id.id) { pmesg(LOG_DEBUG,__FILE__,__LINE__,OPH_METADB_LOG_FRAG_RECORD_ZEROED_WARN); logging(LOG_DEBUG, __FILE__, __LINE__, OPH_METADB_LOG_FRAG_RECORD_ZEROED_WARN); /* frag->db_ptr = NULL; frag->next_frag = NULL; frag->file_offset = 0; frag->db_id = 0; frag->frag_id = 0;*/ } //Check if Frag name exists oph_metadb_frag_row *tmp_row = NULL; if(db->first_frag != NULL){ //If frag list is not empty find record if(oph_metadb_find_frag (db, frag->frag_name, &tmp_row)) { pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_FRAG_RECORD_UPDATE_NOT_FOUND,frag->frag_name); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_FRAG_RECORD_UPDATE_NOT_FOUND,frag->frag_name); return OPH_METADB_IO_ERR; } else { //Update file char *line = NULL; unsigned int length = 0; //Update meta_db tmp_row->frag_size = frag->frag_size; if(_oph_metadb_serialize_frag_row(tmp_row, &line, &length)) { pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_SERIAL_RECORD_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_SERIAL_RECORD_ERROR); return OPH_METADB_IO_ERR; } //Append row if(_oph_metadb_write_row(line, length, tmp_row->is_persistent, frag_file, tmp_row->file_offset, 0)) { pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_WRITE_RECORD_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_WRITE_RECORD_ERROR); free(line); return OPH_METADB_IO_ERR; } free(line); return OPH_METADB_OK; } } else{ pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_FRAG_RECORD_UPDATE_NOT_FOUND,frag->frag_name); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_FRAG_RECORD_UPDATE_NOT_FOUND,frag->frag_name); return OPH_METADB_IO_ERR; } return OPH_METADB_OK; }
//Function for CUBESIZE int oph_io_server_run_size_procedure(oph_metadb_db_row ** meta_db, oph_iostore_handler * dev_handle, oph_io_server_thread_status * thread_status, oph_query_arg ** args, HASHTBL * query_args) { if (!query_args || !thread_status || !meta_db) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_NULL_INPUT_PARAM); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_NULL_INPUT_PARAM); return OPH_IO_SERVER_NULL_PARAM; } //For future implementations UNUSED(dev_handle); UNUSED(args); //First delete last result set if (thread_status->last_result_set != NULL) { if (thread_status->delete_only_rs) oph_iostore_destroy_frag_recordset_only(&(thread_status->last_result_set)); else oph_iostore_destroy_frag_recordset(&(thread_status->last_result_set)); } thread_status->last_result_set = NULL; thread_status->delete_only_rs = 0; //Fetch function arguments char *function_args = hashtbl_get(query_args, OPH_QUERY_ENGINE_LANG_ARG_ARG); if (function_args == NULL) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MISSING_QUERY_ARGUMENT, OPH_QUERY_ENGINE_LANG_ARG_ARG); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MISSING_QUERY_ARGUMENT, OPH_QUERY_ENGINE_LANG_ARG_ARG); return OPH_IO_SERVER_EXEC_ERROR; } char **func_args_list = NULL; int func_args_num = 0; if (oph_query_parse_multivalue_arg(function_args, &func_args_list, &func_args_num)) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_MULTIVAL_PARSE_ERROR, OPH_QUERY_ENGINE_LANG_ARG_ARG); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_MULTIVAL_PARSE_ERROR, OPH_QUERY_ENGINE_LANG_ARG_ARG); return OPH_IO_SERVER_EXEC_ERROR; } //Check number of arguments if (func_args_num < 1) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_WRONG_PROCEDURE_ARG, OPH_IO_SERVER_PROCEDURE_SIZE); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_WRONG_PROCEDURE_ARG, OPH_IO_SERVER_PROCEDURE_SIZE); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } //TODO Arguments should be a string with optionally a hierarchical name (a.b) //Remove leading/trailing spaces and match string long long i = 0; for (i = 0; i < func_args_num; i++) { if (oph_query_check_procedure_string(&(func_args_list[i]))) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[0]); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_ARG_NO_STRING, func_args_list[0]); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } } oph_metadb_db_row *tmp_db = NULL; oph_metadb_frag_row *tmp_frag = NULL; unsigned long long tot_frag_size = 0; //LOCK FROM HERE if (pthread_rwlock_rdlock(&rwlock) != 0) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_LOCK_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_LOCK_ERROR); free(func_args_list); return OPH_IO_SERVER_EXEC_ERROR; } //Compute total size for (i = 0; i < func_args_num; i++) { //Look for fragments for (tmp_db = *meta_db; tmp_db != NULL; tmp_db = tmp_db->next_db) { //Find Frag from MetaDB if (oph_metadb_find_frag(tmp_db, func_args_list[i], &tmp_frag)) { pthread_rwlock_unlock(&rwlock); pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_METADB_ERROR, "Frag find"); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_METADB_ERROR, "Frag find"); free(func_args_list); return OPH_IO_SERVER_METADB_ERROR; } if (tmp_frag != NULL) { //Found fragment tot_frag_size += tmp_frag->frag_size; break; } } //Fragment not found if (tmp_db == NULL) { pthread_rwlock_unlock(&rwlock); free(func_args_list); pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_FRAG_NOT_EXIST_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_QUERY_FRAG_NOT_EXIST_ERROR); return OPH_IO_SERVER_EXEC_ERROR; } } //UNLOCK FROM HERE if (pthread_rwlock_unlock(&rwlock) != 0) { free(func_args_list); pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_UNLOCK_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_UNLOCK_ERROR); return OPH_IO_SERVER_EXEC_ERROR; } free(func_args_list); //Prepare output record set oph_iostore_frag_record_set *rs = NULL; if (oph_iostore_create_frag_recordset(&rs, 0, 1)) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); return OPH_IO_SERVER_MEMORY_ERROR; } rs->field_type[0] = OPH_IOSTORE_LONG_TYPE; rs->field_name[0] = (char *) strndup("frag_size", (strlen("frag_size") + 1) * sizeof(char)); if (rs->field_name[0] == NULL) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); oph_iostore_destroy_frag_recordset(&rs); return OPH_IO_SERVER_MEMORY_ERROR; } //No name required rs->frag_name = NULL; rs->record_set = (oph_iostore_frag_record **) calloc(1 + 1, sizeof(oph_iostore_frag_record *)); if (rs->record_set == NULL) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); oph_iostore_destroy_frag_recordset(&rs); return OPH_IO_SERVER_MEMORY_ERROR; } //Created record struct oph_iostore_frag_record *new_record = NULL; if (oph_iostore_create_frag_record(&new_record, 1) == 1) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); oph_iostore_destroy_frag_recordset(&rs); return OPH_IO_SERVER_MEMORY_ERROR; } //Add record to record set rs->record_set[0] = new_record; //Fill record new_record->field_length[0] = sizeof(long long); new_record->field[0] = (void *) memdup((const void *) &tot_frag_size, new_record->field_length[0]); if (new_record->field[0] == NULL) { pmesg(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_IO_SERVER_LOG_MEMORY_ALLOC_ERROR); oph_iostore_destroy_frag_recordset(&rs); return OPH_IO_SERVER_MEMORY_ERROR; } thread_status->last_result_set = rs; thread_status->delete_only_rs = 0; return OPH_IO_SERVER_SUCCESS; }
int oph_metadb_add_frag (oph_metadb_db_row *db, oph_metadb_frag_row *frag) { if(!db || !frag){ pmesg(LOG_ERROR,__FILE__,__LINE__, OPH_METADB_LOG_NULL_INPUT_PARAM); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_NULL_INPUT_PARAM); return OPH_METADB_NULL_ERR; } if(frag->db_ptr || frag->next_frag || db->file_offset) { pmesg(LOG_DEBUG,__FILE__,__LINE__,OPH_METADB_LOG_FRAG_RECORD_ZEROED_WARN); logging(LOG_DEBUG, __FILE__, __LINE__, OPH_METADB_LOG_FRAG_RECORD_ZEROED_WARN); /* db->first_frag = NULL; db->next_db = NULL; db->file_offset = 0;*/ } //Simple check to verify DB/Frag matching if(oph_iostore_compare_id(db->db_id, frag->db_id) == 1 || STRCMP(db->device, frag->device) == 1 || db->is_persistent != frag->is_persistent){ pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_FRAG_DB_ERROR); logging(LOG_ERROR, __FILE__, __LINE__,OPH_METADB_LOG_RECORD_COPY_ERROR); return OPH_METADB_IO_ERR; } oph_metadb_frag_row *frag_row = NULL; if(oph_metadb_setup_frag_struct (frag->frag_name, frag->device, frag->is_persistent, &(frag->db_id), &(frag->frag_id), frag->frag_size, &frag_row)) { pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_RECORD_COPY_ERROR); logging(LOG_ERROR, __FILE__, __LINE__,OPH_METADB_LOG_RECORD_COPY_ERROR); return OPH_METADB_DATA_ERR; } //Check if Frag name already exists in given DB oph_metadb_frag_row *tmp_row = NULL; if(db->first_frag != NULL){ //If frag list is not empty find record if(oph_metadb_find_frag (db, frag_row->frag_name, &tmp_row)) { pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_FRAG_RECORD_UPDATE_NOT_FOUND,frag_row->frag_name); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_FRAG_RECORD_UPDATE_NOT_FOUND,frag_row->frag_name); return OPH_METADB_IO_ERR; } if(tmp_row != NULL) { pmesg(LOG_DEBUG,__FILE__,__LINE__,OPH_METADB_LOG_FRAG_EXIST_ERROR); logging(LOG_DEBUG, __FILE__, __LINE__,OPH_METADB_LOG_FRAG_EXIST_ERROR); oph_metadb_cleanup_frag_struct (frag_row); return OPH_METADB_OK; } } //Count bytes in file unsigned long long byte_size = 0; if(_oph_metadb_count_bytes(frag_file, &byte_size)) { pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_FILE_SIZE_ERROR); logging(LOG_ERROR, __FILE__, __LINE__,OPH_METADB_LOG_FILE_SIZE_ERROR); oph_metadb_cleanup_frag_struct (frag_row); return OPH_METADB_IO_ERR; } //Insert in file char *line = NULL; unsigned int length = 0; if(_oph_metadb_serialize_frag_row(frag_row, &line, &length)) { pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_SERIAL_RECORD_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_SERIAL_RECORD_ERROR); oph_metadb_cleanup_frag_struct (frag_row); return OPH_METADB_IO_ERR; } //Append row if(_oph_metadb_write_row(line, length, frag_row->is_persistent, frag_file, 0, 1)) { pmesg(LOG_ERROR,__FILE__,__LINE__,OPH_METADB_LOG_WRITE_RECORD_ERROR); logging(LOG_ERROR, __FILE__, __LINE__, OPH_METADB_LOG_WRITE_RECORD_ERROR); free(line); return OPH_METADB_IO_ERR; } free(line); //Insert new Frag into stack frag_row->file_offset = byte_size; frag_row->next_frag = (struct oph_metadb_frag_row *)db->first_frag; frag_row->db_ptr = db; //Update db head pointer db->first_frag = (struct oph_metadb_frag_row *)frag_row; return OPH_METADB_OK; }