// Read string attribute <attr_name> given by address <path> char AH5_read_str_attr(hid_t loc_id, const char *path, char *attr_name, char **rdata) { hid_t attr_id, filetype, memtype; size_t sdim; char success = AH5_FALSE; if (AH5_path_valid(loc_id, path) || strcmp(path, ".") == 0) if (H5Aexists_by_name(loc_id, path, attr_name, H5P_DEFAULT) > 0) { attr_id = H5Aopen_by_name(loc_id, path, attr_name, H5P_DEFAULT, H5P_DEFAULT); filetype = H5Aget_type(attr_id); sdim = H5Tget_size(filetype); sdim++; // make a space for null terminator *rdata = (char *) malloc(sdim * sizeof(char)); memtype = H5Tcopy(H5T_C_S1); H5Tset_size(memtype, sdim); if (H5Aread(attr_id, memtype, *rdata) >= 0) success = AH5_TRUE; else free(*rdata); H5Tclose(memtype); H5Tclose(filetype); H5Aclose(attr_id); } if (!success) *rdata = NULL; return success; }
// read a complex float attribute float_complex read_complex_attribute(hid_t loc_id, const char* path, const char* name) { float_complex value; float *buf; hid_t attr_id, real_type_id; ; hsize_t *dims; herr_t status; status = H5Aexists_by_name(loc_id, path, name, H5P_DEFAULT); if (status < 0) { printf("attribut %s does not exist \n", name); } real_type_id = create_real_type_id(); attr_id = H5Aopen_by_name(loc_id, path, name, H5P_DEFAULT, H5P_DEFAULT); status = H5Aread(attr_id, real_type_id, buf); if (status < 0) { printf("Can't read attribute : %s\n", name); } value.re = buf[0]; value.im=buf[1]; return value; }
// Read integer attribute <attr_name> given by address <path> char AH5_read_int_attr(hid_t loc_id, const char *path, char *attr, int *rdata) { char success = AH5_FALSE; if (AH5_path_valid(loc_id, path)) if (H5Aexists_by_name(loc_id, path, attr, H5P_DEFAULT) > 0) if (H5LTget_attribute_int(loc_id, path, attr, rdata) >= 0) success = AH5_TRUE; if(!success) *rdata = 0; return success; }
/* * Class: hdf_hdf5lib_H5 * Method: H5Aexists_by_name * Signature: (JLjava/lang/String;Ljava/lang/String;J)Z */ JNIEXPORT jboolean JNICALL Java_hdf_hdf5lib_H5_H5Aexists_1by_1name (JNIEnv *env, jclass clss, jlong loc_id, jstring obj_name, jstring attr_name, jlong lapl_id) { htri_t bval = JNI_FALSE; const char *aName; const char *attrName; PIN_JAVA_STRING_TWO(obj_name, aName, attr_name, attrName); if (aName != NULL && attrName != NULL) { bval = H5Aexists_by_name((hid_t)loc_id, aName, attrName, (hid_t)lapl_id); UNPIN_JAVA_STRING_TWO(obj_name, aName, attr_name, attrName); if (bval > 0) bval = JNI_TRUE; else if (bval < 0) h5libraryError(env); } return (jboolean)bval; } /* end Java_hdf_hdf5lib_H5_H5Aexists_1by_1name */
// Read complex attribute <attr_name> given by address <path> char AH5_read_cpx_attr(hid_t loc_id, const char* path, char* attr_name, AH5_complex_t *rdata) { hid_t attr_id, type_id; float buf[2]; char success = AH5_FALSE; if (AH5_path_valid(loc_id, path)) if (H5Aexists_by_name(loc_id, path, attr_name, H5P_DEFAULT) > 0) { attr_id = H5Aopen_by_name(loc_id, path, attr_name, H5P_DEFAULT, H5P_DEFAULT); type_id = AH5_H5Tcreate_cpx_memtype(); if (H5Aread(attr_id, type_id, buf) >= 0) { *rdata = AH5_set_complex(buf[0], buf[1]); success = AH5_TRUE; } H5Tclose(type_id); H5Aclose(attr_id); } if (!success) *rdata = AH5_set_complex(0, 0); return success; }
// Read all optional attributes char AH5_read_opt_attrs(hid_t loc_id, const char *path, AH5_opt_attrs_t *opt_attrs, char mandatory_attrs[][AH5_ATTR_LENGTH], size_t nb_mandatory_attrs) { char success = AH5_FALSE, is_mandatory; H5O_info_t object_info; hsize_t i, j, k = 0; hid_t attr_id, type_id, memtype; float buf[2]; hsize_t nb_present_mandatory_attrs = 0; char temp_name[AH5_ATTR_LENGTH]; if (AH5_path_valid(loc_id, path)) { // Check presence of all mandatory attributes for (i = 0; i < (hsize_t) nb_mandatory_attrs; i++) if (H5Aexists_by_name(loc_id, path, mandatory_attrs[i], H5P_DEFAULT) > 0) nb_present_mandatory_attrs++; H5Oget_info_by_name(loc_id, path, &object_info, H5P_DEFAULT); opt_attrs->nb_instances = object_info.num_attrs - nb_present_mandatory_attrs; if (opt_attrs->nb_instances > 0) opt_attrs->instances = (AH5_attr_instance_t *) malloc ((size_t) opt_attrs->nb_instances * sizeof(AH5_attr_instance_t)); for (i = 0; i < object_info.num_attrs; i++) { is_mandatory = AH5_FALSE; attr_id = H5Aopen_by_idx(loc_id, path, H5_INDEX_CRT_ORDER, H5_ITER_NATIVE, i, H5P_DEFAULT, H5P_DEFAULT); H5Aget_name(attr_id, AH5_ATTR_LENGTH, temp_name); for (j = 0; j < nb_mandatory_attrs; j++) if (strcmp(temp_name, mandatory_attrs[j]) == 0) is_mandatory = AH5_TRUE; if (!is_mandatory) { opt_attrs->instances[k].name = strdup(temp_name); type_id = H5Aget_type(attr_id); opt_attrs->instances[k].type = H5Tget_class(type_id); H5Tclose(type_id); switch (opt_attrs->instances[k].type) { case H5T_INTEGER: opt_attrs->instances[k].value.i = 0; if (H5Aread(attr_id, H5T_NATIVE_INT, &(opt_attrs->instances[k].value.i)) >= 0) success = AH5_TRUE; break; case H5T_FLOAT: opt_attrs->instances[k].value.f = 0; if (H5Aread(attr_id, H5T_NATIVE_FLOAT, &(opt_attrs->instances[k].value.f)) >= 0) success = AH5_TRUE; break; case H5T_COMPOUND: opt_attrs->instances[k].value.c = AH5_set_complex(0, 0); type_id = AH5_H5Tcreate_cpx_memtype(); if (H5Aread(attr_id, type_id, buf) >= 0) { opt_attrs->instances[k].value.c = AH5_set_complex(buf[0], buf[1]); success = AH5_TRUE; } H5Tclose(type_id); break; case H5T_STRING: opt_attrs->instances[k].value.s = NULL; memtype = H5Tcopy(H5T_C_S1); H5Tset_size(memtype, AH5_ATTR_LENGTH); opt_attrs->instances[k].value.s = (char *) malloc(AH5_ATTR_LENGTH * sizeof(char)); if (H5Aread(attr_id, memtype, opt_attrs->instances[k].value.s) >= 0) success = AH5_TRUE; H5Tclose(memtype); break; default: opt_attrs->instances[k].type = H5T_NO_CLASS; printf("***** WARNING: Unsupported type of attribute \"%s@%s\". *****\n\n", path, opt_attrs->instances[k].name); break; } k++; } H5Aclose(attr_id); } } if (!success) { opt_attrs->instances = NULL; opt_attrs->nb_instances = 0; } return success; }
/*------------------------------------------------------------------------- * Function: read_records * * Purpose: For a given dataset, checks to make sure that the stated * and actual sizes are the same. If they are not, then * we have an inconsistent dataset due to a SWMR error. * * Parameters: const char *filename * The SWMR test file's name. * * unsigned verbose * Whether verbose console output is desired. * * unsigned long nrecords * The total number of records to read. * * unsigned poll_time * The amount of time to sleep (s). * * unsigned reopen_count * * * Return: Success: 0 * Failure: -1 * *------------------------------------------------------------------------- */ static int read_records(const char *filename, unsigned verbose, unsigned long nrecords, unsigned poll_time, unsigned reopen_count) { hid_t fid; /* File ID */ hid_t aid; /* Attribute ID */ time_t start_time; /* Starting time */ hid_t mem_sid; /* Memory dataspace ID */ symbol_t record; /* The record to add to the dataset */ unsigned seed; /* Seed for random number generator */ unsigned iter_to_reopen = reopen_count; /* # of iterations until reopen */ unsigned long u; /* Local index variable */ hid_t fapl; HDassert(filename); HDassert(poll_time != 0); /* Create file access property list */ if((fapl = h5_fileaccess()) < 0) return -1; H5Pset_fclose_degree(fapl, H5F_CLOSE_SEMI); /* Emit informational message */ if(verbose) HDfprintf(stderr, "Opening file: %s\n", filename); /* Open the file */ if((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; /* Seed the random number generator with the attribute in the file */ if((aid = H5Aopen(fid, "seed", H5P_DEFAULT)) < 0) return -1; if(H5Aread(aid, H5T_NATIVE_UINT, &seed) < 0) return -1; if(H5Aclose(aid) < 0) return -1; HDsrandom(seed); /* Reset the record */ /* (record's 'info' field might need to change for each record written, also) */ HDmemset(&record, 0, sizeof(record)); /* Create a dataspace for the record to read */ if((mem_sid = H5Screate(H5S_SCALAR)) < 0) return -1; /* Emit informational message */ if(verbose) HDfprintf(stderr, "Reading records\n"); /* Get the starting time */ start_time = HDtime(NULL); /* Read records */ for(u = 0; u < nrecords; u++) { symbol_info_t *symbol = NULL; /* Symbol (dataset) */ htri_t attr_exists; /* Whether the sequence number attribute exists */ unsigned long file_u; /* Attribute sequence number (writer's "u") */ /* Get a random dataset, according to the symbol distribution */ symbol = choose_dataset(); /* Fill in "nrecords" field. Note that this depends on the writer * using the same algorithm and "nrecords" */ symbol->nrecords = nrecords / 5; /* Wait until we can read the dataset */ do { /* Check if sequence attribute exists */ if((attr_exists = H5Aexists_by_name(fid, symbol->name, "seq", H5P_DEFAULT)) < 0) return -1; if(attr_exists) { /* Read sequence number attribute */ if((aid = H5Aopen_by_name(fid, symbol->name, "seq", H5P_DEFAULT, H5P_DEFAULT)) < 0) return -1; if(H5Aread(aid, H5T_NATIVE_ULONG, &file_u) < 0) return -1; if(H5Aclose(aid) < 0) return -1; /* Check if sequence number is at least u - if so, this should * guarantee that this record has been written */ if(file_u >= u) break; } /* end if */ /* Check for timeout */ if(HDtime(NULL) >= (time_t)(start_time + (time_t)TIMEOUT)) { HDfprintf(stderr, "Reader timed out\n"); return -1; } /* end if */ /* Pause */ HDsleep(poll_time); /* Retrieve and print the collection of metadata read retries */ if(print_metadata_retries_info(fid) < 0) HDfprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Reopen the file */ if(H5Fclose(fid) < 0) return -1; if((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; iter_to_reopen = reopen_count; } while(1); /* Emit informational message */ if(verbose) HDfprintf(stderr, "Checking dataset %lu\n", u); /* Check dataset */ if(check_dataset(fid, verbose, symbol, &record, mem_sid) < 0) return -1; HDmemset(&record, 0, sizeof(record)); /* Check for reopen */ iter_to_reopen--; if(iter_to_reopen == 0) { /* Emit informational message */ if(verbose) HDfprintf(stderr, "Reopening file: %s\n", filename); /* Retrieve and print the collection of metadata read retries */ if(print_metadata_retries_info(fid) < 0) HDfprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Reopen the file */ if(H5Fclose(fid) < 0) return -1; if((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; iter_to_reopen = reopen_count; } /* end if */ } /* end while */ /* Retrieve and print the collection of metadata read retries */ if(print_metadata_retries_info(fid) < 0) HDfprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Close file */ if(H5Fclose(fid) < 0) return -1; /* Close the memory dataspace */ if(H5Sclose(mem_sid) < 0) return -1; return 0; } /* end read_records() */