File: HDF5AbstractStorageFeatures.java

package info (click to toggle)
libsis-jhdf5-java 19.04.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,668 kB
  • sloc: java: 79,644; ansic: 18,986; sh: 309; makefile: 49; xml: 12
file content (367 lines) | stat: -rw-r--r-- 11,959 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.systemsx.cisd.hdf5;

import hdf.hdf5lib.exceptions.HDF5JavaException;

/**
 * An object representing the storage features that are to be used for a data set.
 * <p>
 * The available storage layouts are {@link HDF5StorageLayout#COMPACT},
 * {@link HDF5StorageLayout#CONTIGUOUS} or {@link HDF5StorageLayout#CHUNKED} can be chosen. Only
 * {@link HDF5StorageLayout#CHUNKED} is extendable and can be compressed.
 * <p>
 * Two types of compressions are supported: <i>deflation</i> (the method used by <code>gzip</code>)
 * and <i>scaling</i>, which can be used if the accuracy of the values are smaller than what the
 * atomic data type can store. Note that <i>scaling</i> in general can be a lossy compression while
 * <i>deflation</i> is always lossless. <i>Scaling</i> compression is only available with HDF5 1.8
 * and newer. Trying to use <i>scaling</i> in strict HDF5 1.6 compatibility mode will throw an
 * {@link IllegalStateException}.
 * <p>
 * For <i>deflation</i> the deflation level can be chosen to get the right balance between speed of
 * compression and compression ratio. Often the {@link #DEFAULT_DEFLATION_LEVEL} will be the right
 * choice.
 * <p>
 * For <i>scaling</i>, the scaling factor can be chosen that determines the accuracy of the values
 * saved. What exactly the scaling factor means, differs between float and integer values.
 * 
 * @author Bernd Rinn
 */
abstract class HDF5AbstractStorageFeatures
{
    /**
     * A constant that specifies that no deflation should be used.
     */
    public final static byte NO_DEFLATION_LEVEL = 0;

    /**
     * A constant that specifies the default deflation level (gzip compression).
     */
    public final static byte DEFAULT_DEFLATION_LEVEL = 6;

    /**
     * A constant that specifies the maximal deflation level (gzip compression).
     */
    public final static byte MAX_DEFLATION_LEVEL = 9;

    /**
     * The policy on how to deal with write access to existing datasets. "Keeping the dataset" means
     * to overwrite the content of the dataset, while "replacing the dataset" refers to deleting the
     * existing dataset and create a new one.
     */
    public enum DataSetReplacementPolicy
    {
        /** Use the default behavior as specified when the writer was created. */
        USE_WRITER_DEFAULT,
        /** Enforce to keep the existing dataset, overwriting the writer's default. */
        ENFORCE_KEEP_EXISTING,
        /** Enforce to replace the existing dataset, overwriting the writer's default. */
        ENFORCE_REPLACE_WITH_NEW
    }

    /**
     * Do not perform any scaling on the data.
     */
    final static byte NO_SCALING_FACTOR = -1;

    static byte toByte(int i)
    {
        final byte b = (byte) i;
        if (b != i)
        {
            throw new HDF5JavaException("Value " + i + " cannot be casted to type byte");
        }
        return b;
    }

    private final byte deflateLevel;

    private final byte scalingFactor;

    private final DataSetReplacementPolicy datasetReplacementPolicy;

    private final HDF5StorageLayout proposedLayoutOrNull;

    private final boolean shuffleBeforeDeflate;

    public abstract static class HDF5AbstractStorageFeatureBuilder
    {
        private byte deflateLevel;

        private byte scalingFactor;

        private HDF5StorageLayout storageLayout;

        private DataSetReplacementPolicy datasetReplacementPolicy =
                DataSetReplacementPolicy.USE_WRITER_DEFAULT;

        private boolean shuffleBeforeDeflate;

        HDF5AbstractStorageFeatureBuilder()
        {
        }

        public HDF5AbstractStorageFeatureBuilder(HDF5AbstractStorageFeatures template)
        {
            deflateLevel(template.getDeflateLevel());
            scalingFactor(template.getScalingFactor());
            storageLayout(template.tryGetProposedLayout());
            datasetReplacementPolicy(template.getDatasetReplacementPolicy());
            shuffleBeforeDeflate(template.isShuffleBeforeDeflate());
        }

        byte getDeflateLevel()
        {
            return deflateLevel;
        }

        byte getScalingFactor()
        {
            return scalingFactor;
        }

        HDF5StorageLayout getStorageLayout()
        {
            return storageLayout;
        }

        DataSetReplacementPolicy getDatasetReplacementPolicy()
        {
            return datasetReplacementPolicy;
        }

        boolean isShuffleBeforeDeflate()
        {
            return shuffleBeforeDeflate;
        }

        public HDF5AbstractStorageFeatureBuilder compress(boolean compress)
        {
            this.deflateLevel = compress ? DEFAULT_DEFLATION_LEVEL : NO_DEFLATION_LEVEL;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder compress()
        {
            this.deflateLevel = DEFAULT_DEFLATION_LEVEL;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder deflateLevel(@SuppressWarnings("hiding")
        byte deflateLevel)
        {
            this.deflateLevel = deflateLevel;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder scalingFactor(@SuppressWarnings("hiding")
        byte scalingFactor)
        {
            this.scalingFactor = scalingFactor;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder noScaling()
        {
            this.scalingFactor = (byte) -1;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder shuffleBeforeDeflate(@SuppressWarnings("hiding")
        boolean shuffleBeforeDeflate)
        {
            this.shuffleBeforeDeflate = shuffleBeforeDeflate;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder shuffleBeforeDeflate()
        {
            this.shuffleBeforeDeflate = true;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder noShuffleBeforeDeflate()
        {
            this.shuffleBeforeDeflate = true;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder storageLayout(@SuppressWarnings("hiding")
        HDF5StorageLayout storageLayout)
        {
            this.storageLayout = storageLayout;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder compactStorageLayout()
        {
            this.storageLayout = HDF5StorageLayout.COMPACT;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder contiguousStorageLayout()
        {
            this.storageLayout = HDF5StorageLayout.CONTIGUOUS;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder chunkedStorageLayout()
        {
            this.storageLayout = HDF5StorageLayout.CHUNKED;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder defaultStorageLayout()
        {
            this.storageLayout = null;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder datasetReplacementPolicy(
                @SuppressWarnings("hiding")
                DataSetReplacementPolicy datasetReplacementPolicy)
        {
            this.datasetReplacementPolicy = datasetReplacementPolicy;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder datasetReplacementUseWriterDefault()
        {
            this.datasetReplacementPolicy = DataSetReplacementPolicy.USE_WRITER_DEFAULT;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder datasetReplacementEnforceKeepExisting()
        {
            this.datasetReplacementPolicy = DataSetReplacementPolicy.ENFORCE_KEEP_EXISTING;
            return this;
        }

        public HDF5AbstractStorageFeatureBuilder datasetReplacementEnforceReplaceWithNew()
        {
            this.datasetReplacementPolicy = DataSetReplacementPolicy.ENFORCE_REPLACE_WITH_NEW;
            return this;
        }

        abstract public HDF5AbstractStorageFeatures features();
    }

    HDF5AbstractStorageFeatures(final HDF5StorageLayout proposedLayoutOrNull,
            final DataSetReplacementPolicy datasetReplacementPolicy, final byte deflateLevel,
            final byte scalingFactor)
    {
        this(proposedLayoutOrNull, datasetReplacementPolicy, false, deflateLevel, scalingFactor);
    }

    HDF5AbstractStorageFeatures(final HDF5StorageLayout proposedLayoutOrNull,
            final DataSetReplacementPolicy datasetReplacementPolicy,
            final boolean shuffleBeforeDeflate, final byte deflateLevel, final byte scalingFactor)
    {
        if (deflateLevel < 0)
        {
            throw new IllegalArgumentException("Invalid deflateLevel " + deflateLevel);
        }
        this.proposedLayoutOrNull = proposedLayoutOrNull;
        this.datasetReplacementPolicy = datasetReplacementPolicy;
        this.shuffleBeforeDeflate = shuffleBeforeDeflate;
        this.deflateLevel = deflateLevel;
        this.scalingFactor = scalingFactor;
    }

    /**
     * Returns true, if this compression setting can be applied on the given <var>dataClassId</var>.
     */
    abstract boolean isCompatibleWithDataClass(int dataClassId);

    /**
     * Returns the proposed storage layout, or <code>null</code>, if no particular storage layout
     * should be proposed.
     */
    public HDF5StorageLayout tryGetProposedLayout()
    {
        return proposedLayoutOrNull;
    }

    /**
     * Returns the policy of this storage feature object regarding replacing or keeping already
     * existing datasets.
     */
    public DataSetReplacementPolicy getDatasetReplacementPolicy()
    {
        return datasetReplacementPolicy;
    }

    boolean requiresChunking()
    {
        return isDeflating() || isScaling() || proposedLayoutOrNull == HDF5StorageLayout.CHUNKED;
    }

    boolean allowsCompact()
    {
        return proposedLayoutOrNull == null || proposedLayoutOrNull == HDF5StorageLayout.COMPACT;
    }

    /**
     * Returns <code>true</code>, if this storage feature object deflates data.
     */
    public boolean isDeflating()
    {
        return (deflateLevel != NO_DEFLATION_LEVEL);
    }

    /**
     * Returns <code>true</code>, if this storage feature object scales data.
     */
    public boolean isScaling()
    {
        return scalingFactor >= 0;
    }

    /**
     * Returns <code>true</code>, if this storage feature object performs shuffling before deflating
     * the data.
     */
    public boolean isShuffleBeforeDeflate()
    {
        return shuffleBeforeDeflate;
    }

    /**
     * Returns the deflate level of this storage feature object. 0 means no deflate.
     */
    public byte getDeflateLevel()
    {
        return deflateLevel;
    }

    /**
     * Returns the scaling factor of this storage feature object. -1 means no scaling, 0 means
     * auto-scaling.
     */
    public byte getScalingFactor()
    {
        return scalingFactor;
    }

    static DataSetReplacementPolicy getDataSetReplacementPolicy(boolean keepDataSetIfExists,
            boolean deleteDataSetIfExists)
    {
        return keepDataSetIfExists ? DataSetReplacementPolicy.ENFORCE_KEEP_EXISTING
                : (deleteDataSetIfExists ? DataSetReplacementPolicy.ENFORCE_REPLACE_WITH_NEW
                        : DataSetReplacementPolicy.USE_WRITER_DEFAULT);
    }
}