1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Reading API: File, Dataset, and Group handles for reading HDF5 files.
use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom};
use crate::attribute::extract_attributes_full;
use crate::chunk_cache::ChunkCache;
use crate::data_layout::DataLayout;
use crate::data_read;
use crate::dataspace::Dataspace;
use crate::datatype::Datatype;
use crate::error::{Error, FormatError};
use crate::filter_pipeline::FilterPipeline;
use crate::group_v1::GroupEntry;
use crate::group_v2;
use crate::message_type::MessageType;
use crate::object_header::ObjectHeader;
use crate::signature;
use crate::superblock::Superblock;
use crate::types::{AttrValue, DType, attrs_to_map, classify_datatype};
// ---------------------------------------------------------------------------
// File
// ---------------------------------------------------------------------------
/// An open HDF5 file for reading.
pub struct File {
data: Vec<u8>,
superblock: Superblock,
/// Byte offset to add to all relative addresses (= original base_address).
addr_offset: u64,
/// Live file handle, retained only when the file was opened with
/// [`File::open_swmr`] so [`File::refresh`] can re-read appended data.
handle: Option<std::fs::File>,
}
impl File {
/// Open an HDF5 file from a filesystem path.
///
/// Reads the file into memory once. To follow a file that a concurrent
/// single writer is appending to (SWMR), use [`File::open_swmr`] instead.
pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self, Error> {
let bytes = std::fs::read(path.as_ref()).map_err(Error::Io)?;
Self::from_bytes(bytes)
}
/// Open an HDF5 file for SWMR (single-writer/multiple-reader) reading.
///
/// Like [`File::open`], but retains a live handle to the file so that
/// [`File::refresh`] can re-read data appended by a concurrent writer
/// (whether produced by this crate's append writer, the reference HDF5 C
/// library, or h5py in SWMR mode). The initial view is a consistent
/// snapshot; call [`File::refresh`] to advance to a newer one.
///
/// Only the `std` build supports this (it requires a live filesystem
/// handle); the in-memory [`File::from_bytes`] path cannot refresh.
pub fn open_swmr<P: AsRef<std::path::Path>>(path: P) -> Result<Self, Error> {
let mut handle = std::fs::File::open(path.as_ref()).map_err(Error::Io)?;
let mut data = Vec::new();
handle.read_to_end(&mut data).map_err(Error::Io)?;
let (superblock, addr_offset) = Self::parse_superblock(&data)?;
Ok(Self {
data,
superblock,
addr_offset,
handle: Some(handle),
})
}
/// Open an HDF5 file from an in-memory byte vector.
pub fn from_bytes(data: Vec<u8>) -> Result<Self, Error> {
let (superblock, addr_offset) = Self::parse_superblock(&data)?;
Ok(Self {
data,
superblock,
addr_offset,
handle: None,
})
}
/// Parse the superblock from `data`, returning it (with `root_group_address`
/// normalized to an absolute offset) and the base-address offset.
fn parse_superblock(data: &[u8]) -> Result<(Superblock, u64), Error> {
let sig_offset = signature::find_signature(data)?;
let mut superblock = Superblock::parse(data, sig_offset)?;
let addr_offset = superblock.base_address;
// Normalize root_group_address to absolute so resolve_path_any works.
superblock.root_group_address += addr_offset;
Ok((superblock, addr_offset))
}
/// Re-read the file from disk to pick up data appended by a concurrent
/// writer, then re-parse the superblock.
///
/// This is the SWMR reader's refresh primitive (analogous to the C library's
/// `H5Drefresh` / h5py's `Dataset.refresh()`): after it returns, newly
/// fetched [`Dataset`]/[`Group`] handles observe the writer's appended
/// chunks and extended dimensions, because they re-parse object headers at
/// their (stable) addresses against the refreshed bytes. Existing handles
/// borrow `&self`, so they must be dropped before calling this; re-fetch
/// them afterward.
///
/// Returns [`Error::SwmrUnsupported`] if the file was not opened with
/// [`File::open_swmr`]. The superblock is checksum-validated on every
/// re-read; a transient parse failure (a writer caught mid-flush) is
/// retried a bounded number of times before being surfaced.
///
/// Cost: each call re-reads the entire file from disk (`O(file size)`).
/// That keeps the implementation simple and correct, but when following a
/// large, steadily growing log it is the cost paid per refresh; budget
/// refresh frequency accordingly.
pub fn refresh(&mut self) -> Result<(), Error> {
let handle = self.handle.as_mut().ok_or(Error::SwmrUnsupported)?;
// A writer only appends (the file grows) and updates a few fixed-size,
// individually checksummed structures in place (superblock EOF, object
// header dimensions, array header counts). Re-reading the whole file and
// re-validating the superblock checksum yields a consistent view; if the
// superblock is caught mid-update, retry.
const MAX_ATTEMPTS: u32 = 100;
let mut last_err = None;
for attempt in 0..MAX_ATTEMPTS {
let mut data = Vec::new();
handle.seek(SeekFrom::Start(0)).map_err(Error::Io)?;
handle.read_to_end(&mut data).map_err(Error::Io)?;
match Self::parse_superblock(&data) {
Ok((superblock, addr_offset)) => {
self.data = data;
self.superblock = superblock;
self.addr_offset = addr_offset;
return Ok(());
}
Err(e) => {
last_err = Some(e);
// Brief backoff before re-reading; the writer's in-place
// updates are tiny, so a short pause clears the window. Skip
// it on the final attempt, where there is no re-read to come.
if attempt + 1 < MAX_ATTEMPTS {
std::thread::sleep(std::time::Duration::from_micros(
50 * (attempt + 1) as u64,
));
}
}
}
}
// The loop always runs at least once and only reaches here via the
// `Err` arm, so `last_err` is always `Some`; surface the real error.
Err(last_err.expect("refresh retried at least once before failing"))
}
/// Returns a handle to the root group.
pub fn root(&self) -> Group<'_> {
Group {
file: self,
// root_group_address was normalized to absolute in from_bytes()
address: self.superblock.root_group_address,
}
}
/// Resolve a path and return a `Dataset` handle.
pub fn dataset(&self, path: &str) -> Result<Dataset<'_>, Error> {
let addr = group_v2::resolve_path_any(&self.data, &self.superblock, path)?;
let hdr = self.parse_header(addr)?;
if !has_message(&hdr, MessageType::DataLayout) {
return Err(Error::NotADataset(path.to_string()));
}
Ok(Dataset {
file: self,
header: hdr,
chunk_cache: ChunkCache::new(),
})
}
/// Resolve a path and return a `Group` handle.
pub fn group(&self, path: &str) -> Result<Group<'_>, Error> {
let addr = group_v2::resolve_path_any(&self.data, &self.superblock, path)?;
Ok(Group {
file: self,
address: addr,
})
}
/// Returns the raw file bytes.
pub fn as_bytes(&self) -> &[u8] {
&self.data
}
/// Returns a reference to the parsed superblock.
pub fn superblock(&self) -> &Superblock {
&self.superblock
}
fn parse_header(&self, address: u64) -> Result<ObjectHeader, FormatError> {
ObjectHeader::parse_with_base(
&self.data,
address as usize,
self.superblock.offset_size,
self.superblock.length_size,
self.addr_offset,
)
}
fn offset_size(&self) -> u8 {
self.superblock.offset_size
}
fn length_size(&self) -> u8 {
self.superblock.length_size
}
}
impl std::fmt::Debug for File {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("File")
.field("size", &self.data.len())
.field("superblock_version", &self.superblock.version)
.finish()
}
}
// ---------------------------------------------------------------------------
// Group handle
// ---------------------------------------------------------------------------
/// A lightweight handle to an HDF5 group.
pub struct Group<'f> {
file: &'f File,
address: u64,
}
impl<'f> Group<'f> {
/// List the names of datasets in this group.
pub fn datasets(&self) -> Result<Vec<String>, Error> {
let entries = self.children()?;
let mut names = Vec::new();
for entry in &entries {
let hdr = self.file.parse_header(entry.object_header_address)?;
if has_message(&hdr, MessageType::DataLayout) {
names.push(entry.name.clone());
}
}
Ok(names)
}
/// List the names of subgroups in this group.
pub fn groups(&self) -> Result<Vec<String>, Error> {
let entries = self.children()?;
let mut names = Vec::new();
for entry in &entries {
let hdr = self.file.parse_header(entry.object_header_address)?;
if is_group(&hdr) {
names.push(entry.name.clone());
}
}
Ok(names)
}
/// Read all attributes of this group.
pub fn attrs(&self) -> Result<HashMap<String, AttrValue>, Error> {
let hdr = self.file.parse_header(self.address)?;
let attr_msgs = extract_attributes_full(
&self.file.data,
&hdr,
self.file.offset_size(),
self.file.length_size(),
)?;
Ok(attrs_to_map(
&attr_msgs,
&self.file.data,
self.file.offset_size(),
self.file.length_size(),
self.file.addr_offset,
))
}
/// Get a dataset within this group by name.
pub fn dataset(&self, name: &str) -> Result<Dataset<'f>, Error> {
let entries = self.children()?;
let entry = entries
.iter()
.find(|e| e.name == name)
.ok_or_else(|| Error::Format(FormatError::PathNotFound(name.to_string())))?;
let hdr = self.file.parse_header(entry.object_header_address)?;
if !has_message(&hdr, MessageType::DataLayout) {
return Err(Error::NotADataset(name.to_string()));
}
Ok(Dataset {
file: self.file,
header: hdr,
chunk_cache: ChunkCache::new(),
})
}
/// Get a subgroup within this group by name.
pub fn group(&self, name: &str) -> Result<Group<'f>, Error> {
let entries = self.children()?;
let entry = entries
.iter()
.find(|e| e.name == name)
.ok_or_else(|| Error::Format(FormatError::PathNotFound(name.to_string())))?;
Ok(Group {
file: self.file,
address: entry.object_header_address,
})
}
fn children(&self) -> Result<Vec<GroupEntry>, Error> {
let hdr = self.file.parse_header(self.address)?;
let os = self.file.offset_size();
let ls = self.file.length_size();
let base = self.file.addr_offset;
let mut entries = group_v2::resolve_group_entries(&self.file.data, &hdr, os, ls, base)
.map_err(Error::Format)?;
// Convert link addresses from relative to absolute
for entry in &mut entries {
entry.object_header_address += base;
}
Ok(entries)
}
}
// ---------------------------------------------------------------------------
// Dataset handle
// ---------------------------------------------------------------------------
/// A lightweight handle to an HDF5 dataset.
pub struct Dataset<'f> {
file: &'f File,
header: ObjectHeader,
// Held per-dataset: the chunk index is keyed only by chunk coordinate, so
// a file-level cache would alias chunk addresses across datasets.
chunk_cache: ChunkCache,
}
impl<'f> std::fmt::Debug for Dataset<'f> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Dataset")
.field("messages", &self.header.messages.len())
.finish()
}
}
impl<'f> Dataset<'f> {
/// Returns the shape (dimensions) of the dataset.
pub fn shape(&self) -> Result<Vec<u64>, Error> {
let ds = self.dataspace()?;
Ok(ds.dimensions.clone())
}
/// Returns the simplified datatype of the dataset.
pub fn dtype(&self) -> Result<DType, Error> {
let dt = self.datatype()?;
Ok(classify_datatype(&dt))
}
/// Read all data as `f64` values.
pub fn read_f64(&self) -> Result<Vec<f64>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
Ok(data_read::read_as_f64(&raw, &dt)?)
}
/// Read all data as `f32` values.
pub fn read_f32(&self) -> Result<Vec<f32>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
Ok(data_read::read_as_f32(&raw, &dt)?)
}
/// Read all data as `i32` values.
pub fn read_i32(&self) -> Result<Vec<i32>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
Ok(data_read::read_as_i32(&raw, &dt)?)
}
/// Read all data as `i64` values.
pub fn read_i64(&self) -> Result<Vec<i64>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
Ok(data_read::read_as_i64(&raw, &dt)?)
}
/// Read all data as `u64` values.
pub fn read_u64(&self) -> Result<Vec<u64>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
Ok(data_read::read_as_u64(&raw, &dt)?)
}
/// Read all data as `u8` values.
pub fn read_u8(&self) -> Result<Vec<u8>, Error> {
self.read_raw()
}
/// Read all data as `i8` values.
pub fn read_i8(&self) -> Result<Vec<i8>, Error> {
let raw = self.read_raw()?;
Ok(raw.iter().map(|&b| b as i8).collect())
}
/// Read all data as `i16` values.
pub fn read_i16(&self) -> Result<Vec<i16>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
let vals = data_read::read_as_i32(&raw, &dt)?;
Ok(vals.into_iter().map(|v| v as i16).collect())
}
/// Read all data as `u16` values.
pub fn read_u16(&self) -> Result<Vec<u16>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
let vals = data_read::read_as_u64(&raw, &dt)?;
Ok(vals.into_iter().map(|v| v as u16).collect())
}
/// Read all data as `u32` values.
pub fn read_u32(&self) -> Result<Vec<u32>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
let vals = data_read::read_as_u64(&raw, &dt)?;
Ok(vals.into_iter().map(|v| v as u32).collect())
}
/// Read all data as `String` values.
pub fn read_string(&self) -> Result<Vec<String>, Error> {
let raw = self.read_raw()?;
let dt = self.datatype()?;
Ok(data_read::read_as_strings(&raw, &dt)?)
}
/// Read all attributes of this dataset.
pub fn attrs(&self) -> Result<HashMap<String, AttrValue>, Error> {
let attr_msgs = extract_attributes_full(
&self.file.data,
&self.header,
self.file.offset_size(),
self.file.length_size(),
)?;
Ok(attrs_to_map(
&attr_msgs,
&self.file.data,
self.file.offset_size(),
self.file.length_size(),
self.file.addr_offset,
))
}
fn datatype(&self) -> Result<Datatype, Error> {
let msg = find_message(&self.header, MessageType::Datatype)?;
let (dt, _) = Datatype::parse(&msg.data)?;
Ok(dt)
}
fn dataspace(&self) -> Result<Dataspace, Error> {
let msg = find_message(&self.header, MessageType::Dataspace)?;
Ok(Dataspace::parse(&msg.data, self.file.length_size())?)
}
fn data_layout(&self) -> Result<DataLayout, Error> {
let msg = find_message(&self.header, MessageType::DataLayout)?;
Ok(DataLayout::parse(
&msg.data,
self.file.offset_size(),
self.file.length_size(),
)?)
}
fn filter_pipeline(&self) -> Option<FilterPipeline> {
self.header
.messages
.iter()
.find(|m| m.msg_type == MessageType::FilterPipeline)
.and_then(|msg| FilterPipeline::parse(&msg.data).ok())
}
fn read_raw(&self) -> Result<Vec<u8>, Error> {
let dt = self.datatype()?;
let ds = self.dataspace()?;
let mut dl = self.data_layout()?;
// Adjust contiguous data address by base_address offset
if self.file.addr_offset != 0
&& let DataLayout::Contiguous {
ref mut address, ..
} = dl
&& let Some(addr) = address
{
*addr += self.file.addr_offset;
}
let pipeline = self.filter_pipeline();
Ok(data_read::read_raw_data_cached(
&self.file.data,
&dl,
&ds,
&dt,
pipeline.as_ref(),
self.file.offset_size(),
self.file.length_size(),
&self.chunk_cache,
)?)
}
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
fn find_message(
header: &ObjectHeader,
msg_type: MessageType,
) -> Result<&crate::object_header::HeaderMessage, Error> {
header
.messages
.iter()
.find(|m| m.msg_type == msg_type)
.ok_or(Error::MissingMessage(msg_type))
}
fn has_message(header: &ObjectHeader, msg_type: MessageType) -> bool {
header.messages.iter().any(|m| m.msg_type == msg_type)
}
fn is_group(header: &ObjectHeader) -> bool {
header.messages.iter().any(|m| {
m.msg_type == MessageType::LinkInfo
|| m.msg_type == MessageType::Link
|| m.msg_type == MessageType::SymbolTable
})
}