Skip to content

Commit 84d20fb

Browse files
author
mikejiang
committed
don't maintain H5 file handler #15
1 parent 9f89a17 commit 84d20fb

File tree

3 files changed

+20
-55
lines changed

3 files changed

+20
-55
lines changed

inst/include/cytolib/H5CytoFrame.hpp

+1-34
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,14 @@ namespace cytolib
2020
class H5CytoFrame:public CytoFrame{
2121
protected:
2222
string filename_;
23-
/*TODO: We may not want to maintain these handlers, instead treat each IO as atomic operations
24-
* Because it is not easy to accomplish the resource sharing among multiple H5CytoFrame objects solely depending on H5's mechanisms.
25-
* e.g. a second openFile call with H5F_ACC_RDONLY will NOT overwrite the previous H5F_ACC_RDWR , thus cause the unexpected data tampering
26-
* these H5 handlers remain open during the life cycle of H5CytoFrame
27-
* for faster accessing the data
28-
*/
29-
H5File file;
30-
DataSet dataset;
31-
DataSpace dataspace;
3223
hsize_t dims[2]; // dataset dimensions
3324
//flags indicating if cached meta data needs to be flushed to h5
3425
bool is_dirty_params;
3526
bool is_dirty_keys;
3627
bool is_dirty_pdata;
3728
EVENT_DATA_VEC read_data(uvec col_idx) const;
3829
public:
39-
~H5CytoFrame(){
40-
/*
41-
* catch the exception to prevent the destructor from throwing, which could crash the application
42-
*/
43-
// string msg = "Warning: failed to flush the meta data to h5!Changes to meta are unsaved.";
44-
//
45-
// try{
46-
// flush_meta();
47-
// }catch(const H5::DataSetIException &e){
48-
// PRINT(e.getDetailMsg() + "\n");
49-
// PRINT(msg);
50-
// }catch(...){
51-
// PRINT(msg);
52-
// }
53-
54-
};
55-
/*
56-
* for simplicity, we don't want to handle the object that has all the h5 handler closed
57-
* because it will require lots of validity checks before each disk IO operations
58-
*/
59-
// void close_h5(){
60-
// dataspace.close();
61-
// dataset.close();
62-
// file.close();
63-
// }
30+
const unsigned int default_flags = H5F_ACC_RDWR;
6431
void flush_meta();
6532
void flush_params();
6633

inst/utest/CytoFrame_accessors.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ BOOST_AUTO_TEST_CASE(flags)
110110
// }catch(H5::FileIException & ex){
111111
// cout << ex.getDetailMsg() << endl;
112112
// }
113-
BOOST_CHECK_EXCEPTION(fr3.write_h5(h5file);, H5::FileIException,
114-
[](const H5::FileIException & ex) {return ex.getDetailMsg().find("H5Fcreate failed") != string::npos;});
113+
// BOOST_CHECK_EXCEPTION(fr3.write_h5(h5file);, H5::FileIException,
114+
// [](const H5::FileIException & ex) {return ex.getDetailMsg().find("H5Fcreate failed") != string::npos;});
115115

116116
}
117117

src/H5CytoFrame.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ namespace cytolib
88
{
99
EVENT_DATA_VEC H5CytoFrame::read_data(uvec col_idx) const
1010
{
11+
H5File file(filename_, default_flags);
12+
auto dataset = file.openDataSet(DATASET_NAME);
13+
auto dataspace = dataset.getSpace();
14+
1115
unsigned nrow = n_rows();
1216
unsigned ncol = col_idx.size();
1317
/*
@@ -61,6 +65,8 @@ namespace cytolib
6165
}
6266
void H5CytoFrame::flush_params()
6367
{
68+
H5File file(filename_, default_flags);
69+
6470
CompType param_type = get_h5_datatype_params(DataTypeLocation::MEM);
6571
DataSet ds = file.openDataSet("params");
6672
hsize_t size[1] = {params.size()};
@@ -74,6 +80,7 @@ namespace cytolib
7480

7581
void H5CytoFrame::flush_keys()
7682
{
83+
H5File file(filename_, default_flags);
7784
CompType key_type = get_h5_datatype_keys();
7885
DataSet ds = file.openDataSet("keywords");
7986
auto keyVec = to_kw_vec<KEY_WORDS>(keys_);
@@ -87,6 +94,7 @@ namespace cytolib
8794
}
8895
void H5CytoFrame::flush_pheno_data()
8996
{
97+
H5File file(filename_, default_flags);
9098
CompType key_type = get_h5_datatype_keys();
9199
DataSet ds = file.openDataSet("pdata");
92100

@@ -103,9 +111,6 @@ namespace cytolib
103111
H5CytoFrame::H5CytoFrame(const H5CytoFrame & frm):CytoFrame(frm)
104112
{
105113
filename_ = frm.filename_;
106-
file = frm.file;//safe to copy due to refcount during copy constructor provided by h5
107-
dataset = frm.dataset;//safe to copy due to refcount during copy constructor provided by h5
108-
dataspace = frm.dataspace;//safe to copy due to explicit copy through its assignment operator provided by h5
109114
is_dirty_params = frm.is_dirty_params;
110115
is_dirty_keys = frm.is_dirty_keys;
111116
is_dirty_pdata = frm.is_dirty_pdata;
@@ -120,9 +125,6 @@ namespace cytolib
120125
// swap(channel_vs_idx, frm.channel_vs_idx);
121126
// swap(marker_vs_idx, frm.marker_vs_idx);
122127
swap(filename_, frm.filename_);
123-
swap(file, frm.file);
124-
swap(dataset, frm.dataset);
125-
swap(dataspace, frm.dataspace);
126128
swap(dims, frm.dims);
127129

128130
swap(is_dirty_params, frm.is_dirty_params);
@@ -133,9 +135,6 @@ namespace cytolib
133135
{
134136
CytoFrame::operator=(frm);
135137
filename_ = frm.filename_;
136-
file = frm.file;
137-
dataset = frm.dataset;
138-
dataspace = frm.dataspace;
139138
is_dirty_params = frm.is_dirty_params;
140139
is_dirty_keys = frm.is_dirty_keys;
141140
is_dirty_pdata = frm.is_dirty_pdata;
@@ -146,9 +145,6 @@ namespace cytolib
146145
{
147146
CytoFrame::operator=(frm);
148147
swap(filename_, frm.filename_);
149-
swap(file, frm.file);
150-
swap(dataset, frm.dataset);
151-
swap(dataspace, frm.dataspace);
152148
swap(dims, frm.dims);
153149
swap(is_dirty_params, frm.is_dirty_params);
154150
swap(is_dirty_keys, frm.is_dirty_keys);
@@ -174,24 +170,23 @@ namespace cytolib
174170
*/
175171
H5CytoFrame::H5CytoFrame(const string & h5_filename, bool readonly):CytoFrame(readonly),filename_(h5_filename), is_dirty_params(false), is_dirty_keys(false), is_dirty_pdata(false)
176172
{
177-
178-
179-
file.openFile(filename_, H5F_ACC_RDWR);//always use the same flag and keep lock at cf level to avoid h5 open error caused conflicting h5 flags among cf objects that points to the same h5
173+
//always use the same flag and keep lock at cf level to avoid h5 open error caused conflicting h5 flags among cf objects that points to the same h5
174+
H5File file(filename_, default_flags);
180175
load_meta();
181176

182177

183178
//open dataset for event data
184179

185-
dataset = file.openDataSet(DATASET_NAME);
186-
dataspace = dataset.getSpace();
180+
auto dataset = file.openDataSet(DATASET_NAME);
181+
auto dataspace = dataset.getSpace();
187182
dataspace.getSimpleExtentDims(dims);
188183

189184
}
190185
/**
191186
* abandon the changes to the meta data in cache by reloading them from disk
192187
*/
193188
void H5CytoFrame::load_meta(){
194-
189+
H5File file(filename_, default_flags);
195190
DataSet ds_param = file.openDataSet("params");
196191
// DataType param_type = ds_param.getDataType();
197192

@@ -392,11 +387,14 @@ namespace cytolib
392387
*/
393388
void H5CytoFrame::set_data(const EVENT_DATA_VEC & _data)
394389
{
390+
H5File file(filename_, default_flags);
395391
check_write_permission();
396392
hsize_t dims_data[2] = {_data.n_cols, _data.n_rows};
393+
auto dataset = file.openDataSet(DATASET_NAME);
394+
397395
dataset.extend(dims_data);
398396
//refresh data space and dims
399-
dataspace = dataset.getSpace();
397+
auto dataspace = dataset.getSpace();
400398
dataspace.getSimpleExtentDims(dims);
401399

402400
dataset.write(_data.mem, h5_datatype_data(DataTypeLocation::MEM));

0 commit comments

Comments
 (0)