summaryrefslogtreecommitdiffstats
path: root/src/H5HFbtree2.c
blob: 4a324aa5333915989a6b0135bbdd949b0e984a60 (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
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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 files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
 * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:		H5HFbtree2.c
 *			Aug  7 2006
 *			Quincey Koziol <koziol@hdfgroup.org>
 *
 * Purpose:		v2 B-tree callbacks for "huge" object tracker
 *
 *-------------------------------------------------------------------------
 */

/****************/
/* Module Setup */
/****************/

#define H5HF_PACKAGE		/*suppress error about including H5HFpkg  */


/***********/
/* Headers */
/***********/
#include "H5private.h"		/* Generic Functions			*/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5HFpkg.h"		/* Fractal heaps			*/
#include "H5MFprivate.h"	/* File memory management		*/


/****************/
/* Local Macros */
/****************/


/******************/
/* Local Typedefs */
/******************/


/********************/
/* Package Typedefs */
/********************/


/********************/
/* Local Prototypes */
/********************/

/* v2 B-tree function callbacks */
herr_t H5HF_huge_bt2_found(const void *nrecord, void *op_data);
herr_t H5HF_huge_bt2_remove(const void *nrecord, void *op_data);

/* v2 B-tree driver callbacks */
static herr_t H5HF_huge_btree2_store(const H5B2_class_t *cls, void *native, const void *udata);
static herr_t H5HF_huge_btree2_retrieve(const H5B2_class_t *cls, void *udata, const void *native);
static herr_t H5HF_huge_btree2_compare(const H5B2_class_t *cls, const void *rec1, const void *rec2);
static herr_t H5HF_huge_btree2_encode(const H5F_t *f, const H5B2_class_t *cls, uint8_t *raw,
    const void *native);
static herr_t H5HF_huge_btree2_decode(const H5F_t *f, const H5B2_class_t *cls, const uint8_t *raw,
    void *native);
static herr_t H5HF_huge_btree2_debug(FILE *stream, const H5F_t *f, hid_t dxpl_id,
    int indent, int fwidth, const H5B2_class_t *cls, const void *record, const void *_udata);

/*********************/
/* Package Variables */
/*********************/
const H5B2_class_t H5HF_BTREE2[1]={{     /* B-tree class information */
    H5B2_FHEAP_ID,              /* Type of B-tree */
    0,                          /* Size of native record */
                                /* (computed at run-time for each heap) */
    NULL,                       /* Class private information */
    H5HF_huge_btree2_store,          /* Record storage callback */
    H5HF_huge_btree2_retrieve,       /* Record retrieval callback */
    H5HF_huge_btree2_compare,        /* Record comparison callback */
    H5HF_huge_btree2_encode,         /* Record encoding callback */
    H5HF_huge_btree2_decode,         /* Record decoding callback */
    H5HF_huge_btree2_debug           /* Record debugging callback */
}};

/*****************************/
/* Library Private Variables */
/*****************************/


/*******************/
/* Local Variables */
/*******************/


/*-------------------------------------------------------------------------
 * Function:	H5HF_huge_bt2_found
 *
 * Purpose:	Retrieve record for 'huge' object, when it's found in the
 *              v2 B-tree
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, August  8, 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF_huge_bt2_found(const void *nrecord, void *op_data)
{
    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_huge_bt2_found)

#ifdef QAK
HDfprintf(stderr, "%s: nrecord = {%a, %Hu, %Hu}\n", "H5HF_huge_bt2_store",
        ((const H5HF_huge_bt2_rec_t *)nrecord)->addr,
        ((const H5HF_huge_bt2_rec_t *)nrecord)->len,
        ((const H5HF_huge_bt2_rec_t *)nrecord)->id);
#endif /* QAK */
    *(H5HF_huge_bt2_rec_t *)op_data = *(const H5HF_huge_bt2_rec_t *)nrecord;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_huge_bt2_found() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_huge_bt2_remove
 *
 * Purpose:	Free space for 'huge' object, as v2 B-tree is being deleted
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, August  8, 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF_huge_bt2_remove(const void *nrecord, void *_udata)
{
    H5HF_huge_remove_ud1_t *udata = (H5HF_huge_remove_ud1_t *)_udata;   /* User callback data */
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_huge_bt2_remove)

    /* Free the space in the file for the object being removed */
    if(H5MF_xfree(udata->hdr->f, H5FD_MEM_FHEAP_HUGE_OBJ, udata->dxpl_id, ((const H5HF_huge_bt2_rec_t *)nrecord)->addr, ((const H5HF_huge_bt2_rec_t *)nrecord)->len) < 0)
        HGOTO_ERROR(H5E_HEAP, H5E_CANTFREE, FAIL, "unable to free space for huge object on disk")

    /* Set the length of the object removed */
    udata->obj_len = ((const H5HF_huge_bt2_rec_t *)nrecord)->len;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5HF_huge_bt2_remove() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_huge_btree2_store
 *
 * Purpose:	Store native information into record for v2 B-tree
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, August  7, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_huge_btree2_store(const H5B2_class_t UNUSED *cls, void *nrecord, const void *udata)
{
    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_huge_btree2_store)

    *(H5HF_huge_bt2_rec_t *)nrecord = *(const H5HF_huge_bt2_rec_t *)udata;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_huge_btree2_store() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_huge_btree2_retrieve
 *
 * Purpose:	Retrieve native information from record for v2 B-tree
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, August  7, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_huge_btree2_retrieve(const H5B2_class_t UNUSED *cls, void *udata, const void *nrecord)
{
    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_huge_btree2_retrieve)

    *(H5HF_huge_bt2_rec_t *)udata = *(const H5HF_huge_bt2_rec_t *)nrecord;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_huge_btree2_retrieve() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_huge_btree2_compare
 *
 * Purpose:	Compare two native information records, according to some key
 *
 * Return:	<0 if rec1 < rec2
 *              =0 if rec1 == rec2
 *              >0 if rec1 > rec2
 *
 * Programmer:	Quincey Koziol
 *              Monday, August  7, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_huge_btree2_compare(const H5B2_class_t *cls, const void *_rec1, const void *_rec2)
{
    const H5HF_huge_bt2_rec_t *rec1 = (const H5HF_huge_bt2_rec_t *)_rec1;
    const H5HF_huge_bt2_rec_t *rec2 = (const H5HF_huge_bt2_rec_t *)_rec2;
    const H5HF_hdr_t *hdr = (const H5HF_hdr_t *)cls->cls_private;
    herr_t ret_value;           /* Return value */

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_huge_btree2_compare)

#ifdef QAK
HDfprintf(stderr, "%s: hdr->huge_ids_direct = %t\n", "H5HF_huge_btree2_compare", hdr->huge_ids_direct);
HDfprintf(stderr, "%s: rec1 = {%a, %Hu, %Hu}\n", "H5HF_huge_btree2_compare", rec1->addr, rec1->len, rec1->id);
HDfprintf(stderr, "%s: rec2 = {%a, %Hu, %Hu}\n", "H5HF_huge_btree2_compare", rec2->addr, rec2->len, rec2->id);
#endif /* QAK */
    /* Sort differently, depending on whether 'huge' object directly reference disk */
    if(hdr->huge_ids_direct) {
        if(rec1->addr < rec2->addr)
            ret_value = -1;
        else if(rec1->addr > rec2->addr)
            ret_value = 1;
        else if(rec1->len < rec2->len)
            ret_value = -1;
        else if(rec1->len > rec2->len)
            ret_value = 1;
        else
            ret_value = 0;
    } /* end if */
    else
        ret_value = (herr_t)(rec1->id - rec2->id);

    FUNC_LEAVE_NOAPI(ret_value);
} /* H5HF_huge_btree2_compare() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_huge_btree2_encode
 *
 * Purpose:	Encode native information into raw form for storing on disk
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, August  7, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_huge_btree2_encode(const H5F_t *f, const H5B2_class_t *cls, uint8_t *raw, const void *_nrecord)
{
    const H5HF_huge_bt2_rec_t *nrecord = (const H5HF_huge_bt2_rec_t *)_nrecord;
    const H5HF_hdr_t *hdr = (const H5HF_hdr_t *)cls->cls_private;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_huge_btree2_encode)

    /* Encode the record's common fields */
    H5F_addr_encode(f, &raw, nrecord->addr);
    H5F_ENCODE_LENGTH(f, raw, nrecord->len);

    /* If 'huge' objects in this heap are not accessed directly, encode the ID also */
    if(!hdr->huge_ids_direct)
        UINT64ENCODE_VAR(raw, nrecord->id, hdr->huge_id_size)

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_huge_btree2_encode() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_huge_btree2_decode
 *
 * Purpose:	Decode raw disk form of record into native form
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, August  7, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_huge_btree2_decode(const H5F_t *f, const H5B2_class_t *cls, const uint8_t *raw, void *_nrecord)
{
    H5HF_huge_bt2_rec_t *nrecord = (H5HF_huge_bt2_rec_t *)_nrecord;
    const H5HF_hdr_t *hdr = (const H5HF_hdr_t *)cls->cls_private;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_huge_btree2_decode)

    /* Decode the record's common fields */
    H5F_addr_decode(f, &raw, &nrecord->addr);
    H5F_DECODE_LENGTH(f, raw, nrecord->len);

    /* If 'huge' objects in this heap are not accessed directly, decode the ID also */
    if(!hdr->huge_ids_direct)
        UINT64DECODE_VAR(raw, nrecord->id, hdr->huge_id_size)
    else
        nrecord->id = 0;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_huge_btree2_decode() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_huge_btree2_debug
 *
 * Purpose:	Debug native form of record
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, August  7, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_huge_btree2_debug(FILE *stream, const H5F_t UNUSED *f, hid_t UNUSED dxpl_id,
    int indent, int fwidth, const H5B2_class_t *cls, const void *_nrecord,
    const void UNUSED *_udata)
{
    const H5HF_huge_bt2_rec_t *nrecord = (const H5HF_huge_bt2_rec_t *)_nrecord;
    const H5HF_hdr_t *hdr = (const H5HF_hdr_t *)cls->cls_private;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_huge_btree2_debug)

    HDassert(nrecord);

    if(hdr->huge_ids_direct)
        HDfprintf(stream, "%*s%-*s {%a, %Hu}\n", indent, "", fwidth, "Record:",
            nrecord->addr, nrecord->len);
    else
        HDfprintf(stream, "%*s%-*s {%a, %Hu, %Hu}\n", indent, "", fwidth, "Record:",
            nrecord->addr, nrecord->len, nrecord->id);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_huge_btree2_debug() */