summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/H5B2.c128
-rw-r--r--src/H5B2int.c353
-rw-r--r--src/H5B2pkg.h7
-rw-r--r--src/H5B2private.h3
-rw-r--r--test/btree2.c187
5 files changed, 646 insertions, 32 deletions
diff --git a/src/H5B2.c b/src/H5B2.c
index 3dd607e..c98e66e 100644
--- a/src/H5B2.c
+++ b/src/H5B2.c
@@ -509,13 +509,13 @@ H5B2_index(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type, haddr_t addr,
HDassert(op);
/* Look up the B-tree header */
- if (NULL == (bt2 = H5AC_protect(f, dxpl_id, H5AC_BT2_HDR, addr, type, NULL, H5AC_READ)))
+ if(NULL == (bt2 = H5AC_protect(f, dxpl_id, H5AC_BT2_HDR, addr, type, NULL, H5AC_READ)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load B-tree header")
/* Safely grab pointer to reference counted shared B-tree info, so we can release the B-tree header if necessary */
- bt2_shared=bt2->shared;
+ bt2_shared = bt2->shared;
H5RC_INC(bt2_shared);
- incr_rc=TRUE;
+ incr_rc = TRUE;
/* Get the pointer to the shared B-tree info */
shared = H5RC_GET_OBJ(bt2_shared);
@@ -555,11 +555,11 @@ H5B2_index(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type, haddr_t addr,
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load B-tree internal node")
/* Search for record with correct index */
- for(u=0; u<internal->nrec; u++) {
+ for(u = 0; u < internal->nrec; u++) {
/* Check if record is in child node */
if(internal->node_ptrs[u].all_nrec > idx) {
/* Get node pointer for next node to search */
- next_node_ptr=internal->node_ptrs[u];
+ next_node_ptr = internal->node_ptrs[u];
/* Unlock current node */
if(H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, curr_node_ptr.addr, internal, H5AC__NO_FLAGS_SET) < 0)
@@ -575,35 +575,36 @@ H5B2_index(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type, haddr_t addr,
/* Check if record is in this node */
if(internal->node_ptrs[u].all_nrec == idx) {
/* Make callback for current record */
- if ((op)(H5B2_INT_NREC(internal,shared,u), op_data) <0) {
+ if((op)(H5B2_INT_NREC(internal, shared, u), op_data) < 0) {
/* Unlock current node */
- if (H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, curr_node_ptr.addr, internal, H5AC__NO_FLAGS_SET) < 0)
+ if(H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, curr_node_ptr.addr, internal, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node")
HGOTO_ERROR(H5E_BTREE, H5E_NOTFOUND, FAIL, "'found' callback failed for B-tree find operation")
} /* end if */
/* Unlock current node */
- if (H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, curr_node_ptr.addr, internal, H5AC__NO_FLAGS_SET) < 0)
+ if(H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, curr_node_ptr.addr, internal, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node")
HGOTO_DONE(SUCCEED);
} /* end if */
- /* Decrement index we are looking for to account for the node we */
- /* just advanced past */
+ /* Decrement index we are looking for to account for the node we
+ * just advanced past.
+ */
idx -= (internal->node_ptrs[u].all_nrec + 1);
} /* end for */
/* Check last node pointer */
- if(u==internal->nrec) {
+ if(u == internal->nrec) {
/* Check if record is in child node */
if(internal->node_ptrs[u].all_nrec > idx) {
/* Get node pointer for next node to search */
- next_node_ptr=internal->node_ptrs[u];
+ next_node_ptr = internal->node_ptrs[u];
/* Unlock current node */
- if (H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, curr_node_ptr.addr, internal, H5AC__NO_FLAGS_SET) < 0)
+ if(H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, curr_node_ptr.addr, internal, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node")
/* Set pointer to next node to load */
@@ -629,7 +630,7 @@ H5B2_index(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type, haddr_t addr,
HDassert(idx < leaf->nrec);
/* Make callback for correct record */
- if((op)(H5B2_LEAF_NREC(leaf,shared,idx), op_data) < 0) {
+ if((op)(H5B2_LEAF_NREC(leaf, shared, idx), op_data) < 0) {
/* Unlock current node */
if(H5AC_unprotect(f, dxpl_id, H5AC_BT2_LEAF, curr_node_ptr.addr, leaf, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node")
@@ -666,9 +667,9 @@ done:
*/
herr_t
H5B2_remove(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type, haddr_t addr,
- void *udata, H5B2_remove_t op, void *op_data)
+ void *udata, H5B2_remove_t op, void *op_data)
{
- H5B2_t *bt2=NULL; /* Pointer to the B-tree header */
+ H5B2_t *bt2 = NULL; /* Pointer to the B-tree header */
unsigned bt2_flags = H5AC__NO_FLAGS_SET;
H5B2_shared_t *shared; /* Pointer to B-tree's shared information */
herr_t ret_value = SUCCEED;
@@ -681,7 +682,7 @@ H5B2_remove(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type, haddr_t addr,
HDassert(H5F_addr_defined(addr));
/* Look up the b-tree header */
- if (NULL == (bt2 = H5AC_protect(f, dxpl_id, H5AC_BT2_HDR, addr, type, NULL, H5AC_WRITE)))
+ if(NULL == (bt2 = H5AC_protect(f, dxpl_id, H5AC_BT2_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 */
@@ -689,7 +690,7 @@ H5B2_remove(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type, haddr_t addr,
HDassert(shared);
/* Check for empty B-tree */
- if(bt2->root.all_nrec==0)
+ if(bt2->root.all_nrec == 0)
HGOTO_ERROR(H5E_BTREE, H5E_NOTFOUND, FAIL, "record is not in B-tree")
/* Attempt to remove record from B-tree */
@@ -734,6 +735,97 @@ done:
/*-------------------------------------------------------------------------
+ * Function: H5B2_remove_by_idx
+ *
+ * Purpose: Removes the n'th record from a B-tree.
+ *
+ * Return: Non-negative on success/Negative on failure
+ *
+ * Programmer: Quincey Koziol
+ * koziol@hdfgroup.org
+ * Nov 14 2006
+ *
+ *-------------------------------------------------------------------------
+ */
+herr_t
+H5B2_remove_by_idx(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type,
+ haddr_t addr, H5_iter_order_t order, hsize_t idx, H5B2_remove_t op,
+ void *op_data)
+{
+ H5B2_t *bt2 = NULL; /* Pointer to the B-tree header */
+ unsigned bt2_flags = H5AC__NO_FLAGS_SET;
+ H5B2_shared_t *shared; /* Pointer to B-tree's shared information */
+ herr_t ret_value = SUCCEED;
+
+ FUNC_ENTER_NOAPI(H5B2_remove_by_idx, FAIL)
+
+ /* Check arguments. */
+ HDassert(f);
+ HDassert(type);
+ HDassert(H5F_addr_defined(addr));
+
+ /* Look up the b-tree header */
+ if(NULL == (bt2 = H5AC_protect(f, dxpl_id, H5AC_BT2_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(bt2->shared);
+ HDassert(shared);
+
+ /* Check for empty B-tree */
+ if(bt2->root.all_nrec == 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_NOTFOUND, FAIL, "record is not in B-tree")
+
+ /* Check for index greater than the number of records in the tree */
+ if(idx >= bt2->root.all_nrec)
+ HGOTO_ERROR(H5E_BTREE, H5E_NOTFOUND, FAIL, "B-tree doesn't have that many records")
+
+ /* Check for reverse indexing and map requested index to appropriate forward index */
+ if(order == H5_ITER_DEC)
+ idx = bt2->root.all_nrec - (idx + 1);
+
+ /* Attempt to remove record from B-tree */
+ if(shared->depth > 0) {
+ hbool_t depth_decreased = FALSE; /* Flag to indicate whether the depth of the B-tree decreased */
+
+ if(H5B2_remove_internal_by_idx(f, dxpl_id, bt2->shared, &depth_decreased, NULL, shared->depth,
+ &(bt2->cache_info), &bt2_flags, &bt2->root, idx, op, op_data) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTDELETE, FAIL, "unable to remove record from B-tree internal node")
+
+ /* Check for decreasing the depth of the B-tree */
+ if(depth_decreased) {
+ /* Destroy free list factories for previous depth */
+ if(shared->node_info[shared->depth].nat_rec_fac)
+ if(H5FL_fac_term(shared->node_info[shared->depth].nat_rec_fac) < 0)
+ HGOTO_ERROR(H5E_RESOURCE, H5E_CANTRELEASE, FAIL, "can't destroy node's native record block factory")
+ if(shared->node_info[shared->depth].node_ptr_fac)
+ if(H5FL_fac_term(shared->node_info[shared->depth].node_ptr_fac) < 0)
+ HGOTO_ERROR(H5E_RESOURCE, H5E_CANTRELEASE, FAIL, "can't destroy node's node pointer block factory")
+
+ shared->depth -= depth_decreased;
+ } /* end for */
+ } /* end if */
+ else {
+ if(H5B2_remove_leaf_by_idx(f, dxpl_id, bt2->shared, &bt2->root, (unsigned)idx, op, op_data) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTDELETE, FAIL, "unable to remove record from B-tree leaf node")
+ } /* end else */
+
+ /* Decrement # of records in B-tree */
+ bt2->root.all_nrec--;
+
+ /* Mark parent node as dirty */
+ bt2_flags |= H5AC__DIRTIED_FLAG;
+
+done:
+ /* Release the B-tree header info */
+ if (bt2 && H5AC_unprotect(f, dxpl_id, H5AC_BT2_HDR, addr, bt2, bt2_flags) < 0)
+ HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree header info")
+
+ FUNC_LEAVE_NOAPI(ret_value)
+} /* H5B2_remove_by_idx() */
+
+
+/*-------------------------------------------------------------------------
* Function: H5B2_get_nrec
*
* Purpose: Retrieves the number of records in a B-tree
diff --git a/src/H5B2int.c b/src/H5B2int.c
index e97487d..4646734 100644
--- a/src/H5B2int.c
+++ b/src/H5B2int.c
@@ -2231,8 +2231,8 @@ H5B2_remove_leaf(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
void *op_data)
{
H5B2_leaf_t *leaf; /* Pointer to leaf node */
- haddr_t leaf_addr=HADDR_UNDEF; /* Leaf address on disk */
- unsigned leaf_flags=H5AC__NO_FLAGS_SET; /* Flags for unprotecting leaf node */
+ haddr_t leaf_addr = HADDR_UNDEF; /* Leaf address on disk */
+ unsigned leaf_flags = H5AC__NO_FLAGS_SET; /* Flags for unprotecting leaf node */
H5B2_shared_t *shared; /* Pointer to B-tree's shared information */
unsigned idx; /* Location of record which matches key */
herr_t ret_value = SUCCEED;
@@ -2295,7 +2295,7 @@ H5B2_remove_leaf(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
done:
/* Release the B-tree leaf node */
- if (leaf && H5AC_unprotect(f, dxpl_id, H5AC_BT2_LEAF, leaf_addr, leaf, leaf_flags) < 0)
+ if(leaf && H5AC_unprotect(f, dxpl_id, H5AC_BT2_LEAF, leaf_addr, leaf, leaf_flags) < 0)
HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release leaf B-tree node")
FUNC_LEAVE_NOAPI(ret_value)
@@ -2329,7 +2329,6 @@ H5B2_remove_internal(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
unsigned internal_flags = H5AC__NO_FLAGS_SET;
haddr_t internal_addr; /* Address of internal node */
H5B2_shared_t *shared; /* Pointer to B-tree's shared information */
- unsigned idx; /* Location of record which matches key */
size_t merge_nrec; /* Number of records to merge node at */
hbool_t collapsed_root = FALSE; /* Whether the root was collapsed */
herr_t ret_value = SUCCEED;
@@ -2339,7 +2338,7 @@ H5B2_remove_internal(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
/* Check arguments. */
HDassert(f);
HDassert(bt2_shared);
- HDassert(depth>0);
+ HDassert(depth > 0);
HDassert(parent_cache_info);
HDassert(parent_cache_info_flags_ptr);
HDassert(curr_node_ptr);
@@ -2391,6 +2390,7 @@ H5B2_remove_internal(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
} /* end if */
/* Merge or redistribute child node pointers, if necessary */
else {
+ unsigned idx; /* Location of record which matches key */
int cmp = 0; /* Comparison value of records */
unsigned retries; /* Number of times to attempt redistribution */
@@ -2513,6 +2513,349 @@ done:
/*-------------------------------------------------------------------------
+ * Function: H5B2_remove_leaf_by_idx
+ *
+ * Purpose: Removes a record from a B-tree leaf node, according to the
+ * offset in the B-tree records.
+ *
+ * Return: Non-negative on success/Negative on failure
+ *
+ * Programmer: Quincey Koziol
+ * koziol@hdfgroup.org
+ * Nov 14 2006
+ *
+ *-------------------------------------------------------------------------
+ */
+herr_t
+H5B2_remove_leaf_by_idx(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
+ H5B2_node_ptr_t *curr_node_ptr, unsigned idx, H5B2_remove_t op,
+ void *op_data)
+{
+ H5B2_leaf_t *leaf; /* Pointer to leaf node */
+ haddr_t leaf_addr = HADDR_UNDEF; /* Leaf address on disk */
+ unsigned leaf_flags = H5AC__NO_FLAGS_SET; /* Flags for unprotecting leaf node */
+ H5B2_shared_t *shared; /* Pointer to B-tree's shared information */
+ herr_t ret_value = SUCCEED;
+
+ FUNC_ENTER_NOAPI_NOINIT(H5B2_remove_leaf_by_idx)
+
+ /* Check arguments. */
+ HDassert(f);
+ HDassert(bt2_shared);
+ HDassert(curr_node_ptr);
+ HDassert(H5F_addr_defined(curr_node_ptr->addr));
+
+ /* Lock B-tree leaf node */
+ leaf_addr = curr_node_ptr->addr;
+ if(NULL == (leaf = H5AC_protect(f, dxpl_id, H5AC_BT2_LEAF, leaf_addr, &(curr_node_ptr->node_nrec), bt2_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(bt2_shared);
+ HDassert(shared);
+
+ /* Sanity check number of records */
+ HDassert(curr_node_ptr->all_nrec == curr_node_ptr->node_nrec);
+ HDassert(leaf->nrec == curr_node_ptr->node_nrec);
+ HDassert(idx < leaf->nrec);
+
+ /* Make 'remove' callback if there is one */
+ if(op)
+ if((op)(H5B2_LEAF_NREC(leaf, shared, idx), op_data) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTDELETE, FAIL, "unable to remove record into leaf node")
+
+ /* Update number of records in node */
+ leaf->nrec--;
+
+ /* Mark leaf node as dirty also */
+ leaf_flags |= H5AC__DIRTIED_FLAG;
+
+ if(leaf->nrec > 0) {
+ /* Pack record out of leaf */
+ if(idx < leaf->nrec)
+ HDmemmove(H5B2_LEAF_NREC(leaf, shared, idx), H5B2_LEAF_NREC(leaf, shared, (idx + 1)), shared->type->nrec_size * (leaf->nrec-idx));
+ } /* end if */
+ else {
+ /* Release space for B-tree node on disk */
+ if(H5MF_xfree(f, H5FD_MEM_BTREE, dxpl_id, leaf_addr, (hsize_t)shared->node_size) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTFREE, FAIL, "unable to free B-tree leaf node")
+
+ /* Let the cache know that the object is deleted */
+ leaf_flags |= H5AC__DELETED_FLAG;
+
+ /* Reset address of parent node pointer */
+ curr_node_ptr->addr = HADDR_UNDEF;
+ } /* end else */
+
+ /* Update record count for parent of leaf node */
+ curr_node_ptr->node_nrec--;
+
+done:
+ /* Release the B-tree leaf node */
+ if(leaf && H5AC_unprotect(f, dxpl_id, H5AC_BT2_LEAF, leaf_addr, leaf, leaf_flags) < 0)
+ HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release leaf B-tree node")
+
+ FUNC_LEAVE_NOAPI(ret_value)
+} /* H5B2_remove_leaf_by_idx() */
+
+
+/*-------------------------------------------------------------------------
+ * Function: H5B2_remove_internal_by_idx
+ *
+ * Purpose: Removes a record from a B-tree node, according to the offset
+ * in the B-tree records
+ *
+ * Return: Non-negative on success/Negative on failure
+ *
+ * Programmer: Quincey Koziol
+ * koziol@hdfgroup.org
+ * Nov 14 2006
+ *
+ *-------------------------------------------------------------------------
+ */
+herr_t
+H5B2_remove_internal_by_idx(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
+ hbool_t *depth_decreased, void *swap_loc, unsigned depth,
+ H5AC_info_t *parent_cache_info, unsigned *parent_cache_info_flags_ptr,
+ H5B2_node_ptr_t *curr_node_ptr, hsize_t n, H5B2_remove_t op,
+ void *op_data)
+{
+ H5AC_info_t *new_cache_info; /* Pointer to new cache info */
+ unsigned *new_cache_info_flags_ptr = NULL;
+ H5B2_node_ptr_t *new_node_ptr; /* Pointer to new node pointer */
+ H5B2_internal_t *internal; /* Pointer to internal node */
+ unsigned internal_flags = H5AC__NO_FLAGS_SET;
+ haddr_t internal_addr; /* Address of internal node */
+ H5B2_shared_t *shared; /* Pointer to B-tree's shared information */
+ size_t merge_nrec; /* Number of records to merge node at */
+ hbool_t collapsed_root = FALSE; /* Whether the root was collapsed */
+ herr_t ret_value = SUCCEED;
+
+ FUNC_ENTER_NOAPI_NOINIT(H5B2_remove_internal_by_idx)
+
+ /* Check arguments. */
+ HDassert(f);
+ HDassert(bt2_shared);
+ HDassert(depth > 0);
+ HDassert(parent_cache_info);
+ HDassert(parent_cache_info_flags_ptr);
+ HDassert(curr_node_ptr);
+ HDassert(H5F_addr_defined(curr_node_ptr->addr));
+
+ /* Lock current B-tree node */
+ internal_addr = curr_node_ptr->addr;
+ if(NULL == (internal = H5B2_protect_internal(f, dxpl_id, bt2_shared, internal_addr, curr_node_ptr->node_nrec, depth, H5AC_WRITE)))
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load B-tree internal node")
+
+ /* Get the pointer to the shared B-tree info */
+ shared = H5RC_GET_OBJ(bt2_shared);
+ HDassert(shared);
+
+ /* Determine the correct number of records to merge at */
+ merge_nrec = shared->node_info[depth - 1].merge_nrec;
+
+ /* Check for needing to collapse the root node */
+ /* (The root node is the only internal node allowed to have 1 record) */
+ if(internal->nrec == 1 &&
+ ((internal->node_ptrs[0].node_nrec + internal->node_ptrs[1].node_nrec) <= ((merge_nrec * 2) + 1))) {
+
+ /* Merge children of root node */
+ if(H5B2_merge2(f, dxpl_id, depth, curr_node_ptr,
+ parent_cache_info_flags_ptr, internal, &internal_flags, 0) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, FAIL, "unable to merge child node")
+
+ /* Release space for root B-tree node on disk */
+ if(H5MF_xfree(f, H5FD_MEM_BTREE, dxpl_id, internal_addr, (hsize_t)shared->node_size) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTFREE, FAIL, "unable to free B-tree leaf node")
+
+ /* Let the cache know that the object is deleted */
+ internal_flags |= H5AC__DELETED_FLAG;
+
+ /* Reset information in header's root node pointer */
+ curr_node_ptr->addr = internal->node_ptrs[0].addr;
+ curr_node_ptr->node_nrec = internal->node_ptrs[0].node_nrec;
+
+ /* Indicate that the level of the B-tree decreased */
+ *depth_decreased = TRUE;
+
+ /* Set pointers for advancing to child node */
+ new_cache_info = parent_cache_info;
+ new_cache_info_flags_ptr = parent_cache_info_flags_ptr;
+ new_node_ptr = curr_node_ptr;
+
+ /* Set flag to indicate root was collapsed */
+ collapsed_root = TRUE;
+ } /* end if */
+ /* Merge or redistribute child node pointers, if necessary */
+ else {
+ unsigned idx; /* Location of record which matches key */
+ hbool_t found = FALSE; /* Comparison value of records */
+ unsigned retries; /* Number of times to attempt redistribution */
+
+ /* Locate node pointer for child */
+ if(swap_loc)
+ idx = 0;
+ else {
+ /* Search for record with correct index */
+ for(idx = 0; idx < internal->nrec; idx++) {
+HDfprintf(stderr, "%s: Check 1.0 - internal->node_ptrs[%u].all_nrec = %Hu\n", FUNC, idx, internal->node_ptrs[idx].all_nrec);
+HDfprintf(stderr, "%s: n = %Hu\n", FUNC, n);
+ /* Check which child node contains indexed record */
+ if(internal->node_ptrs[idx].all_nrec >= n) {
+ /* Check if record is in this node */
+ if(internal->node_ptrs[idx].all_nrec == n) {
+ found = TRUE;
+ n = 0;
+ } /* end if */
+
+ /* Increment to next record */
+ idx++;
+
+ /* Break out of loop early */
+ break;
+ } /* end if */
+
+ /* Decrement index we are looking for to account for the node we
+ * just advanced past.
+ */
+ n -= (internal->node_ptrs[idx].all_nrec + 1);
+ } /* end for */
+ } /* end else */
+HDfprintf(stderr, "%s: Check 1.0 - idx = %u\n", FUNC, idx);
+HDfprintf(stderr, "%s: n = %Hu\n", FUNC, n);
+
+ /* Set the number of redistribution retries */
+ /* This takes care of the case where a B-tree node needs to be
+ * redistributed, but redistributing the node causes the index
+ * for removal to move to another node, which also needs to be
+ * redistributed. Now, we loop trying to redistribute and then
+ * eventually force a merge */
+ retries = 2;
+
+ /* Preemptively merge/redistribute a node we will enter */
+ while(internal->node_ptrs[idx].node_nrec == merge_nrec) {
+ /* Attempt to redistribute records among children */
+ /* (NOTE: These 2-node redistributions should actually get the
+ * record to promote from the node with more records. - QAK)
+ */
+ if(idx == 0) { /* Left-most child */
+ if(retries > 0 && (internal->node_ptrs[idx + 1].node_nrec > merge_nrec)) {
+ if(H5B2_redistribute2(f, dxpl_id, depth, internal, idx) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTREDISTRIBUTE, FAIL, "unable to redistribute child node records")
+ } /* end if */
+ else {
+ if(H5B2_merge2(f, dxpl_id, depth, curr_node_ptr,
+ parent_cache_info_flags_ptr, internal, &internal_flags, idx) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, FAIL, "unable to merge child node")
+ } /* end else */
+ } /* end if */
+ else if(idx == internal->nrec) { /* Right-most child */
+ if(retries > 0 && (internal->node_ptrs[idx - 1].node_nrec > merge_nrec)) {
+ if(H5B2_redistribute2(f, dxpl_id, depth, internal, (idx - 1)) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTREDISTRIBUTE, FAIL, "unable to redistribute child node records")
+ } /* end if */
+ else {
+ if(H5B2_merge2(f, dxpl_id, depth, curr_node_ptr,
+ parent_cache_info_flags_ptr, internal, &internal_flags, (idx - 1)) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, FAIL, "unable to merge child node")
+ } /* end else */
+ } /* end if */
+ else { /* Middle child */
+ if(retries > 0 && ((internal->node_ptrs[idx + 1].node_nrec > merge_nrec) ||
+ (internal->node_ptrs[idx - 1].node_nrec > merge_nrec))) {
+ if(H5B2_redistribute3(f, dxpl_id, depth, internal, &internal_flags, idx) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTREDISTRIBUTE, FAIL, "unable to redistribute child node records")
+ } /* end if */
+ else {
+ if(H5B2_merge3(f, dxpl_id, depth, curr_node_ptr,
+ parent_cache_info_flags_ptr, internal, &internal_flags, idx) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, FAIL, "unable to merge child node")
+ } /* end else */
+ } /* end else */
+
+ /* Locate node pointer for child (after merge/redistribute) */
+ if(swap_loc)
+ idx = 0;
+ else {
+ /* Search for record with correct index */
+ for(idx = 0; idx < internal->nrec; idx++) {
+HDfprintf(stderr, "%s: Check 2.0 - internal->node_ptrs[%u].all_nrec = %Hu\n", FUNC, idx, internal->node_ptrs[idx].all_nrec);
+HDfprintf(stderr, "%s: n = %Hu\n", FUNC, n);
+ /* Check which child node contains indexed record */
+ if(internal->node_ptrs[idx].all_nrec >= n) {
+ /* Check if record is in this node */
+ if(internal->node_ptrs[idx].all_nrec == n) {
+ found = TRUE;
+ n = 0;
+ } /* end if */
+
+ /* Increment to next record */
+ idx++;
+
+ /* Break out of loop early */
+ break;
+ } /* end if */
+
+ /* Decrement index we are looking for to account for the node we
+ * just advanced past.
+ */
+ n -= (internal->node_ptrs[idx].all_nrec + 1);
+ } /* end for */
+ } /* end else */
+HDfprintf(stderr, "%s: Check 2.0 - idx = %u\n", FUNC, idx);
+HDfprintf(stderr, "%s: n = %Hu\n", FUNC, n);
+
+ /* Decrement the number of redistribution retries left */
+ retries--;
+ } /* end while */
+
+ /* Handle deleting a record from an internal node */
+ if(!swap_loc && found)
+ swap_loc = H5B2_INT_NREC(internal, shared, idx - 1);
+
+ /* Swap record to delete with record from leaf, if we are the last internal node */
+ if(swap_loc && depth == 1)
+ if(H5B2_swap_leaf(f, dxpl_id, depth, internal, &internal_flags, idx, swap_loc) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTSWAP, FAIL, "can't swap records in B-tree")
+
+ /* Set pointers for advancing to child node */
+ new_cache_info_flags_ptr = &internal_flags;
+ new_cache_info = &internal->cache_info;
+ new_node_ptr = &internal->node_ptrs[idx];
+ } /* end else */
+
+ /* Attempt to remove record from child node */
+ if(depth > 1) {
+ if(H5B2_remove_internal_by_idx(f, dxpl_id, bt2_shared, depth_decreased, swap_loc, depth - 1,
+ new_cache_info, new_cache_info_flags_ptr, new_node_ptr, n, op, op_data) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTDELETE, FAIL, "unable to remove record from B-tree internal node")
+ } /* end if */
+ else {
+ if(H5B2_remove_leaf_by_idx(f, dxpl_id, bt2_shared, new_node_ptr, (unsigned)n, op, op_data) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTDELETE, FAIL, "unable to remove record from B-tree leaf node")
+ } /* end else */
+
+ /* Update record count for node pointer to current node */
+ if(!collapsed_root)
+ new_node_ptr->all_nrec--;
+
+ /* Mark node as dirty */
+ internal_flags |= H5AC__DIRTIED_FLAG;
+
+#ifdef H5B2_DEBUG
+ H5B2_assert_internal((!collapsed_root ? (curr_node_ptr->all_nrec-1) : new_node_ptr->all_nrec),shared,internal);
+#endif /* H5B2_DEBUG */
+
+done:
+ /* Release the B-tree internal node */
+ if(internal && H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, internal_addr, internal, internal_flags) < 0)
+ HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release internal B-tree node")
+
+ FUNC_LEAVE_NOAPI(ret_value)
+} /* H5B2_remove_internal_by_idx() */
+
+
+/*-------------------------------------------------------------------------
* Function: H5B2_neighbor_leaf
*
* Purpose: Locate a record relative to the specified information in a
diff --git a/src/H5B2pkg.h b/src/H5B2pkg.h
index 936216c..21b0974 100644
--- a/src/H5B2pkg.h
+++ b/src/H5B2pkg.h
@@ -287,6 +287,13 @@ H5_DLL herr_t H5B2_remove_internal(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
H5_DLL herr_t H5B2_remove_leaf(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
H5B2_node_ptr_t *curr_node_ptr, void *udata, H5B2_remove_t op,
void *op_data);
+H5_DLL herr_t H5B2_remove_internal_by_idx(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
+ hbool_t *depth_decreased, void *swap_loc, unsigned depth, H5AC_info_t *parent_cache_info,
+ hbool_t * parent_cache_info_dirtied_ptr, H5B2_node_ptr_t *curr_node_ptr, hsize_t idx,
+ H5B2_remove_t op, void *op_data);
+H5_DLL herr_t H5B2_remove_leaf_by_idx(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
+ H5B2_node_ptr_t *curr_node_ptr, unsigned idx, H5B2_remove_t op,
+ void *op_data);
/* Routines for deleting nodes */
H5_DLL herr_t H5B2_delete_node(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
diff --git a/src/H5B2private.h b/src/H5B2private.h
index 4973dc2..48d0cbd 100644
--- a/src/H5B2private.h
+++ b/src/H5B2private.h
@@ -139,6 +139,9 @@ H5_DLL herr_t H5B2_modify(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type,
haddr_t addr, void *udata, H5B2_modify_t op, void *op_data);
H5_DLL herr_t H5B2_remove(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type,
haddr_t addr, void *udata, H5B2_remove_t op, void *op_data);
+H5_DLL herr_t H5B2_remove_by_idx(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type,
+ haddr_t addr, H5_iter_order_t order, hsize_t idx, H5B2_remove_t op,
+ void *op_data);
H5_DLL herr_t H5B2_get_nrec(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type,
haddr_t addr, hsize_t *nrec);
H5_DLL herr_t H5B2_delete(H5F_t *f, hid_t dxpl_id, const H5B2_class_t *type,
diff --git a/test/btree2.c b/test/btree2.c
index d86e2de..bcbff18 100644
--- a/test/btree2.c
+++ b/test/btree2.c
@@ -6509,6 +6509,166 @@ error:
/*-------------------------------------------------------------------------
+ * Function: test_remove_by_idx
+ *
+ * Purpose: Basic tests for the B-tree v2 code. This test inserts many
+ * records in random order, enough to make at a level 4 B-tree
+ * and then removes them all, by index.
+ *
+ * Return: Success: 0
+ * Failure: 1
+ *
+ * Programmer: Quincey Koziol
+ * Tuesday, November 14, 2006
+ *
+ *-------------------------------------------------------------------------
+ */
+static int
+test_remove_by_idx(hid_t fapl)
+{
+ hid_t file = -1;
+ char filename[1024];
+ H5F_t *f = NULL;
+ hsize_t record; /* Record to insert into tree */
+ hsize_t rrecord; /* Record to remove from tree */
+ haddr_t bt2_addr; /* Address of B-tree created */
+ haddr_t root_addr; /* Address of root of B-tree created */
+ time_t curr_time; /* Current time, for seeding random number generator */
+ hsize_t *records; /* Record #'s for random insertion */
+ unsigned u; /* Local index variable */
+ unsigned swap_idx; /* Location to swap with when shuffling */
+ unsigned rem_idx; /* Location to remove */
+ hsize_t temp_rec; /* Temporary record */
+ H5B2_stat_t bt2_stat; /* Statistics about B-tree created */
+ hsize_t nrec; /* Number of records in B-tree */
+
+ /* Initialize random number seed */
+ curr_time = HDtime(NULL);
+#ifndef QAK
+curr_time = 1163537969;
+HDfprintf(stderr, "curr_time = %lu\n", (unsigned long)curr_time);
+#endif /* QAK */
+ HDsrandom((unsigned long)curr_time);
+
+ /*
+ * Test removing many records into v2 B-tree
+ */
+ TESTING("B-tree remove: create random level 4 B-tree and delete all records by index");
+
+ /* Allocate space for the records */
+ if(NULL == (records = HDmalloc(sizeof(hsize_t) * INSERT_MANY)))
+ TEST_ERROR
+
+ /* Initialize record #'s */
+ for(u = 0; u < INSERT_MANY; u++)
+ records[u] = u;
+
+ /* Shuffle record #'s */
+ for(u = 0; u < INSERT_MANY; u++) {
+ swap_idx = (unsigned)(HDrandom() % (INSERT_MANY - u)) + u;
+ temp_rec = records[u];
+ records[u] = records[swap_idx];
+ records[swap_idx] = temp_rec;
+ } /* end for */
+
+ h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
+
+ /* Create the file to work on */
+ if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
+ STACK_ERROR
+
+ /* Get a pointer to the internal file object */
+ if(NULL == (f = H5I_object(file)))
+ STACK_ERROR
+
+ /*
+ * Create v2 B-tree
+ */
+ if(H5B2_create(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, 512, 8, 100, 40, &bt2_addr/*out*/) < 0)
+ FAIL_STACK_ERROR
+
+ /* Insert random records */
+ for(u = 0; u < INSERT_MANY; u++) {
+ record = records[u];
+ if(H5B2_insert(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &record) < 0)
+ FAIL_STACK_ERROR
+ } /* end for */
+
+ /* Check up on B-tree */
+ if(H5B2_stat_info(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &bt2_stat) < 0)
+ FAIL_STACK_ERROR
+ if(bt2_stat.depth != 4)
+ TEST_ERROR
+
+ /* Close file */
+ if(H5Fclose(file) < 0)
+ STACK_ERROR
+
+ /* Re-open the file */
+ if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0)
+ FAIL_STACK_ERROR
+
+ /* Get a pointer to the internal file object */
+ if(NULL == (f = H5I_object(file)))
+ FAIL_STACK_ERROR
+
+ /* Remove all records */
+ for(u = 0; u < INSERT_MANY; u++) {
+ /* Pick a record index to remove from randomly */
+ rem_idx = (unsigned)(HDrandom() % (INSERT_MANY - u));
+ rrecord = HSIZET_MAX;
+ if(H5B2_remove_by_idx(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, H5_ITER_INC, (hsize_t)rem_idx, remove_cb, &rrecord) < 0)
+ FAIL_STACK_ERROR
+
+ /* Make certain that the record value is correct */
+ if(rrecord >= INSERT_MANY)
+{
+HDfprintf(stderr, "u = %u\n", u);
+HDfprintf(stderr, "rem_idx = %u\n", rem_idx);
+HDfprintf(stderr, "rrecord = %Hu\n", rrecord);
+HDfprintf(stderr, "curr_time = %lu\n", (unsigned long)curr_time);
+ TEST_ERROR
+}
+
+ /* Query the number of records in the B-tree */
+ if(H5B2_get_nrec(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &nrec) < 0)
+ FAIL_STACK_ERROR
+
+ /* Make certain that the # of records is correct */
+ if(nrec != (INSERT_MANY - (u + 1)))
+ TEST_ERROR
+ } /* end for */
+
+ /* Query the address of the root node in the B-tree */
+ if(H5B2_get_root_addr_test(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &root_addr) < 0)
+ FAIL_STACK_ERROR
+
+ /* Make certain that the address of the root node is defined */
+ if(H5F_addr_defined(root_addr))
+ TEST_ERROR
+
+ /* Close file */
+ if(H5Fclose(file) < 0)
+ STACK_ERROR
+
+ PASSED();
+
+ HDfree(records);
+
+ return 0;
+
+error:
+ H5E_BEGIN_TRY {
+ H5Fclose(file);
+ } H5E_END_TRY;
+
+ HDfree(records);
+
+ return 1;
+} /* test_remove_by_idx() */
+
+
+/*-------------------------------------------------------------------------
* Function: test_find_neighbor
*
* Purpose: Basic tests for the B-tree v2 code. This test exercises
@@ -7272,6 +7432,13 @@ main(void)
else
nerrors += test_remove_lots(fapl);
+#ifdef QAK
+ /* Test removing records by index */
+ nerrors += test_remove_by_idx(fapl);
+#else /* QAK */
+HDfprintf(stderr, "Uncomment tests!\n");
+#endif /* QAK */
+
/* Test more complex B-tree queries */
nerrors += test_find_neighbor(fapl);
@@ -7283,21 +7450,23 @@ main(void)
if(nerrors)
goto error;
+
puts("All v2 B-tree tests passed.");
+
h5_cleanup(FILENAME, fapl);
- }
+ } /* end if */
else
- {
puts("All v2 B-tree tests skipped - Incompatible with current Virtual File Driver");
- }
+
return 0;
- error:
- puts("*** TESTS FAILED ***");
- H5E_BEGIN_TRY {
- H5Pclose(fapl);
- } H5E_END_TRY;
- return 1;
+error:
+ puts("*** TESTS FAILED ***");
+ H5E_BEGIN_TRY {
+ H5Pclose(fapl);
+ } H5E_END_TRY;
+
+ return 1;
} /* end main() */