forked from saalfeldlab/n5-hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN5HDF5Writer.java
More file actions
749 lines (654 loc) · 26.6 KB
/
Copy pathN5HDF5Writer.java
File metadata and controls
749 lines (654 loc) · 26.6 KB
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
/**
* Copyright (c) 2017, Stephan Saalfeld
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.janelia.saalfeldlab.n5.hdf5;
import ch.systemsx.cisd.base.mdarray.MDArray;
import ch.systemsx.cisd.hdf5.HDF5Factory;
import ch.systemsx.cisd.hdf5.HDF5FloatStorageFeatures;
import ch.systemsx.cisd.hdf5.HDF5GenericStorageFeatures;
import ch.systemsx.cisd.hdf5.HDF5IntStorageFeatures;
import ch.systemsx.cisd.hdf5.IHDF5Writer;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import hdf.hdf5lib.exceptions.HDF5Exception;
import org.janelia.saalfeldlab.n5.Compression;
import org.janelia.saalfeldlab.n5.DataBlock;
import org.janelia.saalfeldlab.n5.DataType;
import org.janelia.saalfeldlab.n5.DatasetAttributes;
import org.janelia.saalfeldlab.n5.GsonN5Writer;
import org.janelia.saalfeldlab.n5.GsonUtils;
import org.janelia.saalfeldlab.n5.N5Exception;
import org.janelia.saalfeldlab.n5.N5URI;
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.RawCompression;
import org.janelia.saalfeldlab.n5.hdf5.N5HDF5Util.OpenDataSetCache.OpenDataSet;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import static hdf.hdf5lib.H5.H5Dget_space;
import static hdf.hdf5lib.H5.H5Dwrite;
import static hdf.hdf5lib.H5.H5Sclose;
import static hdf.hdf5lib.H5.H5Screate_simple;
import static hdf.hdf5lib.H5.H5Sselect_hyperslab;
import static hdf.hdf5lib.HDF5Constants.H5P_DEFAULT;
import static hdf.hdf5lib.HDF5Constants.H5S_SELECT_SET;
import static org.janelia.saalfeldlab.n5.N5Exception.N5IOException;
import static org.janelia.saalfeldlab.n5.hdf5.N5HDF5Util.reorderMultiplyToLong;
import static org.janelia.saalfeldlab.n5.hdf5.N5HDF5Util.reorderToLong;
/**
* Best effort {@link N5Writer} implementation for HDF5 files.
*
* @author Stephan Saalfeld
*/
public class N5HDF5Writer extends N5HDF5Reader implements GsonN5Writer {
protected IHDF5Writer writer;
/**
* Opens an {@link N5HDF5Writer} for a given HDF5 file.
*
* @param writer
* HDF5 writer
* @param overrideBlockSize
* true if you want this {@link N5HDF5Writer} to use the
* defaultBlockSize instead of the chunk-size for reading
* datasets
* @param gsonBuilder
* custom {@link GsonBuilder} to support custom attributes
* @param defaultBlockSize
* for all dimensions > defaultBlockSize.length, and for all
* dimensions with defaultBlockSize[i] <= 0, the size of the
* dataset will be used
* @throws N5Exception
* the exception
*/
public N5HDF5Writer(
final IHDF5Writer writer,
final boolean overrideBlockSize,
final GsonBuilder gsonBuilder,
final int... defaultBlockSize) throws N5Exception {
super(writer, overrideBlockSize, gsonBuilder, defaultBlockSize);
this.writer = writer;
setAttribute("/", VERSION_KEY, N5HDF5Reader.VERSION.toString());
}
/**
* Opens an {@link N5HDF5Writer} for a given HDF5 file.
*
* @param writer
* HDF5 writer
* @param overrideBlockSize
* true if you want this {@link N5HDF5Writer} to use the
* defaultBlockSize instead of the chunk-size for reading
* datasets
* @param defaultBlockSize
* for all dimensions > defaultBlockSize.length, and for all
* dimensions with defaultBlockSize[i] <= 0, the size of the
* dataset will be used
* @throws IOException
* the exception
*/
public N5HDF5Writer(
final IHDF5Writer writer,
final boolean overrideBlockSize,
final int... defaultBlockSize) throws IOException {
this(writer, overrideBlockSize, new GsonBuilder(), defaultBlockSize);
}
/**
* Opens an {@link N5HDF5Writer} for a given HDF5 file.
*
* @param writer
* HDF5 writer
* @param defaultBlockSize
* for all dimensions > defaultBlockSize.length, and for all
* dimensions with defaultBlockSize[i] <= 0, the size of the
* dataset will be used
* @throws IOException
* the exception
*/
public N5HDF5Writer(
final IHDF5Writer writer,
final int... defaultBlockSize) throws IOException {
this(writer, false, defaultBlockSize);
}
/**
* Opens an {@link N5HDF5Writer} for a given HDF5 file.
*
* @param hdf5Path
* HDF5 writer
* @param overrideBlockSize
* true if you want this {@link N5HDF5Writer} to use the
* defaultBlockSize instead of the chunk-size for reading
* datasets
* @param gsonBuilder
* custom {@link GsonBuilder} to support custom attributes
* @param defaultBlockSize
* for all dimensions > defaultBlockSize.length, and for all
* dimensions with defaultBlockSize[i] <= 0, the size of the
* dataset will be used
*/
public N5HDF5Writer(
final String hdf5Path,
final boolean overrideBlockSize,
final GsonBuilder gsonBuilder,
final int... defaultBlockSize) {
this(openHdf5Writer(hdf5Path), overrideBlockSize, gsonBuilder, defaultBlockSize);
}
/**
* Opens an {@link N5HDF5Writer} for a given HDF5 file.
*
* @param hdf5Path
* HDF5 file name
* @param overrideBlockSize
* true if you want this {@link N5HDF5Writer} to use the
* defaultBlockSize instead of the chunk-size for reading
* datasets
* @param defaultBlockSize
* for all dimensions > defaultBlockSize.length, and for all
* dimensions with defaultBlockSize[i] <= 0, the size of the
* dataset will be used
*/
public N5HDF5Writer(
final String hdf5Path,
final boolean overrideBlockSize,
final int... defaultBlockSize) {
this(hdf5Path, overrideBlockSize, new GsonBuilder(), defaultBlockSize);
}
/**
* Opens an {@link N5HDF5Writer} for a given HDF5 file.
*
* @param hdf5Path
* HDF5 file name
* @param defaultBlockSize
* for all dimensions > defaultBlockSize.length, and for all
* dimensions with defaultBlockSize[i] <= 0, the size of the
* dataset will be used
*/
public N5HDF5Writer(
final String hdf5Path,
final int... defaultBlockSize) {
this(hdf5Path, false, defaultBlockSize);
}
@Override
public void createDataset(
final String pathName,
final DatasetAttributes datasetAttributes) throws N5Exception {
final DataType dataType = datasetAttributes.getDataType();
final Compression compression = datasetAttributes.getCompression();
final HDF5IntStorageFeatures intCompression;
final HDF5IntStorageFeatures uintCompression;
final HDF5FloatStorageFeatures floatCompression;
final HDF5GenericStorageFeatures stringCompression;
if (compression instanceof RawCompression) {
floatCompression = HDF5FloatStorageFeatures.FLOAT_NO_COMPRESSION;
intCompression = HDF5IntStorageFeatures.INT_NO_COMPRESSION;
uintCompression = HDF5IntStorageFeatures.INT_NO_COMPRESSION_UNSIGNED;
stringCompression = HDF5GenericStorageFeatures.GENERIC_NO_COMPRESSION;
} else {
floatCompression = HDF5FloatStorageFeatures.FLOAT_SHUFFLE_DEFLATE;
intCompression = HDF5IntStorageFeatures.INT_AUTO_SCALING_DEFLATE;
uintCompression = HDF5IntStorageFeatures.INT_AUTO_SCALING_DEFLATE_UNSIGNED;
stringCompression = HDF5GenericStorageFeatures.GENERIC_DEFLATE;
}
if (writer.exists(pathName))
writer.delete(pathName);
final long[] hdf5Dimensions = datasetAttributes.getDimensions().clone();
reorder(hdf5Dimensions);
final int[] hdf5BlockSize = datasetAttributes.getBlockSize().clone();
reorder(hdf5BlockSize);
switch (dataType) {
case UINT8:
writer.uint8().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, uintCompression);
break;
case UINT16:
writer.uint16().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, uintCompression);
break;
case UINT32:
writer.uint32().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, uintCompression);
break;
case UINT64:
writer.uint64().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, uintCompression);
break;
case INT8:
writer.int8().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, intCompression);
break;
case INT16:
writer.int16().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, intCompression);
break;
case INT32:
writer.int32().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, intCompression);
break;
case INT64:
writer.int64().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, intCompression);
break;
case FLOAT32:
writer.float32().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, floatCompression);
break;
case FLOAT64:
writer.float64().createMDArray(pathName, hdf5Dimensions, hdf5BlockSize, floatCompression);
break;
case STRING:
writer.string().createMDArrayVL(pathName, hdf5Dimensions, hdf5BlockSize, stringCompression);
default:
return;
}
}
@Override
public void createGroup(String pathName) throws N5Exception {
final String normalizedPathName = N5URI.normalizeGroupPath(pathName);
pathName = normalizedPathName.isEmpty() ? "/" : normalizedPathName;
if (writer.exists(pathName)) {
if (!writer.isGroup(pathName))
throw new N5Exception("Group " + pathName + " already exists and is not a group.");
} else
writer.object().createGroup(pathName);
}
@Override
public <T> void setAttribute(
String pathName,
final String key,
final T attribute) throws N5Exception {
final String normalizedPathName = N5URI.normalizeGroupPath(pathName);
final String finalPathName = normalizedPathName.isEmpty() ? "/" : normalizedPathName;
/* Any key that looks like an attribute path is treated as one;
* The only exception are top level elements with a single leading `/` */
final String normalizedAttrPath = N5URI.normalizeAttributePath(key).replaceFirst("^/", "");
final String normalizedKey = normalizedAttrPath.isEmpty() ? "/" : normalizedAttrPath;
if (writer.object().hasAttribute(finalPathName, normalizedKey)) {
writer.object().deleteAttribute(finalPathName, normalizedKey);
}
final boolean isRoot = normalizedKey.equals("/");
/* If setting the root attribute, we need to remove all existing attributes */
if (isRoot) {
writer.object().getAllAttributeNames(finalPathName).forEach(it -> writer.object().deleteAttribute(finalPathName, it));
}
final String[] attributePathTokens = normalizedKey.split("/");
final boolean isPath =
attributePathTokens.length > 2
|| attributePathTokens.length > 1 && !attributePathTokens[0].isEmpty()
|| N5URI.ARRAY_INDEX.asPredicate().test(normalizedKey)
|| containsEscapeCharacters(normalizedKey);
if (isRoot || isPath ) {
writeAttributeAsJson(finalPathName, normalizedKey, attribute);
return;
}
if (!writeHdf5Attribute(finalPathName, normalizedKey, attribute))
writeAttributeAsJson(finalPathName, normalizedKey, attribute);
}
private <T> Boolean writeHdf5Attribute(String pathName, String key, T attribute) {
boolean written = true;
if (attribute instanceof Boolean)
writer.bool().setAttr(pathName, key, (Boolean)attribute);
else if (attribute instanceof Byte)
writer.int8().setAttr(pathName, key, (Byte)attribute);
else if (attribute instanceof Short)
writer.int16().setAttr(pathName, key, (Short)attribute);
else if (attribute instanceof Integer)
writer.int32().setAttr(pathName, key, (Integer)attribute);
else if (attribute instanceof Long)
writer.int64().setAttr(pathName, key, (Long)attribute);
else if (attribute instanceof Float)
writer.float32().setAttr(pathName, key, (Float)attribute);
else if (attribute instanceof Double)
writer.float64().setAttr(pathName, key, (Double)attribute);
else if (attribute instanceof String)
writer.string().setAttr(pathName, key, (String)attribute);
else if (attribute instanceof byte[])
writer.int8().setArrayAttr(pathName, key, (byte[])attribute);
else if (attribute instanceof byte[][])
writer.int8().setMatrixAttr(pathName, key, (byte[][])attribute);
else if (attribute instanceof short[])
writer.int16().setArrayAttr(pathName, key, (short[])attribute);
else if (attribute instanceof short[][])
writer.int16().setMatrixAttr(pathName, key, (short[][])attribute);
else if (attribute instanceof int[])
writer.int32().setArrayAttr(pathName, key, (int[])attribute);
else if (attribute instanceof int[][])
writer.int32().setMatrixAttr(pathName, key, (int[][])attribute);
else if (attribute instanceof long[])
writer.int64().setArrayAttr(pathName, key, (long[])attribute);
else if (attribute instanceof long[][])
writer.int64().setMatrixAttr(pathName, key, (long[][])attribute);
else if (attribute instanceof float[])
writer.float32().setArrayAttr(pathName, key, (float[])attribute);
else if (attribute instanceof float[][])
writer.float32().setMatrixAttr(pathName, key, (float[][])attribute);
else if (attribute instanceof double[])
writer.float64().setArrayAttr(pathName, key, (double[])attribute);
else if (attribute instanceof double[][])
writer.float64().setMatrixAttr(pathName, key, (double[][])attribute);
else if (attribute instanceof String[])
writer.string().setArrayAttr(pathName, key, (String[])attribute);
else
written = false;
return written;
}
private <T> void writeAttributeAsRootJson(String pathName, String key, T attribute) {
/* Get the existing attributes, or create the root if not */
//FIXME: HDF5 doesn't support setting null, so all `null` root elements become `"null"`
// And are indistinguishable from the String `"null"` when deserializing.
writer.string().setAttr(pathName, key, gson.toJson(attribute));
}
private <T> void writeAttributeAsJson(String pathName, String key, T attribute) {
/* Get the existing attributes, or create the root if not */
JsonElement root = null;
if (writer.object().hasAttribute(pathName, N5_JSON_ROOT_KEY)) {
root = JsonParser.parseString(writer.string().getAttr(pathName, N5_JSON_ROOT_KEY));
}
//TODO How to handle writing top-level keys that have existing native keys (such as datasetAtributes)
root = GsonUtils.insertAttribute(root, N5URI.normalizeAttributePath(key), attribute, gson );
writer.string().setAttr(pathName, N5_JSON_ROOT_KEY, gson.toJson(root));
}
@Override
public void setAttributes( String pathName, final Map<String, ?> attributes) throws N5Exception {
final String normalizedPathName = N5URI.normalizeGroupPath(pathName);
pathName = normalizedPathName.isEmpty() ? "/" : normalizedPathName;
for (final Entry<String, ?> attribute : attributes.entrySet()) {
final String key = attribute.getKey();
final Object value = attribute.getValue();
setAttribute(pathName, key, value);
}
}
@Override public void setAttributes(String groupPath, JsonElement attributes) throws N5Exception {
setAttribute(groupPath, "/", attributes);
}
@Override
public void setDatasetAttributes(
final String pathName,
final DatasetAttributes datasetAttributes) throws N5Exception {
throw new UnsupportedOperationException("HDF5 datasets cannot be reshaped.");
}
@Override public boolean removeAttribute(String pathName, String key) throws N5Exception {
final String normalizedPathName = N5URI.normalizeGroupPath(pathName);
pathName = normalizedPathName.isEmpty() ? "/" : normalizedPathName;
if (!exists(pathName)) {
return false;
}
final String normalizedAttrPath = N5URI.normalizeAttributePath(key);
final String normalizedKey = normalizedAttrPath.isEmpty() ? "/" : normalizedAttrPath;
if (writer.object().hasAttribute(pathName, normalizedKey)) {
writer.object().deleteAttribute(pathName, normalizedKey);
return true;
}
if (writer.object().hasAttribute(pathName, N5_JSON_ROOT_KEY)) {
final JsonElement jsonRoot = getAttribute(pathName, N5_JSON_ROOT_KEY, JsonElement.class);
if (GsonUtils.removeAttribute(jsonRoot, N5URI.normalizeAttributePath(normalizedKey)) != null) {
writer.string().setAttr(pathName, N5_JSON_ROOT_KEY, gson.toJson(jsonRoot));
return true;
}
}
return false;
}
@Override public <T> T removeAttribute(String pathName, String key, Class<T> cls) throws N5Exception {
final String normalizedPathName = N5URI.normalizeGroupPath(pathName);
pathName = normalizedPathName.isEmpty() ? "/" : normalizedPathName;
if (!exists(pathName)) {
return null;
}
final String normalizedAttrPath = N5URI.normalizeAttributePath(key);
final String normalizedKey = (normalizedAttrPath.isEmpty() || normalizedAttrPath.equals(N5_JSON_ROOT_KEY)) ? "/" : normalizedAttrPath;
final T removedAttribute = getAttribute(pathName, normalizedKey, cls);
if (removedAttribute != null) {
if (writer.object().hasAttribute(pathName, normalizedKey)) {
writer.object().deleteAttribute(pathName, normalizedKey);
}
if (writer.object().hasAttribute(pathName, N5_JSON_ROOT_KEY)) {
final JsonElement jsonRoot = getAttribute(pathName, N5_JSON_ROOT_KEY, JsonElement.class);
if (GsonUtils.removeAttribute(jsonRoot, N5URI.normalizeAttributePath(normalizedKey), cls, gson) != null) {
writer.string().setAttr(pathName, N5_JSON_ROOT_KEY, gson.toJson(jsonRoot));
}
}
}
return removedAttribute;
}
@Override
public boolean removeAttributes(
String pathName,
final List<String> attributes) throws N5Exception {
final String normalizedPathName = N5URI.normalizeGroupPath(pathName);
pathName = normalizedPathName.isEmpty() ? "/" : normalizedPathName;
if (!exists(pathName)) {
return false;
}
JsonElement jsonRoot = null;
boolean removed = false;
for (String attribute : attributes) {
final String normalizedAttrPath = N5URI.normalizeAttributePath(attribute);
attribute = (normalizedAttrPath.isEmpty() || normalizedAttrPath.equals(N5_JSON_ROOT_KEY)) ? "/" : normalizedAttrPath;
if (writer.object().hasAttribute(pathName, attribute)) {
writer.object().deleteAttribute(pathName, attribute);
removed = true;
continue;
}
if (writer.object().hasAttribute(pathName, N5_JSON_ROOT_KEY) && jsonRoot == null) {
jsonRoot = getAttribute(pathName, N5_JSON_ROOT_KEY, JsonElement.class);
}
if (jsonRoot != null) {
removed |= GsonUtils.removeAttribute(jsonRoot, N5URI.normalizeAttributePath(attribute)) != null;
}
}
if (removed && jsonRoot != null) {
writer.string().setAttr(pathName, N5_JSON_ROOT_KEY, gson.toJson(jsonRoot));
}
return removed;
}
@Override
public <T> void writeBlock(
String pathName,
final DatasetAttributes datasetAttributes,
final DataBlock<T> dataBlock) throws N5Exception {
final String normalizedPathName = N5URI.normalizeGroupPath(pathName);
pathName = normalizedPathName.isEmpty() ? "/" : normalizedPathName;
final long[] hdf5DataBlockSize = reorderToLong(dataBlock.getSize());
final long[] hdf5Offset = reorderMultiplyToLong(dataBlock.getGridPosition(), datasetAttributes.getBlockSize());
if (datasetAttributes.getDataType() == DataType.STRING) {
MDArray<String> arr = new MDArray<>((String[]) dataBlock.getData(), hdf5DataBlockSize);
writer.string().writeMDArrayBlockWithOffset(pathName, arr, hdf5Offset);
return;
}
try (OpenDataSet dataset = openDataSetCache.get(pathName)) {
final long memorySpaceId = H5Screate_simple(hdf5DataBlockSize.length, hdf5DataBlockSize, null);
final long fileSpaceId = H5Dget_space(dataset.dataSetId);
H5Sselect_hyperslab(fileSpaceId, H5S_SELECT_SET, hdf5Offset, null, hdf5DataBlockSize, null);
final long memTypeId = N5HDF5Util.memTypeId(datasetAttributes.getDataType());
H5Dwrite(dataset.dataSetId, memTypeId, memorySpaceId, fileSpaceId, H5P_DEFAULT, dataBlock.getData());
H5Sclose(fileSpaceId);
H5Sclose(memorySpaceId);
}
}
@Override
public <T> void writeRegion(
final String datasetPath,
final DatasetAttributes datasetAttributes,
final long[] min,
final long[] size,
final DataBlockSupplier<T> dataBlocks,
final boolean writeFully) throws N5Exception {
// For HDF5 (no sharding support), iterate through blocks in the region
// and write each one using writeBlock
final int n = min.length;
final int[] blockSize = datasetAttributes.getBlockSize();
// Compute min and max block grid positions
final long[] minBlock = new long[n];
final long[] maxBlock = new long[n];
for (int d = 0; d < n; ++d) {
minBlock[d] = min[d] / blockSize[d];
maxBlock[d] = (min[d] + size[d] - 1) / blockSize[d];
}
// Iterate through all blocks in the region
final long[] gridPosition = minBlock.clone();
while (true) {
// Check if we need to read existing data for this block
final DataBlock<T> existingBlock;
if (writeFully) {
existingBlock = null;
} else {
// Check if block is fully contained in the region
boolean fullyContained = true;
for (int d = 0; d < n; ++d) {
final long blockStart = gridPosition[d] * blockSize[d];
final long blockEnd = blockStart + blockSize[d];
if (blockStart < min[d] || blockEnd > min[d] + size[d]) {
fullyContained = false;
break;
}
}
@SuppressWarnings("unchecked")
final DataBlock<T> readBlockUnchecked = (DataBlock<T>) readBlock(datasetPath, datasetAttributes, gridPosition);
existingBlock = fullyContained ? null : readBlockUnchecked;
}
// Get the new block from the supplier
final DataBlock<T> dataBlock = dataBlocks.get(gridPosition, existingBlock);
if (dataBlock != null) {
writeBlock(datasetPath, datasetAttributes, dataBlock);
}
// Increment grid position
int d = 0;
for (; d < n; ++d) {
if (gridPosition[d] < maxBlock[d]) {
gridPosition[d]++;
break;
}
gridPosition[d] = minBlock[d];
}
if (d == n) break; // Done iterating
}
}
@Override
public <T> void writeRegion(
final String datasetPath,
final DatasetAttributes datasetAttributes,
final long[] min,
final long[] size,
final DataBlockSupplier<T> dataBlocks,
final boolean writeFully,
final java.util.concurrent.ExecutorService executor) throws N5Exception {
// For HDF5 (no sharding support), iterate through blocks in the region
// and write each one using writeBlock, parallelized with executor
final int n = min.length;
final int[] blockSize = datasetAttributes.getBlockSize();
// Compute min and max block grid positions
final long[] minBlock = new long[n];
final long[] maxBlock = new long[n];
for (int d = 0; d < n; ++d) {
minBlock[d] = min[d] / blockSize[d];
maxBlock[d] = (min[d] + size[d] - 1) / blockSize[d];
}
// Iterate through all blocks in the region and submit tasks to executor
final long[] gridPosition = minBlock.clone();
while (true) {
final long[] currentGridPosition = gridPosition.clone();
executor.submit(() -> {
// Check if we need to read existing data for this block
final DataBlock<T> existingBlock;
if (writeFully) {
existingBlock = null;
} else {
// Check if block is fully contained in the region
boolean fullyContained = true;
for (int d = 0; d < n; ++d) {
final long blockStart = currentGridPosition[d] * blockSize[d];
final long blockEnd = blockStart + blockSize[d];
if (blockStart < min[d] || blockEnd > min[d] + size[d]) {
fullyContained = false;
break;
}
}
@SuppressWarnings("unchecked")
final DataBlock<T> readBlockUnchecked = (DataBlock<T>) readBlock(datasetPath, datasetAttributes, currentGridPosition);
existingBlock = fullyContained ? null : readBlockUnchecked;
}
// Get the new block from the supplier (must be thread-safe)
final DataBlock<T> dataBlock = dataBlocks.get(currentGridPosition, existingBlock);
if (dataBlock != null) {
writeBlock(datasetPath, datasetAttributes, dataBlock);
}
});
// Increment grid position
int d = 0;
for (; d < n; ++d) {
if (gridPosition[d] < maxBlock[d]) {
gridPosition[d]++;
break;
}
gridPosition[d] = minBlock[d];
}
if (d == n) break; // Done iterating
}
}
@Override
public boolean deleteBlock(String pathName, final long... gridPosition) throws N5Exception {
// deletion is not supported in HDF5, so the block is overwritten with zeroes instead
if (pathName.equals(""))
pathName = "/";
final DatasetAttributes datasetAttributes = getDatasetAttributes(pathName);
final DataType dataType = datasetAttributes.getDataType();
switch (dataType) {
case UINT8:
case INT8:
case UINT16:
case INT16:
case UINT32:
case INT32:
case UINT64:
case INT64:
case FLOAT32:
case FLOAT64:
final DataBlock<?> empty = dataType.createDataBlock(datasetAttributes.getBlockSize(), gridPosition);
writeBlock(pathName, datasetAttributes, empty);
return true;
default:
return false;
}
}
@Override
public boolean remove() {
openDataSetCache.close();
final File file = writer.file().getFile();
writer.close();
return file.delete();
}
@Override
public boolean remove(String pathName) throws N5Exception {
if (pathName.equals(""))
pathName = "/";
openDataSetCache.remove(pathName);
writer.delete(pathName);
return !writer.exists(pathName);
}
private static IHDF5Writer openHdf5Writer(String hdf5Path) {
final String normalHdf5Path = normalizeHdf5PathLocation(hdf5Path);
if (Files.exists(Paths.get(normalHdf5Path)) && !HDF5Utils.isHDF5(normalHdf5Path)) {
throw new N5Exception("File exists at " + normalHdf5Path + " and is not a valid HDF5 file");
}
try {
return HDF5Factory.open(normalHdf5Path);
} catch (HDF5Exception e) {
throw new N5IOException("Cannot open HDF5 Writer", new IOException(e));
}
}
}