/* La encontre!, retorno */
if (node_header.level == 0) {
DelKeyFromLeaf (k->getClave (), node_num, padre);
+ } else {
+ uint left, right;
+ if (it == posterior) {
+ left = lchild->getChild ();
+ right = (*it)->getChild ();
+ } else {
+ left = (*ultima)->getChild ();
+ right = (*it)->getChild ();
+ }
+ std::cout << "Eliminar de Nodo con hijos : " << left << " y " << right << std::endl;
+ DelKeyFromNode (k->getClave (), node_num, padre, left, right);
}
return;
}
WriteBlock (node, node_num);
/* Veo si se cumple la condición de minimalidad */
- if ((node_header.free_space <= (header.block_size/2)) && (node_num != 0)) {
+ if ((node_header.free_space <= ((header.block_size-sizeof(BTreeNodeHeader))/2)) && (node_num != 0)) {
/* Oops! Debo pedir prestada clave */
uint hi, hd;
Clave *pedida;
Clave *k;
uint free = node_header.free_space + (*it)->Size ();
- if (free > (header.block_size/2)) {
+ if (free > ((header.block_size - sizeof (BTreeNodeHeader))/2)) {
WriteKeys (node, node_header, node_keys);
WriteNodoHeader (node, &node_header);
WriteBlock (node, node_num);
return ret;
}
-void BTree::DelKeyFromOther (const Clave &k, BTreeFindResult *r)
+void BTree::DelKeyFromNode (Clave *k, uint node_num, uint padre, uint left, uint right)
{
+ uint padre_hijo;
+ uchar *node;
+ BTreeNodeHeader node_header;
+ std::list<BTreeData *> node_keys;
+
+ node = ReadBlock (padre);
+ ReadNodoHeader (node, &node_header);
+ node_keys = ReadKeys (node, node_header);
+
+ if (right != 0) {
+ std::cout << "Busco para la derecha y luego todo a la izquierda\n";
+ uchar *node_r;
+ BTreeNodeHeader node_hr;
+ std::list<BTreeData *> node_keyr;
+
+ /* Busco la clave inmediatamente superior en el arbol */
+ padre_hijo = node_num;
+ do {
+ node_r = ReadBlock (right);
+ ReadNodoHeader (node_r, &node_hr);
+ if (node_hr.level != 0) {
+ BTreeData *data_r;
+ node_keyr = ReadKeys (node_r, node_hr);
+ data_r = *(node_keyr.begin ());
+ padre_hijo = right;
+ right = data_r->getChild ();
+
+ DeleteKeys (node_keyr);
+ delete [] node_r;
+ }
+ } while (node_hr.level != 0);
+
+ std::cout << "Voy a reemplazar en el nodo " << right << std::endl;
+
+ /* Reemplazo la clave a borrar por la de la hoja */
+ node_keyr = ReadKeys (node_r, node_hr);
+ BTreeData *reemplazar = *(node_keyr.begin ());
+
+ std::string ss = *reemplazar;
+ std::cout << "Voy a reemplazar por : " << ss << std::endl;
+
+ BTreeData *data = new BTreeLeafData (k->Clone());
+
+ std::list<BTreeData *>::iterator it = node_keys.begin ();
+ while (it != node_keys.end ()) {
+ if ((*data) == (*(*it))) {
+ break;
+ }
+ it++;
+ }
+ (*it)->setClave (reemplazar->getClave ());
+ reemplazar->setClave (k->Clone ());
+
+ std::cout << "Tengo todo reemplazado ...\n";
+
+ /* Grabo los nodos */
+ WriteKeys (node, node_header, node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, padre);
+ DeleteKeys (node_keys);
+ delete [] node;
+
+ WriteKeys (node_r, node_hr, node_keyr);
+ WriteNodoHeader (node_r, &node_hr);
+ WriteBlock (node_r, right);
+ DeleteKeys (node_keyr);
+ delete [] node_r;
+
+ std::cout << "Grabe todo en disco ...\n";
+
+ /* Ahora debo eliminar la clave que puse en el nodo hoja */
+ std::cout << "Borro la clave desde la hoja!\n";
+
+ DelKeyFromLeaf (k, right, padre_hijo);
+
+ std::cout << "Listo, Listo!\n";
+ } else if (left != 0) {
+ } else {
+ std::cout << "PANIC : No tengo hijos para reemplazar!!!!\n";
+ }
}
void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)