From 898fe9fe59b869a8d7517ea9c2921a11a8a298a1 Mon Sep 17 00:00:00 2001 From: Brian Benjamin Maranville Date: Sat, 24 Aug 2024 13:27:04 -0400 Subject: [PATCH 1/2] adding method to read metadata for committed Datatype --- src/hdf5_hl.d.ts | 5 ++++- src/hdf5_hl.ts | 13 +++++++++++-- src/hdf5_util.cc | 19 +++++++++++++++++++ src/hdf5_util_helpers.d.ts | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/hdf5_hl.d.ts b/src/hdf5_hl.d.ts index 22b3852..a8771cb 100644 --- a/src/hdf5_hl.d.ts +++ b/src/hdf5_hl.d.ts @@ -51,8 +51,11 @@ export declare class ExternalLink { constructor(filename: string, obj_path: string); } export declare class Datatype { + file_id: bigint; + path: string; type: OBJECT_TYPE; - constructor(); + constructor(file_id: bigint, path: string); + get metadata(): Metadata; } export declare class Reference { ref_data: Uint8Array; diff --git a/src/hdf5_hl.ts b/src/hdf5_hl.ts index 7833f8e..9e1c4df 100644 --- a/src/hdf5_hl.ts +++ b/src/hdf5_hl.ts @@ -524,8 +524,17 @@ export class ExternalLink { } export class Datatype { + file_id: bigint; + path: string; type: OBJECT_TYPE = OBJECT_TYPE.DATATYPE - constructor() {} + constructor(file_id: bigint, path: string) { + this.file_id = file_id; + this.path = path; + } + + get metadata() { + return Module.get_datatype_metadata(this.file_id, this.path); + } } export class Reference { @@ -736,7 +745,7 @@ export class Group extends HasAttrs { return new ExternalLink(filename, obj_path); } else if (type === Module.H5G_TYPE) { - return new Datatype() + return new Datatype(this.file_id, fullpath); } // unknown type or object not found return null diff --git a/src/hdf5_util.cc b/src/hdf5_util.cc index b2aadff..751e04f 100644 --- a/src/hdf5_util.cc +++ b/src/hdf5_util.cc @@ -344,6 +344,24 @@ val get_dtype_metadata(hid_t dtype) return attr; } +val get_datatype_metadata(hid_t loc_id, const std::string& dtype_name_string) +{ + hid_t dtype_id; + herr_t status; + const char *dtype_name = dtype_name_string.c_str(); + + dtype_id = H5Topen2(loc_id, dtype_name, H5P_DEFAULT); + if (dtype_id < 0) + { + throw_error("error - name not defined!"); + return val::null(); + } + val metadata = get_dtype_metadata(dtype_id); + + H5Tclose(dtype_id); + return metadata; +} + val get_abstractDS_metadata(hid_t dspace, hid_t dtype, hid_t dcpl) { val attr = get_dtype_metadata(dtype); @@ -1282,6 +1300,7 @@ EMSCRIPTEN_BINDINGS(hdf5) function("get_attribute_names", &get_attribute_names); function("get_attribute_metadata", &get_attribute_metadata); function("get_dataset_metadata", &get_dataset_metadata); + function("get_datatype_metadata", &get_datatype_metadata); function("get_dataset_filters", &get_dataset_filters); function("refresh_dataset", &refresh_dataset); function("get_dataset_data", &get_dataset_data); diff --git a/src/hdf5_util_helpers.d.ts b/src/hdf5_util_helpers.d.ts index 5857432..2f6e9ba 100644 --- a/src/hdf5_util_helpers.d.ts +++ b/src/hdf5_util_helpers.d.ts @@ -98,6 +98,7 @@ export interface H5Module extends EmscriptenModule { refresh_dataset(file_id: bigint, path: string): number; resize_dataset(file_id: bigint, path: string, new_size: bigint[]): number; get_dataset_metadata(file_id: bigint, path: string): Metadata; + get_datatype_metadata(file_id: bigint, path: string): Metadata; get_dataset_filters(file_id: bigint, path: string): Filter[]; flush(file_id: bigint): number; ccall: typeof ccall; From 80b48528e588b994167fa879d4a40f5214f13b47 Mon Sep 17 00:00:00 2001 From: Brian Benjamin Maranville Date: Mon, 26 Aug 2024 10:21:13 -0400 Subject: [PATCH 2/2] add metadata check to datatype test --- test/datatype_test.mjs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/datatype_test.mjs b/test/datatype_test.mjs index cd90340..7e52344 100644 --- a/test/datatype_test.mjs +++ b/test/datatype_test.mjs @@ -7,7 +7,17 @@ async function datatype_test() { await h5wasm.ready; var f = new h5wasm.File('./test/array.h5', 'r'); - assert.deepEqual(f.get('datatype/value'), new h5wasm.Datatype()); + const datatype = f.get('datatype/value'); + assert(datatype instanceof h5wasm.Datatype); + assert.deepEqual(datatype.metadata, { + signed: false, + type: 3, + cset: 0, + strpad: 1, + vlen: false, + littleEndian: false, + size: 10 + }); } export const tests = [