summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/H5B2.c113
-rw-r--r--src/H5Edefin.h1
-rw-r--r--src/H5Einit.h5
-rw-r--r--src/H5Epubgen.h2
-rw-r--r--src/H5Eterm.h1
-rw-r--r--src/H5err.txt1
-rw-r--r--test/btree2.c163
7 files changed, 284 insertions, 2 deletions
diff --git a/src/H5B2.c b/src/H5B2.c
index 36d57be..0296454 100644
--- a/src/H5B2.c
+++ b/src/H5B2.c
@@ -77,6 +77,8 @@ static herr_t H5B2_merge2(H5F_t *f, hid_t dxpl_id, unsigned depth,
static herr_t H5B2_merge3(H5F_t *f, hid_t dxpl_id, unsigned depth,
H5B2_node_ptr_t *curr_node_ptr, H5AC_info_t *parent_cache_info,
H5B2_internal_t *internal, unsigned idx);
+static herr_t H5B2_swap_child(H5F_t *f, hid_t dxpl_id, unsigned depth,
+ H5B2_internal_t *internal, unsigned idx);
static herr_t H5B2_insert_internal(H5F_t *f, hid_t dxpl_id,
H5RC_t *bt2_shared, unsigned depth, H5AC_info_t *parent_cache_info,
H5B2_node_ptr_t *curr_node_ptr, void *udata);
@@ -2200,6 +2202,109 @@ done:
/*-------------------------------------------------------------------------
+ * Function: H5B2_swap_child
+ *
+ * Purpose: Swap a record in a node with a record in a child node
+ *
+ * Return: Success: Non-negative
+ *
+ * Failure: Negative
+ *
+ * Programmer: Quincey Koziol
+ * koziol@ncsa.uiuc.edu
+ * Mar 4 2005
+ *
+ *-------------------------------------------------------------------------
+ */
+static herr_t
+H5B2_swap_child(H5F_t *f, hid_t dxpl_id, unsigned depth,
+ H5B2_internal_t *internal, unsigned idx)
+{
+ const H5AC_class_t *child_class; /* Pointer to child node's class info */
+ haddr_t child_addr; /* Address of child node */
+ void *child; /* Pointer to child node */
+ unsigned *child_nrec; /* Pointer to child # of records */
+ uint8_t *child_native; /* Pointer to child's native records */
+ H5B2_shared_t *shared; /* B-tree's shared info */
+ herr_t ret_value=SUCCEED; /* Return value */
+
+ FUNC_ENTER_NOAPI_NOINIT(H5B2_swap_child)
+
+ HDassert(f);
+ HDassert(internal);
+
+ /* Get the pointer to the shared B-tree info */
+ shared=H5RC_GET_OBJ(internal->shared);
+ HDassert(shared);
+
+ /* Check for the kind of B-tree node to swap */
+ if(depth>1) {
+ H5B2_internal_t *child_internal; /* Pointer to internal node */
+
+ /* Setup information for unlocking child node */
+ child_class = H5AC_BT2_INT;
+ child_addr = child_internal->node_ptrs[idx].addr;
+
+HDfprintf(stderr,"%s: Swapping with internal node in B-tree, untested!\n",FUNC);
+HGOTO_ERROR(H5E_BTREE, H5E_CANTDELETE, FAIL, "Can't delete record in B-tree")
+ /* Lock B-tree child nodes */
+ if (NULL == (child_internal = H5AC_protect(f, dxpl_id, child_class, child_addr, &(internal->node_ptrs[idx].node_nrec), internal->shared, H5AC_WRITE)))
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load B-tree internal node")
+
+ /* More setup for accessing child node information */
+ child = child_internal;
+ child_nrec = &(child_internal->nrec);
+ child_native = child_internal->int_native;
+
+ /* Mark child node as dirty now */
+ child_internal->cache_info.is_dirty = TRUE;
+ } /* end if */
+ else {
+ H5B2_leaf_t *child_leaf; /* Pointer to leaf node */
+
+ /* Setup information for unlocking child nodes */
+ child_class = H5AC_BT2_LEAF;
+ child_addr = internal->node_ptrs[idx].addr;
+
+ /* Lock B-tree child node */
+ if (NULL == (child_leaf = H5AC_protect(f, dxpl_id, child_class, child_addr, &(internal->node_ptrs[idx].node_nrec), internal->shared, H5AC_WRITE)))
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load B-tree leaf node")
+
+ /* More setup for accessing child node information */
+ child = child_leaf;
+ child_nrec = &(child_leaf->nrec);
+ child_native = child_leaf->leaf_native;
+
+ /* Mark child node as dirty now */
+ child_leaf->cache_info.is_dirty = TRUE;
+ } /* end else */
+
+ /* Swap records (use disk page as temporary buffer) */
+ HDmemcpy(shared->page, H5B2_NAT_NREC(child_native,shared,0), shared->type->nrec_size);
+ HDmemcpy(H5B2_NAT_NREC(child_native,shared,0), H5B2_INT_NREC(internal,shared,idx-1), shared->type->nrec_size);
+ HDmemcpy(H5B2_INT_NREC(internal,shared,idx-1), shared->page, shared->type->nrec_size);
+
+ /* Mark parent as dirty */
+ internal->cache_info.is_dirty = TRUE;
+
+#ifdef H5B2_DEBUG
+ H5B2_assert_internal((hsize_t)0,shared,internal);
+ if(depth>1)
+ H5B2_assert_internal(internal->node_ptrs[idx].all_nrec,shared,child);
+ else
+ H5B2_assert_leaf(shared,child);
+#endif /* H5B2_DEBUG */
+
+ /* Unlock child node */
+ if (H5AC_unprotect(f, dxpl_id, child_class, child_addr, child, H5AC__NO_FLAGS_SET) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")
+
+done:
+ FUNC_LEAVE_NOAPI(ret_value);
+} /* end H5B2_swap_child */
+
+
+/*-------------------------------------------------------------------------
* Function: H5B2_insert_leaf
*
* Purpose: Adds a new record to a B-tree leaf node.
@@ -3304,8 +3409,12 @@ H5B2_remove_internal(H5F_t *f, hid_t dxpl_id, H5RC_t *bt2_shared,
/* Handle deleting a record from an internal node */
if(cmp==0) {
-HDfprintf(stderr,"%s: Deleting record in internal node of non-trival B-tree!\n",FUNC);
-HGOTO_ERROR(H5E_BTREE, H5E_CANTDELETE, FAIL, "Can't delete record in B-tree")
+ /* Increment the index to work with */
+ idx++;
+
+ /* Swap record to delete with record from child */
+ if(H5B2_swap_child(f,dxpl_id,depth,internal,idx) < 0)
+ HGOTO_ERROR(H5E_BTREE, H5E_CANTSWAP, FAIL, "Can't swap records in B-tree")
} /* end if */
diff --git a/src/H5Edefin.h b/src/H5Edefin.h
index 22f825c..b081b16 100644
--- a/src/H5Edefin.h
+++ b/src/H5Edefin.h
@@ -163,6 +163,7 @@ hid_t H5E_CANTENCODE_g = FAIL; /* Unable to encode value */
hid_t H5E_CANTDECODE_g = FAIL; /* Unable to decode value */
hid_t H5E_CANTSPLIT_g = FAIL; /* Unable to split node */
hid_t H5E_CANTREDISTRIBUTE_g = FAIL; /* Unable to redistribute records */
+hid_t H5E_CANTSWAP_g = FAIL; /* Unable to swap records */
hid_t H5E_CANTINSERT_g = FAIL; /* Unable to insert object */
hid_t H5E_CANTLIST_g = FAIL; /* Unable to list node */
diff --git a/src/H5Einit.h b/src/H5Einit.h
index 05a2aa1..8164584 100644
--- a/src/H5Einit.h
+++ b/src/H5Einit.h
@@ -605,6 +605,11 @@ if((msg = H5E_create_msg(cls, H5E_MINOR, "Unable to redistribute records"))==NUL
HGOTO_ERROR(H5E_ERROR, H5E_CANTINIT, FAIL, "error message initialization failed")
if((H5E_CANTREDISTRIBUTE_g = H5I_register(H5I_ERROR_MSG, msg))<0)
HGOTO_ERROR(H5E_ERROR, H5E_CANTREGISTER, FAIL, "can't register error message")
+assert(H5E_CANTSWAP_g==(-1));
+if((msg = H5E_create_msg(cls, H5E_MINOR, "Unable to swap records"))==NULL)
+ HGOTO_ERROR(H5E_ERROR, H5E_CANTINIT, FAIL, "error message initialization failed")
+if((H5E_CANTSWAP_g = H5I_register(H5I_ERROR_MSG, msg))<0)
+ HGOTO_ERROR(H5E_ERROR, H5E_CANTREGISTER, FAIL, "can't register error message")
assert(H5E_CANTINSERT_g==(-1));
if((msg = H5E_create_msg(cls, H5E_MINOR, "Unable to insert object"))==NULL)
HGOTO_ERROR(H5E_ERROR, H5E_CANTINIT, FAIL, "error message initialization failed")
diff --git a/src/H5Epubgen.h b/src/H5Epubgen.h
index a21989f..6a4fcd1 100644
--- a/src/H5Epubgen.h
+++ b/src/H5Epubgen.h
@@ -271,6 +271,7 @@ H5_DLLVAR hid_t H5E_CANTCOMPARE_g; /* Can't compare objects */
#define H5E_CANTDECODE (H5OPEN H5E_CANTDECODE_g)
#define H5E_CANTSPLIT (H5OPEN H5E_CANTSPLIT_g)
#define H5E_CANTREDISTRIBUTE (H5OPEN H5E_CANTREDISTRIBUTE_g)
+#define H5E_CANTSWAP (H5OPEN H5E_CANTSWAP_g)
#define H5E_CANTINSERT (H5OPEN H5E_CANTINSERT_g)
#define H5E_CANTLIST (H5OPEN H5E_CANTLIST_g)
H5_DLLVAR hid_t H5E_NOTFOUND_g; /* Object not found */
@@ -279,6 +280,7 @@ H5_DLLVAR hid_t H5E_CANTENCODE_g; /* Unable to encode value */
H5_DLLVAR hid_t H5E_CANTDECODE_g; /* Unable to decode value */
H5_DLLVAR hid_t H5E_CANTSPLIT_g; /* Unable to split node */
H5_DLLVAR hid_t H5E_CANTREDISTRIBUTE_g; /* Unable to redistribute records */
+H5_DLLVAR hid_t H5E_CANTSWAP_g; /* Unable to swap records */
H5_DLLVAR hid_t H5E_CANTINSERT_g; /* Unable to insert object */
H5_DLLVAR hid_t H5E_CANTLIST_g; /* Unable to list node */
diff --git a/src/H5Eterm.h b/src/H5Eterm.h
index ca9413a..67b3415 100644
--- a/src/H5Eterm.h
+++ b/src/H5Eterm.h
@@ -165,6 +165,7 @@ H5E_CANTENCODE_g=
H5E_CANTDECODE_g=
H5E_CANTSPLIT_g=
H5E_CANTREDISTRIBUTE_g=
+H5E_CANTSWAP_g=
H5E_CANTINSERT_g=
H5E_CANTLIST_g=
diff --git a/src/H5err.txt b/src/H5err.txt
index 3836cb0..dcfb5b0 100644
--- a/src/H5err.txt
+++ b/src/H5err.txt
@@ -163,6 +163,7 @@ MINOR, BTREE, H5E_CANTENCODE, Unable to encode value
MINOR, BTREE, H5E_CANTDECODE, Unable to decode value
MINOR, BTREE, H5E_CANTSPLIT, Unable to split node
MINOR, BTREE, H5E_CANTREDISTRIBUTE, Unable to redistribute records
+MINOR, BTREE, H5E_CANTSWAP, Unable to swap records
MINOR, BTREE, H5E_CANTINSERT, Unable to insert object
MINOR, BTREE, H5E_CANTLIST, Unable to list node
diff --git a/test/btree2.c b/test/btree2.c
index ec2da8e..d2d3547 100644
--- a/test/btree2.c
+++ b/test/btree2.c
@@ -2823,6 +2823,168 @@ error:
/*-------------------------------------------------------------------------
+ * Function: test_remove_level1_promote
+ *
+ * Purpose: Basic tests for the B-tree v2 code
+ *
+ * Return: Success: 0
+ *
+ * Failure: 1
+ *
+ * Programmer: Quincey Koziol
+ * Friday, March 4, 2005
+ *
+ * Modifications:
+ *
+ *-------------------------------------------------------------------------
+ */
+static int
+test_remove_level1_promote(hid_t fapl)
+{
+ hid_t file=-1;
+ char filename[1024];
+ H5F_t *f=NULL;
+ hsize_t record; /* Record to insert into tree */
+ hsize_t nrec; /* Number of records in B-tree */
+ haddr_t bt2_addr; /* Address of B-tree created */
+ haddr_t root_addr; /* Address of root of B-tree created */
+ unsigned u; /* Local index variable */
+
+ 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) TEST_ERROR;
+
+ /* Get a pointer to the internal file object */
+ if (NULL==(f=H5I_object(file))) {
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ TEST_ERROR;
+ }
+
+ /*
+ * Test v2 B-tree creation
+ */
+ if (H5B2_create(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, 512, 8, 100, 40, &bt2_addr/*out*/)<0) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ }
+
+ /* Create level-1 B-tree with 5 leaves */
+ for(u=0; u<INSERT_SPLIT_ROOT_NREC*3; u++) {
+ record=u;
+ if (H5B2_insert(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &record)<0) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto 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) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ } /* end if */
+
+ /* Make certain that the # of records is correct */
+ if(nrec != (INSERT_SPLIT_ROOT_NREC*3)) TEST_ERROR;
+
+ /* Query the address of the root node in the B-tree */
+ if (H5B2_get_root_addr(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &root_addr)<0) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ } /* end if */
+
+ /* Make certain that the address of the root node is defined */
+ if(!H5F_addr_defined(root_addr)) TEST_ERROR;
+
+ /* Attempt to remove record from root node of a level-1 B-tree to force promotion from right leaf */
+ TESTING("B-tree remove: promote from right leaf of level-1 B-tree");
+ record = 181;
+ if(H5B2_remove(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &record)<0) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ } /* end if */
+
+ /* Make certain that the record value is correct */
+ if(record != 181) 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) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ } /* end if */
+
+ /* Make certain that the # of records is correct */
+ if(nrec != (INSERT_SPLIT_ROOT_NREC*3)-1) TEST_ERROR;
+
+ PASSED();
+
+ /* Attempt to remove record from root node of a level-1 B-tree to force promotion from left leaf */
+ TESTING("B-tree remove: promote from left leaf of level-1 B-tree");
+ record = 42;
+ if(H5B2_remove(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &record)<0) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ } /* end if */
+
+ /* Make certain that the record value is correct */
+ if(record != 42) 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) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ } /* end if */
+
+ /* Make certain that the # of records is correct */
+ if(nrec != (INSERT_SPLIT_ROOT_NREC*3)-2) TEST_ERROR;
+
+ PASSED();
+
+ /* Attempt to remove record from root node of a level-1 B-tree to force promotion from middle leaf */
+ TESTING("B-tree remove: promote from middle leaf of level-1 B-tree");
+ record = 85;
+ if(H5B2_remove(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &record)<0) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ } /* end if */
+
+ /* Make certain that the record value is correct */
+ if(record != 85) 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) {
+ H5_FAILED();
+ H5Eprint_stack(H5E_DEFAULT, stdout);
+ goto error;
+ } /* end if */
+
+ /* Make certain that the # of records is correct */
+ if(nrec != (INSERT_SPLIT_ROOT_NREC*3)-3) TEST_ERROR;
+
+ PASSED();
+
+ if (H5Fclose(file)<0) TEST_ERROR;
+
+ return 0;
+
+error:
+ H5E_BEGIN_TRY {
+ H5Fclose(file);
+ } H5E_END_TRY;
+ return 1;
+} /* test_remove_level1_promote() */
+
+
+/*-------------------------------------------------------------------------
* Function: main
*
* Purpose: Test the B-tree v2 code
@@ -2877,6 +3039,7 @@ HDfprintf(stderr,"Uncomment tests!\n");
nerrors += test_remove_level1_redistrib(fapl);
nerrors += test_remove_level1_2leaf_merge(fapl);
nerrors += test_remove_level1_3leaf_merge(fapl);
+ nerrors += test_remove_level1_promote(fapl);
#else /* QAK */
HDfprintf(stderr,"Uncomment tests!\n");
#endif /* QAK */