usnistgov/h5wasm

I cant createDataSet in a array

Closed this issue · 7 comments

export async function json_to_hdf5(json: string): Promise<H5WasmFile> {
    const empty = await createEmptyHdf5();
    empty.create_dataset({
        name: JSON_TAG,
        data: json,
    });
    return empty;
}

const a = new Array(10).fill(10);
a.forEach(async () => {
    await json_to_hdf5('xxxxxx');
});

img_v2_6f3a24e4-7775-4f82-9e54-00be23fa86eh

Can you paste example code that includes all the imports and functions you're using?

import { File as H5WasmFile, ready as h5wasmReady } from 'h5wasm';
import { nanoid } from 'nanoid';
import h5wasm from 'h5wasm';
export const JSON_TAG = 'JSON_STRING';
 
export const createEmptyHdf5 = async (): Promise<H5WasmFile> => {
    await h5wasm.ready;
    const filename = nanoid() + '.hdf5';
    const newHdf5 = new H5WasmFile(filename, 'w');
    return newHdf5;
};

export async function json_to_hdf5(json: string): Promise<H5WasmFile> {
    const empty = await createEmptyHdf5();
    empty.create_dataset({
        name: JSON_TAG,
        data: json,
    });
    return empty;
}

const a = new Array(10).fill(10);
a.forEach(async () => {
    await json_to_hdf5('xxxxxx');
});

Under what circumstances will this error occur?

This was being caused by writing a lot of vlen string datasets quickly, which exposed a memory error in the wasm code - a memory-freeing function (H5Treclaim) was being called on memory that was allocated by a vector in an earlier function, and this should not have been done. H5Treclaim is needed for memory allocated by HDF5 directly when reading vlen datasets, but is not needed on write (since we are directly allocating the memory for the data to be written)

This is fixed in v0.6.1, published to npm just now. Your test code now completes without error.

FYI - if you want to use fixed-length strings you can use e.g. dtype='S10' in create_dataset. They might have better performance, and when combined with chunks can be compressed as well.

thanks, i will try v0.6..1

when i use dtype='S10', a long json string will be truncated

image
can you give me a api to get a hdf5file is closed?

    const newHdf5 = new H5WasmFile(filename, 'w');
    if(newHdf5.isClosed()) {
        return
    } else {
        newHdf5.close()
       console.log('ff')
    }

There are a couple ways this could be done -

  • a simple property of the javascript object that gets set to true on close(),
  • checking the output of H5Iis_valid(file_id)

Can you open a new issue for this feature request, and close this one if you feel like your original problem has been resolved?