summaryrefslogtreecommitdiffstats
path: root/src/H5BP.c
blob: 758d1d6dd92376412d95e1e4312af714186dc565 (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
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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:		H5BP.c
 *			Apr 14 2005
 *			Quincey Koziol <koziol@ncsa.uiuc.edu>
 *
 * Purpose:		Implements a B+ tree, with several modifications from
 *                      the "standard" methods.
 *
 *                      Please see the documentation in:
 *                      doc/html/TechNotes/Btrees.html for a full description
 *                      of how they work, etc.
 *
 *-------------------------------------------------------------------------
 */

#define H5BP_PACKAGE		/*suppress error about including H5BPpkg  */

/* Private headers */
#include "H5private.h"		/* Generic Functions			*/
#include "H5BPpkg.h"		/* B+ trees				*/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5MFprivate.h"	/* File memory management		*/
#include "H5MMprivate.h"	/* Memory management			*/

/* Local macros */

/* Uncomment this macro to enable extra sanity checking */
/* #define H5BP_DEBUG */


/* Local typedefs */

/* Local prototypes */

/* Helper functions */


/* Package variables */

/* Declare a free list to manage the H5BP_t struct */
H5FL_DEFINE(H5BP_t);

/* Declare a free list to manage the H5BP_branch_t struct */
H5FL_DEFINE(H5BP_branch_t);

/* Declare a free list to manage the H5BP_twig_t struct */
H5FL_DEFINE(H5BP_twig_t);

/* Declare a free list to manage the H5BP_leaf_t struct */
H5FL_DEFINE(H5BP_leaf_t);


/* Static variables */

/* Declare a free list to manage B-tree node pages to/from disk */
H5FL_BLK_DEFINE_STATIC(node_page);

/* Declare a free list to manage the H5BP_shared_t struct */
H5FL_DEFINE_STATIC(H5BP_shared_t);



/*-------------------------------------------------------------------------
 * Function:	H5BP_shared_init
 *
 * Purpose:	Allocate & initialize shared B-tree info
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Apr 14 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5BP_shared_init (H5BP_t *bpt, const H5BP_class_t *type,
    size_t node_size, unsigned split_percent, unsigned merge_percent)
{
    H5BP_shared_t *shared = NULL;       /* Shared B+ tree information */
    herr_t ret_value=SUCCEED;           /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5BP_shared_init)

    /* Allocate space for the shared information */
    if(NULL==(shared = H5FL_CALLOC(H5BP_shared_t)))
	HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for B+ tree shared information")

    /* Assign user's information */
    shared->split_percent = split_percent;
    shared->merge_percent = merge_percent;
    shared->node_size = node_size;

    /* Assign common type information */
    shared->type = type;

    /* Allocate "page" for node I/O */
    if((shared->page=H5FL_BLK_MALLOC(node_page,shared->node_size))==NULL)
        HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
#ifdef H5_USING_PURIFY
HDmemset(shared->page,0,shared->node_size);
#endif /* H5_USING_PURIFY */

    /* Make shared B-tree info reference counted */
    if(NULL==(bpt->shared=H5RC_create(shared,H5BP_shared_free)))
	HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "can't create ref-count wrapper for shared B+ tree info")

done:
    if(ret_value<0)
        if(shared)
            H5BP_shared_free(shared);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5BP_shared_init() */


/*-------------------------------------------------------------------------
 * Function:	H5BP_shared_free
 *
 * Purpose:	Free shared B+ tree info
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Apr 14 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5BP_shared_free (void *_shared)
{
    H5BP_shared_t *shared = (H5BP_shared_t *)_shared;
    herr_t ret_value=SUCCEED;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5BP_shared_free)

    /* Sanity check */
    HDassert(shared);

    /* Free the B-tree node buffer */
    if(shared->page)
        H5FL_BLK_FREE(node_page,shared->page);

    /* Free the shared B-tree info itself */
    H5FL_FREE(H5BP_shared_t,shared);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5BP_shared_free() */


/*-------------------------------------------------------------------------
 * Function:	H5BP_create
 *
 * Purpose:	Creates a new empty B+ tree in the file.
 *
 * Return:	Non-negative on success (with address of new B-tree
 *              filled in), negative on failure
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Apr 19 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5BP_create(H5F_t *f, hid_t dxpl_id, const H5BP_class_t *type,
    size_t node_size, unsigned split_percent, unsigned merge_percent,
    haddr_t *addr_p)
{
    H5BP_t *bpt = NULL;                 /* The new B+ tree header information */
    herr_t ret_value=SUCCEED;

    FUNC_ENTER_NOAPI(H5BP_create, FAIL)

    /*
     * Check arguments.
     */
    HDassert(f);
    HDassert(type);
    HDassert(node_size>0);
    HDassert(merge_percent>0 && merge_percent<=100);
    HDassert(split_percent>0 && split_percent<=100);
    HDassert(merge_percent<(split_percent/2));

    /*
     * Allocate file and memory data structures.
     */
    if (NULL==(bpt = H5FL_MALLOC(H5BP_t)))
	HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for B-tree header")

    /* Assign internal information */
    HDmemset(&bpt->cache_info,0,sizeof(H5AC_info_t));
    bpt->cache_info.is_dirty = TRUE;
    bpt->depth = 0;
    bpt->root.addr = HADDR_UNDEF;
    bpt->root.all_nrec = 0;
    bpt->root.util = 0;

    /* Initialize shared B-tree info */
    if(H5BP_shared_init(bpt, type, node_size, split_percent, merge_percent)<0)
	HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "can't create shared B+ tree info")

    /* Allocate space for the header on disk */
    if (HADDR_UNDEF==(*addr_p=H5MF_alloc(f, H5FD_MEM_BTREE, dxpl_id, (hsize_t)H5BP_HEADER_SIZE(f))))
	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "file allocation failed for B-tree header")

    /* Cache the new B-tree node */
    if (H5AC_set(f, dxpl_id, H5AC_BPT_HDR, *addr_p, bpt, H5AC__NO_FLAGS_SET) < 0)
	HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "can't add B+ tree header to cache")

done:
    if (ret_value<0) {
	if (bpt)
            (void)H5BP_cache_hdr_dest(f,bpt);
    } /* end if */

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5BP_create() */


/*-------------------------------------------------------------------------
 * Function:	H5BP_create_leaf
 *
 * Purpose:	Creates empty leaf node of a B+ tree and updates node pointer
 *              to point to it.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Apr 19 2005
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5BP_create_leaf(H5F_t *f, hid_t dxpl_id, H5RC_t *bpt_shared, H5BP_node_ptr_t *node_ptr)
{
    H5BP_leaf_t *leaf=NULL;     /* Pointer to new leaf node created */
    H5BP_shared_t *shared;      /* Shared B+ tree information */
    herr_t	ret_value = SUCCEED;

    FUNC_ENTER_NOAPI(H5BP_create_leaf, FAIL)

    /* Check arguments. */
    HDassert(f);
    HDassert(bpt_shared);
    HDassert(node_ptr);

    /* Allocate memory for leaf information */
    if (NULL==(leaf = H5FL_MALLOC(H5BP_leaf_t)))
	HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for B+ tree leaf info")

    /* Set metadata cache info */
    HDmemset(&leaf->cache_info,0,sizeof(H5AC_info_t));
    leaf->cache_info.is_dirty = TRUE;

    /* Share common B+ tree information */
    leaf->shared = bpt_shared;
    H5RC_INC(leaf->shared);

    /* Get the pointer to the shared B-tree info */
    shared=H5RC_GET_OBJ(leaf->shared);
    HDassert(shared);

    /* Create record pool for leaf node */
    if((leaf->rec_pool = H5MP_create(H5BP_LEAF_POOL_PAGE, H5MP_FLG_DEFAULT)) == NULL)
        HGOTO_ERROR(H5E_BTREE, H5E_NOSPACE, NULL, "can't allocate memory pool")

    /* Set number of records */
    leaf->nrec=0;
    leaf->flags=0;

    /* Allocate space on disk for the leaf */
    if (HADDR_UNDEF==(node_ptr->addr=H5MF_alloc(f, H5FD_MEM_BTREE, dxpl_id, (hsize_t)shared->node_size)))
	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "file allocation failed for B+ tree leaf node")

    /* Cache the new B+ tree node */
    if (H5AC_set(f, dxpl_id, H5AC_BPT_LEAF, node_ptr->addr, leaf, H5AC__NO_FLAGS_SET) < 0)
	HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "can't add B+ tree leaf to cache")

done:
    if (ret_value<0) {
	if (leaf)
            (void)H5BP_cache_leaf_dest(f,leaf);
    } /* end if */

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5BP_create_leaf() */


/*-------------------------------------------------------------------------
 * Function:	H5BP_insert_leaf
 *
 * Purpose:	Adds a new record to a B+ tree leaf node.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Apr 22 2005
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5BP_insert_leaf(H5F_t *f, hid_t dxpl_id, H5RC_t *bpt_shared,
    H5BP_node_ptr_t *curr_node_ptr, void *udata)
{
    H5BP_leaf_t *leaf;                  /* Pointer to leaf node */
    H5BP_shared_t *shared;              /* Pointer to B+ tree's shared information */
    int         cmp;                    /* Comparison value of records */
    unsigned    idx;                    /* Location of record which matches key */
    herr_t	ret_value = SUCCEED;

    FUNC_ENTER_NOAPI_NOINIT(H5BP_insert_leaf)

    /* Check arguments. */
    HDassert(f);
    HDassert(bpt_shared);
    HDassert(curr_node_ptr);
    HDassert(H5F_addr_defined(curr_node_ptr->addr));

if( H5BP_NODE_OVERSIZE(curr_node_ptr->util)) {
    HDfprintf(stderr,"%s: attempting to insert record into extent node\n",FUNC);
    HGOTO_ERROR(H5E_BTREE, H5E_UNSUPPORTED, FAIL, "Can't handle extent node!")
}
    /* Lock B+ tree node */
    if (NULL == (leaf = H5AC_protect(f, dxpl_id, H5AC_BPT_LEAF, curr_node_ptr->addr, &(curr_node_ptr->util), bpt_shared, H5AC_WRITE)))
        HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load B+ tree leaf node")

    /* Get the pointer to the shared B+ tree info */
    shared=H5RC_GET_OBJ(bpt_shared);
    HDassert(shared);

#ifndef LATER
HDfprintf(stderr,"%s: finish record insertion code\n",FUNC);
HGOTO_ERROR(H5E_BTREE, H5E_UNSUPPORTED, FAIL, "Can't handle record insertion!")
#else /* LATER */
    /* Check for inserting into empty leaf */
    if(leaf->nrec==0)
        idx=0;
    else {
        /* Find correct location to insert this record */
        if((cmp = H5BP_locate_record(shared->type,leaf->nrec,shared->nat_off,leaf->leaf_native,udata,&idx)) == 0)
            HGOTO_ERROR(H5E_BTREE, H5E_EXISTS, FAIL, "record is already in B+ tree")
        if(cmp > 0)
            idx++;

        /* Make room for new record */
        if(idx<leaf->nrec)
            HDmemmove(H5BP_LEAF_NREC(leaf,shared,idx+1),H5BP_LEAF_NREC(leaf,shared,idx),shared->type->nrec_size*(leaf->nrec-idx));
    } /* end else */

    /* Make callback to store record in native form */
    if((shared->type->store)(H5BP_LEAF_NREC(leaf,shared,idx),udata)<0)
        HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, FAIL, "unable to insert record into leaf node")
#endif /* LATER */

    /* Update record count for node pointer to current node */
    curr_node_ptr->all_nrec++;

    /* Update record count for current node */
    leaf->nrec++;

done:
    /* Release the B+ tree leaf node */
    if (leaf && H5AC_unprotect(f, dxpl_id, H5AC_BPT_LEAF, curr_node_ptr->addr, leaf, H5AC__DIRTIED_FLAG) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release leaf B+ tree node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5BP_insert_leaf() */


/*-------------------------------------------------------------------------
 * Function:	H5BP_insert
 *
 * Purpose:	Adds a new record to the B+ tree.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Apr 19 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5BP_insert(H5F_t *f, hid_t dxpl_id, const H5BP_class_t *type, haddr_t addr,
           void *udata)
{
    H5BP_t	*bpt=NULL;              /* Pointer to the B+ tree header */
    H5BP_shared_t *shared;              /* Pointer to B+ tree's shared information */
    herr_t	ret_value = SUCCEED;

    FUNC_ENTER_NOAPI(H5BP_insert, FAIL)

    /* Check arguments. */
    HDassert(f);
    HDassert(type);
    HDassert(H5F_addr_defined(addr));

    /* Look up the B+ tree header */
    if (NULL == (bpt = H5AC_protect(f, dxpl_id, H5AC_BPT_HDR, addr, type, NULL, H5AC_WRITE)))
	HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load B+ tree header")

    /* Get the pointer to the shared B+ tree info */
    shared=H5RC_GET_OBJ(bpt->shared);
    HDassert(shared);

    /* Check if the root node is allocated yet */
    if(!H5F_addr_defined(bpt->root.addr)) {
        /* Create root node as leaf node in B+ tree */
        if(H5BP_create_leaf(f, dxpl_id, bpt->shared, &(bpt->root))<0)
	    HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "unable to create root node")

        /* Mark B+ tree header as dirty, since we updated the address of the root node */
        bpt->cache_info.is_dirty = TRUE;
    } /* end if */
    /* Check if we need to split the root node (equiv. to a 1->2 leaf node split) */
    else if(H5BP_NODE_OVERSIZE(bpt->root.util) ||
            (!H5BP_NODE_OVERSIZE(bpt->root.util) && bpt->root.util>=shared->split_percent)) {
        /* Split root node */
#ifdef LATER
        if(H5BP_split_root(f, dxpl_id, bpt, bpt->shared)<0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, FAIL, "unable to split root node")
#else /* LATER */
HDfprintf(stderr,"%s: attempting to split root node\n",FUNC);
HGOTO_ERROR(H5E_BTREE, H5E_UNSUPPORTED, FAIL, "Can't split root yet!")
#endif /* LATER */
    } /* end if */

    /* Attempt to insert record into B+ tree */
    if(bpt->depth>0) {
#ifdef LATER
        if(H5BP_insert_internal(f,dxpl_id,bpt->shared,bpt->depth,&(bpt->cache_info),&bpt->root,udata)<0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, FAIL, "unable to insert record into B+ tree internal node")
#else /* LATER */
HDfprintf(stderr,"%s: attempting to insert record with internal node\n",FUNC);
HGOTO_ERROR(H5E_BTREE, H5E_UNSUPPORTED, FAIL, "Can't insert records yet!")
#endif /* LATER */
    } /* end if */
    else {
        if(H5BP_insert_leaf(f,dxpl_id,bpt->shared,&bpt->root,udata)<0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, FAIL, "unable to insert record into B+ tree leaf node")
    } /* end else */

done:
    /* Release the B+ tree header info */
    if (bpt && H5AC_unprotect(f, dxpl_id, H5AC_BPT_HDR, addr, bpt, H5AC__DIRTIED_FLAG) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B+ tree header info")

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5BP_insert() */