summaryrefslogtreecommitdiffstats
path: root/tools/src/h5repack/h5repack_opttable.c
blob: 6c409ec98c62121a615d8dc10392ff12357312b3 (plain)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "h5repack.h"
#include "h5tools.h"
#include "h5tools_utils.h"

/*-------------------------------------------------------------------------
 * Function: init_packobject
 *
 * Purpose: initialize a pack_info_t structure
 *
 * Return: void
  *-------------------------------------------------------------------------
 */

void init_packobject(pack_info_t *obj) {
    int j, k;

    HDstrcpy(obj->path, "\0");
    for (j = 0; j < H5_REPACK_MAX_NFILTERS; j++) {
        obj->filter[j].filtn = -1;
        for (k = 0; k < CD_VALUES; k++)
            obj->filter[j].cd_values[k] = 0;
    }
    obj->chunk.rank = -1;
    obj->refobj_id = -1;
    obj->layout = H5D_LAYOUT_ERROR;
    obj->nfilters = 0;
}

/*-------------------------------------------------------------------------
 * Function: aux_tblinsert_filter
 *
 * Purpose: auxiliary function, inserts the filter in object OBJS[ I ]
 *
 * Return: void
 *-------------------------------------------------------------------------
 */

static void aux_tblinsert_filter(pack_opttbl_t *table, unsigned int I, filter_info_t filt) {
    if (table->objs[I].nfilters < H5_REPACK_MAX_NFILTERS)
        table->objs[I].filter[table->objs[I].nfilters++] = filt;
    else
        H5TOOLS_INFO(H5E_tools_min_id_g, "cannot insert the filter in this object. Maximum capacity exceeded");
}

/*-------------------------------------------------------------------------
 * Function: aux_tblinsert_layout
 *
 * Purpose: auxiliary function, inserts the layout in object OBJS[ I ]
 *
 * Return: void
 *-------------------------------------------------------------------------
 */
static void aux_tblinsert_layout(pack_opttbl_t *table, unsigned int I, pack_info_t *pack) {
    int k;

    table->objs[I].layout = pack->layout;
    if (H5D_CHUNKED == pack->layout) {
        /* -2 means the NONE option, remove chunking
        and set the layout to contiguous */
        if (pack->chunk.rank == -2) {
            table->objs[I].layout = H5D_CONTIGUOUS;
            table->objs[I].chunk.rank = -2;
        }
        /* otherwise set the chunking type */
        else {
            table->objs[I].chunk.rank = pack->chunk.rank;
            for (k = 0; k < pack->chunk.rank; k++)
                table->objs[I].chunk.chunk_lengths[k] =
                        pack->chunk.chunk_lengths[k];
        }
    }
}

/*-------------------------------------------------------------------------
 * Function: aux_inctable
 *
 * Purpose: auxiliary function, increases the size of the collection by N_OBJS
 *
 * Return: 0, ok, -1, fail
 *-------------------------------------------------------------------------
 */
static int
aux_inctable(pack_opttbl_t *table, unsigned n_objs)
{
    unsigned u;

    table->size += n_objs;
    table->objs = (pack_info_t*) HDrealloc(table->objs, table->size * sizeof(pack_info_t));
    if (table->objs == NULL) {
        H5TOOLS_INFO(H5E_tools_min_id_g, "not enough memory for options table");
        return -1;
    }

    for (u = table->nelems; u < table->size; u++)
        init_packobject(&table->objs[u]);

    return 0;
}


/*-------------------------------------------------------------------------
 * Function: options_table_init
 *
 * Purpose: init options table
 *
 * Return: 0, ok, -1, fail
  *-------------------------------------------------------------------------
 */
int options_table_init(pack_opttbl_t **tbl) {
    unsigned int i;
    pack_opttbl_t *table;

    if (NULL == (table = (pack_opttbl_t *) HDmalloc(sizeof(pack_opttbl_t)))) {
        H5TOOLS_INFO(H5E_tools_min_id_g, "not enough memory for options table");
        return -1;
    }

    table->size = 30;
    table->nelems = 0;
    if (NULL == (table->objs = (pack_info_t*) HDmalloc(table->size * sizeof(pack_info_t)))) {
        H5TOOLS_INFO(H5E_tools_min_id_g, "not enough memory for options table");
        HDfree(table);
        return -1;
    }

    for (i = 0; i < table->size; i++)
        init_packobject(&table->objs[i]);

    *tbl = table;
    return 0;
}


/*-------------------------------------------------------------------------
 * Function: options_table_free
 *
 * Purpose: free table memory
 *
 * Return: 0
 *-------------------------------------------------------------------------
 */

int options_table_free(pack_opttbl_t *table) {
    HDfree(table->objs);
    HDfree(table);
    return 0;
}

/*-------------------------------------------------------------------------
 * Function: options_add_layout
 *
 * Purpose: add a layout option to the option list
 *
 * Return: 0, ok, -1, fail
  *-------------------------------------------------------------------------
 */
int
options_add_layout(obj_list_t *obj_list, unsigned  n_objs, pack_info_t *pack, pack_opttbl_t *table)
{
    unsigned i, j, I;
    unsigned added = 0;
    hbool_t found = FALSE;

    /* increase the size of the collection by N_OBJS if necessary */
    if (table->nelems + n_objs >= table->size)
        if (aux_inctable(table, n_objs) < 0)
            return -1;

    /* search if this object is already in the table; "path" is the key */
    if (table->nelems > 0) {
        /* go tru the supplied list of names */
        for (j = 0; j < n_objs; j++) {
            /* linear table search */
            for (i = 0; i < table->nelems; i++) {
                /*already on the table */
                if (HDstrcmp(obj_list[j].obj,table->objs[i].path) == 0) {
                    /* already chunk info inserted for this one; exit */
                    if (table->objs[i].chunk.rank > 0) {
                        H5TOOLS_INFO(H5E_tools_min_id_g, "chunk information already inserted for <%s>\n", obj_list[j].obj);
                        HDexit(EXIT_FAILURE);
                    }
                    /* insert the layout info */
                    else {
                        aux_tblinsert_layout(table, i, pack);
                        found = TRUE;
                        break;
                    }
                } /* if */
            } /* i */

            if (!found) {
                /* keep the grow in a temp var */
                I = table->nelems + added;
                added++;
                HDstrcpy(table->objs[I].path, obj_list[j].obj);
                aux_tblinsert_layout(table, I, pack);
            }
            /* cases where we have an already inserted name but there is a new name also
             example:
             -f dset1:GZIP=1 -l dset1,dset2:CHUNK=20x20
             dset1 is already inserted, but dset2 must also be
             */
            else
                if(found && HDstrcmp(obj_list[j].obj,table->objs[i].path) != 0) {
                    /* keep the grow in a temp var */
                    I = table->nelems + added;
                    added++;
                    HDstrcpy(table->objs[I].path, obj_list[j].obj);
                    aux_tblinsert_layout(table, I, pack);
                }
        } /* j */
    }
    /* first time insertion */
    else {
        /* go tru the supplied list of names */
        for (j = 0; j < n_objs; j++) {
            I = table->nelems + added;
            added++;
            HDstrcpy(table->objs[I].path, obj_list[j].obj);
            aux_tblinsert_layout(table, I, pack);
        }
    }

    table->nelems += added;

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: options_add_filter
 *
 * Purpose: add a compression -f option to the option list
 *
 * Return: 0, ok, -1, fail
 *-------------------------------------------------------------------------
 */
int
options_add_filter(obj_list_t *obj_list, unsigned n_objs, filter_info_t filt, pack_opttbl_t *table)
{
    unsigned int i, j, I;
    unsigned added = 0;
    hbool_t found = FALSE;

    /* increase the size of the collection by N_OBJS if necessary */
    if (table->nelems + n_objs >= table->size)
        if (aux_inctable(table, n_objs) < 0)
            return -1;

    /* search if this object is already in the table; "path" is the key */
    if (table->nelems > 0) {
        /* go tru the supplied list of names */
        for (j = 0; j < n_objs; j++) {
            /* linear table search */
            for (i = 0; i < table->nelems; i++) {
                /*already on the table */
                if (HDstrcmp(obj_list[j].obj, table->objs[i].path) == 0) {
                    /* insert */
                    aux_tblinsert_filter(table, i, filt);
                    found = TRUE;
                    break;
                } /* if */
            } /* i */

            if (!found) {
                /* keep the grow in a temp var */
                I = table->nelems + added;
                added++;
                HDstrcpy(table->objs[I].path, obj_list[j].obj);
                aux_tblinsert_filter(table, I, filt);
            }
            /* cases where we have an already inserted name but there is a new name also
             example:
             -l dset1:CHUNK=20x20 -f dset1,dset2:GZIP=1
             dset1 is already inserted, but dset2 must also be
             */
            else
                if(found && HDstrcmp(obj_list[j].obj,table->objs[i].path) != 0) {
                    /* keep the grow in a temp var */
                    I = table->nelems + added;
                    added++;
                    HDstrcpy(table->objs[I].path, obj_list[j].obj);
                    aux_tblinsert_filter(table, I, filt);
                }
        } /* j */
    }

    /* first time insertion */
    else {
        /* go tru the supplied list of names */
        for (j = 0; j < n_objs; j++) {
            I = table->nelems + added;
            added++;
            HDstrcpy(table->objs[I].path, obj_list[j].obj);
            aux_tblinsert_filter(table, I, filt);
        }
    }

    table->nelems += added;

    return 0;
}

/*-------------------------------------------------------------------------
 * Function: options_get_object
 *
 * Purpose: get object from table; "path" is the key
 *
 * Return: pack_info_t* OBJECT or NULL if not found; PATH is the key
 *-------------------------------------------------------------------------
 */

pack_info_t* options_get_object(const char *path, pack_opttbl_t *table) {
    unsigned int i;
    char tbl_path[MAX_NC_NAME + 1]; /* +1 for start with "/" case */

    for (i = 0; i < table->nelems; i++) {
        /* make full path (start with "/") to compare correctly  */
        if (HDstrncmp(table->objs[i].path, "/", 1)) {
            HDstrcpy(tbl_path, "/");
            HDstrcat(tbl_path, table->objs[i].path);
        }
        else
            HDstrcpy(tbl_path, table->objs[i].path);

        /* found it */
        if (HDstrcmp(tbl_path, path) == 0) {
            return (&table->objs[i]);
        }
    }
    return NULL;
}