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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
//! Dataset creation and I/O.
//!
//! Datasets are created via the fluent [`DatasetBuilder`] API obtained from
//! [`H5File::new_dataset`](crate::file::H5File::new_dataset). Once created,
//! the [`H5Dataset`] handle can read or write raw typed data.
use crate::attribute::AttrBuilder;
use crate::error::{Hdf5Error, Result};
use crate::file::{borrow_inner, borrow_inner_mut, clone_inner, H5FileInner, SharedInner};
use crate::types::H5Type;
// ---------------------------------------------------------------------------
// DatasetBuilder
// ---------------------------------------------------------------------------
/// A fluent builder for creating datasets.
///
/// Obtained from [`H5File::new_dataset::<T>()`](crate::file::H5File::new_dataset).
///
/// ```no_run
/// # use rust_hdf5::H5File;
/// let file = H5File::create("builder.h5").unwrap();
/// let ds = file.new_dataset::<f32>()
/// .shape(&[10, 20])
/// .create("temperatures")
/// .unwrap();
/// ```
pub struct DatasetBuilder<T: H5Type> {
file_inner: SharedInner,
shape: Option<Vec<usize>>,
chunk_dims: Option<Vec<usize>>,
max_shape: Option<Vec<Option<usize>>>,
deflate_level: Option<u32>,
shuffle_deflate_level: Option<u32>,
custom_pipeline: Option<crate::format::messages::filter::FilterPipeline>,
group_path: Option<String>,
fill_value: Option<Vec<u8>>,
_marker: std::marker::PhantomData<T>,
}
impl<T: H5Type> DatasetBuilder<T> {
pub(crate) fn new(file_inner: SharedInner) -> Self {
Self {
file_inner,
shape: None,
chunk_dims: None,
max_shape: None,
deflate_level: None,
shuffle_deflate_level: None,
custom_pipeline: None,
group_path: None,
fill_value: None,
_marker: std::marker::PhantomData,
}
}
pub(crate) fn new_in_group(file_inner: SharedInner, group_path: String) -> Self {
Self {
file_inner,
shape: None,
chunk_dims: None,
max_shape: None,
deflate_level: None,
shuffle_deflate_level: None,
custom_pipeline: None,
group_path: Some(group_path),
fill_value: None,
_marker: std::marker::PhantomData,
}
}
/// Set the dataset dimensions.
///
/// This is required before calling [`create`](Self::create).
/// Use an empty slice `&[]` for a scalar (0-dimensional) dataset.
#[must_use]
pub fn shape<S: AsRef<[usize]>>(mut self, dims: S) -> Self {
self.shape = Some(dims.as_ref().to_vec());
self
}
/// Create a scalar (0-dimensional) dataset holding a single value.
#[must_use]
pub fn scalar(mut self) -> Self {
self.shape = Some(vec![]);
self
}
/// Set chunk dimensions for chunked storage.
///
/// When set, the dataset uses chunked storage with the extensible array
/// index. You should also call [`max_shape`](Self::max_shape) or
/// [`resizable`](Self::resizable) to allow extending.
#[must_use]
pub fn chunk(mut self, chunk_dims: &[usize]) -> Self {
self.chunk_dims = Some(chunk_dims.to_vec());
self
}
/// Make all dimensions unlimited (resizable).
///
/// This sets max_dims to u64::MAX for all dimensions.
#[must_use]
pub fn resizable(mut self) -> Self {
self.max_shape = Some(vec![None; self.shape.as_ref().map_or(0, |s| s.len())]);
self
}
/// Set maximum dimensions. `None` means unlimited for that dimension.
#[must_use]
pub fn max_shape(mut self, max: &[Option<usize>]) -> Self {
self.max_shape = Some(max.to_vec());
self
}
/// Enable deflate (gzip) compression with the given level (0-9).
///
/// Requires chunked storage (call `.chunk()` before `.create()`).
/// Level 0 = no compression, 9 = maximum compression. Default is 6.
#[must_use]
pub fn deflate(mut self, level: u32) -> Self {
self.deflate_level = Some(level);
self
}
/// Enable shuffle + deflate compression.
///
/// Shuffle reorders bytes by position within elements before compression,
/// which typically improves compression ratios for numeric data.
/// Requires chunked storage.
#[must_use]
pub fn shuffle_deflate(mut self, level: u32) -> Self {
self.shuffle_deflate_level = Some(level);
self
}
/// Enable Zstandard compression with the given level (1-22, default 3).
///
/// Requires chunked storage (call `.chunk()` before `.create()`).
#[must_use]
pub fn zstd(mut self, level: u32) -> Self {
self.custom_pipeline = Some(crate::format::messages::filter::FilterPipeline::zstd(level));
self
}
/// Set a custom filter pipeline for compression.
///
/// This takes precedence over [`deflate`](Self::deflate) and
/// [`shuffle_deflate`](Self::shuffle_deflate). Requires chunked storage.
#[must_use]
pub fn filter_pipeline(
mut self,
pipeline: crate::format::messages::filter::FilterPipeline,
) -> Self {
self.custom_pipeline = Some(pipeline);
self
}
/// Set a user-defined fill value for unwritten elements.
///
/// Without this, datasets use the HDF5 default zero-fill. When set,
/// the value is written into the dataset's fill-value message
/// (`fill_defined = 2`), so HDF5 readers treat unallocated chunks and
/// unwritten regions as this value rather than zero.
///
/// ```no_run
/// # use rust_hdf5::H5File;
/// let file = H5File::create("fv.h5").unwrap();
/// let ds = file.new_dataset::<f32>()
/// .shape(&[100])
/// .fill_value(f32::NAN)
/// .create("data")
/// .unwrap();
/// ```
#[must_use]
pub fn fill_value(mut self, value: T) -> Self {
let es = T::element_size();
// Safety: `T: H5Type` is a `Copy` numeric primitive with a
// well-defined byte representation; `element_size()` matches
// `size_of::<T>()`. The slice borrows `value` only for this call.
let raw = unsafe { std::slice::from_raw_parts(&value as *const T as *const u8, es) };
self.fill_value = Some(raw.to_vec());
self
}
/// Finalize and create the dataset with the given `name`.
///
/// The name is the link name within the root group (e.g. `"data"` or
/// `"group1/data"` once nested groups are supported).
pub fn create(self, name: &str) -> Result<H5Dataset> {
let shape = self.shape.ok_or_else(|| {
Hdf5Error::InvalidState("shape must be set before calling create()".into())
})?;
// Build the full name: if created within a group, prefix with group path
let full_name = if let Some(ref gp) = self.group_path {
if gp == "/" {
name.to_string()
} else {
let trimmed = gp.trim_start_matches('/');
format!("{}/{}", trimmed, name)
}
} else {
name.to_string()
};
let group_path = self.group_path.clone();
let fill_value = self.fill_value.clone();
let dims_u64: Vec<u64> = shape.iter().map(|&d| d as u64).collect();
let datatype = T::hdf5_type();
let element_size = T::element_size();
if let Some(ref chunk_dims) = self.chunk_dims {
// Chunked dataset
let chunk_u64: Vec<u64> = chunk_dims.iter().map(|&d| d as u64).collect();
let max_u64: Vec<u64> = if let Some(ref max) = self.max_shape {
max.iter()
.map(|m| m.map_or(u64::MAX, |v| v as u64))
.collect()
} else {
// Default: max = current
dims_u64.clone()
};
// libhdf5 selects the chunk index from the dataspace: a v2
// B-tree for two or more unlimited dimensions, an extensible
// array for exactly one, and a fixed array when there are none.
let n_unlimited = max_u64.iter().filter(|&&m| m == u64::MAX).count();
let is_btree2 = n_unlimited >= 2;
let is_fixed_array = n_unlimited == 0;
let wants_filter = self.custom_pipeline.is_some()
|| self.shuffle_deflate_level.is_some()
|| self.deflate_level.is_some();
let index = {
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
let idx = if is_btree2 {
if wants_filter {
return Err(Hdf5Error::InvalidState(
"compression of v2 B-tree (multi-unlimited-dimension) \
datasets is not yet supported"
.into(),
));
}
writer.create_btree_v2_dataset(
&full_name, datatype, &dims_u64, &max_u64, &chunk_u64,
)?
} else if is_fixed_array {
// A chunked dataset with no unlimited dimension
// must use the fixed-array index — libhdf5
// rejects an extensible-array index here. A
// compressed fixed-shape dataset uses a *filtered*
// fixed array (FA client id 1).
if wants_filter {
let pipeline = if let Some(p) = self.custom_pipeline {
p
} else if let Some(level) = self.shuffle_deflate_level {
crate::format::messages::filter::FilterPipeline::shuffle_deflate(
T::element_size() as u32,
level,
)
} else {
// deflate_level (checked by wants_filter).
crate::format::messages::filter::FilterPipeline::deflate(
self.deflate_level.unwrap(),
)
};
writer.create_fixed_array_dataset_with_pipeline(
&full_name, datatype, &dims_u64, &chunk_u64, pipeline,
)?
} else {
writer.create_fixed_array_dataset(
&full_name, datatype, &dims_u64, &chunk_u64,
)?
}
} else if let Some(pipeline) = self.custom_pipeline {
writer.create_chunked_dataset_with_pipeline(
&full_name, datatype, &dims_u64, &max_u64, &chunk_u64, pipeline,
)?
} else if let Some(level) = self.shuffle_deflate_level {
let pipeline =
crate::format::messages::filter::FilterPipeline::shuffle_deflate(
T::element_size() as u32,
level,
);
writer.create_chunked_dataset_with_pipeline(
&full_name, datatype, &dims_u64, &max_u64, &chunk_u64, pipeline,
)?
} else if let Some(level) = self.deflate_level {
writer.create_chunked_dataset_compressed(
&full_name, datatype, &dims_u64, &max_u64, &chunk_u64, level,
)?
} else {
writer.create_chunked_dataset(
&full_name, datatype, &dims_u64, &max_u64, &chunk_u64,
)?
};
if let Some(ref gp) = group_path {
if gp != "/" {
writer.assign_dataset_to_group(gp, idx)?;
}
}
if let Some(ref fv) = fill_value {
writer.set_dataset_fill_value(idx, fv.clone())?;
}
idx
}
H5FileInner::Reader(_) => {
return Err(Hdf5Error::InvalidState(
"cannot create a dataset in read mode".into(),
));
}
H5FileInner::Closed => {
return Err(Hdf5Error::InvalidState("file is closed".into()));
}
}
};
Ok(H5Dataset {
file_inner: clone_inner(&self.file_inner),
info: DatasetInfo::Writer {
index,
shape,
element_size,
chunked: true,
btree2: is_btree2,
fixed_array: is_fixed_array,
},
})
} else {
// Contiguous dataset (original path)
let index = {
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
let idx = writer.create_dataset(&full_name, datatype, &dims_u64)?;
if let Some(ref gp) = group_path {
if gp != "/" {
writer.assign_dataset_to_group(gp, idx)?;
}
}
if let Some(ref fv) = fill_value {
writer.set_dataset_fill_value(idx, fv.clone())?;
}
idx
}
H5FileInner::Reader(_) => {
return Err(Hdf5Error::InvalidState(
"cannot create a dataset in read mode".into(),
));
}
H5FileInner::Closed => {
return Err(Hdf5Error::InvalidState("file is closed".into()));
}
}
};
Ok(H5Dataset {
file_inner: clone_inner(&self.file_inner),
info: DatasetInfo::Writer {
index,
shape,
element_size,
chunked: false,
btree2: false,
fixed_array: false,
},
})
}
}
}
// ---------------------------------------------------------------------------
// DatasetInfo
// ---------------------------------------------------------------------------
/// Internal metadata about a dataset handle.
enum DatasetInfo {
/// A dataset created via `new_dataset().create()` in write mode.
Writer {
/// Index into the writer's dataset list.
index: usize,
/// Shape (current dimensions).
shape: Vec<usize>,
/// Size of one element in bytes.
element_size: usize,
/// Whether this is a chunked dataset.
chunked: bool,
/// Whether the chunk index is a v2 B-tree (multiple unlimited dims).
btree2: bool,
/// Whether the chunk index is a Fixed Array (no unlimited dims).
fixed_array: bool,
},
/// A dataset opened by name in read mode.
Reader {
/// The link name of the dataset.
name: String,
/// Shape (current dimensions).
shape: Vec<usize>,
/// Size of one element in bytes.
element_size: usize,
},
}
// ---------------------------------------------------------------------------
// H5Dataset
// ---------------------------------------------------------------------------
/// A handle to an HDF5 dataset, supporting typed read and write operations.
///
/// The dataset holds a shared reference to the file's I/O backend, so it
/// remains valid even if the originating [`H5File`](crate::file::H5File) is
/// moved or dropped (they share ownership via `Rc`).
pub struct H5Dataset {
file_inner: SharedInner,
info: DatasetInfo,
}
impl H5Dataset {
/// Create a reader-mode dataset handle (called internally by `H5File::dataset`).
pub(crate) fn new_reader(
file_inner: SharedInner,
name: String,
shape: Vec<usize>,
element_size: usize,
) -> Self {
Self {
file_inner,
info: DatasetInfo::Reader {
name,
shape,
element_size,
},
}
}
/// Return the dataset dimensions.
pub fn shape(&self) -> Vec<usize> {
match &self.info {
DatasetInfo::Writer { shape, .. } => shape.clone(),
DatasetInfo::Reader { shape, .. } => shape.clone(),
}
}
/// Return the number of dimensions (rank) of the dataset.
pub fn ndims(&self) -> usize {
match &self.info {
DatasetInfo::Writer { shape, .. } => shape.len(),
DatasetInfo::Reader { shape, .. } => shape.len(),
}
}
/// Return the total number of elements in the dataset.
pub fn total_elements(&self) -> usize {
match &self.info {
DatasetInfo::Writer { shape, .. } => shape.iter().product(),
DatasetInfo::Reader { shape, .. } => shape.iter().product(),
}
}
/// Return the size of one element in bytes.
pub fn element_size(&self) -> usize {
match &self.info {
DatasetInfo::Writer { element_size, .. } => *element_size,
DatasetInfo::Reader { element_size, .. } => *element_size,
}
}
/// Return the chunk dimensions, if this is a chunked dataset.
pub fn chunk_dims(&self) -> Option<Vec<usize>> {
match &self.info {
DatasetInfo::Reader { name, .. } => {
let inner = borrow_inner(&self.file_inner);
if let H5FileInner::Reader(reader) = &*inner {
if let Some(info) = reader.dataset_info(name) {
use crate::format::messages::data_layout::DataLayoutMessage;
let chunk_dims = match &info.layout {
DataLayoutMessage::ChunkedV4 { chunk_dims, .. }
| DataLayoutMessage::ChunkedV3 { chunk_dims, .. } => Some(chunk_dims),
_ => None,
};
if let Some(chunk_dims) = chunk_dims {
// Strip trailing element-size dimension
return Some(
chunk_dims[..chunk_dims.len() - 1]
.iter()
.map(|&d| d as usize)
.collect(),
);
}
}
}
None
}
DatasetInfo::Writer { .. } => None,
}
}
/// Return whether this is a chunked dataset.
pub fn is_chunked(&self) -> bool {
match &self.info {
DatasetInfo::Writer { chunked, .. } => *chunked,
DatasetInfo::Reader { name, .. } => {
let inner = borrow_inner(&self.file_inner);
match &*inner {
H5FileInner::Reader(reader) => {
if let Some(info) = reader.dataset_info(name) {
use crate::format::messages::data_layout::DataLayoutMessage;
matches!(
info.layout,
DataLayoutMessage::ChunkedV4 { .. }
| DataLayoutMessage::ChunkedV3 { .. }
)
} else {
false
}
}
_ => false,
}
}
}
}
/// Return the names of all attributes on this dataset (read mode only).
pub fn attr_names(&self) -> Result<Vec<String>> {
match &self.info {
DatasetInfo::Reader { name, .. } => {
let inner = borrow_inner(&self.file_inner);
match &*inner {
H5FileInner::Reader(reader) => Ok(reader.dataset_attr_names(name)?),
_ => Err(Hdf5Error::InvalidState("file is not in read mode".into())),
}
}
DatasetInfo::Writer { .. } => Err(Hdf5Error::InvalidState(
"attr_names not available in write mode".into(),
)),
}
}
/// Open an attribute by name (read mode only).
pub fn attr(&self, attr_name: &str) -> Result<crate::attribute::H5Attribute> {
match &self.info {
DatasetInfo::Reader { name, .. } => {
let inner = borrow_inner(&self.file_inner);
match &*inner {
H5FileInner::Reader(reader) => {
let attr_msg = reader.dataset_attr(name, attr_name)?.clone();
Ok(crate::attribute::H5Attribute::new_reader(
clone_inner(&self.file_inner),
attr_msg,
))
}
_ => Err(Hdf5Error::InvalidState("file is not in read mode".into())),
}
}
DatasetInfo::Writer { .. } => Err(Hdf5Error::InvalidState(
"attr() not available in write mode".into(),
)),
}
}
/// Start building a new attribute on this dataset.
///
/// Returns a fluent builder. Call `.shape(())` for a scalar attribute
/// and `.create("name")` to finalize.
///
/// # Example
///
/// ```no_run
/// # use rust_hdf5::H5File;
/// # use rust_hdf5::types::VarLenUnicode;
/// let file = H5File::create("attr.h5").unwrap();
/// let ds = file.new_dataset::<f32>().shape(&[10]).create("data").unwrap();
/// let attr = ds.new_attr::<VarLenUnicode>().shape(()).create("units").unwrap();
/// attr.write_scalar(&VarLenUnicode("meters".to_string())).unwrap();
/// ```
pub fn new_attr<T: 'static>(&self) -> AttrBuilder<'_, T> {
let ds_index = match &self.info {
DatasetInfo::Writer { index, .. } => *index,
DatasetInfo::Reader { .. } => {
// Reader mode: we'll return a builder that will error on create.
// Using usize::MAX as sentinel.
usize::MAX
}
};
AttrBuilder::new(&self.file_inner, ds_index)
}
/// Write a typed slice to the dataset (contiguous datasets only).
///
/// The slice length must match the total number of elements declared by
/// the dataset shape. The data is reinterpreted as raw bytes and written
/// to the file.
///
/// # Errors
///
/// Returns an error if:
/// - The file is in read mode.
/// - The data length does not match the declared shape.
pub fn write_raw<T: H5Type>(&self, data: &[T]) -> Result<()> {
match &self.info {
DatasetInfo::Writer {
index,
shape,
element_size,
chunked,
btree2: _,
fixed_array: _,
} => {
if *chunked {
return Err(Hdf5Error::InvalidState(
"use write_chunk for chunked datasets".into(),
));
}
let total_elements: usize = shape.iter().product();
if data.len() != total_elements {
return Err(Hdf5Error::InvalidState(format!(
"data length {} does not match dataset size {}",
data.len(),
total_elements,
)));
}
// Verify element size matches
if T::element_size() != *element_size {
return Err(Hdf5Error::TypeMismatch(format!(
"write type has element size {} but dataset expects {}",
T::element_size(),
element_size,
)));
}
// Safety: T: Copy + 'static (numeric primitive) with well-defined
// byte representation. The resulting slice borrows `data` and
// lives only as long as this block.
let byte_len = data.len() * T::element_size();
let raw =
unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, byte_len) };
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
writer.write_dataset_raw(*index, raw)?;
Ok(())
}
_ => Err(Hdf5Error::InvalidState(
"file is no longer in write mode".into(),
)),
}
}
DatasetInfo::Reader { .. } => Err(Hdf5Error::InvalidState(
"cannot write to a dataset opened in read mode".into(),
)),
}
}
/// Write a single chunk to a chunked dataset.
///
/// `chunk_idx` is the linear chunk index (typically the frame number for
/// streaming datasets). `data` is the raw byte data for one chunk.
///
/// For datasets with two or more unlimited dimensions (v2 B-tree index),
/// use [`write_chunk_at`](Self::write_chunk_at) instead.
pub fn write_chunk(&self, chunk_idx: usize, data: &[u8]) -> Result<()> {
match &self.info {
DatasetInfo::Writer {
index,
chunked,
btree2,
fixed_array,
..
} => {
if !*chunked {
return Err(Hdf5Error::InvalidState(
"write_chunk is only for chunked datasets".into(),
));
}
if *btree2 {
return Err(Hdf5Error::InvalidState(
"this dataset uses a v2 B-tree chunk index; use write_chunk_at \
with the chunk's grid coordinates"
.into(),
));
}
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
if *fixed_array {
// Fixed-array dataset: convert the linear chunk
// index into row-major grid coordinates.
let chunk_dims = writer
.dataset_chunk_dims(*index)
.ok_or_else(|| {
Hdf5Error::InvalidState("dataset has no chunk info".into())
})?
.to_vec();
let dims = writer.dataset_dims(*index).to_vec();
let mut grid = vec![0u64; dims.len()];
for d in 0..dims.len() {
grid[d] = if chunk_dims[d] > 0 {
dims[d].div_ceil(chunk_dims[d])
} else {
1
};
}
// A zero-extent dimension yields a grid of 0
// chunks — there is no chunk to write.
if grid.contains(&0) {
return Err(Hdf5Error::InvalidState(
"dataset has a zero-extent dimension and no chunks".into(),
));
}
let mut rem = chunk_idx as u64;
let mut coords = vec![0u64; dims.len()];
for d in (0..dims.len()).rev() {
coords[d] = rem % grid[d];
rem /= grid[d];
}
// A leftover means chunk_idx exceeded the grid.
if rem != 0 {
return Err(Hdf5Error::InvalidState(format!(
"chunk index {chunk_idx} is out of range for this dataset"
)));
}
writer.write_chunk_fixed_array(*index, &coords, data)?;
} else {
writer.write_chunk(*index, chunk_idx as u64, data)?;
}
Ok(())
}
_ => Err(Hdf5Error::InvalidState(
"file is no longer in write mode".into(),
)),
}
}
DatasetInfo::Reader { .. } => {
Err(Hdf5Error::InvalidState("cannot write in read mode".into()))
}
}
}
/// Write a single chunk to a v2-B-tree-indexed dataset, addressed by its
/// chunk-grid coordinates (one per dimension).
///
/// This is the entry point for datasets with two or more unlimited
/// dimensions. The dataset's logical dimensions are extended to cover
/// the written chunk. `data` is the raw bytes of one full chunk.
///
/// ```no_run
/// # use rust_hdf5::H5File;
/// let file = H5File::create("bt2.h5").unwrap();
/// let ds = file.new_dataset::<i32>()
/// .shape(&[0, 0])
/// .chunk(&[2, 2])
/// .max_shape(&[None, None])
/// .create("grid")
/// .unwrap();
/// let chunk = [0i32, 1, 2, 3];
/// let bytes: Vec<u8> = chunk.iter().flat_map(|v| v.to_le_bytes()).collect();
/// ds.write_chunk_at(&[0, 0], &bytes).unwrap();
/// ```
pub fn write_chunk_at(&self, chunk_coords: &[usize], data: &[u8]) -> Result<()> {
match &self.info {
DatasetInfo::Writer {
index,
chunked,
btree2,
fixed_array,
..
} => {
if !*chunked {
return Err(Hdf5Error::InvalidState(
"write_chunk_at is only for chunked datasets".into(),
));
}
let coords: Vec<u64> = chunk_coords.iter().map(|&c| c as u64).collect();
let btree2 = *btree2;
let fixed_array = *fixed_array;
let mut inner = borrow_inner_mut(&self.file_inner);
let writer = match &mut *inner {
H5FileInner::Writer(w) => w,
_ => {
return Err(Hdf5Error::InvalidState(
"file is no longer in write mode".into(),
))
}
};
let chunk_dims = writer
.dataset_chunk_dims(*index)
.ok_or_else(|| Hdf5Error::InvalidState("dataset has no chunk info".into()))?
.to_vec();
let dims = writer.dataset_dims(*index).to_vec();
if coords.len() != dims.len() {
return Err(Hdf5Error::InvalidState(format!(
"chunk_coords has {} entries but the dataset has {} dimensions",
coords.len(),
dims.len()
)));
}
if chunk_dims.len() != dims.len() {
return Err(Hdf5Error::InvalidState(format!(
"dataset chunk shape has {} dimensions but the dataspace has {}",
chunk_dims.len(),
dims.len()
)));
}
// Validate coordinates and compute the grown dimensions
// up-front, before any chunk is written, so an overflowing
// coordinate cannot leave an orphaned chunk in the file.
let mut new_dims = dims.clone();
for d in 0..dims.len() {
let needed = coords[d]
.checked_add(1)
.and_then(|c| c.checked_mul(chunk_dims[d]))
.ok_or_else(|| {
Hdf5Error::InvalidState(format!(
"chunk coordinate {} in dimension {} is too large",
coords[d], d
))
})?;
if needed > new_dims[d] {
new_dims[d] = needed;
}
}
if fixed_array {
// Fixed-array (fixed-shape) dataset: no dimension growth.
writer.write_chunk_fixed_array(*index, &coords, data)?;
return Ok(());
}
if btree2 {
writer.write_chunk_btree_v2(*index, &coords, data)?;
} else {
// Extensible array: linearize the chunk-grid coordinates
// (row-major) into the array's chunk index.
let mut linear = 0u64;
for d in 0..dims.len() {
let grid = if chunk_dims[d] > 0 {
dims[d].div_ceil(chunk_dims[d])
} else {
1
};
linear = linear
.checked_mul(grid)
.and_then(|l| l.checked_add(coords[d]))
.ok_or_else(|| {
Hdf5Error::InvalidState(
"chunk coordinates overflow the array index".into(),
)
})?;
}
writer.write_chunk(*index, linear, data)?;
}
if new_dims != dims {
writer.extend_dataset(*index, &new_dims)?;
}
Ok(())
}
DatasetInfo::Reader { .. } => {
Err(Hdf5Error::InvalidState("cannot write in read mode".into()))
}
}
}
/// Write multiple chunks in a batch, optionally compressing in parallel.
///
/// `chunks` is a slice of `(chunk_index, raw_data)` pairs. When a filter
/// pipeline is configured and the `parallel` feature is enabled, all
/// chunks are compressed concurrently via rayon.
pub fn write_chunks_batch(&self, chunks: &[(usize, &[u8])]) -> Result<()> {
match &self.info {
DatasetInfo::Writer { index, chunked, .. } => {
if !*chunked {
return Err(Hdf5Error::InvalidState(
"write_chunks_batch is only for chunked datasets".into(),
));
}
let pairs: Vec<(u64, &[u8])> = chunks
.iter()
.map(|(idx, data)| (*idx as u64, *data))
.collect();
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
writer.write_chunks_batch(*index, &pairs)?;
Ok(())
}
_ => Err(Hdf5Error::InvalidState(
"file is no longer in write mode".into(),
)),
}
}
DatasetInfo::Reader { .. } => {
Err(Hdf5Error::InvalidState("cannot write in read mode".into()))
}
}
}
/// Append data along the first dimension of a chunked dataset.
///
/// `data` must contain a whole number of "frames" — slices along
/// dimension 0. For example, if the dataset has shape `[N, H, W]`
/// and `chunk_dims = [1, H, W]`, then `data.len()` must be a
/// multiple of `H * W`.
///
/// This method writes the necessary chunks and extends the dataset
/// shape automatically.
///
/// ```no_run
/// # use rust_hdf5::H5File;
/// let file = H5File::create("append.h5").unwrap();
/// let ds = file.new_dataset::<f64>()
/// .shape(&[0, 3])
/// .chunk(&[1, 3])
/// .max_shape(&[None, Some(3)])
/// .create("data")
/// .unwrap();
/// ds.append(&[1.0, 2.0, 3.0]).unwrap(); // shape becomes [1, 3]
/// ds.append(&[4.0, 5.0, 6.0, 7.0, 8.0, 9.0]).unwrap(); // shape becomes [3, 3]
/// ```
pub fn append<T: H5Type>(&self, data: &[T]) -> Result<()> {
match &self.info {
DatasetInfo::Writer {
index,
element_size,
chunked,
..
} => {
if !*chunked {
return Err(Hdf5Error::InvalidState(
"append is only for chunked datasets".into(),
));
}
if T::element_size() != *element_size {
return Err(Hdf5Error::TypeMismatch(format!(
"append type has element size {} but dataset expects {}",
T::element_size(),
element_size,
)));
}
let ds_index = *index;
let es = *element_size;
let mut inner = borrow_inner_mut(&self.file_inner);
let writer = match &mut *inner {
H5FileInner::Writer(w) => w,
_ => {
return Err(Hdf5Error::InvalidState(
"file is no longer in write mode".into(),
))
}
};
let chunk_dims = writer
.dataset_chunk_dims(ds_index)
.ok_or_else(|| Hdf5Error::InvalidState("dataset has no chunk info".into()))?
.to_vec();
let dims = writer.dataset_dims(ds_index).to_vec();
// Frame size = product of dims[1..]
let frame_elems: usize = if dims.len() > 1 {
dims[1..].iter().map(|&d| d as usize).product()
} else {
1
};
if frame_elems == 0 {
return Err(Hdf5Error::InvalidState(
"cannot append to dataset with zero-size trailing dimensions".into(),
));
}
if !data.len().is_multiple_of(frame_elems) {
return Err(Hdf5Error::InvalidState(format!(
"data length {} is not a multiple of frame size {}",
data.len(),
frame_elems,
)));
}
let n_new_frames = data.len() / frame_elems;
let current_dim0 = dims[0] as usize;
// Chunk size along first dimension
let chunk_dim0 = chunk_dims[0] as usize;
let frame_bytes = frame_elems * es;
let raw = unsafe {
std::slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * es)
};
// Merge buffered data with new data
let ds = &mut writer.datasets[ds_index];
let buffered_frames = ds.append_buffered_frames as usize;
let mut combined = std::mem::take(&mut ds.append_buffer);
combined.extend_from_slice(raw);
ds.append_buffered_frames = 0;
let total_frames = buffered_frames + n_new_frames;
let total_bytes = combined.len();
// Base chunk index: account for buffered frames
let base_dim0 = current_dim0 - buffered_frames;
let mut byte_pos = 0usize;
let mut frame_pos = 0usize;
while frame_pos < total_frames {
let abs_frame = base_dim0 + frame_pos;
let chunk_idx = abs_frame / chunk_dim0;
let remaining_frames = total_frames - frame_pos;
let frames_to_fill = chunk_dim0 - (abs_frame % chunk_dim0);
if remaining_frames >= frames_to_fill {
// Full chunk — write
let end = byte_pos + frames_to_fill * frame_bytes;
if frames_to_fill == chunk_dim0 {
writer.write_chunk(
ds_index,
chunk_idx as u64,
&combined[byte_pos..end],
)?;
} else {
// Partial-chunk write: this branch only runs with
// offset_in_chunk > 0, meaning the chunk already
// holds earlier frames on disk. Read-modify-write
// so those frames survive — a fresh fill buffer
// would erase them.
let offset_in_chunk = (abs_frame % chunk_dim0) * frame_bytes;
let mut chunk_buf =
match writer.read_chunk_if_present(ds_index, chunk_idx as u64)? {
Some(existing) => existing,
None => {
return Err(Hdf5Error::InvalidState(format!(
"cannot append into partially-written chunk {}: \
its existing content was not found in the chunk \
index (the file may be inconsistent)",
chunk_idx
)));
}
};
chunk_buf
[offset_in_chunk..offset_in_chunk + frames_to_fill * frame_bytes]
.copy_from_slice(&combined[byte_pos..end]);
writer.write_chunk(ds_index, chunk_idx as u64, &chunk_buf)?;
}
byte_pos = end;
frame_pos += frames_to_fill;
} else {
// Partial chunk — buffer for next append
let ds = &mut writer.datasets[ds_index];
ds.append_buffer = combined[byte_pos..total_bytes].to_vec();
ds.append_buffered_frames = remaining_frames as u64;
frame_pos = total_frames;
}
}
// Extend dims to include all frames (buffered + new)
let logical_dim0 = base_dim0 + total_frames;
let mut new_dims: Vec<u64> = dims;
new_dims[0] = logical_dim0 as u64;
writer.extend_dataset(ds_index, &new_dims)?;
Ok(())
}
DatasetInfo::Reader { .. } => {
Err(Hdf5Error::InvalidState("cannot append in read mode".into()))
}
}
}
/// Extend the dimensions of a chunked dataset.
pub fn extend(&self, new_dims: &[usize]) -> Result<()> {
match &self.info {
DatasetInfo::Writer { index, chunked, .. } => {
if !*chunked {
return Err(Hdf5Error::InvalidState(
"extend is only for chunked datasets".into(),
));
}
let dims_u64: Vec<u64> = new_dims.iter().map(|&d| d as u64).collect();
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
writer.extend_dataset(*index, &dims_u64)?;
Ok(())
}
_ => Err(Hdf5Error::InvalidState(
"file is no longer in write mode".into(),
)),
}
}
DatasetInfo::Reader { .. } => {
Err(Hdf5Error::InvalidState("cannot extend in read mode".into()))
}
}
}
/// Set the logical extent of a chunked dataset, growing **or
/// shrinking** any dimension.
///
/// Unlike [`extend`](Self::extend), which only grows, this can reduce a
/// dimension — for example to correct an over-extended frame count
/// after writing a partial multi-frame chunk. Shrinking changes the
/// logical dataspace only: data in chunks beyond the new extent stays
/// in the file but is no longer visible on read, exactly as libhdf5's
/// `H5Dset_extent` behaves. The new extent must not exceed the
/// dataset's maximum dimensions.
pub fn set_extent(&self, new_dims: &[usize]) -> Result<()> {
match &self.info {
DatasetInfo::Writer { index, .. } => {
let dims_u64: Vec<u64> = new_dims.iter().map(|&d| d as u64).collect();
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
writer.set_dataset_extent(*index, &dims_u64)?;
Ok(())
}
_ => Err(Hdf5Error::InvalidState(
"file is no longer in write mode".into(),
)),
}
}
DatasetInfo::Reader { .. } => Err(Hdf5Error::InvalidState(
"cannot set extent in read mode".into(),
)),
}
}
/// Flush a chunked dataset's index structures to disk.
pub fn flush(&self) -> Result<()> {
match &self.info {
DatasetInfo::Writer { index, .. } => {
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
writer.flush_dataset(*index)?;
Ok(())
}
_ => Ok(()),
}
}
DatasetInfo::Reader { .. } => Ok(()),
}
}
/// Read a slice (hyperslab) of the dataset as a typed vector.
///
/// `starts` and `counts` define the N-dimensional selection:
/// `starts[d]` = first index along dim d, `counts[d]` = how many elements.
pub fn read_slice<T: H5Type>(&self, starts: &[usize], counts: &[usize]) -> Result<Vec<T>> {
match &self.info {
DatasetInfo::Reader {
name, element_size, ..
} => {
if T::element_size() != *element_size {
return Err(Hdf5Error::TypeMismatch(format!(
"read type has element size {} but dataset has element size {}",
T::element_size(),
element_size,
)));
}
let starts_u64: Vec<u64> = starts.iter().map(|&s| s as u64).collect();
let counts_u64: Vec<u64> = counts.iter().map(|&c| c as u64).collect();
let raw = {
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Reader(reader) => {
reader.read_slice(name, &starts_u64, &counts_u64)?
}
_ => {
return Err(Hdf5Error::InvalidState("file is not in read mode".into()))
}
}
};
if raw.len() % T::element_size() != 0 {
return Err(Hdf5Error::TypeMismatch(format!(
"raw data size {} is not a multiple of element size {}",
raw.len(),
T::element_size(),
)));
}
let count = raw.len() / T::element_size();
let mut result = Vec::<T>::with_capacity(count);
unsafe {
std::ptr::copy_nonoverlapping(
raw.as_ptr(),
result.as_mut_ptr() as *mut u8,
raw.len(),
);
result.set_len(count);
}
Ok(result)
}
DatasetInfo::Writer { .. } => Err(Hdf5Error::InvalidState(
"cannot read_slice from a dataset in write mode".into(),
)),
}
}
/// Write a typed slice to a sub-region of a contiguous dataset.
///
/// `starts` and `counts` define the N-dimensional selection.
pub fn write_slice<T: H5Type>(
&self,
starts: &[usize],
counts: &[usize],
data: &[T],
) -> Result<()> {
match &self.info {
DatasetInfo::Writer {
index,
element_size,
chunked,
..
} => {
if *chunked {
return Err(Hdf5Error::InvalidState(
"write_slice is only for contiguous datasets".into(),
));
}
if T::element_size() != *element_size {
return Err(Hdf5Error::TypeMismatch(format!(
"write type has element size {} but dataset expects {}",
T::element_size(),
element_size,
)));
}
let expected: usize = counts.iter().product();
if data.len() != expected {
return Err(Hdf5Error::InvalidState(format!(
"data length {} does not match slice size {}",
data.len(),
expected,
)));
}
let starts_u64: Vec<u64> = starts.iter().map(|&s| s as u64).collect();
let counts_u64: Vec<u64> = counts.iter().map(|&c| c as u64).collect();
let byte_len = data.len() * T::element_size();
let raw =
unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, byte_len) };
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Writer(writer) => {
writer.write_slice(*index, &starts_u64, &counts_u64, raw)?;
Ok(())
}
_ => Err(Hdf5Error::InvalidState(
"file is no longer in write mode".into(),
)),
}
}
DatasetInfo::Reader { .. } => {
Err(Hdf5Error::InvalidState("cannot write in read mode".into()))
}
}
}
/// Read variable-length strings from a dataset.
///
/// This handles h5py-style vlen string datasets that store strings
/// as global heap references. Returns one String per element.
pub fn read_vlen_strings(&self) -> Result<Vec<String>> {
match &self.info {
DatasetInfo::Reader { name, .. } => {
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Reader(reader) => Ok(reader.read_vlen_strings(name)?),
_ => Err(Hdf5Error::InvalidState("file is not in read mode".into())),
}
}
DatasetInfo::Writer { .. } => Err(Hdf5Error::InvalidState(
"cannot read vlen strings from a dataset in write mode".into(),
)),
}
}
/// Read the entire dataset as a typed vector.
///
/// The raw bytes are read from the file and reinterpreted as `T`. The
/// caller must ensure that `T` matches the datatype used when the dataset
/// was written.
///
/// # Errors
///
/// Returns an error if:
/// - The file is in write mode.
/// - The raw data size is not a multiple of `T::element_size()`.
pub fn read_raw<T: H5Type>(&self) -> Result<Vec<T>> {
match &self.info {
DatasetInfo::Reader {
name, element_size, ..
} => {
if T::element_size() != *element_size {
return Err(Hdf5Error::TypeMismatch(format!(
"read type has element size {} but dataset has element size {}",
T::element_size(),
element_size,
)));
}
let raw = {
let mut inner = borrow_inner_mut(&self.file_inner);
match &mut *inner {
H5FileInner::Reader(reader) => reader.read_dataset_raw(name)?,
_ => {
return Err(Hdf5Error::InvalidState("file is not in read mode".into()));
}
}
};
if raw.len() % T::element_size() != 0 {
return Err(Hdf5Error::TypeMismatch(format!(
"raw data size {} is not a multiple of element size {}",
raw.len(),
T::element_size(),
)));
}
let count = raw.len() / T::element_size();
let mut result = Vec::<T>::with_capacity(count);
// Safety: T is Copy + 'static (required by H5Type). We verified
// the byte count matches count * size_of::<T>() above.
// copy_nonoverlapping fills the memory with valid bit patterns
// for all H5Type implementors (numeric primitives).
// We call set_len AFTER the copy so that if an unexpected panic
// occurs, uninitialized memory is never exposed.
unsafe {
std::ptr::copy_nonoverlapping(
raw.as_ptr(),
result.as_mut_ptr() as *mut u8,
raw.len(),
);
result.set_len(count);
}
Ok(result)
}
DatasetInfo::Writer { .. } => Err(Hdf5Error::InvalidState(
"cannot read from a dataset in write mode".into(),
)),
}
}
}
#[cfg(test)]
mod tests {
use crate::H5File;
use std::path::PathBuf;
fn temp_path(name: &str) -> PathBuf {
// Include PID + a per-call atomic counter so that concurrent
// cargo invocations and any kernel-level "lock not yet
// released" races between sequential opens cannot collide.
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!(
"hdf5_dataset_test_{}_{}_{}.h5",
name,
std::process::id(),
n
))
}
#[test]
fn builder_requires_shape() {
let path = temp_path("no_shape");
let file = H5File::create(&path).unwrap();
let result = file.new_dataset::<u8>().create("data");
assert!(result.is_err());
std::fs::remove_file(&path).ok();
}
#[test]
fn write_raw_size_mismatch() {
let path = temp_path("size_mismatch");
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<u8>().shape([4]).create("data").unwrap();
// Provide 3 elements instead of 4
let result = ds.write_raw(&[1u8, 2, 3]);
assert!(result.is_err());
std::fs::remove_file(&path).ok();
}
#[test]
fn roundtrip_u8_1d() {
let path = temp_path("rt_u8_1d");
let data: Vec<u8> = (0..10).collect();
{
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<u8>().shape([10]).create("seq").unwrap();
ds.write_raw(&data).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("seq").unwrap();
assert_eq!(ds.shape(), vec![10]);
let readback = ds.read_raw::<u8>().unwrap();
assert_eq!(readback, data);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn roundtrip_i32_2d() {
let path = temp_path("rt_i32_2d");
let data: Vec<i32> = vec![-1, 0, 1, 2, 3, 4];
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([2, 3])
.create("matrix")
.unwrap();
ds.write_raw(&data).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("matrix").unwrap();
assert_eq!(ds.shape(), vec![2, 3]);
let readback = ds.read_raw::<i32>().unwrap();
assert_eq!(readback, data);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn roundtrip_f64_3d() {
let path = temp_path("rt_f64_3d");
let data: Vec<f64> = (0..24).map(|i| i as f64 * 0.5).collect();
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<f64>()
.shape([2, 3, 4])
.create("cube")
.unwrap();
ds.write_raw(&data).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("cube").unwrap();
assert_eq!(ds.shape(), vec![2, 3, 4]);
let readback = ds.read_raw::<f64>().unwrap();
assert_eq!(readback, data);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn cannot_read_in_write_mode() {
let path = temp_path("no_read_write");
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<u8>().shape([4]).create("x").unwrap();
ds.write_raw(&[1u8, 2, 3, 4]).unwrap();
let result = ds.read_raw::<u8>();
assert!(result.is_err());
std::fs::remove_file(&path).ok();
}
#[test]
fn cannot_write_in_read_mode() {
let path = temp_path("no_write_read");
{
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<u8>().shape([4]).create("x").unwrap();
ds.write_raw(&[1u8, 2, 3, 4]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("x").unwrap();
let result = ds.write_raw(&[5u8, 6, 7, 8]);
assert!(result.is_err());
}
std::fs::remove_file(&path).ok();
}
#[test]
fn numeric_attr_roundtrip() {
let path = temp_path("num_attr");
{
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<f32>().shape([4]).create("data").unwrap();
ds.write_raw(&[1.0f32; 4]).unwrap();
let a1 = ds.new_attr::<f64>().shape(()).create("scale").unwrap();
a1.write_numeric(&1.2345f64).unwrap();
let a2 = ds.new_attr::<i32>().shape(()).create("count").unwrap();
a2.write_numeric(&42i32).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("data").unwrap();
let scale = ds.attr("scale").unwrap();
let val: f64 = scale.read_numeric().unwrap();
assert!((val - 1.2345).abs() < 1e-10);
let count = ds.attr("count").unwrap();
let val: i32 = count.read_numeric().unwrap();
assert_eq!(val, 42);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn cannot_create_dataset_in_read_mode() {
let path = temp_path("no_create_read");
{
let _file = H5File::create(&path).unwrap();
}
{
let file = H5File::open(&path).unwrap();
let result = file.new_dataset::<u8>().shape([4]).create("x");
assert!(result.is_err());
}
std::fs::remove_file(&path).ok();
}
#[test]
fn shape_accessor() {
let path = temp_path("shape_acc");
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<f32>()
.shape([5, 10, 3])
.create("tensor")
.unwrap();
assert_eq!(ds.shape(), vec![5, 10, 3]);
std::fs::remove_file(&path).ok();
}
#[test]
fn slice_roundtrip_2d() {
let path = temp_path("slice_2d");
// Create a 4x5 dataset, write full, then read a slice
let data: Vec<i32> = (0..20).collect();
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([4, 5])
.create("mat")
.unwrap();
ds.write_raw(&data).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("mat").unwrap();
// Read rows 1..3, cols 2..4 (2x2 slice)
let slice = ds.read_slice::<i32>(&[1, 2], &[2, 2]).unwrap();
// Row 1: [5,6,7,8,9] -> cols 2..4 = [7,8]
// Row 2: [10,11,12,13,14] -> cols 2..4 = [12,13]
assert_eq!(slice, vec![7, 8, 12, 13]);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn write_slice_2d() {
let path = temp_path("write_slice_2d");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<f32>()
.shape([3, 4])
.create("data")
.unwrap();
ds.write_raw(&[0.0f32; 12]).unwrap();
// Overwrite a 2x2 sub-region
ds.write_slice(&[1, 1], &[2, 2], &[10.0f32, 20.0, 30.0, 40.0])
.unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("data").unwrap();
let full = ds.read_raw::<f32>().unwrap();
// Row 0: [0,0,0,0]
// Row 1: [0,10,20,0]
// Row 2: [0,30,40,0]
assert_eq!(
full,
vec![0.0, 0.0, 0.0, 0.0, 0.0, 10.0, 20.0, 0.0, 0.0, 30.0, 40.0, 0.0,]
);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn write_slice_out_of_bounds_rejected() {
let path = temp_path("write_slice_oob");
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<i32>().shape([4]).create("d").unwrap();
ds.write_raw(&[0i32; 4]).unwrap();
// start 2 + count 6 = 8 > extent 4 -> must error, not corrupt.
assert!(ds.write_slice(&[2], &[6], &[9i32; 6]).is_err());
// An in-bounds slice still works.
assert!(ds.write_slice(&[1], &[2], &[7i32, 8]).is_ok());
std::fs::remove_file(&path).ok();
}
#[test]
fn duplicate_dataset_name_rejected() {
let path = temp_path("dup_name");
let file = H5File::create(&path).unwrap();
let _ = file.new_dataset::<i32>().shape([2]).create("d").unwrap();
assert!(file.new_dataset::<i32>().shape([2]).create("d").is_err());
std::fs::remove_file(&path).ok();
}
#[test]
fn extend_cannot_shrink() {
let path = temp_path("extend_shrink");
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0])
.chunk(&[2])
.max_shape(&[None])
.create("d")
.unwrap();
ds.append(&[1i32, 2, 3, 4]).unwrap();
// Shrinking below the written extent must be rejected.
assert!(ds.extend(&[2]).is_err());
// Growing is fine.
assert!(ds.extend(&[6]).is_ok());
std::fs::remove_file(&path).ok();
}
#[test]
fn attr_read_roundtrip() {
use crate::types::VarLenUnicode;
let path = temp_path("attr_read");
{
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<u8>().shape([4]).create("data").unwrap();
ds.write_raw(&[1u8, 2, 3, 4]).unwrap();
let a1 = ds
.new_attr::<VarLenUnicode>()
.shape(())
.create("units")
.unwrap();
a1.write_string("meters").unwrap();
let a2 = ds
.new_attr::<VarLenUnicode>()
.shape(())
.create("desc")
.unwrap();
a2.write_string("test data").unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("data").unwrap();
let names = ds.attr_names().unwrap();
assert!(names.contains(&"units".to_string()));
assert!(names.contains(&"desc".to_string()));
let units = ds.attr("units").unwrap();
assert_eq!(units.read_string().unwrap(), "meters");
let desc = ds.attr("desc").unwrap();
assert_eq!(desc.read_string().unwrap(), "test data");
}
std::fs::remove_file(&path).ok();
}
#[test]
fn type_mismatch_element_size() {
let path = temp_path("type_mismatch");
{
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<f64>().shape([4]).create("data").unwrap();
ds.write_raw(&[1.0f64, 2.0, 3.0, 4.0]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("data").unwrap();
// Try to read as u8 (element_size = 1) from a f64 dataset (element_size = 8)
let result = ds.read_raw::<u8>();
assert!(result.is_err());
}
std::fs::remove_file(&path).ok();
}
#[test]
fn dataset_survives_file_move() {
let path = temp_path("ds_survives");
let ds = {
let file = H5File::create(&path).unwrap();
file.new_dataset::<u8>().shape([4]).create("x").unwrap()
};
// file is dropped here, but ds still holds Rc to the inner state
ds.write_raw(&[1u8, 2, 3, 4]).unwrap();
// The writer will finalize on drop of the last Rc
std::fs::remove_file(&path).ok();
}
#[test]
fn new_attr_scalar_string() {
use crate::types::VarLenUnicode;
let path = temp_path("attr_scalar_string");
{
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<u8>().shape([4]).create("data").unwrap();
ds.write_raw(&[1u8, 2, 3, 4]).unwrap();
let attr = ds
.new_attr::<VarLenUnicode>()
.shape(())
.create("name")
.unwrap();
attr.write_scalar(&VarLenUnicode("test_value".to_string()))
.unwrap();
file.close().unwrap();
}
// Verify the file is still valid and readable
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("data").unwrap();
assert_eq!(ds.shape(), vec![4]);
let readback = ds.read_raw::<u8>().unwrap();
assert_eq!(readback, vec![1u8, 2, 3, 4]);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn all_numeric_types_roundtrip() {
let path = temp_path("all_types");
{
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<u8>().shape([2]).create("u8").unwrap();
ds.write_raw(&[1u8, 2]).unwrap();
let ds = file.new_dataset::<i8>().shape([2]).create("i8").unwrap();
ds.write_raw(&[-1i8, 1]).unwrap();
let ds = file.new_dataset::<u16>().shape([2]).create("u16").unwrap();
ds.write_raw(&[100u16, 200]).unwrap();
let ds = file.new_dataset::<i16>().shape([2]).create("i16").unwrap();
ds.write_raw(&[-100i16, 100]).unwrap();
let ds = file.new_dataset::<u32>().shape([2]).create("u32").unwrap();
ds.write_raw(&[1000u32, 2000]).unwrap();
let ds = file.new_dataset::<i32>().shape([2]).create("i32").unwrap();
ds.write_raw(&[-1000i32, 1000]).unwrap();
let ds = file.new_dataset::<u64>().shape([2]).create("u64").unwrap();
ds.write_raw(&[10000u64, 20000]).unwrap();
let ds = file.new_dataset::<i64>().shape([2]).create("i64").unwrap();
ds.write_raw(&[-10000i64, 10000]).unwrap();
let ds = file.new_dataset::<f32>().shape([2]).create("f32").unwrap();
ds.write_raw(&[1.5f32, 2.5]).unwrap();
let ds = file.new_dataset::<f64>().shape([2]).create("f64").unwrap();
ds.write_raw(&[1.23456f64, 7.89012]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
assert_eq!(
file.dataset("u8").unwrap().read_raw::<u8>().unwrap(),
vec![1u8, 2]
);
assert_eq!(
file.dataset("i8").unwrap().read_raw::<i8>().unwrap(),
vec![-1i8, 1]
);
assert_eq!(
file.dataset("u16").unwrap().read_raw::<u16>().unwrap(),
vec![100u16, 200]
);
assert_eq!(
file.dataset("i16").unwrap().read_raw::<i16>().unwrap(),
vec![-100i16, 100]
);
assert_eq!(
file.dataset("u32").unwrap().read_raw::<u32>().unwrap(),
vec![1000u32, 2000]
);
assert_eq!(
file.dataset("i32").unwrap().read_raw::<i32>().unwrap(),
vec![-1000i32, 1000]
);
assert_eq!(
file.dataset("u64").unwrap().read_raw::<u64>().unwrap(),
vec![10000u64, 20000]
);
assert_eq!(
file.dataset("i64").unwrap().read_raw::<i64>().unwrap(),
vec![-10000i64, 10000]
);
assert_eq!(
file.dataset("f32").unwrap().read_raw::<f32>().unwrap(),
vec![1.5f32, 2.5]
);
assert_eq!(
file.dataset("f64").unwrap().read_raw::<f64>().unwrap(),
vec![1.23456f64, 7.89012]
);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn append_chunked_roundtrip() {
let path = temp_path("append_chunked");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<f64>()
.shape([0, 3])
.chunk(&[1, 3])
.max_shape(&[None, Some(3)])
.create("data")
.unwrap();
// Append one frame
ds.append(&[1.0f64, 2.0, 3.0]).unwrap();
// Append two frames at once
ds.append(&[4.0f64, 5.0, 6.0, 7.0, 8.0, 9.0]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("data").unwrap();
assert_eq!(ds.shape(), vec![3, 3]);
let all = ds.read_raw::<f64>().unwrap();
assert_eq!(all, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn append_1d_chunked() {
let path = temp_path("append_1d");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0])
.chunk(&[4])
.max_shape(&[None])
.create("values")
.unwrap();
ds.append(&[10i32, 20, 30]).unwrap(); // partial chunk
ds.append(&[40i32]).unwrap(); // fills chunk boundary
ds.append(&[50i32, 60, 70, 80]).unwrap(); // full chunk
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("values").unwrap();
assert_eq!(ds.shape(), vec![8]);
let all = ds.read_raw::<i32>().unwrap();
assert_eq!(all, vec![10, 20, 30, 40, 50, 60, 70, 80]);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn append_partial_chunk_flushed_on_close() {
let path = temp_path("append_partial_close");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<f64>()
.shape([0])
.chunk(&[4])
.max_shape(&[None])
.create("vals")
.unwrap();
// Append 5 elements: chunk 0 = full [1,2,3,4], chunk 1 = partial [5,0,0,0]
ds.append(&[1.0f64, 2.0, 3.0, 4.0, 5.0]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("vals").unwrap();
assert_eq!(ds.shape(), vec![5]);
let all = ds.read_raw::<f64>().unwrap();
// The full dataset is 2 chunks * 4 = 8 elements; shape says 5
// read_raw reads total shape elements
assert_eq!(all.len(), 5);
assert_eq!(all, vec![1.0, 2.0, 3.0, 4.0, 5.0]);
}
std::fs::remove_file(&path).ok();
}
#[cfg(feature = "deflate")]
#[test]
fn vlen_append_after_reopen_filtered() {
// Reopen + append into a partially-written *compressed* vlen chunk
// (index-block chunk). Exercises filtered-index-block reconstruction
// in open_append plus filtered read-modify-write.
let path = temp_path("vlen_reopen_filtered");
{
let file = H5File::create(&path).unwrap();
file.create_appendable_vlen_dataset(
"strs",
4,
Some(crate::format::messages::filter::FilterPipeline::deflate(6)),
)
.unwrap();
file.append_vlen_strings("strs", &["alpha", "beta", "gamma"])
.unwrap();
file.close().unwrap();
}
{
let file = H5File::open_rw(&path).unwrap();
file.append_vlen_strings("strs", &["delta"]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let got = file.dataset("strs").unwrap().read_vlen_strings().unwrap();
assert_eq!(
got.iter().map(|s| s.as_str()).collect::<Vec<_>>(),
vec!["alpha", "beta", "gamma", "delta"]
);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn vlen_append_after_reopen_data_block() {
// Reopen + append into a partial chunk that lives in an extensible-
// array *data block* (chunk index >= idx_blk_elmts). Exercises
// data-block resolution in read_chunk_if_present and write_chunk.
let path = temp_path("vlen_reopen_datablk");
let labels: Vec<String> = (0..9).map(|i| format!("s{i}")).collect();
{
let file = H5File::create(&path).unwrap();
file.create_appendable_vlen_dataset("strs", 2, None)
.unwrap();
let refs: Vec<&str> = labels.iter().map(|s| s.as_str()).collect();
file.append_vlen_strings("strs", &refs).unwrap();
file.close().unwrap();
}
{
let file = H5File::open_rw(&path).unwrap();
file.append_vlen_strings("strs", &["s9"]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let got = file.dataset("strs").unwrap().read_vlen_strings().unwrap();
let want: Vec<String> = (0..10).map(|i| format!("s{i}")).collect();
assert_eq!(got, want);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn vlen_append_after_reopen_super_block() {
// Reopen + append into a partial chunk whose index falls in an
// extensible-array *super block* (chunk index 244 with the default
// EA geometry: idx_blk_elmts=4, data_blk_min_elmts=16,
// sup_blk_min_data_ptrs=4 -> chunks 0..=243 are reached via the
// index block or its direct data blocks, so chunk 244 is reached
// via a super block read from disk). Exercises the ViaSblk branch
// of read_chunk_if_present.
let path = temp_path("vlen_reopen_super");
// 489 strings, chunk size 2 -> chunk 244 holds one string only
// (partially filled) and is flushed to disk on close.
let labels: Vec<String> = (0..489).map(|i| format!("v{i}")).collect();
{
let file = H5File::create(&path).unwrap();
file.create_appendable_vlen_dataset("strs", 2, None)
.unwrap();
let refs: Vec<&str> = labels.iter().map(|s| s.as_str()).collect();
file.append_vlen_strings("strs", &refs).unwrap();
file.close().unwrap();
}
{
let file = H5File::open_rw(&path).unwrap();
file.append_vlen_strings("strs", &["v489"]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let got = file.dataset("strs").unwrap().read_vlen_strings().unwrap();
let want: Vec<String> = (0..490).map(|i| format!("v{i}")).collect();
assert_eq!(got, want);
}
std::fs::remove_file(&path).ok();
}
#[cfg(feature = "deflate")]
#[test]
fn vlen_append_after_reopen_filtered_data_block() {
// The hardest path: compressed + chunk in a data block + partial
// read-modify-write across a reopen.
let path = temp_path("vlen_reopen_filt_datablk");
let labels: Vec<String> = (0..9).map(|i| format!("item{i:02}")).collect();
{
let file = H5File::create(&path).unwrap();
file.create_appendable_vlen_dataset(
"strs",
2,
Some(crate::format::messages::filter::FilterPipeline::deflate(6)),
)
.unwrap();
let refs: Vec<&str> = labels.iter().map(|s| s.as_str()).collect();
file.append_vlen_strings("strs", &refs).unwrap();
file.close().unwrap();
}
{
let file = H5File::open_rw(&path).unwrap();
file.append_vlen_strings("strs", &["item09"]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let got = file.dataset("strs").unwrap().read_vlen_strings().unwrap();
let want: Vec<String> = (0..10).map(|i| format!("item{i:02}")).collect();
assert_eq!(got, want);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn group_nx_class_attribute_roundtrip() {
// Non-root groups carry attributes (NeXus `NX_class`) in their
// own object header, and the reader reads them back by path.
let path = temp_path("group_nx_class");
{
let file = H5File::create(&path).unwrap();
let entry = file.create_group("entry").unwrap();
entry.set_attr_string("NX_class", "NXentry").unwrap();
let det = entry.create_group("detector").unwrap();
det.set_attr_string("NX_class", "NXdetector").unwrap();
det.set_attr_numeric("frame_count", &7i32).unwrap();
det.new_dataset::<f32>()
.shape([4])
.create("data")
.unwrap()
.write_raw(&[1.0f32; 4])
.unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let entry = file.root_group().group("entry").unwrap();
assert_eq!(entry.attr_string("NX_class").unwrap(), "NXentry");
let det = entry.group("detector").unwrap();
assert_eq!(det.attr_string("NX_class").unwrap(), "NXdetector");
let names = det.attr_names().unwrap();
assert!(names.contains(&"NX_class".to_string()));
assert!(names.contains(&"frame_count".to_string()));
}
std::fs::remove_file(&path).ok();
}
#[test]
fn ea_super_block_roundtrip() {
// 2000 chunks span several extensible-array super blocks. Before
// super-block support the writer errored at chunk index 228.
let path = temp_path("ea_super_rt");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0])
.chunk(&[1])
.max_shape(&[None])
.create("v")
.unwrap();
ds.append(&(0..2000).collect::<Vec<i32>>()).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let v = file.dataset("v").unwrap().read_raw::<i32>().unwrap();
assert_eq!(v.len(), 2000);
assert!(v.iter().enumerate().all(|(i, &x)| x == i as i32));
}
std::fs::remove_file(&path).ok();
}
#[cfg(feature = "deflate")]
#[test]
fn ea_filtered_super_block_roundtrip() {
// Compressed chunks across super blocks.
let path = temp_path("ea_filt_super");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0])
.chunk(&[1])
.max_shape(&[None])
.deflate(4)
.create("v")
.unwrap();
ds.append(&(0..600).collect::<Vec<i32>>()).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let v = file.dataset("v").unwrap().read_raw::<i32>().unwrap();
assert_eq!(v, (0..600).collect::<Vec<i32>>());
}
std::fs::remove_file(&path).ok();
}
#[test]
fn ea_super_block_open_append() {
// Reopen a dataset and append chunks that fall in super blocks.
let path = temp_path("ea_super_append");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0])
.chunk(&[1])
.max_shape(&[None])
.create("v")
.unwrap();
ds.append(&(0..300).collect::<Vec<i32>>()).unwrap();
file.close().unwrap();
}
{
let mut w = crate::io::writer::Hdf5Writer::open_append(&path).unwrap();
let idx = w.dataset_index("v").unwrap();
for c in 300..900u64 {
w.write_chunk(idx, c, &(c as i32).to_le_bytes()).unwrap();
}
w.extend_dataset(idx, &[900]).unwrap();
w.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let v = file.dataset("v").unwrap().read_raw::<i32>().unwrap();
assert_eq!(v.len(), 900);
assert!(v.iter().enumerate().all(|(i, &x)| x == i as i32));
}
std::fs::remove_file(&path).ok();
}
#[test]
fn btree_v2_multi_unlimited_roundtrip() {
// A dataset with two unlimited dimensions uses the v2 B-tree chunk
// index; chunks are written by grid coordinates with write_chunk_at.
let path = temp_path("bt2_multi");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0, 0])
.chunk(&[2, 2])
.max_shape(&[None, None])
.create("grid")
.unwrap();
assert!(ds.is_chunked());
// 4x4 logical grid, value[r][c] = r*4 + c, in 2x2 chunks.
for cr in 0..2usize {
for cc in 0..2usize {
let mut bytes = Vec::new();
for i in 0..2usize {
for j in 0..2usize {
let v = ((cr * 2 + i) * 4 + (cc * 2 + j)) as i32;
bytes.extend_from_slice(&v.to_le_bytes());
}
}
ds.write_chunk_at(&[cr, cc], &bytes).unwrap();
}
}
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("grid").unwrap();
assert_eq!(ds.shape(), vec![4, 4]);
assert_eq!(ds.read_raw::<i32>().unwrap(), (0..16).collect::<Vec<i32>>());
}
std::fs::remove_file(&path).ok();
}
#[test]
fn subframe_chunking_roundtrip() {
// A chunk smaller than a frame: shape [N,8,8], chunk [1,4,4], so each
// frame is tiled into a 2x2 grid of 4x4 chunks. write_chunk_at takes
// the chunk-grid coordinates.
let path = temp_path("subframe");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0, 8, 8])
.chunk(&[1, 4, 4])
.max_shape(&[None, Some(8), Some(8)])
.create("v")
.unwrap();
for f in 0..3usize {
for cr in 0..2usize {
for cc in 0..2usize {
let mut bytes = Vec::new();
for i in 0..4usize {
for j in 0..4usize {
let v = (f * 64 + (cr * 4 + i) * 8 + (cc * 4 + j)) as i32;
bytes.extend_from_slice(&v.to_le_bytes());
}
}
ds.write_chunk_at(&[f, cr, cc], &bytes).unwrap();
}
}
}
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("v").unwrap();
assert_eq!(ds.shape(), vec![3, 8, 8]);
assert_eq!(
ds.read_raw::<i32>().unwrap(),
(0..192).collect::<Vec<i32>>()
);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn fill_value_contiguous_roundtrip() {
let path = temp_path("fill_value_contig");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<f32>()
.shape([4])
.fill_value(2.5f32)
.create("data")
.unwrap();
ds.write_raw(&[1.0f32, 2.0, 3.0, 4.0]).unwrap();
file.close().unwrap();
}
// open_append decodes the fill-value message back from the header.
{
let writer = crate::io::writer::Hdf5Writer::open_append(&path).unwrap();
let idx = writer.dataset_index("data").unwrap();
assert_eq!(
writer.datasets[idx].fill_value,
Some(2.5f32.to_le_bytes().to_vec())
);
}
// Data still reads back correctly.
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("data").unwrap();
assert_eq!(ds.read_raw::<f32>().unwrap(), vec![1.0, 2.0, 3.0, 4.0]);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn fill_value_chunked_roundtrip() {
let path = temp_path("fill_value_chunked");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0])
.chunk(&[4])
.max_shape(&[None])
.fill_value(-7i32)
.create("vals")
.unwrap();
ds.append(&[1i32, 2, 3, 4]).unwrap();
file.close().unwrap();
}
{
let writer = crate::io::writer::Hdf5Writer::open_append(&path).unwrap();
let idx = writer.dataset_index("vals").unwrap();
assert_eq!(
writer.datasets[idx].fill_value,
Some((-7i32).to_le_bytes().to_vec())
);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn fill_value_read_missing_chunks() {
// A chunked dataset with chunk 1 left unwritten must read that
// gap back as the user-defined fill value, not zero.
fn i32_bytes(vals: &[i32]) -> Vec<u8> {
vals.iter().flat_map(|v| v.to_le_bytes()).collect()
}
let path = temp_path("fill_value_read_missing");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0])
.chunk(&[2])
.max_shape(&[None])
.fill_value(-1i32)
.create("vals")
.unwrap();
// chunk 0 = [10,20]; chunk 1 unwritten; chunk 2 = [50,60].
ds.write_chunk(0, &i32_bytes(&[10, 20])).unwrap();
ds.write_chunk(2, &i32_bytes(&[50, 60])).unwrap();
ds.extend(&[6]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("vals").unwrap();
let all = ds.read_raw::<i32>().unwrap();
assert_eq!(all, vec![10, 20, -1, -1, 50, 60]);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn fill_value_partial_chunk_padded_with_fill() {
// A partial trailing chunk flushed at close must pad its unwritten
// tail with the fill value. That pad sits beyond the logical shape,
// so it is verified by scanning the on-disk chunk bytes directly.
let path = temp_path("fill_value_partial_pad");
{
let file = H5File::create(&path).unwrap();
let ds = file
.new_dataset::<i32>()
.shape([0])
.chunk(&[4])
.max_shape(&[None])
.fill_value(-9i32)
.create("vals")
.unwrap();
// 3 of 4 frames -> flushed as a partial chunk on close.
ds.append(&[1i32, 2, 3]).unwrap();
file.close().unwrap();
}
let bytes = std::fs::read(&path).unwrap();
// Locate the chunk: i32 LE of [1, 2, 3] written contiguously.
let needle: Vec<u8> = [1i32, 2, 3].iter().flat_map(|v| v.to_le_bytes()).collect();
let pos = bytes
.windows(needle.len())
.position(|w| w == needle)
.expect("chunk data [1,2,3] not found in file");
let pad = &bytes[pos + needle.len()..pos + needle.len() + 4];
assert_eq!(
pad,
&(-9i32).to_le_bytes(),
"partial chunk tail must be padded with fill value -9, got {:?}",
pad
);
std::fs::remove_file(&path).ok();
}
#[test]
fn vlen_append_after_reopen_preserves_existing() {
// Reopening and appending into a partially-written vlen chunk must
// read-modify-write: the strings already on disk must survive.
let path = temp_path("vlen_append_reopen");
{
let file = H5File::create(&path).unwrap();
file.create_appendable_vlen_dataset("strs", 4, None)
.unwrap();
// 3 of 4 frames -> flushed as a partial chunk on close.
file.append_vlen_strings("strs", &["a", "b", "c"]).unwrap();
file.close().unwrap();
}
{
// Append a 4th string -> partial-chunk write into chunk 0.
let file = H5File::open_rw(&path).unwrap();
file.append_vlen_strings("strs", &["d"]).unwrap();
file.close().unwrap();
}
{
let file = H5File::open(&path).unwrap();
let ds = file.dataset("strs").unwrap();
let got = ds.read_vlen_strings().unwrap();
assert_eq!(
got.iter().map(|s| s.as_str()).collect::<Vec<_>>(),
vec!["a", "b", "c", "d"]
);
}
std::fs::remove_file(&path).ok();
}
#[test]
fn fill_value_size_mismatch_errors() {
let path = temp_path("fill_value_mismatch");
let mut writer = crate::io::writer::Hdf5Writer::create(&path).unwrap();
let dt = <f64 as crate::types::H5Type>::hdf5_type();
let idx = writer.create_dataset("d", dt, &[4u64]).unwrap();
// f64 element size is 8; a 4-byte fill value must be rejected.
assert!(writer.set_dataset_fill_value(idx, vec![0u8; 4]).is_err());
// The correct width succeeds.
writer.set_dataset_fill_value(idx, vec![0u8; 8]).unwrap();
writer.close().unwrap();
std::fs::remove_file(&path).ok();
}
}