Coverage Report

Created: 2026-11-23 00:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5Dbtree.c
Line
Count
Source
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/* Purpose:     v1 B-tree indexed (chunked) I/O functions.  The chunks are
14
 *              given a multi-dimensional index which is used as a lookup key
15
 *              in a B-tree that maps chunk index to disk address.
16
 */
17
18
/****************/
19
/* Module Setup */
20
/****************/
21
22
#include "H5Dmodule.h" /* This source code file is part of the H5D module */
23
24
/***********/
25
/* Headers */
26
/***********/
27
#include "H5private.h"   /* Generic Functions                    */
28
#include "H5Bprivate.h"  /* B-link trees                         */
29
#include "H5Dpkg.h"      /* Datasets                             */
30
#include "H5Eprivate.h"  /* Error handling                       */
31
#include "H5Fprivate.h"  /* Files                                */
32
#include "H5FDprivate.h" /* File drivers                         */
33
#include "H5FLprivate.h" /* Free Lists                           */
34
#include "H5MFprivate.h" /* File space management                */
35
#include "H5MMprivate.h" /* Memory management                    */
36
#include "H5Oprivate.h"  /* Object headers                       */
37
#include "H5Sprivate.h"  /* Dataspaces                           */
38
#include "H5VMprivate.h" /* Vector and array functions           */
39
40
/****************/
41
/* Local Macros */
42
/****************/
43
44
0
#define H5D_BTREE_IDX_IS_OPEN(idx_info) (NULL != (idx_info)->layout->storage.u.chunk.u.btree.shared)
45
46
/******************/
47
/* Local Typedefs */
48
/******************/
49
50
/*
51
 * B-tree key.  A key contains the minimum logical N-dimensional coordinates and
52
 * the logical size of the chunk to which this key refers.  The
53
 * fastest-varying dimension is assumed to reference individual bytes of the
54
 * array, so a 100-element 1-d array of 4-byte integers would really be a 2-d
55
 * array with the slow varying dimension of size 100 and the fast varying
56
 * dimension of size 4 (the storage dimensionality has very little to do with
57
 * the real dimensionality).
58
 *
59
 * Only the first few values of the OFFSET and SIZE fields are actually
60
 * stored on disk, depending on the dimensionality.
61
 *
62
 * The chunk's file address is part of the B-tree and not part of the key.
63
 */
64
typedef struct H5D_btree_key_t {
65
    hsize_t  scaled[H5O_LAYOUT_NDIMS]; /*logical offset to start */
66
    uint32_t nbytes;                   /*size of stored data */
67
    unsigned filter_mask;              /*excluded filters */
68
} H5D_btree_key_t;
69
70
/* B-tree callback info for iteration over chunks */
71
typedef struct H5D_btree_it_ud_t {
72
    H5D_chunk_common_ud_t common; /* Common info for B-tree user data (must be first) */
73
    H5D_chunk_cb_func_t   cb;     /* Chunk callback routine */
74
    void                 *udata;  /* User data for chunk callback routine */
75
} H5D_btree_it_ud_t;
76
77
/* B-tree callback info for debugging */
78
typedef struct H5D_btree_dbg_t {
79
    H5D_chunk_common_ud_t common; /* Common info for B-tree user data (must be first) */
80
    unsigned              ndims;  /* Number of dimensions */
81
} H5D_btree_dbg_t;
82
83
/********************/
84
/* Local Prototypes */
85
/********************/
86
87
static herr_t H5D__btree_shared_free(void *_shared);
88
static herr_t H5D__btree_shared_create(const H5F_t *f, H5O_storage_chunk_t *store,
89
                                       const H5O_layout_chunk_t *layout);
90
91
/* B-tree iterator callbacks */
92
static int H5D__btree_idx_iterate_cb(H5F_t *f, const void *left_key, haddr_t addr, const void *right_key,
93
                                     void *_udata);
94
95
/* B-tree callbacks */
96
static H5UC_t   *H5D__btree_get_shared(const H5F_t *f, const void *_udata);
97
static herr_t    H5D__btree_new_node(H5F_t *f, H5B_ins_t, void *_lt_key, void *_udata, void *_rt_key,
98
                                     haddr_t *addr_p /*out*/);
99
static int       H5D__btree_cmp2(void *_lt_key, void *_udata, void *_rt_key);
100
static int       H5D__btree_cmp3(void *_lt_key, void *_udata, void *_rt_key);
101
static htri_t    H5D__btree_found(H5F_t *f, haddr_t addr, const void *_lt_key, bool *found, void *_udata);
102
static H5B_ins_t H5D__btree_insert(H5F_t *f, haddr_t addr, void *_lt_key, bool *lt_key_changed, void *_md_key,
103
                                   void *_udata, void *_rt_key, bool *rt_key_changed,
104
                                   haddr_t *new_node /*out*/);
105
static H5B_ins_t H5D__btree_remove(H5F_t *f, haddr_t addr, void *_lt_key, bool *lt_key_changed, void *_udata,
106
                                   void *_rt_key, bool *rt_key_changed);
107
static herr_t    H5D__btree_decode_key(const H5B_shared_t *shared, const uint8_t *raw, void *_key);
108
static herr_t    H5D__btree_encode_key(const H5B_shared_t *shared, uint8_t *raw, const void *_key);
109
static herr_t H5D__btree_debug_key(FILE *stream, int indent, int fwidth, const void *key, const void *udata);
110
111
/* Chunked layout indexing callbacks */
112
static herr_t H5D__btree_idx_init(const H5D_chk_idx_info_t *idx_info, const H5S_t *space,
113
                                  haddr_t dset_ohdr_addr);
114
static herr_t H5D__btree_idx_create(const H5D_chk_idx_info_t *idx_info);
115
static herr_t H5D__btree_idx_open(const H5D_chk_idx_info_t *idx_info);
116
static herr_t H5D__btree_idx_close(const H5D_chk_idx_info_t *idx_info);
117
static herr_t H5D__btree_idx_is_open(const H5D_chk_idx_info_t *idx_info, bool *is_open);
118
static bool   H5D__btree_idx_is_space_alloc(const H5O_storage_chunk_t *storage);
119
static herr_t H5D__btree_idx_insert(const H5D_chk_idx_info_t *idx_info, H5D_chunk_ud_t *udata,
120
                                    const H5D_t *dset);
121
static herr_t H5D__btree_idx_get_addr(const H5D_chk_idx_info_t *idx_info, H5D_chunk_ud_t *udata);
122
static herr_t H5D__btree_idx_load_metadata(const H5D_chk_idx_info_t *idx_info);
123
static int    H5D__btree_idx_iterate(const H5D_chk_idx_info_t *idx_info, H5D_chunk_cb_func_t chunk_cb,
124
                                     void *chunk_udata);
125
static herr_t H5D__btree_idx_remove(const H5D_chk_idx_info_t *idx_info, H5D_chunk_common_ud_t *udata);
126
static herr_t H5D__btree_idx_delete(const H5D_chk_idx_info_t *idx_info);
127
static herr_t H5D__btree_idx_copy_setup(const H5D_chk_idx_info_t *idx_info_src,
128
                                        const H5D_chk_idx_info_t *idx_info_dst);
129
static herr_t H5D__btree_idx_copy_shutdown(H5O_storage_chunk_t *storage_src,
130
                                           H5O_storage_chunk_t *storage_dst);
131
static herr_t H5D__btree_idx_size(const H5D_chk_idx_info_t *idx_info, hsize_t *size);
132
static herr_t H5D__btree_idx_reset(H5O_storage_chunk_t *storage, bool reset_addr);
133
static herr_t H5D__btree_idx_dump(const H5O_storage_chunk_t *storage, FILE *stream);
134
static herr_t H5D__btree_idx_dest(const H5D_chk_idx_info_t *idx_info);
135
136
/*********************/
137
/* Package Variables */
138
/*********************/
139
140
/* v1 B-tree indexed chunk I/O ops */
141
const H5D_chunk_ops_t H5D_COPS_BTREE[1] = {{
142
    false,                         /* v1 B-tree indices does not support SWMR access */
143
    H5D__btree_idx_init,           /* insert */
144
    H5D__btree_idx_create,         /* create */
145
    H5D__btree_idx_open,           /* open */
146
    H5D__btree_idx_close,          /* close */
147
    H5D__btree_idx_is_open,        /* is_open */
148
    H5D__btree_idx_is_space_alloc, /* is_space_alloc */
149
    H5D__btree_idx_insert,         /* insert */
150
    H5D__btree_idx_get_addr,       /* get_addr */
151
    H5D__btree_idx_load_metadata,  /* load_metadata */
152
    NULL,                          /* resize */
153
    H5D__btree_idx_iterate,        /* iterate */
154
    H5D__btree_idx_remove,         /* remove */
155
    H5D__btree_idx_delete,         /* delete */
156
    H5D__btree_idx_copy_setup,     /* copy_setup */
157
    H5D__btree_idx_copy_shutdown,  /* copy_shutdown */
158
    H5D__btree_idx_size,           /* size */
159
    H5D__btree_idx_reset,          /* reset */
160
    H5D__btree_idx_dump,           /* dump */
161
    H5D__btree_idx_dest            /* destroy */
162
}};
163
164
/*****************************/
165
/* Library Private Variables */
166
/*****************************/
167
168
/* inherits B-tree like properties from H5B */
169
static H5B_class_t H5B_BTREE[1] = {{
170
    H5B_CHUNK_ID,            /* id */
171
    sizeof(H5D_btree_key_t), /* sizeof_nkey */
172
    H5D__btree_get_shared,   /* get_shared */
173
    H5D__btree_new_node,     /* new */
174
    H5D__btree_cmp2,         /* cmp2 */
175
    H5D__btree_cmp3,         /* cmp3 */
176
    H5D__btree_found,        /* found */
177
    H5D__btree_insert,       /* insert */
178
    false,                   /* follow min branch? */
179
    false,                   /* follow max branch? */
180
    H5B_LEFT,                /* critical key */
181
    H5D__btree_remove,       /* remove */
182
    H5D__btree_decode_key,   /* decode */
183
    H5D__btree_encode_key,   /* encode */
184
    H5D__btree_debug_key     /* debug */
185
}};
186
187
/*******************/
188
/* Local Variables */
189
/*******************/
190
191
/* Declare a free list to manage H5O_layout_chunk_t objects */
192
H5FL_DEFINE_STATIC(H5O_layout_chunk_t);
193
194
/*-------------------------------------------------------------------------
195
 * Function:    H5D__btree_get_shared
196
 *
197
 * Purpose:     Returns the shared B-tree info for the specified UDATA.
198
 *
199
 * Return:      Success:    Pointer to the raw B-tree page for this dataset
200
 *
201
 *              Failure:    Can't fail
202
 *
203
 *-------------------------------------------------------------------------
204
 */
205
static H5UC_t *
206
H5D__btree_get_shared(const H5F_t H5_ATTR_UNUSED *f, const void *_udata)
207
0
{
208
0
    const H5D_chunk_common_ud_t *udata = (const H5D_chunk_common_ud_t *)_udata;
209
210
0
    FUNC_ENTER_PACKAGE_NOERR
211
212
0
    assert(udata);
213
0
    assert(udata->storage);
214
0
    assert(udata->storage->idx_type == H5D_CHUNK_IDX_BTREE);
215
0
    assert(udata->storage->u.btree.shared);
216
217
    /* Return the pointer to the ref-count object */
218
0
    FUNC_LEAVE_NOAPI(udata->storage->u.btree.shared)
219
0
} /* end H5D__btree_get_shared() */
220
221
/*-------------------------------------------------------------------------
222
 * Function:    H5D__btree_new_node
223
 *
224
 * Purpose:     Adds a new entry to an i-storage B-tree.  We can assume
225
 *              that the domain represented by UDATA doesn't intersect the
226
 *              domain already represented by the B-tree.
227
 *
228
 * Return:      Success:    Non-negative. The address of leaf is returned
229
 *                          through the ADDR argument.  It is also added
230
 *                          to the UDATA.
231
 *
232
 *              Failure:    Negative
233
 *
234
 *-------------------------------------------------------------------------
235
 */
236
static herr_t
237
H5D__btree_new_node(H5F_t H5_ATTR_NDEBUG_UNUSED *f, H5B_ins_t op, void *_lt_key, void *_udata, void *_rt_key,
238
                    haddr_t *addr_p /*out*/)
239
0
{
240
0
    H5D_btree_key_t *lt_key = (H5D_btree_key_t *)_lt_key;
241
0
    H5D_btree_key_t *rt_key = (H5D_btree_key_t *)_rt_key;
242
0
    H5D_chunk_ud_t  *udata  = (H5D_chunk_ud_t *)_udata;
243
0
    unsigned         u;
244
0
    herr_t           ret_value = SUCCEED; /* Return value */
245
246
0
    FUNC_ENTER_PACKAGE_NOERR
247
248
    /* check args */
249
0
    assert(f);
250
0
    assert(lt_key);
251
0
    assert(rt_key);
252
0
    assert(udata);
253
0
    assert(udata->common.layout->ndims > 0 && udata->common.layout->ndims < H5O_LAYOUT_NDIMS);
254
0
    assert(addr_p);
255
256
    /* Set address */
257
0
    assert(H5_addr_defined(udata->chunk_block.offset));
258
0
    assert(udata->chunk_block.length > 0);
259
0
    *addr_p = udata->chunk_block.offset;
260
261
    /*
262
     * The left key describes the storage of the UDATA chunk being
263
     * inserted into the tree.
264
     */
265
0
    H5_CHECKED_ASSIGN(lt_key->nbytes, uint32_t, udata->chunk_block.length, hsize_t);
266
0
    lt_key->filter_mask = udata->filter_mask;
267
0
    for (u = 0; u < udata->common.layout->ndims; u++)
268
0
        lt_key->scaled[u] = udata->common.scaled[u];
269
270
    /*
271
     * The right key might already be present.  If not, then add a zero-width
272
     * chunk.
273
     */
274
0
    if (H5B_INS_LEFT != op) {
275
0
        rt_key->nbytes      = 0;
276
0
        rt_key->filter_mask = 0;
277
0
        for (u = 0; u < udata->common.layout->ndims; u++) {
278
0
            assert(udata->common.scaled[u] + 1 > udata->common.scaled[u]);
279
0
            rt_key->scaled[u] = udata->common.scaled[u] + 1;
280
0
        } /* end if */
281
0
    }     /* end if */
282
283
0
    FUNC_LEAVE_NOAPI(ret_value)
284
0
} /* end H5D__btree_new_node() */
285
286
/*-------------------------------------------------------------------------
287
 * Function:    H5D__btree_cmp2
288
 *
289
 * Purpose:     Compares two keys sort of like strcmp().  The UDATA pointer
290
 *              is only to supply extra information not carried in the keys
291
 *              (in this case, the dimensionality) and is not compared
292
 *              against the keys.
293
 *
294
 * Return:      Success:    -1 if LT_KEY is less than RT_KEY;
295
 *                          1 if LT_KEY is greater than RT_KEY;
296
 *                          0 if LT_KEY and RT_KEY are equal.
297
 *
298
 *              Failure:    FAIL (same as LT_KEY < RT_KEY)
299
 *
300
 *-------------------------------------------------------------------------
301
 */
302
static int
303
H5D__btree_cmp2(void *_lt_key, void *_udata, void *_rt_key)
304
0
{
305
0
    H5D_btree_key_t       *lt_key    = (H5D_btree_key_t *)_lt_key;
306
0
    H5D_btree_key_t       *rt_key    = (H5D_btree_key_t *)_rt_key;
307
0
    H5D_chunk_common_ud_t *udata     = (H5D_chunk_common_ud_t *)_udata;
308
0
    int                    ret_value = -1; /* Return value */
309
310
0
    FUNC_ENTER_PACKAGE_NOERR
311
312
0
    assert(lt_key);
313
0
    assert(rt_key);
314
0
    assert(udata);
315
0
    assert(udata->layout->ndims > 0 && udata->layout->ndims <= H5O_LAYOUT_NDIMS);
316
317
    /* Compare the offsets but ignore the other fields */
318
0
    ret_value = H5VM_vector_cmp_u(udata->layout->ndims, lt_key->scaled, rt_key->scaled);
319
320
0
    FUNC_LEAVE_NOAPI(ret_value)
321
0
} /* end H5D__btree_cmp2() */
322
323
/*-------------------------------------------------------------------------
324
 * Function:    H5D__btree_cmp3
325
 *
326
 * Purpose:     Compare the requested datum UDATA with the left and right
327
 *              keys of the B-tree.
328
 *
329
 * Return:      Success:    negative if the min_corner of UDATA is less
330
 *                          than the min_corner of LT_KEY.
331
 *
332
 *                          positive if the min_corner of UDATA is
333
 *                          greater than or equal the min_corner of
334
 *                          RT_KEY.
335
 *
336
 *                          zero otherwise.  The min_corner of UDATA is
337
 *                          not necessarily contained within the address
338
 *                          space represented by LT_KEY, but a key that
339
 *                          would describe the UDATA min_corner address
340
 *                          would fall lexicographically between LT_KEY
341
 *                          and RT_KEY.
342
 *
343
 *              Failure:    FAIL (same as UDATA < LT_KEY)
344
 *
345
 *-------------------------------------------------------------------------
346
 */
347
static int
348
H5D__btree_cmp3(void *_lt_key, void *_udata, void *_rt_key)
349
0
{
350
0
    H5D_btree_key_t       *lt_key    = (H5D_btree_key_t *)_lt_key;
351
0
    H5D_btree_key_t       *rt_key    = (H5D_btree_key_t *)_rt_key;
352
0
    H5D_chunk_common_ud_t *udata     = (H5D_chunk_common_ud_t *)_udata;
353
0
    int                    ret_value = 0;
354
355
0
    FUNC_ENTER_PACKAGE_NOERR
356
357
0
    assert(lt_key);
358
0
    assert(rt_key);
359
0
    assert(udata);
360
0
    assert(udata->layout->ndims > 0 && udata->layout->ndims <= H5O_LAYOUT_NDIMS);
361
362
    /* Special case for faster checks on 1-D chunks */
363
    /* (Checking for ndims==2 because last dimension is the datatype size) */
364
    /* The additional checking for the right key is necessary due to the */
365
    /* slightly odd way the library initializes the right-most node in the */
366
    /* indexed storage B-tree... */
367
    /* (Dump the B-tree with h5debug to look at it) -QAK */
368
0
    if (udata->layout->ndims == 2) {
369
0
        if (udata->scaled[0] > rt_key->scaled[0])
370
0
            ret_value = 1;
371
0
        else if (udata->scaled[0] == rt_key->scaled[0] && udata->scaled[1] >= rt_key->scaled[1])
372
0
            ret_value = 1;
373
0
        else if (udata->scaled[0] < lt_key->scaled[0])
374
0
            ret_value = (-1);
375
0
    } /* end if */
376
0
    else {
377
0
        if (H5VM_vector_ge_u(udata->layout->ndims, udata->scaled, rt_key->scaled))
378
0
            ret_value = 1;
379
0
        else if (H5VM_vector_lt_u(udata->layout->ndims, udata->scaled, lt_key->scaled))
380
0
            ret_value = (-1);
381
0
    } /* end else */
382
383
0
    FUNC_LEAVE_NOAPI(ret_value)
384
0
} /* end H5D__btree_cmp3() */
385
386
/*-------------------------------------------------------------------------
387
 * Function:    H5D__btree_found
388
 *
389
 * Purpose:     This function is called when the B-tree search engine has
390
 *              found the leaf entry that points to a chunk of storage that
391
 *              contains the beginning of the logical address space
392
 *              represented by UDATA.  The LT_KEY is the left key (the one
393
 *              that describes the chunk) and RT_KEY is the right key (the
394
 *              one that describes the next or last chunk).
395
 *
396
 * Note:        It's possible that the chunk isn't really found.  For
397
 *              instance, in a sparse dataset the requested chunk might fall
398
 *              between two stored chunks in which case this function is
399
 *              called with the maximum stored chunk indices less than the
400
 *              requested chunk indices.
401
 *
402
 * Return:      Non-negative on success with information about the
403
 *              chunk returned through the UDATA argument, if *FOUND is
404
 *              true.
405
 *              Negative on failure.
406
 *
407
 *-------------------------------------------------------------------------
408
 */
409
static htri_t
410
H5D__btree_found(H5F_t H5_ATTR_UNUSED *f, haddr_t addr, const void *_lt_key, bool *found, void *_udata)
411
0
{
412
0
    H5D_chunk_ud_t        *udata  = (H5D_chunk_ud_t *)_udata;
413
0
    const H5D_btree_key_t *lt_key = (const H5D_btree_key_t *)_lt_key;
414
0
    unsigned               u;
415
0
    herr_t                 ret_value = SUCCEED; /* Return value */
416
417
0
    FUNC_ENTER_PACKAGE_NOERR
418
419
    /* Check arguments */
420
0
    assert(f);
421
0
    assert(H5_addr_defined(addr));
422
0
    assert(lt_key);
423
0
    assert(found);
424
0
    assert(udata);
425
426
    /* Is this *really* the requested chunk? */
427
0
    for (u = 0; u < udata->common.layout->ndims; u++)
428
0
        if (udata->common.scaled[u] >= (lt_key->scaled[u] + 1)) {
429
0
            *found = false;
430
0
            HGOTO_DONE(SUCCEED);
431
0
        }
432
433
    /* Initialize return values */
434
0
    assert(lt_key->nbytes > 0);
435
0
    udata->chunk_block.offset = addr;
436
0
    udata->chunk_block.length = lt_key->nbytes;
437
0
    udata->filter_mask        = lt_key->filter_mask;
438
0
    *found                    = true;
439
440
0
done:
441
0
    FUNC_LEAVE_NOAPI(ret_value)
442
0
} /* end H5D__btree_found() */
443
444
/*-------------------------------------------------------------------------
445
 * Function:    H5D__chunk_disjoint
446
 *
447
 * Purpose:     Determines if two chunks are disjoint.
448
 *
449
 * Return:      Success:    false if they are not disjoint.
450
 *                          true if they are disjoint.
451
 *
452
 * Note:        Assumes that the chunk offsets are scaled coordinates
453
 *
454
 *-------------------------------------------------------------------------
455
 */
456
static bool
457
H5D__chunk_disjoint(unsigned n, const hsize_t *scaled1, const hsize_t *scaled2)
458
0
{
459
0
    unsigned u;                 /* Local index variable */
460
0
    bool     ret_value = false; /* Return value */
461
462
0
    FUNC_ENTER_PACKAGE_NOERR
463
464
    /* Sanity checks */
465
0
    assert(n);
466
0
    assert(scaled1);
467
0
    assert(scaled2);
468
469
    /* Loop over two chunks, detecting disjointedness and getting out quickly */
470
0
    for (u = 0; u < n; u++)
471
0
        if ((scaled1[u] + 1) <= scaled2[u] || (scaled2[u] + 1) <= scaled1[u])
472
0
            HGOTO_DONE(true);
473
474
0
done:
475
0
    FUNC_LEAVE_NOAPI(ret_value)
476
0
} /* end H5D__chunk_disjoint() */
477
478
/*-------------------------------------------------------------------------
479
 * Function:    H5D__btree_insert
480
 *
481
 * Purpose:     This function is called when the B-tree insert engine finds
482
 *              the node to use to insert new data.  The UDATA argument
483
 *              points to a struct that describes the logical addresses being
484
 *              added to the file.  This function allocates space for the
485
 *              data and returns information through UDATA describing a
486
 *              file chunk to receive (part of) the data.
487
 *
488
 *              The LT_KEY is always the key describing the chunk of file
489
 *              memory at address ADDR. On entry, UDATA describes the logical
490
 *              addresses for which storage is being requested (through the
491
 *              `offset' and `size' fields). On return, UDATA describes the
492
 *              logical addresses contained in a chunk on disk.
493
 *
494
 * Return:      Success:    An insertion command for the caller, one of
495
 *                          the H5B_INS_* constants.  The address of the
496
 *                          new chunk is returned through the NEW_NODE
497
 *                          argument.
498
 *
499
 *              Failure:    H5B_INS_ERROR
500
 *
501
 *-------------------------------------------------------------------------
502
 */
503
static H5B_ins_t
504
H5D__btree_insert(H5F_t H5_ATTR_NDEBUG_UNUSED *f, haddr_t H5_ATTR_NDEBUG_UNUSED addr, void *_lt_key,
505
                  bool *lt_key_changed, void *_md_key, void *_udata, void *_rt_key,
506
                  bool H5_ATTR_UNUSED *rt_key_changed, haddr_t *new_node_p /*out*/)
507
0
{
508
0
    H5D_btree_key_t *lt_key = (H5D_btree_key_t *)_lt_key;
509
0
    H5D_btree_key_t *md_key = (H5D_btree_key_t *)_md_key;
510
0
    H5D_btree_key_t *rt_key = (H5D_btree_key_t *)_rt_key;
511
0
    H5D_chunk_ud_t  *udata  = (H5D_chunk_ud_t *)_udata;
512
0
    int              cmp;
513
0
    unsigned         u;
514
0
    H5B_ins_t        ret_value = H5B_INS_ERROR; /* Return value */
515
516
0
    FUNC_ENTER_PACKAGE
517
518
    /* check args */
519
0
    assert(f);
520
0
    assert(H5_addr_defined(addr));
521
0
    assert(lt_key);
522
0
    assert(lt_key_changed);
523
0
    assert(md_key);
524
0
    assert(udata);
525
0
    assert(rt_key);
526
0
    assert(new_node_p);
527
528
0
    cmp = H5D__btree_cmp3(lt_key, udata, rt_key);
529
0
    assert(cmp <= 0);
530
531
0
    if (cmp < 0) {
532
        /* Negative indices not supported yet */
533
0
        HGOTO_ERROR(H5E_STORAGE, H5E_UNSUPPORTED, H5B_INS_ERROR, "internal error");
534
0
    }
535
0
    else if (H5VM_vector_eq_u(udata->common.layout->ndims, udata->common.scaled, lt_key->scaled) &&
536
0
             lt_key->nbytes > 0) {
537
        /*
538
         * Already exists.  If the new size is not the same as the old size
539
         * then we should reallocate storage.
540
         */
541
0
        if (lt_key->nbytes != udata->chunk_block.length) {
542
            /* Set node's address (already re-allocated by main chunk routines) */
543
0
            assert(H5_addr_defined(udata->chunk_block.offset));
544
0
            *new_node_p = udata->chunk_block.offset;
545
0
            H5_CHECKED_ASSIGN(lt_key->nbytes, uint32_t, udata->chunk_block.length, hsize_t);
546
0
            lt_key->filter_mask = udata->filter_mask;
547
0
            *lt_key_changed     = true;
548
0
            ret_value           = H5B_INS_CHANGE;
549
0
        }
550
0
        else {
551
            /* Already have address in udata, from main chunk routines */
552
0
            assert(H5_addr_defined(udata->chunk_block.offset));
553
0
            ret_value = H5B_INS_NOOP;
554
0
        }
555
0
    }
556
0
    else if (H5D__chunk_disjoint(udata->common.layout->ndims, lt_key->scaled, udata->common.scaled)) {
557
0
        assert(H5D__chunk_disjoint(udata->common.layout->ndims, rt_key->scaled, udata->common.scaled));
558
        /*
559
         * Split this node, inserting the new new node to the right of the
560
         * current node.  The MD_KEY is where the split occurs.
561
         */
562
0
        H5_CHECKED_ASSIGN(md_key->nbytes, uint32_t, udata->chunk_block.length, hsize_t);
563
0
        md_key->filter_mask = udata->filter_mask;
564
0
        for (u = 0; u < udata->common.layout->ndims; u++)
565
0
            md_key->scaled[u] = udata->common.scaled[u];
566
567
0
        assert(H5_addr_defined(udata->chunk_block.offset));
568
0
        *new_node_p = udata->chunk_block.offset;
569
0
        ret_value   = H5B_INS_RIGHT;
570
0
    }
571
0
    else {
572
0
        HGOTO_ERROR(H5E_IO, H5E_UNSUPPORTED, H5B_INS_ERROR, "internal error");
573
0
    }
574
575
0
done:
576
0
    FUNC_LEAVE_NOAPI(ret_value)
577
0
} /* end H5D__btree_insert() */
578
579
/*-------------------------------------------------------------------------
580
 * Function:    H5D__btree_remove
581
 *
582
 * Purpose:     Removes chunks that are no longer necessary in the B-tree.
583
 *
584
 * Return:      Non-negative on success/Negative on failure
585
 *
586
 *-------------------------------------------------------------------------
587
 */
588
static H5B_ins_t
589
H5D__btree_remove(H5F_t *f, haddr_t addr, void *_lt_key /*in,out */, bool *lt_key_changed /*out */,
590
                  void H5_ATTR_UNUSED *_udata /*in,out */, void H5_ATTR_UNUSED *_rt_key /*in,out */,
591
                  bool *rt_key_changed /*out */)
592
0
{
593
0
    H5D_btree_key_t *lt_key    = (H5D_btree_key_t *)_lt_key;
594
0
    H5B_ins_t        ret_value = H5B_INS_REMOVE; /* Return value */
595
596
0
    FUNC_ENTER_PACKAGE
597
598
    /* Remove raw data chunk from file */
599
0
    H5_CHECK_OVERFLOW(lt_key->nbytes, uint32_t, hsize_t);
600
0
    if (H5MF_xfree(f, H5FD_MEM_DRAW, addr, (hsize_t)lt_key->nbytes) < 0)
601
0
        HGOTO_ERROR(H5E_STORAGE, H5E_CANTFREE, H5B_INS_ERROR, "unable to free chunk");
602
603
    /* Mark keys as unchanged */
604
0
    *lt_key_changed = false;
605
0
    *rt_key_changed = false;
606
607
0
done:
608
0
    FUNC_LEAVE_NOAPI(ret_value)
609
0
} /* end H5D__btree_remove() */
610
611
/*-------------------------------------------------------------------------
612
 * Function:    H5D__btree_decode_key
613
 *
614
 * Purpose:     Decodes a raw key into a native key for the B-tree
615
 *
616
 * Return:      SUCCEED/FAIL
617
 *-------------------------------------------------------------------------
618
 */
619
static herr_t
620
H5D__btree_decode_key(const H5B_shared_t *shared, const uint8_t *raw, void *_key)
621
0
{
622
0
    const H5O_layout_chunk_t *layout;                        /* Chunk layout description */
623
0
    H5D_btree_key_t          *key = (H5D_btree_key_t *)_key; /* Pointer to decoded key */
624
0
    hsize_t                   tmp_offset;                    /* Temporary coordinate offset, from file */
625
0
    herr_t                    ret_value = SUCCEED;
626
627
0
    FUNC_ENTER_PACKAGE
628
629
0
    assert(shared);
630
0
    assert(raw);
631
0
    assert(key);
632
0
    layout = (const H5O_layout_chunk_t *)shared->udata;
633
0
    assert(layout);
634
635
0
    if (layout->ndims > H5O_LAYOUT_NDIMS)
636
0
        HGOTO_ERROR(H5E_DATASET, H5E_BADVALUE, FAIL, "bad number of dimensions");
637
638
0
    UINT32DECODE(raw, key->nbytes);
639
0
    UINT32DECODE(raw, key->filter_mask);
640
0
    for (unsigned u = 0; u < layout->ndims; u++) {
641
0
        if (layout->dim[u] == 0)
642
0
            HGOTO_ERROR(H5E_DATASET, H5E_BADVALUE, FAIL, "chunk size must be > 0, dim = %u ", u);
643
644
        /* Retrieve coordinate offset */
645
0
        UINT64DECODE(raw, tmp_offset);
646
0
        if (0 != (tmp_offset % layout->dim[u]))
647
0
            HGOTO_ERROR(H5E_DATASET, H5E_BADVALUE, FAIL, "bad coordinate offset");
648
649
        /* Convert to a scaled offset */
650
0
        key->scaled[u] = tmp_offset / layout->dim[u];
651
0
    }
652
653
0
done:
654
0
    FUNC_LEAVE_NOAPI(ret_value)
655
0
} /* end H5D__btree_decode_key() */
656
657
/*-------------------------------------------------------------------------
658
 * Function:    H5D__btree_encode_key
659
 *
660
 * Purpose:     Encode a key from native format to raw format.
661
 *
662
 * Return:      Non-negative on success/Negative on failure
663
 *
664
 *-------------------------------------------------------------------------
665
 */
666
static herr_t
667
H5D__btree_encode_key(const H5B_shared_t *shared, uint8_t *raw, const void *_key)
668
0
{
669
0
    const H5O_layout_chunk_t *layout; /* Chunk layout description */
670
0
    const H5D_btree_key_t    *key = (const H5D_btree_key_t *)_key;
671
0
    hsize_t                   tmp_offset; /* Temporary coordinate offset, from file */
672
0
    unsigned                  u;          /* Local index variable */
673
674
0
    FUNC_ENTER_PACKAGE_NOERR
675
676
    /* check args */
677
0
    assert(shared);
678
0
    assert(raw);
679
0
    assert(key);
680
0
    layout = (const H5O_layout_chunk_t *)shared->udata;
681
0
    assert(layout);
682
0
    assert(layout->ndims > 0 && layout->ndims <= H5O_LAYOUT_NDIMS);
683
684
    /* encode */
685
0
    UINT32ENCODE(raw, key->nbytes);
686
0
    UINT32ENCODE(raw, key->filter_mask);
687
0
    for (u = 0; u < layout->ndims; u++) {
688
        /* Compute coordinate offset from scaled offset */
689
0
        tmp_offset = key->scaled[u] * layout->dim[u];
690
0
        UINT64ENCODE(raw, tmp_offset);
691
0
    } /* end for */
692
693
0
    FUNC_LEAVE_NOAPI(SUCCEED)
694
0
} /* end H5D__btree_encode_key() */
695
696
/*-------------------------------------------------------------------------
697
 * Function:    H5D__btree_debug_key
698
 *
699
 * Purpose:     Prints a key.
700
 *
701
 * Return:      Non-negative on success/Negative on failure
702
 *
703
 *-------------------------------------------------------------------------
704
 */
705
static herr_t
706
H5D__btree_debug_key(FILE *stream, int indent, int fwidth, const void *_key, const void *_udata)
707
0
{
708
0
    const H5D_btree_key_t *key   = (const H5D_btree_key_t *)_key;
709
0
    const H5D_btree_dbg_t *udata = (const H5D_btree_dbg_t *)_udata;
710
0
    unsigned               u;
711
712
0
    FUNC_ENTER_PACKAGE_NOERR
713
714
0
    assert(key);
715
716
0
    fprintf(stream, "%*s%-*s %u bytes\n", indent, "", fwidth, "Chunk size:", (unsigned)key->nbytes);
717
0
    fprintf(stream, "%*s%-*s 0x%08x\n", indent, "", fwidth, "Filter mask:", key->filter_mask);
718
0
    fprintf(stream, "%*s%-*s {", indent, "", fwidth, "Logical offset:");
719
0
    for (u = 0; u < udata->ndims; u++)
720
0
        fprintf(stream, "%s%" PRIuHSIZE, u ? ", " : "", (key->scaled[u] * udata->common.layout->dim[u]));
721
0
    fputs("}\n", stream);
722
723
0
    FUNC_LEAVE_NOAPI(SUCCEED)
724
0
} /* end H5D__btree_debug_key() */
725
726
/*-------------------------------------------------------------------------
727
 * Function:    H5D__btree_shared_free
728
 *
729
 * Purpose:     Free "local" B-tree shared info
730
 *
731
 * Return:      Non-negative on success/Negative on failure
732
 *
733
 *-------------------------------------------------------------------------
734
 */
735
static herr_t
736
H5D__btree_shared_free(void *_shared)
737
0
{
738
0
    H5B_shared_t *shared    = (H5B_shared_t *)_shared;
739
0
    herr_t        ret_value = SUCCEED; /* Return value */
740
741
0
    FUNC_ENTER_PACKAGE
742
743
    /* Free the chunk layout information */
744
0
    shared->udata = H5FL_FREE(H5O_layout_chunk_t, shared->udata);
745
746
    /* Chain up to the generic B-tree shared info free routine */
747
0
    if (H5B_shared_free(shared) < 0)
748
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTFREE, FAIL, "can't free shared B-tree info");
749
750
0
done:
751
0
    FUNC_LEAVE_NOAPI(ret_value)
752
0
} /* end H5D__btree_shared_free() */
753
754
/*-------------------------------------------------------------------------
755
 * Function:    H5D__btree_shared_create
756
 *
757
 * Purpose:     Create & initialize B-tree shared info
758
 *
759
 * Return:      Non-negative on success/Negative on failure
760
 *
761
 *-------------------------------------------------------------------------
762
 */
763
static herr_t
764
H5D__btree_shared_create(const H5F_t *f, H5O_storage_chunk_t *store, const H5O_layout_chunk_t *layout)
765
0
{
766
0
    H5B_shared_t       *shared;              /* Shared B-tree node info */
767
0
    H5O_layout_chunk_t *my_layout = NULL;    /* Pointer to copy of layout info */
768
0
    size_t              sizeof_rkey;         /* Size of raw (disk) key       */
769
0
    herr_t              ret_value = SUCCEED; /* Return value */
770
771
0
    FUNC_ENTER_PACKAGE
772
773
    /* Set the raw key size */
774
0
    sizeof_rkey = 4 +                /*storage size   */
775
0
                  4 +                /*filter mask    */
776
0
                  layout->ndims * 8; /*dimension indices  */
777
778
    /* Allocate & initialize global info for the shared structure */
779
0
    if (NULL == (shared = H5B_shared_new(f, H5B_BTREE, sizeof_rkey)))
780
0
        HGOTO_ERROR(H5E_DATASET, H5E_NOSPACE, FAIL, "memory allocation failed for shared B-tree info");
781
782
    /* Set up the "local" information for this dataset's chunks */
783
0
    if (NULL == (my_layout = H5FL_MALLOC(H5O_layout_chunk_t)))
784
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTALLOC, FAIL, "can't allocate chunk layout");
785
0
    H5MM_memcpy(my_layout, layout, sizeof(H5O_layout_chunk_t));
786
0
    shared->udata = my_layout;
787
788
    /* Make shared B-tree info reference counted */
789
0
    if (NULL == (store->u.btree.shared = H5UC_create(shared, H5D__btree_shared_free)))
790
0
        HGOTO_ERROR(H5E_DATASET, H5E_NOSPACE, FAIL, "can't create ref-count wrapper for shared B-tree info");
791
792
0
done:
793
0
    if (ret_value < 0)
794
0
        if (my_layout)
795
0
            my_layout = H5FL_FREE(H5O_layout_chunk_t, my_layout);
796
797
0
    FUNC_LEAVE_NOAPI(ret_value)
798
0
} /* end H5D__btree_shared_create() */
799
800
/*-------------------------------------------------------------------------
801
 * Function:    H5D__btree_idx_init
802
 *
803
 * Purpose:     Initialize the indexing information for a dataset.
804
 *
805
 * Return:      Non-negative on success/Negative on failure
806
 *
807
 *-------------------------------------------------------------------------
808
 */
809
static herr_t
810
H5D__btree_idx_init(const H5D_chk_idx_info_t *idx_info, const H5S_t H5_ATTR_UNUSED *space,
811
                    haddr_t dset_ohdr_addr)
812
0
{
813
0
    herr_t ret_value = SUCCEED; /* Return value */
814
815
0
    FUNC_ENTER_PACKAGE
816
817
    /* Check args */
818
0
    assert(idx_info);
819
0
    assert(idx_info->f);
820
0
    assert(idx_info->pline);
821
0
    assert(idx_info->layout);
822
0
    assert(H5_addr_defined(dset_ohdr_addr));
823
824
0
    idx_info->layout->storage.u.chunk.u.btree.dset_ohdr_addr = dset_ohdr_addr;
825
826
    /* Allocate the shared structure */
827
0
    if (H5D__btree_shared_create(idx_info->f, &idx_info->layout->storage.u.chunk,
828
0
                                 &idx_info->layout->u.chunk) < 0)
829
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, FAIL, "can't create wrapper for shared B-tree info");
830
831
0
done:
832
0
    FUNC_LEAVE_NOAPI(ret_value)
833
0
} /* end H5D__btree_idx_init() */
834
835
/*-------------------------------------------------------------------------
836
 * Function:    H5D__btree_idx_create
837
 *
838
 * Purpose:     Creates a new indexed-storage B-tree and initializes the
839
 *              layout struct with information about the storage.  The
840
 *              struct should be immediately written to the object header.
841
 *
842
 *              This function must be called before passing LAYOUT to any
843
 *              of the other indexed storage functions!
844
 *
845
 * Return:      Non-negative on success (with the LAYOUT argument
846
 *              initialized and ready to write to an object header).
847
 *              Negative on failure.
848
 *
849
 *-------------------------------------------------------------------------
850
 */
851
static herr_t
852
H5D__btree_idx_create(const H5D_chk_idx_info_t *idx_info)
853
0
{
854
0
    H5D_chunk_common_ud_t udata;               /* User data for B-tree callback */
855
0
    herr_t                ret_value = SUCCEED; /* Return value */
856
857
0
    FUNC_ENTER_PACKAGE
858
859
    /* Check args */
860
0
    assert(idx_info);
861
0
    assert(idx_info->f);
862
0
    assert(idx_info->pline);
863
0
    assert(idx_info->layout);
864
0
    assert(!H5_addr_defined(idx_info->layout->storage.u.chunk.idx_addr));
865
866
    /* Initialize "user" data for B-tree callbacks, etc. */
867
0
    udata.layout  = &idx_info->layout->u.chunk;
868
0
    udata.storage = &idx_info->layout->storage.u.chunk;
869
870
    /* Create the v1 B-tree for the chunk index */
871
0
    if (H5B_create(idx_info->f, H5B_BTREE, &udata, &(idx_info->layout->storage.u.chunk.idx_addr) /*out*/) < 0)
872
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "can't create B-tree");
873
874
0
done:
875
0
    FUNC_LEAVE_NOAPI(ret_value)
876
0
} /* end H5D__btree_idx_create() */
877
878
/*-------------------------------------------------------------------------
879
 * Function:    H5D__btree_idx_open
880
 *
881
 * Purpose:     Opens an existing B-tree. Currently a no-op.
882
 *
883
 * Return:      SUCCEED (can't fail)
884
 *
885
 *-------------------------------------------------------------------------
886
 */
887
static herr_t
888
H5D__btree_idx_open(const H5D_chk_idx_info_t H5_ATTR_UNUSED *idx_info)
889
0
{
890
0
    FUNC_ENTER_PACKAGE_NOERR
891
892
    /* NO OP */
893
894
0
    FUNC_LEAVE_NOAPI(SUCCEED)
895
0
} /* end H5D__btree_idx_open() */
896
897
/*-------------------------------------------------------------------------
898
 * Function:    H5D__btree_idx_close
899
 *
900
 * Purpose:     Closes an existing B-tree. Currently a no-op.
901
 *
902
 * Return:      SUCCEED (can't fail)
903
 *
904
 *-------------------------------------------------------------------------
905
 */
906
static herr_t
907
H5D__btree_idx_close(const H5D_chk_idx_info_t H5_ATTR_UNUSED *idx_info)
908
0
{
909
0
    FUNC_ENTER_PACKAGE_NOERR
910
911
    /* NO OP */
912
913
0
    FUNC_LEAVE_NOAPI(SUCCEED)
914
0
} /* end H5D__btree_idx_close() */
915
916
/*-------------------------------------------------------------------------
917
 * Function:    H5D__btree_idx_is_open
918
 *
919
 * Purpose:     Query if the index is opened or not
920
 *
921
 * Return:      SUCCEED (can't fail)
922
 *
923
 *-------------------------------------------------------------------------
924
 */
925
static herr_t
926
H5D__btree_idx_is_open(const H5D_chk_idx_info_t *idx_info, bool *is_open)
927
0
{
928
0
    FUNC_ENTER_PACKAGE_NOERR
929
930
0
    assert(idx_info);
931
0
    assert(idx_info->layout);
932
0
    assert(H5D_CHUNK_IDX_BTREE == idx_info->layout->storage.u.chunk.idx_type);
933
0
    assert(is_open);
934
935
0
    *is_open = H5D_BTREE_IDX_IS_OPEN(idx_info);
936
937
0
    FUNC_LEAVE_NOAPI(SUCCEED)
938
0
} /* end H5D__btree_idx_is_open() */
939
940
/*-------------------------------------------------------------------------
941
 * Function:    H5D__btree_idx_is_space_alloc
942
 *
943
 * Purpose:     Query if space is allocated for index method
944
 *
945
 * Return:      Non-negative on success/Negative on failure
946
 *
947
 *-------------------------------------------------------------------------
948
 */
949
static bool
950
H5D__btree_idx_is_space_alloc(const H5O_storage_chunk_t *storage)
951
0
{
952
0
    FUNC_ENTER_PACKAGE_NOERR
953
954
    /* Check args */
955
0
    assert(storage);
956
957
0
    FUNC_LEAVE_NOAPI((bool)H5_addr_defined(storage->idx_addr))
958
0
} /* end H5D__btree_idx_is_space_alloc() */
959
960
/*-------------------------------------------------------------------------
961
 * Function:    H5D__btree_idx_insert
962
 *
963
 * Purpose:     Insert chunk entry into the indexing structure.
964
 *
965
 * Return:      Non-negative on success/Negative on failure
966
 *
967
 *-------------------------------------------------------------------------
968
 */
969
static herr_t
970
H5D__btree_idx_insert(const H5D_chk_idx_info_t *idx_info, H5D_chunk_ud_t *udata,
971
                      const H5D_t H5_ATTR_UNUSED *dset)
972
0
{
973
0
    herr_t ret_value = SUCCEED; /* Return value */
974
975
0
    FUNC_ENTER_PACKAGE
976
977
0
    assert(idx_info);
978
0
    assert(idx_info->f);
979
0
    assert(idx_info->pline);
980
0
    assert(idx_info->layout);
981
0
    assert(H5_addr_defined(idx_info->layout->storage.u.chunk.idx_addr));
982
0
    assert(udata);
983
984
    /*
985
     * Create the chunk it if it doesn't exist, or reallocate the chunk if
986
     * its size changed.
987
     */
988
0
    if (H5B_insert(idx_info->f, H5B_BTREE, idx_info->layout->storage.u.chunk.idx_addr, udata) < 0)
989
0
        HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "unable to allocate chunk");
990
991
0
done:
992
0
    FUNC_LEAVE_NOAPI(ret_value)
993
0
} /* H5D__btree_idx_insert() */
994
995
/*-------------------------------------------------------------------------
996
 * Function:    H5D__btree_idx_get_addr
997
 *
998
 * Purpose:     Get the file address of a chunk if file space has been
999
 *              assigned.  Save the retrieved information in the udata
1000
 *              supplied.
1001
 *
1002
 * Return:      Non-negative on success/Negative on failure
1003
 *
1004
 *-------------------------------------------------------------------------
1005
 */
1006
static herr_t
1007
H5D__btree_idx_get_addr(const H5D_chk_idx_info_t *idx_info, H5D_chunk_ud_t *udata)
1008
0
{
1009
0
    bool   found;               /* Whether chunk was found */
1010
0
    herr_t ret_value = SUCCEED; /* Return value */
1011
1012
0
    FUNC_ENTER_PACKAGE
1013
1014
0
    assert(idx_info);
1015
0
    assert(idx_info->f);
1016
0
    assert(idx_info->pline);
1017
0
    assert(idx_info->layout);
1018
0
    assert(idx_info->layout->u.chunk.ndims > 0);
1019
0
    assert(H5_addr_defined(idx_info->layout->storage.u.chunk.idx_addr));
1020
0
    assert(udata);
1021
1022
    /* Go get the chunk information from the B-tree */
1023
0
    found = false;
1024
0
    if (H5B_find(idx_info->f, H5B_BTREE, idx_info->layout->storage.u.chunk.idx_addr, &found, udata) < 0)
1025
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTFIND, FAIL, "can't check for chunk in B-tree");
1026
1027
0
done:
1028
0
    FUNC_LEAVE_NOAPI(ret_value)
1029
0
} /* H5D__btree_idx_get_addr() */
1030
1031
/*-------------------------------------------------------------------------
1032
 * Function:    H5D__btree_idx_load_metadata
1033
 *
1034
 * Purpose:     Load additional chunk index metadata beyond the chunk index
1035
 *              itself. Currently a no-op.
1036
 *
1037
 * Return:      Non-negative on success/Negative on failure
1038
 *
1039
 *-------------------------------------------------------------------------
1040
 */
1041
static herr_t
1042
H5D__btree_idx_load_metadata(const H5D_chk_idx_info_t H5_ATTR_UNUSED *idx_info)
1043
0
{
1044
0
    FUNC_ENTER_PACKAGE_NOERR
1045
1046
    /* NO OP */
1047
1048
0
    FUNC_LEAVE_NOAPI(SUCCEED)
1049
0
} /* end H5D__btree_idx_load_metadata() */
1050
1051
/*-------------------------------------------------------------------------
1052
 * Function:    H5D__btree_idx_iterate_cb
1053
 *
1054
 * Purpose:     Translate the B-tree specific chunk record into a generic
1055
 *              form and make the callback to the generic chunk callback
1056
 *              routine.
1057
 *
1058
 * Return:      Success:    Non-negative
1059
 *              Failure:    Negative
1060
 *
1061
 *-------------------------------------------------------------------------
1062
 */
1063
static int
1064
H5D__btree_idx_iterate_cb(H5F_t H5_ATTR_UNUSED *f, const void *_lt_key, haddr_t addr,
1065
                          const void H5_ATTR_UNUSED *_rt_key, void *_udata)
1066
0
{
1067
0
    H5D_btree_it_ud_t     *udata  = (H5D_btree_it_ud_t *)_udata;      /* User data */
1068
0
    const H5D_btree_key_t *lt_key = (const H5D_btree_key_t *)_lt_key; /* B-tree key for chunk */
1069
0
    H5D_chunk_rec_t        chunk_rec;                                 /* Generic chunk record for callback */
1070
0
    int                    ret_value = -1;                            /* Return value */
1071
1072
0
    FUNC_ENTER_PACKAGE_NOERR
1073
1074
    /* Sanity check for memcpy() */
1075
0
    HDcompile_assert(offsetof(H5D_chunk_rec_t, scaled) == offsetof(H5D_btree_key_t, scaled));
1076
0
    HDcompile_assert(sizeof(chunk_rec.scaled) == sizeof(lt_key->scaled));
1077
1078
    /* Compose generic chunk record for callback */
1079
0
    H5MM_memcpy(&(chunk_rec.scaled), &(lt_key->scaled), sizeof(lt_key->scaled));
1080
0
    chunk_rec.nbytes      = (hsize_t)lt_key->nbytes;
1081
0
    chunk_rec.filter_mask = (uint32_t)lt_key->filter_mask;
1082
0
    chunk_rec.chunk_addr  = addr;
1083
1084
    /* Make "generic chunk" callback */
1085
0
    if ((ret_value = (udata->cb)(&chunk_rec, udata->udata)) < 0)
1086
0
        HERROR(H5E_DATASET, H5E_CALLBACK, "failure in generic chunk iterator callback");
1087
1088
0
    FUNC_LEAVE_NOAPI(ret_value)
1089
0
} /* H5D__btree_idx_iterate_cb() */
1090
1091
/*-------------------------------------------------------------------------
1092
 * Function:    H5D__btree_idx_iterate
1093
 *
1094
 * Purpose:     Iterate over the chunks in an index, making a callback
1095
 *              for each one.
1096
 *
1097
 * Return:      Non-negative on success/Negative on failure
1098
 *
1099
 *-------------------------------------------------------------------------
1100
 */
1101
static int
1102
H5D__btree_idx_iterate(const H5D_chk_idx_info_t *idx_info, H5D_chunk_cb_func_t chunk_cb, void *chunk_udata)
1103
0
{
1104
0
    H5D_btree_it_ud_t udata;          /* User data for B-tree iterator callback */
1105
0
    int               ret_value = -1; /* Return value */
1106
1107
0
    FUNC_ENTER_PACKAGE_NOERR
1108
1109
0
    assert(idx_info);
1110
0
    assert(idx_info->f);
1111
0
    assert(idx_info->pline);
1112
0
    assert(idx_info->layout);
1113
0
    assert(H5_addr_defined(idx_info->layout->storage.u.chunk.idx_addr));
1114
0
    assert(chunk_cb);
1115
0
    assert(chunk_udata);
1116
1117
    /* Initialize userdata */
1118
0
    memset(&udata, 0, sizeof udata);
1119
0
    udata.common.layout  = &idx_info->layout->u.chunk;
1120
0
    udata.common.storage = &idx_info->layout->storage.u.chunk;
1121
0
    udata.cb             = chunk_cb;
1122
0
    udata.udata          = chunk_udata;
1123
1124
    /* Iterate over existing chunks */
1125
0
    if ((ret_value = H5B_iterate(idx_info->f, H5B_BTREE, idx_info->layout->storage.u.chunk.idx_addr,
1126
0
                                 H5D__btree_idx_iterate_cb, &udata)) < 0)
1127
0
        HERROR(H5E_DATASET, H5E_BADITER, "unable to iterate over chunk B-tree");
1128
1129
0
    FUNC_LEAVE_NOAPI(ret_value)
1130
0
} /* end H5D__btree_idx_iterate() */
1131
1132
/*-------------------------------------------------------------------------
1133
 * Function:    H5D__btree_idx_remove
1134
 *
1135
 * Purpose:     Remove chunk from index.
1136
 *
1137
 * Return:      Non-negative on success/Negative on failure
1138
 *
1139
 *-------------------------------------------------------------------------
1140
 */
1141
static herr_t
1142
H5D__btree_idx_remove(const H5D_chk_idx_info_t *idx_info, H5D_chunk_common_ud_t *udata)
1143
0
{
1144
0
    herr_t ret_value = SUCCEED; /* Return value */
1145
1146
0
    FUNC_ENTER_PACKAGE
1147
1148
0
    assert(idx_info);
1149
0
    assert(idx_info->f);
1150
0
    assert(idx_info->pline);
1151
0
    assert(idx_info->layout);
1152
0
    assert(H5_addr_defined(idx_info->layout->storage.u.chunk.idx_addr));
1153
0
    assert(udata);
1154
1155
    /* Remove the chunk from the v1 B-tree index and release the space for the
1156
     * chunk (in the B-tree callback).
1157
     */
1158
0
    if (H5B_remove(idx_info->f, H5B_BTREE, idx_info->layout->storage.u.chunk.idx_addr, udata) < 0)
1159
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTDELETE, FAIL, "unable to remove chunk entry");
1160
1161
0
done:
1162
0
    FUNC_LEAVE_NOAPI(ret_value)
1163
0
} /* H5D__btree_idx_remove() */
1164
1165
/*-------------------------------------------------------------------------
1166
 * Function:    H5D__btree_idx_delete
1167
 *
1168
 * Purpose:     Delete index and raw data storage for entire dataset
1169
 *              (i.e. all chunks)
1170
 *
1171
 * Return:      Success:    Non-negative
1172
 *              Failure:    negative
1173
 *
1174
 *-------------------------------------------------------------------------
1175
 */
1176
static herr_t
1177
H5D__btree_idx_delete(const H5D_chk_idx_info_t *idx_info)
1178
0
{
1179
0
    herr_t ret_value = SUCCEED; /* Return value */
1180
1181
0
    FUNC_ENTER_PACKAGE
1182
1183
    /* Sanity checks */
1184
0
    assert(idx_info);
1185
0
    assert(idx_info->f);
1186
0
    assert(idx_info->pline);
1187
0
    assert(idx_info->layout);
1188
1189
    /* Check if the index data structure has been allocated */
1190
0
    if (H5_addr_defined(idx_info->layout->storage.u.chunk.idx_addr)) {
1191
0
        H5O_storage_chunk_t   tmp_storage; /* Local copy of storage info */
1192
0
        H5D_chunk_common_ud_t udata;       /* User data for B-tree operations */
1193
1194
        /* Set up temporary chunked storage info */
1195
0
        tmp_storage = idx_info->layout->storage.u.chunk;
1196
1197
        /* Set up the shared structure */
1198
0
        if (H5D__btree_shared_create(idx_info->f, &tmp_storage, &idx_info->layout->u.chunk) < 0)
1199
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "can't create wrapper for shared B-tree info");
1200
1201
        /* Set up B-tree user data */
1202
0
        memset(&udata, 0, sizeof udata);
1203
0
        udata.layout  = &idx_info->layout->u.chunk;
1204
0
        udata.storage = &tmp_storage;
1205
1206
        /* Delete entire B-tree */
1207
0
        if (H5B_delete(idx_info->f, H5B_BTREE, tmp_storage.idx_addr, &udata) < 0)
1208
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTDELETE, FAIL, "unable to delete chunk B-tree");
1209
1210
        /* Release the shared B-tree page */
1211
0
        if (NULL == tmp_storage.u.btree.shared)
1212
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTFREE, FAIL, "ref-counted page nil");
1213
0
        if (H5UC_DEC(tmp_storage.u.btree.shared) < 0)
1214
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTFREE, FAIL, "unable to decrement ref-counted page");
1215
0
    } /* end if */
1216
1217
0
done:
1218
0
    FUNC_LEAVE_NOAPI(ret_value)
1219
0
} /* end H5D__btree_idx_delete() */
1220
1221
/*-------------------------------------------------------------------------
1222
 * Function:    H5D__btree_idx_copy_setup
1223
 *
1224
 * Purpose:     Set up any necessary information for copying chunks
1225
 *
1226
 * Return:      Non-negative on success/Negative on failure
1227
 *
1228
 *-------------------------------------------------------------------------
1229
 */
1230
static herr_t
1231
H5D__btree_idx_copy_setup(const H5D_chk_idx_info_t *idx_info_src, const H5D_chk_idx_info_t *idx_info_dst)
1232
0
{
1233
0
    herr_t ret_value = SUCCEED; /* Return value */
1234
1235
0
    FUNC_ENTER_PACKAGE_TAG(H5AC__COPIED_TAG)
1236
1237
0
    assert(idx_info_src);
1238
0
    assert(idx_info_src->f);
1239
0
    assert(idx_info_src->pline);
1240
0
    assert(idx_info_src->layout);
1241
0
    assert(idx_info_dst);
1242
0
    assert(idx_info_dst->f);
1243
0
    assert(idx_info_dst->pline);
1244
0
    assert(idx_info_dst->layout);
1245
0
    assert(!H5_addr_defined(idx_info_dst->layout->storage.u.chunk.idx_addr));
1246
1247
    /* Create shared B-tree info for each file */
1248
0
    if (H5D__btree_shared_create(idx_info_src->f, &idx_info_src->layout->storage.u.chunk,
1249
0
                                 &idx_info_src->layout->u.chunk) < 0)
1250
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, FAIL, "can't create wrapper for source shared B-tree info");
1251
0
    if (H5D__btree_shared_create(idx_info_dst->f, &idx_info_dst->layout->storage.u.chunk,
1252
0
                                 &idx_info_dst->layout->u.chunk) < 0)
1253
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, FAIL,
1254
0
                    "can't create wrapper for destination shared B-tree info");
1255
1256
    /* Create the root of the B-tree that describes chunked storage in the dest. file */
1257
0
    if (H5D__btree_idx_create(idx_info_dst) < 0)
1258
0
        HGOTO_ERROR(H5E_IO, H5E_CANTINIT, FAIL, "unable to initialize chunked storage");
1259
0
    assert(H5_addr_defined(idx_info_dst->layout->storage.u.chunk.idx_addr));
1260
1261
0
done:
1262
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
1263
0
} /* end H5D__btree_idx_copy_setup() */
1264
1265
/*-------------------------------------------------------------------------
1266
 * Function:    H5D__btree_idx_copy_shutdown
1267
 *
1268
 * Purpose:     Shutdown any information from copying chunks
1269
 *
1270
 * Return:      Non-negative on success/Negative on failure
1271
 *
1272
 *-------------------------------------------------------------------------
1273
 */
1274
static herr_t
1275
H5D__btree_idx_copy_shutdown(H5O_storage_chunk_t *storage_src, H5O_storage_chunk_t *storage_dst)
1276
0
{
1277
0
    herr_t ret_value = SUCCEED; /* Return value */
1278
1279
0
    FUNC_ENTER_PACKAGE
1280
1281
0
    assert(storage_src);
1282
0
    assert(storage_dst);
1283
1284
    /* Decrement refcount on shared B-tree info */
1285
0
    if (H5UC_DEC(storage_src->u.btree.shared) < 0)
1286
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTDEC, FAIL, "unable to decrement ref-counted page");
1287
0
    if (H5UC_DEC(storage_dst->u.btree.shared) < 0)
1288
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTDEC, FAIL, "unable to decrement ref-counted page");
1289
1290
0
done:
1291
0
    FUNC_LEAVE_NOAPI(ret_value)
1292
0
} /* end H5D__btree_idx_copy_shutdown() */
1293
1294
/*-------------------------------------------------------------------------
1295
 * Function:    H5D__btree_idx_size
1296
 *
1297
 * Purpose:     Retrieve the amount of index storage for chunked dataset
1298
 *
1299
 * Return:      Success:        Non-negative
1300
 *              Failure:        negative
1301
 *
1302
 *-------------------------------------------------------------------------
1303
 */
1304
static herr_t
1305
H5D__btree_idx_size(const H5D_chk_idx_info_t *idx_info, hsize_t *index_size)
1306
0
{
1307
0
    H5D_chunk_common_ud_t udata;               /* User-data for loading B-tree nodes */
1308
0
    H5B_info_t            bt_info;             /* B-tree info */
1309
0
    herr_t                ret_value = SUCCEED; /* Return value */
1310
1311
0
    FUNC_ENTER_PACKAGE
1312
1313
    /* Check args */
1314
0
    assert(idx_info);
1315
0
    assert(idx_info->f);
1316
0
    assert(idx_info->pline);
1317
0
    assert(idx_info->layout);
1318
0
    assert(index_size);
1319
1320
    /* Initialize B-tree node user-data */
1321
0
    memset(&udata, 0, sizeof udata);
1322
0
    udata.layout  = &idx_info->layout->u.chunk;
1323
0
    udata.storage = &idx_info->layout->storage.u.chunk;
1324
1325
    /* Get metadata information for B-tree */
1326
0
    if (H5B_get_info(idx_info->f, H5B_BTREE, idx_info->layout->storage.u.chunk.idx_addr, &bt_info, NULL,
1327
0
                     &udata) < 0)
1328
0
        HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "unable to iterate over chunk B-tree");
1329
1330
    /* Set the size of the B-tree */
1331
0
    *index_size = bt_info.size;
1332
1333
0
done:
1334
0
    FUNC_LEAVE_NOAPI(ret_value)
1335
0
} /* end H5D__btree_idx_size() */
1336
1337
/*-------------------------------------------------------------------------
1338
 * Function:    H5D__btree_idx_reset
1339
 *
1340
 * Purpose:     Reset indexing information.
1341
 *
1342
 * Return:      Non-negative on success/Negative on failure
1343
 *
1344
 *-------------------------------------------------------------------------
1345
 */
1346
static herr_t
1347
H5D__btree_idx_reset(H5O_storage_chunk_t *storage, bool reset_addr)
1348
0
{
1349
0
    FUNC_ENTER_PACKAGE_NOERR
1350
1351
0
    assert(storage);
1352
1353
    /* Reset index info */
1354
0
    if (reset_addr)
1355
0
        storage->idx_addr = HADDR_UNDEF;
1356
0
    storage->u.btree.shared = NULL;
1357
1358
0
    FUNC_LEAVE_NOAPI(SUCCEED)
1359
0
} /* end H5D__btree_idx_reset() */
1360
1361
/*-------------------------------------------------------------------------
1362
 * Function:    H5D__btree_idx_dump
1363
 *
1364
 * Purpose:     Dump indexing information to a stream.
1365
 *
1366
 * Return:      Non-negative on success/Negative on failure
1367
 *
1368
 *-------------------------------------------------------------------------
1369
 */
1370
static herr_t
1371
H5D__btree_idx_dump(const H5O_storage_chunk_t *storage, FILE *stream)
1372
0
{
1373
0
    FUNC_ENTER_PACKAGE_NOERR
1374
1375
0
    assert(storage);
1376
0
    assert(stream);
1377
1378
0
    fprintf(stream, "    Address: %" PRIuHADDR "\n", storage->idx_addr);
1379
1380
0
    FUNC_LEAVE_NOAPI(SUCCEED)
1381
0
} /* end H5D__btree_idx_dump() */
1382
1383
/*-------------------------------------------------------------------------
1384
 * Function:    H5D__btree_idx_dest
1385
 *
1386
 * Purpose:     Release indexing information in memory.
1387
 *
1388
 * Return:      Non-negative on success/Negative on failure
1389
 *
1390
 *-------------------------------------------------------------------------
1391
 */
1392
static herr_t
1393
H5D__btree_idx_dest(const H5D_chk_idx_info_t *idx_info)
1394
0
{
1395
0
    herr_t ret_value = SUCCEED; /* Return value */
1396
1397
0
    FUNC_ENTER_PACKAGE
1398
1399
0
    assert(idx_info);
1400
0
    assert(idx_info->f);
1401
0
    assert(idx_info->pline);
1402
0
    assert(idx_info->layout);
1403
1404
    /* Free the raw B-tree node buffer */
1405
0
    if (NULL == idx_info->layout->storage.u.chunk.u.btree.shared)
1406
0
        HGOTO_ERROR(H5E_IO, H5E_CANTFREE, FAIL, "ref-counted page nil");
1407
0
    if (H5UC_DEC(idx_info->layout->storage.u.chunk.u.btree.shared) < 0)
1408
0
        HGOTO_ERROR(H5E_IO, H5E_CANTFREE, FAIL, "unable to decrement ref-counted page");
1409
1410
0
done:
1411
0
    FUNC_LEAVE_NOAPI(ret_value)
1412
0
} /* end H5D__btree_idx_dest() */
1413
1414
/*-------------------------------------------------------------------------
1415
 * Function:    H5D_btree_debug
1416
 *
1417
 * Purpose:     Debugs a B-tree node for indexed raw data storage.
1418
 *
1419
 * Return:      Non-negative on success/Negative on failure
1420
 *
1421
 *-------------------------------------------------------------------------
1422
 */
1423
herr_t
1424
H5D_btree_debug(H5F_t *f, haddr_t addr, FILE *stream, int indent, int fwidth, unsigned ndims,
1425
                const uint32_t *dim)
1426
0
{
1427
0
    H5D_btree_dbg_t     udata;               /* User data for B-tree callback */
1428
0
    H5O_storage_chunk_t storage;             /* Storage information for B-tree callback */
1429
0
    H5O_layout_chunk_t  layout;              /* Layout information for B-tree callback */
1430
0
    bool                shared_init = false; /* Whether B-tree shared info is initialized */
1431
0
    unsigned            u;                   /* Local index variable */
1432
0
    herr_t              ret_value = SUCCEED; /* Return value */
1433
1434
0
    FUNC_ENTER_NOAPI(FAIL)
1435
1436
    /* Reset "fake" storage info */
1437
0
    memset(&storage, 0, sizeof(storage));
1438
0
    storage.idx_type = H5D_CHUNK_IDX_BTREE;
1439
1440
    /* Reset "fake" layout info */
1441
0
    memset(&layout, 0, sizeof(layout));
1442
0
    layout.ndims = ndims;
1443
0
    for (u = 0; u < ndims; u++)
1444
0
        layout.dim[u] = dim[u];
1445
1446
    /* Allocate the shared structure */
1447
0
    if (H5D__btree_shared_create(f, &storage, &layout) < 0)
1448
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, FAIL, "can't create wrapper for shared B-tree info");
1449
0
    shared_init = true;
1450
1451
    /* Set up user data for callback */
1452
0
    udata.common.layout  = &layout;
1453
0
    udata.common.storage = &storage;
1454
0
    udata.common.scaled  = NULL;
1455
0
    udata.ndims          = ndims;
1456
1457
    /* Dump the records for the B-tree */
1458
0
    (void)H5B_debug(f, addr, stream, indent, fwidth, H5B_BTREE, &udata);
1459
1460
0
done:
1461
0
    if (shared_init) {
1462
        /* Free the raw B-tree node buffer */
1463
0
        if (NULL == storage.u.btree.shared)
1464
0
            HDONE_ERROR(H5E_IO, H5E_CANTFREE, FAIL, "ref-counted shared info nil");
1465
0
        else if (H5UC_DEC(storage.u.btree.shared) < 0)
1466
0
            HDONE_ERROR(H5E_IO, H5E_CANTFREE, FAIL, "unable to decrement ref-counted shared info");
1467
0
    } /* end if */
1468
1469
0
    FUNC_LEAVE_NOAPI(ret_value)
1470
0
} /* end H5D_btree_debug() */