File: h5lHelperImp.c

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 (493 lines) | stat: -rw-r--r-- 14,557 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
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
/*
 * 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.
 */

#include "hdf5.h"
#include "H5Ppublic.h"
#include "h5jni.h"
#include "jni/h5util.h"

#include <jni.h>
#include <stdlib.h>
#include <string.h>

/*
/////////////////////////////////////////////////////////////////////////////////
//
// H5L and H5O helper methods.
// Add these methods so that we don't need to call H5Lget_info in a Java loop 
// to get information for all the object in a group, which takes
// a lot of time to execute if the number of objects is more than 10,000
//
/////////////////////////////////////////////////////////////////////////////////
*/

/* major and minor error numbers */
typedef struct H5E_num_t {
    hid_t maj_num;
    hid_t min_num;
} H5E_num_t;

/* get the major and minor error numbers on the top of the error stack */
static herr_t
walk_error_callback
    (unsigned n, const H5E_error2_t *err_desc, void *_err_nums)
{
    H5E_num_t *err_nums = (H5E_num_t *)_err_nums;

    if(err_desc) {
        err_nums->maj_num = err_desc->maj_num;
        err_nums->min_num = err_desc->min_num;
    } /* end if */

    return 0;
} /* end walk_error_callback() */

hid_t get_minor_error_number()
{
    H5E_num_t err_nums;
    err_nums.maj_num = 0;
    err_nums.min_num = 0;

    H5Ewalk2(H5E_DEFAULT, H5E_WALK_DOWNWARD, walk_error_callback, &err_nums);

    return err_nums.min_num;
} 

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Lexists
 * Signature: (JLjava/lang/String;J)Z
 */
JNIEXPORT jboolean JNICALL
Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper__1H5Lexists
    (JNIEnv *env, jclass clss, jlong loc_id, jstring name, jlong access_id)
{
    htri_t   bval = JNI_FALSE;
    const char *lName;

    PIN_JAVA_STRING(name, lName);
    if (lName != NULL) {
        bval = H5Lexists((hid_t)loc_id, lName, (hid_t)access_id);

        UNPIN_JAVA_STRING(name, lName);

        if (bval > 0)
            bval = JNI_TRUE;
        else if (bval < 0)
        {
           if (get_minor_error_number() == H5E_NOTFOUND)
           {
               bval = JNI_FALSE;
           } else
           {
               h5libraryError(env);
           }
        }
    }

    return (jboolean)bval;
} /* end Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper__1H5Lexists */

/*
 * Class:     ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper
 * Method:    _H5Lget_link_info
 */
JNIEXPORT jint JNICALL Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper__1H5Lget_1link_1info
  (JNIEnv *env, jclass clss, jlong loc_id, jstring object_name,
    jobjectArray linkName)
{
    jint type;
    herr_t status;
    long minor_err_num;
    char *oName;
   	char *linkval_buf;
    const char *filename;
    const char *obj_path;
    jboolean isCopy;
    jstring str;
    H5L_info_t link_info;
    H5O_info_t obj_info;

    PIN_JAVA_STRING(object_name, oName);

    type = H5Lget_info( (hid_t) loc_id, oName, &link_info, H5P_DEFAULT );

    if (type < 0) 
    {
       UNPIN_JAVA_STRING(object_name, oName);
       h5libraryError(env);
       return -1;
    } else {
       str = NULL;
       if (link_info.type == H5L_TYPE_HARD)
       {
          status = H5Oget_info_by_name1(loc_id, oName, &obj_info, H5P_DEFAULT); 
          UNPIN_JAVA_STRING(object_name, oName);
          if (status  < 0 )
          {
             h5libraryError(env);
             return -1;
          } else {
             type = obj_info.type;
          }
       } else
       {
          type = H5O_TYPE_NTYPES + link_info.type;
          if (linkName != NULL)
          {
             linkval_buf = (char*) malloc(link_info.u.val_size);
             if (linkval_buf == NULL)
             {
                h5outOfMemory(env, "H5Lget_link_info: malloc failed");
                return -1;
             }
             if (H5Lget_val(loc_id, oName, linkval_buf, link_info.u.val_size, H5P_DEFAULT) < 0)
             {
                h5libraryError(env);
                return -1;					
             }
             if (link_info.type == H5L_TYPE_EXTERNAL)
             {
                H5Lunpack_elink_val(linkval_buf, link_info.u.val_size, NULL, &filename, &obj_path);
                str = (*env)->NewStringUTF(env,obj_path);
                (*env)->SetObjectArrayElement(env,linkName,0,(jobject)str);
                str = (*env)->NewStringUTF(env,filename);
                (*env)->SetObjectArrayElement(env,linkName,1,(jobject)str);
             } else /* H5L_TYPE_SYMBOLIC */
             {
                str = (*env)->NewStringUTF(env,linkval_buf);
                (*env)->SetObjectArrayElement(env,linkName,0,(jobject)str);
             }
             free(linkval_buf);
          }
       }
    }

    return (jint)type;
}

typedef struct link_info_all
{
	JNIEnv *env;
    char **name;
    int *type;
    char **linkname;
    char **linkfname;
    void **buf;
    int count;
} link_info_all_t;

herr_t link_names_all(hid_t loc_id, const char *name, const H5L_info_t *link_info, void *opdata)
{
    link_info_all_t* info = (link_info_all_t*)opdata;
    H5O_info_t obj_info;
    
    *(info->name+info->count) = (char *) malloc(strlen(name)+1);
    if (*(info->name+info->count) == NULL)
    {
        h5outOfMemory(info->env, "H5Lget_link_info_all: malloc failed");
        return -1;
    }
    strcpy(*(info->name+info->count), name);
    
    info->count++;

    return 0;
}

herr_t H5Lget_link_names_all( JNIEnv *env, hid_t loc_id, char *group_name, char **names )
{
    link_info_all_t info;
    info.env = env;
    info.name = names;
    info.count = 0;

    if(H5Literate_by_name(loc_id, group_name, H5_INDEX_NAME, H5_ITER_NATIVE, NULL, link_names_all, (void *)&info, H5P_DEFAULT) < 0)
        return -1;

    return 0;
}

/*
 * Class:     ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper
 * Method:    _H5Lget_link_names_all
 */
JNIEXPORT jint JNICALL Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper__1H5Lget_1link_1names_1all
  (JNIEnv *env, jclass clss, jlong loc_id, jstring group_name,
    jobjectArray oname, jint n)
{
    herr_t status;
    char *gName=NULL;
    char **oName=NULL;
    char **lName=NULL;
    jstring str;
    jboolean isCopy;
    int i;

    if (oname == NULL) {
        h5nullArgument( env, "_H5Lget_link_names_all:  oname is NULL");
        return -1;
    }

    PIN_JAVA_STRING(group_name, gName);

    oName = malloc(n * sizeof (*oName));
    if (oName == NULL) {
        UNPIN_JAVA_STRING(group_name, gName);
        h5outOfMemory(env, "_H5Lget_link_names_all: malloc failed");
        return -1;
    }
    for (i=0; i<n; i++) {
        oName[i] = NULL;
    } /* for (i=0; i<n; i++)*/
    status = H5Lget_link_names_all(env, (hid_t) loc_id, gName,  oName);

    UNPIN_JAVA_STRING(group_name, gName);

    if (status < 0) {
        h5str_array_free(oName, n);
        h5libraryError(env);
    } else {
        for (i=0; i<n; i++) {
            if (*(oName+i)) {
                str = (*env)->NewStringUTF(env,*(oName+i));
                (*env)->SetObjectArrayElement(env,oname,i,(jobject)str);
            }
        } /* for (i=0; i<n; i++)*/
        h5str_array_free(oName, n);
    }

    return (jint)status;

}


herr_t link_info_all(hid_t loc_id, const char *name, const H5L_info_t *link_info, void *opdata)
{
    link_info_all_t* info = (link_info_all_t*)opdata;
    H5O_info_t obj_info;
   	char *linkval_buf;
    const char *filename;
    const char *obj_path;
    *(info->name+info->count) = (char *) malloc(strlen(name)+1);
    if (*(info->name+info->count) == NULL)
    {
        h5outOfMemory(info->env, "H5Lget_link_info_all: malloc failed");
        return -1;
    }
    strcpy(*(info->name+info->count), name);
    
    if (link_info->type == H5L_TYPE_HARD)
    {
      if (info->linkname != NULL)
      {
	    	*(info->linkname+info->count) = NULL;
	    	}
	    if ( H5Oget_info_by_name1(loc_id, name, &obj_info, H5P_DEFAULT) < 0 )
	    {
	        *(info->type+info->count) = H5O_TYPE_UNKNOWN;
	    } else {
	        *(info->type+info->count) = obj_info.type;
	    }
	  } else
	  {
      *(info->type+info->count) = H5O_TYPE_NTYPES + link_info->type;
      if (info->linkname != NULL)
      {
	    	linkval_buf = (char*) malloc(link_info->u.val_size);
		    if (linkval_buf == NULL)
		    {
		        h5outOfMemory(info->env, "H5Lget_link_info_all: malloc failed");
		        return -1;
		    }
		    if (H5Lget_val(loc_id, name, linkval_buf, link_info->u.val_size, H5P_DEFAULT) < 0)
		    {
               h5libraryError(info->env);
               free(linkval_buf);
               return -1;	        
		    }
		    if (link_info->type == H5L_TYPE_EXTERNAL)
		    {
                H5Lunpack_elink_val(linkval_buf, link_info->u.val_size, NULL, &filename, &obj_path);
		        *(info->linkname+info->count) = obj_path;
		        *(info->linkfname+info->count) = filename;
		        *(info->buf+info->count) = linkval_buf;
		    } else
		    {
		        *(info->linkname+info->count) = linkval_buf;
		        *(info->linkfname+info->count) = NULL;
		        *(info->buf+info->count) = linkval_buf;
		    }
		  }
    }
    info->count++;

    return 0;
}

herr_t H5Lget_link_info_all( JNIEnv *env, hid_t loc_id, char *group_name, char **names, int *linktypes, char **linknames, char **linkfnames, void **buf )
{
    link_info_all_t info;
    info.env = env;
    info.name = names;
    info.type = linktypes;
    info.linkname = linknames;
    info.linkfname = linkfnames;
    info.buf = buf;
    info.count = 0;

    if(H5Literate_by_name(loc_id, group_name, H5_INDEX_NAME, H5_ITER_NATIVE, NULL, link_info_all, (void *)&info, H5P_DEFAULT) < 0)
        return -1;

    return 0;
}

/*
 * Class:     ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper
 * Method:    _H5Lget_link_info_all
 */
JNIEXPORT jint JNICALL Java_ch_systemsx_cisd_hdf5_hdf5lib_HDFHelper__1H5Lget_1link_1info_1all
  (JNIEnv *env, jclass clss, jlong loc_id, jstring group_name,
    jobjectArray oname, jintArray otype, jobjectArray lname, jobjectArray lfname, jint n)
{
    herr_t status;
    char *gName=NULL;
    char **oName=NULL;
    char **lName=NULL;
    char **lfName=NULL;
    void **buf=NULL;
    jboolean isCopy;
    jstring str;
    jint *tarr;
    int i;

    PIN_JAVA_STRING(group_name, gName);

    if (oname == NULL) {
        h5nullArgument( env, "H5Lget_link_info_all:  oname is NULL");
        return -1;
    }

    if (otype == NULL) {
        h5nullArgument( env, "H5Lget_link_info_all:  otype is NULL");
        return -1;
    }

    if ((lname != NULL && lfname == NULL) || (lname == NULL && lfname != NULL)) {
        h5nullArgument( env, "H5Lget_link_info_all:  lname and lfname either both NULL or both not NULL");
        return -1;
    }

    tarr = (*env)->GetIntArrayElements(env,otype,&isCopy);
    if (tarr == NULL) {
        (*env)->ReleaseStringUTFChars(env,group_name,gName);
        h5JNIFatalError( env, "H5Lget_link_info_all:  type not pinned");
        return -1;
    }

    oName = malloc(n * sizeof (*oName));
    if (oName == NULL) {
        UNPIN_JAVA_STRING(group_name, gName);
        (*env)->ReleaseIntArrayElements(env,otype,tarr,0);
        h5outOfMemory(env, "H5Lget_link_info_all: malloc failed");
        return -1;
    }
    for (i=0; i<n; i++) 
    {
        oName[i] = NULL;
    } /* for (i=0; i<n; i++)*/
    if (lname != NULL)
    {
            lName = malloc(n * sizeof (*lName));
            if (lName == NULL) {
                UNPIN_JAVA_STRING(group_name, gName);
                (*env)->ReleaseIntArrayElements(env,otype,tarr,0);
                h5str_array_free(oName, n);
                h5outOfMemory(env, "H5Lget_link_info_all: malloc failed");
                return -1;
            }
            lfName = malloc(n * sizeof (*lfName));
            if (lfName == NULL) {
                UNPIN_JAVA_STRING(group_name, gName);
                (*env)->ReleaseIntArrayElements(env,otype,tarr,0);
                h5str_array_free(oName, n);
                free(lName);
                h5outOfMemory(env, "H5Lget_link_info_all: malloc failed");
                return -1;
            }
            buf = malloc(n * sizeof (*buf));
            if (buf == NULL) {
                UNPIN_JAVA_STRING(group_name, gName);
                (*env)->ReleaseIntArrayElements(env,otype,tarr,0);
                h5str_array_free(oName, n);
                free(lName);
                free(lfName);
                h5outOfMemory(env, "H5Lget_link_info_all: malloc failed");
                return -1;
            }
            for (i=0; i<n; i++) {
                lName[i] = NULL;
                lfName[i] = NULL;
                buf[i] = NULL;
	    } /* for (i=0; i<n; i++)*/
    }
    status = H5Lget_link_info_all( env, (hid_t) loc_id, gName, oName, (int *)tarr, lName, lfName, buf );

    UNPIN_JAVA_STRING(group_name, gName);
    if (status < 0) {
        (*env)->ReleaseIntArrayElements(env,otype,tarr,JNI_ABORT);
        h5str_array_free(oName, n);
        if (lName != NULL)
        {
            h5str_array_free(lName, n);
            h5str_array_free(lfName, n);
            h5str_array_free(buf, n);
       	}
        h5libraryError(env);
    } else {
        (*env)->ReleaseIntArrayElements(env,otype,tarr,0);

        for (i=0; i<n; i++) {
            if (*(oName+i)) {
                str = (*env)->NewStringUTF(env,*(oName+i));
                (*env)->SetObjectArrayElement(env,oname,i,(jobject)str);
            }
        } /* for (i=0; i<n; i++)*/
        if (lname != NULL)
        {
            for (i=0; i<n; i++) 
            {
                if (*(lName+i)) 
                {
	             str = (*env)->NewStringUTF(env,*(lName+i));
	             (*env)->SetObjectArrayElement(env,lname,i,(jobject)str);
	        }
	        if (*(lfName+i)) 
                {
	            str = (*env)->NewStringUTF(env,*(lfName+i));
	            (*env)->SetObjectArrayElement(env,lfname,i,(jobject)str);
	        }
            } /* for (i=0; i<n; i++)*/
            free(lName);
            free(lfName);
            h5str_array_free(buf, n);
        }
        h5str_array_free(oName, n);
    }

    return (jint)status;

}